Skip to content

Commit

Permalink
[TASK] Clean up config when loading definition
Browse files Browse the repository at this point in the history
Config cleanup is important, to get rid of old
configuration, which is not available anymore.
This happens of course after on-the-fly migration
is completed.

Prior to this, config cleanup was only done in the
FieldsController. The intent here was to only
present valid data to VueJS.

Impact: When saving or running the mask:convert
command, obsolete configuration is removed.

(cherry picked from commit ee8fda7)
  • Loading branch information
nhovratov committed Dec 18, 2021
1 parent 0fb7627 commit f81dec1
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Classes/Controller/FieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ protected function addFields(array $fields, string $table, string $elementKey =
if ($tableDefinition->sql->hasColumn($newField['key'])) {
$newField['sql'] = $this->tableDefinitionCollection->getTable($table)->sql->getColumn($newField['key'])->sqlDefinition;
}
$newField['tca'] = TcaConverter::convertTcaArrayToFlat($field['config'] ?? []);
$newField['tca'] = TcaConverter::convertTcaArrayToFlat($field['config'] ?? [], ['config']);
$newField['tca']['l10n_mode'] = $field['l10n_mode'] ?? '';
}

Expand Down
49 changes: 49 additions & 0 deletions Classes/Loader/ConfigCleanerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace MASK\Mask\Loader;

use MASK\Mask\Definition\TableDefinitionCollection;
use MASK\Mask\Utility\TcaConverter;

trait ConfigCleanerTrait
{
use WithConfigurationLoaderTrait;

/**
* This method removes all tca options defined which aren't available in Mask.
*/
public function cleanUpConfig(TableDefinitionCollection $tableDefinitionCollection): void
{
foreach ($tableDefinitionCollection as $tableDefinition) {
foreach ($tableDefinition->tca as $tcaFieldDefinition) {
$tabConfig = $this->configurationLoader->loadTab((string)$tcaFieldDefinition->type);
$tcaOptions = [];
foreach ($tabConfig as $options) {
foreach ($options as $row) {
$tcaOptions[] = array_keys($row);
}
}
$tcaOptions = array_merge([], ...$tcaOptions);

$tcaFieldDefinition->realTca = array_filter(TcaConverter::convertTcaArrayToFlat($tcaFieldDefinition->realTca), static function ($key) use ($tcaOptions) {
return in_array($key, $tcaOptions, true);
}, ARRAY_FILTER_USE_KEY);
}
}
}
}
13 changes: 2 additions & 11 deletions Classes/Loader/DefaultTcaCompatibilityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,13 @@

namespace MASK\Mask\Loader;

use MASK\Mask\ConfigurationLoader\ConfigurationLoaderInterface;
use MASK\Mask\Definition\TableDefinitionCollection;
use MASK\Mask\Utility\TcaConverter;
use TYPO3\CMS\Core\Utility\ArrayUtility;

trait DefaultTcaCompatibilityTrait
{
/**
* @var ConfigurationLoaderInterface
*/
protected $configurationLoader;

public function setConfigurationLoader(ConfigurationLoaderInterface $configurationLoader): void
{
$this->configurationLoader = $configurationLoader;
}
use WithConfigurationLoaderTrait;

public function addMissingDefaults(TableDefinitionCollection $tableDefinitionCollection): void
{
Expand All @@ -44,7 +35,7 @@ public function addMissingDefaults(TableDefinitionCollection $tableDefinitionCol
}
$tcaDefaults = $this->configurationLoader->loadDefaults()[(string)$tcaFieldDefinition->type];
$tcaDefaults = $tcaDefaults['tca_out'] ?? [];
$flatTcaFieldDefinition = TcaConverter::convertTcaArrayToFlat($tcaFieldDefinition->realTca, []);
$flatTcaFieldDefinition = TcaConverter::convertTcaArrayToFlat($tcaFieldDefinition->realTca);

// If the defaults are a subset of the current tca, continue.
if (array_diff_assoc($tcaDefaults, $flatTcaFieldDefinition) === []) {
Expand Down
8 changes: 8 additions & 0 deletions Classes/Loader/JsonLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace MASK\Mask\Loader;

use MASK\Mask\ConfigurationLoader\ConfigurationLoaderInterface;
use MASK\Mask\Definition\TableDefinitionCollection;
use MASK\Mask\Definition\TcaFieldDefinition;
use MASK\Mask\Enumeration\FieldType;
Expand All @@ -36,12 +37,18 @@ class JsonLoader implements LoaderInterface
protected $maskExtensionConfiguration;

use DefaultTcaCompatibilityTrait;
use ConfigCleanerTrait;

public function __construct(array $maskExtensionConfiguration)
{
$this->maskExtensionConfiguration = $maskExtensionConfiguration;
}

public function setConfigurationLoader(ConfigurationLoaderInterface $configurationLoader): void
{
$this->configurationLoader = $configurationLoader;
}

public function load(): TableDefinitionCollection
{
$maskJsonFilePath = $this->validateGetJsonFilePath();
Expand Down Expand Up @@ -73,6 +80,7 @@ public function load(): TableDefinitionCollection
}
}

$this->cleanUpConfig($this->tableDefinitionCollection);
$this->addMissingDefaults($this->tableDefinitionCollection);

return $this->tableDefinitionCollection;
Expand Down
8 changes: 8 additions & 0 deletions Classes/Loader/JsonSplitLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace MASK\Mask\Loader;

use MASK\Mask\ConfigurationLoader\ConfigurationLoaderInterface;
use MASK\Mask\Definition\ElementDefinitionCollection;
use MASK\Mask\Definition\PaletteDefinitionCollection;
use MASK\Mask\Definition\SqlDefinition;
Expand Down Expand Up @@ -46,8 +47,14 @@ class JsonSplitLoader implements LoaderInterface
'pages' => 'backend_layouts_folder'
];

use ConfigCleanerTrait;
use DefaultTcaCompatibilityTrait;

public function setConfigurationLoader(ConfigurationLoaderInterface $configurationLoader): void
{
$this->configurationLoader = $configurationLoader;
}

public function __construct(array $maskExtensionConfiguration)
{
$this->maskExtensionConfiguration = $maskExtensionConfiguration;
Expand Down Expand Up @@ -75,6 +82,7 @@ public function load(): TableDefinitionCollection
}
}

