Skip to content

Commit

Permalink
Merge pull request #1082 from wilr/5
Browse files Browse the repository at this point in the history
fix: getPage returning the wrong page
  • Loading branch information
GuySartorelli committed Sep 21, 2023
2 parents 70a0e71 + 7f1daf8 commit 4506c15
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Models/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -692,21 +692,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
13 changes: 13 additions & 0 deletions tests/BaseElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,4 +511,17 @@ public function testGetContentForCmsSearch()
$element = $this->objFromFixture(TestElement::class, 'elementDataObject3');
$this->assertSame('Hello Test|#|Element 3', $element->getContentForCmsSearch());
}


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');
}
}

0 comments on commit 4506c15

Please sign in to comment.