843da0586e0e8295301d246b991ca340cae784c9
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Tests / Unit / Service / CoreVersionServiceTest.php
1 <?php
2 namespace TYPO3\CMS\Install\Tests\Unit\Service;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Registry;
18 use TYPO3\CMS\Install\Service\CoreVersionService;
19 use TYPO3\CMS\Install\Service\Exception\CoreVersionServiceException;
20
21 /**
22 * Test case
23 */
24 class CoreVersionServiceTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
25 {
26 /**
27 * @test
28 */
29 public function updateVersionMatrixStoresVersionMatrixInRegistry()
30 {
31 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
32 $instance = $this->getAccessibleMock(CoreVersionService::class, ['fetchVersionMatrixFromRemote'], [], '', false);
33 $registry = $this->createMock(Registry::class);
34 $versionArray = [9 => []];
35 $registry->expects($this->once())->method('set')->with('TYPO3.CMS.Install', 'coreVersionMatrix', $versionArray);
36 $instance->_set('registry', $registry);
37 $instance->expects($this->once())->method('fetchVersionMatrixFromRemote')->will($this->returnValue($versionArray));
38 $instance->updateVersionMatrix();
39 }
40
41 /**
42 * @test
43 */
44 public function updateVersionMatrixRemovesOldReleasesFromMatrix()
45 {
46 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
47 $instance = $this->getAccessibleMock(CoreVersionService::class, ['fetchVersionMatrixFromRemote'], [], '', false);
48 $registry = $this->createMock(Registry::class);
49 $versionArray = [
50 '7' => [],
51 '6.2' => [],
52 ];
53 $registry
54 ->expects($this->once())
55 ->method('set')
56 ->with('TYPO3.CMS.Install', 'coreVersionMatrix', $this->logicalNot($this->arrayHasKey('6.2')));
57 $instance->_set('registry', $registry);
58 $instance->expects($this->once())->method('fetchVersionMatrixFromRemote')->will($this->returnValue($versionArray));
59 $instance->updateVersionMatrix();
60 }
61
62 /**
63 * @test
64 */
65 public function isInstalledVersionAReleasedVersionReturnsTrueForNonDevelopmentVersion()
66 {
67 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
68 $instance = $this->getAccessibleMock(CoreVersionService::class, ['getInstalledVersion'], [], '', false);
69 $instance->expects($this->once())->method('getInstalledVersion')->will($this->returnValue('7.2.0'));
70 $this->assertTrue($instance->isInstalledVersionAReleasedVersion());
71 }
72
73 /**
74 * @test
75 */
76 public function isInstalledVersionAReleasedVersionReturnsFalseForDevelopmentVersion()
77 {
78 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
79 $instance = $this->getAccessibleMock(CoreVersionService::class, ['getInstalledVersion'], [], '', false);
80 $instance->expects($this->once())->method('getInstalledVersion')->will($this->returnValue('7.4-dev'));
81 $this->assertFalse($instance->isInstalledVersionAReleasedVersion());
82 }
83
84 /**
85 * @test
86 */
87 public function getTarGzSha1OfVersionThrowsExceptionIfSha1DoesNotExistInMatrix()
88 {
89 $this->expectException(CoreVersionServiceException::class);
90 $this->expectExceptionCode(1381263173);
91 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
92 $instance = $this->getAccessibleMock(
93 CoreVersionService::class,
94 ['getVersionMatrix', 'getMajorVersion', 'ensureVersionExistsInMatrix'],
95 [],
96 '',
97 false
98 );
99 $versionMatrix = [
100 '7' => [
101 'releases' => [
102 '7.2.0' => [],
103 ],
104 ],
105 ];
106 $instance->expects($this->once())->method('getMajorVersion')->will($this->returnValue('7'));
107 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
108 $this->assertTrue($instance->getTarGzSha1OfVersion('7.2.0'));
109 }
110
111 /**
112 * @test
113 */
114 public function getTarGzSha1OfVersionReturnsSha1OfSpecifiedVersion()
115 {
116 $versionMatrixFixtureFile = __DIR__ . '/Fixtures/VersionMatrixFixture.php';
117 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
118 $instance = $this->getAccessibleMock(
119 CoreVersionService::class,
120 ['getVersionMatrix', 'getMajorVersion', 'ensureVersionExistsInMatrix'],
121 [],
122 '',
123 false
124 );
125 $instance->expects($this->any())->method('getMajorVersion')->will($this->returnValue('7'));
126 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue(require $versionMatrixFixtureFile));
127 $this->assertSame('3dc156eed4b99577232f537d798a8691493f8a83', $instance->getTarGzSha1OfVersion('7.2.0'));
128 }
129
130 /**
131 * Whitebox test of API method: This tests multiple methods, only 'current version' and 'version matrix' are mocked.
132 *
133 * @test
134 */
135 public function isYoungerPatchReleaseAvailableReturnsTrueIfYoungerReleaseIsAvailable()
136 {
137 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
138 $instance = $this->getAccessibleMock(
139 CoreVersionService::class,
140 ['getVersionMatrix', 'getInstalledVersion'],
141 [],
142 '',
143 false
144 );
145 $versionMatrix = [
146 '7' => [
147 'releases' => [
148 '7.2.1' => [
149 'type' => 'security',
150 'date' => '2013-12-01 18:24:25 UTC',
151 ],
152 '7.2.0' => [
153 'type' => 'regular',
154 'date' => '2013-11-01 18:24:25 UTC',
155 ],
156 ],
157 ],
158 ];
159 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
160 $instance->expects($this->any())->method('getInstalledVersion')->will($this->returnValue('7.2.0'));
161 $this->assertTrue($instance->isYoungerPatchReleaseAvailable());
162 }
163
164 /**
165 * Whitebox test of API method: This tests multiple methods, only 'current version' and 'version matrix' are mocked.
166 *
167 * @test
168 */
169 public function isYoungerReleaseAvailableReturnsFalseIfNoYoungerReleaseExists()
170 {
171 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
172 $instance = $this->getAccessibleMock(
173 CoreVersionService::class,
174 ['getVersionMatrix', 'getInstalledVersion'],
175 [],
176 '',
177 false
178 );
179 $versionMatrix = [
180 '7' => [
181 'releases' => [
182 '7.2.0' => [
183 'type' => 'regular',
184 'date' => '2013-12-01 18:24:25 UTC',
185 ],
186 '7.1.0' => [
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('7.2.0'));
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 {
205 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
206 $instance = $this->getAccessibleMock(
207 CoreVersionService::class,
208 ['getVersionMatrix', 'getInstalledVersion'],
209 [],
210 '',
211 false
212 );
213 $versionMatrix = [
214 '7' => [
215 'releases' => [
216 '7.3.0' => [
217 'type' => 'development',
218 'date' => '2013-12-01 18:24:25 UTC',
219 ],
220 '7.2.0' => [
221 'type' => 'regular',
222 'date' => '2013-11-01 18:24:25 UTC',
223 ],
224 ],
225 ],
226 ];
227 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
228 $instance->expects($this->any())->method('getInstalledVersion')->will($this->returnValue('7.2.0'));
229 $this->assertFalse($instance->isYoungerPatchReleaseAvailable());
230 }
231
232 /**
233 * Whitebox test of API method: This tests multiple methods, only 'current version' and 'version matrix' are mocked.
234 *
235 * @test
236 */
237 public function isYoungerDevelopmentReleaseAvailableReturnsTrueIfADevelopmentReleaseIsYounger()
238 {
239 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
240 $instance = $this->getAccessibleMock(
241 CoreVersionService::class,
242 ['getVersionMatrix', 'getInstalledVersion'],
243 [],
244 '',
245 false
246 );
247 $versionMatrix = [
248 '7' => [
249 'releases' => [
250 '7.3.0' => [
251 'type' => 'development',
252 'date' => '2013-12-01 18:24:25 UTC',
253 ],
254 '7.2.0' => [
255 'type' => 'regular',
256 'date' => '2013-11-01 18:24:25 UTC',
257 ],
258 ],
259 ],
260 ];
261 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
262 $instance->expects($this->any())->method('getInstalledVersion')->will($this->returnValue('7.2.0'));
263 $this->assertTrue($instance->isYoungerPatchDevelopmentReleaseAvailable());
264 }
265
266 /**
267 * Whitebox test of API method: This tests multiple methods, only 'current version' and 'version matrix' are mocked.
268 *
269 * @test
270 */
271 public function isUpdateSecurityRelevantReturnsTrueIfAnUpdateIsSecurityRelevant()
272 {
273 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
274 $instance = $this->getAccessibleMock(
275 CoreVersionService::class,
276 ['getVersionMatrix', 'getInstalledVersion'],
277 [],
278 '',
279 false
280 );
281 $versionMatrix = [
282 '7' => [
283 'releases' => [
284 '7.3.0' => [
285 'type' => 'security',
286 'date' => '2013-12-01 18:24:25 UTC',
287 ],
288 '7.2.0' => [
289 'type' => 'regular',
290 'date' => '2013-11-01 18:24:25 UTC',
291 ],
292 ],
293 ],
294 ];
295 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
296 $instance->expects($this->any())->method('getInstalledVersion')->will($this->returnValue('7.2.0'));
297 $this->assertTrue($instance->isUpdateSecurityRelevant());
298 }
299
300 /**
301 * Whitebox test of API method: This tests multiple methods, only 'current version' and 'version matrix' are mocked.
302 *
303 * @test
304 */
305 public function isUpdateSecurityRelevantReturnsFalseIfUpdateIsNotSecurityRelevant()
306 {
307 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
308 $instance = $this->getAccessibleMock(
309 CoreVersionService::class,
310 ['getVersionMatrix', 'getInstalledVersion'],
311 [],
312 '',
313 false
314 );
315 $versionMatrix = [
316 '7' => [
317 'releases' => [
318 '7.3.0' => [
319 'type' => 'regular',
320 'date' => '2013-12-01 18:24:25 UTC',
321 ],
322 '7.2.0' => [
323 'type' => 'regular',
324 'date' => '2013-11-01 18:24:25 UTC',
325 ],
326 ],
327 ],
328 ];
329 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
330 $instance->expects($this->any())->method('getInstalledVersion')->will($this->returnValue('7.2.0'));
331 $this->assertFalse($instance->isUpdateSecurityRelevant());
332 }
333
334 /**
335 * @test
336 */
337 public function getInstalledMajorVersionFetchesInstalledVersionNumber()
338 {
339 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
340 $instance = $this->getAccessibleMock(CoreVersionService::class, ['getInstalledVersion'], [], '', false);
341 $instance->expects($this->once())->method('getInstalledVersion')->will($this->returnValue('7.2.0'));
342 $this->assertSame('7', $instance->_call('getInstalledMajorVersion'));
343 }
344
345 /**
346 * Data provider
347 */
348 public function getMajorVersionDataProvider()
349 {
350 return [
351 '7.2' => [
352 '7.2.0',
353 '7',
354 ],
355 '7.4-dev' => [
356 '7.4-dev',
357 '7',
358 ],
359 '4.5' => [
360 '4.5.40',
361 '4',
362 ],
363 ];
364 }
365
366 /**
367 * @test
368 * @dataProvider getMajorVersionDataProvider
369 * @param string $version
370 * @param string $expectedMajor
371 * @throws \InvalidArgumentException
372 */
373 public function getMajorVersionReturnsCorrectMajorVersion($version, $expectedMajor)
374 {
375 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
376 $instance = $this->getAccessibleMock(CoreVersionService::class, ['dummy'], [], '', false);
377 $this->assertSame($expectedMajor, $instance->_call('getMajorVersion', $version));
378 }
379
380 /**
381 * @test
382 */
383 public function getVersionMatrixThrowsExceptionIfVersionMatrixIsNotYetSetInRegistry()
384 {
385 $this->expectException(CoreVersionServiceException::class);
386 $this->expectExceptionCode(1380898792);
387 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
388 $instance = $this->getAccessibleMock(CoreVersionService::class, ['fetchVersionMatrixFromRemote'], [], '', false);
389 $registry = $this->createMock(Registry::class);
390 $registry->expects($this->once())->method('get')->will($this->returnValue(null));
391 $instance->_set('registry', $registry);
392 $instance->_call('getVersionMatrix');
393 }
394
395 /**
396 * @test
397 */
398 public function getVersionMatrixReturnsMatrixFromRegistry()
399 {
400 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
401 $instance = $this->getAccessibleMock(CoreVersionService::class, ['fetchVersionMatrixFromRemote'], [], '', false);
402 $registry = $this->createMock(Registry::class);
403 $versionArray = [$this->getUniqueId()];
404 $registry->expects($this->once())->method('get')->will($this->returnValue($versionArray));
405 $instance->_set('registry', $registry);
406 $this->assertSame($versionArray, $instance->_call('getVersionMatrix'));
407 }
408
409 /**
410 * @test
411 */
412 public function getReleaseTimestampOfVersionThrowsExceptionIfReleaseDateIsNotDefined()
413 {
414 $this->expectException(CoreVersionServiceException::class);
415 $this->expectExceptionCode(1380905853);
416 $versionMatrix = [
417 '7' => [
418 'releases' => [
419 '7.2.0' => []
420 ],
421 ],
422 ];
423 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
424 $instance = $this->getAccessibleMock(
425 CoreVersionService::class,
426 ['getVersionMatrix', 'getMajorVersion', 'ensureVersionExistsInMatrix'],
427 [],
428 '',
429 false
430 );
431 $instance->expects($this->once())->method('getMajorVersion')->will($this->returnValue('7'));
432 $instance->expects($this->once())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
433 $instance->_call('getReleaseTimestampOfVersion', '7.2.0');
434 }
435
436 /**
437 * @test
438 */
439 public function getReleaseTimestampOfVersionReturnsTimestamp()
440 {
441 $versionMatrixFixtureFile = __DIR__ . '/Fixtures/VersionMatrixFixture.php';
442 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
443 $instance = $this->getAccessibleMock(
444 CoreVersionService::class,
445 ['getVersionMatrix', 'getMajorVersion', 'ensureVersionExistsInMatrix'],
446 [],
447 '',
448 false
449 );
450 $instance->expects($this->once())->method('getMajorVersion')->will($this->returnValue('7'));
451 $instance->expects($this->once())->method('getVersionMatrix')->will($this->returnValue(require $versionMatrixFixtureFile));
452 $this->assertSame(1398968665, $instance->_call('getReleaseTimestampOfVersion', '7.3.1'));
453 }
454
455 /**
456 * @test
457 */
458 public function ensureVersionExistsInMatrixThrowsExceptionIfMinorVersionDoesNotExist()
459 {
460 $this->expectException(CoreVersionServiceException::class);
461 $this->expectExceptionCode(1380905851);
462 $versionMatrixFixtureFile = __DIR__ . '/Fixtures/VersionMatrixFixture.php';
463 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
464 $instance = $this->getAccessibleMock(
465 CoreVersionService::class,
466 ['getVersionMatrix', 'getMajorVersion'],
467 [],
468 '',
469 false
470 );
471 $instance->expects($this->once())->method('getMajorVersion')->will($this->returnValue('2'));
472 $instance->expects($this->once())->method('getVersionMatrix')->will($this->returnValue(require $versionMatrixFixtureFile));
473 $instance->_call('ensureVersionExistsInMatrix', '2.0.42');
474 }
475
476 /**
477 * @test
478 */
479 public function ensureVersionExistsInMatrixThrowsExceptionIfPatchLevelDoesNotExist()
480 {
481 $this->expectException(CoreVersionServiceException::class);
482 $this->expectExceptionCode(1380905852);
483 $versionMatrixFixtureFile = __DIR__ . '/Fixtures/VersionMatrixFixture.php';
484 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
485 $instance = $this->getAccessibleMock(
486 CoreVersionService::class,
487 ['getVersionMatrix', 'getMajorVersion'],
488 [],
489 '',
490 false
491 );
492 $instance->expects($this->once())->method('getMajorVersion')->will($this->returnValue('7'));
493 $instance->expects($this->once())->method('getVersionMatrix')->will($this->returnValue(require $versionMatrixFixtureFile));
494 $instance->_call('ensureVersionExistsInMatrix', '7.2.5');
495 }
496 }