[CLEANUP] Adjust code to coding guidelines
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Service / ExtensionServiceTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 Extbase Team
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Testcase for class \TYPO3\CMS\Extbase\Service\ExtensionService
28 */
29 class ExtensionServiceTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
30
31 /**
32 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
33 */
34 protected $typo3DbBackup;
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
38 */
39 protected $mockConfigurationManager;
40
41 /**
42 * @var \TYPO3\CMS\Extbase\Service\ExtensionService
43 */
44 protected $extensionService;
45
46 public function setUp() {
47 $this->typo3DbBackup = $GLOBALS['TYPO3_DB'];
48 $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('fullQuoteStr', 'exec_SELECTgetRows'));
49 $GLOBALS['TSFE'] = new \stdClass();
50 $this->extensionService = new \TYPO3\CMS\Extbase\Service\ExtensionService();
51 $this->mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
52 $this->extensionService->injectConfigurationManager($this->mockConfigurationManager);
53 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'] = array(
54 'ExtensionName' => array(
55 'plugins' => array(
56 'SomePlugin' => array(
57 'controllers' => array(
58 'ControllerName' => array(
59 'actions' => array('index', 'otherAction')
60 )
61 )
62 ),
63 'ThirdPlugin' => array(
64 'controllers' => array(
65 'ControllerName' => array(
66 'actions' => array('otherAction', 'thirdAction')
67 )
68 )
69 )
70 )
71 ),
72 'SomeOtherExtensionName' => array(
73 'plugins' => array(
74 'SecondPlugin' => array(
75 'controllers' => array(
76 'ControllerName' => array(
77 'actions' => array('index', 'otherAction')
78 ),
79 'SecondControllerName' => array(
80 'actions' => array('someAction', 'someOtherAction'),
81 'nonCacheableActions' => array('someOtherAction')
82 )
83 )
84 )
85 )
86 )
87 );
88 }
89
90 public function tearDown() {
91 $GLOBALS['TYPO3_DB'] = $this->typo3DbBackup;
92 }
93
94 /**
95 * DataProvider for getPluginNamespaceByPluginSignatureTests()
96 *
97 * @return array
98 */
99 public function getPluginNamespaceDataProvider() {
100 return array(
101 array('SomeExtension', 'SomePlugin', 'tx_someextension_someplugin'),
102 array('NonExistingExtension', 'SomePlugin', 'tx_nonexistingextension_someplugin'),
103 array('Invalid', '', 'tx_invalid_')
104 );
105 }
106
107 /**
108 * @test
109 * @dataProvider getPluginNamespaceDataProvider
110 * @param string $extensionName
111 * @param string $pluginName
112 * @param mixed $expectedResult
113 */
114 public function getPluginNamespaceTests($extensionName, $pluginName, $expectedResult) {
115 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array()));
116 $actualResult = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
117 $this->assertEquals($expectedResult, $actualResult, 'Failing for extension: "' . $extensionName . '", plugin: "' . $pluginName . '"');
118 }
119
120 /**
121 * @test
122 */
123 public function pluginNamespaceCanBeOverridden() {
124 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->with(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, 'SomeExtension', 'SomePlugin')->will($this->returnValue(array('view' => array('pluginNamespace' => 'overridden_plugin_namespace'))));
125 $expectedResult = 'overridden_plugin_namespace';
126 $actualResult = $this->extensionService->getPluginNamespace('SomeExtension', 'SomePlugin');
127 $this->assertEquals($expectedResult, $actualResult);
128 }
129
130 /**
131 * DataProvider for getPluginNameByActionTests()
132 *
133 * @return array
134 */
135 public function getPluginNameByActionDataProvider() {
136 return array(
137 array('ExtensionName', 'ControllerName', 'someNonExistingAction', NULL),
138 array('ExtensionName', 'ControllerName', 'index', 'SomePlugin'),
139 array('ExtensionName', 'ControllerName', 'thirdAction', 'ThirdPlugin'),
140 array('eXtEnSiOnNaMe', 'cOnTrOlLeRnAmE', 'thirdAction', NULL),
141 array('eXtEnSiOnNaMe', 'cOnTrOlLeRnAmE', 'ThIrDaCtIoN', NULL),
142 array('SomeOtherExtensionName', 'ControllerName', 'otherAction', 'SecondPlugin')
143 );
144 }
145
146 /**
147 * @test
148 * @dataProvider getPluginNameByActionDataProvider
149 * @param string $extensionName
150 * @param string $controllerName
151 * @param string $actionName
152 * @param mixed $expectedResult
153 */
154 public function getPluginNameByActionTests($extensionName, $controllerName, $actionName, $expectedResult) {
155 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->with(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK)->will($this->returnValue(array('view' => array('pluginNamespace' => 'overridden_plugin_namespace'))));
156 $actualResult = $this->extensionService->getPluginNameByAction($extensionName, $controllerName, $actionName);
157 $this->assertEquals($expectedResult, $actualResult, 'Failing for $extensionName: "' . $extensionName . '", $controllerName: "' . $controllerName . '", $actionName: "' . $actionName . '" - ');
158 }
159
160 /**
161 * @test
162 * @expectedException \TYPO3\CMS\Extbase\Exception
163 */
164 public function getPluginNameByActionThrowsExceptionIfMoreThanOnePluginMatches() {
165 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->with(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK)->will($this->returnValue(array('view' => array('pluginNamespace' => 'overridden_plugin_namespace'))));
166 $this->extensionService->getPluginNameByAction('ExtensionName', 'ControllerName', 'otherAction');
167 }
168
169 /**
170 * @test
171 */
172 public function getPluginNameByActionReturnsCurrentIfItCanHandleTheActionEvenIfMoreThanOnePluginMatches() {
173 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->with(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK)->will($this->returnValue(array('extensionName' => 'CurrentExtension', 'pluginName' => 'CurrentPlugin', 'controllerConfiguration' => array('ControllerName' => array('actions' => array('otherAction'))))));
174 $actualResult = $this->extensionService->getPluginNameByAction('CurrentExtension', 'ControllerName', 'otherAction');
175 $expectedResult = 'CurrentPlugin';
176 $this->assertEquals($expectedResult, $actualResult);
177 }
178
179 /**
180 * @test
181 */
182 public function isActionCacheableReturnsTrueByDefault() {
183 $mockConfiguration = array();
184 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($mockConfiguration));
185 $actualResult = $this->extensionService->isActionCacheable('SomeExtension', 'SomePlugin', 'SomeController', 'someAction');
186 $this->assertTrue($actualResult);
187 }
188
189 /**
190 * @test
191 */
192 public function isActionCacheableReturnsFalseIfActionIsNotCacheable() {
193 $mockConfiguration = array(
194 'controllerConfiguration' => array(
195 'SomeController' => array(
196 'nonCacheableActions' => array('someAction')
197 )
198 )
199 );
200 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($mockConfiguration));
201 $actualResult = $this->extensionService->isActionCacheable('SomeExtension', 'SomePlugin', 'SomeController', 'someAction');
202 $this->assertFalse($actualResult);
203 }
204
205 /**
206 * @test
207 */
208 public function getTargetPidByPluginSignatureReturnsNullIfConfigurationManagerIsNotInitialized() {
209 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(NULL));
210 $this->assertNull($this->extensionService->getTargetPidByPlugin('ExtensionName', 'PluginName'));
211 }
212
213 /**
214 * @test
215 */
216 public function getTargetPidByPluginSignatureReturnsNullIfDefaultPidIsZero() {
217 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 0))));
218 $this->assertNull($this->extensionService->getTargetPidByPlugin('ExtensionName', 'PluginName'));
219 }
220
221 /**
222 * @test
223 */
224 public function getTargetPidByPluginSignatureReturnsTheConfiguredDefaultPid() {
225 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 123))));
226 $expectedResult = 123;
227 $actualResult = $this->extensionService->getTargetPidByPlugin('ExtensionName', 'SomePlugin');
228 $this->assertEquals($expectedResult, $actualResult);
229 }
230
231 /**
232 * @test
233 */
234 public function getTargetPidByPluginSignatureDeterminesTheTargetPidIfDefaultPidIsAuto() {
235 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
236 $pluginSignature = 'extensionname_someplugin';
237 $GLOBALS['TSFE']->sys_page = $this->getMock('TYPO3\\CMS\\Frontend\\Page\\PageRepository', array('enableFields'));
238 $GLOBALS['TSFE']->sys_page->expects($this->once())->method('enableFields')->with('tt_content')->will($this->returnValue(' AND enable_fields'));
239 $GLOBALS['TYPO3_DB']->expects($this->once())->method('fullQuoteStr')->with($pluginSignature, 'tt_content')->will($this->returnValue('"pluginSignature"'));
240 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->with('pid', 'tt_content', 'list_type="pluginSignature" AND CType="list" AND enable_fields AND sys_language_uid=', '', '')->will($this->returnValue(array(array('pid' => '321'))));
241 $expectedResult = 321;
242 $actualResult = $this->extensionService->getTargetPidByPlugin('ExtensionName', 'SomePlugin');
243 $this->assertEquals($expectedResult, $actualResult);
244 }
245
246 /**
247 * @test
248 */
249 public function getTargetPidByPluginSignatureReturnsNullIfTargetPidCouldNotBeDetermined() {
250 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
251 $GLOBALS['TSFE']->sys_page = $this->getMock('TYPO3\\CMS\\Frontend\\Page\\PageRepository', array('enableFields'));
252 $GLOBALS['TSFE']->sys_page->expects($this->once())->method('enableFields')->will($this->returnValue(' AND enable_fields'));
253 $GLOBALS['TYPO3_DB']->expects($this->once())->method('fullQuoteStr')->will($this->returnValue('"pluginSignature"'));
254 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnValue(array()));
255 $this->assertNull($this->extensionService->getTargetPidByPlugin('ExtensionName', 'SomePlugin'));
256 }
257
258 /**
259 * @test
260 * @expectedException \TYPO3\CMS\Extbase\Exception
261 */
262 public function getTargetPidByPluginSignatureThrowsExceptionIfMoreThanOneTargetPidsWereFound() {
263 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
264 $GLOBALS['TSFE']->sys_page = $this->getMock('TYPO3\\CMS\\Frontend\\Page\\PageRepository', array('enableFields'));
265 $GLOBALS['TSFE']->sys_page->expects($this->once())->method('enableFields')->will($this->returnValue(' AND enable_fields'));
266 $GLOBALS['TYPO3_DB']->expects($this->once())->method('fullQuoteStr')->will($this->returnValue('"pluginSignature"'));
267 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnValue(array(array('pid' => 123), array('pid' => 124))));
268 $this->extensionService->getTargetPidByPlugin('ExtensionName', 'SomePlugin');
269 }
270
271 /**
272 * @test
273 */
274 public function getDefaultControllerNameByPluginReturnsNullIfGivenExtensionCantBeFound() {
275 $this->assertNull($this->extensionService->getDefaultControllerNameByPlugin('NonExistingExtensionName', 'SomePlugin'));
276 }
277
278 /**
279 * @test
280 */
281 public function getDefaultControllerNameByPluginReturnsNullIfGivenPluginCantBeFound() {
282 $this->assertNull($this->extensionService->getDefaultControllerNameByPlugin('ExtensionName', 'NonExistingPlugin'));
283 }
284
285 /**
286 * @test
287 */
288 public function getDefaultControllerNameByPluginReturnsFirstControllerNameOfGivenPlugin() {
289 $expectedResult = 'ControllerName';
290 $actualResult = $this->extensionService->getDefaultControllerNameByPlugin('ExtensionName', 'SomePlugin');
291 $this->assertEquals($expectedResult, $actualResult);
292 }
293
294 /**
295 * @test
296 */
297 public function getDefaultActionNameByPluginAndControllerReturnsNullIfGivenExtensionCantBeFound() {
298 $this->assertNull($this->extensionService->getDefaultActionNameByPluginAndController('NonExistingExtensionName', 'SomePlugin', 'ControllerName'));
299 }
300
301 /**
302 * @test
303 */
304 public function getDefaultActionNameByPluginAndControllerReturnsNullIfGivenPluginCantBeFound() {
305 $this->assertNull($this->extensionService->getDefaultActionNameByPluginAndController('ExtensionName', 'NonExistingPlugin', 'ControllerName'));
306 }
307
308 /**
309 * @test
310 */
311 public function getDefaultActionNameByPluginAndControllerReturnsNullIfGivenControllerCantBeFound() {
312 $this->assertNull($this->extensionService->getDefaultActionNameByPluginAndController('ExtensionName', 'SomePlugin', 'NonExistingControllerName'));
313 }
314
315 /**
316 * @test
317 */
318 public function getDefaultActionNameByPluginAndControllerReturnsFirstActionNameOfGivenController() {
319 $expectedResult = 'someAction';
320 $actualResult = $this->extensionService->getDefaultActionNameByPluginAndController('SomeOtherExtensionName', 'SecondPlugin', 'SecondControllerName');
321 $this->assertEquals($expectedResult, $actualResult);
322 }
323 }
324
325 ?>