2 /***************************************************************
5 * (c) 2011 Extbase Team
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 ***************************************************************/
27 * Testcase for class Tx_Extbase_Service_ExtensionService
33 class Tx_Extbase_Tests_Unit_Service_ExtensionServiceTest
extends Tx_Extbase_Tests_Unit_BaseTestCase
{
36 * Contains backup of $TYPO3_CONF_VARS
39 protected $typo3ConfVars = array();
44 protected $typo3DbBackup;
47 * @var t3lib_fe contains a backup of the current $GLOBALS['TSFE']
49 protected $tsfeBackup;
52 * @var Tx_Extbase_Configuration_ConfigurationManagerInterface
54 protected $mockConfigurationManager;
57 * @var Tx_Extbase_Service_ExtensionService
59 protected $extensionService;
61 public function setUp() {
62 $this->typo3ConfVars
= $GLOBALS['TYPO3_CONF_VARS'];
63 $this->typo3DbBackup
= $GLOBALS['TYPO3_DB'];
64 $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB', array('fullQuoteStr', 'exec_SELECTgetRows'));
65 $this->extensionService
= new Tx_Extbase_Service_ExtensionService();
67 $this->mockConfigurationManager
= $this->getMock('Tx_Extbase_Configuration_ConfigurationManagerInterface');
68 $this->extensionService
->injectConfigurationManager($this->mockConfigurationManager
);
70 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'] = array(
71 'ExtensionName' => array(
73 'SomePlugin' => array(
74 'controllers' => array(
75 'ControllerName' => array(
76 'actions' => array('index', 'otherAction')
80 'ThirdPlugin' => array(
81 'controllers' => array(
82 'ControllerName' => array(
83 'actions' => array('otherAction', 'thirdAction')
89 'SomeOtherExtensionName' => array(
91 'SecondPlugin' => array(
92 'controllers' => array(
93 'ControllerName' => array(
94 'actions' => array('index', 'otherAction')
96 'SecondControllerName' => array(
97 'actions' => array('someAction', 'someOtherAction'),
98 'nonCacheableActions' => array('someOtherAction')
107 public function tearDown() {
108 $GLOBALS['TYPO3_CONF_VARS'] = $this->typo3ConfVars
;
109 $GLOBALS['TSFE'] = $this->tsfeBackup
;
113 * DataProvider for getPluginNamespaceByPluginSignatureTests()
117 public function getPluginNamespaceDataProvider() {
119 array('SomeExtension', 'SomePlugin', 'tx_someextension_someplugin'),
120 array('NonExistingExtension', 'SomePlugin', 'tx_nonexistingextension_someplugin'),
121 array('Invalid', '', 'tx_invalid_'),
127 * @dataProvider getPluginNamespaceDataProvider
129 public function getPluginNamespaceTests($extensionName, $pluginName, $expectedResult) {
130 $this->mockConfigurationManager
->expects($this->once())->method('getConfiguration')->will($this->returnValue(array()));
132 $actualResult = $this->extensionService
->getPluginNamespace($extensionName, $pluginName);
133 $this->assertEquals($expectedResult, $actualResult, 'Failing for extension: "' . $extensionName . '", plugin: "' . $pluginName . '"');
139 public function pluginNamespaceCanBeOverridden() {
140 $this->mockConfigurationManager
->expects($this->once())->method('getConfiguration')->with(Tx_Extbase_Configuration_ConfigurationManagerInterface
::CONFIGURATION_TYPE_FRAMEWORK
, 'SomeExtension', 'SomePlugin')->will($this->returnValue(array('view' => array('pluginNamespace' => 'overridden_plugin_namespace'))));
142 $expectedResult = 'overridden_plugin_namespace';
143 $actualResult = $this->extensionService
->getPluginNamespace('SomeExtension', 'SomePlugin');
144 $this->assertEquals($expectedResult, $actualResult);
148 * DataProvider for getPluginNameByActionTests()
152 public function getPluginNameByActionDataProvider() {
154 array('ExtensionName', 'ControllerName', 'someNonExistingAction', NULL
),
155 array('ExtensionName', 'ControllerName', 'index', 'SomePlugin'),
156 array('ExtensionName', 'ControllerName', 'thirdAction', 'ThirdPlugin'),
157 array('eXtEnSiOnNaMe', 'cOnTrOlLeRnAmE', 'thirdAction', NULL
),
158 array('eXtEnSiOnNaMe', 'cOnTrOlLeRnAmE', 'ThIrDaCtIoN', NULL
),
159 array('SomeOtherExtensionName', 'ControllerName', 'otherAction', 'SecondPlugin'),
165 * @dataProvider getPluginNameByActionDataProvider
167 public function getPluginNameByActionTests($extensionName, $controllerName, $actionName, $expectedResult) {
168 $this->mockConfigurationManager
->expects($this->once())->method('getConfiguration')->with(Tx_Extbase_Configuration_ConfigurationManagerInterface
::CONFIGURATION_TYPE_FRAMEWORK
)->will($this->returnValue(array('view' => array('pluginNamespace' => 'overridden_plugin_namespace'))));
169 $actualResult = $this->extensionService
->getPluginNameByAction($extensionName, $controllerName, $actionName);
170 $this->assertEquals($expectedResult, $actualResult, 'Failing for $extensionName: "' . $extensionName . '", $controllerName: "' . $controllerName . '", $actionName: "' . $actionName . '" - ');
175 * @expectedException Tx_Extbase_Exception
177 public function getPluginNameByActionThrowsExceptionIfMoreThanOnePluginMatches() {
178 $this->mockConfigurationManager
->expects($this->once())->method('getConfiguration')->with(Tx_Extbase_Configuration_ConfigurationManagerInterface
::CONFIGURATION_TYPE_FRAMEWORK
)->will($this->returnValue(array('view' => array('pluginNamespace' => 'overridden_plugin_namespace'))));
179 $this->extensionService
->getPluginNameByAction('ExtensionName', 'ControllerName', 'otherAction');
185 public function getPluginNameByActionReturnsCurrentIfItCanHandleTheActionEvenIfMoreThanOnePluginMatches() {
186 $this->mockConfigurationManager
->expects($this->once())->method('getConfiguration')->with(Tx_Extbase_Configuration_ConfigurationManagerInterface
::CONFIGURATION_TYPE_FRAMEWORK
)->will($this->returnValue(array('extensionName' => 'CurrentExtension', 'pluginName' => 'CurrentPlugin', 'controllerConfiguration' => array('ControllerName' => array('actions' => array('otherAction'))))));
188 $actualResult = $this->extensionService
->getPluginNameByAction('CurrentExtension', 'ControllerName', 'otherAction');
189 $expectedResult = 'CurrentPlugin';
190 $this->assertEquals($expectedResult, $actualResult);
196 public function isActionCacheableReturnsTrueByDefault() {
197 $mockConfiguration = array();
198 $this->mockConfigurationManager
->expects($this->any())->method('getConfiguration')->will($this->returnValue($mockConfiguration));
200 $actualResult = $this->extensionService
->isActionCacheable('SomeExtension', 'SomePlugin', 'SomeController', 'someAction');
201 $this->assertTrue($actualResult);
207 public function isActionCacheableReturnsFalseIfActionIsNotCacheable() {
208 $mockConfiguration = array(
209 'controllerConfiguration' => array(
210 'SomeController' => array(
211 'nonCacheableActions' => array('someAction')
215 $this->mockConfigurationManager
->expects($this->any())->method('getConfiguration')->will($this->returnValue($mockConfiguration));
217 $actualResult = $this->extensionService
->isActionCacheable('SomeExtension', 'SomePlugin', 'SomeController', 'someAction');
218 $this->assertFalse($actualResult);
224 public function getTargetPidByPluginSignatureReturnsNullIfConfigurationManagerIsNotInitialized() {
225 $this->mockConfigurationManager
->expects($this->once())->method('getConfiguration')->will($this->returnValue(NULL
));
227 $this->assertNull($this->extensionService
->getTargetPidByPlugin('ExtensionName', 'PluginName'));
233 public function getTargetPidByPluginSignatureReturnsNullIfDefaultPidIsZero() {
234 $this->mockConfigurationManager
->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 0))));
236 $this->assertNull($this->extensionService
->getTargetPidByPlugin('ExtensionName', 'PluginName'));
242 public function getTargetPidByPluginSignatureReturnsTheConfiguredDefaultPid() {
243 $this->mockConfigurationManager
->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 123))));
245 $expectedResult = 123;
246 $actualResult = $this->extensionService
->getTargetPidByPlugin('ExtensionName', 'SomePlugin');
247 $this->assertEquals($expectedResult, $actualResult);
253 public function getTargetPidByPluginSignatureDeterminesTheTargetPidIfDefaultPidIsAuto() {
254 $this->mockConfigurationManager
->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
256 $pluginSignature = 'extensionname_someplugin';
257 $GLOBALS['TSFE']->sys_page
= $this->getMock('t3lib_pageSelect', array('enableFields'));
258 $GLOBALS['TSFE']->sys_page
->expects($this->once())->method('enableFields')->with('tt_content')->will($this->returnValue(' AND enable_fields'));
259 $GLOBALS['TYPO3_DB']->expects($this->once())->method('fullQuoteStr')->with($pluginSignature, 'tt_content')->will($this->returnValue('"pluginSignature"'));
260 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->with(
263 'list_type="pluginSignature" AND CType="list" AND enable_fields',
266 )->will($this->returnValue(array(array('pid' => '321'))));
267 $expectedResult = 321;
268 $actualResult = $this->extensionService
->getTargetPidByPlugin('ExtensionName', 'SomePlugin');
269 $this->assertEquals($expectedResult, $actualResult);
275 public function getTargetPidByPluginSignatureReturnsNullIfTargetPidCouldNotBeDetermined() {
276 $this->mockConfigurationManager
->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
278 $GLOBALS['TSFE']->sys_page
= $this->getMock('t3lib_pageSelect', array('enableFields'));
279 $GLOBALS['TSFE']->sys_page
->expects($this->once())->method('enableFields')->will($this->returnValue(' AND enable_fields'));
280 $GLOBALS['TYPO3_DB']->expects($this->once())->method('fullQuoteStr')->will($this->returnValue('"pluginSignature"'));
281 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnValue(array()));
282 $this->assertNull($this->extensionService
->getTargetPidByPlugin('ExtensionName', 'SomePlugin'));
287 * @expectedException Tx_Extbase_Exception
289 public function getTargetPidByPluginSignatureThrowsExceptionIfMoreThanOneTargetPidsWereFound() {
290 $this->mockConfigurationManager
->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
292 $GLOBALS['TSFE']->sys_page
= $this->getMock('t3lib_pageSelect', array('enableFields'));
293 $GLOBALS['TSFE']->sys_page
->expects($this->once())->method('enableFields')->will($this->returnValue(' AND enable_fields'));
294 $GLOBALS['TYPO3_DB']->expects($this->once())->method('fullQuoteStr')->will($this->returnValue('"pluginSignature"'));
295 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnValue(array(array('pid' => 123), array('pid' => 124))));
296 $this->extensionService
->getTargetPidByPlugin('ExtensionName', 'SomePlugin');
302 public function getDefaultControllerNameByPluginReturnsNullIfGivenExtensionCantBeFound() {
303 $this->assertNull($this->extensionService
->getDefaultControllerNameByPlugin('NonExistingExtensionName', 'SomePlugin'));
309 public function getDefaultControllerNameByPluginReturnsNullIfGivenPluginCantBeFound() {
310 $this->assertNull($this->extensionService
->getDefaultControllerNameByPlugin('ExtensionName', 'NonExistingPlugin'));
316 public function getDefaultControllerNameByPluginReturnsFirstControllerNameOfGivenPlugin() {
317 $expectedResult = 'ControllerName';
318 $actualResult = $this->extensionService
->getDefaultControllerNameByPlugin('ExtensionName', 'SomePlugin');
319 $this->assertEquals($expectedResult, $actualResult);
325 public function getDefaultActionNameByPluginAndControllerReturnsNullIfGivenExtensionCantBeFound() {
326 $this->assertNull($this->extensionService
->getDefaultActionNameByPluginAndController('NonExistingExtensionName', 'SomePlugin', 'ControllerName'));
332 public function getDefaultActionNameByPluginAndControllerReturnsNullIfGivenPluginCantBeFound() {
333 $this->assertNull($this->extensionService
->getDefaultActionNameByPluginAndController('ExtensionName', 'NonExistingPlugin', 'ControllerName'));
339 public function getDefaultActionNameByPluginAndControllerReturnsNullIfGivenControllerCantBeFound() {
340 $this->assertNull($this->extensionService
->getDefaultActionNameByPluginAndController('ExtensionName', 'SomePlugin', 'NonExistingControllerName'));
346 public function getDefaultActionNameByPluginAndControllerReturnsFirstActionNameOfGivenController() {
347 $expectedResult = 'someAction';
348 $actualResult = $this->extensionService
->getDefaultActionNameByPluginAndController('SomeOtherExtensionName', 'SecondPlugin', 'SecondControllerName');
349 $this->assertEquals($expectedResult, $actualResult);