Commit | Line | Data |
---|---|---|
ecbe1139 TO |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
5 | | CiviCRM version 4.3 | | |
6 | +--------------------------------------------------------------------+ | |
7 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
8 | +--------------------------------------------------------------------+ | |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
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. | | |
14 | | | | |
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. | | |
19 | | | | |
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 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | ||
30 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
31 | class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase { | |
32 | ||
33 | /** | |
34 | * @var CRM_Utils_Cache_Interface | |
35 | */ | |
36 | protected $cache; | |
37 | ||
38 | /** | |
39 | * @var array list of possible web responses | |
40 | */ | |
376d8353 | 41 | protected static $webResponses = NULL; |
ecbe1139 | 42 | |
376d8353 TO |
43 | public static function initWebResponses() { |
44 | if (self::$webResponses === NULL) { | |
45 | self::$webResponses = array( | |
46 | 'http-error' => array( | |
47 | CRM_Utils_HttpClient::STATUS_DL_ERROR, | |
48 | NULL | |
49 | ), | |
50 | 'bad-json' => array( | |
51 | CRM_Utils_HttpClient::STATUS_OK, | |
52 | '<html>this is not json!</html>' | |
53 | ), | |
54 | 'invalid-ttl-document' => array( | |
55 | CRM_Utils_HttpClient::STATUS_OK, | |
56 | json_encode(array( | |
57 | 'ttl' => 'z', // not an integer! | |
58 | 'retry' => 'z', // not an integer! | |
59 | 'messages' => array( | |
60 | array( | |
61 | 'markup' => '<h1>Invalid document</h1>', | |
62 | ), | |
8bfc3cb1 | 63 | ), |
376d8353 TO |
64 | )) |
65 | ), | |
66 | 'hello-world' => array( | |
67 | CRM_Utils_HttpClient::STATUS_OK, | |
68 | json_encode(array( | |
69 | 'ttl' => 600, | |
70 | 'retry' => 600, | |
71 | 'messages' => array( | |
72 | array( | |
73 | 'markup' => '<h1>Hello world</h1>', | |
74 | ), | |
ecbe1139 | 75 | ), |
376d8353 TO |
76 | )) |
77 | ), | |
78 | 'salut-a-tout' => array( | |
79 | CRM_Utils_HttpClient::STATUS_OK, | |
80 | json_encode(array( | |
81 | 'ttl' => 600, | |
82 | 'retry' => 600, | |
83 | 'messages' => array( | |
84 | array( | |
85 | 'markup' => '<h1>Salut a tout</h1>', | |
86 | ), | |
ecbe1139 | 87 | ), |
376d8353 TO |
88 | )) |
89 | ), | |
90 | ); | |
91 | } | |
92 | return self::$webResponses; | |
93 | } | |
94 | ||
95 | public function setUp() { | |
96 | parent::setUp(); | |
97 | $this->cache = new CRM_Utils_Cache_Arraycache(array()); | |
98 | self::initWebResponses(); | |
ecbe1139 TO |
99 | } |
100 | ||
101 | public function tearDown() { | |
102 | parent::tearDown(); | |
103 | CRM_Utils_Time::resetTime(); | |
104 | } | |
105 | ||
376d8353 TO |
106 | public function badWebRequests() { |
107 | self::initWebResponses(); | |
108 | $result = array( | |
109 | array(self::$webResponses['http-error']), | |
110 | array(self::$webResponses['bad-json']), | |
111 | array(self::$webResponses['invalid-ttl-document']), | |
112 | ); | |
113 | return $result; | |
114 | } | |
115 | ||
e8977170 TO |
116 | public function testGetDocument_disabled() { |
117 | $communityMessages = new CRM_Core_CommunityMessages( | |
118 | $this->cache, | |
119 | $this->expectNoHttpRequest(), | |
120 | FALSE | |
121 | ); | |
122 | $doc = $communityMessages->getDocument(); | |
123 | $this->assertTrue(NULL === $doc); | |
124 | } | |
125 | ||
ecbe1139 TO |
126 | /** |
127 | * Download a document; after the set expiration period, download again. | |
128 | */ | |
e8977170 | 129 | public function testGetDocument_NewOK_CacheOK_UpdateOK() { |
ecbe1139 TO |
130 | // first try, good response |
131 | CRM_Utils_Time::setTime('2013-03-01 10:00:00'); | |
132 | $communityMessages = new CRM_Core_CommunityMessages( | |
133 | $this->cache, | |
376d8353 | 134 | $this->expectOneHttpRequest(self::$webResponses['hello-world']) |
ecbe1139 TO |
135 | ); |
136 | $doc1 = $communityMessages->getDocument(); | |
137 | $this->assertEquals('<h1>Hello world</h1>', $doc1['messages'][0]['markup']); | |
138 | $this->assertEquals(strtotime('2013-03-01 10:10:00'), $doc1['expires']); | |
139 | ||
140 | // second try, $doc1 hasn't expired yet, so still use it | |
141 | CRM_Utils_Time::setTime('2013-03-01 10:09:00'); | |
142 | $communityMessages = new CRM_Core_CommunityMessages( | |
143 | $this->cache, | |
144 | $this->expectNoHttpRequest() | |
145 | ); | |
146 | $doc2 = $communityMessages->getDocument(); | |
147 | $this->assertEquals('<h1>Hello world</h1>', $doc2['messages'][0]['markup']); | |
148 | $this->assertEquals(strtotime('2013-03-01 10:10:00'), $doc2['expires']); | |
149 | ||
150 | // third try, $doc1 expired, update it | |
151 | CRM_Utils_Time::setTime('2013-03-01 12:00:02'); // more than 2 hours later (DEFAULT_RETRY) | |
152 | $communityMessages = new CRM_Core_CommunityMessages( | |
153 | $this->cache, | |
376d8353 | 154 | $this->expectOneHttpRequest(self::$webResponses['salut-a-tout']) |
ecbe1139 TO |
155 | ); |
156 | $doc3 = $communityMessages->getDocument(); | |
157 | $this->assertEquals('<h1>Salut a tout</h1>', $doc3['messages'][0]['markup']); | |
158 | $this->assertEquals(strtotime('2013-03-01 12:10:02'), $doc3['expires']); | |
159 | } | |
160 | ||
161 | /** | |
376d8353 TO |
162 | * First download attempt fails (due to some bad web request). |
163 | * Store the NACK and retry after the default time period (DEFAULT_RETRY). | |
164 | * | |
165 | * @dataProvider badWebRequests | |
ecbe1139 | 166 | */ |
376d8353 TO |
167 | public function testGetDocument_NewFailure_CacheOK_UpdateOK($badWebRequest) { |
168 | $this->assertNotEmpty($badWebRequest); | |
169 | ||
ecbe1139 TO |
170 | // first try, bad response |
171 | CRM_Utils_Time::setTime('2013-03-01 10:00:00'); | |
172 | $communityMessages = new CRM_Core_CommunityMessages( | |
173 | $this->cache, | |
376d8353 | 174 | $this->expectOneHttpRequest($badWebRequest) |
ecbe1139 TO |
175 | ); |
176 | $doc1 = $communityMessages->getDocument(); | |
177 | $this->assertEquals(array(), $doc1['messages']); | |
178 | $this->assertTrue($doc1['expires'] > CRM_Utils_Time::getTimeRaw()); | |
179 | ||
180 | // second try, $doc1 hasn't expired yet, so still use it | |
181 | CRM_Utils_Time::setTime('2013-03-01 10:09:00'); | |
182 | $communityMessages = new CRM_Core_CommunityMessages( | |
183 | $this->cache, | |
184 | $this->expectNoHttpRequest() | |
185 | ); | |
186 | $doc2 = $communityMessages->getDocument(); | |
187 | $this->assertEquals(array(), $doc2['messages']); | |
188 | $this->assertEquals($doc1['expires'], $doc2['expires']); | |
189 | ||
190 | // third try, $doc1 expired, try again, get a good response | |
191 | CRM_Utils_Time::setTime('2013-03-01 12:00:02'); // more than 2 hours later (DEFAULT_RETRY) | |
192 | $communityMessages = new CRM_Core_CommunityMessages( | |
193 | $this->cache, | |
376d8353 | 194 | $this->expectOneHttpRequest(self::$webResponses['hello-world']) |
ecbe1139 TO |
195 | ); |
196 | $doc3 = $communityMessages->getDocument(); | |
197 | $this->assertEquals('<h1>Hello world</h1>', $doc3['messages'][0]['markup']); | |
198 | $this->assertTrue($doc3['expires'] > CRM_Utils_Time::getTimeRaw()); | |
199 | } | |
200 | ||
201 | /** | |
202 | * First download of new doc is OK. | |
376d8353 | 203 | * The update fails (due to some bad web request) |
ecbe1139 TO |
204 | * The failure cached. |
205 | * The failure eventually expires and new update succeeds. | |
376d8353 TO |
206 | * |
207 | * @dataProvider badWebRequests | |
ecbe1139 | 208 | */ |
376d8353 TO |
209 | public function testGetDocument_NewOK_UpdateFailure_CacheOK_UpdateOK($badWebRequest) { |
210 | $this->assertNotEmpty($badWebRequest); | |
211 | ||
ecbe1139 TO |
212 | // first try, good response |
213 | CRM_Utils_Time::setTime('2013-03-01 10:00:00'); | |
214 | $communityMessages = new CRM_Core_CommunityMessages( | |
215 | $this->cache, | |
376d8353 | 216 | $this->expectOneHttpRequest(self::$webResponses['hello-world']) |
ecbe1139 TO |
217 | ); |
218 | $doc1 = $communityMessages->getDocument(); | |
219 | $this->assertEquals('<h1>Hello world</h1>', $doc1['messages'][0]['markup']); | |
220 | $this->assertEquals(strtotime('2013-03-01 10:10:00'), $doc1['expires']); | |
221 | ||
222 | // second try, $doc1 has expired; bad response; keep old data | |
223 | CRM_Utils_Time::setTime('2013-03-01 12:00:02'); // more than 2 hours later (DEFAULT_RETRY) | |
224 | $communityMessages = new CRM_Core_CommunityMessages( | |
225 | $this->cache, | |
376d8353 | 226 | $this->expectOneHttpRequest($badWebRequest) |
ecbe1139 TO |
227 | ); |
228 | $doc2 = $communityMessages->getDocument(); | |
229 | $this->assertEquals('<h1>Hello world</h1>', $doc2['messages'][0]['markup']); | |
230 | $this->assertTrue($doc2['expires'] > CRM_Utils_Time::getTimeRaw()); | |
231 | ||
232 | // third try, $doc2 hasn't expired yet; no request; keep old data | |
233 | CRM_Utils_Time::setTime('2013-03-01 12:09:00'); | |
234 | $communityMessages = new CRM_Core_CommunityMessages( | |
235 | $this->cache, | |
236 | $this->expectNoHttpRequest() | |
237 | ); | |
238 | $doc3 = $communityMessages->getDocument(); | |
239 | $this->assertEquals('<h1>Hello world</h1>', $doc3['messages'][0]['markup']); | |
240 | $this->assertEquals($doc2['expires'], $doc3['expires']); | |
241 | ||
242 | // fourth try, $doc2 has expired yet; new request; replace data | |
243 | CRM_Utils_Time::setTime('2013-03-01 12:10:02'); | |
244 | $communityMessages = new CRM_Core_CommunityMessages( | |
245 | $this->cache, | |
376d8353 | 246 | $this->expectOneHttpRequest(self::$webResponses['salut-a-tout']) |
ecbe1139 TO |
247 | ); |
248 | $doc4 = $communityMessages->getDocument(); | |
249 | $this->assertEquals('<h1>Salut a tout</h1>', $doc4['messages'][0]['markup']); | |
250 | $this->assertEquals(strtotime('2013-03-01 12:20:02'), $doc4['expires']); | |
251 | } | |
252 | ||
ecbe1139 TO |
253 | /** |
254 | * Generate a mock HTTP client with the expectation that it is never called. | |
255 | * | |
256 | * @return CRM_Utils_HttpClient|PHPUnit_Framework_MockObject_MockObject | |
257 | */ | |
258 | protected function expectNoHttpRequest() { | |
259 | $client = $this->getMock('CRM_Utils_HttpClient'); | |
260 | $client->expects($this->never()) | |
261 | ->method('get'); | |
262 | return $client; | |
263 | } | |
264 | ||
265 | /** | |
266 | * Generate a mock HTTP client with the expectation that it is called once. | |
267 | * | |
268 | * @return CRM_Utils_HttpClient|PHPUnit_Framework_MockObject_MockObject | |
269 | */ | |
270 | protected function expectOneHttpRequest($response) { | |
271 | $client = $this->getMock('CRM_Utils_HttpClient'); | |
272 | $client->expects($this->once()) | |
273 | ->method('get') | |
274 | ->will($this->returnValue($response)); | |
275 | return $client; | |
276 | } | |
277 | } |