Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sorting in page for translations #526

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Classes/Command/SortingInPageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class SortingInPageCommand extends Command
protected function configure()
{
$this->addArgument('pid', InputArgument::OPTIONAL, 'limit to this pid', 0);
$this->addArgument('languageId', InputArgument::OPTIONAL, 'limit to this languageId', 0);
$this->addOption('apply', null, InputOption::VALUE_NONE, 'apply migration');
$this->addOption(
'enable-logging',
Expand All @@ -51,13 +52,17 @@ public function execute(InputInterface $input, OutputInterface $output): int
{
$dryrun = $input->getOption('apply') !== true;
$pid = (int)$input->getArgument('pid');
if ($input->getArgument('languageId') !== 'all') {
$languageId = (int)$input->getArgument('languageId');
}

Bootstrap::initializeBackendAuthentication();
$GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageServiceFactory::class)->createFromUserPreferences($GLOBALS['BE_USER']);
$errors = $this->sorting->run(
$dryrun,
$input->getOption('enable-logging'),
$pid
$pid,
$languageId,
);
foreach ($errors as $error) {
$output->writeln($error);
Expand Down
18 changes: 13 additions & 5 deletions Classes/Domain/Service/ContainerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,26 @@ public function getNewContentElementAtTopTargetInColumn(Container $container, in
return $target;
}

public function getAfterContainerElementTarget(Container $container): int
public function getAfterContainerRecord(Container $container): array
{
$target = -$container->getUid();
$childRecords = $container->getChildRecords();
if (empty($childRecords)) {
return $target;
return $container->getContainerRecord();
}

$lastChild = array_pop($childRecords);
if (!$this->tcaRegistry->isContainerElement($lastChild['CType'])) {
return -(int)$lastChild['uid'];
return $lastChild;
}

$container = $this->containerFactory->buildContainer((int)$lastChild['uid']);
return $this->getAfterContainerElementTarget($container);
return $this->getAfterContainerRecord($container);
}

public function getAfterContainerElementTarget(Container $container): int
{
$target = $this->getAfterContainerRecord($container);

return -$target['uid'];
}
}
14 changes: 10 additions & 4 deletions Classes/Integrity/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function getChildrenByContainerAndColPos(int $containerId, int $colPos, i
return $rows;
}

public function getNonContainerChildrenPerColPos(array $containerUsedColPosArray, ?int $pid = null): array
public function getNonContainerChildrenPerColPos(array $containerUsedColPosArray, ?int $pid = null, ?int $languageId = null): array
{
$queryBuilder = $this->getQueryBuilder();
$stm = $queryBuilder
Expand All @@ -116,12 +116,18 @@ public function getNonContainerChildrenPerColPos(array $containerUsedColPosArray
$queryBuilder->expr()->notIn(
'colPos',
$queryBuilder->createNamedParameter($containerUsedColPosArray, Connection::PARAM_INT_ARRAY)
),
)
);

if (!is_null($languageId)) {
$stm->andWhere(
$queryBuilder->expr()->eq(
'sys_language_uid',
$queryBuilder->createNamedParameter(0, Connection::PARAM_INT)
$queryBuilder->createNamedParameter($languageId, Connection::PARAM_INT)
)
);
}

if (!empty($pid)) {
$stm->andWhere(
$queryBuilder->expr()->eq(
Expand All @@ -136,7 +142,7 @@ public function getNonContainerChildrenPerColPos(array $containerUsedColPosArray
$results = $stm->executeQuery()->fetchAllAssociative();
$rows = [];
foreach ($results as $result) {
$key = $result['pid'] . '-' . $result['colPos'];
$key = $result['pid'] . '-' . $result['sys_language_uid'] . '-' . $result['colPos'];
if (!isset($rows[$key])) {
$rows[$key] = [];
}
Expand Down
13 changes: 5 additions & 8 deletions Classes/Integrity/SortingInPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __construct(Database $database, Registry $tcaRegistry, Container
$this->containerService = $containerService;
}

public function run(bool $dryRun = true, bool $enableLogging = false, ?int $pid = null): array
public function run(bool $dryRun = true, bool $enableLogging = false, ?int $pid = null, ?int $languageId = null): array
{
$this->unsetContentDefenderConfiguration();
$dataHandler = GeneralUtility::makeInstance(DataHandler::class);
Expand All @@ -65,21 +65,18 @@ public function run(bool $dryRun = true, bool $enableLogging = false, ?int $pid
$containerUsedColPosArray[] = $column['colPos'];
}
}
$rows = $this->database->getNonContainerChildrenPerColPos($containerUsedColPosArray, $pid);
$rows = $this->database->getNonContainerChildrenPerColPos($containerUsedColPosArray, $pid, $languageId);
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'];
$lastChild = $this->containerService->getAfterContainerRecord($container);
$sorting = $lastChild['sorting'];

if ($record['uid'] !== $lastChild['uid']) {
if ($prevChild === null || $prevContainer === null) {
$prevChild = $lastChild;
$prevContainer = $container;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"pages"
,"uid","pid","sys_language_uid"
,1,0,0
,2,1,1
,3,1,2
"tt_content"
,"uid","pid","colPos","CType","sorting","tx_container_parent","sys_language_uid"
,1,2,0,"b13-2cols-with-header-container",1,,1
,2,2,0,"b13-2cols-with-header-container",2,,1
,3,2,202,,4,1,1
,4,2,202,,3,2,1
,5,3,0,"b13-2cols-with-header-container",5,,2
,6,3,0,"b13-2cols-with-header-container",6,,2
,7,3,202,,8,5,2
,8,3,202,,7,6,2
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"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,202,b13-2cols-with-header-container,2,1
,3,1,202,,5,2
,4,1,0,"b13-2cols-with-header-container",3,
,5,1,202,,4,4
64 changes: 64 additions & 0 deletions Tests/Functional/Integrity/SortingInPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,57 @@ public function containerIsSortedAfterChildOfPreviousContainer(): void
self::assertTrue($rows[2]['sorting'] > $rows[3]['sorting'], 'container should be sorted after child of previous container');
}

public static function getPossibleTranslations(): iterable
{
yield 'with language id 1' => [
'languageId' => 1,
'expected' => [
'errors' => 1,
'sortings' => [
2 => 3,
]
]
];
yield 'with language id 2' => [
'languageId' => 2,
'expected' => [
'errors' => 1,
'sortings' => [
6 => 7,
]
]
];
yield 'with all languages' => [
'languageId' => null,
'expected' => [
'errors' => 2,
'sortings' => [
2 => 3,
6 => 7,
]
]
];
}

/**
* @test
* @dataProvider getPossibleTranslations
*/
public function containerIsSortedAfterChildOfPreviousContainerOnTranslatedPage(?int $languageId = null, array $expected): void
{
$this->importCSVDataSet(__DIR__ . '/Fixtures/SortingInPage/container_is_sorted_before_child_of_previous_container_on_translated_page.csv');
$errors = $this->sorting->run(false, false, null, 0);
self::assertTrue(count($errors) === 0, 'different number of errors for default language');

$errors = $this->sorting->run(false, false, null, $languageId);
self::assertTrue(count($errors) === $expected['errors'], 'different number of errors for given language');

$rows = $this->getContentsByUid();
foreach ($expected['sortings'] as $before => $after) {
self::assertTrue($rows[$before]['sorting'] > $rows[$after]['sorting'], 'container should be sorted after child of previous container');
}
}

/**
* @test
*/
Expand All @@ -77,6 +128,19 @@ public function containerIsSortedAfterChildOfPreviousContainerWithChangedChildre
self::assertTrue($rows[2]['sorting'] > $rows[3]['sorting'], 'container should be sorted after child of previous container');
}

/**
* @test
*/
public function containerIsSortedAfterChildOfPreviousContainerWithNestedChangedChildrenSorting(): void
{
$this->importCSVDataSet(__DIR__ . '/Fixtures/SortingInPage/container_is_sorted_before_child_of_previous_container_with_nested_changed_children_sorting.csv');
$errors = $this->sorting->run(false);
self::assertTrue(count($errors) === 1, 'should get one error');
$rows = $this->getContentsByUid();
self::assertTrue($rows[4]['sorting'] > $rows[3]['sorting'], 'container should be sorted after last nested child of previous container');
self::assertTrue($rows[5]['sorting'] > $rows[4]['sorting'], 'child should be sorted after its own parent container after resorting');
}

/**
* @test
*/
Expand Down