2235b34f9ee297a4a33be0b6ea6ab83dd33e666c
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Utility / ExtensionUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\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\Extbase\Core\Bootstrap;
18 use TYPO3\CMS\Extbase\Utility\ExtensionUtility;
19 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
20
21 /**
22 * Testcase for class \TYPO3\CMS\Extbase\Utility\ExtensionUtility
23 */
24 class ExtensionUtilityTest extends UnitTestCase
25 {
26 protected function setUp()
27 {
28 $GLOBALS['TSFE'] = new \stdClass();
29 $GLOBALS['TSFE']->tmpl = new \stdClass();
30 $GLOBALS['TSFE']->tmpl->setup = [];
31 $GLOBALS['TSFE']->tmpl->setup['tt_content.']['list.']['20.'] = [
32 '9' => 'CASE',
33 '9.' => [
34 'key.' => [
35 'field' => 'layout'
36 ],
37 0 => '< plugin.tt_news'
38 ],
39 'extensionname_someplugin' => 'USER',
40 'extensionname_someplugin.' => [
41 'userFunc' => Bootstrap::class . '->run',
42 'extensionName' => 'ExtensionName',
43 'pluginName' => 'SomePlugin'
44 ],
45 'someotherextensionname_secondplugin' => 'USER',
46 'someotherextensionname_secondplugin.' => [
47 'userFunc' => Bootstrap::class . '->run',
48 'extensionName' => 'SomeOtherExtensionName',
49 'pluginName' => 'SecondPlugin'
50 ],
51 'extensionname_thirdplugin' => 'USER',
52 'extensionname_thirdplugin.' => [
53 'userFunc' => Bootstrap::class . '->run',
54 'extensionName' => 'ExtensionName',
55 'pluginName' => 'ThirdPlugin'
56 ]
57 ];
58 }
59
60 /**
61 * @test
62 * @see \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin
63 */
64 public function configurePluginWorksForMinimalisticSetup()
65 {
66 $GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_setup.'] = [];
67 ExtensionUtility::configurePlugin('MyExtension', 'Pi1', ['Blog' => 'index']);
68 $staticTypoScript = $GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_setup.']['defaultContentRendering'];
69 $this->assertContains('tt_content.list.20.myextension_pi1 = USER', $staticTypoScript);
70 $this->assertContains('
71 userFunc = TYPO3\\CMS\\Extbase\\Core\\Bootstrap->run
72 extensionName = MyExtension
73 pluginName = Pi1', $staticTypoScript);
74 $this->assertNotContains('USER_INT', $staticTypoScript);
75 }
76
77 /**
78 * @test
79 * @see \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin
80 */
81 public function configurePluginCreatesCorrectDefaultTypoScriptSetup()
82 {
83 $GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_setup.'] = [];
84 ExtensionUtility::configurePlugin('MyExtension', 'Pi1', ['Blog' => 'index']);
85 $staticTypoScript = $GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_setup.']['defaultContentRendering'];
86 $this->assertContains('tt_content.list.20.myextension_pi1 = USER', $staticTypoScript);
87 }
88
89 /**
90 * @test
91 * @see \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin
92 */
93 public function configurePluginWorksForASingleControllerAction()
94 {
95 $GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_setup.'] = [];
96 ExtensionUtility::configurePlugin('MyExtension', 'Pi1', [
97 'FirstController' => 'index'
98 ]);
99 $staticTypoScript = $GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_setup.']['defaultContentRendering'];
100 $this->assertContains('tt_content.list.20.myextension_pi1 = USER', $staticTypoScript);
101 $this->assertContains('
102 extensionName = MyExtension
103 pluginName = Pi1', $staticTypoScript);
104 $expectedResult = [
105 'controllers' => [
106 'FirstController' => [
107 'actions' => ['index']
108 ]
109 ],
110 'pluginType' => 'list_type'
111 ];
112 $this->assertEquals($expectedResult, $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions']['MyExtension']['plugins']['Pi1']);
113 }
114
115 /**
116 * @test
117 * @see \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin
118 */
119 public function configurePluginThrowsExceptionIfExtensionNameIsEmpty()
120 {
121 $this->expectException(\InvalidArgumentException::class);
122 $this->expectExceptionCode(1239891990);
123 ExtensionUtility::configurePlugin('', 'SomePlugin', [
124 'FirstController' => 'index'
125 ]);
126 }
127
128 /**
129 * @test
130 * @see \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin
131 */
132 public function configurePluginThrowsExceptionIfPluginNameIsEmpty()
133 {
134 $this->expectException(\InvalidArgumentException::class);
135 $this->expectExceptionCode(1239891988);
136 ExtensionUtility::configurePlugin('MyExtension', '', [
137 'FirstController' => 'index'
138 ]);
139 }
140
141 /**
142 * @test
143 * @see \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin
144 */
145 public function configurePluginRespectsDefaultActionAsANonCacheableAction()
146 {
147 $GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_setup.'] = [];
148 ExtensionUtility::configurePlugin('MyExtension', 'Pi1', [
149 'FirstController' => 'index,show,new, create,delete,edit,update'
150 ], [
151 'FirstController' => 'index,show'
152 ]);
153 $staticTypoScript = $GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_setup.']['defaultContentRendering'];
154 $this->assertContains('tt_content.list.20.myextension_pi1 = USER', $staticTypoScript);
155 $this->assertContains('
156 extensionName = MyExtension
157 pluginName = Pi1', $staticTypoScript);
158 $expectedResult = [
159 'controllers' => [
160 'FirstController' => [
161 'actions' => ['index', 'show', 'new', 'create', 'delete', 'edit', 'update'],
162 'nonCacheableActions' => ['index', 'show']
163 ]
164 ],
165 'pluginType' => 'list_type'
166 ];
167 $this->assertEquals($expectedResult, $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions']['MyExtension']['plugins']['Pi1']);
168 }
169
170 /**
171 * @test
172 * @see \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin
173 */
174 public function configurePluginRespectsNonDefaultActionAsANonCacheableAction()
175 {
176 $GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_setup.'] = [];
177 ExtensionUtility::configurePlugin('MyExtension', 'Pi1', [
178 'FirstController' => 'index,show,new, create,delete,edit,update'
179 ], [
180 'FirstController' => 'new,show'
181 ]);
182 $staticTypoScript = $GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_setup.']['defaultContentRendering'];
183 $this->assertContains('tt_content.list.20.myextension_pi1 = USER', $staticTypoScript);
184 $this->assertContains('
185 extensionName = MyExtension
186 pluginName = Pi1', $staticTypoScript);
187 $expectedResult = [
188 'controllers' => [
189 'FirstController' => [
190 'actions' => ['index', 'show', 'new', 'create', 'delete', 'edit', 'update'],
191 'nonCacheableActions' => ['new', 'show']
192 ]
193 ],
194 'pluginType' => 'list_type'
195 ];
196 $this->assertEquals($expectedResult, $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions']['MyExtension']['plugins']['Pi1']);
197 }
198
199 /**
200 * @test
201 * @see \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin
202 */
203 public function configurePluginWorksForMultipleControllerActionsWithCacheableActionAsDefault()
204 {
205 $GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_setup.'] = [];
206 ExtensionUtility::configurePlugin('MyExtension', 'Pi1', [
207 'FirstController' => 'index,show,new,create,delete,edit,update',
208 'SecondController' => 'index,show,delete',
209 'ThirdController' => 'create'
210 ], [
211 'FirstController' => 'new,create,edit,update',
212 'ThirdController' => 'create'
213 ]);
214 $expectedResult = [
215 'controllers' => [
216 'FirstController' => [
217 'actions' => ['index', 'show', 'new', 'create', 'delete', 'edit', 'update'],
218 'nonCacheableActions' => ['new', 'create', 'edit', 'update']
219 ],
220 'SecondController' => [
221 'actions' => ['index', 'show', 'delete']
222 ],
223 'ThirdController' => [
224 'actions' => ['create'],
225 'nonCacheableActions' => ['create']
226 ]
227 ],
228 'pluginType' => 'list_type'
229 ];
230 $this->assertEquals($expectedResult, $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions']['MyExtension']['plugins']['Pi1']);
231 }
232
233 /**
234 * @test
235 * @see \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin
236 */
237 public function configurePluginWorksForMultipleControllerActionsWithNonCacheableActionAsDefault()
238 {
239 $GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_setup.'] = [];
240 ExtensionUtility::configurePlugin('MyExtension', 'Pi1', [
241 'FirstController' => 'index,show,new,create,delete,edit,update',
242 'SecondController' => 'index,show,delete',
243 'ThirdController' => 'create'
244 ], [
245 'FirstController' => 'index,new,create,edit,update',
246 'SecondController' => 'delete',
247 'ThirdController' => 'create'
248 ]);
249 $expectedResult = [
250 'controllers' => [
251 'FirstController' => [
252 'actions' => ['index', 'show', 'new', 'create', 'delete', 'edit', 'update'],
253 'nonCacheableActions' => ['index', 'new', 'create', 'edit', 'update']
254 ],
255 'SecondController' => [
256 'actions' => ['index', 'show', 'delete'],
257 'nonCacheableActions' => ['delete']
258 ],
259 'ThirdController' => [
260 'actions' => ['create'],
261 'nonCacheableActions' => ['create']
262 ]
263 ],
264 'pluginType' => 'list_type'
265 ];
266 $this->assertEquals($expectedResult, $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions']['MyExtension']['plugins']['Pi1']);
267 }
268
269 /**
270 * Tests method combination of registerPlugin() and its dependency addPlugin() to
271 * verify plugin icon path resolving works.
272 *
273 * @test
274 */
275 public function registerPluginTriggersAddPluginWhichSetsPluginIconPathIfUsingUnderscoredExtensionNameAndIconPathNotGiven()
276 {
277 $GLOBALS['TCA']['tt_content']['columns']['list_type']['config']['items'] = [];
278 $GLOBALS['TYPO3_LOADED_EXT'] = [];
279 $GLOBALS['TYPO3_LOADED_EXT']['indexed_search']['ext_icon'] = 'foo.gif';
280 ExtensionUtility::registerPlugin(
281 'indexed_search',
282 'Pi2',
283 'Testing'
284 );
285 $this->assertEquals(
286 'EXT:indexed_search/foo.gif',
287 $GLOBALS['TCA']['tt_content']['columns']['list_type']['config']['items'][0][2]
288 );
289 }
290
291 /**
292 * Tests method combination of registerPlugin() and its dependency addPlugin() to
293 * verify plugin icon path resolving works.
294 *
295 * @test
296 */
297 public function registerPluginTriggersAddPluginWhichSetsPluginIconPathIfUsingUpperCameCasedExtensionNameAndIconPathNotGiven()
298 {
299 $GLOBALS['TCA']['tt_content']['columns']['list_type']['config']['items'] = [];
300 $GLOBALS['TYPO3_LOADED_EXT'] = [];
301 $GLOBALS['TYPO3_LOADED_EXT']['indexed_search']['ext_icon'] = 'foo.gif';
302 ExtensionUtility::registerPlugin(
303 'IndexedSearch',
304 'Pi2',
305 'Testing'
306 );
307 $this->assertEquals(
308 'EXT:indexed_search/foo.gif',
309 $GLOBALS['TCA']['tt_content']['columns']['list_type']['config']['items'][0][2]
310 );
311 }
312
313 /**
314 * Tests method combination of registerPlugin() and its dependency addPlugin() to
315 * verify plugin icon path resolving works.
316 *
317 * @test
318 */
319 public function registerPluginTriggersAddPluginWhichSetsPluginIconPathIfIconPathIsGiven()
320 {
321 $GLOBALS['TCA']['tt_content']['columns']['list_type']['config']['items'] = [];
322 ExtensionUtility::registerPlugin(
323 'IndexedSearch',
324 'Pi2',
325 'Testing',
326 'EXT:indexed_search/foo.gif'
327 );
328 $this->assertEquals(
329 'EXT:indexed_search/foo.gif',
330 $GLOBALS['TCA']['tt_content']['columns']['list_type']['config']['items'][0][2]
331 );
332 }
333
334 /**
335 * A type converter added several times with the exact same class name must
336 * not be added more than once to the global array.
337 *
338 * @test
339 */
340 public function sameTypeConvertersRegisteredAreAddedOnlyOnce()
341 {
342 $typeConverterClassName = \TYPO3\CMS\Extbase\Property\TypeConverter\ArrayConverter::class;
343
344 // the Extbase EXTCONF is not set at all at this point
345 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'] = [];
346
347 \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerTypeConverter($typeConverterClassName);
348
349 $this->assertContains($typeConverterClassName, $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters']);
350 $this->assertEquals(1, count($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters']));
351
352 \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerTypeConverter($typeConverterClassName);
353 $this->assertEquals(1, count($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters']));
354 }
355 }