73fc21464bbf6aef87dcc374f8c0825d14eeb754
[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 use TYPO3\CMS\Extbase\Exception;
17
18 /**
19 * Test case
20 */
21 class ExtensionServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
22 {
23 /**
24 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
25 */
26 protected $mockConfigurationManager;
27
28 /**
29 * @var \TYPO3\CMS\Extbase\Service\ExtensionService
30 */
31 protected $extensionService;
32
33 protected function setUp()
34 {
35 $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array('fullQuoteStr', 'exec_SELECTgetRows'));
36 $GLOBALS['TSFE'] = new \stdClass();
37 $this->extensionService = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Service\ExtensionService::class, array('dummy'));
38 $this->mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
39 $this->extensionService->_set('configurationManager', $this->mockConfigurationManager);
40 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'] = array(
41 'ExtensionName' => array(
42 'plugins' => array(
43 'SomePlugin' => array(
44 'controllers' => array(
45 'ControllerName' => array(
46 'actions' => array('index', 'otherAction')
47 )
48 )
49 ),
50 'ThirdPlugin' => array(
51 'controllers' => array(
52 'ControllerName' => array(
53 'actions' => array('otherAction', 'thirdAction')
54 )
55 )
56 )
57 )
58 ),
59 'SomeOtherExtensionName' => array(
60 'plugins' => array(
61 'SecondPlugin' => array(
62 'controllers' => array(
63 'ControllerName' => array(
64 'actions' => array('index', 'otherAction')
65 ),
66 'SecondControllerName' => array(
67 'actions' => array('someAction', 'someOtherAction'),
68 'nonCacheableActions' => array('someOtherAction')
69 )
70 )
71 )
72 )
73 )
74 );
75 }
76
77 /**
78 * DataProvider for getPluginNamespaceByPluginSignatureTests()
79 *
80 * @return array
81 */
82 public function getPluginNamespaceDataProvider()
83 {
84 return array(
85 array('SomeExtension', 'SomePlugin', 'tx_someextension_someplugin'),
86 array('NonExistingExtension', 'SomePlugin', 'tx_nonexistingextension_someplugin'),
87 array('Invalid', '', 'tx_invalid_')
88 );
89 }
90
91 /**
92 * @test
93 * @dataProvider getPluginNamespaceDataProvider
94 * @param string $extensionName
95 * @param string $pluginName
96 * @param mixed $expectedResult
97 */
98 public function getPluginNamespaceTests($extensionName, $pluginName, $expectedResult)
99 {
100 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array()));
101 $actualResult = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
102 $this->assertEquals($expectedResult, $actualResult, 'Failing for extension: "' . $extensionName . '", plugin: "' . $pluginName . '"');
103 }
104
105 /**
106 * @test
107 */
108 public function pluginNamespaceCanBeOverridden()
109 {
110 $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'))));
111 $expectedResult = 'overridden_plugin_namespace';
112 $actualResult = $this->extensionService->getPluginNamespace('SomeExtension', 'SomePlugin');
113 $this->assertEquals($expectedResult, $actualResult);
114 }
115
116 /**
117 * DataProvider for getPluginNameByActionTests()
118 *
119 * @return array
120 */
121 public function getPluginNameByActionDataProvider()
122 {
123 return array(
124 array('ExtensionName', 'ControllerName', 'someNonExistingAction', null),
125 array('ExtensionName', 'ControllerName', 'index', 'SomePlugin'),
126 array('ExtensionName', 'ControllerName', 'thirdAction', 'ThirdPlugin'),
127 array('eXtEnSiOnNaMe', 'cOnTrOlLeRnAmE', 'thirdAction', null),
128 array('eXtEnSiOnNaMe', 'cOnTrOlLeRnAmE', 'ThIrDaCtIoN', null),
129 array('SomeOtherExtensionName', 'ControllerName', 'otherAction', 'SecondPlugin')
130 );
131 }
132
133 /**
134 * @test
135 * @dataProvider getPluginNameByActionDataProvider
136 * @param string $extensionName
137 * @param string $controllerName
138 * @param string $actionName
139 * @param mixed $expectedResult
140 */
141 public function getPluginNameByActionTests($extensionName, $controllerName, $actionName, $expectedResult)
142 {
143 $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'))));
144 $actualResult = $this->extensionService->getPluginNameByAction($extensionName, $controllerName, $actionName);
145 $this->assertEquals($expectedResult, $actualResult, 'Failing for $extensionName: "' . $extensionName . '", $controllerName: "' . $controllerName . '", $actionName: "' . $actionName . '" - ');
146 }
147
148 /**
149 * @test
150 */
151 public function getPluginNameByActionThrowsExceptionIfMoreThanOnePluginMatches()
152 {
153 $this->expectException(Exception::class);
154 $this->expectExceptionCode(1280825466);
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 $this->extensionService->getPluginNameByAction('ExtensionName', 'ControllerName', 'otherAction');
157 }
158
159 /**
160 * @test
161 */
162 public function getPluginNameByActionReturnsCurrentIfItCanHandleTheActionEvenIfMoreThanOnePluginMatches()
163 {
164 $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'))))));
165 $actualResult = $this->extensionService->getPluginNameByAction('CurrentExtension', 'ControllerName', 'otherAction');
166 $expectedResult = 'CurrentPlugin';
167 $this->assertEquals($expectedResult, $actualResult);
168 }
169
170 /**
171 * @test
172 */
173 public function isActionCacheableReturnsTrueByDefault()
174 {
175 $mockConfiguration = array();
176 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($mockConfiguration));
177 $actualResult = $this->extensionService->isActionCacheable('SomeExtension', 'SomePlugin', 'SomeController', 'someAction');
178 $this->assertTrue($actualResult);
179 }
180
181 /**
182 * @test
183 */
184 public function isActionCacheableReturnsFalseIfActionIsNotCacheable()
185 {
186 $mockConfiguration = array(
187 'controllerConfiguration' => array(
188 'SomeController' => array(
189 'nonCacheableActions' => array('someAction')
190 )
191 )
192 );
193 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($mockConfiguration));
194 $actualResult = $this->extensionService->isActionCacheable('SomeExtension', 'SomePlugin', 'SomeController', 'someAction');
195 $this->assertFalse($actualResult);
196 }
197
198 /**
199 * @test
200 */
201 public function getTargetPidByPluginSignatureReturnsNullIfConfigurationManagerIsNotInitialized()
202 {
203 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(null));
204 $this->assertNull($this->extensionService->getTargetPidByPlugin('ExtensionName', 'PluginName'));
205 }
206
207 /**
208 * @test
209 */
210 public function getTargetPidByPluginSignatureReturnsNullIfDefaultPidIsZero()
211 {
212 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 0))));
213 $this->assertNull($this->extensionService->getTargetPidByPlugin('ExtensionName', 'PluginName'));
214 }
215
216 /**
217 * @test
218 */
219 public function getTargetPidByPluginSignatureReturnsTheConfiguredDefaultPid()
220 {
221 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 123))));
222 $expectedResult = 123;
223 $actualResult = $this->extensionService->getTargetPidByPlugin('ExtensionName', 'SomePlugin');
224 $this->assertEquals($expectedResult, $actualResult);
225 }
226
227 /**
228 * @test
229 */
230 public function getTargetPidByPluginSignatureDeterminesTheTargetPidIfDefaultPidIsAuto()
231 {
232 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
233 $pluginSignature = 'extensionname_someplugin';
234 $GLOBALS['TSFE']->sys_page = $this->getMock(\TYPO3\CMS\Frontend\Page\PageRepository::class, array('enableFields'));
235 $GLOBALS['TSFE']->sys_page->expects($this->once())->method('enableFields')->with('tt_content')->will($this->returnValue(' AND enable_fields'));
236 $GLOBALS['TYPO3_DB']->expects($this->once())->method('fullQuoteStr')->with($pluginSignature, 'tt_content')->will($this->returnValue('"pluginSignature"'));
237 $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'))));
238 $expectedResult = 321;
239 $actualResult = $this->extensionService->getTargetPidByPlugin('ExtensionName', 'SomePlugin');
240 $this->assertEquals($expectedResult, $actualResult);
241 }
242
243 /**
244 * @test
245 */
246 public function getTargetPidByPluginSignatureReturnsNullIfTargetPidCouldNotBeDetermined()
247 {
248 $this->mockConfigurationManager->expects($this->once())->method('getConfiguration')->will($this->returnValue(array('view' => array('defaultPid' => 'auto'))));
249 $GLOBALS['TSFE']->sys_page = $this->getMock(\TYPO3\CMS\Frontend\Page\PageRepository::class, array('enableFields'));
250 $GLOBALS['TSFE']->sys_page->expects($this->once())->method('enableFields')->will($this->returnValue(' AND enable_fields'));
251 $GLOBALS['TYPO3_DB']->expects($this->once())->method('fullQuoteStr')->will($this->returnValue('"pluginSignature"'));
252 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnValue(array()));
253 $this->assertNull($this->extensionService->getTargetPidByPlugin('ExtensionName', 'SomePlugin'));
254 }
255
256 /**
257 * @test
258 */
259 public function getTargetPidByPluginSignatureThrowsExceptionIfMoreThanOneTargetPidsWereFound()
260 {
261 $this->expectException(Exception::class);
262 $this->expectExceptionCode(1280773643);
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::class, 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 {
276 $this->assertNull($this->extensionService->getDefaultControllerNameByPlugin('NonExistingExtensionName', 'SomePlugin'));
277 }
278
279 /**
280 * @test
281 */
282 public function getDefaultControllerNameByPluginReturnsNullIfGivenPluginCantBeFound()
283 {
284 $this->assertNull($this->extensionService->getDefaultControllerNameByPlugin('ExtensionName', 'NonExistingPlugin'));
285 }
286
287 /**
288 * @test
289 */
290 public function getDefaultControllerNameByPluginReturnsFirstControllerNameOfGivenPlugin()
291 {
292 $expectedResult = 'ControllerName';
293 $actualResult = $this->extensionService->getDefaultControllerNameByPlugin('ExtensionName', 'SomePlugin');
294 $this->assertEquals($expectedResult, $actualResult);
295 }
296
297 /**
298 * @test
299 */
300 public function getDefaultActionNameByPluginAndControllerReturnsNullIfGivenExtensionCantBeFound()
301 {
302 $this->assertNull($this->extensionService->getDefaultActionNameByPluginAndController('NonExistingExtensionName', 'SomePlugin', 'ControllerName'));
303 }
304
305 /**
306 * @test
307 */
308 public function getDefaultActionNameByPluginAndControllerReturnsNullIfGivenPluginCantBeFound()
309 {
310 $this->assertNull($this->extensionService->getDefaultActionNameByPluginAndController('ExtensionName', 'NonExistingPlugin', 'ControllerName'));
311 }
312
313 /**
314 * @test
315 */
316 public function getDefaultActionNameByPluginAndControllerReturnsNullIfGivenControllerCantBeFound()
317 {
318 $this->assertNull($this->extensionService->getDefaultActionNameByPluginAndController('ExtensionName', 'SomePlugin', 'NonExistingControllerName'));
319 }
320
321 /**
322 * @test
323 */
324 public function getDefaultActionNameByPluginAndControllerReturnsFirstActionNameOfGivenController()
325 {
326 $expectedResult = 'someAction';
327 $actualResult = $this->extensionService->getDefaultActionNameByPluginAndController('SomeOtherExtensionName', 'SecondPlugin', 'SecondControllerName');
328 $this->assertEquals($expectedResult, $actualResult);
329 }
330 }