4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
30 require_once 'CiviTest/CiviUnitTestCase.php';
31 class CRM_Core_CommunityMessagesTest
extends CiviUnitTestCase
{
32 //@todo make BAO enotice compliant & remove the line below
33 // WARNING - NEVER COPY & PASTE $_eNoticeCompliant = FALSE
34 // new test classes should be compliant.
35 public $_eNoticeCompliant = FALSE;
37 * @var CRM_Utils_Cache_Interface
42 * @var array list of possible web responses
44 protected static $webResponses = NULL;
46 public static function initWebResponses() {
47 if (self
::$webResponses === NULL) {
48 self
::$webResponses = array(
49 'http-error' => array(
50 CRM_Utils_HttpClient
::STATUS_DL_ERROR
,
54 CRM_Utils_HttpClient
::STATUS_OK
,
55 '<html>this is not json!</html>'
57 'invalid-ttl-document' => array(
58 CRM_Utils_HttpClient
::STATUS_OK
,
60 'ttl' => 'z', // not an integer!
61 'retry' => 'z', // not an integer!
64 'markup' => '<h1>Invalid document</h1>',
69 'first-valid-response' => array(
70 CRM_Utils_HttpClient
::STATUS_OK
,
76 'markup' => '<h1>First valid response</h1>',
81 'second-valid-response' => array(
82 CRM_Utils_HttpClient
::STATUS_OK
,
88 'markup' => '<h1>Second valid response</h1>',
93 'two-messages' => array(
94 CRM_Utils_HttpClient
::STATUS_OK
,
100 'markup' => '<h1>One</h1>',
101 'components' => array('CiviMail'),
104 'markup' => '<h1>Two</h1>',
105 'components' => array('CiviMail'),
110 'two-messages-halfbadcomp' => array(
111 CRM_Utils_HttpClient
::STATUS_OK
,
117 'markup' => '<h1>One</h1>',
118 'components' => array('NotARealComponent'),
121 'markup' => '<h1>Two</h1>',
122 'components' => array('CiviMail'),
129 return self
::$webResponses;
132 public function setUp() {
134 $this->cache
= new CRM_Utils_Cache_Arraycache(array());
135 self
::initWebResponses();
138 public function tearDown() {
140 CRM_Utils_Time
::resetTime();
144 * A list of bad web-responses; in general, whenever the downloader
145 * encounters one of these bad responses, it should ignore the
146 * document, retain the old data, and retry again later.
150 public function badWebResponses() {
151 self
::initWebResponses();
153 array(self
::$webResponses['http-error']),
154 array(self
::$webResponses['bad-json']),
155 array(self
::$webResponses['invalid-ttl-document']),
160 public function testIsEnabled() {
161 $communityMessages = new CRM_Core_CommunityMessages(
163 $this->expectNoHttpRequest()
165 $this->assertTrue($communityMessages->isEnabled());
168 public function testIsEnabled_false() {
169 $communityMessages = new CRM_Core_CommunityMessages(
171 $this->expectNoHttpRequest(),
174 $this->assertFalse($communityMessages->isEnabled());
178 * Download a document; after the set expiration period, download again.
180 public function testGetDocument_NewOK_CacheOK_UpdateOK() {
181 // first try, good response
182 CRM_Utils_Time
::setTime('2013-03-01 10:00:00');
183 $communityMessages = new CRM_Core_CommunityMessages(
185 $this->expectOneHttpRequest(self
::$webResponses['first-valid-response'])
187 $doc1 = $communityMessages->getDocument();
188 $this->assertEquals('<h1>First valid response</h1>', $doc1['messages'][0]['markup']);
189 $this->assertEquals(strtotime('2013-03-01 10:10:00'), $doc1['expires']);
191 // second try, $doc1 hasn't expired yet, so still use it
192 CRM_Utils_Time
::setTime('2013-03-01 10:09:00');
193 $communityMessages = new CRM_Core_CommunityMessages(
195 $this->expectNoHttpRequest()
197 $doc2 = $communityMessages->getDocument();
198 $this->assertEquals('<h1>First valid response</h1>', $doc2['messages'][0]['markup']);
199 $this->assertEquals(strtotime('2013-03-01 10:10:00'), $doc2['expires']);
201 // third try, $doc1 expired, update it
202 CRM_Utils_Time
::setTime('2013-03-01 12:00:02'); // more than 2 hours later (DEFAULT_RETRY)
203 $communityMessages = new CRM_Core_CommunityMessages(
205 $this->expectOneHttpRequest(self
::$webResponses['second-valid-response'])
207 $doc3 = $communityMessages->getDocument();
208 $this->assertEquals('<h1>Second valid response</h1>', $doc3['messages'][0]['markup']);
209 $this->assertEquals(strtotime('2013-03-01 12:10:02'), $doc3['expires']);
213 * First download attempt fails (due to some bad web request).
214 * Store the NACK and retry after the default time period (DEFAULT_RETRY).
216 * @dataProvider badWebResponses
217 * @param array $badWebResponse Description of a web request that returns some kind of failure
219 public function testGetDocument_NewFailure_CacheOK_UpdateOK($badWebResponse) {
220 $this->assertNotEmpty($badWebResponse);
222 // first try, bad response
223 CRM_Utils_Time
::setTime('2013-03-01 10:00:00');
224 $communityMessages = new CRM_Core_CommunityMessages(
226 $this->expectOneHttpRequest($badWebResponse)
228 $doc1 = $communityMessages->getDocument();
229 $this->assertEquals(array(), $doc1['messages']);
230 $this->assertTrue($doc1['expires'] > CRM_Utils_Time
::getTimeRaw());
232 // second try, $doc1 hasn't expired yet, so still use it
233 CRM_Utils_Time
::setTime('2013-03-01 10:09:00');
234 $communityMessages = new CRM_Core_CommunityMessages(
236 $this->expectNoHttpRequest()
238 $doc2 = $communityMessages->getDocument();
239 $this->assertEquals(array(), $doc2['messages']);
240 $this->assertEquals($doc1['expires'], $doc2['expires']);
242 // third try, $doc1 expired, try again, get a good response
243 CRM_Utils_Time
::setTime('2013-03-01 12:00:02'); // more than 2 hours later (DEFAULT_RETRY)
244 $communityMessages = new CRM_Core_CommunityMessages(
246 $this->expectOneHttpRequest(self
::$webResponses['first-valid-response'])
248 $doc3 = $communityMessages->getDocument();
249 $this->assertEquals('<h1>First valid response</h1>', $doc3['messages'][0]['markup']);
250 $this->assertTrue($doc3['expires'] > CRM_Utils_Time
::getTimeRaw());
254 * First download of new doc is OK.
255 * The update fails (due to some bad web response).
256 * The old data is retained in the cache.
257 * The failure eventually expires.
258 * A new update succeeds.
260 * @dataProvider badWebResponses
261 * @param array $badWebResponse Description of a web request that returns some kind of failure
263 public function testGetDocument_NewOK_UpdateFailure_CacheOK_UpdateOK($badWebResponse) {
264 $this->assertNotEmpty($badWebResponse);
266 // first try, good response
267 CRM_Utils_Time
::setTime('2013-03-01 10:00:00');
268 $communityMessages = new CRM_Core_CommunityMessages(
270 $this->expectOneHttpRequest(self
::$webResponses['first-valid-response'])
272 $doc1 = $communityMessages->getDocument();
273 $this->assertEquals('<h1>First valid response</h1>', $doc1['messages'][0]['markup']);
274 $this->assertEquals(strtotime('2013-03-01 10:10:00'), $doc1['expires']);
276 // second try, $doc1 has expired; bad response; keep old data
277 CRM_Utils_Time
::setTime('2013-03-01 12:00:02'); // more than 2 hours later (DEFAULT_RETRY)
278 $communityMessages = new CRM_Core_CommunityMessages(
280 $this->expectOneHttpRequest($badWebResponse)
282 $doc2 = $communityMessages->getDocument();
283 $this->assertEquals('<h1>First valid response</h1>', $doc2['messages'][0]['markup']);
284 $this->assertTrue($doc2['expires'] > CRM_Utils_Time
::getTimeRaw());
286 // third try, $doc2 hasn't expired yet; no request; keep old data
287 CRM_Utils_Time
::setTime('2013-03-01 12:09:00');
288 $communityMessages = new CRM_Core_CommunityMessages(
290 $this->expectNoHttpRequest()
292 $doc3 = $communityMessages->getDocument();
293 $this->assertEquals('<h1>First valid response</h1>', $doc3['messages'][0]['markup']);
294 $this->assertEquals($doc2['expires'], $doc3['expires']);
296 // fourth try, $doc2 has expired yet; new request; replace data
297 CRM_Utils_Time
::setTime('2013-03-01 12:10:02');
298 $communityMessages = new CRM_Core_CommunityMessages(
300 $this->expectOneHttpRequest(self
::$webResponses['second-valid-response'])
302 $doc4 = $communityMessages->getDocument();
303 $this->assertEquals('<h1>Second valid response</h1>', $doc4['messages'][0]['markup']);
304 $this->assertEquals(strtotime('2013-03-01 12:20:02'), $doc4['expires']);
308 * Randomly pick among two options
310 public function testPick_rand() {
311 $communityMessages = new CRM_Core_CommunityMessages(
313 $this->expectOneHttpRequest(self
::$webResponses['two-messages'])
315 $doc1 = $communityMessages->getDocument();
316 $this->assertEquals('<h1>One</h1>', $doc1['messages'][0]['markup']);
317 $this->assertEquals('<h1>Two</h1>', $doc1['messages'][1]['markup']);
319 // randomly pick many times
321 $freq = array(); // array($message => $count)
322 for ($i = 0; $i < $trials; $i++
) {
323 $message = $communityMessages->pick();
324 $freq[$message['markup']]++
;
327 // assert the probabilities
328 $this->assertApproxEquals(0.5, $freq['<h1>One</h1>'] / $trials, 0.3);
329 $this->assertApproxEquals(0.5, $freq['<h1>Two</h1>'] / $trials, 0.3);
330 $this->assertEquals($trials, $freq['<h1>One</h1>'] +
$freq['<h1>Two</h1>']);
334 * When presented with two options using component filters, always
335 * choose the one which references an active component.
337 public function testPick_componentFilter() {
338 $communityMessages = new CRM_Core_CommunityMessages(
340 $this->expectOneHttpRequest(self
::$webResponses['two-messages-halfbadcomp'])
342 $doc1 = $communityMessages->getDocument();
343 $this->assertEquals('<h1>One</h1>', $doc1['messages'][0]['markup']);
344 $this->assertEquals('<h1>Two</h1>', $doc1['messages'][1]['markup']);
346 // randomly pick many times
348 $freq = array(); // array($message => $count)
349 for ($i = 0; $i < $trials; $i++
) {
350 $message = $communityMessages->pick();
351 $freq[$message['markup']]++
;
354 $this->assertEquals($trials, $freq['<h1>Two</h1>']);
357 function testEvalMarkup() {
358 $communityMessages = new CRM_Core_CommunityMessages(
360 $this->expectNoHttpRequest()
362 $this->assertEquals('cms=UnitTests cms=UnitTests', $communityMessages->evalMarkup('cms=%%uf%% cms={{uf}}'));
366 * Generate a mock HTTP client with the expectation that it is never called.
368 * @return CRM_Utils_HttpClient|PHPUnit_Framework_MockObject_MockObject
370 protected function expectNoHttpRequest() {
371 $client = $this->getMock('CRM_Utils_HttpClient');
372 $client->expects($this->never())
378 * Generate a mock HTTP client with the expectation that it is called once.
380 * @return CRM_Utils_HttpClient|PHPUnit_Framework_MockObject_MockObject
382 protected function expectOneHttpRequest($response) {
383 $client = $this->getMock('CRM_Utils_HttpClient');
384 $client->expects($this->once())
386 ->will($this->returnValue($response));