From e8977170f994623631a4f5ca6bc3529ac4a6c2f0 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 29 Mar 2013 20:55:41 -0400 Subject: [PATCH] CRM-12193 - Add testGetDocument_disabled; misc cleanup ---------------------------------------- * CRM-12193: In-app fundraising for CiviCRM http://issues.civicrm.org/jira/browse/CRM-12193 --- CRM/Core/CommunityMessages.php | 33 +++++++++++-------- .../CRM/Core/CommunityMessagesTest.php | 18 +++++++--- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/CRM/Core/CommunityMessages.php b/CRM/Core/CommunityMessages.php index 85062c0b15..e0efd96a20 100644 --- a/CRM/Core/CommunityMessages.php +++ b/CRM/Core/CommunityMessages.php @@ -30,6 +30,8 @@ */ 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 */ @@ -45,13 +47,24 @@ class CRM_Core_CommunityMessages { */ 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; + } } /** @@ -60,9 +73,7 @@ class CRM_Core_CommunityMessages { * @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; } @@ -80,11 +91,12 @@ class CRM_Core_CommunityMessages { } 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; @@ -104,7 +116,7 @@ class CRM_Core_CommunityMessages { * @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; } @@ -134,11 +146,4 @@ class CRM_Core_CommunityMessages { throw new Exception('not implemented'); } - /** - * @param string $markup - * @return string - */ - public static function evalUrl($url) { - return $url; // FIXME - } } diff --git a/tests/phpunit/CRM/Core/CommunityMessagesTest.php b/tests/phpunit/CRM/Core/CommunityMessagesTest.php index 0c545dac7f..f4d154fc48 100644 --- a/tests/phpunit/CRM/Core/CommunityMessagesTest.php +++ b/tests/phpunit/CRM/Core/CommunityMessagesTest.php @@ -86,10 +86,20 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase { 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( @@ -125,7 +135,7 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase { * 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( @@ -163,7 +173,7 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase { * 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( @@ -205,7 +215,7 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase { $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( -- 2.25.1