[TASK] Make ExtensionManagementServiceTest notice free
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Tests / Unit / Service / ExtensionManagementServiceTest.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Extensionmanager\Tests\Unit\Service;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Extensionmanager\Domain\Model\DownloadQueue;
19 use TYPO3\CMS\Extensionmanager\Domain\Model\Extension;
20 use TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService;
21 use TYPO3\CMS\Extensionmanager\Utility\DependencyUtility;
22 use TYPO3\CMS\Extensionmanager\Utility\DownloadUtility;
23 use TYPO3\CMS\Extensionmanager\Utility\ExtensionModelUtility;
24 use TYPO3\CMS\Extensionmanager\Utility\InstallUtility;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26
27 /**
28 * Testcase
29 */
30 class ExtensionManagementServiceTest extends UnitTestCase
31 {
32 /**
33 * @test
34 */
35 public function resolveDependenciesCallsDownloadDependenciesIfDownloadKeyExistsInQueue()
36 {
37 $managementMock = $this->getAccessibleMock(
38 ExtensionManagementService::class,
39 ['downloadDependencies', 'uninstallDependenciesToBeUpdated', 'setInExtensionRepository', 'downloadMainExtension', 'isAutomaticInstallationEnabled']
40 );
41 $managementMock->expects($this->any())->method('downloadMainExtension')->will($this->returnValue([]));
42 $managementMock->expects($this->any())->method('isAutomaticInstallationEnabled')->will($this->returnValue([false]));
43 $extensionModelMock = $this->getAccessibleMock(Extension::class);
44 $extensionModelMock->_set('extensionKey', 'foobar');
45 $extensionModelMock->_set('version', '1.0.0');
46 $dependencyUtilityMock = $this->getAccessibleMock(DependencyUtility::class, ['checkDependencies']);
47 $dependencyUtilityMock->expects($this->atLeastOnce())->method('checkDependencies');
48 $managementMock->_set('dependencyUtility', $dependencyUtilityMock);
49 $downloadQueueMock = $this->getAccessibleMock(DownloadQueue::class, ['isCopyQueueEmpty', 'isQueueEmpty', 'resetExtensionQueue', 'addExtensionToInstallQueue']);
50 $downloadQueueMock->expects($this->any())->method('isCopyQueueEmpty')->willReturn(true);
51 $downloadQueueMock->expects($this->at(1))->method('isQueueEmpty')->with('download')->willReturn(false);
52 $downloadQueueMock->expects($this->at(4))->method('isQueueEmpty')->with('download')->willReturn(true);
53 $downloadQueueMock->expects($this->at(5))->method('isQueueEmpty')->with('update')->willReturn(true);
54 $downloadQueueMock->expects($this->atLeastOnce())->method('resetExtensionQueue')->will($this->returnValue([
55 'download' => [
56 'foo' => $extensionModelMock
57 ]
58 ]));
59 $managementMock->_set('downloadQueue', $downloadQueueMock);
60 $managementMock->expects($this->once())->method('downloadDependencies')->with(['foo' => $extensionModelMock])->will($this->returnValue([]));
61 $managementMock->_call('installExtension', $extensionModelMock);
62 }
63
64 /**
65 * @test
66 */
67 public function resolveDependenciesCallsUpdateAndDownloadDependenciesIfUpdateKeyExistsInQueue()
68 {
69 $managementMock = $this->getAccessibleMock(
70 ExtensionManagementService::class,
71 ['downloadDependencies', 'uninstallDependenciesToBeUpdated', 'setInExtensionRepository', 'downloadMainExtension', 'isAutomaticInstallationEnabled']
72 );
73 $managementMock->expects($this->any())->method('downloadMainExtension')->will($this->returnValue([]));
74 $managementMock->expects($this->any())->method('isAutomaticInstallationEnabled')->will($this->returnValue(true));
75 $extensionModelMock = $this->getAccessibleMock(Extension::class);
76 $extensionModelMock->_set('extensionKey', 'foobar');
77 $extensionModelMock->_set('version', '1.0.0');
78 $dependencyUtilityMock = $this->getAccessibleMock(DependencyUtility::class, ['checkDependencies']);
79 $dependencyUtilityMock->expects($this->atLeastOnce())->method('checkDependencies');
80 $managementMock->_set('dependencyUtility', $dependencyUtilityMock);
81 $downloadQueueMock = $this->getAccessibleMock(DownloadQueue::class, ['isCopyQueueEmpty', 'isQueueEmpty', 'resetExtensionQueue', 'addExtensionToInstallQueue']);
82 $downloadQueueMock->expects($this->any())->method('isCopyQueueEmpty')->willReturn(true);
83 $downloadQueueMock->expects($this->at(1))->method('isQueueEmpty')->with('download')->willReturn(false);
84 $downloadQueueMock->expects($this->at(4))->method('isQueueEmpty')->with('download')->willReturn(true);
85 $downloadQueueMock->expects($this->at(5))->method('isQueueEmpty')->with('update')->willReturn(true);
86 $downloadQueueMock->expects($this->atLeastOnce())->method('resetExtensionQueue')->will($this->returnValue([
87 'update' => [
88 'foo' => $extensionModelMock
89 ]
90 ]));
91 $managementMock->_set('downloadQueue', $downloadQueueMock);
92 $managementMock->expects($this->once())->method('downloadDependencies')->with(['foo' => $extensionModelMock])->will($this->returnValue([]));
93 $managementMock->expects($this->once())->method('uninstallDependenciesToBeUpdated')->with(['foo' => $extensionModelMock])->will($this->returnValue([]));
94 $managementMock->_call('installExtension', $extensionModelMock);
95 }
96
97 /**
98 * @test
99 */
100 public function downloadDependenciesCallsDownloadUtilityDownloadMethod()
101 {
102 $managementMock = $this->getAccessibleMock(ExtensionManagementService::class, [
103 'dummy'
104 ]);
105
106 $extensionModelMock = $this->getAccessibleMock(Extension::class, ['getExtensionKey']);
107 $extensionModelMock->_set('extensionKey', 'foobar');
108 $extensionModelMock->_set('version', '1.0.0');
109
110 $dependencyUtilityMock = $this->getAccessibleMock(DependencyUtility::class);
111 $dependencyUtilityMock->expects($this->atLeastOnce())->method('checkDependencies');
112 $installUtilityMock = $this->getMockBuilder(InstallUtility::class)->getMock();
113 $installUtilityMock->expects($this->any())->method('enrichExtensionWithDetails')->will($this->returnValue([]));
114 $extensionModelUtilityMock = $this->getMockBuilder(ExtensionModelUtility::class)->getMock();
115 $extensionModelUtilityMock->expects($this->any())->method('mapExtensionArrayToModel')->will($this->returnValue($extensionModelMock));
116 $managementMock->_set('dependencyUtility', $dependencyUtilityMock);
117 $managementMock->_set('installUtility', $installUtilityMock);
118 $managementMock->_set('extensionModelUtility', $extensionModelUtilityMock);
119
120 $downloadQueue = [
121 $extensionModelMock
122 ];
123 $downloadQueueMock = $this->getAccessibleMock(DownloadQueue::class, ['removeExtensionFromQueue', 'addExtensionToInstallQueue']);
124 $downloadUtilityMock = $this->getAccessibleMock(DownloadUtility::class, ['download']);
125 $downloadUtilityMock->expects($this->once())->method('download')->with($extensionModelMock);
126 $managementMock->_set('downloadUtility', $downloadUtilityMock);
127 $managementMock->_set('downloadQueue', $downloadQueueMock);
128 $managementMock->_call('downloadDependencies', $downloadQueue);
129 }
130
131 /**
132 * @test
133 */
134 public function downloadDependenciesCallsRemoveExtensionFromQueue()
135 {
136 $managementMock = $this->getAccessibleMock(ExtensionManagementService::class, [
137 'dummy'
138 ]);
139
140 /** @var Extension $extensionModelMock */
141 $extensionModelMock = $this->getMockBuilder(Extension::class)
142 ->setMethods(['getExtensionKey'])
143 ->getMock();
144 $extensionModelMock->setExtensionKey('foobar');
145 $extensionModelMock->setVersion('1.0.0');
146 $downloadQueue = [
147 $extensionModelMock
148 ];
149
150 $dependencyUtilityMock = $this->getAccessibleMock(DependencyUtility::class);
151 $dependencyUtilityMock->expects($this->atLeastOnce())->method('checkDependencies');
152 $installUtilityMock = $this->getMockBuilder(InstallUtility::class)->getMock();
153 $installUtilityMock->expects($this->any())->method('enrichExtensionWithDetails')->will($this->returnValue([]));
154 $extensionModelUtilityMock = $this->getMockBuilder(ExtensionModelUtility::class)->getMock();
155 $extensionModelUtilityMock->expects($this->any())->method('mapExtensionArrayToModel')->will($this->returnValue($extensionModelMock));
156 $managementMock->_set('dependencyUtility', $dependencyUtilityMock);
157 $managementMock->_set('installUtility', $installUtilityMock);
158 $managementMock->_set('extensionModelUtility', $extensionModelUtilityMock);
159
160 $downloadQueueMock = $this->getAccessibleMock(DownloadQueue::class, ['removeExtensionFromQueue', 'addExtensionToInstallQueue']);
161 $downloadUtilityMock = $this->getAccessibleMock(DownloadUtility::class, ['download']);
162 $downloadQueueMock->expects($this->once())->method('removeExtensionFromQueue')->with($extensionModelMock);
163 $managementMock->_set('downloadUtility', $downloadUtilityMock);
164 $managementMock->_set('downloadQueue', $downloadQueueMock);
165 $managementMock->_call('downloadDependencies', $downloadQueue);
166 }
167
168 /**
169 * @test
170 */
171 public function downloadDependenciesReturnsResolvedDependencies()
172 {
173 $managementMock = $this->getAccessibleMock(ExtensionManagementService::class, [
174 'dummy'
175 ]);
176
177 $extensionModelMock = $this->getAccessibleMock(Extension::class, ['getExtensionKey']);
178 $extensionModelMock->_set('extensionKey', 'foobar');
179 $extensionModelMock->_set('version', '1.0.0');
180 $downloadQueue = [
181 $extensionModelMock
182 ];
183
184 $dependencyUtilityMock = $this->getAccessibleMock(DependencyUtility::class);
185 $dependencyUtilityMock->expects($this->atLeastOnce())->method('checkDependencies');
186 $installUtilityMock = $this->getMockBuilder(InstallUtility::class)->getMock();
187 $installUtilityMock->expects($this->any())->method('enrichExtensionWithDetails')->will($this->returnValue([]));
188 $extensionModelUtilityMock = $this->getMockBuilder(ExtensionModelUtility::class)->getMock();
189 $extensionModelUtilityMock->expects($this->any())->method('mapExtensionArrayToModel')->will($this->returnValue($extensionModelMock));
190 $managementMock->_set('dependencyUtility', $dependencyUtilityMock);
191 $managementMock->_set('installUtility', $installUtilityMock);
192 $managementMock->_set('extensionModelUtility', $extensionModelUtilityMock);
193
194 $downloadQueueMock = $this->getAccessibleMock(DownloadQueue::class, ['removeExtensionFromQueue', 'addExtensionToInstallQueue']);
195 $downloadUtilityMock = $this->getAccessibleMock(DownloadUtility::class, ['download']);
196 $extensionModelMock->expects($this->atLeastOnce())->method('getExtensionKey')->will($this->returnValue('foobar'));
197 $managementMock->_set('downloadUtility', $downloadUtilityMock);
198 $managementMock->_set('downloadQueue', $downloadQueueMock);
199 $resolvedDependencies = $managementMock->_call('downloadDependencies', $downloadQueue);
200 $this->assertEquals(['downloaded' => ['foobar' => $extensionModelMock]], $resolvedDependencies);
201 }
202
203 /**
204 * @test
205 */
206 public function uninstallDependenciesToBeUpdatedCallsUninstall()
207 {
208 $managementMock = $this->getAccessibleMock(ExtensionManagementService::class, [
209 'dummy'
210 ]);
211 $extensionModelMock = $this->getAccessibleMock(Extension::class, ['getExtensionKey']);
212 $extensionModelMock->_set('extensionKey', 'foobar');
213 $extensionModelMock->_set('version', '1.0.0');
214 $extensionModelMock->expects($this->atLeastOnce())->method('getExtensionKey')->will($this->returnValue('foobar'));
215 $downloadQueue = [
216 $extensionModelMock
217 ];
218 $installUtility = $this->getAccessibleMock(InstallUtility::class, ['uninstall'], [], '', false);
219 $installUtility->expects($this->once())->method('uninstall')->with('foobar');
220 $managementMock->_set('installUtility', $installUtility);
221 $managementMock->_call('uninstallDependenciesToBeUpdated', $downloadQueue);
222 }
223
224 /**
225 * @test
226 */
227 public function uninstallDependenciesToBeUpdatedReturnsResolvedDependencies()
228 {
229 $managementMock = $this->getAccessibleMock(ExtensionManagementService::class, [
230 'dummy'
231 ]);
232 $extensionModelMock = $this->getAccessibleMock(Extension::class, ['getExtensionKey']);
233 $extensionModelMock->_set('extensionKey', 'foobar');
234 $extensionModelMock->_set('version', '1.0.0');
235 $extensionModelMock->expects($this->atLeastOnce())->method('getExtensionKey')->will($this->returnValue('foobar'));
236 $downloadQueue = [
237 $extensionModelMock
238 ];
239 $installUtility = $this->getAccessibleMock(InstallUtility::class, ['uninstall'], [], '', false);
240 $managementMock->_set('installUtility', $installUtility);
241 $resolvedDependencies = $managementMock->_call('uninstallDependenciesToBeUpdated', $downloadQueue);
242 $this->assertEquals(['updated' => ['foobar' => $extensionModelMock]], $resolvedDependencies);
243 }
244
245 /**
246 * @test
247 */
248 public function installDependenciesCallsInstall()
249 {
250 $managementMock = $this->getAccessibleMock(
251 ExtensionManagementService::class,
252 ['emitWillInstallExtensionsSignal', 'emitHasInstalledExtensionSignal']
253 );
254 /** @var Extension $extensionMock */
255 $extensionMock = $this->getMockBuilder(Extension::class)
256 ->setMethods(['dummy'])
257 ->getMock();
258 $extensionMock->setExtensionKey('foobar');
259 $installQueue = [
260 'foobar' => $extensionMock,
261 ];
262 $installUtility = $this->getAccessibleMock(InstallUtility::class, ['install', 'emitWillInstallExtensionsSignal'], [], '', false);
263 $installUtility->expects($this->once())->method('install')->with('foobar');
264 $managementMock->_set('installUtility', $installUtility);
265 $managementMock->_call('installDependencies', $installQueue);
266 }
267
268 /**
269 * @test
270 */
271 public function installDependenciesReturnsResolvedDependencies()
272 {
273 $managementMock = $this->getAccessibleMock(ExtensionManagementService::class, [
274 'emitWillInstallExtensionsSignal',
275 'emitHasInstalledExtensionSignal'
276 ]);
277 /** @var Extension $extensionMock */
278 $extensionMock = $this->getMockBuilder(Extension::class)
279 ->setMethods(['dummy'])
280 ->getMock();
281 $extensionMock->setExtensionKey('foobar');
282 $installQueue = [
283 'foobar' => $extensionMock,
284 ];
285 $installUtility = $this->getAccessibleMock(InstallUtility::class, ['install', 'emitWillInstallExtensionsSignal'], [], '', false);
286 $installUtility->expects($this->once())->method('install')->with('foobar');
287 $managementMock->_set('installUtility', $installUtility);
288 $resolvedDependencies = $managementMock->_call('installDependencies', $installQueue);
289 $this->assertEquals([
290 'installed' => [
291 'foobar' => 'foobar'
292 ]
293 ], $resolvedDependencies);
294 }
295 }