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