29a9e9f8b25f5c4c55dc15a1bed8bdd15ed90435
[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\Compatibility\LoadedExtensionsArray;
19 use TYPO3\CMS\Core\Core\Environment;
20 use TYPO3\CMS\Core\Package\Package;
21 use TYPO3\CMS\Core\Package\PackageManager;
22 use TYPO3\CMS\Core\Tests\Unit\Utility\AccessibleProxies\ExtensionManagementUtilityAccessibleProxy;
23 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26
27 /**
28 * Test case
29 */
30 class ExtensionManagementUtilityTest extends UnitTestCase
31 {
32 /**
33 * @var \TYPO3\CMS\Core\Package\PackageManager
34 */
35 protected $backUpPackageManager;
36
37 /**
38 * Set up
39 */
40 protected function setUp()
41 {
42 $this->backUpPackageManager = ExtensionManagementUtilityAccessibleProxy::getPackageManager();
43 }
44
45 /**
46 * Tear down
47 */
48 protected function tearDown()
49 {
50 ExtensionManagementUtility::clearExtensionKeyMap();
51 ExtensionManagementUtilityAccessibleProxy::setPackageManager($this->backUpPackageManager);
52 ExtensionManagementUtilityAccessibleProxy::setCacheManager(null);
53 $GLOBALS['TYPO3_LOADED_EXT'] = new LoadedExtensionsArray($this->backUpPackageManager);
54 parent::tearDown();
55 }
56
57 /**
58 * @param string $packageKey
59 * @param array $packageMethods
60 * @return PackageManager|\PHPUnit_Framework_MockObject_MockObject
61 */
62 protected function createMockPackageManagerWithMockPackage($packageKey, $packageMethods = ['getPackagePath', 'getPackageKey'])
63 {
64 $packagePath = Environment::getVarPath() . '/tests/' . $packageKey . '/';
65 GeneralUtility::mkdir_deep($packagePath);
66 $this->testFilesToDelete[] = $packagePath;
67 $package = $this->getMockBuilder(Package::class)
68 ->disableOriginalConstructor()
69 ->setMethods($packageMethods)
70 ->getMock();
71 $packageManager = $this->getMockBuilder(PackageManager::class)
72 ->setMethods(['isPackageActive', 'getPackage', 'getActivePackages'])
73 ->getMock();
74 $package->expects($this->any())
75 ->method('getPackagePath')
76 ->will($this->returnValue($packagePath));
77 $package->expects($this->any())
78 ->method('getPackageKey')
79 ->will($this->returnValue($packageKey));
80 $packageManager->expects($this->any())
81 ->method('isPackageActive')
82 ->will($this->returnValueMap([
83 [null, false],
84 [$packageKey, true]
85 ]));
86 $packageManager->expects($this->any())
87 ->method('getPackage')
88 ->with($this->equalTo($packageKey))
89 ->will($this->returnValue($package));
90 $packageManager->expects($this->any())
91 ->method('getActivePackages')
92 ->will($this->returnValue([$packageKey => $package]));
93 return $packageManager;
94 }
95
96 ///////////////////////////////
97 // Tests concerning isLoaded
98 ///////////////////////////////
99 /**
100 * @test
101 */
102 public function isLoadedReturnsFalseIfExtensionIsNotLoadedAndExitIsDisabled()
103 {
104 $this->assertFalse(ExtensionManagementUtility::isLoaded($this->getUniqueId('foobar'), false));
105 }
106
107 ///////////////////////////////
108 // Tests concerning extPath
109 ///////////////////////////////
110 /**
111 * @test
112 */
113 public function extPathThrowsExceptionIfExtensionIsNotLoaded()
114 {
115 $this->expectException(\BadFunctionCallException::class);
116 $this->expectExceptionCode(1365429656);
117
118 $packageName = $this->getUniqueId('foo');
119 /** @var PackageManager|\PHPUnit_Framework_MockObject_MockObject $packageManager */
120 $packageManager = $this->getMockBuilder(PackageManager::class)
121 ->setMethods(['isPackageActive'])
122 ->getMock();
123 $packageManager->expects($this->once())
124 ->method('isPackageActive')
125 ->with($this->equalTo($packageName))
126 ->will($this->returnValue(false));
127 ExtensionManagementUtility::setPackageManager($packageManager);
128 ExtensionManagementUtility::extPath($packageName);
129 }
130
131 /**
132 * @test
133 */
134 public function extPathAppendsScriptNameToPath()
135 {
136 $package = $this->getMockBuilder(Package::class)
137 ->disableOriginalConstructor()
138 ->setMethods(['getPackagePath'])
139 ->getMock();
140 /** @var PackageManager|\PHPUnit_Framework_MockObject_MockObject $packageManager */
141 $packageManager = $this->getMockBuilder(PackageManager::class)
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 ->setMethods(['getActivePackages'])
213 ->getMock();
214 $packageManager->expects($this->once())
215 ->method('getActivePackages')
216 ->will($this->returnValue([$extensionKey => $package]));
217 ExtensionManagementUtility::setPackageManager($packageManager);
218 $this->assertEquals($extensionKey, ExtensionManagementUtility::getExtensionKeyByPrefix($extensionPrefix));
219 }
220
221 /**
222 * @test
223 * @see ExtensionManagementUtility::getExtensionKeyByPrefix
224 */
225 public function getExtensionKeyByPrefixForLoadedExtensionWithoutUnderscoresReturnsExtensionKey()
226 {
227 ExtensionManagementUtility::clearExtensionKeyMap();
228 $uniqueSuffix = $this->getUniqueId('test');
229 $extensionKey = 'kickstarter' . $uniqueSuffix;
230 $extensionPrefix = 'tx_kickstarter' . $uniqueSuffix;
231 $package = $this->getMockBuilder(Package::class)
232 ->disableOriginalConstructor()
233 ->setMethods(['getPackageKey'])
234 ->getMock();
235 $package->expects($this->exactly(2))
236 ->method('getPackageKey')
237 ->will($this->returnValue($extensionKey));
238 /** @var PackageManager|\PHPUnit_Framework_MockObject_MockObject $packageManager */
239 $packageManager = $this->getMockBuilder(PackageManager::class)
240 ->setMethods(['getActivePackages'])
241 ->getMock();
242 $packageManager->expects($this->once())
243 ->method('getActivePackages')
244 ->will($this->returnValue([$extensionKey => $package]));
245 ExtensionManagementUtility::setPackageManager($packageManager);
246 $this->assertEquals($extensionKey, ExtensionManagementUtility::getExtensionKeyByPrefix($extensionPrefix));
247 }
248
249 /**
250 * @test
251 * @see ExtensionManagementUtility::getExtensionKeyByPrefix
252 */
253 public function getExtensionKeyByPrefixForNotLoadedExtensionReturnsFalse()
254 {
255 ExtensionManagementUtility::clearExtensionKeyMap();
256 $uniqueSuffix = $this->getUniqueId('test');
257 $extensionPrefix = 'tx_unloadedextension' . $uniqueSuffix;
258 $this->assertFalse(ExtensionManagementUtility::getExtensionKeyByPrefix($extensionPrefix));
259 }
260
261 /////////////////////////////////////////
262 // Tests concerning removeCacheFiles
263 /////////////////////////////////////////
264 /**
265 * @test
266 */
267 public function removeCacheFilesFlushesSystemCaches()
268 {
269 /** @var CacheManager|\PHPUnit_Framework_MockObject_MockObject $mockCacheManager */
270 $mockCacheManager = $this->getMockBuilder(CacheManager::class)
271 ->setMethods(['flushCachesInGroup'])
272 ->getMock();
273 $mockCacheManager->expects($this->once())->method('flushCachesInGroup')->with('system');
274 ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
275 ExtensionManagementUtility::removeCacheFiles();
276 }
277 }