[!!!][TASK] Remove deprecated class Package\DependencyResolver
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / UnitDeprecated / Utility / ExtensionManagementUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Utility;
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\Cache\CacheManager;
18 use TYPO3\CMS\Core\Core\Environment;
19 use TYPO3\CMS\Core\Package\Package;
20 use TYPO3\CMS\Core\Package\PackageManager;
21 use TYPO3\CMS\Core\Tests\Unit\Utility\AccessibleProxies\ExtensionManagementUtilityAccessibleProxy;
22 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25
26 /**
27 * Test case
28 */
29 class ExtensionManagementUtilityTest extends UnitTestCase
30 {
31 /**
32 * @var \TYPO3\CMS\Core\Package\PackageManager
33 */
34 protected $backUpPackageManager;
35
36 /**
37 * Set up
38 */
39 protected function setUp()
40 {
41 $this->backUpPackageManager = ExtensionManagementUtilityAccessibleProxy::getPackageManager();
42 }
43
44 /**
45 * Tear down
46 */
47 protected function tearDown()
48 {
49 ExtensionManagementUtility::clearExtensionKeyMap();
50 ExtensionManagementUtilityAccessibleProxy::setPackageManager($this->backUpPackageManager);
51 ExtensionManagementUtilityAccessibleProxy::setCacheManager(null);
52 parent::tearDown();
53 }
54
55 /**
56 * @param string $packageKey
57 * @param array $packageMethods
58 * @return PackageManager|\PHPUnit_Framework_MockObject_MockObject
59 */
60 protected function createMockPackageManagerWithMockPackage($packageKey, $packageMethods = ['getPackagePath', 'getPackageKey'])
61 {
62 $packagePath = Environment::getVarPath() . '/tests/' . $packageKey . '/';
63 GeneralUtility::mkdir_deep($packagePath);
64 $this->testFilesToDelete[] = $packagePath;
65 $package = $this->getMockBuilder(Package::class)
66 ->disableOriginalConstructor()
67 ->setMethods($packageMethods)
68 ->getMock();
69 $packageManager = $this->getMockBuilder(PackageManager::class)
70 ->setMethods(['isPackageActive', 'getPackage', 'getActivePackages'])
71 ->getMock();
72 $package->expects($this->any())
73 ->method('getPackagePath')
74 ->will($this->returnValue($packagePath));
75 $package->expects($this->any())
76 ->method('getPackageKey')
77 ->will($this->returnValue($packageKey));
78 $packageManager->expects($this->any())
79 ->method('isPackageActive')
80 ->will($this->returnValueMap([
81 [null, false],
82 [$packageKey, true]
83 ]));
84 $packageManager->expects($this->any())
85 ->method('getPackage')
86 ->with($this->equalTo($packageKey))
87 ->will($this->returnValue($package));
88 $packageManager->expects($this->any())
89 ->method('getActivePackages')
90 ->will($this->returnValue([$packageKey => $package]));
91 return $packageManager;
92 }
93
94 ///////////////////////////////
95 // Tests concerning isLoaded
96 ///////////////////////////////
97 /**
98 * @test
99 */
100 public function isLoadedReturnsFalseIfExtensionIsNotLoadedAndExitIsDisabled()
101 {
102 $this->assertFalse(ExtensionManagementUtility::isLoaded($this->getUniqueId('foobar'), false));
103 }
104
105 ///////////////////////////////
106 // Tests concerning extPath
107 ///////////////////////////////
108 /**
109 * @test
110 */
111 public function extPathThrowsExceptionIfExtensionIsNotLoaded()
112 {
113 $this->expectException(\BadFunctionCallException::class);
114 $this->expectExceptionCode(1365429656);
115
116 $packageName = $this->getUniqueId('foo');
117 /** @var PackageManager|\PHPUnit_Framework_MockObject_MockObject $packageManager */
118 $packageManager = $this->getMockBuilder(PackageManager::class)
119 ->disableOriginalConstructor()
120 ->setMethods(['isPackageActive'])
121 ->getMock();
122 $packageManager->expects($this->once())
123 ->method('isPackageActive')
124 ->with($this->equalTo($packageName))
125 ->will($this->returnValue(false));
126 ExtensionManagementUtility::setPackageManager($packageManager);
127 ExtensionManagementUtility::extPath($packageName);
128 }
129
130 /**
131 * @test
132 */
133 public function extPathAppendsScriptNameToPath()
134 {
135 $package = $this->getMockBuilder(Package::class)
136 ->disableOriginalConstructor()
137 ->setMethods(['getPackagePath'])
138 ->getMock();
139 /** @var PackageManager|\PHPUnit_Framework_MockObject_MockObject $packageManager */
140 $packageManager = $this->getMockBuilder(PackageManager::class)
141 ->disableOriginalConstructor()
142 ->setMethods(['isPackageActive', 'getPackage'])
143 ->getMock();
144 $package->expects($this->once())
145 ->method('getPackagePath')
146 ->will($this->returnValue(Environment::getPublicPath() . '/foo/'));
147 $packageManager->expects($this->once())
148 ->method('isPackageActive')
149 ->with($this->equalTo('foo'))
150 ->will($this->returnValue(true));
151 $packageManager->expects($this->once())
152 ->method('getPackage')
153 ->with('foo')
154 ->will($this->returnValue($package));
155 ExtensionManagementUtility::setPackageManager($packageManager);
156 $this->assertSame(Environment::getPublicPath() . '/foo/bar.txt', ExtensionManagementUtility::extPath('foo', 'bar.txt'));
157 }
158
159 //////////////////////
160 // Utility functions
161 //////////////////////
162 /**
163 * Generates a basic TCA for a given table.
164 *
165 * @param string $table name of the table, must not be empty
166 * @return array generated TCA for the given table, will not be empty
167 */
168 private function generateTCAForTable($table)
169 {
170 $tca = [];
171 $tca[$table] = [];
172 $tca[$table]['columns'] = [
173 'fieldA' => [],
174 'fieldC' => []
175 ];
176 $tca[$table]['types'] = [
177 'typeA' => ['showitem' => 'fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, fieldC1, fieldD, fieldD1'],
178 'typeB' => ['showitem' => 'fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, fieldC1, fieldD, fieldD1'],
179 'typeC' => ['showitem' => 'fieldC;;paletteD']
180 ];
181 $tca[$table]['palettes'] = [
182 'paletteA' => ['showitem' => 'fieldX, fieldX1, fieldY'],
183 'paletteB' => ['showitem' => 'fieldX, fieldX1, fieldY'],
184 'paletteC' => ['showitem' => 'fieldX, fieldX1, fieldY'],
185 'paletteD' => ['showitem' => 'fieldX, fieldX1, fieldY']
186 ];
187 return $tca;
188 }
189
190 /////////////////////////////////////////////
191 // Tests concerning getExtensionKeyByPrefix
192 /////////////////////////////////////////////
193 /**
194 * @test
195 * @see ExtensionManagementUtility::getExtensionKeyByPrefix
196 */
197 public function getExtensionKeyByPrefixForLoadedExtensionWithUnderscoresReturnsExtensionKey()
198 {
199 ExtensionManagementUtility::clearExtensionKeyMap();
200 $uniqueSuffix = $this->getUniqueId('test');
201 $extensionKey = 'tt_news' . $uniqueSuffix;
202 $extensionPrefix = 'tx_ttnews' . $uniqueSuffix;
203 $package = $this->getMockBuilder(Package::class)
204 ->disableOriginalConstructor()
205 ->setMethods(['getPackageKey'])
206 ->getMock();
207 $package->expects($this->exactly(2))
208 ->method('getPackageKey')
209 ->will($this->returnValue($extensionKey));
210 /** @var PackageManager|\PHPUnit_Framework_MockObject_MockObject $packageManager */
211 $packageManager = $this->getMockBuilder(PackageManager::class)
212 ->disableOriginalConstructor()
213 ->setMethods(['getActivePackages'])
214 ->getMock();
215 $packageManager->expects($this->once())
216 ->method('getActivePackages')
217 ->will($this->returnValue([$extensionKey => $package]));
218 ExtensionManagementUtility::setPackageManager($packageManager);
219 $this->assertEquals($extensionKey, ExtensionManagementUtility::getExtensionKeyByPrefix($extensionPrefix));
220 }
221
222 /**
223 * @test
224 * @see ExtensionManagementUtility::getExtensionKeyByPrefix
225 */
226 public function getExtensionKeyByPrefixForLoadedExtensionWithoutUnderscoresReturnsExtensionKey()
227 {
228 ExtensionManagementUtility::clearExtensionKeyMap();
229 $uniqueSuffix = $this->getUniqueId('test');
230 $extensionKey = 'kickstarter' . $uniqueSuffix;
231 $extensionPrefix = 'tx_kickstarter' . $uniqueSuffix;
232 $package = $this->getMockBuilder(Package::class)
233 ->disableOriginalConstructor()
234 ->setMethods(['getPackageKey'])
235 ->getMock();
236 $package->expects($this->exactly(2))
237 ->method('getPackageKey')
238 ->will($this->returnValue($extensionKey));
239 /** @var PackageManager|\PHPUnit_Framework_MockObject_MockObject $packageManager */
240 $packageManager = $this->getMockBuilder(PackageManager::class)
241 ->disableOriginalConstructor()
242 ->setMethods(['getActivePackages'])
243 ->getMock();
244 $packageManager->expects($this->once())
245 ->method('getActivePackages')
246 ->will($this->returnValue([$extensionKey => $package]));
247 ExtensionManagementUtility::setPackageManager($packageManager);
248 $this->assertEquals($extensionKey, ExtensionManagementUtility::getExtensionKeyByPrefix($extensionPrefix));
249 }
250
251 /**
252 * @test
253 * @see ExtensionManagementUtility::getExtensionKeyByPrefix
254 */
255 public function getExtensionKeyByPrefixForNotLoadedExtensionReturnsFalse()
256 {
257 ExtensionManagementUtility::clearExtensionKeyMap();
258 $uniqueSuffix = $this->getUniqueId('test');
259 $extensionPrefix = 'tx_unloadedextension' . $uniqueSuffix;
260 $this->assertFalse(ExtensionManagementUtility::getExtensionKeyByPrefix($extensionPrefix));
261 }
262
263 /////////////////////////////////////////
264 // Tests concerning removeCacheFiles
265 /////////////////////////////////////////
266 /**
267 * @test
268 */
269 public function removeCacheFilesFlushesSystemCaches()
270 {
271 /** @var CacheManager|\PHPUnit_Framework_MockObject_MockObject $mockCacheManager */
272 $mockCacheManager = $this->getMockBuilder(CacheManager::class)
273 ->setMethods(['flushCachesInGroup'])
274 ->getMock();
275 $mockCacheManager->expects($this->once())->method('flushCachesInGroup')->with('system');
276 ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
277 ExtensionManagementUtility::removeCacheFiles();
278 }
279 }