[TASK][WIP] Move non class code back to source files
[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 class Tx_Extbase_Tests_Unit_Configuration_FrontendConfigurationManagerTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
28
29 /**
30 * @var tslib_fe
31 */
32 protected $tsfeBackup;
33
34 /**
35 * @var tslib_cObj
36 */
37 protected $mockContentObject;
38
39 /**
40 * @var Tx_Extbase_Configuration_FrontendConfigurationManager
41 */
42 protected $frontendConfigurationManager;
43
44 /**
45 * @var array
46 */
47 protected $extConfBackup;
48
49 /**
50 * @var Tx_Extbase_Service_TypoScriptService
51 */
52 protected $mockTypoScriptService;
53
54 /**
55 * Sets up this testcase
56 */
57 public function setUp() {
58 $this->tsfeBackup = $GLOBALS['TSFE'];
59 $this->mockContentObject = $this->getMock('tslib_cObj');
60 $this->extConfBackup = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase'];
61 $this->frontendConfigurationManager = $this->getAccessibleMock('Tx_Extbase_Configuration_FrontendConfigurationManager', array('dummy'));
62 $this->frontendConfigurationManager->_set('contentObject', $this->mockContentObject);
63 $this->mockTypoScriptService = $this->getAccessibleMock('Tx_Extbase_Service_TypoScriptService');
64 $this->frontendConfigurationManager->injectTypoScriptService($this->mockTypoScriptService);
65 }
66
67 /**
68 * Tears down this testcase
69 */
70 public function tearDown() {
71 $GLOBALS['TSFE']->tmpl->setup;
72 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase'] = $this->extConfBackup;
73 }
74
75 /**
76 * @test
77 */
78 public function getTypoScriptSetupReturnsSetupFromTsfe() {
79 $GLOBALS['TSFE']->tmpl->setup = array('foo' => 'bar');
80 $this->assertEquals(array('foo' => 'bar'), $this->frontendConfigurationManager->_call('getTypoScriptSetup'));
81 }
82
83 /**
84 * @test
85 */
86 public function getPluginConfigurationReturnsEmptyArrayIfNoPluginConfigurationWasFound() {
87 $GLOBALS['TSFE']->tmpl->setup = array('foo' => 'bar');
88 $expectedResult = array();
89 $actualResult = $this->frontendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
90 $this->assertEquals($expectedResult, $actualResult);
91 }
92
93 /**
94 * @test
95 */
96 public function getPluginConfigurationReturnsExtensionConfiguration() {
97 $testSettings = array(
98 'settings.' => array(
99 'foo' => 'bar'
100 )
101 );
102 $testSettingsConverted = array(
103 'settings' => array(
104 'foo' => 'bar'
105 )
106 );
107 $testSetup = array(
108 'plugin.' => array(
109 'tx_someextensionname.' => $testSettings
110 )
111 );
112 $this->mockTypoScriptService->expects($this->any())->method('convertTypoScriptArrayToPlainArray')->with($testSettings)->will($this->returnValue($testSettingsConverted));
113 $GLOBALS['TSFE']->tmpl->setup = $testSetup;
114 $expectedResult = array(
115 'settings' => array(
116 'foo' => 'bar'
117 )
118 );
119 $actualResult = $this->frontendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName');
120 $this->assertEquals($expectedResult, $actualResult);
121 }
122
123 /**
124 * @test
125 */
126 public function getPluginConfigurationReturnsPluginConfiguration() {
127 $testSettings = array(
128 'settings.' => array(
129 'foo' => 'bar'
130 )
131 );
132 $testSettingsConverted = array(
133 'settings' => array(
134 'foo' => 'bar'
135 )
136 );
137 $testSetup = array(
138 'plugin.' => array(
139 'tx_someextensionname_somepluginname.' => $testSettings
140 )
141 );
142 $this->mockTypoScriptService->expects($this->any())->method('convertTypoScriptArrayToPlainArray')->with($testSettings)->will($this->returnValue($testSettingsConverted));
143 $GLOBALS['TSFE']->tmpl->setup = $testSetup;
144 $expectedResult = array(
145 'settings' => array(
146 'foo' => 'bar'
147 )
148 );
149 $actualResult = $this->frontendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
150 $this->assertEquals($expectedResult, $actualResult);
151 }
152
153 /**
154 * @test
155 */
156 public function getPluginConfigurationRecursivelyMergesExtensionAndPluginConfiguration() {
157 $testExtensionSettings = array(
158 'settings.' => array(
159 'foo' => 'bar',
160 'some.' => array(
161 'nested' => 'value'
162 )
163 )
164 );
165 $testExtensionSettingsConverted = array(
166 'settings' => array(
167 'foo' => 'bar',
168 'some' => array(
169 'nested' => 'value'
170 )
171 )
172 );
173 $testPluginSettings = array(
174 'settings.' => array(
175 'some.' => array(
176 'nested' => 'valueOverridde',
177 'new' => 'value'
178 )
179 )
180 );
181 $testPluginSettingsConverted = array(
182 'settings' => array(
183 'some' => array(
184 'nested' => 'valueOverridde',
185 'new' => 'value'
186 )
187 )
188 );
189 $testSetup = array(
190 'plugin.' => array(
191 'tx_someextensionname.' => $testExtensionSettings,
192 'tx_someextensionname_somepluginname.' => $testPluginSettings
193 )
194 );
195 $this->mockTypoScriptService->expects($this->at(0))->method('convertTypoScriptArrayToPlainArray')->with($testExtensionSettings)->will($this->returnValue($testExtensionSettingsConverted));
196 $this->mockTypoScriptService->expects($this->at(1))->method('convertTypoScriptArrayToPlainArray')->with($testPluginSettings)->will($this->returnValue($testPluginSettingsConverted));
197 $GLOBALS['TSFE']->tmpl->setup = $testSetup;
198 $expectedResult = array(
199 'settings' => array(
200 'foo' => 'bar',
201 'some' => array(
202 'nested' => 'valueOverridde',
203 'new' => 'value'
204 )
205 )
206 );
207 $actualResult = $this->frontendConfigurationManager->_call('getPluginConfiguration', 'SomeExtensionName', 'SomePluginName');
208 $this->assertEquals($expectedResult, $actualResult);
209 }
210
211 /**
212 * @test
213 */
214 public function getSwitchableControllerActionsReturnsEmptyArrayByDefault() {
215 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase'] = NULL;
216 $expectedResult = array();
217 $actualResult = $this->frontendConfigurationManager->_call('getSwitchableControllerActions', 'SomeExtensionName', 'SomePluginName');
218 $this->assertEquals($expectedResult, $actualResult);
219 }
220
221 /**
222 * @test
223 */
224 public function getSwitchableControllerActionsReturnsConfigurationStoredInExtconf() {
225 $testSwitchableControllerActions = array(
226 'Controller1' => array(
227 'actions' => array(
228 'action1',
229 'action2'
230 ),
231 'nonCacheableActions' => array(
232 'action1'
233 )
234 ),
235 'Controller2' => array(
236 'actions' => array(
237 'action3',
238 'action4'
239 )
240 )
241 );
242 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions']['SomeExtensionName']['plugins']['SomePluginName']['controllers'] = $testSwitchableControllerActions;
243 $expectedResult = $testSwitchableControllerActions;
244 $actualResult = $this->frontendConfigurationManager->_call('getSwitchableControllerActions', 'SomeExtensionName', 'SomePluginName');
245 $this->assertEquals($expectedResult, $actualResult);
246 }
247
248 /**
249 * @test
250 */
251 public function overrideSwitchableControllerActionsFromFlexFormReturnsUnchangedFrameworkConfigurationIfNoFlexFormConfigurationIsFound() {
252 $frameworkConfiguration = array(
253 'pluginName' => 'Pi1',
254 'extensionName' => 'SomeExtension',
255 'controllerConfiguration' => array(
256 'Controller1' => array(
257 'controller' => 'Controller1',
258 'actions' => 'action1 , action2'
259 ),
260 'Controller2' => array(
261 'controller' => 'Controller2',
262 'actions' => 'action2 , action1,action3',
263 'nonCacheableActions' => 'action2, action3'
264 )
265 )
266 );
267 $flexFormConfiguration = array();
268 $actualResult = $this->frontendConfigurationManager->_call('overrideSwitchableControllerActionsFromFlexForm', $frameworkConfiguration, $flexFormConfiguration);
269 $this->assertSame($frameworkConfiguration, $actualResult);
270 }
271
272 /**
273 * @test
274 */
275 public function overrideSwitchableControllerActionsFromFlexFormMergesNonCacheableActions() {
276 $frameworkConfiguration = array(
277 'pluginName' => 'Pi1',
278 'extensionName' => 'SomeExtension',
279 'controllerConfiguration' => array(
280 'Controller1' => array(
281 'actions' => array('action1 , action2')
282 ),
283 'Controller2' => array(
284 'actions' => array('action2', 'action1', 'action3'),
285 'nonCacheableActions' => array('action2', 'action3')
286 )
287 )
288 );
289 $flexFormConfiguration = array(
290 'switchableControllerActions' => 'Controller1 -> action2;Controller2->action3; Controller2->action1'
291 );
292 $expectedResult = array(
293 'pluginName' => 'Pi1',
294 'extensionName' => 'SomeExtension',
295 'controllerConfiguration' => array(
296 'Controller1' => array(
297 'actions' => array('action2')
298 ),
299 'Controller2' => array(
300 'actions' => array('action3', 'action1'),
301 'nonCacheableActions' => array(1 => 'action3')
302 )
303 )
304 );
305 $actualResult = $this->frontendConfigurationManager->_call('overrideSwitchableControllerActionsFromFlexForm', $frameworkConfiguration, $flexFormConfiguration);
306 $this->assertEquals($expectedResult, $actualResult);
307 }
308
309 /**
310 * @test
311 * @expectedException Tx_Extbase_Configuration_Exception_ParseError
312 */
313 public function overrideSwitchableControllerActionsThrowsExceptionIfFlexFormConfigurationIsInvalid() {
314 $frameworkConfiguration = array(
315 'pluginName' => 'Pi1',
316 'extensionName' => 'SomeExtension',
317 'controllerConfiguration' => array(
318 'Controller1' => array(
319 'actions' => array('action1 , action2')
320 ),
321 'Controller2' => array(
322 'actions' => array('action2', 'action1', 'action3'),
323 'nonCacheableActions' => array('action2', 'action3')
324 )
325 )
326 );
327 $flexFormConfiguration = array(
328 'switchableControllerActions' => 'Controller1->;Controller2->action3;Controller2->action1'
329 );
330 $this->frontendConfigurationManager->_call('overrideSwitchableControllerActionsFromFlexForm', $frameworkConfiguration, $flexFormConfiguration);
331 }
332
333 /**
334 * @test
335 */
336 public function getContextSpecificFrameworkConfigurationCorrectlyCallsOverrideMethods() {
337 $frameworkConfiguration = array(
338 'some' => array(
339 'framework' => 'configuration'
340 )
341 );
342 $frontendConfigurationManager = $this->getAccessibleMock('Tx_Extbase_Configuration_FrontendConfigurationManager', array('overrideStoragePidIfStartingPointIsSet', 'overrideConfigurationFromPlugin', 'overrideConfigurationFromFlexForm'));
343 $frontendConfigurationManager->expects($this->at(0))->method('overrideStoragePidIfStartingPointIsSet')->with($frameworkConfiguration)->will($this->returnValue(array('overridden' => 'storagePid')));
344 $frontendConfigurationManager->expects($this->at(1))->method('overrideConfigurationFromPlugin')->with(array('overridden' => 'storagePid'))->will($this->returnValue(array('overridden' => 'pluginConfiguration')));
345 $frontendConfigurationManager->expects($this->at(2))->method('overrideConfigurationFromFlexForm')->with(array('overridden' => 'pluginConfiguration'))->will($this->returnValue(array('overridden' => 'flexFormConfiguration')));
346 $expectedResult = array('overridden' => 'flexFormConfiguration');
347 $actualResult = $frontendConfigurationManager->_call('getContextSpecificFrameworkConfiguration', $frameworkConfiguration);
348 $this->assertEquals($expectedResult, $actualResult);
349 }
350
351 }
352
353 ?>