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 | */ | |
41 | protected $webResponses; | |
42 | ||
43 | public function setUp() { | |
44 | parent::setUp(); | |
45 | ||
46 | $this->cache = new CRM_Utils_Cache_Arraycache(array()); | |
47 | ||
48 | $this->webResponses = array( | |
49 | 'http-error' => array( | |
50 | CRM_Utils_HttpClient::STATUS_DL_ERROR, | |
51 | NULL | |
52 | ), | |
53 | 'bad-json' => array( | |
54 | CRM_Utils_HttpClient::STATUS_OK, | |
55 | '<html>this is not json!</html>' | |
56 | ), | |
57 | 'hello-world' => array( | |
58 | CRM_Utils_HttpClient::STATUS_OK, | |
59 | json_encode(array( | |
60 | 'ttl' => 600, | |
61 | 'retry' => 600, | |
62 | 'messages' => array( | |
63 | array( | |
64 | 'markup' => '<h1>Hello world</h1>', | |
65 | ), | |
66 | ), | |
67 | )) | |
68 | ), | |
69 | 'salut-a-tout' => array( | |
70 | CRM_Utils_HttpClient::STATUS_OK, | |
71 | json_encode(array( | |
72 | 'ttl' => 600, | |
73 | 'retry' => 600, | |
74 | 'messages' => array( | |
75 | array( | |
76 | 'markup' => '<h1>Salut a tout</h1>', | |
77 | ), | |
78 | ), | |
79 | )) | |
80 | ), | |
81 | ); | |
82 | } | |
83 | ||
84 | public function tearDown() { | |
85 | parent::tearDown(); | |
86 | CRM_Utils_Time::resetTime(); | |
87 | } | |
88 | ||
89 | /** | |
90 | * Download a document; after the set expiration period, download again. | |
91 | */ | |
92 | public function testNewOK_CacheOK_UpdateOK() { | |
93 | // first try, good response | |
94 | CRM_Utils_Time::setTime('2013-03-01 10:00:00'); | |
95 | $communityMessages = new CRM_Core_CommunityMessages( | |
96 | $this->cache, | |
97 | $this->expectOneHttpRequest($this->webResponses['hello-world']) | |
98 | ); | |
99 | $doc1 = $communityMessages->getDocument(); | |
100 | $this->assertEquals('<h1>Hello world</h1>', $doc1['messages'][0]['markup']); | |
101 | $this->assertEquals(strtotime('2013-03-01 10:10:00'), $doc1['expires']); | |
102 | ||
103 | // second try, $doc1 hasn't expired yet, so still use it | |
104 | CRM_Utils_Time::setTime('2013-03-01 10:09:00'); | |
105 | $communityMessages = new CRM_Core_CommunityMessages( | |
106 | $this->cache, | |
107 | $this->expectNoHttpRequest() | |
108 | ); | |
109 | $doc2 = $communityMessages->getDocument(); | |
110 | $this->assertEquals('<h1>Hello world</h1>', $doc2['messages'][0]['markup']); | |
111 | $this->assertEquals(strtotime('2013-03-01 10:10:00'), $doc2['expires']); | |
112 | ||
113 | // third try, $doc1 expired, update it | |
114 | CRM_Utils_Time::setTime('2013-03-01 12:00:02'); // more than 2 hours later (DEFAULT_RETRY) | |
115 | $communityMessages = new CRM_Core_CommunityMessages( | |
116 | $this->cache, | |
117 | $this->expectOneHttpRequest($this->webResponses['salut-a-tout']) | |
118 | ); | |
119 | $doc3 = $communityMessages->getDocument(); | |
120 | $this->assertEquals('<h1>Salut a tout</h1>', $doc3['messages'][0]['markup']); | |
121 | $this->assertEquals(strtotime('2013-03-01 12:10:02'), $doc3['expires']); | |
122 | } | |
123 | ||
124 | /** | |
125 | * First download attempt fails. Store the NACK and retry after | |
126 | * the default time period (DEFAULT_RETRY). | |
127 | */ | |
128 | public function testNewFailure_CacheOK_UpdateOK() { | |
129 | // first try, bad response | |
130 | CRM_Utils_Time::setTime('2013-03-01 10:00:00'); | |
131 | $communityMessages = new CRM_Core_CommunityMessages( | |
132 | $this->cache, | |
133 | $this->expectOneHttpRequest($this->webResponses['http-error']) | |
134 | ); | |
135 | $doc1 = $communityMessages->getDocument(); | |
136 | $this->assertEquals(array(), $doc1['messages']); | |
137 | $this->assertTrue($doc1['expires'] > CRM_Utils_Time::getTimeRaw()); | |
138 | ||
139 | // second try, $doc1 hasn't expired yet, so still use it | |
140 | CRM_Utils_Time::setTime('2013-03-01 10:09:00'); | |
141 | $communityMessages = new CRM_Core_CommunityMessages( | |
142 | $this->cache, | |
143 | $this->expectNoHttpRequest() | |
144 | ); | |
145 | $doc2 = $communityMessages->getDocument(); | |
146 | $this->assertEquals(array(), $doc2['messages']); | |
147 | $this->assertEquals($doc1['expires'], $doc2['expires']); | |
148 | ||
149 | // third try, $doc1 expired, try again, get a good response | |
150 | CRM_Utils_Time::setTime('2013-03-01 12:00:02'); // more than 2 hours later (DEFAULT_RETRY) | |
151 | $communityMessages = new CRM_Core_CommunityMessages( | |
152 | $this->cache, | |
153 | $this->expectOneHttpRequest($this->webResponses['hello-world']) | |
154 | ); | |
155 | $doc3 = $communityMessages->getDocument(); | |
156 | $this->assertEquals('<h1>Hello world</h1>', $doc3['messages'][0]['markup']); | |
157 | $this->assertTrue($doc3['expires'] > CRM_Utils_Time::getTimeRaw()); | |
158 | } | |
159 | ||
160 | /** | |
161 | * First download of new doc is OK. | |
162 | * The update fails. | |
163 | * The failure cached. | |
164 | * The failure eventually expires and new update succeeds. | |
165 | */ | |
166 | public function testNewOK_UpdateFailure_CacheOK_UpdateOK() { | |
167 | // first try, good response | |
168 | CRM_Utils_Time::setTime('2013-03-01 10:00:00'); | |
169 | $communityMessages = new CRM_Core_CommunityMessages( | |
170 | $this->cache, | |
171 | $this->expectOneHttpRequest($this->webResponses['hello-world']) | |
172 | ); | |
173 | $doc1 = $communityMessages->getDocument(); | |
174 | $this->assertEquals('<h1>Hello world</h1>', $doc1['messages'][0]['markup']); | |
175 | $this->assertEquals(strtotime('2013-03-01 10:10:00'), $doc1['expires']); | |
176 | ||
177 | // second try, $doc1 has expired; bad response; keep old data | |
178 | CRM_Utils_Time::setTime('2013-03-01 12:00:02'); // more than 2 hours later (DEFAULT_RETRY) | |
179 | $communityMessages = new CRM_Core_CommunityMessages( | |
180 | $this->cache, | |
181 | $this->expectOneHttpRequest($this->webResponses['http-error']) | |
182 | ); | |
183 | $doc2 = $communityMessages->getDocument(); | |
184 | $this->assertEquals('<h1>Hello world</h1>', $doc2['messages'][0]['markup']); | |
185 | $this->assertTrue($doc2['expires'] > CRM_Utils_Time::getTimeRaw()); | |
186 | ||
187 | // third try, $doc2 hasn't expired yet; no request; keep old data | |
188 | CRM_Utils_Time::setTime('2013-03-01 12:09:00'); | |
189 | $communityMessages = new CRM_Core_CommunityMessages( | |
190 | $this->cache, | |
191 | $this->expectNoHttpRequest() | |
192 | ); | |
193 | $doc3 = $communityMessages->getDocument(); | |
194 | $this->assertEquals('<h1>Hello world</h1>', $doc3['messages'][0]['markup']); | |
195 | $this->assertEquals($doc2['expires'], $doc3['expires']); | |
196 | ||
197 | // fourth try, $doc2 has expired yet; new request; replace data | |
198 | CRM_Utils_Time::setTime('2013-03-01 12:10:02'); | |
199 | $communityMessages = new CRM_Core_CommunityMessages( | |
200 | $this->cache, | |
201 | $this->expectOneHttpRequest($this->webResponses['salut-a-tout']) | |
202 | ); | |
203 | $doc4 = $communityMessages->getDocument(); | |
204 | $this->assertEquals('<h1>Salut a tout</h1>', $doc4['messages'][0]['markup']); | |
205 | $this->assertEquals(strtotime('2013-03-01 12:20:02'), $doc4['expires']); | |
206 | } | |
207 | ||
208 | public function testNewOK_UpdateParseError() { | |
209 | // first try, good response | |
210 | CRM_Utils_Time::setTime('2013-03-01 10:00:00'); | |
211 | $communityMessages = new CRM_Core_CommunityMessages( | |
212 | $this->cache, | |
213 | $this->expectOneHttpRequest($this->webResponses['hello-world']) | |
214 | ); | |
215 | $doc1 = $communityMessages->getDocument(); | |
216 | $this->assertEquals('<h1>Hello world</h1>', $doc1['messages'][0]['markup']); | |
217 | $this->assertEquals(strtotime('2013-03-01 10:10:00'), $doc1['expires']); | |
218 | ||
219 | // second try, $doc1 has expired; bad response; keep old data | |
220 | CRM_Utils_Time::setTime('2013-03-01 12:00:02'); // more than 2 hours later (DEFAULT_RETRY) | |
221 | $communityMessages = new CRM_Core_CommunityMessages( | |
222 | $this->cache, | |
223 | $this->expectOneHttpRequest($this->webResponses['bad-json']) | |
224 | ); | |
225 | $doc2 = $communityMessages->getDocument(); | |
226 | $this->assertEquals('<h1>Hello world</h1>', $doc2['messages'][0]['markup']); | |
227 | $this->assertEquals(strtotime('2013-03-01 12:10:02'), $doc2['expires']); | |
228 | } | |
229 | ||
230 | /** | |
231 | * Generate a mock HTTP client with the expectation that it is never called. | |
232 | * | |
233 | * @return CRM_Utils_HttpClient|PHPUnit_Framework_MockObject_MockObject | |
234 | */ | |
235 | protected function expectNoHttpRequest() { | |
236 | $client = $this->getMock('CRM_Utils_HttpClient'); | |
237 | $client->expects($this->never()) | |
238 | ->method('get'); | |
239 | return $client; | |
240 | } | |
241 | ||
242 | /** | |
243 | * Generate a mock HTTP client with the expectation that it is called once. | |
244 | * | |
245 | * @return CRM_Utils_HttpClient|PHPUnit_Framework_MockObject_MockObject | |
246 | */ | |
247 | protected function expectOneHttpRequest($response) { | |
248 | $client = $this->getMock('CRM_Utils_HttpClient'); | |
249 | $client->expects($this->once()) | |
250 | ->method('get') | |
251 | ->will($this->returnValue($response)); | |
252 | return $client; | |
253 | } | |
254 | } |