Skip to content

Commit

Permalink
Always insert the new article into an available layout column
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Jul 7, 2020
1 parent 701cd59 commit 0be7232
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function generateArticleForPage(DataContainer $dc): void
if (
empty($pageModel->title)
|| !$this->supportsComposition($pageModel)
|| !$this->hasArticlesInLayout($pageModel)
|| null === ($column = $this->getArticleColumnInLayout($pageModel))
) {
return;
}
Expand Down Expand Up @@ -167,7 +167,7 @@ public function generateArticleForPage(DataContainer $dc): void
'sorting' => 128,
'tstamp' => time(),
'author' => $user->id,
'inColumn' => 'main',
'inColumn' => $column,
'title' => $dc->activeRecord->title,
'alias' => str_replace('/', '-', $dc->activeRecord->alias), // see #516
'published' => $dc->activeRecord->published,
Expand Down Expand Up @@ -274,22 +274,39 @@ private function supportsComposition(PageModel $pageModel): bool
}

private function hasArticlesInLayout(PageModel $pageModel): bool
{
return null !== $this->getArticleColumnInLayout($pageModel);
}

private function getArticleColumnInLayout(PageModel $pageModel): ?string
{
$pageModel->loadDetails();

/** @var LayoutModel $layout */
$layout = $pageModel->getRelated('layout');

if (null === $layout) {
return false;
return null;
}

$columns = [];

foreach (StringUtil::deserialize($layout->modules, true) as $config) {
if (0 === (int) $config['mod']) {
return true;
$columns[] = $config['col'];
}
}

return false;
$columns = array_filter(array_unique($columns));

if (empty($columns)) {
return null;
}

if (\in_array('main', $columns, true)) {
return 'main';
}

return reset($columns);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,85 @@ public function testGenerateArticleForNewPage(): void
$this->listener->generateArticleForPage($dc);
}

/**
* @dataProvider moduleConfigProvider
*/
public function testUsesTheLayoutColumnForNewArticle(array $modules, string $expectedColumn): void
{
$this->expectRequest(true, ['tl_foo' => [17]]);
$this->expectUser();
$page = $this->expectPageWithRow($this->pageRecord);
$this->mockPageProvider(true, true, $page);
$this->expectArticleCount(0);

$page
->expects($this->once())
->method('getRelated')
->with('layout')
->willReturn(
$this->mockClassWithProperties(LayoutModel::class, ['modules' => serialize($modules)])
)
;

$article = [
'pid' => 17,
'sorting' => 128,
'tstamp' => time(),
'author' => 1,
'inColumn' => $expectedColumn,
'title' => 'foo',
'alias' => 'foo-bar', // Expect folder alias conversion
'published' => '1',
];

$this->connection
->expects($this->once())
->method('insert')
->with('tl_article', $article)
;

/** @var DataContainer&MockObject $dc */
$dc = $this->mockClassWithProperties(DC_Table::class, ['id' => 17, 'table' => 'tl_foo', 'activeRecord' => (object) $this->pageRecord]);

$this->listener->generateArticleForPage($dc);
}

public function moduleConfigProvider(): \Generator
{
yield [
[
['mod' => 0, 'col' => 'main'],
],
'main'
];

yield [
[
['mod' => 1, 'col' => 'foo'],
['mod' => 0, 'col' => 'main'],
],
'main'
];

yield [
[
['mod' => 1, 'col' => 'main'],
['mod' => 0, 'col' => 'foo'],
],
'foo'
];

yield [
[
['mod' => 1, 'col' => 'main'],
['mod' => 2, 'col' => 'foo'],
['mod' => 0, 'col' => 'bar'],
['mod' => 0, 'col' => 'foo'],
],
'bar'
];
}

public function testCannotPasteArticleWithoutBackendUser(): void
{
$this->expectUser(FrontendUser::class);
Expand Down Expand Up @@ -935,7 +1014,7 @@ private function expectPageWithRow(array $row, $moduleId = false): PageModel
$moduleId = $this->mockClassWithProperties(
LayoutModel::class, [
'modules' => serialize([
['mod' => $moduleId],
['mod' => $moduleId, 'col' => 'main'],
]),
]
);
Expand Down Expand Up @@ -978,7 +1057,7 @@ private function expectPageFindByPk(int $id, array $row, $moduleId = false): Pag
$moduleId = $this->mockClassWithProperties(
LayoutModel::class, [
'modules' => serialize([
['mod' => $moduleId],
['mod' => $moduleId, 'col' => 'main'],
]),
]
);
Expand Down

0 comments on commit 0be7232

Please sign in to comment.