Skip to content

Commit

Permalink
Merge pull request #1006 from creative-commoners/pulls/4.9/backport-r…
Browse files Browse the repository at this point in the history
…espect-versioned

FIX getOwnerPage() should respect Versioned state.
  • Loading branch information
GuySartorelli authored Sep 6, 2022
2 parents 54246f9 + 2195434 commit 86e2c78
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
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;

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();

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

0 comments on commit 86e2c78

Please sign in to comment.