9e3a5b7c13114bd402d44ae8d5a01cbdbdc34f87
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Utility / ExtensionTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Oliver Hader <oliver@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Testcase for class Tx_Extbase_Utility_Extension
27 *
28 * @package Extbase
29 * @subpackage extbase
30 */
31 class Tx_Extbase_Tests_Unit_Utility_ExtensionTest extends tx_phpunit_testcase {
32
33 /**
34 * Contains backup of $TYPO3_CONF_VARS
35 * @var array
36 */
37 protected $typo3ConfVars = array();
38
39 /**
40 * @var t3lib_DB
41 */
42 protected $typo3DbBackup;
43
44 /**
45 * @var t3lib_fe contains a backup of the current $GLOBALS['TSFE']
46 */
47 protected $tsfeBackup;
48
49 public function setUp() {
50 global $TYPO3_CONF_VARS;
51 $this->typo3ConfVars = $TYPO3_CONF_VARS;
52 $this->typo3DbBackup = $GLOBALS['TYPO3_DB'];
53 $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB', array('fullQuoteStr', 'exec_SELECTgetRows'));
54 $this->tsfeBackup = $GLOBALS['TSFE'];
55 if (!isset($GLOBALS['TSFE']->tmpl)) {
56 $GLOBALS['TSFE']->tmpl = new stdClass();
57 }
58 if (!isset($GLOBALS['TSFE']->tmpl->setup)) {
59 $GLOBALS['TSFE']->tmpl->setup = array();
60 }
61 $GLOBALS['TSFE']->tmpl->setup['tt_content.']['list.']['20.'] = array(
62 '9' => 'CASE',
63 '9.' => array(
64 'key.' => array(
65 'field' => 'layout'),
66 0 => '< plugin.tt_news'
67 ),
68 'extensionname_someplugin' => 'USER',
69 'extensionname_someplugin.' => array(
70 'userFunc' => 'tx_extbase_dispatcher->dispatch',
71 'pluginName' => 'SomePlugin',
72 'extensionName' => 'ExtensionName',
73 'controller' => 'ControllerName',
74 'action' => 'index',
75 'switchableControllerActions.' => array(
76 'ControllerName.' => array(
77 'actions' => 'index,otherAction',
78 ),
79 ),
80 ),
81 'someotherextensionname_secondplugin' => 'USER',
82 'someotherextensionname_secondplugin.' => array(
83 'userFunc' => 'tx_extbase_dispatcher->dispatch',
84 'pluginName' => 'SecondPlugin',
85 'extensionName' => 'SomeOtherExtensionName',
86 'controller' => 'ControllerName',
87 'action' => 'index',
88 'switchableControllerActions.' => array(
89 'ControllerName.' => array(
90 'actions' => 'index,otherAction',
91 ),
92 'SecondControllerName.' => array(
93 'actions' => 'someAction,someOtherAction',
94 'nonCacheableActions' => 'someOtherAction',
95 ),
96 ),
97 ),
98 'extensionname_thirdplugin' => 'USER',
99 'extensionname_thirdplugin.' => array(
100 'userFunc' => 'tx_extbase_dispatcher->dispatch',
101 'pluginName' => 'ThirdPlugin',
102 'extensionName' => 'ExtensionName',
103 'controller' => 'ControllerName',
104 'action' => 'index',
105 'switchableControllerActions.' => array(
106 'ControllerName.' => array(
107 'actions' => 'otherAction,thirdAction',
108 ),
109 ),
110 ),
111 );
112 }
113
114 public function tearDown() {
115 global $TYPO3_CONF_VARS;
116 $TYPO3_CONF_VARS = $this->typo3ConfVars;
117 $GLOBALS['TSFE'] = $this->tsfeBackup;
118 }
119
120 /**
121 * @test
122 * @see Tx_Extbase_Utility_Extension::registerPlugin
123 */
124 public function configurePluginWorksForMinimalisticSetup() {
125 global $TYPO3_CONF_VARS;
126 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
127 Tx_Extbase_Utility_Extension::configurePlugin(
128 'MyExtension',
129 'Pi1',
130 array('Blog' => 'index')
131 );
132 $staticTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.']['43'];
133
134 $this->assertContains('tt_content.list.20.myextension_pi1 = USER', $staticTypoScript);
135 $this->assertContains('
136 pluginName = Pi1
137 extensionName = MyExtension', $staticTypoScript);
138
139 $this->assertNotContains('USER_INT', $staticTypoScript);
140 }
141
142
143 /**
144 * @test
145 * @see Tx_Extbase_Utility_Extension::registerPlugin
146 */
147 public function configurePluginCreatesCorrectDefaultTypoScriptSetup() {
148 global $TYPO3_CONF_VARS;
149 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
150 Tx_Extbase_Utility_Extension::configurePlugin(
151 'MyExtension',
152 'Pi1',
153 array('Blog' => 'index')
154 );
155 $staticTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.']['43'];
156 $defaultTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup'];
157 $this->assertContains('tt_content.list.20.myextension_pi1 = USER', $staticTypoScript);
158 $this->assertContains('
159 plugin.tx_myextension {
160 settings {
161 }
162 persistence {
163 storagePid =
164 classes {
165 }
166 }
167 view {
168 templateRootPath =
169 layoutRootPath =
170 partialRootPath =
171 # with defaultPid you can specify the default page uid of this plugin. If you set this to the string "auto" the target page will be determined automatically. Defaults to an empty string that expects the target page to be the current page.
172 defaultPid =
173 }
174 }', $defaultTypoScript);
175 }
176
177 /**
178 * @test
179 * @see Tx_Extbase_Utility_Extension::registerPlugin
180 */
181 public function configurePluginWorksForASingleControllerAction() {
182 global $TYPO3_CONF_VARS;
183 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
184 Tx_Extbase_Utility_Extension::configurePlugin(
185 'MyExtension',
186 'Pi1',
187 array(
188 'FirstController' => 'index'
189 )
190 );
191 $staticTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.']['43'];
192
193 $this->assertContains('tt_content.list.20.myextension_pi1 = USER', $staticTypoScript);
194 $this->assertContains('
195 pluginName = Pi1
196 extensionName = MyExtension', $staticTypoScript);
197 $this->assertContains('
198 controller = FirstController
199 action = index', $staticTypoScript);
200 $this->assertNotContains('USER_INT', $staticTypoScript);
201 }
202
203 /**
204 * @test
205 * @see Tx_Extbase_Utility_Extension::registerPlugin
206 */
207 public function configurePluginWithEmptyPluginNameResultsInAnError() {
208 $this->setExpectedException('InvalidArgumentException');
209 Tx_Extbase_Utility_Extension::configurePlugin(
210 'MyExtension',
211 '',
212 array(
213 'FirstController' => 'index'
214 )
215 );
216 }
217
218 /**
219 * @test
220 * @see Tx_Extbase_Utility_Extension::registerPlugin
221 */
222 public function configurePluginWithEmptyExtensionNameResultsInAnError() {
223 $this->setExpectedException('InvalidArgumentException');
224 Tx_Extbase_Utility_Extension::configurePlugin(
225 '',
226 'Pi1',
227 array(
228 'FirstController' => 'index'
229 )
230 );
231 }
232
233 /**
234 * @test
235 * @see Tx_Extbase_Utility_Extension::registerPlugin
236 */
237 public function configurePluginRespectsDefaultActionAsANonCacheableAction() {
238 global $TYPO3_CONF_VARS;
239 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
240 Tx_Extbase_Utility_Extension::configurePlugin(
241 'MyExtension',
242 'Pi1',
243 array(
244 'FirstController' => 'index,show,new,create,delete,edit,update'
245 ),
246 array(
247 'FirstController' => 'index,show'
248 )
249 );
250 $staticTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.']['43'];
251 $this->assertContains('
252 tt_content.list.20.myextension_pi1 = USER
253 tt_content.list.20.myextension_pi1 {', $staticTypoScript);
254 $this->assertContains('FirstController.nonCacheableActions = index,show
255 ', $staticTypoScript);
256 }
257
258 /**
259 * @test
260 * @see Tx_Extbase_Utility_Extension::registerPlugin
261 */
262 public function configurePluginRespectsNonDefaultActionAsANonCacheableAction() {
263 global $TYPO3_CONF_VARS;
264 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
265 Tx_Extbase_Utility_Extension::configurePlugin(
266 'MyExtension',
267 'Pi1',
268 array(
269 'FirstController' => 'index,show,new,create,delete,edit,update'
270 ),
271 array(
272 'FirstController' => 'show,new'
273 )
274 );
275 $staticTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.']['43'];
276
277 $this->assertContains('
278 tt_content.list.20.myextension_pi1 = USER
279 tt_content.list.20.myextension_pi1 {', $staticTypoScript);
280 $this->assertContains('FirstController.nonCacheableActions = show,new
281 ', $staticTypoScript);
282 }
283
284 /**
285 * @test
286 * @see Tx_Extbase_Utility_Extension::registerPlugin
287 */
288 public function configurePluginWorksForMultipleControllerActionsWithCacheableActionAsDefault() {
289 global $TYPO3_CONF_VARS;
290 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
291 Tx_Extbase_Utility_Extension::configurePlugin(
292 'MyExtension',
293 'Pi1',
294 array(
295 'FirstController' => 'index,show,new,create,delete,edit,update',
296 'SecondController' => 'index,show,delete',
297 'ThirdController' => 'create'
298 ),
299 array(
300 'FirstController' => 'new,create,edit,update',
301 'SecondController' => 'delete',
302 'ThirdController' => 'create'
303 )
304 );
305 $staticTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.']['43'];
306
307 $this->assertContains('
308 tt_content.list.20.myextension_pi1 = USER
309 tt_content.list.20.myextension_pi1 {', $staticTypoScript);
310
311 $this->assertContains('FirstController.nonCacheableActions = new,create,edit,update
312 ', $staticTypoScript);
313
314 $this->assertContains('SecondController.nonCacheableActions = delete
315 ', $staticTypoScript);
316
317 $this->assertContains('ThirdController.nonCacheableActions = create
318 ', $staticTypoScript);
319 }
320
321
322 /**
323 * @test
324 * @see Tx_Extbase_Utility_Extension::registerPlugin
325 */
326 public function configurePluginWorksForMultipleControllerActionsWithNonCacheableActionAsDefault() {
327 global $TYPO3_CONF_VARS;
328 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
329 Tx_Extbase_Utility_Extension::configurePlugin(
330 'MyExtension',
331 'Pi1',
332 array(
333 'FirstController' => 'index,show,new,create,delete,edit,update',
334 'SecondController' => 'index,show,delete',
335 'ThirdController' => 'create'
336 ),
337 array(
338 'FirstController' => 'index,new,create,edit,update',
339 'SecondController' => 'delete',
340 'ThirdController' => 'create'
341 )
342 );
343 $staticTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.']['43'];
344
345 $this->assertContains('
346 tt_content.list.20.myextension_pi1 = USER
347 tt_content.list.20.myextension_pi1 {', $staticTypoScript);
348
349 $this->assertContains('FirstController.nonCacheableActions = index,new,create,edit,update
350 ', $staticTypoScript);
351
352 $this->assertContains('SecondController.nonCacheableActions = delete
353 ', $staticTypoScript);
354
355 $this->assertContains('ThirdController.nonCacheableActions = create
356 ', $staticTypoScript);
357 }
358
359 /**
360 * DataProvider for getPluginNamespaceByPluginSignatureTests()
361 *
362 * @return array
363 */
364 public function getPluginNamespaceByPluginSignatureDataProvider() {
365 return array(
366 array('someextension_someplugin', 'tx_someextension_someplugin'),
367 array('nonexistingextension_someplugin', 'tx_nonexistingextension_someplugin'),
368 array('InvalidPluginNamespace', 'tx_InvalidPluginNamespace'),
369 );
370 }
371
372 /**
373 * @test
374 * @dataProvider getPluginNamespaceByPluginSignatureDataProvider
375 */
376 public function getPluginNamespaceByPluginSignatureTests($pluginSignature, $expectedResult) {
377 $dispatcher = new Tx_Extbase_Dispatcher();
378 $mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_ConfigurationManager', array('getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup'));
379 $dispatcher->injectConfigurationManager($mockConfigurationManager);
380 $actualResult = Tx_Extbase_Utility_Extension::getPluginNamespaceByPluginSignature($pluginSignature);
381 $this->assertEquals($expectedResult, $actualResult, 'Failing for $pluginSignature: "' . $pluginSignature . '"');
382 }
383
384 /**
385 * @test
386 */
387 public function pluginNamespaceCanBeOverridden() {
388 $dispatcher = new Tx_Extbase_Dispatcher();
389 $mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_ConfigurationManager', array('getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getConfiguration'));
390 $mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('pluginNamespace' => 'overridden_plugin_namespace'))));
391 $dispatcher->injectConfigurationManager($mockConfigurationManager);
392 $expectedResult = 'overridden_plugin_namespace';
393 $actualResult = Tx_Extbase_Utility_Extension::getPluginNamespaceByPluginSignature('somePluginSignature');
394 $this->assertEquals($expectedResult, $actualResult);
395 }
396
397 /**
398 * DataProvider for getPluginNameByActionTests()
399 *
400 * @return array
401 */
402 public function getPluginNameByActionDataProvider() {
403 return array(
404 array('ExtensionName', 'ControllerName', 'someNonExistingAction', NULL),
405 array('ExtensionName', 'ControllerName', 'index', 'SomePlugin'),
406 array('ExtensionName', 'ControllerName', 'thirdAction', 'ThirdPlugin'),
407 array('eXtEnSiOnNaMe', 'cOnTrOlLeRnAmE', 'thirdAction', 'ThirdPlugin'),
408 array('eXtEnSiOnNaMe', 'cOnTrOlLeRnAmE', 'ThIrDaCtIoN', NULL),
409 array('SomeOtherExtensionName', 'ControllerName', 'otherAction', 'SecondPlugin'),
410 );
411 }
412
413 /**
414 * @test
415 * @dataProvider getPluginNameByActionDataProvider
416 */
417 public function getPluginNameByActionTests($extensionName, $controllerName, $actionName, $expectedResult) {
418 $actualResult = Tx_Extbase_Utility_Extension::getPluginNameByAction($extensionName, $controllerName, $actionName);
419 $this->assertEquals($expectedResult, $actualResult, 'Failing for $extensionName: "' . $extensionName . '", $controllerName: "' . $controllerName . '", $actionName: "' . $actionName . '" - ');
420 }
421
422 /**
423 * @test
424 * @expectedException Tx_Extbase_Exception
425 */
426 public function getPluginNameByActionThrowsExceptionIfMoreThanOnePluginMatches() {
427 Tx_Extbase_Utility_Extension::getPluginNameByAction('ExtensionName', 'ControllerName', 'otherAction');
428 }
429
430 /**
431 * @test
432 */
433 public function getTargetPidByPluginSignatureReturnsNullIfConfigurationManagerIsNotInitialized() {
434 $this->assertNull(Tx_Extbase_Utility_Extension::getTargetPidByPluginSignature('plugin_signature'));
435 }
436
437 /**
438 * @test
439 */
440 public function getTargetPidByPluginSignatureReturnsNullIfDefaultPidIsNotConfigured() {
441 $dispatcher = new Tx_Extbase_Dispatcher();
442 $mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_ConfigurationManager', array('getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getConfiguration'));
443 $mockConfigurationManager->expects($this->once())->method('getConfiguration')->with($GLOBALS['TSFE']->tmpl->setup['tt_content.']['list.']['20.']['extensionname_someplugin.'])->will($this->returnValue(array()));
444 $dispatcher->injectConfigurationManager($mockConfigurationManager);
445 $this->assertNull(Tx_Extbase_Utility_Extension::getTargetPidByPluginSignature('extensionname_someplugin'));
446 }
447
448 /**
449 * @test
450 */
451 public function getTargetPidByPluginSignatureReturnsTheConfiguredDefaultPid() {
452 $dispatcher = new Tx_Extbase_Dispatcher();
453 $mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_ConfigurationManager', array('getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getConfiguration'));
454 $mockConfigurationManager->expects($this->once())->method('getConfiguration')->with($GLOBALS['TSFE']->tmpl->setup['tt_content.']['list.']['20.']['extensionname_someplugin.'])->will($this->returnValue(array('view' => array('defaultPid' => '123'))));
455 $dispatcher->injectConfigurationManager($mockConfigurationManager);
456 $expectedResult = 123;
457 $actualResult = Tx_Extbase_Utility_Extension::getTargetPidByPluginSignature('extensionname_someplugin');
458 $this->assertEquals($expectedResult, $actualResult);
459 }
460
461 /**
462 * @test
463 */
464 public function getTargetPidByPluginSignatureDeterminesTheTargetPidIfDefaultPidIsAuto() {
465 $dispatcher = new Tx_Extbase_Dispatcher();
466 $mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_ConfigurationManager', array('getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getConfiguration'));
467 $mockConfigurationManager->expects($this->once())->method('getConfiguration')->with($GLOBALS['TSFE']->tmpl->setup['tt_content.']['list.']['20.']['extensionname_someplugin.'])->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
468 $dispatcher->injectConfigurationManager($mockConfigurationManager);
469 $pluginSignature = 'extensionname_someplugin';
470 $GLOBALS['TSFE']->sys_page = $this->getMock('t3lib_pageSelect', array('enableFields'));
471 $GLOBALS['TSFE']->sys_page->expects($this->once())->method('enableFields')->with('tt_content')->will($this->returnValue(' AND enable_fields'));
472 $GLOBALS['TYPO3_DB']->expects($this->once())->method('fullQuoteStr')->with($pluginSignature, 'tt_content')->will($this->returnValue('"pluginSignature"'));
473 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->with(
474 'pid',
475 'tt_content',
476 'list_type="pluginSignature" AND enable_fields',
477 '',
478 ''
479 )->will($this->returnValue(array(array('pid' => '321'))));
480 $expectedResult = 321;
481 $actualResult = Tx_Extbase_Utility_Extension::getTargetPidByPluginSignature($pluginSignature);
482 $this->assertEquals($expectedResult, $actualResult);
483 }
484
485 /**
486 * @test
487 */
488 public function getTargetPidByPluginSignatureReturnsNullIfTargetPidCouldNotBeDetermined() {
489 $dispatcher = new Tx_Extbase_Dispatcher();
490 $mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_ConfigurationManager', array('getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getConfiguration'));
491 $mockConfigurationManager->expects($this->once())->method('getConfiguration')->with($GLOBALS['TSFE']->tmpl->setup['tt_content.']['list.']['20.']['extensionname_someplugin.'])->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
492 $dispatcher->injectConfigurationManager($mockConfigurationManager);
493 $GLOBALS['TSFE']->sys_page = $this->getMock('t3lib_pageSelect', array('enableFields'));
494 $GLOBALS['TSFE']->sys_page->expects($this->once())->method('enableFields')->will($this->returnValue(' AND enable_fields'));
495 $GLOBALS['TYPO3_DB']->expects($this->once())->method('fullQuoteStr')->will($this->returnValue('"pluginSignature"'));
496 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnValue(array()));
497 $this->assertNull(Tx_Extbase_Utility_Extension::getTargetPidByPluginSignature('extensionname_someplugin'));
498 }
499
500 /**
501 * @test
502 * @expectedException Tx_Extbase_Exception
503 */
504 public function getTargetPidByPluginSignatureThrowsExceptionIfMoreThanOneTargetPidsWereFound() {
505 $dispatcher = new Tx_Extbase_Dispatcher();
506 $mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_ConfigurationManager', array('getContextSpecificFrameworkConfiguration', 'getTypoScriptSetup', 'getConfiguration'));
507 $mockConfigurationManager->expects($this->once())->method('getConfiguration')->with($GLOBALS['TSFE']->tmpl->setup['tt_content.']['list.']['20.']['extensionname_someplugin.'])->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
508 $dispatcher->injectConfigurationManager($mockConfigurationManager);
509 $GLOBALS['TSFE']->sys_page = $this->getMock('t3lib_pageSelect', array('enableFields'));
510 $GLOBALS['TSFE']->sys_page->expects($this->once())->method('enableFields')->will($this->returnValue(' AND enable_fields'));
511 $GLOBALS['TYPO3_DB']->expects($this->once())->method('fullQuoteStr')->will($this->returnValue('"pluginSignature"'));
512 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnValue(array(array('pid' => 123), array('pid' => 124))));
513 Tx_Extbase_Utility_Extension::getTargetPidByPluginSignature('extensionname_someplugin');
514 }
515
516 }
517
518 ?>