Merge "[+TASK] Extbase: Move static typoscript utility methods to a service"
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Configuration / FrontendConfigurationManagerTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 class Tx_Extbase_Tests_Unit_Configuration_FrontendConfigurationManagerTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
29
30 /**
31 * @var tslib_fe
32 */
33 protected $tsfeBackup;
34
35 /**
36 * @var tslib_cObj
37 */
38 protected $mockContentObject;
39
40 /**
41 * @var Tx_Extbase_Configuration_FrontendConfigurationManager
42 */
43 protected $frontendConfigurationManager;
44
45 /**
46 * @var array
47 */
48 protected $extConfBackup;
49
50 /**
51 * @var Tx_Extbase_Service_TypoScriptService
52 */
53 protected $mockTypoScriptService;
54
55 /**
56 * Sets up this testcase
57 */
58 public function setUp() {
59 $this->tsfeBackup = $GLOBALS['TSFE'];
60 $this->mockContentObject = $this->getMock('tslib_cObj');
61 $this->extConfBackup = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase'];
62 $this->frontendConfigurationManager = $this->getAccessibleMock('Tx_Extbase_Configuration_FrontendConfigurationManager', array('dummy'));
63 $this->frontendConfigurationManager->_set('contentObject', $this->mockContentObject);
64 $this->mockTypoScriptService = $this->getAccessibleMock('Tx_Extbase_Service_TypoScriptService');
65 $this->frontendConfigurationManager->injectTypoScriptService($this->mockTypoScriptService);
66 }
67
68 /**
69 * Tears down this testcase
70 */
71 public function tearDown() {
72 $GLOBALS['TSFE']->tmpl->setup;
73 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase'] = $this->extConfBackup;
74 }
75
76 /**
77 * @test
78 */
79 public function getTypoScriptSetupReturnsSetupFromTSFE() {
80 $GLOBALS['TSFE']->tmpl->setup = array('foo' => 'bar');
81 $this->assertEquals(array('foo' => 'bar'), $this->frontendConfigurationManager->_call('getTypoScriptSetup'));
82 }
83
84 /**
85 * @test
86 */
87 public function getPluginConfigurationReturnsEmptyArrayIfNoPluginConfigurationWasFound() {
88 $GLOBALS['TSFE']->tmpl->setup = array('foo' => 'bar');
89 $expectedResult = array();
90 $actualResult = $this->frontendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
91 $this->assertEquals($expectedResult, $actualResult);
92 }
93
94 /**
95 * @test
96 */
97 public function getPluginConfigurationReturnsExtensionConfiguration() {
98 $testSettings = array(
99 'settings.' => array(
100 'foo' => 'bar'
101 )
102 );
103 $testSettingsConverted = array(
104 'settings' => array(
105 'foo' => 'bar'
106 )
107 );
108 $testSetup = array(
109 'plugin.' => array(
110 'tx_someextensionname.' => $testSettings,
111 ),
112 );
113 $this->mockTypoScriptService->expects($this->any())->method('convertTypoScriptArrayToPlainArray')->with($testSettings)->will($this->returnValue($testSettingsConverted));
114 $GLOBALS['TSFE']->tmpl->setup = $testSetup;
115 $expectedResult = array(
116 'settings' => array(
117 'foo' => 'bar'
118 )
119 );
120 $actualResult = $this->frontendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
121 $this->assertEquals($expectedResult, $actualResult);
122 }
123
124 /**
125 * @test
126 */
127 public function getPluginConfigurationReturnsPluginConfiguration() {
128 $testSettings = array(
129 'settings.' => array(
130 'foo' => 'bar'
131 )
132 );
133 $testSettingsConverted = array(
134 'settings' => array(
135 'foo' => 'bar'
136 )
137 );
138 $testSetup = array(
139 'plugin.' => array(
140 'tx_someextensionname_somepluginname.' => $testSettings
141 ),
142 );
143 $this->mockTypoScriptService->expects($this->any())->method('convertTypoScriptArrayToPlainArray')->with($testSettings)->will($this->returnValue($testSettingsConverted));
144 $GLOBALS['TSFE']->tmpl->setup = $testSetup;
145 $expectedResult = array(
146 'settings' => array(
147 'foo' => 'bar'
148 )
149 );
150 $actualResult = $this->frontendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
151 $this->assertEquals($expectedResult, $actualResult);
152 }
153
154 /**
155 * @test
156 */
157 public function getPluginConfigurationRecursivelyMergesExtensionAndPluginConfiguration() {
158 $testExtensionSettings = array(
159 'settings.' => array(
160 'foo' => 'bar',
161 'some.' => array(
162 'nested' => 'value'
163 ),
164 )
165 );
166 $testExtensionSettingsConverted = array(
167 'settings' => array(
168 'foo' => 'bar',
169 'some' => array(
170 'nested' => 'value'
171 ),
172 )
173 );
174 $testPluginSettings = array(
175 'settings.' => array(
176 'some.' => array(
177 'nested' => 'valueOverridde',
178 'new' => 'value',
179 ),
180 )
181 );
182 $testPluginSettingsConverted = array(
183 'settings' => array(
184 'some' => array(
185 'nested' => 'valueOverridde',
186 'new' => 'value',
187 ),
188 )
189 );
190 $testSetup = array(
191 'plugin.' => array(
192 'tx_someextensionname.' => $testExtensionSettings,
193 'tx_someextensionname_somepluginname.' => $testPluginSettings,
194 ),
195 );
196 $this->mockTypoScriptService->expects($this->at(0))->method('convertTypoScriptArrayToPlainArray')->with($testExtensionSettings)->will($this->returnValue($testExtensionSettingsConverted));
197 $this->mockTypoScriptService->expects($this->at(1))->method('convertTypoScriptArrayToPlainArray')->with($testPluginSettings)->will($this->returnValue($testPluginSettingsConverted));
198 $GLOBALS['TSFE']->tmpl->setup = $testSetup;
199 $expectedResult = array(
200 'settings' => array(
201 'foo' => 'bar',
202 'some' => array(
203 'nested' => 'valueOverridde',
204 'new' => 'value'
205 ),
206 ),
207 );
208 $actualResult = $this->frontendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
209 $this->assertEquals($expectedResult, $actualResult);
210 }
211
212 /**
213 * @test
214 */
215 public function getSwitchableControllerActionsReturnsEmptyArrayByDefault() {
216 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase'] = NULL;
217 $expectedResult = array();
218 $actualResult = $this->frontendConfigurationManager->_call('getSwitchableControllerActions', 'SomeExtensionName', 'SomePluginName');
219 $this->assertEquals($expectedResult, $actualResult);
220 }
221
222 /**
223 * @test
224 */
225 public function getSwitchableControllerActionsReturnsConfigurationStoredInExtconf() {
226 $testSwitchableControllerActions = array(
227 'Controller1' => array(
228 'actions' => array(
229 'action1', 'action2'
230 ),
231 'nonCacheableActions' => array(
232 'action1'
233 ),
234 ),
235 'Controller2' => array(
236 'actions' => array(
237 'action3', 'action4'
238 ),
239 )
240 );
241 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions']['SomeExtensionName']['plugins']['SomePluginName']['controllers'] = $testSwitchableControllerActions;
242 $expectedResult = $testSwitchableControllerActions;
243 $actualResult = $this->frontendConfigurationManager->_call('getSwitchableControllerActions', 'SomeExtensionName', 'SomePluginName');
244 $this->assertEquals($expectedResult, $actualResult);
245 }
246
247 /**
248 * @test
249 */
250 public function overrideSwitchableControllerActionsFromFlexFormReturnsUnchangedFrameworkConfigurationIfNoFlexFormConfigurationIsFound() {
251 $frameworkConfiguration = array(
252 'pluginName' => 'Pi1',
253 'extensionName' => 'SomeExtension',
254 'controllerConfiguration' => array(
255 'Controller1' => array(
256 'controller' => 'Controller1',
257 'actions' => 'action1 , action2'
258 ),
259 'Controller2' => array(
260 'controller' => 'Controller2',
261 'actions' => 'action2 , action1,action3',
262 'nonCacheableActions' => 'action2, action3'
263 )
264 )
265 );
266 $flexFormConfiguration = array();
267 $actualResult = $this->frontendConfigurationManager->_call('overrideSwitchableControllerActionsFromFlexForm', $frameworkConfiguration, $flexFormConfiguration);
268 $this->assertSame($frameworkConfiguration, $actualResult);
269 }
270
271 /**
272 * @test
273 */
274 public function overrideSwitchableControllerActionsFromFlexFormMergesNonCacheableActions() {
275 $frameworkConfiguration = array(
276 'pluginName' => 'Pi1',
277 'extensionName' => 'SomeExtension',
278 'controllerConfiguration' => array(
279 'Controller1' => array(
280 'actions' => array('action1 , action2')
281 ),
282 'Controller2' => array(
283 'actions' => array('action2', 'action1','action3'),
284 'nonCacheableActions' => array('action2', 'action3')
285 )
286 )
287 );
288 $flexFormConfiguration = array(
289 'switchableControllerActions' => 'Controller1 -> action2;Controller2->action3; Controller2->action1'
290 );
291 $expectedResult = array(
292 'pluginName' => 'Pi1',
293 'extensionName' => 'SomeExtension',
294 'controllerConfiguration' => array(
295 'Controller1' => array(
296 'actions' => array('action2'),
297 ),
298 'Controller2' => array(
299 'actions' => array('action3', 'action1'),
300 'nonCacheableActions' => array(1 => 'action3'),
301 )
302 )
303 );
304 $actualResult = $this->frontendConfigurationManager->_call('overrideSwitchableControllerActionsFromFlexForm', $frameworkConfiguration, $flexFormConfiguration);
305 $this->assertEquals($expectedResult, $actualResult);
306 }
307
308 /**
309 * @test
310 * @expectedException Tx_Extbase_Configuration_Exception_ParseError
311 */
312 public function overrideSwitchableControllerActionsThrowsExceptionIfFlexFormConfigurationIsInvalid() {
313 $frameworkConfiguration = array(
314 'pluginName' => 'Pi1',
315 'extensionName' => 'SomeExtension',
316 'controllerConfiguration' => array(
317 'Controller1' => array(
318 'actions' => array('action1 , action2')
319 ),
320 'Controller2' => array(
321 'actions' => array('action2', 'action1','action3'),
322 'nonCacheableActions' => array('action2', 'action3')
323 )
324 )
325 );
326 $flexFormConfiguration = array(
327 'switchableControllerActions' => 'Controller1->;Controller2->action3;Controller2->action1'
328 );
329 $this->frontendConfigurationManager->_call('overrideSwitchableControllerActionsFromFlexForm', $frameworkConfiguration, $flexFormConfiguration);
330 }
331
332 /**
333 * @test
334 */
335 public function getContextSpecificFrameworkConfigurationCorrectlyCallsOverrideMethods() {
336 $frameworkConfiguration = array(
337 'some' => array(
338 'framework' => 'configuration'
339 ),
340 );
341 $frontendConfigurationManager = $this->getAccessibleMock('Tx_Extbase_Configuration_FrontendConfigurationManager', array('overrideStoragePidIfStartingPointIsSet', 'overrideConfigurationFromPlugin', 'overrideConfigurationFromFlexForm'));
342 $frontendConfigurationManager->expects($this->at(0))->method('overrideStoragePidIfStartingPointIsSet')->with($frameworkConfiguration)->will($this->returnValue(array('overridden' => 'storagePid')));
343 $frontendConfigurationManager->expects($this->at(1))->method('overrideConfigurationFromPlugin')->with(array('overridden' => 'storagePid'))->will($this->returnValue(array('overridden' => 'pluginConfiguration')));
344 $frontendConfigurationManager->expects($this->at(2))->method('overrideConfigurationFromFlexForm')->with(array('overridden' => 'pluginConfiguration'))->will($this->returnValue(array('overridden' => 'flexFormConfiguration')));
345 $expectedResult = array('overridden' => 'flexFormConfiguration');
346 $actualResult = $frontendConfigurationManager->_call('getContextSpecificFrameworkConfiguration', $frameworkConfiguration);
347 $this->assertEquals($expectedResult, $actualResult);
348 }
349 }
350 ?>