‪TYPO3CMS  10.4
ExtensionServiceTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Prophecy\Argument;
21 use Prophecy\Prophecy\ObjectProphecy;
32 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
33 
37 class ‪ExtensionServiceTest extends UnitTestCase
38 {
43 
47  protected ‪$extensionService;
48 
53  protected ‪$resetSingletonInstances = true;
54 
55  protected function ‪setUp(): void
56  {
57  parent::setUp();
58  ‪$GLOBALS['TSFE'] = new \stdClass();
59  $this->extensionService = new ‪ExtensionService();
60  $this->mockConfigurationManager = $this->createMock(ConfigurationManagerInterface::class);
61  $this->extensionService->injectConfigurationManager($this->mockConfigurationManager);
62  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'] = [
63  'ExtensionName' => [
64  'plugins' => [
65  'SomePlugin' => [
66  'controllers' => [
67  'Fully\\Qualified\\ControllerName' => [
68  'alias' => 'ControllerName',
69  'actions' => ['index', 'otherAction']
70  ]
71  ]
72  ],
73  'ThirdPlugin' => [
74  'controllers' => [
75  'Fully\\Qualified\\ControllerName' => [
76  'alias' => 'ControllerName',
77  'actions' => ['otherAction', 'thirdAction']
78  ]
79  ]
80  ]
81  ]
82  ],
83  'SomeOtherExtensionName' => [
84  'plugins' => [
85  'SecondPlugin' => [
86  'controllers' => [
87  'Fully\\Qualified\\ControllerName' => [
88  'alias' => 'ControllerName',
89  'actions' => ['index', 'otherAction']
90  ],
91  'Fully\\Qualified\\SecondControllerName' => [
92  'actions' => ['someAction', 'someOtherAction'],
93  'alias' => 'SecondControllerName',
94  'nonCacheableActions' => ['someOtherAction']
95  ]
96  ]
97  ]
98  ]
99  ]
100  ];
101  }
102 
109  protected function ‪getMockDatabaseConnection(): ObjectProphecy
110  {
111  $connection = $this->prophesize(Connection::class);
112  $connection->getDatabasePlatform()->willReturn(new ‪MockPlatform());
113  $connection->getExpressionBuilder()->willReturn(new ‪ExpressionBuilder($connection->reveal()));
114  $connection->quoteIdentifier(Argument::cetera())->willReturnArgument(0);
115 
116  $queryBuilder = new ‪QueryBuilder(
117  $connection->reveal(),
118  null,
119  new \Doctrine\DBAL\Query\‪QueryBuilder($connection->reveal())
120  );
121 
122  $connectionPool = $this->prophesize(ConnectionPool::class);
123  $connectionPool->getQueryBuilderForTable('tt_content')->willReturn($queryBuilder);
124  GeneralUtility::addInstance(ConnectionPool::class, $connectionPool->reveal());
125 
126  return $connection;
127  }
128 
134  public function ‪getPluginNamespaceDataProvider()
135  {
136  return [
137  [null, null, 'tx__'],
138  ['', '', 'tx__'],
139  ['SomeExtension', 'SomePlugin', 'tx_someextension_someplugin'],
140  ['NonExistingExtension', 'SomePlugin', 'tx_nonexistingextension_someplugin'],
141  ['Invalid', '', 'tx_invalid_']
142  ];
143  }
144 
152  public function ‪getPluginNamespaceTests($extensionName, $pluginName, $expectedResult)
153  {
154  $this->mockConfigurationManager->expects(self::once())->method('getConfiguration')->willReturn([]);
155  $actualResult = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
156  self::assertEquals($expectedResult, $actualResult, 'Failing for extension: "' . $extensionName . '", plugin: "' . $pluginName . '"');
157  }
158 
162  public function ‪pluginNamespaceCanBeOverridden()
163  {
164  $this->mockConfigurationManager->expects(self::once())->method('getConfiguration')->with(‪ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, 'SomeExtension', 'SomePlugin')->willReturn(['view' => ['pluginNamespace' => 'overridden_plugin_namespace']]);
165  $expectedResult = 'overridden_plugin_namespace';
166  $actualResult = $this->extensionService->getPluginNamespace('SomeExtension', 'SomePlugin');
167  self::assertEquals($expectedResult, $actualResult);
168  }
169 
175  public function ‪getPluginNameByActionDataProvider()
176  {
177  return [
178  ['ExtensionName', 'ControllerName', 'someNonExistingAction', null],
179  ['ExtensionName', 'ControllerName', 'index', 'SomePlugin'],
180  ['ExtensionName', 'ControllerName', 'thirdAction', 'ThirdPlugin'],
181  ['eXtEnSiOnNaMe', 'cOnTrOlLeRnAmE', 'thirdAction', null],
182  ['eXtEnSiOnNaMe', 'cOnTrOlLeRnAmE', 'ThIrDaCtIoN', null],
183  ['SomeOtherExtensionName', 'ControllerName', 'otherAction', 'SecondPlugin']
184  ];
185  }
186 
195  public function ‪getPluginNameByActionTests($extensionName, $controllerName, $actionName, $expectedResult)
196  {
197  $this->mockConfigurationManager->expects(self::once())->method('getConfiguration')->with(‪ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK)->willReturn(['view' => ['pluginNamespace' => 'overridden_plugin_namespace']]);
198  $actualResult = $this->extensionService->getPluginNameByAction($extensionName, $controllerName, $actionName);
199  self::assertEquals($expectedResult, $actualResult, 'Failing for $extensionName: "' . $extensionName . '", $controllerName: "' . $controllerName . '", $actionName: "' . $actionName . '" - ');
200  }
201 
206  {
207  $this->expectException(Exception::class);
208  $this->expectExceptionCode(1280825466);
209  $this->mockConfigurationManager->expects(self::once())->method('getConfiguration')->with(‪ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK)->willReturn(['view' => ['pluginNamespace' => 'overridden_plugin_namespace']]);
210  $this->extensionService->getPluginNameByAction('ExtensionName', 'ControllerName', 'otherAction');
211  }
212 
217  {
218  $frameworkConfiguration = [
219  'extensionName' => 'CurrentExtension',
220  'pluginName' => 'CurrentPlugin',
221  'controllerConfiguration' => [
222  'Fully\\Qualified\\ControllerName' => [
223  'alias' => 'ControllerName',
224  'actions' => ['otherAction']
225  ]
226  ]
227  ];
228 
229  $this->mockConfigurationManager
230  ->expects(self::once())
231  ->method('getConfiguration')
233  ->willReturn($frameworkConfiguration);
234 
235  $actualResult = $this->extensionService->getPluginNameByAction('CurrentExtension', 'ControllerName', 'otherAction');
236  $expectedResult = 'CurrentPlugin';
237  self::assertEquals($expectedResult, $actualResult);
238  }
239 
244  {
245  $this->mockConfigurationManager->expects(self::once())->method('getConfiguration')->willReturn([]);
246  self::assertNull($this->extensionService->getTargetPidByPlugin('ExtensionName', 'PluginName'));
247  }
248 
253  {
254  $this->mockConfigurationManager->expects(self::once())->method('getConfiguration')->willReturn(['view' => ['defaultPid' => 0]]);
255  self::assertNull($this->extensionService->getTargetPidByPlugin('ExtensionName', 'PluginName'));
256  }
257 
262  {
263  $this->mockConfigurationManager->expects(self::once())->method('getConfiguration')->willReturn(['view' => ['defaultPid' => 123]]);
264  $expectedResult = 123;
265  $actualResult = $this->extensionService->getTargetPidByPlugin('ExtensionName', 'SomePlugin');
266  self::assertEquals($expectedResult, $actualResult);
267  }
268 
273  {
274  self::assertNull($this->extensionService->getDefaultControllerNameByPlugin('NonExistingExtensionName', 'SomePlugin'));
275  }
276 
281  {
282  self::assertNull($this->extensionService->getDefaultControllerNameByPlugin('ExtensionName', 'NonExistingPlugin'));
283  }
284 
289  {
290  $expectedResult = 'ControllerName';
291  $actualResult = $this->extensionService->getDefaultControllerNameByPlugin('ExtensionName', 'SomePlugin');
292  self::assertEquals($expectedResult, $actualResult);
293  }
294 
299  {
300  self::assertNull($this->extensionService->getDefaultActionNameByPluginAndController('NonExistingExtensionName', 'SomePlugin', 'ControllerName'));
301  }
302 
307  {
308  self::assertNull($this->extensionService->getDefaultActionNameByPluginAndController('ExtensionName', 'NonExistingPlugin', 'ControllerName'));
309  }
310 
315  {
316  self::assertNull($this->extensionService->getDefaultActionNameByPluginAndController('ExtensionName', 'SomePlugin', 'NonExistingControllerName'));
317  }
318 
323  {
324  $expectedResult = 'someAction';
325  $actualResult = $this->extensionService->getDefaultActionNameByPluginAndController('SomeOtherExtensionName', 'SecondPlugin', 'SecondControllerName');
326  self::assertEquals($expectedResult, $actualResult);
327  }
328 
333  {
334  $configurationManagerProphecy = $this->prophesize(ConfigurationManager::class);
335  $configurationManagerProphecy->getConfiguration(
337  'extension'
338  )->willReturn([]);
339  $this->extensionService->injectConfigurationManager($configurationManagerProphecy->reveal());
340 
341  $result = $this->extensionService->getTargetPageTypeByFormat('extension', 'json');
342 
343  self::assertSame(0, $result);
344  }
345 
350  {
351  $configurationManagerProphecy = $this->prophesize(ConfigurationManager::class);
352  $configurationManagerProphecy->getConfiguration(
354  'extension'
355  )->willReturn([
356  'view' => [
357  'formatToPageTypeMapping' => [
358  'json' => 111
359  ]
360  ]
361  ]);
362  $this->extensionService->injectConfigurationManager($configurationManagerProphecy->reveal());
363 
364  $result = $this->extensionService->getTargetPageTypeByFormat('extension', 'json');
365 
366  self::assertSame(111, $result);
367  }
368 }
‪TYPO3\CMS\Extbase\Tests\Unit\Service
Definition: CacheServiceTest.php:16
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getTargetPageTypeByFormatReturnsMappedPageTypeFromConfiguration
‪getTargetPageTypeByFormatReturnsMappedPageTypeFromConfiguration()
Definition: ExtensionServiceTest.php:346
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getDefaultActionNameByPluginAndControllerReturnsNullIfGivenExtensionCantBeFound
‪getDefaultActionNameByPluginAndControllerReturnsNullIfGivenExtensionCantBeFound()
Definition: ExtensionServiceTest.php:295
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getTargetPidByPluginSignatureReturnsTheConfiguredDefaultPid
‪getTargetPidByPluginSignatureReturnsTheConfiguredDefaultPid()
Definition: ExtensionServiceTest.php:258
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder
Definition: ExpressionBuilder.php:35
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getMockDatabaseConnection
‪ObjectProphecy getMockDatabaseConnection()
Definition: ExtensionServiceTest.php:106
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getPluginNameByActionDataProvider
‪array getPluginNameByActionDataProvider()
Definition: ExtensionServiceTest.php:172
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest
Definition: ExtensionServiceTest.php:38
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getDefaultActionNameByPluginAndControllerReturnsNullIfGivenControllerCantBeFound
‪getDefaultActionNameByPluginAndControllerReturnsNullIfGivenControllerCantBeFound()
Definition: ExtensionServiceTest.php:311
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:28
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getDefaultActionNameByPluginAndControllerReturnsNullIfGivenPluginCantBeFound
‪getDefaultActionNameByPluginAndControllerReturnsNullIfGivenPluginCantBeFound()
Definition: ExtensionServiceTest.php:303
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_FRAMEWORK
‪const CONFIGURATION_TYPE_FRAMEWORK
Definition: ConfigurationManagerInterface.php:29
‪TYPO3\CMS\Core\Database\Query\QueryBuilder
Definition: QueryBuilder.php:52
‪TYPO3\CMS\Extbase\Exception
Definition: Exception.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\$mockConfigurationManager
‪TYPO3 CMS Extbase Configuration ConfigurationManagerInterface $mockConfigurationManager
Definition: ExtensionServiceTest.php:41
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManager
Definition: ConfigurationManager.php:33
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getPluginNamespaceTests
‪getPluginNamespaceTests($extensionName, $pluginName, $expectedResult)
Definition: ExtensionServiceTest.php:149
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getPluginNamespaceDataProvider
‪array getPluginNamespaceDataProvider()
Definition: ExtensionServiceTest.php:131
‪TYPO3\CMS\Core\Tests\Unit\Database\Mocks\MockPlatform
Definition: MockPlatform.php:22
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getDefaultControllerNameByPluginReturnsNullIfGivenExtensionCantBeFound
‪getDefaultControllerNameByPluginReturnsNullIfGivenExtensionCantBeFound()
Definition: ExtensionServiceTest.php:269
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getDefaultActionNameByPluginAndControllerReturnsFirstActionNameOfGivenController
‪getDefaultActionNameByPluginAndControllerReturnsFirstActionNameOfGivenController()
Definition: ExtensionServiceTest.php:319
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getDefaultControllerNameByPluginReturnsFirstControllerNameOfGivenPlugin
‪getDefaultControllerNameByPluginReturnsFirstControllerNameOfGivenPlugin()
Definition: ExtensionServiceTest.php:285
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\setUp
‪setUp()
Definition: ExtensionServiceTest.php:52
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\pluginNamespaceCanBeOverridden
‪pluginNamespaceCanBeOverridden()
Definition: ExtensionServiceTest.php:159
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getTargetPidByPluginSignatureReturnsNullIfConfigurationManagerIsNotInitialized
‪getTargetPidByPluginSignatureReturnsNullIfConfigurationManagerIsNotInitialized()
Definition: ExtensionServiceTest.php:240
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getPluginNameByActionReturnsCurrentIfItCanHandleTheActionEvenIfMoreThanOnePluginMatches
‪getPluginNameByActionReturnsCurrentIfItCanHandleTheActionEvenIfMoreThanOnePluginMatches()
Definition: ExtensionServiceTest.php:213
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getPluginNameByActionTests
‪getPluginNameByActionTests($extensionName, $controllerName, $actionName, $expectedResult)
Definition: ExtensionServiceTest.php:192
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getTargetPidByPluginSignatureReturnsNullIfDefaultPidIsZero
‪getTargetPidByPluginSignatureReturnsNullIfDefaultPidIsZero()
Definition: ExtensionServiceTest.php:249
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:36
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getDefaultControllerNameByPluginReturnsNullIfGivenPluginCantBeFound
‪getDefaultControllerNameByPluginReturnsNullIfGivenPluginCantBeFound()
Definition: ExtensionServiceTest.php:277
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Service\ExtensionService
Definition: ExtensionService.php:33
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getTargetPageTypeByFormatReturnsZeroIfNoMappingIsSet
‪getTargetPageTypeByFormatReturnsZeroIfNoMappingIsSet()
Definition: ExtensionServiceTest.php:329
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\$extensionService
‪TYPO3 CMS Extbase Service ExtensionService $extensionService
Definition: ExtensionServiceTest.php:45
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: ExtensionServiceTest.php:50
‪TYPO3\CMS\Extbase\Tests\Unit\Service\ExtensionServiceTest\getPluginNameByActionThrowsExceptionIfMoreThanOnePluginMatches
‪getPluginNameByActionThrowsExceptionIfMoreThanOnePluginMatches()
Definition: ExtensionServiceTest.php:202