[BUGFIX] ConfigurationManager use FrontendSimulator only if neccessary
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Service / ExtensionServiceTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Extbase Team
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 /**
27 * Testcase for class Tx_Extbase_Service_ExtensionService
28 *
29 * @package Extbase
30 * @subpackage extbase
31 */
32
33 class Tx_Extbase_Tests_Unit_Service_ExtensionServiceTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
34
35 /**
36 * Contains backup of $TYPO3_CONF_VARS
37 * @var array
38 */
39 protected $typo3ConfVars = array();
40
41 /**
42 * @var t3lib_DB
43 */
44 protected $typo3DbBackup;
45
46 /**
47 * @var t3lib_fe contains a backup of the current $GLOBALS['TSFE']
48 */
49 protected $tsfeBackup;
50
51 /**
52 * @var Tx_Extbase_Configuration_ConfigurationManagerInterface
53 */
54 protected $mockConfigurationManager;
55
56 /**
57 * @var Tx_Extbase_Service_ExtensionService
58 */
59 protected $extensionService;
60
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();
66
67 $this->mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_ConfigurationManagerInterface');
68 $this->extensionService->injectConfigurationManager($this->mockConfigurationManager);
69
70 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'] = array(
71 'ExtensionName' => array(
72 'plugins' => array(
73 'SomePlugin' => array(
74 'controllers' => array(
75 'ControllerName' => array(
76 'actions' => array('index', 'otherAction')
77 ),
78 ),
79 ),
80 'ThirdPlugin' => array(
81 'controllers' => array(
82 'ControllerName' => array(
83 'actions' => array('otherAction', 'thirdAction')
84 ),
85 ),
86 ),
87 ),
88 ),
89 'SomeOtherExtensionName' => array(
90 'plugins' => array(
91 'SecondPlugin' => array(
92 'controllers' => array(
93 'ControllerName' => array(
94 'actions' => array('index', 'otherAction')
95 ),
96 'SecondControllerName' => array(
97 'actions' => array('someAction', 'someOtherAction'),
98 'nonCacheableActions' => array('someOtherAction')
99 )
100 ),
101 ),
102 ),
103 ),
104 );
105 }
106
107 public function tearDown() {
108 $GLOBALS['TYPO3_CONF_VARS'] = $this->typo3ConfVars;
109 $GLOBALS['TSFE'] = $this->tsfeBackup;
110 }
111
112 /**
113 * DataProvider for getPluginNamespaceByPluginSignatureTests()
114 *
115 * @return array
116 */
117 public function getPluginNamespaceDataProvider() {
118 return array(
119 array('SomeExtension', 'SomePlugin', 'tx_someextension_someplugin'),
120 array('NonExistingExtension', 'SomePlugin', 'tx_nonexistingextension_someplugin'),
121 array('Invalid', '', 'tx_invalid_'),
122 );
123 }
124
125 /**
126 * @test
127 * @dataProvider getPluginNamespaceDataProvider
128 */
129 public function getPluginNamespaceTests($extensionName, $pluginName, $expectedResult) {
130 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array()));
131
132 $actualResult = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
133 $this->assertEquals($expectedResult, $actualResult, 'Failing for extension: "' . $extensionName . '", plugin: "' . $pluginName . '"');
134 }
135
136 /**
137 * @test
138 */
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'))));
141
142 $expectedResult = 'overridden_plugin_namespace';
143 $actualResult = $this->extensionService->getPluginNamespace('SomeExtension', 'SomePlugin');
144 $this->assertEquals($expectedResult, $actualResult);
145 }
146
147 /**
148 * DataProvider for getPluginNameByActionTests()
149 *
150 * @return array
151 */
152 public function getPluginNameByActionDataProvider() {
153 return array(
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'),
160 );
161 }
162
163 /**
164 * @test
165 * @dataProvider getPluginNameByActionDataProvider
166 */
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 . '" - ');
171 }
172
173 /**
174 * @test
175 * @expectedException Tx_Extbase_Exception
176 */
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');
180 }
181
182 /**
183 * @test
184 */
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'))))));
187
188 $actualResult = $this->extensionService->getPluginNameByAction('CurrentExtension', 'ControllerName', 'otherAction');
189 $expectedResult = 'CurrentPlugin';
190 $this->assertEquals($expectedResult, $actualResult);
191 }
192
193 /**
194 * @test
195 */
196 public function isActionCacheableReturnsTrueByDefault() {
197 $mockConfiguration = array();
198 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($mockConfiguration));
199
200 $actualResult = $this->extensionService->isActionCacheable('SomeExtension', 'SomePlugin', 'SomeController', 'someAction');
201 $this->assertTrue($actualResult);
202 }
203
204 /**
205 * @test
206 */
207 public function isActionCacheableReturnsFalseIfActionIsNotCacheable() {
208 $mockConfiguration = array(
209 'controllerConfiguration' => array(
210 'SomeController' => array(
211 'nonCacheableActions' => array('someAction')
212 )
213 )
214 );
215 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($mockConfiguration));
216
217 $actualResult = $this->extensionService->isActionCacheable('SomeExtension', 'SomePlugin', 'SomeController', 'someAction');
218 $this->assertFalse($actualResult);
219 }
220
221 /**
222 * @test
223 */
224 public function getTargetPidByPluginSignatureReturnsNullIfConfigurationManagerIsNotInitialized() {
225 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(NULL));
226
227 $this->assertNull($this->extensionService->getTargetPidByPlugin('ExtensionName', 'PluginName'));
228 }
229
230 /**
231 * @test
232 */
233 public function getTargetPidByPluginSignatureReturnsNullIfDefaultPidIsZero() {
234 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 0))));
235
236 $this->assertNull($this->extensionService->getTargetPidByPlugin('ExtensionName', 'PluginName'));
237 }
238
239 /**
240 * @test
241 */
242 public function getTargetPidByPluginSignatureReturnsTheConfiguredDefaultPid() {
243 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 123))));
244
245 $expectedResult = 123;
246 $actualResult = $this->extensionService->getTargetPidByPlugin('ExtensionName', 'SomePlugin');
247 $this->assertEquals($expectedResult, $actualResult);
248 }
249
250 /**
251 * @test
252 */
253 public function getTargetPidByPluginSignatureDeterminesTheTargetPidIfDefaultPidIsAuto() {
254 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
255
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(
261 'pid',
262 'tt_content',
263 'list_type="pluginSignature" AND CType="list" AND enable_fields',
264 '',
265 ''
266 )->will($this->returnValue(array(array('pid' => '321'))));
267 $expectedResult = 321;
268 $actualResult = $this->extensionService->getTargetPidByPlugin('ExtensionName', 'SomePlugin');
269 $this->assertEquals($expectedResult, $actualResult);
270 }
271
272 /**
273 * @test
274 */
275 public function getTargetPidByPluginSignatureReturnsNullIfTargetPidCouldNotBeDetermined() {
276 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
277
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'));
283 }
284
285 /**
286 * @test
287 * @expectedException Tx_Extbase_Exception
288 */
289 public function getTargetPidByPluginSignatureThrowsExceptionIfMoreThanOneTargetPidsWereFound() {
290 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
291
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');
297 }
298
299 }
300 ?>