9b31971a0c41d527d24eae69fdcfe0dfa05f1857
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Utility / Extension_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Oliver Hader <oliver@typo3.org>
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 require_once (t3lib_extMgm::extPath('extbase') . 'Tests/Fixtures/Dispatcher.php');
26
27 /**
28 * Testcase for class Tx_Extbase_Utility_Extension
29 *
30 * @package Extbase
31 * @subpackage extbase
32 */
33 class Tx_Extbase_Utility_Extension_testcase extends tx_phpunit_testcase {
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 public function setUp() {
52 global $TYPO3_CONF_VARS;
53 $this->typo3ConfVars = $TYPO3_CONF_VARS;
54 $this->typo3DbBackup = $GLOBALS['TYPO3_DB'];
55 $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB', array('fullQuoteStr', 'exec_SELECTgetRows'));
56 $this->tsfeBackup = $GLOBALS['TSFE'];
57 if (!isset($GLOBALS['TSFE']->tmpl)) {
58 $GLOBALS['TSFE']->tmpl = new stdClass();
59 }
60 if (!isset($GLOBALS['TSFE']->tmpl->setup)) {
61 $GLOBALS['TSFE']->tmpl->setup = array();
62 }
63 $GLOBALS['TSFE']->tmpl->setup['tt_content.']['list.']['20.'] = array(
64 '9' => 'CASE',
65 '9.' => array(
66 'key.' => array(
67 'field' => 'layout'),
68 0 => '< plugin.tt_news'
69 ),
70 'someextension_someplugin' => 'USER',
71 'someextension_someplugin.' => array(
72 'userFunc' => 'tx_extbase_dispatcher->dispatch',
73 'pluginName' => 'SomePlugin',
74 'extensionName' => 'SomeExtension',
75 'controller' => 'ControllerName',
76 'action' => 'index',
77 'switchableControllerActions.' => array(
78 'ControllerName.' => array(
79 'actions' => 'index,otherAction',
80 ),
81 ),
82 ),
83 'someotherextensionname_secondplugin' => 'USER',
84 'someotherextensionname_secondplugin.' => array(
85 'userFunc' => 'tx_extbase_dispatcher->dispatch',
86 'pluginName' => 'SecondPlugin',
87 'extensionName' => 'SomeOtherExtensionName',
88 'controller' => 'ControllerName',
89 'action' => 'index',
90 'switchableControllerActions.' => array(
91 'ControllerName.' => array(
92 'actions' => 'index,otherAction',
93 ),
94 'SecondControllerName.' => array(
95 'actions' => 'someAction,someOtherAction',
96 'nonCacheableActions' => 'someOtherAction',
97 ),
98 ),
99 ),
100 'extensionname_thirdplugin' => 'USER',
101 'extensionname_thirdplugin.' => array(
102 'userFunc' => 'tx_extbase_dispatcher->dispatch',
103 'pluginName' => 'ThirdPlugin',
104 'extensionName' => 'ExtensionName',
105 'controller' => 'ControllerName',
106 'action' => 'index',
107 'switchableControllerActions.' => array(
108 'FirstController.' => array(
109 'actions' => 'otherAction,thirdAction',
110 ),
111 ),
112 ),
113 );
114 }
115
116 public function tearDown() {
117 global $TYPO3_CONF_VARS;
118 $TYPO3_CONF_VARS = $this->typo3ConfVars;
119 $GLOBALS['TSFE'] = $this->tsfeBackup;
120 }
121
122 /**
123 * @test
124 * @see Tx_Extbase_Utility_Extension::registerPlugin
125 */
126 public function configurePluginWorksForMinimalisticSetup() {
127 global $TYPO3_CONF_VARS;
128 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
129 Tx_Extbase_Utility_Extension::configurePlugin(
130 'MyExtension',
131 'Pi1',
132 array('Blog' => 'index')
133 );
134 $staticTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.']['43'];
135
136 $this->assertContains('tt_content.list.20.myextension_pi1 = USER', $staticTypoScript);
137 $this->assertContains('
138 pluginName = Pi1
139 extensionName = MyExtension', $staticTypoScript);
140
141 $this->assertNotContains('USER_INT', $staticTypoScript);
142 }
143
144
145 /**
146 * @test
147 * @see Tx_Extbase_Utility_Extension::registerPlugin
148 */
149 public function configurePluginCreatesCorrectDefaultTypoScriptSetup() {
150 global $TYPO3_CONF_VARS;
151 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
152 Tx_Extbase_Utility_Extension::configurePlugin(
153 'MyExtension',
154 'Pi1',
155 array('Blog' => 'index')
156 );
157 $staticTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.']['43'];
158 $defaultTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup'];
159 $this->assertContains('tt_content.list.20.myextension_pi1 = USER', $staticTypoScript);
160 $this->assertContains('
161 plugin.tx_myextension {
162 settings {
163 }
164 persistence {
165 storagePid =
166 classes {
167 }
168 }
169 view {
170 templateRootPath =
171 layoutRootPath =
172 partialRootPath =
173 }
174 }', $defaultTypoScript);
175 }
176
177 /**
178 * @test
179 * @see Tx_Extbase_Utility_Extension::registerPlugin
180 */
181 public function configurePluginWorksForASingleControllerAction() {
182 global $TYPO3_CONF_VARS;
183 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
184 Tx_Extbase_Utility_Extension::configurePlugin(
185 'MyExtension',
186 'Pi1',
187 array(
188 'FirstController' => 'index'
189 )
190 );
191 $staticTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.']['43'];
192
193 $this->assertContains('tt_content.list.20.myextension_pi1 = USER', $staticTypoScript);
194 $this->assertContains('
195 pluginName = Pi1
196 extensionName = MyExtension', $staticTypoScript);
197 $this->assertContains('
198 controller = FirstController
199 action = index', $staticTypoScript);
200 $this->assertNotContains('USER_INT', $staticTypoScript);
201 }
202
203 /**
204 * @test
205 * @see Tx_Extbase_Utility_Extension::registerPlugin
206 */
207 public function configurePluginWithEmptyPluginNameResultsInAnError() {
208 $this->setExpectedException('InvalidArgumentException');
209 Tx_Extbase_Utility_Extension::configurePlugin(
210 'MyExtension',
211 '',
212 array(
213 'FirstController' => 'index'
214 )
215 );
216 }
217
218 /**
219 * @test
220 * @see Tx_Extbase_Utility_Extension::registerPlugin
221 */
222 public function configurePluginWithEmptyExtensionNameResultsInAnError() {
223 $this->setExpectedException('InvalidArgumentException');
224 Tx_Extbase_Utility_Extension::configurePlugin(
225 '',
226 'Pi1',
227 array(
228 'FirstController' => 'index'
229 )
230 );
231 }
232
233 /**
234 * @test
235 * @see Tx_Extbase_Utility_Extension::registerPlugin
236 */
237 public function configurePluginRespectsDefaultActionAsANonCacheableAction() {
238 global $TYPO3_CONF_VARS;
239 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
240 Tx_Extbase_Utility_Extension::configurePlugin(
241 'MyExtension',
242 'Pi1',
243 array(
244 'FirstController' => 'index,show,new,create,delete,edit,update'
245 ),
246 array(
247 'FirstController' => 'index,show'
248 )
249 );
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);
256 }
257
258 /**
259 * @test
260 * @see Tx_Extbase_Utility_Extension::registerPlugin
261 */
262 public function configurePluginRespectsNonDefaultActionAsANonCacheableAction() {
263 global $TYPO3_CONF_VARS;
264 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
265 Tx_Extbase_Utility_Extension::configurePlugin(
266 'MyExtension',
267 'Pi1',
268 array(
269 'FirstController' => 'index,show,new,create,delete,edit,update'
270 ),
271 array(
272 'FirstController' => 'show,new'
273 )
274 );
275 $staticTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.']['43'];
276
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);
282 }
283
284 /**
285 * @test
286 * @see Tx_Extbase_Utility_Extension::registerPlugin
287 */
288 public function configurePluginWorksForMultipleControllerActionsWithCacheableActionAsDefault() {
289 global $TYPO3_CONF_VARS;
290 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
291 Tx_Extbase_Utility_Extension::configurePlugin(
292 'MyExtension',
293 'Pi1',
294 array(
295 'FirstController' => 'index,show,new,create,delete,edit,update',
296 'SecondController' => 'index,show,delete',
297 'ThirdController' => 'create'
298 ),
299 array(
300 'FirstController' => 'new,create,edit,update',
301 'SecondController' => 'delete',
302 'ThirdController' => 'create'
303 )
304 );
305 $staticTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.']['43'];
306
307 $this->assertContains('
308 tt_content.list.20.myextension_pi1 = USER
309 tt_content.list.20.myextension_pi1 {', $staticTypoScript);
310
311 $this->assertContains('FirstController.nonCacheableActions = new,create,edit,update
312 ', $staticTypoScript);
313
314 $this->assertContains('SecondController.nonCacheableActions = delete
315 ', $staticTypoScript);
316
317 $this->assertContains('ThirdController.nonCacheableActions = create
318 ', $staticTypoScript);
319 }
320
321
322 /**
323 * @test
324 * @see Tx_Extbase_Utility_Extension::registerPlugin
325 */
326 public function configurePluginWorksForMultipleControllerActionsWithNonCacheableActionAsDefault() {
327 global $TYPO3_CONF_VARS;
328 $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.'] = array();
329 Tx_Extbase_Utility_Extension::configurePlugin(
330 'MyExtension',
331 'Pi1',
332 array(
333 'FirstController' => 'index,show,new,create,delete,edit,update',
334 'SecondController' => 'index,show,delete',
335 'ThirdController' => 'create'
336 ),
337 array(
338 'FirstController' => 'index,new,create,edit,update',
339 'SecondController' => 'delete',
340 'ThirdController' => 'create'
341 )
342 );
343 $staticTypoScript = $TYPO3_CONF_VARS['FE']['defaultTypoScript_setup.']['43'];
344
345 $this->assertContains('
346 tt_content.list.20.myextension_pi1 = USER
347 tt_content.list.20.myextension_pi1 {', $staticTypoScript);
348
349 $this->assertContains('FirstController.nonCacheableActions = index,new,create,edit,update
350 ', $staticTypoScript);
351
352 $this->assertContains('SecondController.nonCacheableActions = delete
353 ', $staticTypoScript);
354
355 $this->assertContains('ThirdController.nonCacheableActions = create
356 ', $staticTypoScript);
357 }
358
359 /**
360 * DataProvider for getPluginNamespaceByPluginSignatureTests()
361 *
362 * @return array
363 */
364 public function getPluginNamespaceByPluginSignatureDataProvider() {
365 return array(
366 array('someextension_someplugin', 'tx_someextension_someplugin'),
367 array('nonexistingextension_someplugin', 'tx_nonexistingextension_someplugin'),
368 array('InvalidPluginNamespace', 'tx_InvalidPluginNamespace'),
369 );
370 }
371
372 /**
373 * @test
374 * @dataProvider getPluginNamespaceByPluginSignatureDataProvider
375 */
376 public function getPluginNamespaceByPluginSignatureTests($pluginSignature, $expectedResult) {
377 $dispatcher = new Tx_Extbase_Tests_Fixtures_Dispatcher();
378 $mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_AbstractConfigurationManager', array('getContextSpecificFrameworkConfiguration', 'loadTypoScriptSetup'));
379 $dispatcher->setConfigurationManager($mockConfigurationManager);
380 $actualResult = Tx_Extbase_Utility_Extension::getPluginNamespaceByPluginSignature($pluginSignature);
381 $this->assertEquals($expectedResult, $actualResult, 'Failing for $pluginSignature: "' . $pluginSignature . '"');
382 }
383
384 /**
385 * @test
386 */
387 public function pluginNamespaceCanBeOverridden() {
388 $dispatcher = new Tx_Extbase_Tests_Fixtures_Dispatcher();
389 $mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_AbstractConfigurationManager', array('getContextSpecificFrameworkConfiguration', 'loadTypoScriptSetup', 'getFrameworkConfiguration'));
390 $mockConfigurationManager->expects($this->once())->method('getFrameworkConfiguration')->will($this->returnValue(array('view' => array('pluginNamespace' => 'overridden_plugin_namespace'))));
391 $dispatcher->setConfigurationManager($mockConfigurationManager);
392 $expectedResult = 'overridden_plugin_namespace';
393 $actualResult = Tx_Extbase_Utility_Extension::getPluginNamespaceByPluginSignature('somePluginSignature');
394 $this->assertEquals($expectedResult, $actualResult);
395 }
396 }
397
398 ?>