2 /***************************************************************
5 * (c) 2009 Oliver Hader <oliver@typo3.org>
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.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
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.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
26 * Testcase for class Tx_Extbase_Utility_Extension
31 class Tx_Extbase_Utility_Extension_testcase
extends tx_phpunit_testcase
{
34 * Contains backup of $TYPO3_CONF_VARS
37 protected $typo3ConfVars = array();
42 protected $typo3DbBackup;
45 * @var t3lib_fe contains a backup of the current $GLOBALS['TSFE']
47 protected $tsfeBackup;
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();
58 if (!isset($GLOBALS['TSFE']->tmpl
->setup
)) {
59 $GLOBALS['TSFE']->tmpl
->setup
= array();
61 $GLOBALS['TSFE']->tmpl
->setup
['tt_content.']['list.']['20.'] = array(
66 0 => '< plugin.tt_news'
68 'extensionname_someplugin' => 'USER',
69 'extensionname_someplugin.' => array(
70 'userFunc' => 'tx_extbase_dispatcher->dispatch',
71 'pluginName' => 'SomePlugin',
72 'extensionName' => 'ExtensionName',
73 'controller' => 'ControllerName',
75 'switchableControllerActions.' => array(
76 'ControllerName.' => array(
77 'actions' => 'index,otherAction',
81 'someotherextensionname_secondplugin' => 'USER',
82 'someotherextensionname_secondplugin.' => array(
83 'userFunc' => 'tx_extbase_dispatcher->dispatch',
84 'pluginName' => 'SecondPlugin',
85 'extensionName' => 'SomeOtherExtensionName',
86 'controller' => 'ControllerName',
88 'switchableControllerActions.' => array(
89 'ControllerName.' => array(
90 'actions' => 'index,otherAction',
92 'SecondControllerName.' => array(
93 'actions' => 'someAction,someOtherAction',
94 'nonCacheableActions' => 'someOtherAction',
98 'extensionname_thirdplugin' => 'USER',
99 'extensionname_thirdplugin.' => array(
100 'userFunc' => 'tx_extbase_dispatcher->dispatch',
101 'pluginName' => 'ThirdPlugin',
102 'extensionName' => 'ExtensionName',
103 'controller' => 'ControllerName',
105 'switchableControllerActions.' => array(
106 'ControllerName.' => array(
107 'actions' => 'otherAction,thirdAction',
114 public function tearDown() {
115 global $TYPO3_CONF_VARS;
116 $TYPO3_CONF_VARS = $this->typo3ConfVars
;
117 $GLOBALS['TSFE'] = $this->tsfeBackup
;
122 * @see Tx_Extbase_Utility_Extension::registerPlugin
124 public function configurePluginWorksForMinimalisticSetup() {
125 global $TYPO3_CONF_VARS;
126 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
127 Tx_Extbase_Utility_Extension
::configurePlugin(
130 array('Blog' => 'index')
132 $staticTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.']['43'];
134 $this->assertContains('tt_content.list.20.myextension_pi1 = USER', $staticTypoScript);
135 $this->assertContains('
137 extensionName = MyExtension', $staticTypoScript);
139 $this->assertNotContains('USER_INT', $staticTypoScript);
145 * @see Tx_Extbase_Utility_Extension::registerPlugin
147 public function configurePluginCreatesCorrectDefaultTypoScriptSetup() {
148 global $TYPO3_CONF_VARS;
149 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
150 Tx_Extbase_Utility_Extension
::configurePlugin(
153 array('Blog' => 'index')
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 {
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.
174 }', $defaultTypoScript);
179 * @see Tx_Extbase_Utility_Extension::registerPlugin
181 public function configurePluginWorksForASingleControllerAction() {
182 global $TYPO3_CONF_VARS;
183 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
184 Tx_Extbase_Utility_Extension
::configurePlugin(
188 'FirstController' => 'index'
191 $staticTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.']['43'];
193 $this->assertContains('tt_content.list.20.myextension_pi1 = USER', $staticTypoScript);
194 $this->assertContains('
196 extensionName = MyExtension', $staticTypoScript);
197 $this->assertContains('
198 controller = FirstController
199 action = index', $staticTypoScript);
200 $this->assertNotContains('USER_INT', $staticTypoScript);
205 * @see Tx_Extbase_Utility_Extension::registerPlugin
207 public function configurePluginWithEmptyPluginNameResultsInAnError() {
208 $this->setExpectedException('InvalidArgumentException');
209 Tx_Extbase_Utility_Extension
::configurePlugin(
213 'FirstController' => 'index'
220 * @see Tx_Extbase_Utility_Extension::registerPlugin
222 public function configurePluginWithEmptyExtensionNameResultsInAnError() {
223 $this->setExpectedException('InvalidArgumentException');
224 Tx_Extbase_Utility_Extension
::configurePlugin(
228 'FirstController' => 'index'
235 * @see Tx_Extbase_Utility_Extension::registerPlugin
237 public function configurePluginRespectsDefaultActionAsANonCacheableAction() {
238 global $TYPO3_CONF_VARS;
239 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
240 Tx_Extbase_Utility_Extension
::configurePlugin(
244 'FirstController' => 'index,show,new,create,delete,edit,update'
247 'FirstController' => 'index,show'
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);
260 * @see Tx_Extbase_Utility_Extension::registerPlugin
262 public function configurePluginRespectsNonDefaultActionAsANonCacheableAction() {
263 global $TYPO3_CONF_VARS;
264 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
265 Tx_Extbase_Utility_Extension
::configurePlugin(
269 'FirstController' => 'index,show,new,create,delete,edit,update'
272 'FirstController' => 'show,new'
275 $staticTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.']['43'];
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);
286 * @see Tx_Extbase_Utility_Extension::registerPlugin
288 public function configurePluginWorksForMultipleControllerActionsWithCacheableActionAsDefault() {
289 global $TYPO3_CONF_VARS;
290 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
291 Tx_Extbase_Utility_Extension
::configurePlugin(
295 'FirstController' => 'index,show,new,create,delete,edit,update',
296 'SecondController' => 'index,show,delete',
297 'ThirdController' => 'create'
300 'FirstController' => 'new,create,edit,update',
301 'SecondController' => 'delete',
302 'ThirdController' => 'create'
305 $staticTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.']['43'];
307 $this->assertContains('
308 tt_content.list.20.myextension_pi1 = USER
309 tt_content.list.20.myextension_pi1 {', $staticTypoScript);
311 $this->assertContains('FirstController.nonCacheableActions = new,create,edit,update
312 ', $staticTypoScript);
314 $this->assertContains('SecondController.nonCacheableActions = delete
315 ', $staticTypoScript);
317 $this->assertContains('ThirdController.nonCacheableActions = create
318 ', $staticTypoScript);
324 * @see Tx_Extbase_Utility_Extension::registerPlugin
326 public function configurePluginWorksForMultipleControllerActionsWithNonCacheableActionAsDefault() {
327 global $TYPO3_CONF_VARS;
328 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
329 Tx_Extbase_Utility_Extension
::configurePlugin(
333 'FirstController' => 'index,show,new,create,delete,edit,update',
334 'SecondController' => 'index,show,delete',
335 'ThirdController' => 'create'
338 'FirstController' => 'index,new,create,edit,update',
339 'SecondController' => 'delete',
340 'ThirdController' => 'create'
343 $staticTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.']['43'];
345 $this->assertContains('
346 tt_content.list.20.myextension_pi1 = USER
347 tt_content.list.20.myextension_pi1 {', $staticTypoScript);
349 $this->assertContains('FirstController.nonCacheableActions = index,new,create,edit,update
350 ', $staticTypoScript);
352 $this->assertContains('SecondController.nonCacheableActions = delete
353 ', $staticTypoScript);
355 $this->assertContains('ThirdController.nonCacheableActions = create
356 ', $staticTypoScript);
360 * DataProvider for getPluginNamespaceByPluginSignatureTests()
364 public function getPluginNamespaceByPluginSignatureDataProvider() {
366 array('someextension_someplugin', 'tx_someextension_someplugin'),
367 array('nonexistingextension_someplugin', 'tx_nonexistingextension_someplugin'),
368 array('InvalidPluginNamespace', 'tx_InvalidPluginNamespace'),
374 * @dataProvider getPluginNamespaceByPluginSignatureDataProvider
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 . '"');
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);
398 * DataProvider for getPluginNameByActionTests()
402 public function getPluginNameByActionDataProvider() {
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'),
415 * @dataProvider getPluginNameByActionDataProvider
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 . '" - ');
424 * @expectedException Tx_Extbase_Exception
426 public function getPluginNameByActionThrowsExceptionIfMoreThanOnePluginMatches() {
427 Tx_Extbase_Utility_Extension
::getPluginNameByAction('ExtensionName', 'ControllerName', 'otherAction');
433 public function getTargetPidByPluginSignatureReturnsNullIfConfigurationManagerIsNotInitialized() {
434 $this->assertNull(Tx_Extbase_Utility_Extension
::getTargetPidByPluginSignature('plugin_signature'));
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'));
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);
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(
476 'list_type="pluginSignature" AND enable_fields',
479 )->will($this->returnValue(array(array('pid' => '321'))));
480 $expectedResult = 321;
481 $actualResult = Tx_Extbase_Utility_Extension
::getTargetPidByPluginSignature($pluginSignature);
482 $this->assertEquals($expectedResult, $actualResult);
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'));
502 * @expectedException Tx_Extbase_Exception
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');