Skip to content

Commit

Permalink
[TASK] Have more DI in FlexFormTools
Browse files Browse the repository at this point in the history
FlexFormTools is a stateless service since #102229.
The - also stateless - TcaMigration dependency is
now injected. Get the service injected more often
and adapt a few tests to deal with this.

Resolves: #104268
Related: #102229
Releases: main
Change-Id: I4cca73dc90ec652a4963b56c9a6ce5e2fccccf17
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/85026
Tested-by: Andreas Nedbal <andy@pixelde.su>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Andreas Nedbal <andy@pixelde.su>
Tested-by: Stefan Bürk <stefan@buerk.tech>
  • Loading branch information
lolli42 committed Jun 30, 2024
1 parent 1db5339 commit b0420cc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 38 deletions.
6 changes: 3 additions & 3 deletions Classes/Configuration/FlexForm/FlexFormTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
{
public function __construct(
private EventDispatcherInterface $eventDispatcher,
private TcaMigration $tcaMigration,
) {}

/**
Expand Down Expand Up @@ -258,7 +259,6 @@ protected function migrateFlexFormTcaRecursive(array $structure): array
foreach ($structure as $key => $value) {
if ($key === 'el' && is_array($value)) {
$newSubStructure = [];
$tcaMigration = GeneralUtility::makeInstance(TcaMigration::class);
foreach ($value as $subKey => $subValue) {
// On-the-fly migration for flex form "TCA". Call the TcaMigration and log any deprecations.
$dummyTca = [
Expand All @@ -268,8 +268,8 @@ protected function migrateFlexFormTcaRecursive(array $structure): array
],
],
];
$migratedTca = $tcaMigration->migrate($dummyTca);
$messages = $tcaMigration->getMessages();
$migratedTca = $this->tcaMigration->migrate($dummyTca);
$messages = $this->tcaMigration->getMessages();
if (!empty($messages)) {
$context = 'FlexFormTools did an on-the-fly migration of a flex form data structure. This is deprecated and will be removed.'
. ' Merge the following changes into the flex form definition "' . $subKey . '":';
Expand Down
8 changes: 2 additions & 6 deletions Classes/Configuration/Tca/TcaPreparation.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ class TcaPreparation
* For flex form TCA, this class is called dynamically if opening a record in the backend.
*
* See unit tests for details.
*
* @param array $tca
*/
public function prepare(array $tca): array
{
Expand All @@ -56,13 +54,11 @@ public function prepare(array $tca): array
* This also sets necessary MM properties, in case relationship is
* set to "manyToMany" (which is the default).
*
* Finally all category fields with a "manyToMany" relationship are
* Finally, all category fields with a "manyToMany" relationship are
* added to the MM_oppositeUsage of sys_category "items".
*
* Important: Since this method defines a "foreign_table_where", this
* must always be executed before prepareQuotingOfTableNamesAndColumnNames().
*
* @param array $tca
*/
protected function configureCategoryRelations(array $tca): array
{
Expand Down Expand Up @@ -104,7 +100,7 @@ protected function configureCategoryRelations(array $tca): array
if ($fieldConfig['config']['relationship'] === 'oneToOne') {
// In case relationship is set to "oneToOne", the database column for this
// field will be an integer column. This means, only one uid can be stored.
// Therefore maxitems must be 1.
// Therefore, maxitems must be 1.
if ((int)($fieldConfig['config']['maxitems'] ?? 0) > 1) {
throw new \RuntimeException(
$fieldName . ' of table ' . $table . ' is defined as type category with an oneToOne relationship. ' .
Expand Down
12 changes: 6 additions & 6 deletions Tests/Unit/DataHandling/DataHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected function setUp(): void
new PagePermissionAssembler(),
$this->tcaSchemaFactory,
new PageDoktypeRegistry($this->tcaSchemaFactory),
new FlexFormTools(new NoopEventDispatcher()),
$this->createMock(FlexFormTools::class),
new PasswordHashFactory(),
new Random(),
new TypoLinkCodecService(new NoopEventDispatcher()),
Expand Down Expand Up @@ -244,7 +244,7 @@ public function checkValuePasswordWithSaltedPasswordDispatchesEvent(): void
new PagePermissionAssembler(),
$this->tcaSchemaFactory,
new PageDoktypeRegistry($this->tcaSchemaFactory),
new FlexFormTools(new NoopEventDispatcher()),
$this->createMock(FlexFormTools::class),
new PasswordHashFactory(),
new Random(),
new TypoLinkCodecService(new NoopEventDispatcher()),
Expand Down Expand Up @@ -669,7 +669,7 @@ public function processDatamapForFrozenNonZeroWorkspaceReturnsFalse(): void
new PagePermissionAssembler(),
$this->tcaSchemaFactory,
new PageDoktypeRegistry($this->tcaSchemaFactory),
new FlexFormTools(new NoopEventDispatcher()),
$this->createMock(FlexFormTools::class),
new PasswordHashFactory(),
new Random(),
new TypoLinkCodecService(new NoopEventDispatcher()),
Expand Down Expand Up @@ -831,7 +831,7 @@ public function logFormatsDetailMessageWithAdditionalDataInLocalErrorArray(): vo
new PagePermissionAssembler(),
$this->tcaSchemaFactory,
new PageDoktypeRegistry($this->tcaSchemaFactory),
new FlexFormTools(new NoopEventDispatcher()),
$this->createMock(FlexFormTools::class),
new PasswordHashFactory(),
new Random(),
new TypoLinkCodecService(new NoopEventDispatcher()),
Expand All @@ -857,7 +857,7 @@ public function logFormatsDetailMessageWithPlaceholders(): void
new PagePermissionAssembler(),
$this->tcaSchemaFactory,
new PageDoktypeRegistry($this->tcaSchemaFactory),
new FlexFormTools(new NoopEventDispatcher()),
$this->createMock(FlexFormTools::class),
new PasswordHashFactory(),
new Random(),
new TypoLinkCodecService(new NoopEventDispatcher()),
Expand Down Expand Up @@ -1308,7 +1308,7 @@ public function clearPrefixFromValueRemovesPrefix(string $input, string $expecte
new PagePermissionAssembler(),
$this->tcaSchemaFactory,
new PageDoktypeRegistry($this->tcaSchemaFactory),
new FlexFormTools(new NoopEventDispatcher()),
$this->createMock(FlexFormTools::class),
new PasswordHashFactory(),
new Random(),
new TypoLinkCodecService(new NoopEventDispatcher()),
Expand Down
39 changes: 16 additions & 23 deletions Tests/Unit/Hooks/TcaItemsProcessorFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Backend\Module\ModuleFactory;
use TYPO3\CMS\Backend\Module\ModuleProvider;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
use TYPO3\CMS\Core\Configuration\Tca\TcaMigration;
use TYPO3\CMS\Core\EventDispatcher\NoopEventDispatcher;
use TYPO3\CMS\Core\Hooks\TcaItemsProcessorFunctions;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Imaging\IconRegistry;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Tests\Unit\Fixtures\EventDispatcher\MockEventDispatcher;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

Expand Down Expand Up @@ -187,24 +186,6 @@ public function populateAvailableUserModulesTest(): void
self::assertSame($expected, $fieldDefinition);
}

#[DataProvider('populateExcludeFieldsTestDataProvider')]
#[Test]
public function populateExcludeFieldsTest(array $tca, array $expectedItems): void
{
$GLOBALS['TCA'] = $tca;
$fieldDefinition = ['items' => []];
$expected = [
'items' => $expectedItems,
];

$eventDispatcher = new MockEventDispatcher();
GeneralUtility::addInstance(EventDispatcherInterface::class, $eventDispatcher);
GeneralUtility::addInstance(FlexFormTools::class, new FlexFormTools($eventDispatcher));

(new TcaItemsProcessorFunctions())->populateExcludeFields($fieldDefinition);
self::assertSame($expected, $fieldDefinition);
}

public static function populateExcludeFieldsTestDataProvider(): array
{
return [
Expand Down Expand Up @@ -316,6 +297,20 @@ public static function populateExcludeFieldsTestDataProvider(): array
];
}

#[DataProvider('populateExcludeFieldsTestDataProvider')]
#[Test]
public function populateExcludeFieldsTest(array $tca, array $expectedItems): void
{
$GLOBALS['TCA'] = $tca;
$fieldDefinition = ['items' => []];
$expected = [
'items' => $expectedItems,
];
GeneralUtility::addInstance(FlexFormTools::class, new FlexFormTools(new NoopEventDispatcher(), new TcaMigration()));
(new TcaItemsProcessorFunctions())->populateExcludeFields($fieldDefinition);
self::assertSame($expected, $fieldDefinition);
}

#[Test]
public function populateExcludeFieldsWithFlexFormTest(): void
{
Expand Down Expand Up @@ -376,9 +371,7 @@ public function populateExcludeFieldsWithFlexFormTest(): void
$cacheManagerMock->method('getCache')->with('runtime')->willReturn($cacheMock);
GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerMock);

$eventDispatcher = new MockEventDispatcher();
GeneralUtility::addInstance(EventDispatcherInterface::class, $eventDispatcher);
GeneralUtility::addInstance(FlexFormTools::class, new FlexFormTools($eventDispatcher));
GeneralUtility::addInstance(FlexFormTools::class, new FlexFormTools(new NoopEventDispatcher(), new TcaMigration()));

(new TcaItemsProcessorFunctions())->populateExcludeFields($fieldDefinition);
self::assertSame($expected, $fieldDefinition);
Expand Down

0 comments on commit b0420cc

Please sign in to comment.