diff --git a/model/Versioned.php b/model/Versioned.php index 849e3056a..b8356cd57 100644 --- a/model/Versioned.php +++ b/model/Versioned.php @@ -815,7 +815,7 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { $oldMode = Versioned::get_reading_mode(); Versioned::reading_stage($stage); - $versionFromStage = DataObject::get($this->class)->byID($this->ID); + $versionFromStage = DataObject::get($this->owner->class)->byID($this->owner->ID); Versioned::set_reading_mode($oldMode); return $versionFromStage ? $versionFromStage->canView($member) : false; diff --git a/tests/model/VersionedTest.php b/tests/model/VersionedTest.php index 72fc3d312..bdd4bbbb4 100644 --- a/tests/model/VersionedTest.php +++ b/tests/model/VersionedTest.php @@ -33,7 +33,7 @@ class VersionedTest extends SapphireTest { 'VersionedTest_WithIndexes_Live' => array('value' => false, 'message' => 'Unique indexes are no longer unique in _Live table'), ); - + // Test each table's performance foreach ($tableExpectations as $tableName => $expectation) { $indexes = DB::get_schema()->indexList($tableName); @@ -800,6 +800,40 @@ class VersionedTest extends SapphireTest { $this->assertTrue($public2->canView()); $this->assertTrue($private->canView()); } + + + public function testCanViewStage() { + $public = $this->objFromFixture('VersionedTest_PublicStage', 'public1'); + $private = $this->objFromFixture('VersionedTest_DataObject', 'page1'); + Session::clear("loggedInAs"); + Versioned::reading_stage('Stage'); + + // Test that all (and only) public pages are viewable in stage mode + // Unpublished records are not viewable in live regardless of permissions + $this->assertTrue($public->canViewStage('Stage')); + $this->assertFalse($private->canViewStage('Stage')); + $this->assertFalse($public->canViewStage('Live')); + $this->assertFalse($private->canViewStage('Live')); + + // Writing records to live should make both stage and live modes viewable + $private->publish("Stage", "Live"); + $public->publish("Stage", "Live"); + $this->assertTrue($public->canViewStage('Stage')); + $this->assertTrue($private->canViewStage('Stage')); + $this->assertTrue($public->canViewStage('Live')); + $this->assertTrue($private->canViewStage('Live')); + + // If the draft mode changes, the live mode remains public, although the updated + // draft mode is secured for non-public records. + $private->Title = 'Secret Title'; + $private->write(); + $public->Title = 'Public Title'; + $public->write(); + $this->assertTrue($public->canViewStage('Stage')); + $this->assertFalse($private->canViewStage('Stage')); + $this->assertTrue($public->canViewStage('Live')); + $this->assertTrue($private->canViewStage('Live')); + } }