[TASK] Re-work/simplify copyright header in PHP files - Part 8
[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 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Test case
19 */
20 class ExtensionServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
21
22 /**
23 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
24 */
25 protected $mockConfigurationManager;
26
27 /**
28 * @var \TYPO3\CMS\Extbase\Service\ExtensionService
29 */
30 protected $extensionService;
31
32 public function setUp() {
33 $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('fullQuoteStr', 'exec_SELECTgetRows'));
34 $GLOBALS['TSFE'] = new \stdClass();
35 $this->extensionService = $this->getAccessibleMock('TYPO3\CMS\Extbase\Service\ExtensionService', array('dummy'));
36 $this->mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
37 $this->extensionService->_set('configurationManager', $this->mockConfigurationManager);
38 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'] = array(
39 'ExtensionName' => array(
40 'plugins' => array(
41 'SomePlugin' => array(
42 'controllers' => array(
43 'ControllerName' => array(
44 'actions' => array('index', 'otherAction')
45 )
46 )
47 ),
48 'ThirdPlugin' => array(
49 'controllers' => array(
50 'ControllerName' => array(
51 'actions' => array('otherAction', 'thirdAction')
52 )
53 )
54 )
55 )
56 ),
57 'SomeOtherExtensionName' => array(
58 'plugins' => array(
59 'SecondPlugin' => array(
60 'controllers' => array(
61 'ControllerName' => array(
62 'actions' => array('index', 'otherAction')
63 ),
64 'SecondControllerName' => array(
65 'actions' => array('someAction', 'someOtherAction'),
66 'nonCacheableActions' => array('someOtherAction')
67 )
68 )
69 )
70 )
71 )
72 );
73 }
74
75 /**
76 * DataProvider for getPluginNamespaceByPluginSignatureTests()
77 *
78 * @return array
79 */
80 public function getPluginNamespaceDataProvider() {
81 return array(
82 array('SomeExtension', 'SomePlugin', 'tx_someextension_someplugin'),
83 array('NonExistingExtension', 'SomePlugin', 'tx_nonexistingextension_someplugin'),
84 array('Invalid', '', 'tx_invalid_')
85 );
86 }
87
88 /**
89 * @test
90 * @dataProvider getPluginNamespaceDataProvider
91 * @param string $extensionName
92 * @param string $pluginName
93 * @param mixed $expectedResult
94 */
95 public function getPluginNamespaceTests($extensionName, $pluginName, $expectedResult) {
96 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array()));
97 $actualResult = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
98 $this->assertEquals($expectedResult, $actualResult, 'Failing for extension: "' . $extensionName . '", plugin: "' . $pluginName . '"');
99 }
100
101 /**
102 * @test
103 */
104 public function pluginNamespaceCanBeOverridden() {
105 $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'))));
106 $expectedResult = 'overridden_plugin_namespace';
107 $actualResult = $this->extensionService->getPluginNamespace('SomeExtension', 'SomePlugin');
108 $this->assertEquals($expectedResult, $actualResult);
109 }
110
111 /**
112 * DataProvider for getPluginNameByActionTests()
113 *
114 * @return array
115 */
116 public function getPluginNameByActionDataProvider() {
117 return array(
118 array('ExtensionName', 'ControllerName', 'someNonExistingAction', NULL),
119 array('ExtensionName', 'ControllerName', 'index', 'SomePlugin'),
120 array('ExtensionName', 'ControllerName', 'thirdAction', 'ThirdPlugin'),
121 array('eXtEnSiOnNaMe', 'cOnTrOlLeRnAmE', 'thirdAction', NULL),
122 array('eXtEnSiOnNaMe', 'cOnTrOlLeRnAmE', 'ThIrDaCtIoN', NULL),
123 array('SomeOtherExtensionName', 'ControllerName', 'otherAction', 'SecondPlugin')
124 );
125 }
126
127 /**
128 * @test
129 * @dataProvider getPluginNameByActionDataProvider
130 * @param string $extensionName
131 * @param string $controllerName
132 * @param string $actionName
133 * @param mixed $expectedResult
134 */
135 public function getPluginNameByActionTests($extensionName, $controllerName, $actionName, $expectedResult) {
136 $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'))));
137 $actualResult = $this->extensionService->getPluginNameByAction($extensionName, $controllerName, $actionName);
138 $this->assertEquals($expectedResult, $actualResult, 'Failing for $extensionName: "' . $extensionName . '", $controllerName: "' . $controllerName . '", $actionName: "' . $actionName . '" - ');
139 }
140
141 /**
142 * @test
143 * @expectedException \TYPO3\CMS\Extbase\Exception
144 */
145 public function getPluginNameByActionThrowsExceptionIfMoreThanOnePluginMatches() {
146 $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'))));
147 $this->extensionService->getPluginNameByAction('ExtensionName', 'ControllerName', 'otherAction');
148 }
149
150 /**
151 * @test
152 */
153 public function getPluginNameByActionReturnsCurrentIfItCanHandleTheActionEvenIfMoreThanOnePluginMatches() {
154 $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'))))));
155 $actualResult = $this->extensionService->getPluginNameByAction('CurrentExtension', 'ControllerName', 'otherAction');
156 $expectedResult = 'CurrentPlugin';
157 $this->assertEquals($expectedResult, $actualResult);
158 }
159
160 /**
161 * @test
162 */
163 public function isActionCacheableReturnsTrueByDefault() {
164 $mockConfiguration = array();
165 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($mockConfiguration));
166 $actualResult = $this->extensionService->isActionCacheable('SomeExtension', 'SomePlugin', 'SomeController', 'someAction');
167 $this->assertTrue($actualResult);
168 }
169
170 /**
171 * @test
172 */
173 public function isActionCacheableReturnsFalseIfActionIsNotCacheable() {
174 $mockConfiguration = array(
175 'controllerConfiguration' => array(
176 'SomeController' => array(
177 'nonCacheableActions' => array('someAction')
178 )
179 )
180 );
181 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($mockConfiguration));
182 $actualResult = $this->extensionService->isActionCacheable('SomeExtension', 'SomePlugin', 'SomeController', 'someAction');
183 $this->assertFalse($actualResult);
184 }
185
186 /**
187 * @test
188 */
189 public function getTargetPidByPluginSignatureReturnsNullIfConfigurationManagerIsNotInitialized() {
190 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(NULL));
191 $this->assertNull($this->extensionService->getTargetPidByPlugin('ExtensionName', 'PluginName'));
192 }
193
194 /**
195 * @test
196 */
197 public function getTargetPidByPluginSignatureReturnsNullIfDefaultPidIsZero() {
198 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 0))));
199 $this->assertNull($this->extensionService->getTargetPidByPlugin('ExtensionName', 'PluginName'));
200 }
201
202 /**
203 * @test
204 */
205 public function getTargetPidByPluginSignatureReturnsTheConfiguredDefaultPid() {
206 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 123))));
207 $expectedResult = 123;
208 $actualResult = $this->extensionService->getTargetPidByPlugin('ExtensionName', 'SomePlugin');
209 $this->assertEquals($expectedResult, $actualResult);
210 }
211
212 /**
213 * @test
214 */
215 public function getTargetPidByPluginSignatureDeterminesTheTargetPidIfDefaultPidIsAuto() {
216 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
217 $pluginSignature = 'extensionname_someplugin';
218 $GLOBALS['TSFE']->sys_page = $this->getMock('TYPO3\\CMS\\Frontend\\Page\\PageRepository', array('enableFields'));
219 $GLOBALS['TSFE']->sys_page->expects($this->once())->method('enableFields')->with('tt_content')->will($this->returnValue(' AND enable_fields'));
220 $GLOBALS['TYPO3_DB']->expects($this->once())->method('fullQuoteStr')->with($pluginSignature, 'tt_content')->will($this->returnValue('"pluginSignature"'));
221 $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'))));
222 $expectedResult = 321;
223 $actualResult = $this->extensionService->getTargetPidByPlugin('ExtensionName', 'SomePlugin');
224 $this->assertEquals($expectedResult, $actualResult);
225 }
226
227 /**
228 * @test
229 */
230 public function getTargetPidByPluginSignatureReturnsNullIfTargetPidCouldNotBeDetermined() {
231 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
232 $GLOBALS['TSFE']->sys_page = $this->getMock('TYPO3\\CMS\\Frontend\\Page\\PageRepository', array('enableFields'));
233 $GLOBALS['TSFE']->sys_page->expects($this->once())->method('enableFields')->will($this->returnValue(' AND enable_fields'));
234 $GLOBALS['TYPO3_DB']->expects($this->once())->method('fullQuoteStr')->will($this->returnValue('"pluginSignature"'));
235 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnValue(array()));
236 $this->assertNull($this->extensionService->getTargetPidByPlugin('ExtensionName', 'SomePlugin'));
237 }
238
239 /**
240 * @test
241 * @expectedException \TYPO3\CMS\Extbase\Exception
242 */
243 public function getTargetPidByPluginSignatureThrowsExceptionIfMoreThanOneTargetPidsWereFound() {
244 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
245 $GLOBALS['TSFE']->sys_page = $this->getMock('TYPO3\\CMS\\Frontend\\Page\\PageRepository', array('enableFields'));
246 $GLOBALS['TSFE']->sys_page->expects($this->once())->method('enableFields')->will($this->returnValue(' AND enable_fields'));
247 $GLOBALS['TYPO3_DB']->expects($this->once())->method('fullQuoteStr')->will($this->returnValue('"pluginSignature"'));
248 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnValue(array(array('pid' => 123), array('pid' => 124))));
249 $this->extensionService->getTargetPidByPlugin('ExtensionName', 'SomePlugin');
250 }
251
252 /**
253 * @test
254 */
255 public function getDefaultControllerNameByPluginReturnsNullIfGivenExtensionCantBeFound() {
256 $this->assertNull($this->extensionService->getDefaultControllerNameByPlugin('NonExistingExtensionName', 'SomePlugin'));
257 }
258
259 /**
260 * @test
261 */
262 public function getDefaultControllerNameByPluginReturnsNullIfGivenPluginCantBeFound() {
263 $this->assertNull($this->extensionService->getDefaultControllerNameByPlugin('ExtensionName', 'NonExistingPlugin'));
264 }
265
266 /**
267 * @test
268 */
269 public function getDefaultControllerNameByPluginReturnsFirstControllerNameOfGivenPlugin() {
270 $expectedResult = 'ControllerName';
271 $actualResult = $this->extensionService->getDefaultControllerNameByPlugin('ExtensionName', 'SomePlugin');
272 $this->assertEquals($expectedResult, $actualResult);
273 }
274
275 /**
276 * @test
277 */
278 public function getDefaultActionNameByPluginAndControllerReturnsNullIfGivenExtensionCantBeFound() {
279 $this->assertNull($this->extensionService->getDefaultActionNameByPluginAndController('NonExistingExtensionName', 'SomePlugin', 'ControllerName'));
280 }
281
282 /**
283 * @test
284 */
285 public function getDefaultActionNameByPluginAndControllerReturnsNullIfGivenPluginCantBeFound() {
286 $this->assertNull($this->extensionService->getDefaultActionNameByPluginAndController('ExtensionName', 'NonExistingPlugin', 'ControllerName'));
287 }
288
289 /**
290 * @test
291 */
292 public function getDefaultActionNameByPluginAndControllerReturnsNullIfGivenControllerCantBeFound() {
293 $this->assertNull($this->extensionService->getDefaultActionNameByPluginAndController('ExtensionName', 'SomePlugin', 'NonExistingControllerName'));
294 }
295
296 /**
297 * @test
298 */
299 public function getDefaultActionNameByPluginAndControllerReturnsFirstActionNameOfGivenController() {
300 $expectedResult = 'someAction';
301 $actualResult = $this->extensionService->getDefaultActionNameByPluginAndController('SomeOtherExtensionName', 'SecondPlugin', 'SecondControllerName');
302 $this->assertEquals($expectedResult, $actualResult);
303 }
304 }