diff --git a/core/model/VirtualPage.php b/core/model/VirtualPage.php index 0dec2711d..2184156a8 100755 --- a/core/model/VirtualPage.php +++ b/core/model/VirtualPage.php @@ -148,6 +148,15 @@ class VirtualPage extends Page { if($this->extension_instances['Versioned']->migratingVersion && Versioned::current_stage() == 'Live') { $performCopyFrom = true; + + $stageSourceVersion = DB::query("SELECT Version FROM SiteTree WHERE ID = $this->CopyContentFromID")->value(); + $liveSourceVersion = DB::query("SELECT Version FROM SiteTree_Live WHERE ID = $this->CopyContentFromID")->value(); + + // We're going to create a new VP record in SiteTree_versions because the published + // version might not exist, unless we're publishing the latest version + if($stageSourceVersion != $liveSourceVersion) { + $this->extension_instances['Versioned']->migratingVersion = null; + } // On regular write, this will copy from draft source. This is only executed when the source // page changeds diff --git a/tests/model/VirtualPageTest.php b/tests/model/VirtualPageTest.php index 02d73adb1..100900b6b 100644 --- a/tests/model/VirtualPageTest.php +++ b/tests/model/VirtualPageTest.php @@ -214,6 +214,7 @@ class VirtualPageTest extends SapphireTest { $source = $this->objFromFixture('Page', 'master'); $source->Title = "T0"; $source->write(); + $source->doPublish(); // Creating a new VP to ensure that Version #s are out of alignment $vp = new VirtualPage(); @@ -231,6 +232,20 @@ class VirtualPageTest extends SapphireTest { WHERE \"RecordID\" = $vp->ID AND \"Title\" = 'T2'")->value()); $this->assertEquals($vp->ID, DB::query("SELECT \"RecordID\" FROM \"SiteTree_versions\" WHERE \"RecordID\" = $vp->ID AND \"Version\" = $vp->Version")->value()); + + $vp->doPublish(); + + // Check that the published content is copied from the published page, with a legal + // version + $liveVersion = DB::query("SELECT Version FROM \"SiteTree_Live\" WHERE ID = $vp->ID")->value(); + + $this->assertEquals("T0", DB::query("SELECT \"Title\" FROM \"SiteTree_Live\" + WHERE \"ID\" = $vp->ID")->value()); + + // SiteTree_Live.Version should reference a legal entry in SiteTree_versions for the + // virtual page + $this->assertEquals("T0", DB::query("SELECT \"Title\" FROM \"SiteTree_versions\" + WHERE \"RecordID\" = $vp->ID AND \"Version\" = $liveVersion")->value()); } function fixVersionNumberCache($page) {