44ae76ce82af47fc44fb2cf75db2355f26cb779c
[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 * Whitebox test of API method: This tests multiple methods, only 'current version' and 'version matrix' are mocked.
91 *
92 * @test
93 */
94 public function isYoungerPatchReleaseAvailableReturnsTrueIfYoungerReleaseIsAvailable() {
95 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
96 $instance = $this->getAccessibleMock(
97 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
98 array('getVersionMatrix', 'getInstalledVersion'),
99 array(),
100 '',
101 FALSE
102 );
103 $versionMatrix = array(
104 '6.2' => array(
105 'releases' => array(
106 '6.2.42' => array(
107 'type' => 'regular',
108 'date' => '2013-12-01 18:24:25 UTC',
109 ),
110 '6.2.41' => array(
111 'type' => 'regular',
112 'date' => '2013-11-01 18:24:25 UTC',
113 ),
114 ),
115 ),
116 );
117 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
118 $instance->expects($this->any())->method('getInstalledVersion')->will($this->returnValue('6.2.41'));
119 $this->assertTrue($instance->isYoungerPatchReleaseAvailable());
120 }
121
122 /**
123 * Whitebox test of API method: This tests multiple methods, only 'current version' and 'version matrix' are mocked.
124 *
125 * @test
126 */
127 public function isYoungerReleaseAvailableReturnsFalseIfNoYoungerReleaseExists() {
128 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
129 $instance = $this->getAccessibleMock(
130 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
131 array('getVersionMatrix', 'getInstalledVersion'),
132 array(),
133 '',
134 FALSE
135 );
136 $versionMatrix = array(
137 '6.2' => array(
138 'releases' => array(
139 '6.2.42' => array(
140 'type' => 'regular',
141 'date' => '2013-12-01 18:24:25 UTC',
142 ),
143 '6.2.41' => array(
144 'type' => 'regular',
145 'date' => '2013-11-01 18:24:25 UTC',
146 ),
147 ),
148 ),
149 );
150 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
151 $instance->expects($this->any())->method('getInstalledVersion')->will($this->returnValue('6.2.42'));
152 $this->assertFalse($instance->isYoungerPatchReleaseAvailable());
153 }
154
155 /**
156 * Whitebox test of API method: This tests multiple methods, only 'current version' and 'version matrix' are mocked.
157 *
158 * @test
159 */
160 public function isYoungerReleaseAvailableReturnsFalseIfOnlyADevelopmentReleaseIsYounger() {
161 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
162 $instance = $this->getAccessibleMock(
163 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
164 array('getVersionMatrix', 'getInstalledVersion'),
165 array(),
166 '',
167 FALSE
168 );
169 $versionMatrix = array(
170 '6.2' => array(
171 'releases' => array(
172 '6.2.42' => array(
173 'type' => 'development',
174 'date' => '2013-12-01 18:24:25 UTC',
175 ),
176 '6.2.41' => array(
177 'type' => 'regular',
178 'date' => '2013-11-01 18:24:25 UTC',
179 ),
180 ),
181 ),
182 );
183 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
184 $instance->expects($this->any())->method('getInstalledVersion')->will($this->returnValue('6.2.41'));
185 $this->assertFalse($instance->isYoungerPatchReleaseAvailable());
186 }
187
188 /**
189 * Whitebox test of API method: This tests multiple methods, only 'current version' and 'version matrix' are mocked.
190 *
191 * @test
192 */
193 public function isYoungerDevelopmentReleaseAvailableReturnsTrueIfADevelopmentReleaseIsYounger() {
194 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
195 $instance = $this->getAccessibleMock(
196 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
197 array('getVersionMatrix', 'getInstalledVersion'),
198 array(),
199 '',
200 FALSE
201 );
202 $versionMatrix = array(
203 '6.2' => array(
204 'releases' => array(
205 '6.2.42' => array(
206 'type' => 'development',
207 'date' => '2013-12-01 18:24:25 UTC',
208 ),
209 '6.2.41' => array(
210 'type' => 'regular',
211 'date' => '2013-11-01 18:24:25 UTC',
212 ),
213 ),
214 ),
215 );
216 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
217 $instance->expects($this->any())->method('getInstalledVersion')->will($this->returnValue('6.2.41'));
218 $this->assertTrue($instance->isYoungerPatchDevelopmentReleaseAvailable());
219 }
220
221 /**
222 * Whitebox test of API method: This tests multiple methods, only 'current version' and 'version matrix' are mocked.
223 *
224 * @test
225 */
226 public function isUpdateSecurityRelevantReturnsTrueIfAnUpdateIsSecurityRelevant() {
227 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
228 $instance = $this->getAccessibleMock(
229 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
230 array('getVersionMatrix', 'getInstalledVersion'),
231 array(),
232 '',
233 FALSE
234 );
235 $versionMatrix = array(
236 '6.2' => array(
237 'releases' => array(
238 '6.2.42' => array(
239 'type' => 'security',
240 'date' => '2013-12-01 18:24:25 UTC',
241 ),
242 '6.2.41' => array(
243 'type' => 'regular',
244 'date' => '2013-11-01 18:24:25 UTC',
245 ),
246 ),
247 ),
248 );
249 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
250 $instance->expects($this->any())->method('getInstalledVersion')->will($this->returnValue('6.2.41'));
251 $this->assertTrue($instance->isUpdateSecurityRelevant());
252 }
253
254 /**
255 * Whitebox test of API method: This tests multiple methods, only 'current version' and 'version matrix' are mocked.
256 *
257 * @test
258 */
259 public function isUpdateSecurityRelevantReturnsFalseIfUpdateIsNotSecurityRelevant() {
260 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
261 $instance = $this->getAccessibleMock(
262 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
263 array('getVersionMatrix', 'getInstalledVersion'),
264 array(),
265 '',
266 FALSE
267 );
268 $versionMatrix = array(
269 '6.2' => array(
270 'releases' => array(
271 '6.2.42' => array(
272 'type' => 'regular',
273 'date' => '2013-12-01 18:24:25 UTC',
274 ),
275 '6.2.41' => array(
276 'type' => 'regular',
277 'date' => '2013-11-01 18:24:25 UTC',
278 ),
279 ),
280 ),
281 );
282 $instance->expects($this->any())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
283 $instance->expects($this->any())->method('getInstalledVersion')->will($this->returnValue('6.2.41'));
284 $this->assertFalse($instance->isUpdateSecurityRelevant());
285 }
286
287 /**
288 * @test
289 */
290 public function getInstalledMinorVersionFetchesInstalledVersionNumber() {
291 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
292 $instance = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Service\\CoreVersionService', array('getInstalledVersion'), array(), '', FALSE);
293 $instance->expects($this->once())->method('getInstalledVersion')->will($this->returnValue('6.2.0'));
294 $this->assertSame('6.2', $instance->_call('getInstalledMinorVersion'));
295 }
296
297 /**
298 * Data provider
299 */
300 public function getMinorVersionDataProvider() {
301 return array(
302 '6.2.0' => array(
303 '6.2.0',
304 '6.2',
305 ),
306 '6.2-dev' => array(
307 '6.2-dev',
308 '6.2',
309 ),
310 '6.2.0alpha2' => array(
311 '6.2.0alpha2',
312 '6.2',
313 ),
314 '4.5.25' => array(
315 '4.5.25',
316 '4.5',
317 ),
318 );
319 }
320
321 /**
322 * @test
323 * @dataProvider getMinorVersionDataProvider
324 */
325 public function getMinorVersionReturnsCorrectMinorVersion($version, $expectedMajor) {
326 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
327 $instance = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Service\\CoreVersionService', array('dummy'), array(), '', FALSE);
328 $this->assertSame($expectedMajor, $instance->_call('getMinorVersion', $version));
329 }
330
331 /**
332 * @test
333 * @expectedException \TYPO3\CMS\Install\Service\Exception\CoreVersionServiceException
334 */
335 public function getVersionMatrixThrowsExceptionIfVersionMatrixIsNotYetSetInRegistry() {
336 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
337 $instance = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Service\\CoreVersionService', array('fetchVersionMatrixFromRemote'), array(), '', FALSE);
338 $registry = $this->getMock('TYPO3\CMS\Core\Registry');
339 $registry->expects($this->once())->method('get')->will($this->returnValue(NULL));
340 $instance->_set('registry', $registry);
341 $instance->_call('getVersionMatrix');
342 }
343
344 /**
345 * @test
346 */
347 public function getVersionMatrixReturnsMatrixFromRegistry() {
348 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
349 $instance = $this->getAccessibleMock('TYPO3\\CMS\\Install\\Service\\CoreVersionService', array('fetchVersionMatrixFromRemote'), array(), '', FALSE);
350 $registry = $this->getMock('TYPO3\CMS\Core\Registry');
351 $versionArray = array(uniqId());
352 $registry->expects($this->once())->method('get')->will($this->returnValue($versionArray));
353 $instance->_set('registry', $registry);
354 $this->assertSame($versionArray, $instance->_call('getVersionMatrix'));
355 }
356
357 /**
358 * @test
359 * @expectedException \TYPO3\CMS\Install\Service\Exception\CoreVersionServiceException
360 */
361 public function getReleaseTimestampOfVersionThrowsExceptionIfReleaseDateIsNotDefined() {
362 $versionMatrix = array(
363 '6.2' => array(
364 'releases' => array(
365 '6.2.42' => array()
366 ),
367 ),
368 );
369 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
370 $instance = $this->getAccessibleMock(
371 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
372 array('getVersionMatrix', 'getMinorVersion', 'ensureVersionExistsInMatrix'),
373 array(),
374 '',
375 FALSE
376 );
377 $instance->expects($this->once())->method('getMinorVersion')->will($this->returnValue('6.2'));
378 $instance->expects($this->once())->method('getVersionMatrix')->will($this->returnValue($versionMatrix));
379 $instance->_call('getReleaseTimestampOfVersion', '6.2.42');
380 }
381
382 /**
383 * @test
384 */
385 public function getReleaseTimestampOfVersionReturnsTimestamp() {
386 $versionMatrixFixtureFile = __DIR__ . '/Fixtures/VersionMatrixFixture.php';
387 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
388 $instance = $this->getAccessibleMock(
389 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
390 array('getVersionMatrix', 'getMinorVersion', 'ensureVersionExistsInMatrix'),
391 array(),
392 '',
393 FALSE
394 );
395 $instance->expects($this->once())->method('getMinorVersion')->will($this->returnValue('6.2'));
396 $instance->expects($this->once())->method('getVersionMatrix')->will($this->returnValue(require($versionMatrixFixtureFile)));
397 $this->assertSame(1380651865, $instance->_call('getReleaseTimestampOfVersion', '6.2.0alpha3'));
398 }
399
400 /**
401 * @test
402 * @expectedException \TYPO3\CMS\Install\Service\Exception\CoreVersionServiceException
403 */
404 public function ensureVersionExistsInMatrixThrowsExceptionIfMinorVersionDoesNotExist() {
405 $versionMatrixFixtureFile = __DIR__ . '/Fixtures/VersionMatrixFixture.php';
406 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
407 $instance = $this->getAccessibleMock(
408 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
409 array('getVersionMatrix', 'getMinorVersion'),
410 array(),
411 '',
412 FALSE
413 );
414 $instance->expects($this->once())->method('getMinorVersion')->will($this->returnValue('2.0'));
415 $instance->expects($this->once())->method('getVersionMatrix')->will($this->returnValue(require($versionMatrixFixtureFile)));
416 $instance->_call('ensureVersionExistsInMatrix', '2.0.42');
417 }
418
419 /**
420 * @test
421 * @expectedException \TYPO3\CMS\Install\Service\Exception\CoreVersionServiceException
422 */
423 public function ensureVersionExistsInMatrixThrowsExceptionIfPatchLevelDoesNotExist() {
424 $versionMatrixFixtureFile = __DIR__ . '/Fixtures/VersionMatrixFixture.php';
425 /** @var $instance \TYPO3\CMS\Install\Service\CoreVersionService|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
426 $instance = $this->getAccessibleMock(
427 'TYPO3\\CMS\\Install\\Service\\CoreVersionService',
428 array('getVersionMatrix', 'getMinorVersion'),
429 array(),
430 '',
431 FALSE
432 );
433 $instance->expects($this->once())->method('getMinorVersion')->will($this->returnValue('6.2'));
434 $instance->expects($this->once())->method('getVersionMatrix')->will($this->returnValue(require($versionMatrixFixtureFile)));
435 $instance->_call('ensureVersionExistsInMatrix', '6.2.5');
436 }
437 }