[FEATURE] ext:install Verify checksum of downloaded core
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Tests / Unit / Service / CoreVersionServiceTest.php
1 <?php
2 namespace TYPO3\CMS\Install\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 /**
31 * Test case
32 */
33 class CoreVersionServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
34
35 /**
36 * @test
37 */
38 public function updateVersionMatrixStoresVersionMatrixInRegistry() {
39 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
40 $instance = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Service\\CoreVersionService', array('fetchVersionMatrixFromRemote'), array(), '', FALSE);
41 $registry = $this->getMock('TYPO3\CMS\Core\Registry');
42 $versionArray = array(uniqId());
43 $registry->expects($this->once())->method('set')->with('TYPO3.CMS.Install', 'coreVersionMatrix', $versionArray);
44 $instance->_set('registry', $registry);
45 $instance->expects($this->once())->method('fetchVersionMatrixFromRemote')->will($this->returnValue($versionArray));
46 $instance->updateVersionMatrix();
47 }
48
49 /**
50 * @test
51 */
52 public function updateVersionMatrixRemovesOldReleasesFromMatrix() {
53 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
54 $instance = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Service\\CoreVersionService', array('fetchVersionMatrixFromRemote'), array(), '', FALSE);
55 $registry = $this->getMock('TYPO3\CMS\Core\Registry');
56 $versionArray = array(
57 '6.2' => array(),
58 '6.1' => array(),
59 );
60 $registry
61 ->expects($this->once())
62 ->method('set')
63 ->with('TYPO3.CMS.Install', 'coreVersionMatrix', $this->logicalNot($this->arrayHasKey('6.1')));
64 $instance->_set('registry', $registry);
65 $instance->expects($this->once())->method('fetchVersionMatrixFromRemote')->will($this->returnValue($versionArray));
66 $instance->updateVersionMatrix();
67 }
68
69 /**
70 * @test
71 */
72 public function isInstalledVersionAReleasedVersionReturnsTrueForNonDevelopmentVersion() {
73 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
74 $instance = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Service\\CoreVersionService', array('getInstalledVersion'), array(), '', FALSE);
75 $instance->expects($this->once())->method('getInstalledVersion')->will($this->returnValue('6.2.0'));
76 $this->assertTrue($instance->isInstalledVersionAReleasedVersion());
77 }
78
79 /**
80 * @test
81 */
82 public function isInstalledVersionAReleasedVersionReturnsFalseForDevelopmentVersion() {
83 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
84 $instance = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Service\\CoreVersionService', array('getInstalledVersion'), array(), '', FALSE);
85 $instance->expects($this->once())->method('getInstalledVersion')->will($this->returnValue('6.2-dev'));
86 $this->assertFalse($instance->isInstalledVersionAReleasedVersion());
87 }
88
89 /**
90 * @test
91 * @expectedException \TYPO3\CMS\Install\Service\Exception\CoreVersionServiceException
92 */
93 public function getTarGzSha1OfVersionThrowsExceptionIfSha1DoesNotExistInMatrix() {
94 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
95 $instance = $this->getAccessibleMock(
96 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
97 array('getVersionMatrix', 'getMinorVersion', 'ensureVersionExistsInMatrix'),
98 array(),
99 '',
100 FALSE
101 );
102 $versionMatrix = array(
103 '6.2' => array(
104 'releases' => array(
105 '6.2.42' => array(),
106 ),
107 ),
108 );
109 $instance->expects($this->once())->method('getMinorVersion')->will($this->returnValue('6.2'));
110 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
111 $this->assertTrue($instance->getTarGzSha1OfVersion('6.2.42'));
112 }
113
114 /**
115 * @test
116 */
117 public function getTarGzSha1OfVersionReturnsSha1OfSpecifiedVersion() {
118 $versionMatrixFixtureFile = __DIR__ . '/Fixtures/VersionMatrixFixture.php';
119 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
120 $instance = $this->getAccessibleMock(
121 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
122 array('getVersionMatrix', 'getMinorVersion', 'ensureVersionExistsInMatrix'),
123 array(),
124 '',
125 FALSE
126 );
127 $instance->expects($this->any())->method('getMinorVersion')->will($this->returnValue('6.2'));
128 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue(require($versionMatrixFixtureFile)));
129 $this->assertSame('3dc156eed4b99577232f537d798a8691493f8a83', $instance->getTarGzSha1OfVersion('6.2.0alpha3'));
130 }
131
132 /**
133 * Whitebox test of API method: This tests multiple methods, only 'current version' and 'version matrix' are mocked.
134 *
135 * @test
136 */
137 public function isYoungerPatchReleaseAvailableReturnsTrueIfYoungerReleaseIsAvailable() {
138 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
139 $instance = $this->getAccessibleMock(
140 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
141 array('getVersionMatrix', 'getInstalledVersion'),
142 array(),
143 '',
144 FALSE
145 );
146 $versionMatrix = array(
147 '6.2' => array(
148 'releases' => array(
149 '6.2.42' => array(
150 'type' => 'regular',
151 'date' => '2013-12-01 18:24:25 UTC',
152 ),
153 '6.2.41' => array(
154 'type' => 'regular',
155 'date' => '2013-11-01 18:24:25 UTC',
156 ),
157 ),
158 ),
159 );
160 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
161 $instance->expects($this->any())->method('getInstalledVersion')->will($this->returnValue('6.2.41'));
162 $this->assertTrue($instance->isYoungerPatchReleaseAvailable());
163 }
164
165 /**
166 * Whitebox test of API method: This tests multiple methods, only 'current version' and 'version matrix' are mocked.
167 *
168 * @test
169 */
170 public function isYoungerReleaseAvailableReturnsFalseIfNoYoungerReleaseExists() {
171 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
172 $instance = $this->getAccessibleMock(
173 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
174 array('getVersionMatrix', 'getInstalledVersion'),
175 array(),
176 '',
177 FALSE
178 );
179 $versionMatrix = array(
180 '6.2' => array(
181 'releases' => array(
182 '6.2.42' => array(
183 'type' => 'regular',
184 'date' => '2013-12-01 18:24:25 UTC',
185 ),
186 '6.2.41' => array(
187 'type' => 'regular',
188 'date' => '2013-11-01 18:24:25 UTC',
189 ),
190 ),
191 ),
192 );
193 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
194 $instance->expects($this->any())->method('getInstalledVersion')->will($this->returnValue('6.2.42'));
195 $this->assertFalse($instance->isYoungerPatchReleaseAvailable());
196 }
197
198 /**
199 * Whitebox test of API method: This tests multiple methods, only 'current version' and 'version matrix' are mocked.
200 *
201 * @test
202 */
203 public function isYoungerReleaseAvailableReturnsFalseIfOnlyADevelopmentReleaseIsYounger() {
204 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
205 $instance = $this->getAccessibleMock(
206 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
207 array('getVersionMatrix', 'getInstalledVersion'),
208 array(),
209 '',
210 FALSE
211 );
212 $versionMatrix = array(
213 '6.2' => array(
214 'releases' => array(
215 '6.2.42' => array(
216 'type' => 'development',
217 'date' => '2013-12-01 18:24:25 UTC',
218 ),
219 '6.2.41' => array(
220 'type' => 'regular',
221 'date' => '2013-11-01 18:24:25 UTC',
222 ),
223 ),
224 ),
225 );
226 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
227 $instance->expects($this->any())->method('getInstalledVersion')->will($this->returnValue('6.2.41'));
228 $this->assertFalse($instance->isYoungerPatchReleaseAvailable());
229 }
230
231 /**
232 * Whitebox test of API method: This tests multiple methods, only 'current version' and 'version matrix' are mocked.
233 *
234 * @test
235 */
236 public function isYoungerDevelopmentReleaseAvailableReturnsTrueIfADevelopmentReleaseIsYounger() {
237 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
238 $instance = $this->getAccessibleMock(
239 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
240 array('getVersionMatrix', 'getInstalledVersion'),
241 array(),
242 '',
243 FALSE
244 );
245 $versionMatrix = array(
246 '6.2' => array(
247 'releases' => array(
248 '6.2.42' => array(
249 'type' => 'development',
250 'date' => '2013-12-01 18:24:25 UTC',
251 ),
252 '6.2.41' => array(
253 'type' => 'regular',
254 'date' => '2013-11-01 18:24:25 UTC',
255 ),
256 ),
257 ),
258 );
259 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
260 $instance->expects($this->any())->method('getInstalledVersion')->will($this->returnValue('6.2.41'));
261 $this->assertTrue($instance->isYoungerPatchDevelopmentReleaseAvailable());
262 }
263
264 /**
265 * Whitebox test of API method: This tests multiple methods, only 'current version' and 'version matrix' are mocked.
266 *
267 * @test
268 */
269 public function isUpdateSecurityRelevantReturnsTrueIfAnUpdateIsSecurityRelevant() {
270 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
271 $instance = $this->getAccessibleMock(
272 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
273 array('getVersionMatrix', 'getInstalledVersion'),
274 array(),
275 '',
276 FALSE
277 );
278 $versionMatrix = array(
279 '6.2' => array(
280 'releases' => array(
281 '6.2.42' => array(
282 'type' => 'security',
283 'date' => '2013-12-01 18:24:25 UTC',
284 ),
285 '6.2.41' => array(
286 'type' => 'regular',
287 'date' => '2013-11-01 18:24:25 UTC',
288 ),
289 ),
290 ),
291 );
292 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
293 $instance->expects($this->any())->method('getInstalledVersion')->will($this->returnValue('6.2.41'));
294 $this->assertTrue($instance->isUpdateSecurityRelevant());
295 }
296
297 /**
298 * Whitebox test of API method: This tests multiple methods, only 'current version' and 'version matrix' are mocked.
299 *
300 * @test
301 */
302 public function isUpdateSecurityRelevantReturnsFalseIfUpdateIsNotSecurityRelevant() {
303 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
304 $instance = $this->getAccessibleMock(
305 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
306 array('getVersionMatrix', 'getInstalledVersion'),
307 array(),
308 '',
309 FALSE
310 );
311 $versionMatrix = array(
312 '6.2' => array(
313 'releases' => array(
314 '6.2.42' => array(
315 'type' => 'regular',
316 'date' => '2013-12-01 18:24:25 UTC',
317 ),
318 '6.2.41' => array(
319 'type' => 'regular',
320 'date' => '2013-11-01 18:24:25 UTC',
321 ),
322 ),
323 ),
324 );
325 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
326 $instance->expects($this->any())->method('getInstalledVersion')->will($this->returnValue('6.2.41'));
327 $this->assertFalse($instance->isUpdateSecurityRelevant());
328 }
329
330 /**
331 * @test
332 */
333 public function getInstalledMinorVersionFetchesInstalledVersionNumber() {
334 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
335 $instance = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Service\\CoreVersionService', array('getInstalledVersion'), array(), '', FALSE);
336 $instance->expects($this->once())->method('getInstalledVersion')->will($this->returnValue('6.2.0'));
337 $this->assertSame('6.2', $instance->_call('getInstalledMinorVersion'));
338 }
339
340 /**
341 * Data provider
342 */
343 public function getMinorVersionDataProvider() {
344 return array(
345 '6.2.0' => array(
346 '6.2.0',
347 '6.2',
348 ),
349 '6.2-dev' => array(
350 '6.2-dev',
351 '6.2',
352 ),
353 '6.2.0alpha2' => array(
354 '6.2.0alpha2',
355 '6.2',
356 ),
357 '4.5.25' => array(
358 '4.5.25',
359 '4.5',
360 ),
361 );
362 }
363
364 /**
365 * @test
366 * @dataProvider getMinorVersionDataProvider
367 */
368 public function getMinorVersionReturnsCorrectMinorVersion($version, $expectedMajor) {
369 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
370 $instance = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Service\\CoreVersionService', array('dummy'), array(), '', FALSE);
371 $this->assertSame($expectedMajor, $instance->_call('getMinorVersion', $version));
372 }
373
374 /**
375 * @test
376 * @expectedException \TYPO3\CMS\Install\Service\Exception\CoreVersionServiceException
377 */
378 public function getVersionMatrixThrowsExceptionIfVersionMatrixIsNotYetSetInRegistry() {
379 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
380 $instance = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Service\\CoreVersionService', array('fetchVersionMatrixFromRemote'), array(), '', FALSE);
381 $registry = $this->getMock('TYPO3\CMS\Core\Registry');
382 $registry->expects($this->once())->method('get')->will($this->returnValue(NULL));
383 $instance->_set('registry', $registry);
384 $instance->_call('getVersionMatrix');
385 }
386
387 /**
388 * @test
389 */
390 public function getVersionMatrixReturnsMatrixFromRegistry() {
391 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
392 $instance = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Service\\CoreVersionService', array('fetchVersionMatrixFromRemote'), array(), '', FALSE);
393 $registry = $this->getMock('TYPO3\CMS\Core\Registry');
394 $versionArray = array(uniqId());
395 $registry->expects($this->once())->method('get')->will($this->returnValue($versionArray));
396 $instance->_set('registry', $registry);
397 $this->assertSame($versionArray, $instance->_call('getVersionMatrix'));
398 }
399
400 /**
401 * @test
402 * @expectedException \TYPO3\CMS\Install\Service\Exception\CoreVersionServiceException
403 */
404 public function getReleaseTimestampOfVersionThrowsExceptionIfReleaseDateIsNotDefined() {
405 $versionMatrix = array(
406 '6.2' => array(
407 'releases' => array(
408 '6.2.42' => array()
409 ),
410 ),
411 );
412 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
413 $instance = $this->getAccessibleMock(
414 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
415 array('getVersionMatrix', 'getMinorVersion', 'ensureVersionExistsInMatrix'),
416 array(),
417 '',
418 FALSE
419 );
420 $instance->expects($this->once())->method('getMinorVersion')->will($this->returnValue('6.2'));
421 $instance->expects($this->once())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
422 $instance->_call('getReleaseTimestampOfVersion', '6.2.42');
423 }
424
425 /**
426 * @test
427 */
428 public function getReleaseTimestampOfVersionReturnsTimestamp() {
429 $versionMatrixFixtureFile = __DIR__ . '/Fixtures/VersionMatrixFixture.php';
430 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
431 $instance = $this->getAccessibleMock(
432 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
433 array('getVersionMatrix', 'getMinorVersion', 'ensureVersionExistsInMatrix'),
434 array(),
435 '',
436 FALSE
437 );
438 $instance->expects($this->once())->method('getMinorVersion')->will($this->returnValue('6.2'));
439 $instance->expects($this->once())->method('getVersionMatrix')->will($this->returnValue(require($versionMatrixFixtureFile)));
440 $this->assertSame(1380651865, $instance->_call('getReleaseTimestampOfVersion', '6.2.0alpha3'));
441 }
442
443 /**
444 * @test
445 * @expectedException \TYPO3\CMS\Install\Service\Exception\CoreVersionServiceException
446 */
447 public function ensureVersionExistsInMatrixThrowsExceptionIfMinorVersionDoesNotExist() {
448 $versionMatrixFixtureFile = __DIR__ . '/Fixtures/VersionMatrixFixture.php';
449 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
450 $instance = $this->getAccessibleMock(
451 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
452 array('getVersionMatrix', 'getMinorVersion'),
453 array(),
454 '',
455 FALSE
456 );
457 $instance->expects($this->once())->method('getMinorVersion')->will($this->returnValue('2.0'));
458 $instance->expects($this->once())->method('getVersionMatrix')->will($this->returnValue(require($versionMatrixFixtureFile)));
459 $instance->_call('ensureVersionExistsInMatrix', '2.0.42');
460 }
461
462 /**
463 * @test
464 * @expectedException \TYPO3\CMS\Install\Service\Exception\CoreVersionServiceException
465 */
466 public function ensureVersionExistsInMatrixThrowsExceptionIfPatchLevelDoesNotExist() {
467 $versionMatrixFixtureFile = __DIR__ . '/Fixtures/VersionMatrixFixture.php';
468 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
469 $instance = $this->getAccessibleMock(
470 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
471 array('getVersionMatrix', 'getMinorVersion'),
472 array(),
473 '',
474 FALSE
475 );
476 $instance->expects($this->once())->method('getMinorVersion')->will($this->returnValue('6.2'));
477 $instance->expects($this->once())->method('getVersionMatrix')->will($this->returnValue(require($versionMatrixFixtureFile)));
478 $instance->_call('ensureVersionExistsInMatrix', '6.2.5');
479 }
480 }