Skip to content

Commit

Permalink
Always check root pages when searching for related pages (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed May 8, 2017
1 parent fb343bf commit 3524b77
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function findNavigationItems(PageModel $currentPage)
$this->setTargetPageForNavigationItems(
$navigationItems,
$rootPages,
$this->pageFinder->findAssociatedForPage($currentPage)
$this->pageFinder->findAssociatedForPage($currentPage, false, $rootPages)
);

foreach ($navigationItems as $k => $item) {
Expand Down
15 changes: 13 additions & 2 deletions library/Terminal42/ChangeLanguage/PageFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,16 @@ public function findMasterRootForPage(PageModel $page)
*
* @return \Contao\PageModel[]
*/
public function findAssociatedForPage(PageModel $page, $skipCurrent = false)
public function findAssociatedForPage(PageModel $page, $skipCurrent = false, array $rootPages = null)
{
if ('root' === $page->type) {
return $this->findRootPagesForPage($page, $skipCurrent);
}

if (null === $rootPages) {
$rootPages = $this->findRootPagesForPage($page, $skipCurrent);
}

$page->loadDetails();
$t = $page::getTable();

Expand All @@ -132,7 +136,14 @@ public function findAssociatedForPage(PageModel $page, $skipCurrent = false)

$this->addPublishingConditions($columns, $t);

return $this->findPages($columns, $values);
return array_filter(
$this->findPages($columns, $values),
function(PageModel $page) use ($rootPages) {
$page->loadDetails();

return array_key_exists($page->rootId, $rootPages);
}
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function testFindsOnePage()

$dePage = new PageModel();
$dePage->id = $this->createPage($deRoot, $en);
$dePage->pid = $deRoot;
$dePage->languageMain = $en;

$page = $this->pageFinder->findAssociatedForLanguage($dePage, 'en');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ public function testFindsOnePage()

public function testFindsAllFromFallback()
{
$enRoot = $this->createRootPage('en');
$deRoot = $this->createRootPage('de', '');
$frRoot = $this->createRootPage('fr', '');

$pageModel = new PageModel();
$pageModel->id = $this->createPage();
$pageModel->rootIsFallback = true;
$pageModel->rootId = $this->createRootPage('en');
$pageModel->id = $this->createPage(0, $enRoot);
$pageModel->pid = $enRoot;

$this->createPage($pageModel->id);
$this->createPage($pageModel->id);
$this->createPage($pageModel->id, $frRoot);
$this->createPage($pageModel->id, $deRoot);

$pages = $this->pageFinder->findAssociatedForPage($pageModel);

Expand All @@ -56,14 +59,18 @@ public function testFindsAllFromFallback()

public function testFindsAllFromRelated()
{
$fallback = $this->createPage();
$enRoot = $this->createRootPage('en');
$deRoot = $this->createRootPage('de', '');
$frRoot = $this->createRootPage('fr', '');

$fallback = $this->createPage(0, $enRoot);

$pageModel = new PageModel();
$pageModel->id = $this->createPage($fallback);
$pageModel->rootIsFallback = false;
$pageModel->id = $this->createPage($fallback, $deRoot);
$pageModel->pid = $deRoot;
$pageModel->languageMain = $fallback;

$this->createPage($fallback);
$this->createPage($fallback, $frRoot);

$pages = $this->pageFinder->findAssociatedForPage($pageModel);

Expand All @@ -77,7 +84,6 @@ public function testIgnoresEmptyMain()

$pageModel = new PageModel();
$pageModel->id = $this->createPage();
$pageModel->rootIsFallback = false;
$pageModel->languageMain = 0;

$pages = $this->pageFinder->findAssociatedForPage($pageModel);
Expand All @@ -87,12 +93,14 @@ public function testIgnoresEmptyMain()

public function testIgnoresLanguageMainOnFallback()
{
$fallback = $this->createPage();
$enRoot = $this->createRootPage('en');
$deRoot = $this->createRootPage('de', '');

$fallback = $this->createPage(0, $deRoot);

$pageModel = new PageModel();
$pageModel->id = $this->createPage($fallback);
$pageModel->rootIsFallback = true;
$pageModel->rootId = $this->createRootPage('en');
$pageModel->id = $this->createPage($fallback, $enRoot);
$pageModel->pid = $enRoot;
$pageModel->languageMain = $fallback;

$this->createPage($fallback);
Expand Down Expand Up @@ -129,8 +137,29 @@ public function testFindsAllOnDifferentDomains()

$pageModel = new PageModel();
$pageModel->id = $de;
$pageModel->pid = $deRoot;
$pageModel->languageMain = $en;
$pageModel->type = 'regular';

$pages = $this->pageFinder->findAssociatedForPage($pageModel);

$this->assertPageCount($pages, 2);
}

public function testIgnoresPagesInWrongRoot()
{
$enRoot = $this->createRootPage('en', '1', 'www.example.com');
$deRoot = $this->createRootPage('de', '', 'www.example.com');
$frRoot = $this->createRootPage('fr', '1', 'www.example.org');

$en = $this->createPage(0, $enRoot);
$de = $this->createPage($en, $deRoot);
$this->createPage($en, $frRoot);

$pageModel = new PageModel();
$pageModel->id = $de;
$pageModel->pid = $deRoot;
$pageModel->languageMain = $en;
$pageModel->rootIsFallback = true;
$pageModel->type = 'regular';

$pages = $this->pageFinder->findAssociatedForPage($pageModel);
Expand Down

0 comments on commit 3524b77

Please sign in to comment.