[TASK] Deprecate $GLOBALS['TYPO3_LOADED_EXT']
[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 ->setMethods(['isPackageActive'])
120 ->getMock();
121 $packageManager->expects($this->once())
122 ->method('isPackageActive')
123 ->with($this->equalTo($packageName))
124 ->will($this->returnValue(false));
125 ExtensionManagementUtility::setPackageManager($packageManager);
126 ExtensionManagementUtility::extPath($packageName);
127 }
128
129 /**
130 * @test
131 */
132 public function extPathAppendsScriptNameToPath()
133 {
134 $package = $this->getMockBuilder(Package::class)
135 ->disableOriginalConstructor()
136 ->setMethods(['getPackagePath'])
137 ->getMock();
138 /** @var PackageManager|\PHPUnit_Framework_MockObject_MockObject $packageManager */
139 $packageManager = $this->getMockBuilder(PackageManager::class)
140 ->setMethods(['isPackageActive', 'getPackage'])
141 ->getMock();
142 $package->expects($this->once())
143 ->method('getPackagePath')
144 ->will($this->returnValue(Environment::getPublicPath() . '/foo/'));
145 $packageManager->expects($this->once())
146 ->method('isPackageActive')
147 ->with($this->equalTo('foo'))
148 ->will($this->returnValue(true));
149 $packageManager->expects($this->once())
150 ->method('getPackage')
151 ->with('foo')
152 ->will($this->returnValue($package));
153 ExtensionManagementUtility::setPackageManager($packageManager);
154 $this->assertSame(Environment::getPublicPath() . '/foo/bar.txt', ExtensionManagementUtility::extPath('foo', 'bar.txt'));
155 }
156
157 //////////////////////
158 // Utility functions
159 //////////////////////
160 /**
161 * Generates a basic TCA for a given table.
162 *
163 * @param string $table name of the table, must not be empty
164 * @return array generated TCA for the given table, will not be empty
165 */
166 private function generateTCAForTable($table)
167 {
168 $tca = [];
169 $tca[$table] = [];
170 $tca[$table]['columns'] = [
171 'fieldA' => [],
172 'fieldC' => []
173 ];
174 $tca[$table]['types'] = [
175 'typeA' => ['showitem' => 'fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, fieldC1, fieldD, fieldD1'],
176 'typeB' => ['showitem' => 'fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, fieldC1, fieldD, fieldD1'],
177 'typeC' => ['showitem' => 'fieldC;;paletteD']
178 ];
179 $tca[$table]['palettes'] = [
180 'paletteA' => ['showitem' => 'fieldX, fieldX1, fieldY'],
181 'paletteB' => ['showitem' => 'fieldX, fieldX1, fieldY'],
182 'paletteC' => ['showitem' => 'fieldX, fieldX1, fieldY'],
183 'paletteD' => ['showitem' => 'fieldX, fieldX1, fieldY']
184 ];
185 return $tca;
186 }
187
188 /////////////////////////////////////////////
189 // Tests concerning getExtensionKeyByPrefix
190 /////////////////////////////////////////////
191 /**
192 * @test
193 * @see ExtensionManagementUtility::getExtensionKeyByPrefix
194 */
195 public function getExtensionKeyByPrefixForLoadedExtensionWithUnderscoresReturnsExtensionKey()
196 {
197 ExtensionManagementUtility::clearExtensionKeyMap();
198 $uniqueSuffix = $this->getUniqueId('test');
199 $extensionKey = 'tt_news' . $uniqueSuffix;
200 $extensionPrefix = 'tx_ttnews' . $uniqueSuffix;
201 $package = $this->getMockBuilder(Package::class)
202 ->disableOriginalConstructor()
203 ->setMethods(['getPackageKey'])
204 ->getMock();
205 $package->expects($this->exactly(2))
206 ->method('getPackageKey')
207 ->will($this->returnValue($extensionKey));
208 /** @var PackageManager|\PHPUnit_Framework_MockObject_MockObject $packageManager */
209 $packageManager = $this->getMockBuilder(PackageManager::class)
210 ->setMethods(['getActivePackages'])
211 ->getMock();
212 $packageManager->expects($this->once())
213 ->method('getActivePackages')
214 ->will($this->returnValue([$extensionKey => $package]));
215 ExtensionManagementUtility::setPackageManager($packageManager);
216 $this->assertEquals($extensionKey, ExtensionManagementUtility::getExtensionKeyByPrefix($extensionPrefix));
217 }
218
219 /**
220 * @test
221 * @see ExtensionManagementUtility::getExtensionKeyByPrefix
222 */
223 public function getExtensionKeyByPrefixForLoadedExtensionWithoutUnderscoresReturnsExtensionKey()
224 {
225 ExtensionManagementUtility::clearExtensionKeyMap();
226 $uniqueSuffix = $this->getUniqueId('test');
227 $extensionKey = 'kickstarter' . $uniqueSuffix;
228 $extensionPrefix = 'tx_kickstarter' . $uniqueSuffix;
229 $package = $this->getMockBuilder(Package::class)
230 ->disableOriginalConstructor()
231 ->setMethods(['getPackageKey'])
232 ->getMock();
233 $package->expects($this->exactly(2))
234 ->method('getPackageKey')
235 ->will($this->returnValue($extensionKey));
236 /** @var PackageManager|\PHPUnit_Framework_MockObject_MockObject $packageManager */
237 $packageManager = $this->getMockBuilder(PackageManager::class)
238 ->setMethods(['getActivePackages'])
239 ->getMock();
240 $packageManager->expects($this->once())
241 ->method('getActivePackages')
242 ->will($this->returnValue([$extensionKey => $package]));
243 ExtensionManagementUtility::setPackageManager($packageManager);
244 $this->assertEquals($extensionKey, ExtensionManagementUtility::getExtensionKeyByPrefix($extensionPrefix));
245 }
246
247 /**
248 * @test
249 * @see ExtensionManagementUtility::getExtensionKeyByPrefix
250 */
251 public function getExtensionKeyByPrefixForNotLoadedExtensionReturnsFalse()
252 {
253 ExtensionManagementUtility::clearExtensionKeyMap();
254 $uniqueSuffix = $this->getUniqueId('test');
255 $extensionPrefix = 'tx_unloadedextension' . $uniqueSuffix;
256 $this->assertFalse(ExtensionManagementUtility::getExtensionKeyByPrefix($extensionPrefix));
257 }
258
259 /////////////////////////////////////////
260 // Tests concerning removeCacheFiles
261 /////////////////////////////////////////
262 /**
263 * @test
264 */
265 public function removeCacheFilesFlushesSystemCaches()
266 {
267 /** @var CacheManager|\PHPUnit_Framework_MockObject_MockObject $mockCacheManager */
268 $mockCacheManager = $this->getMockBuilder(CacheManager::class)
269 ->setMethods(['flushCachesInGroup'])
270 ->getMock();
271 $mockCacheManager->expects($this->once())->method('flushCachesInGroup')->with('system');
272 ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
273 ExtensionManagementUtility::removeCacheFiles();
274 }
275 }