$this->cleanUpConfig($this->tableDefinitionCollection);
$this->addMissingDefaults($this->tableDefinitionCollection);

return $this->tableDefinitionCollection;
Expand Down
34 changes: 34 additions & 0 deletions Classes/Loader/WithConfigurationLoaderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/


namespace MASK\Mask\Loader;

use MASK\Mask\ConfigurationLoader\ConfigurationLoaderInterface;

trait WithConfigurationLoaderTrait
{
/**
* @var ConfigurationLoaderInterface
*/
protected $configurationLoader;

public function setConfigurationLoader(ConfigurationLoaderInterface $configurationLoader): void
{
$this->configurationLoader = $configurationLoader;
}
}
2 changes: 1 addition & 1 deletion Classes/Utility/TcaConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class TcaConverter
* 'config.renderType' => 'inputLink'
* ]
*/
public static function convertTcaArrayToFlat(array $config, array $path = ['config']): array
public static function convertTcaArrayToFlat(array $config, array $path = []): array
{
$tca = [];
foreach ($config as $key => $value) {
Expand Down
95 changes: 95 additions & 0 deletions Tests/Unit/Loader/ConfigCleanerTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace MASK\Mask\Tests\Unit\Loader;

use MASK\Mask\Definition\TableDefinitionCollection;
use MASK\Mask\Loader\ConfigCleanerTrait;
use MASK\Mask\Tests\Unit\ConfigurationLoader\FakeConfigurationLoader;
use MASK\Mask\Tests\Unit\PackageManagerTrait;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

class ConfigCleanerTraitTest extends UnitTestCase
{
protected $resetSingletonInstances = true;

use PackageManagerTrait;

/**
* @test
*/
public function cleanupConfig(): void
{
$this->registerPackageManager();

$loader = new class {
use ConfigCleanerTrait;
};

$loader->setConfigurationLoader(new FakeConfigurationLoader());

$input = [
'tt_content' => [
'elements' => [
'element1' => [
'key' => 'element1',
'label' => 'Element 1',
'labels' => [
'RTE Field',
],
'columns' => [
'tx_mask_rte',
],
],
],
'tca' => [
'tx_mask_rte' => [
'key' => 'rte',
'config' => [
'type' => 'text',
'foo' => 'bar'
],
'type' => 'richtext',
'exclude' => '1',
'defaultExtras' => 'richtext[]:rte_transform[mode=ts_css]'
],
],
],
];

$expected = [
'tt_content' => [
'elements' => [
'element1' => [
'key' => 'element1',
'label' => 'Element 1',
'description' => '',
'shortLabel' => '',
'color' => '',
'icon' => '',
'descriptions' => [],
'columns' => [
'tx_mask_rte',
],
'labels' => [
'RTE Field',
],
'sorting' => 0
]
],
'tca' => [
'tx_mask_rte' => [
'key' => 'rte',
'fullKey' => 'tx_mask_rte',
'type' => 'richtext',
]
],
'sql' => [],
'palettes' => []
]
];

$tableDefinitionCollection = TableDefinitionCollection::createFromArray($input);
$loader->cleanUpConfig($tableDefinitionCollection);
self::assertEquals($expected, $tableDefinitionCollection->toArray());
}
}
7 changes: 3 additions & 4 deletions Tests/Unit/Loader/DefaultTcaCompatibilityTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ public function missingDefaults(): void
],
'tx_mask_rte' => [
'key' => 'rte',
'config' =>
[
'type' => 'text',
],
'config' => [
'type' => 'text',
],
'type' => 'richtext',
'exclude' => '1',
],
Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/Utility/TcaConverterUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function convertTcaArrayToFlatTestDataProvider()
*/
public function convertTcaArrayToFlatTest($array, $expected)
{
self::assertSame($expected, TcaConverter::convertTcaArrayToFlat($array));
self::assertSame($expected, TcaConverter::convertTcaArrayToFlat($array, ['config']));
}

public function convertFlatTcaToArrayTestDataProvider()
Expand Down

0 comments on commit f81dec1

Please sign in to comment.