Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
b6708aeb | 2 | /* |
3 | +--------------------------------------------------------------------+ | |
7d61e75f TO |
4 | | Copyright CiviCRM LLC. All rights reserved. | |
5 | | | | |
6 | | This work is published under the GNU AGPLv3 license with some | | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
9 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 10 | */ |
6a488035 | 11 | |
e9479dcf EM |
12 | /** |
13 | * Class CRM_Extension_ManagerTest | |
acb109b7 | 14 | * @group headless |
e9479dcf | 15 | */ |
6a488035 TO |
16 | class CRM_Extension_ManagerTest extends CiviUnitTestCase { |
17 | const TESTING_TYPE = 'report'; | |
18 | const OTHER_TESTING_TYPE = 'module'; | |
19 | ||
00be9182 | 20 | public function setUp() { |
6a488035 TO |
21 | parent::setUp(); |
22 | list ($this->basedir, $this->container) = $this->_createContainer(); | |
23 | $this->mapper = new CRM_Extension_Mapper($this->container); | |
24 | } | |
25 | ||
00be9182 | 26 | public function tearDown() { |
6a488035 TO |
27 | parent::tearDown(); |
28 | } | |
29 | ||
30 | /** | |
eceb18cc | 31 | * Install an extension with an invalid type name. |
6a488035 TO |
32 | * |
33 | * @expectedException CRM_Extension_Exception | |
34 | */ | |
00be9182 | 35 | public function testInstallInvalidType() { |
dcadbba4 SL |
36 | $mockFunction = $this->mockMethod; |
37 | $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface'); | |
6a488035 TO |
38 | $testingTypeManager->expects($this->never()) |
39 | ->method('onPreInstall'); | |
9099cab3 | 40 | $manager = $this->_createManager([ |
6a488035 | 41 | self::OTHER_TESTING_TYPE => $testingTypeManager, |
9099cab3 CW |
42 | ]); |
43 | $manager->install(['test.foo.bar']); | |
6a488035 TO |
44 | } |
45 | ||
46 | /** | |
eceb18cc | 47 | * Install an extension with a valid type name. |
6a488035 TO |
48 | * |
49 | * Note: We initially install two extensions but then toggle only | |
50 | * the second. This controls for bad SQL queries which hit either | |
51 | * "the first row" or "all rows". | |
52 | */ | |
00be9182 | 53 | public function testInstall_Disable_Uninstall() { |
dcadbba4 SL |
54 | $mockFunction = $this->mockMethod; |
55 | $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface'); | |
9099cab3 | 56 | $manager = $this->_createManager([ |
6a488035 | 57 | self::TESTING_TYPE => $testingTypeManager, |
9099cab3 | 58 | ]); |
6a488035 TO |
59 | $this->assertEquals('uninstalled', $manager->getStatus('test.foo.bar')); |
60 | $this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang')); | |
61 | ||
62 | $testingTypeManager | |
63 | ->expects($this->exactly(2)) | |
64 | ->method('onPreInstall'); | |
65 | $testingTypeManager | |
66 | ->expects($this->exactly(2)) | |
67 | ->method('onPostInstall'); | |
9099cab3 | 68 | $manager->install(['test.whiz.bang', 'test.foo.bar']); |
6a488035 TO |
69 | $this->assertEquals('installed', $manager->getStatus('test.foo.bar')); |
70 | $this->assertEquals('installed', $manager->getStatus('test.whiz.bang')); | |
71 | ||
72 | $testingTypeManager | |
73 | ->expects($this->once()) | |
74 | ->method('onPreDisable'); | |
75 | $testingTypeManager | |
76 | ->expects($this->once()) | |
77 | ->method('onPostDisable'); | |
9099cab3 | 78 | $manager->disable(['test.foo.bar']); |
6a488035 | 79 | $this->assertEquals('disabled', $manager->getStatus('test.foo.bar')); |
39b959db SL |
80 | // no side-effect |
81 | $this->assertEquals('installed', $manager->getStatus('test.whiz.bang')); | |
6a488035 TO |
82 | |
83 | $testingTypeManager | |
84 | ->expects($this->once()) | |
85 | ->method('onPreUninstall'); | |
86 | $testingTypeManager | |
87 | ->expects($this->once()) | |
88 | ->method('onPostUninstall'); | |
9099cab3 | 89 | $manager->uninstall(['test.foo.bar']); |
6a488035 | 90 | $this->assertEquals('uninstalled', $manager->getStatus('test.foo.bar')); |
39b959db SL |
91 | // no side-effect |
92 | $this->assertEquals('installed', $manager->getStatus('test.whiz.bang')); | |
6a488035 TO |
93 | } |
94 | ||
f8a7cfff TO |
95 | /** |
96 | * This is the same as testInstall_Disable_Uninstall, but we also install and remove a dependency. | |
97 | * | |
98 | * @throws \CRM_Extension_Exception | |
99 | */ | |
100 | public function test_InstallAuto_DisableDownstream_UninstallDownstream() { | |
dcadbba4 SL |
101 | $mockFunction = $this->mockMethod; |
102 | $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface'); | |
9099cab3 | 103 | $manager = $this->_createManager([ |
f8a7cfff | 104 | self::TESTING_TYPE => $testingTypeManager, |
9099cab3 | 105 | ]); |
f8a7cfff TO |
106 | $this->assertEquals('uninstalled', $manager->getStatus('test.foo.bar')); |
107 | $this->assertEquals('uninstalled', $manager->getStatus('test.foo.downstream')); | |
108 | $this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang')); | |
109 | ||
110 | $testingTypeManager->expects($this->exactly(2))->method('onPreInstall'); | |
111 | $testingTypeManager->expects($this->exactly(2))->method('onPostInstall'); | |
9099cab3 CW |
112 | $this->assertEquals(['test.foo.bar', 'test.foo.downstream'], |
113 | $manager->findInstallRequirements(['test.foo.downstream'])); | |
f8a7cfff | 114 | $manager->install( |
9099cab3 | 115 | $manager->findInstallRequirements(['test.foo.downstream'])); |
f8a7cfff TO |
116 | $this->assertEquals('installed', $manager->getStatus('test.foo.bar')); |
117 | $this->assertEquals('installed', $manager->getStatus('test.foo.downstream')); | |
118 | $this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang')); | |
119 | ||
120 | $testingTypeManager->expects($this->once())->method('onPreDisable'); | |
121 | $testingTypeManager->expects($this->once())->method('onPostDisable'); | |
9099cab3 CW |
122 | $this->assertEquals(['test.foo.downstream'], |
123 | $manager->findDisableRequirements(['test.foo.downstream'])); | |
124 | $manager->disable(['test.foo.downstream']); | |
f8a7cfff TO |
125 | $this->assertEquals('installed', $manager->getStatus('test.foo.bar')); |
126 | $this->assertEquals('disabled', $manager->getStatus('test.foo.downstream')); | |
127 | $this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang')); | |
128 | ||
129 | $testingTypeManager->expects($this->once())->method('onPreUninstall'); | |
130 | $testingTypeManager->expects($this->once())->method('onPostUninstall'); | |
9099cab3 | 131 | $manager->uninstall(['test.foo.downstream']); |
f8a7cfff TO |
132 | $this->assertEquals('installed', $manager->getStatus('test.foo.bar')); |
133 | $this->assertEquals('uninstalled', $manager->getStatus('test.foo.downstream')); | |
134 | $this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang')); | |
135 | } | |
136 | ||
ca29efdc TO |
137 | /** |
138 | * This is the same as testInstallAuto_Twice | |
139 | * | |
140 | * @throws \CRM_Extension_Exception | |
141 | */ | |
142 | public function testInstallAuto_Twice() { | |
dcadbba4 SL |
143 | $mockFunction = $this->mockMethod; |
144 | $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface'); | |
9099cab3 | 145 | $manager = $this->_createManager([ |
ca29efdc | 146 | self::TESTING_TYPE => $testingTypeManager, |
9099cab3 | 147 | ]); |
ca29efdc TO |
148 | $this->assertEquals('uninstalled', $manager->getStatus('test.foo.bar')); |
149 | $this->assertEquals('uninstalled', $manager->getStatus('test.foo.downstream')); | |
150 | $this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang')); | |
151 | ||
152 | $testingTypeManager->expects($this->exactly(2))->method('onPreInstall'); | |
153 | $testingTypeManager->expects($this->exactly(2))->method('onPostInstall'); | |
9099cab3 CW |
154 | $this->assertEquals(['test.foo.bar', 'test.foo.downstream'], |
155 | $manager->findInstallRequirements(['test.foo.downstream'])); | |
ca29efdc | 156 | $manager->install( |
9099cab3 | 157 | $manager->findInstallRequirements(['test.foo.downstream'])); |
ca29efdc TO |
158 | $this->assertEquals('installed', $manager->getStatus('test.foo.bar')); |
159 | $this->assertEquals('installed', $manager->getStatus('test.foo.downstream')); | |
160 | $this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang')); | |
161 | ||
162 | // And install a second time... | |
163 | $testingTypeManager->expects($this->exactly(0))->method('onPreInstall'); | |
164 | $testingTypeManager->expects($this->exactly(0))->method('onPostInstall'); | |
165 | $manager->install( | |
9099cab3 | 166 | $manager->findInstallRequirements(['test.foo.downstream'])); |
ca29efdc TO |
167 | $this->assertEquals('installed', $manager->getStatus('test.foo.bar')); |
168 | $this->assertEquals('installed', $manager->getStatus('test.foo.downstream')); | |
169 | $this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang')); | |
170 | } | |
171 | ||
f8a7cfff | 172 | public function test_InstallAuto_DisableUpstream() { |
dcadbba4 SL |
173 | $mockFunction = $this->mockMethod; |
174 | $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface'); | |
9099cab3 | 175 | $manager = $this->_createManager([ |
f8a7cfff | 176 | self::TESTING_TYPE => $testingTypeManager, |
9099cab3 | 177 | ]); |
f8a7cfff TO |
178 | $this->assertEquals('uninstalled', $manager->getStatus('test.foo.bar')); |
179 | $this->assertEquals('uninstalled', $manager->getStatus('test.foo.downstream')); | |
180 | $this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang')); | |
181 | ||
182 | $testingTypeManager->expects($this->exactly(2))->method('onPreInstall'); | |
183 | $testingTypeManager->expects($this->exactly(2))->method('onPostInstall'); | |
9099cab3 CW |
184 | $this->assertEquals(['test.foo.bar', 'test.foo.downstream'], |
185 | $manager->findInstallRequirements(['test.foo.downstream'])); | |
f8a7cfff | 186 | $manager->install( |
9099cab3 | 187 | $manager->findInstallRequirements(['test.foo.downstream'])); |
f8a7cfff TO |
188 | $this->assertEquals('installed', $manager->getStatus('test.foo.bar')); |
189 | $this->assertEquals('installed', $manager->getStatus('test.foo.downstream')); | |
190 | $this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang')); | |
191 | ||
192 | $testingTypeManager->expects($this->never())->method('onPreDisable'); | |
193 | $testingTypeManager->expects($this->never())->method('onPostDisable'); | |
9099cab3 CW |
194 | $this->assertEquals(['test.foo.downstream', 'test.foo.bar'], |
195 | $manager->findDisableRequirements(['test.foo.bar'])); | |
f8a7cfff TO |
196 | |
197 | try { | |
9099cab3 | 198 | $manager->disable(['test.foo.bar']); |
f8a7cfff TO |
199 | $this->fail('Expected disable to fail due to dependency'); |
200 | } | |
201 | catch (CRM_Extension_Exception $e) { | |
202 | $this->assertRegExp('/test.foo.downstream/', $e->getMessage()); | |
203 | } | |
204 | ||
205 | // Status unchanged | |
206 | $this->assertEquals('installed', $manager->getStatus('test.foo.bar')); | |
207 | $this->assertEquals('installed', $manager->getStatus('test.foo.downstream')); | |
208 | $this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang')); | |
209 | } | |
210 | ||
6a488035 TO |
211 | /** |
212 | * Install an extension and then harshly remove the underlying source. | |
213 | * Subseuently disable and uninstall. | |
214 | */ | |
00be9182 | 215 | public function testInstall_DirtyRemove_Disable_Uninstall() { |
dcadbba4 SL |
216 | $mockFunction = $this->mockMethod; |
217 | $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface'); | |
9099cab3 | 218 | $manager = $this->_createManager([ |
6a488035 | 219 | self::TESTING_TYPE => $testingTypeManager, |
9099cab3 | 220 | ]); |
6a488035 TO |
221 | $this->assertEquals('uninstalled', $manager->getStatus('test.foo.bar')); |
222 | ||
9099cab3 | 223 | $manager->install(['test.foo.bar']); |
6a488035 TO |
224 | $this->assertEquals('installed', $manager->getStatus('test.foo.bar')); |
225 | ||
226 | $this->assertTrue(file_exists("{$this->basedir}/weird/foobar/info.xml")); | |
227 | CRM_Utils_File::cleanDir("{$this->basedir}/weird/foobar", TRUE, FALSE); | |
228 | $this->assertFalse(file_exists("{$this->basedir}/weird/foobar/info.xml")); | |
229 | $manager->refresh(); | |
230 | $this->assertEquals('installed-missing', $manager->getStatus('test.foo.bar')); | |
231 | ||
232 | $testingTypeManager | |
233 | ->expects($this->once()) | |
234 | ->method('onPreDisable'); | |
235 | $testingTypeManager | |
236 | ->expects($this->once()) | |
237 | ->method('onPostDisable'); | |
9099cab3 | 238 | $manager->disable(['test.foo.bar']); |
6a488035 TO |
239 | $this->assertEquals('disabled-missing', $manager->getStatus('test.foo.bar')); |
240 | ||
241 | $testingTypeManager | |
242 | ->expects($this->once()) | |
243 | ->method('onPreUninstall'); | |
244 | $testingTypeManager | |
245 | ->expects($this->once()) | |
246 | ->method('onPostUninstall'); | |
9099cab3 | 247 | $manager->uninstall(['test.foo.bar']); |
6a488035 TO |
248 | $this->assertEquals('unknown', $manager->getStatus('test.foo.bar')); |
249 | } | |
250 | ||
251 | /** | |
eceb18cc | 252 | * Install an extension with a valid type name. |
6a488035 | 253 | */ |
00be9182 | 254 | public function testInstall_Disable_Enable() { |
dcadbba4 SL |
255 | $mockFunction = $this->mockMethod; |
256 | $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface'); | |
9099cab3 | 257 | $manager = $this->_createManager([ |
6a488035 | 258 | self::TESTING_TYPE => $testingTypeManager, |
9099cab3 | 259 | ]); |
6a488035 TO |
260 | $this->assertEquals('uninstalled', $manager->getStatus('test.foo.bar')); |
261 | $this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang')); | |
262 | ||
263 | $testingTypeManager | |
264 | ->expects($this->exactly(2)) | |
265 | ->method('onPreInstall'); | |
266 | $testingTypeManager | |
267 | ->expects($this->exactly(2)) | |
268 | ->method('onPostInstall'); | |
9099cab3 | 269 | $manager->install(['test.whiz.bang', 'test.foo.bar']); |
6a488035 TO |
270 | $this->assertEquals('installed', $manager->getStatus('test.foo.bar')); |
271 | $this->assertEquals('installed', $manager->getStatus('test.whiz.bang')); | |
272 | ||
273 | $testingTypeManager | |
274 | ->expects($this->once()) | |
275 | ->method('onPreDisable'); | |
276 | $testingTypeManager | |
277 | ->expects($this->once()) | |
278 | ->method('onPostDisable'); | |
9099cab3 | 279 | $manager->disable(['test.foo.bar']); |
6a488035 TO |
280 | $this->assertEquals('disabled', $manager->getStatus('test.foo.bar')); |
281 | $this->assertEquals('installed', $manager->getStatus('test.whiz.bang')); | |
282 | ||
283 | $testingTypeManager | |
284 | ->expects($this->once()) | |
285 | ->method('onPreEnable'); | |
286 | $testingTypeManager | |
287 | ->expects($this->once()) | |
288 | ->method('onPostEnable'); | |
9099cab3 | 289 | $manager->enable(['test.foo.bar']); |
6a488035 TO |
290 | $this->assertEquals('installed', $manager->getStatus('test.foo.bar')); |
291 | $this->assertEquals('installed', $manager->getStatus('test.whiz.bang')); | |
292 | } | |
293 | ||
294 | /** | |
295 | * Performing 'install' on a 'disabled' extension performs an 'enable' | |
296 | */ | |
00be9182 | 297 | public function testInstall_Disable_Install() { |
dcadbba4 SL |
298 | $mockFunction = $this->mockMethod; |
299 | $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface'); | |
9099cab3 | 300 | $manager = $this->_createManager([ |
6a488035 | 301 | self::TESTING_TYPE => $testingTypeManager, |
9099cab3 | 302 | ]); |
6a488035 TO |
303 | $this->assertEquals('uninstalled', $manager->getStatus('test.foo.bar')); |
304 | ||
305 | $testingTypeManager | |
306 | ->expects($this->once()) | |
307 | ->method('onPreInstall'); | |
308 | $testingTypeManager | |
309 | ->expects($this->once()) | |
310 | ->method('onPostInstall'); | |
9099cab3 | 311 | $manager->install(['test.foo.bar']); |
6a488035 TO |
312 | $this->assertEquals('installed', $manager->getStatus('test.foo.bar')); |
313 | ||
314 | $testingTypeManager | |
315 | ->expects($this->once()) | |
316 | ->method('onPreDisable'); | |
317 | $testingTypeManager | |
318 | ->expects($this->once()) | |
319 | ->method('onPostDisable'); | |
9099cab3 | 320 | $manager->disable(['test.foo.bar']); |
6a488035 TO |
321 | $this->assertEquals('disabled', $manager->getStatus('test.foo.bar')); |
322 | ||
323 | $testingTypeManager | |
324 | ->expects($this->once()) | |
325 | ->method('onPreEnable'); | |
326 | $testingTypeManager | |
327 | ->expects($this->once()) | |
328 | ->method('onPostEnable'); | |
39b959db | 329 | // install() instead of enable() |
9099cab3 | 330 | $manager->install(['test.foo.bar']); |
6a488035 TO |
331 | $this->assertEquals('installed', $manager->getStatus('test.foo.bar')); |
332 | } | |
333 | ||
334 | /** | |
eceb18cc | 335 | * Install an extension with a valid type name. |
6a488035 | 336 | */ |
00be9182 | 337 | public function testEnableBare() { |
dcadbba4 SL |
338 | $mockFunction = $this->mockMethod; |
339 | $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface'); | |
9099cab3 | 340 | $manager = $this->_createManager([ |
6a488035 | 341 | self::TESTING_TYPE => $testingTypeManager, |
9099cab3 | 342 | ]); |
6a488035 TO |
343 | $this->assertEquals('uninstalled', $manager->getStatus('test.foo.bar')); |
344 | ||
345 | $testingTypeManager | |
346 | ->expects($this->once()) | |
347 | ->method('onPreInstall'); | |
348 | $testingTypeManager | |
349 | ->expects($this->once()) | |
350 | ->method('onPostInstall'); | |
351 | $testingTypeManager | |
352 | ->expects($this->never()) | |
353 | ->method('onPreEnable'); | |
354 | $testingTypeManager | |
355 | ->expects($this->never()) | |
356 | ->method('onPostEnable'); | |
39b959db | 357 | // enable not install |
9099cab3 | 358 | $manager->enable(['test.foo.bar']); |
6a488035 TO |
359 | $this->assertEquals('installed', $manager->getStatus('test.foo.bar')); |
360 | } | |
361 | ||
362 | /** | |
eceb18cc | 363 | * Get the status of an unknown extension. |
6a488035 | 364 | */ |
00be9182 | 365 | public function testStatusUnknownKey() { |
dcadbba4 SL |
366 | $mockFunction = $this->mockMethod; |
367 | $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface'); | |
6a488035 TO |
368 | $testingTypeManager->expects($this->never()) |
369 | ->method('onPreInstall'); | |
9099cab3 | 370 | $manager = $this->_createManager([ |
6a488035 | 371 | self::TESTING_TYPE => $testingTypeManager, |
9099cab3 | 372 | ]); |
6a488035 TO |
373 | $this->assertEquals('unknown', $manager->getStatus('test.foo.bar.whiz.bang')); |
374 | } | |
375 | ||
376 | /** | |
377 | * Replace code for an extension that doesn't exist in the container | |
378 | */ | |
00be9182 | 379 | public function testReplace_Unknown() { |
dcadbba4 SL |
380 | $mockFunction = $this->mockMethod; |
381 | $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface'); | |
9099cab3 | 382 | $manager = $this->_createManager([ |
6a488035 | 383 | self::TESTING_TYPE => $testingTypeManager, |
9099cab3 | 384 | ]); |
6a488035 TO |
385 | $this->assertEquals('unknown', $manager->getStatus('test.newextension')); |
386 | ||
387 | $this->download = $this->_createDownload('test.newextension', 'newextension'); | |
388 | ||
389 | $testingTypeManager | |
39b959db SL |
390 | // no data to replace |
391 | ->expects($this->never()) | |
6a488035 TO |
392 | ->method('onPreReplace'); |
393 | $testingTypeManager | |
39b959db SL |
394 | // no data to replace |
395 | ->expects($this->never()) | |
6a488035 TO |
396 | ->method('onPostReplace'); |
397 | $manager->replace($this->download); | |
398 | $this->assertEquals('uninstalled', $manager->getStatus('test.newextension')); | |
399 | $this->assertTrue(file_exists("{$this->basedir}/test.newextension/info.xml")); | |
400 | $this->assertTrue(file_exists("{$this->basedir}/test.newextension/newextension.php")); | |
401 | $this->assertEquals(self::TESTING_TYPE, $this->mapper->keyToInfo('test.newextension')->type); | |
402 | $this->assertEquals('newextension', $this->mapper->keyToInfo('test.newextension')->file); | |
403 | } | |
404 | ||
405 | /** | |
406 | * Replace code for an extension that doesn't exist in the container | |
407 | */ | |
00be9182 | 408 | public function testReplace_Uninstalled() { |
dcadbba4 SL |
409 | $mockFunction = $this->mockMethod; |
410 | $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface'); | |
9099cab3 | 411 | $manager = $this->_createManager([ |
6a488035 | 412 | self::TESTING_TYPE => $testingTypeManager, |
9099cab3 | 413 | ]); |
6a488035 TO |
414 | $this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang')); |
415 | $this->assertEquals('oddball', $this->mapper->keyToInfo('test.whiz.bang')->file); | |
416 | ||
417 | $this->download = $this->_createDownload('test.whiz.bang', 'newextension'); | |
418 | ||
419 | $testingTypeManager | |
39b959db SL |
420 | // no data to replace |
421 | ->expects($this->never()) | |
6a488035 TO |
422 | ->method('onPreReplace'); |
423 | $testingTypeManager | |
39b959db SL |
424 | // no data to replace |
425 | ->expects($this->never()) | |
6a488035 TO |
426 | ->method('onPostReplace'); |
427 | $manager->replace($this->download); | |
428 | $this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang')); | |
429 | $this->assertTrue(file_exists("{$this->basedir}/weird/whizbang/info.xml")); | |
430 | $this->assertTrue(file_exists("{$this->basedir}/weird/whizbang/newextension.php")); | |
431 | $this->assertFalse(file_exists("{$this->basedir}/weird/whizbang/oddball.php")); | |
432 | $this->assertEquals(self::TESTING_TYPE, $this->mapper->keyToInfo('test.whiz.bang')->type); | |
433 | $this->assertEquals('newextension', $this->mapper->keyToInfo('test.whiz.bang')->file); | |
434 | } | |
435 | ||
436 | /** | |
eceb18cc | 437 | * Install a module and then replace it with new code. |
6a488035 TO |
438 | * |
439 | * Note that some metadata changes between versions -- the original has | |
440 | * file="oddball", and the upgrade has file="newextension". | |
441 | */ | |
00be9182 | 442 | public function testReplace_Installed() { |
dcadbba4 SL |
443 | $mockFunction = $this->mockMethod; |
444 | $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface'); | |
9099cab3 | 445 | $manager = $this->_createManager([ |
6a488035 | 446 | self::TESTING_TYPE => $testingTypeManager, |
9099cab3 | 447 | ]); |
6a488035 TO |
448 | $this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang')); |
449 | $this->assertEquals('oddball', $this->mapper->keyToInfo('test.whiz.bang')->file); | |
450 | ||
9099cab3 | 451 | $manager->install(['test.whiz.bang']); |
6a488035 TO |
452 | $this->assertEquals('installed', $manager->getStatus('test.whiz.bang')); |
453 | $this->assertEquals('oddball', $this->mapper->keyToInfo('test.whiz.bang')->file); | |
454 | $this->assertDBQuery('oddball', 'SELECT file FROM civicrm_extension WHERE full_name ="test.whiz.bang"'); | |
455 | ||
456 | $this->download = $this->_createDownload('test.whiz.bang', 'newextension'); | |
457 | ||
458 | $testingTypeManager | |
459 | ->expects($this->once()) | |
460 | ->method('onPreReplace'); | |
461 | $testingTypeManager | |
462 | ->expects($this->once()) | |
463 | ->method('onPostReplace'); | |
464 | $manager->replace($this->download); | |
465 | $this->assertEquals('installed', $manager->getStatus('test.whiz.bang')); | |
466 | $this->assertTrue(file_exists("{$this->basedir}/weird/whizbang/info.xml")); | |
467 | $this->assertTrue(file_exists("{$this->basedir}/weird/whizbang/newextension.php")); | |
468 | $this->assertFalse(file_exists("{$this->basedir}/weird/whizbang/oddball.php")); | |
469 | $this->assertEquals('newextension', $this->mapper->keyToInfo('test.whiz.bang')->file); | |
470 | $this->assertDBQuery('newextension', 'SELECT file FROM civicrm_extension WHERE full_name ="test.whiz.bang"'); | |
471 | } | |
472 | ||
473 | /** | |
474 | * Install a module and then delete (leaving stale DB info); restore | |
475 | * the module by downloading new code. | |
476 | * | |
477 | * Note that some metadata changes between versions -- the original has | |
478 | * file="oddball", and the upgrade has file="newextension". | |
479 | */ | |
00be9182 | 480 | public function testReplace_InstalledMissing() { |
dcadbba4 SL |
481 | $mockFunction = $this->mockMethod; |
482 | $testingTypeManager = $this->$mockFunction('CRM_Extension_Manager_Interface'); | |
9099cab3 | 483 | $manager = $this->_createManager([ |
6a488035 | 484 | self::TESTING_TYPE => $testingTypeManager, |
9099cab3 | 485 | ]); |
b6708aeb | 486 | |
6a488035 TO |
487 | // initial installation |
488 | $this->assertEquals('uninstalled', $manager->getStatus('test.whiz.bang')); | |
9099cab3 | 489 | $manager->install(['test.whiz.bang']); |
6a488035 TO |
490 | $this->assertEquals('installed', $manager->getStatus('test.whiz.bang')); |
491 | ||
492 | // dirty remove | |
493 | $this->assertTrue(file_exists("{$this->basedir}/weird/whizbang/info.xml")); | |
494 | CRM_Utils_File::cleanDir("{$this->basedir}/weird/whizbang", TRUE, FALSE); | |
495 | $this->assertFalse(file_exists("{$this->basedir}/weird/whizbang/info.xml")); | |
496 | $manager->refresh(); | |
497 | $this->assertEquals('installed-missing', $manager->getStatus('test.whiz.bang')); | |
498 | ||
499 | // download and reinstall | |
500 | $this->download = $this->_createDownload('test.whiz.bang', 'newextension'); | |
501 | ||
502 | $testingTypeManager | |
503 | ->expects($this->once()) | |
504 | ->method('onPreReplace'); | |
505 | $testingTypeManager | |
506 | ->expects($this->once()) | |
507 | ->method('onPostReplace'); | |
508 | $manager->replace($this->download); | |
509 | $this->assertEquals('installed', $manager->getStatus('test.whiz.bang')); | |
510 | $this->assertTrue(file_exists("{$this->basedir}/test.whiz.bang/info.xml")); | |
511 | $this->assertTrue(file_exists("{$this->basedir}/test.whiz.bang/newextension.php")); | |
512 | $this->assertEquals('newextension', $this->mapper->keyToInfo('test.whiz.bang')->file); | |
513 | $this->assertDBQuery('newextension', 'SELECT file FROM civicrm_extension WHERE full_name ="test.whiz.bang"'); | |
514 | } | |
515 | ||
4cbe18b8 EM |
516 | /** |
517 | * @param $typeManagers | |
518 | * | |
519 | * @return CRM_Extension_Manager | |
520 | */ | |
00be9182 | 521 | public function _createManager($typeManagers) { |
6a488035 TO |
522 | //list ($basedir, $c) = $this->_createContainer(); |
523 | $mapper = new CRM_Extension_Mapper($this->container); | |
524 | return new CRM_Extension_Manager($this->container, $this->container, $this->mapper, $typeManagers); | |
525 | } | |
526 | ||
4cbe18b8 EM |
527 | /** |
528 | * @param CRM_Utils_Cache_Interface $cache | |
529 | * @param null $cacheKey | |
530 | * | |
531 | * @return array | |
532 | */ | |
00be9182 | 533 | public function _createContainer(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) { |
6a488035 TO |
534 | $basedir = $this->createTempDir('ext-'); |
535 | mkdir("$basedir/weird"); | |
536 | mkdir("$basedir/weird/foobar"); | |
92fcb95f | 537 | file_put_contents("$basedir/weird/foobar/info.xml", "<extension key='test.foo.bar' type='" . self::TESTING_TYPE . "'><file>oddball</file></extension>"); |
6a488035 TO |
538 | // not needed for now // file_put_contents("$basedir/weird/bar/oddball.php", "<?php\n"); |
539 | mkdir("$basedir/weird/whizbang"); | |
92fcb95f | 540 | file_put_contents("$basedir/weird/whizbang/info.xml", "<extension key='test.whiz.bang' type='" . self::TESTING_TYPE . "'><file>oddball</file></extension>"); |
6a488035 | 541 | // not needed for now // file_put_contents("$basedir/weird/whizbang/oddball.php", "<?php\n"); |
f8a7cfff TO |
542 | mkdir("$basedir/weird/downstream"); |
543 | file_put_contents("$basedir/weird/downstream/info.xml", "<extension key='test.foo.downstream' type='" . self::TESTING_TYPE . "'><file>oddball</file><requires><ext>test.foo.bar</ext></requires></extension>"); | |
544 | // not needed for now // file_put_contents("$basedir/weird/downstream/oddball.php", "<?php\n"); | |
6a488035 | 545 | $c = new CRM_Extension_Container_Basic($basedir, 'http://example/basedir', $cache, $cacheKey); |
9099cab3 | 546 | return [$basedir, $c]; |
6a488035 TO |
547 | } |
548 | ||
4cbe18b8 EM |
549 | /** |
550 | * @param $key | |
551 | * @param $file | |
552 | * | |
553 | * @return string | |
554 | */ | |
00be9182 | 555 | public function _createDownload($key, $file) { |
6a488035 | 556 | $basedir = $this->createTempDir('ext-dl-'); |
92fcb95f | 557 | file_put_contents("$basedir/info.xml", "<extension key='$key' type='" . self::TESTING_TYPE . "'><file>$file</file></extension>"); |
6a488035 TO |
558 | file_put_contents("$basedir/$file.php", "<?php\n"); |
559 | return $basedir; | |
560 | } | |
96025800 | 561 | |
6a488035 | 562 | } |