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 getOwnerPage() should respect Versioned state. #975

Merged
merged 1 commit into from
May 30, 2022
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/node_modules/
/**/*.js.map
/**/*.css.map
/vendor/
/resources/
/assets/
16 changes: 11 additions & 5 deletions src/Models/ElementalArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ public function setElementsCached(ArrayList $elements)
*/
public function setOwnerPageCached(DataObject $page)
{
$this->cacheData['owner_page'] = $page;
$cacheKey = 'owner_page_'. Versioned::get_reading_mode();

$this->cacheData[$cacheKey] = $page;
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved

return $this;
}
Expand Down Expand Up @@ -193,8 +195,10 @@ public function getOwnerPage()
}

// Allow for repeated calls to read from cache
if (isset($this->cacheData['owner_page'])) {
return $this->cacheData['owner_page'];
$cacheKey = 'owner_page_'. Versioned::get_reading_mode();

GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
if (isset($this->cacheData[$cacheKey])) {
return $this->cacheData[$cacheKey];
}

if ($this->OwnerClassName && ClassInfo::exists($this->OwnerClassName)) {
Expand All @@ -217,6 +221,7 @@ public function getOwnerPage()

if ($page) {
$this->setOwnerPageCached($page);

return $page;
}
}
Expand All @@ -234,7 +239,7 @@ public function getOwnerPage()
}

try {
$page = Versioned::get_by_stage($class, Versioned::DRAFT)->filterAny($areaIDFilters)->first();
$page = DataObject::get($class)->filterAny($areaIDFilters)->first();
} catch (\Exception $ex) {
// Usually this is catching cases where test stubs from other modules are trying to be loaded
// and failing in unit tests.
Expand All @@ -255,7 +260,8 @@ public function getOwnerPage()
}
}

$this->cacheData['area_relation_name'] = $page;
$this->setOwnerPageCached($page);

return $page;
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/ElementalAreaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,29 @@ public function testGetOwnerPage()

// OwnerClassName not set
$ownerpage1 = $area1->getOwnerPage();

// OwnerClassName set
$ownerpage2 = $area2->getOwnerPage();

$this->assertEquals("DNADesign\Elemental\Tests\Src\TestPage", $ownerpage1);
$this->assertEquals("DNADesign\Elemental\Tests\Src\TestPage", $ownerpage2);

// if ownerpage1 has draft changes then getOwnerPage() should return the
// live version of the owner page, since the draft record will be
// unviewable by logged out users
$ownerpage1->publishRecursive();

$ownerpage1->Title = 'I have edited the page';
$ownerpage1->writeToStage(Versioned::DRAFT);

$liveOwner = Versioned::withVersionedMode(function () use ($area1) {
Versioned::set_stage(Versioned::LIVE);
$page = $area1->getOwnerPage();

return $page;
});

$this->assertEquals($liveOwner->Title, 'Page 1', 'getOwnerPage returns live version of page, not the draft');
}

public function testForTemplate()
Expand Down