[FEATURE] Use new REST API for update checks
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Tests / Unit / Service / CoreVersionServiceTest.php
1 <?php
2 declare(strict_types=1);
3
4 namespace TYPO3\CMS\Install\Tests\Unit\Service;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Prophecy\Argument;
20 use TYPO3\CMS\Core\Http\JsonResponse;
21 use TYPO3\CMS\Core\Http\RequestFactory;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Install\Service\CoreVersionService;
24
25 /**
26 * Test case
27 */
28 class CoreVersionServiceTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
29 {
30 public function setUpApiResponse(string $url, array $responseData)
31 {
32 $response = new JsonResponse($responseData);
33 $requestFactory = $this->prophesize(RequestFactory::class);
34 $requestFactory->request('https://get.typo3.org/v1/api/' . $url, Argument::cetera())->willReturn($response);
35 GeneralUtility::addInstance(RequestFactory::class, $requestFactory->reveal());
36 }
37
38 /**
39 * @test
40 */
41 public function getTarGzSha1OfVersionReturnsSha1(): void
42 {
43 $this->setUpApiResponse(
44 'release/8.7.12',
45 [
46 'version' => '8.7.12',
47 'date' => '2018-03-22T11:36:39+00:00',
48 'type' => 'regular',
49 'tar_package' =>
50 [
51 'md5sum' => 'e835f454229b1077c9042f1bae4d46c7',
52 'sha1sum' => '185f3796751a903554a03378634a438beeef966e',
53 'sha256sum' => '77c3589161bea9d2c30e5d3d944443ba64b56813314ac2511b830e37d3297881',
54 ],
55 'zip_package' =>
56 [
57 'md5sum' => 'e5736ca3b3725966a4528a0c53fc849f',
58 'sha1sum' => 'eba49b9033da52d98f48876e97ed090a0c5593e0',
59 'sha256sum' => '7aad3f5864256f3f989c0378cec8bb729e728b30adb25e55ae713d8e682ef72b',
60 ],
61 ]
62 );
63 $coreVersionService = new CoreVersionService();
64 $result = $coreVersionService->getTarGzSha1OfVersion('8.7.12');
65 self::assertSame('185f3796751a903554a03378634a438beeef966e', $result);
66 }
67
68 /**
69 * @test
70 */
71 public function getTarGzSha1OfVersionReturnsSha1ReturnsEmptyStringIfNoVersionData(): void
72 {
73 $this->setUpApiResponse(
74 'release/8.7.44',
75 [
76 'error' =>
77 [
78 'code' => 404,
79 'message' => 'Not Found',
80 ],
81 ]
82 );
83 $coreVersionService = new CoreVersionService();
84 $result = $coreVersionService->getTarGzSha1OfVersion('8.7.44');
85 self::assertSame('', $result);
86 }
87
88 /**
89 * @test
90 */
91 public function isVersionActivelyMaintainedReturnsTrueIfMaintainedUntilIsNotSet(): void
92 {
93 $this->setUpApiResponse(
94 'major/9',
95 [
96 'version' => 9.0,
97 'title' => 'TYPO3 v9',
98 'release_date' => '2018-01-30T00:00:00+01:00',
99 ]
100 );
101
102 $instance = $this->getAccessibleMock(CoreVersionService::class, ['getInstalledVersion']);
103 $instance->expects($this->once())->method('getInstalledVersion')->will($this->returnValue('9.1.0'));
104
105 $result = $instance->isVersionActivelyMaintained();
106
107 self::assertTrue($result);
108 }
109
110 /**
111 * @test
112 */
113 public function isVersionActivelyMaintainedReturnsTrueIfMaintainedUntilIsAfterToday(): void
114 {
115 $this->setUpApiResponse(
116 'major/9',
117 [
118 'version' => 9.0,
119 'title' => 'TYPO3 v9',
120 'release_date' => '2018-01-30T00:00:00+01:00',
121 'maintained_until' => '2222-01-30T00:00:00+01:00',
122 ]
123 );
124
125 $instance = $this->getAccessibleMock(CoreVersionService::class, ['getInstalledVersion']);
126 $instance->expects($this->once())->method('getInstalledVersion')->will($this->returnValue('9.1.0'));
127
128 $result = $instance->isVersionActivelyMaintained();
129
130 self::assertTrue($result);
131 }
132
133 /**
134 * @test
135 */
136 public function isVersionActivelyMaintainedReturnsFalseIfMaintainedUntilWasBeforeToday(): void
137 {
138 $this->setUpApiResponse(
139 'major/7',
140 [
141 'version' => 7,
142 'title' => 'TYPO3 v7',
143 'maintained_until' => '2003-02-18T08:10:14+00:00',
144 'release_date' => '2002-06-03T12:01:07+00:00',
145 ]
146 );
147
148 $instance = $this->getAccessibleMock(CoreVersionService::class, ['getInstalledVersion']);
149 $instance->expects($this->once())->method('getInstalledVersion')->will($this->returnValue('7.6.25'));
150
151 $result = $instance->isVersionActivelyMaintained();
152
153 self::assertFalse($result);
154 }
155
156 /**
157 * @test
158 */
159 public function isYoungerPatchReleaseAvailableReturnsTrueIfNewerVersionExists(): void
160 {
161 $this->setUpApiResponse(
162 'major/9/release/latest',
163 [
164 'version' => '9.1.0',
165 'date' => '2018-01-30T15:44:52+00:00',
166 'type' => 'regular',
167 ]
168 );
169
170 $instance = $this->getAccessibleMock(CoreVersionService::class, ['getInstalledVersion']);
171 $instance->expects($this->atLeastOnce())->method('getInstalledVersion')->will($this->returnValue('9.0.0'));
172
173 $result = $instance->isYoungerPatchReleaseAvailable();
174
175 self::assertTrue($result);
176 }
177
178 /**
179 * @test
180 */
181 public function isYoungerPatchReleaseAvailableReturnsFalseIfNoNewerVersionExists(): void
182 {
183 $this->setUpApiResponse(
184 'major/9/release/latest',
185 [
186 'version' => '9.1.0',
187 'date' => '2018-01-30T15:44:52+00:00',
188 'type' => 'regular',
189 ]
190 );
191
192 $instance = $this->getAccessibleMock(CoreVersionService::class, ['getInstalledVersion']);
193 $instance->expects($this->atLeastOnce())->method('getInstalledVersion')->will($this->returnValue('9.1.0'));
194
195 $result = $instance->isYoungerPatchReleaseAvailable();
196
197 self::assertFalse($result);
198 }
199
200 /**
201 * @test
202 */
203 public function isUpdateSecurityRelevantReturnsTrueIfNewerSecurityUpdateExists(): void
204 {
205 $this->setUpApiResponse(
206 'major/8/release/latest/security',
207 [
208 'version' => '8.7.5',
209 'date' => '2017-09-05T10:54:18+00:00',
210 'type' => 'security',
211 ]
212 );
213
214 $instance = $this->getAccessibleMock(CoreVersionService::class, ['getInstalledVersion']);
215 $instance->expects($this->atLeastOnce())->method('getInstalledVersion')->will($this->returnValue('8.7.1'));
216
217 $result = $instance->isUpdateSecurityRelevant();
218
219 self::assertTrue($result);
220 }
221
222 /**
223 * @test
224 */
225 public function isUpdateSecurityRelevantReturnsFalseIfNoNewerSecurityUpdatesExist(): void
226 {
227 $this->setUpApiResponse(
228 'major/8/release/latest/security',
229 [
230 'version' => '8.7.5',
231 'date' => '2017-09-05T10:54:18+00:00',
232 'type' => 'security',
233 ]
234 );
235
236 $instance = $this->getAccessibleMock(CoreVersionService::class, ['getInstalledVersion']);
237 $instance->expects($this->atLeastOnce())->method('getInstalledVersion')->will($this->returnValue('8.7.5'));
238
239 $result = $instance->isUpdateSecurityRelevant();
240
241 self::assertFalse($result);
242 }
243
244 /**
245 * @test
246 */
247 public function getYoungestPatchReleaseReturnsLatestReleaseForCurrentMajorVersion(): void
248 {
249 $this->setUpApiResponse(
250 'major/9/release/latest',
251 [
252 'version' => '9.1.0',
253 'date' => '2018-01-30T15:44:52+00:00',
254 'type' => 'regular',
255 ]
256 );
257
258 $instance = $this->getAccessibleMock(CoreVersionService::class, ['getInstalledVersion']);
259 $instance->expects($this->atLeastOnce())->method('getInstalledVersion')->will($this->returnValue('9.0.0'));
260
261 $result = $instance->getYoungestPatchRelease();
262
263 self::assertSame('9.1.0', $result);
264 }
265
266 /**
267 * Data provider
268 */
269 public function getMajorVersionDataProvider(): array
270 {
271 return [
272 '7.2' => [
273 '7.2.0',
274 '7',
275 ],
276 '7.4-dev' => [
277 '7.4-dev',
278 '7',
279 ],
280 '4.5' => [
281 '4.5.40',
282 '4',
283 ],
284 ];
285 }
286
287 /**
288 * @test
289 * @dataProvider getMajorVersionDataProvider
290 * @param string $version
291 * @param string $expectedMajor
292 * @throws \InvalidArgumentException
293 */
294 public function getMajorVersionReturnsCorrectMajorVersion($version, $expectedMajor): void
295 {
296 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
297 $instance = $this->getAccessibleMock(CoreVersionService::class, ['dummy'], [], '', false);
298 $this->assertSame($expectedMajor, $instance->_call('getMajorVersion', $version));
299 }
300
301 /**
302 * @test
303 */
304 public function isInstalledVersionAReleasedVersionReturnsTrueForNonDevelopmentVersion(): void
305 {
306 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
307 $instance = $this->getAccessibleMock(CoreVersionService::class, ['getInstalledVersion'], [], '', false);
308 $instance->expects($this->once())->method('getInstalledVersion')->will($this->returnValue('7.2.0'));
309 $this->assertTrue($instance->isInstalledVersionAReleasedVersion());
310 }
311
312 /**
313 * @test
314 */
315 public function isInstalledVersionAReleasedVersionReturnsFalseForDevelopmentVersion()
316 {
317 /** @var $instance CoreVersionService|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
318 $instance = $this->getAccessibleMock(CoreVersionService::class, ['getInstalledVersion'], [], '', false);
319 $instance->expects($this->once())->method('getInstalledVersion')->will($this->returnValue('7.4-dev'));
320 $this->assertFalse($instance->isInstalledVersionAReleasedVersion());
321 }
322 }