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 Element::getPage returning the wrong page #1103

Merged
merged 1 commit into from
Sep 25, 2023
Merged
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
13 changes: 10 additions & 3 deletions src/Models/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,21 +627,28 @@ public function getSimpleClassName()

/**
* Despite the name of the method, getPage can return any type of DataObject
*
* @return null|DataObject
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws \SilverStripe\ORM\ValidationException
*/
public function getPage()
{
// Allow for repeated calls to be cached
if (isset($this->cacheData['page'])) {
return $this->cacheData['page'];
if (isset($this->cacheData['parent_id']) && $this->cacheData['parent_id'] === $this->ParentID) {
return $this->cacheData['page'];
}
}

$class = DataObject::getSchema()->hasOneComponent($this, 'Parent');
$area = ($this->ParentID) ? DataObject::get_by_id($class, $this->ParentID) : null;

if ($area instanceof ElementalArea && $area->exists()) {
$this->cacheData['page'] = $area->getOwnerPage();
$page = $area->getOwnerPage();

$this->cacheData['page'] = $page;
$this->cacheData['parent_id'] = $this->ParentID;

return $this->cacheData['page'];
}

Expand Down
12 changes: 12 additions & 0 deletions tests/BaseElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,4 +503,16 @@ public function testPreviewLink(string $class, string $elementIdentifier, ?strin
$this->assertEmpty(rtrim($previewLink ?? '', '/'));
}
}

public function testGetPage()
{
$element = $this->objFromFixture(ElementContent::class, 'content1');

$this->assertStringContainsString($element->getPage()->Title, 'Test Elemental');

$newArea = $this->objFromFixture(ElementalArea::class, 'area52');
$element->ParentID = $newArea->ID;

$this->assertStringContainsString($element->getPage()->Title, 'Page with one elements');
}
}