*/
class CRM_Core_CommunityMessages {
+ const DEFAULT_MESSAGES_URL = 'http://alert.civicrm.org/alert?prot=1&ver={ver}&uf={uf}&sid={sid}';
+
/**
* Default time to wait before retrying
*/
*/
protected $cache;
+ /**
+ * @var FALSE|string
+ */
+ protected $messagesUrl;
+
/**
* @param CRM_Utils_Cache_Interface $cache
* @param CRM_Utils_HttpClient $client
*/
- public function __construct($cache, $client) {
+ public function __construct($cache, $client, $messagesUrl = NULL) {
$this->cache = $cache;
$this->client = $client;
+ if ($messagesUrl === NULL) {
+ $this->messagesUrl = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'community_messages_url', NULL, self::DEFAULT_MESSAGES_URL);
+ }
+ else {
+ $this->messagesUrl = $messagesUrl;
+ }
}
/**
* @return NULL|array
*/
public function getDocument() {
- // FIXME register in settings
- $url = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'communityMessagesUrl', NULL, TRUE);
- if (empty($url)) {
+ if ($this->messagesUrl === FALSE) {
return NULL;
}
}
if ($document['expires'] <= CRM_Utils_Time::getTimeRaw()) {
- $newDocument = $this->fetchDocument($url);
+ $newDocument = $this->fetchDocument($this->messagesUrl);
if ($newDocument) {
$document = $newDocument;
$document['expires'] = CRM_Utils_Time::getTimeRaw() + $document['ttl'];
- } else {
+ }
+ else {
$document['expires'] = CRM_Utils_Time::getTimeRaw() + $document['retry'];
}
$isChanged = TRUE;
* @return NULL|array parsed JSON
*/
public function fetchDocument($url) {
- list($status, $json) = $this->client->get(self::evalUrl($url));
+ list($status, $json) = $this->client->get(CRM_Utils_System::evalUrl($url));
if ($status != CRM_Utils_HttpClient::STATUS_OK || empty($json)) {
return NULL;
}
throw new Exception('not implemented');
}
- /**
- * @param string $markup
- * @return string
- */
- public static function evalUrl($url) {
- return $url; // FIXME
- }
}
CRM_Utils_Time::resetTime();
}
+ public function testGetDocument_disabled() {
+ $communityMessages = new CRM_Core_CommunityMessages(
+ $this->cache,
+ $this->expectNoHttpRequest(),
+ FALSE
+ );
+ $doc = $communityMessages->getDocument();
+ $this->assertTrue(NULL === $doc);
+ }
+
/**
* Download a document; after the set expiration period, download again.
*/
- public function testNewOK_CacheOK_UpdateOK() {
+ public function testGetDocument_NewOK_CacheOK_UpdateOK() {
// first try, good response
CRM_Utils_Time::setTime('2013-03-01 10:00:00');
$communityMessages = new CRM_Core_CommunityMessages(
* First download attempt fails. Store the NACK and retry after
* the default time period (DEFAULT_RETRY).
*/
- public function testNewFailure_CacheOK_UpdateOK() {
+ public function testGetDocument_NewFailure_CacheOK_UpdateOK() {
// first try, bad response
CRM_Utils_Time::setTime('2013-03-01 10:00:00');
$communityMessages = new CRM_Core_CommunityMessages(
* The failure cached.
* The failure eventually expires and new update succeeds.
*/
- public function testNewOK_UpdateFailure_CacheOK_UpdateOK() {
+ public function testGetDocument_NewOK_UpdateFailure_CacheOK_UpdateOK() {
// first try, good response
CRM_Utils_Time::setTime('2013-03-01 10:00:00');
$communityMessages = new CRM_Core_CommunityMessages(
$this->assertEquals(strtotime('2013-03-01 12:20:02'), $doc4['expires']);
}
- public function testNewOK_UpdateParseError() {
+ public function testGetDocument_NewOK_UpdateParseError() {
// first try, good response
CRM_Utils_Time::setTime('2013-03-01 10:00:00');
$communityMessages = new CRM_Core_CommunityMessages(