CRM-12193 - Add testGetDocument_disabled; misc cleanup
authorTim Otten <totten@civicrm.org>
Sat, 30 Mar 2013 00:55:41 +0000 (20:55 -0400)
committerTim Otten <totten@civicrm.org>
Sat, 30 Mar 2013 00:55:41 +0000 (20:55 -0400)
----------------------------------------
* CRM-12193: In-app fundraising for CiviCRM
  http://issues.civicrm.org/jira/browse/CRM-12193

CRM/Core/CommunityMessages.php
tests/phpunit/CRM/Core/CommunityMessagesTest.php

index 85062c0b15cffe065ce8cd0f8b8466b175885359..e0efd96a20bee92913ab7acb467e9240439feed6 100644 (file)
@@ -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
-  }
 }
index 0c545dac7f06938e09092a250ad563d51e86e7db..f4d154fc48e03cff53ce31718a66d85bbcc851b4 100644 (file)
@@ -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(