CRM-13890 - Test fixes (unit tests were coded against the buggy behavior where real...
[civicrm-core.git] / tests / phpunit / CRM / Core / CommunityMessagesTest.php
index f4d154fc48e03cff53ce31718a66d85bbcc851b4..e6049e1b2f00dea653ea1d78b7aabfc9566351c0 100644 (file)
@@ -2,7 +2,7 @@
 
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
 
 require_once 'CiviTest/CiviUnitTestCase.php';
 class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase {
-
+  //@todo make BAO enotice compliant  & remove the line below
+  // WARNING - NEVER COPY & PASTE $_eNoticeCompliant = FALSE
+  // new test classes should be compliant.
+  public $_eNoticeCompliant = FALSE;
   /**
    * @var CRM_Utils_Cache_Interface
    */
@@ -38,47 +41,98 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase {
   /**
    * @var array list of possible web responses
    */
-  protected $webResponses;
+  protected static $webResponses = NULL;
+
+  public static function initWebResponses() {
+    if (self::$webResponses === NULL) {
+      self::$webResponses = array(
+        'http-error' => array(
+          CRM_Utils_HttpClient::STATUS_DL_ERROR,
+          NULL
+        ),
+        'bad-json' => array(
+          CRM_Utils_HttpClient::STATUS_OK,
+          '<html>this is not json!</html>'
+        ),
+        'invalid-ttl-document' => array(
+          CRM_Utils_HttpClient::STATUS_OK,
+          json_encode(array(
+            'ttl' => 'z', // not an integer!
+            'retry' => 'z', // not an integer!
+            'messages' => array(
+              array(
+                'markup' => '<h1>Invalid document</h1>',
+              ),
+            ),
+          ))
+        ),
+        'first-valid-response' => array(
+          CRM_Utils_HttpClient::STATUS_OK,
+          json_encode(array(
+            'ttl' => 600,
+            'retry' => 600,
+            'messages' => array(
+              array(
+                'markup' => '<h1>First valid response</h1>',
+              ),
+            ),
+          ))
+        ),
+        'second-valid-response' => array(
+          CRM_Utils_HttpClient::STATUS_OK,
+          json_encode(array(
+            'ttl' => 600,
+            'retry' => 600,
+            'messages' => array(
+              array(
+                'markup' => '<h1>Second valid response</h1>',
+              ),
+            ),
+          ))
+        ),
+        'two-messages' => array(
+          CRM_Utils_HttpClient::STATUS_OK,
+          json_encode(array(
+            'ttl' => 600,
+            'retry' => 600,
+            'messages' => array(
+              array(
+                'markup' => '<h1>One</h1>',
+                'components' => array('CiviMail'),
+              ),
+              array(
+                'markup' => '<h1>Two</h1>',
+                'components' => array('CiviMail'),
+              ),
+            ),
+          ))
+        ),
+        'two-messages-halfbadcomp' => array(
+          CRM_Utils_HttpClient::STATUS_OK,
+          json_encode(array(
+            'ttl' => 600,
+            'retry' => 600,
+            'messages' => array(
+              array(
+                'markup' => '<h1>One</h1>',
+                'components' => array('NotARealComponent'),
+              ),
+              array(
+                'markup' => '<h1>Two</h1>',
+                'components' => array('CiviMail'),
+              ),
+            ),
+          ))
+        ),
+      );
+    }
+    return self::$webResponses;
+  }
 
   public function setUp() {
     parent::setUp();
-
     $this->cache = new CRM_Utils_Cache_Arraycache(array());
-
-    $this->webResponses = array(
-      'http-error' => array(
-        CRM_Utils_HttpClient::STATUS_DL_ERROR,
-        NULL
-      ),
-      'bad-json' => array(
-        CRM_Utils_HttpClient::STATUS_OK,
-        '<html>this is not json!</html>'
-      ),
-      'hello-world' => array(
-        CRM_Utils_HttpClient::STATUS_OK,
-        json_encode(array(
-          'ttl' => 600,
-          'retry' => 600,
-          'messages' => array(
-            array(
-              'markup' => '<h1>Hello world</h1>',
-            ),
-          ),
-        ))
-      ),
-      'salut-a-tout' => array(
-        CRM_Utils_HttpClient::STATUS_OK,
-        json_encode(array(
-          'ttl' => 600,
-          'retry' => 600,
-          'messages' => array(
-            array(
-              'markup' => '<h1>Salut a tout</h1>',
-            ),
-          ),
-        ))
-      ),
-    );
+    self::initWebResponses();
   }
 
   public function tearDown() {
@@ -86,14 +140,38 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase {
     CRM_Utils_Time::resetTime();
   }
 
-  public function testGetDocument_disabled() {
+  /**
+   * A list of bad web-responses; in general, whenever the downloader
+   * encounters one of these bad responses, it should ignore the
+   * document, retain the old data, and retry again later.
+   *
+   * @return array
+   */
+  public function badWebResponses() {
+    self::initWebResponses();
+    $result = array(
+      array(self::$webResponses['http-error']),
+      array(self::$webResponses['bad-json']),
+      array(self::$webResponses['invalid-ttl-document']),
+    );
+    return $result;
+  }
+
+  public function testIsEnabled() {
+    $communityMessages = new CRM_Core_CommunityMessages(
+      $this->cache,
+      $this->expectNoHttpRequest()
+    );
+    $this->assertTrue($communityMessages->isEnabled());
+  }
+
+  public function testIsEnabled_false() {
     $communityMessages = new CRM_Core_CommunityMessages(
       $this->cache,
       $this->expectNoHttpRequest(),
       FALSE
     );
-    $doc = $communityMessages->getDocument();
-    $this->assertTrue(NULL === $doc);
+    $this->assertFalse($communityMessages->isEnabled());
   }
 
   /**
@@ -104,10 +182,10 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase {
     CRM_Utils_Time::setTime('2013-03-01 10:00:00');
     $communityMessages = new CRM_Core_CommunityMessages(
       $this->cache,
-      $this->expectOneHttpRequest($this->webResponses['hello-world'])
+      $this->expectOneHttpRequest(self::$webResponses['first-valid-response'])
     );
     $doc1 = $communityMessages->getDocument();
-    $this->assertEquals('<h1>Hello world</h1>', $doc1['messages'][0]['markup']);
+    $this->assertEquals('<h1>First valid response</h1>', $doc1['messages'][0]['markup']);
     $this->assertEquals(strtotime('2013-03-01 10:10:00'), $doc1['expires']);
 
     // second try, $doc1 hasn't expired yet, so still use it
@@ -117,30 +195,35 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase {
       $this->expectNoHttpRequest()
     );
     $doc2 = $communityMessages->getDocument();
-    $this->assertEquals('<h1>Hello world</h1>', $doc2['messages'][0]['markup']);
+    $this->assertEquals('<h1>First valid response</h1>', $doc2['messages'][0]['markup']);
     $this->assertEquals(strtotime('2013-03-01 10:10:00'), $doc2['expires']);
 
     // third try, $doc1 expired, update it
     CRM_Utils_Time::setTime('2013-03-01 12:00:02'); // more than 2 hours later (DEFAULT_RETRY)
     $communityMessages = new CRM_Core_CommunityMessages(
       $this->cache,
-      $this->expectOneHttpRequest($this->webResponses['salut-a-tout'])
+      $this->expectOneHttpRequest(self::$webResponses['second-valid-response'])
     );
     $doc3 = $communityMessages->getDocument();
-    $this->assertEquals('<h1>Salut a tout</h1>', $doc3['messages'][0]['markup']);
+    $this->assertEquals('<h1>Second valid response</h1>', $doc3['messages'][0]['markup']);
     $this->assertEquals(strtotime('2013-03-01 12:10:02'), $doc3['expires']);
   }
 
   /**
-   * First download attempt fails. Store the NACK and retry after
-   * the default time period (DEFAULT_RETRY).
+   * First download attempt fails (due to some bad web request).
+   * Store the NACK and retry after the default time period (DEFAULT_RETRY).
+   *
+   * @dataProvider badWebResponses
+   * @param array $badWebResponse Description of a web request that returns some kind of failure
    */
-  public function testGetDocument_NewFailure_CacheOK_UpdateOK() {
+  public function testGetDocument_NewFailure_CacheOK_UpdateOK($badWebResponse) {
+    $this->assertNotEmpty($badWebResponse);
+
     // first try, bad response
     CRM_Utils_Time::setTime('2013-03-01 10:00:00');
     $communityMessages = new CRM_Core_CommunityMessages(
       $this->cache,
-      $this->expectOneHttpRequest($this->webResponses['http-error'])
+      $this->expectOneHttpRequest($badWebResponse)
     );
     $doc1 = $communityMessages->getDocument();
     $this->assertEquals(array(), $doc1['messages']);
@@ -160,38 +243,44 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase {
     CRM_Utils_Time::setTime('2013-03-01 12:00:02'); // more than 2 hours later (DEFAULT_RETRY)
     $communityMessages = new CRM_Core_CommunityMessages(
       $this->cache,
-      $this->expectOneHttpRequest($this->webResponses['hello-world'])
+      $this->expectOneHttpRequest(self::$webResponses['first-valid-response'])
     );
     $doc3 = $communityMessages->getDocument();
-    $this->assertEquals('<h1>Hello world</h1>', $doc3['messages'][0]['markup']);
+    $this->assertEquals('<h1>First valid response</h1>', $doc3['messages'][0]['markup']);
     $this->assertTrue($doc3['expires'] > CRM_Utils_Time::getTimeRaw());
   }
 
   /**
    * First download of new doc is OK.
-   * The update fails.
-   * The failure cached.
-   * The failure eventually expires and new update succeeds.
+   * The update fails (due to some bad web response).
+   * The old data is retained in the cache.
+   * The failure eventually expires.
+   * A new update succeeds.
+   *
+   * @dataProvider badWebResponses
+   * @param array $badWebResponse Description of a web request that returns some kind of failure
    */
-  public function testGetDocument_NewOK_UpdateFailure_CacheOK_UpdateOK() {
+  public function testGetDocument_NewOK_UpdateFailure_CacheOK_UpdateOK($badWebResponse) {
+    $this->assertNotEmpty($badWebResponse);
+
     // first try, good response
     CRM_Utils_Time::setTime('2013-03-01 10:00:00');
     $communityMessages = new CRM_Core_CommunityMessages(
       $this->cache,
-      $this->expectOneHttpRequest($this->webResponses['hello-world'])
+      $this->expectOneHttpRequest(self::$webResponses['first-valid-response'])
     );
     $doc1 = $communityMessages->getDocument();
-    $this->assertEquals('<h1>Hello world</h1>', $doc1['messages'][0]['markup']);
+    $this->assertEquals('<h1>First valid response</h1>', $doc1['messages'][0]['markup']);
     $this->assertEquals(strtotime('2013-03-01 10:10:00'), $doc1['expires']);
 
     // second try, $doc1 has expired; bad response; keep old data
     CRM_Utils_Time::setTime('2013-03-01 12:00:02'); // more than 2 hours later (DEFAULT_RETRY)
     $communityMessages = new CRM_Core_CommunityMessages(
       $this->cache,
-      $this->expectOneHttpRequest($this->webResponses['http-error'])
+      $this->expectOneHttpRequest($badWebResponse)
     );
     $doc2 = $communityMessages->getDocument();
-    $this->assertEquals('<h1>Hello world</h1>', $doc2['messages'][0]['markup']);
+    $this->assertEquals('<h1>First valid response</h1>', $doc2['messages'][0]['markup']);
     $this->assertTrue($doc2['expires'] > CRM_Utils_Time::getTimeRaw());
 
     // third try, $doc2 hasn't expired yet; no request; keep old data
@@ -201,40 +290,76 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase {
       $this->expectNoHttpRequest()
     );
     $doc3 = $communityMessages->getDocument();
-    $this->assertEquals('<h1>Hello world</h1>', $doc3['messages'][0]['markup']);
+    $this->assertEquals('<h1>First valid response</h1>', $doc3['messages'][0]['markup']);
     $this->assertEquals($doc2['expires'], $doc3['expires']);
 
     // fourth try, $doc2 has expired yet; new request; replace data
     CRM_Utils_Time::setTime('2013-03-01 12:10:02');
     $communityMessages = new CRM_Core_CommunityMessages(
       $this->cache,
-      $this->expectOneHttpRequest($this->webResponses['salut-a-tout'])
+      $this->expectOneHttpRequest(self::$webResponses['second-valid-response'])
     );
     $doc4 = $communityMessages->getDocument();
-    $this->assertEquals('<h1>Salut a tout</h1>', $doc4['messages'][0]['markup']);
+    $this->assertEquals('<h1>Second valid response</h1>', $doc4['messages'][0]['markup']);
     $this->assertEquals(strtotime('2013-03-01 12:20:02'), $doc4['expires']);
   }
 
-  public function testGetDocument_NewOK_UpdateParseError() {
-    // first try, good response
-    CRM_Utils_Time::setTime('2013-03-01 10:00:00');
+  /**
+   * Randomly pick among two options
+   */
+  public function testPick_rand() {
     $communityMessages = new CRM_Core_CommunityMessages(
       $this->cache,
-      $this->expectOneHttpRequest($this->webResponses['hello-world'])
+      $this->expectOneHttpRequest(self::$webResponses['two-messages'])
     );
     $doc1 = $communityMessages->getDocument();
-    $this->assertEquals('<h1>Hello world</h1>', $doc1['messages'][0]['markup']);
-    $this->assertEquals(strtotime('2013-03-01 10:10:00'), $doc1['expires']);
+    $this->assertEquals('<h1>One</h1>', $doc1['messages'][0]['markup']);
+    $this->assertEquals('<h1>Two</h1>', $doc1['messages'][1]['markup']);
 
-    // second try, $doc1 has expired; bad response; keep old data
-    CRM_Utils_Time::setTime('2013-03-01 12:00:02'); // more than 2 hours later (DEFAULT_RETRY)
+    // randomly pick many times
+    $trials = 80;
+    $freq = array(); // array($message => $count)
+    for ($i = 0; $i < $trials; $i++) {
+      $message = $communityMessages->pick();
+      $freq[$message['markup']]++;
+    }
+
+    // assert the probabilities
+    $this->assertApproxEquals(0.5, $freq['<h1>One</h1>'] / $trials, 0.3);
+    $this->assertApproxEquals(0.5, $freq['<h1>Two</h1>'] / $trials, 0.3);
+    $this->assertEquals($trials, $freq['<h1>One</h1>'] + $freq['<h1>Two</h1>']);
+  }
+
+  /**
+   * When presented with two options using component filters, always
+   * choose the one which references an active component.
+   */
+  public function testPick_componentFilter() {
     $communityMessages = new CRM_Core_CommunityMessages(
       $this->cache,
-      $this->expectOneHttpRequest($this->webResponses['bad-json'])
+      $this->expectOneHttpRequest(self::$webResponses['two-messages-halfbadcomp'])
     );
-    $doc2 = $communityMessages->getDocument();
-    $this->assertEquals('<h1>Hello world</h1>', $doc2['messages'][0]['markup']);
-    $this->assertEquals(strtotime('2013-03-01 12:10:02'), $doc2['expires']);
+    $doc1 = $communityMessages->getDocument();
+    $this->assertEquals('<h1>One</h1>', $doc1['messages'][0]['markup']);
+    $this->assertEquals('<h1>Two</h1>', $doc1['messages'][1]['markup']);
+
+    // randomly pick many times
+    $trials = 10;
+    $freq = array(); // array($message => $count)
+    for ($i = 0; $i < $trials; $i++) {
+      $message = $communityMessages->pick();
+      $freq[$message['markup']]++;
+    }
+
+    $this->assertEquals($trials, $freq['<h1>Two</h1>']);
+  }
+
+  function testEvalMarkup() {
+    $communityMessages = new CRM_Core_CommunityMessages(
+      $this->cache,
+      $this->expectNoHttpRequest()
+    );
+    $this->assertEquals('cms=UnitTests cms=UnitTests', $communityMessages->evalMarkup('cms=%%uf%% cms={{uf}}'));
   }
 
   /**