-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
344 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace B13\Container\Command; | ||
|
||
/* | ||
* This file is part of TYPO3 CMS-based extension "container" by b13. | ||
* | ||
* 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. | ||
*/ | ||
|
||
use B13\Container\Integrity\SortingInPage; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use TYPO3\CMS\Core\Core\Bootstrap; | ||
|
||
class SortingInPageCommand extends Command | ||
{ | ||
/** | ||
* @var SortingInPage | ||
*/ | ||
protected $sorting; | ||
|
||
protected function configure() | ||
{ | ||
$this->addArgument('pid', InputArgument::OPTIONAL, 'limit to this pid', 0); | ||
$this->addOption('apply', null, InputOption::VALUE_NONE, 'apply migration'); | ||
$this->addOption( | ||
'enable-logging', | ||
null, | ||
InputOption::VALUE_NONE, | ||
'enables datahandler logging, should only use for debug issues, not in production' | ||
); | ||
} | ||
|
||
public function __construct(SortingInPage $sorting, string $name = null) | ||
{ | ||
parent::__construct($name); | ||
$this->sorting = $sorting; | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$dryrun = $input->getOption('apply') !== true; | ||
$pid = (int)$input->getArgument('pid'); | ||
|
||
Bootstrap::initializeBackendAuthentication(); | ||
Bootstrap::initializeLanguageObject(); | ||
$errors = $this->sorting->run( | ||
$dryrun, | ||
$input->getOption('enable-logging'), | ||
$pid | ||
); | ||
foreach ($errors as $error) { | ||
$output->writeln($error); | ||
} | ||
if (empty($errors)) { | ||
$output->writeln('migration finished'); | ||
} | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace B13\Container\Integrity; | ||
|
||
/* | ||
* This file is part of TYPO3 CMS-based extension "container" by b13. | ||
* | ||
* 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. | ||
*/ | ||
|
||
use B13\Container\Domain\Factory\ContainerFactory; | ||
use B13\Container\Domain\Model\Container; | ||
use B13\Container\Domain\Service\ContainerService; | ||
use B13\Container\Tca\Registry; | ||
use TYPO3\CMS\Core\DataHandling\DataHandler; | ||
use TYPO3\CMS\Core\SingletonInterface; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
class SortingInPage implements SingletonInterface | ||
{ | ||
/** | ||
* @var Database | ||
*/ | ||
protected $database; | ||
|
||
/** | ||
* @var Registry | ||
*/ | ||
protected $tcaRegistry; | ||
|
||
/** | ||
* @var ContainerFactory | ||
*/ | ||
protected $containerFactory; | ||
|
||
/** | ||
* @var ContainerService | ||
*/ | ||
protected $containerService; | ||
|
||
protected $errors = []; | ||
|
||
public function __construct(Database $database, Registry $tcaRegistry, ContainerFactory $containerFactory, ContainerService $containerService) | ||
{ | ||
$this->database = $database; | ||
$this->tcaRegistry = $tcaRegistry; | ||
$this->containerFactory = $containerFactory; | ||
$this->containerService = $containerService; | ||
} | ||
|
||
public function run(bool $dryRun = true, bool $enableLogging = false, ?int $pid = null): array | ||
{ | ||
$datahandler = GeneralUtility::makeInstance(DataHandler::class); | ||
$datahandler->enableLogging = $enableLogging; | ||
$cTypes = $this->tcaRegistry->getRegisteredCTypes(); | ||
$containerUsedColPosArray = []; | ||
foreach ($cTypes as $cType) { | ||
$columns = $this->tcaRegistry->getAvailableColumns($cType); | ||
foreach ($columns as $column) { | ||
$containerUsedColPosArray[] = $column['colPos']; | ||
} | ||
$this->unsetContentDefenderConfiguration($cType); | ||
} | ||
$rows = $this->database->getNonContainerChildrenPerColPos($containerUsedColPosArray, $pid); | ||
foreach ($rows as $recordsPerPageAndColPos) { | ||
$prevSorting = 0; | ||
$prevContainer = null; | ||
$prevChild = null; | ||
foreach ($recordsPerPageAndColPos as $record) { | ||
if (in_array($record['CType'], $cTypes, true)) { | ||
$container = $this->containerFactory->buildContainer($record['uid']); | ||
$children = $container->getChildRecords(); | ||
if (empty($children)) { | ||
$sorting = $record['sorting']; | ||
} else { | ||
$lastChild = array_pop($children); | ||
$sorting = $lastChild['sorting']; | ||
if ($prevChild === null || $prevContainer === null) { | ||
$prevChild = $lastChild; | ||
$prevContainer = $container; | ||
$prevSorting = $sorting; | ||
continue; | ||
} | ||
if ($sorting < $prevSorting) { | ||
$this->errors[] = 'record ' . $record['uid'] . ' (' . $record['sorting'] . ')' . | ||
' should be sorted after last child ' . $prevChild['uid'] . ' (' . $prevChild['sorting'] . ')' . | ||
' of container ' . $prevContainer->getUid() . ' (' . $prevContainer->getContainerRecord()['sorting'] . ')'; | ||
if ($dryRun === false) { | ||
$cmdmap = [ | ||
'tt_content' => [ | ||
$record['uid'] => [ | ||
'move' => -1 * $prevContainer->getUid(), | ||
], | ||
], | ||
]; | ||
$datahandler->start([], $cmdmap); | ||
$datahandler->process_datamap(); | ||
$datahandler->process_cmdmap(); | ||
} | ||
} | ||
$prevContainer = $container; | ||
$prevChild = $lastChild; | ||
} | ||
} else { | ||
$sorting = $record['sorting']; | ||
} | ||
$prevSorting = $sorting; | ||
} | ||
} | ||
return $this->errors; | ||
} | ||
|
||
protected function unsetContentDefenderConfiguration(string $cType): void | ||
{ | ||
// unset content_defender configuration for migration because already unallowed children in container may exist | ||
foreach ($GLOBALS['TCA']['tt_content']['containerConfiguration'][$cType]['grid'] ?? [] as $rowKey => $row) { | ||
foreach ($row as $colKey => $column) { | ||
$column['allowed'] = []; | ||
$column['disallowed'] = []; | ||
$column['maxitems'] = 0; | ||
$GLOBALS['TCA']['tt_content']['containerConfiguration'][$cType]['grid'][$rowKey][$colKey] = $column; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...tegrity/Fixtures/SortingInPage/container_is_sorted_before_child_of_previous_container.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"pages" | ||
,"uid","pid" | ||
,1,0 | ||
"tt_content" | ||
,"uid","pid","colPos","CType","sorting","tx_container_parent" | ||
,1,1,0,"b13-2cols-with-header-container",1, | ||
,2,1,0,"b13-2cols-with-header-container",2, | ||
,3,1,202,,4,1 | ||
,4,1,202,,3,2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace B13\Container\Tests\Functional\Integrity; | ||
|
||
/* | ||
* This file is part of TYPO3 CMS-based extension "container" by b13. | ||
* | ||
* 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. | ||
*/ | ||
|
||
use B13\Container\Domain\Factory\ContainerFactory; | ||
use B13\Container\Domain\Service\ContainerService; | ||
use B13\Container\Integrity\Database; | ||
use B13\Container\Integrity\SortingInPage; | ||
use B13\Container\Tca\Registry; | ||
use TYPO3\CMS\Core\Context\Context; | ||
use TYPO3\CMS\Core\Core\Bootstrap; | ||
use TYPO3\CMS\Core\Database\ConnectionPool; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; | ||
|
||
class SortingInPageTest extends FunctionalTestCase | ||
{ | ||
/** | ||
* @var SortingInPage | ||
*/ | ||
protected $sorting; | ||
|
||
/** | ||
* @var non-empty-string[] | ||
*/ | ||
protected array $testExtensionsToLoad = [ | ||
'typo3conf/ext/container', | ||
'typo3conf/ext/container_example', | ||
]; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->importCSVDataSet(__DIR__ . '/../Fixtures/be_users.csv'); | ||
$GLOBALS['BE_USER'] = $this->setUpBackendUser(1); | ||
Bootstrap::initializeLanguageObject(); | ||
$context = GeneralUtility::makeInstance(Context::class); | ||
$containerRegistry = GeneralUtility::makeInstance(Registry::class); | ||
$sortingDatabase = GeneralUtility::makeInstance(Database::class); | ||
$factoryDatabase = GeneralUtility::makeInstance(\B13\Container\Domain\Factory\Database::class, $context); | ||
$containerFactory = GeneralUtility::makeInstance(ContainerFactory::class, $factoryDatabase, $containerRegistry, $context); | ||
$containerService = GeneralUtility::makeInstance(ContainerService::class, $containerRegistry, $containerFactory); | ||
$this->sorting = GeneralUtility::makeInstance(SortingInPage::class, $sortingDatabase, $containerRegistry, $containerFactory, $containerService); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function containerIsSortedAfterChildOfPreviousContainer(): void | ||
{ | ||
$this->importCSVDataSet(__DIR__ . '/Fixtures/SortingInPage/container_is_sorted_before_child_of_previous_container.csv'); | ||
$errors = $this->sorting->run(false); | ||
self::assertTrue(count($errors) === 1, 'should get one error'); | ||
$rows = $this->getContentsByUid(); | ||
self::assertTrue($rows[2]['sorting'] > $rows[3]['sorting'], 'container should be sorted after child of previous container'); | ||
} | ||
|
||
protected function getContentsByUid(): array | ||
{ | ||
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content'); | ||
$res = $queryBuilder->select('uid', 'sorting', 'colPos') | ||
->from('tt_content') | ||
->orderBy('sorting') | ||
->execute() | ||
->fetchAllAssociative(); | ||
$rows = []; | ||
foreach ($res as $row) { | ||
$rows[$row['uid']] = $row; | ||
} | ||
return $rows; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters