From b20f1fd59df3ccc63bffcc668c731e341f5a214d Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 6 Jul 2010 02:47:00 +0000 Subject: [PATCH] BUGFIX Remove dummy entry created by Versioned if record is first written to Live stage (fixes #5596, thanks muzdowski) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@107537 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/Versioned.php | 5 +++++ tests/model/VersionedTest.php | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/core/model/Versioned.php b/core/model/Versioned.php index 724b2d460..6fea558c1 100755 --- a/core/model/Versioned.php +++ b/core/model/Versioned.php @@ -396,6 +396,11 @@ class Versioned extends DataObjectDecorator { // If we're editing Live, then use (table)_Live instead of (table) if(Versioned::current_stage() && Versioned::current_stage() != $this->defaultStage) { + // If the record has already been inserted in the (table), get rid of it. + if($manipulation[$table]['command']=='insert') { + DB::query("DELETE FROM \"{$table}\" WHERE \"ID\"='$id'"); + } + $newTable = $table . '_' . Versioned::current_stage(); $manipulation[$newTable] = $manipulation[$table]; unset($manipulation[$table]); diff --git a/tests/model/VersionedTest.php b/tests/model/VersionedTest.php index 41e7579ab..c1b22a8f6 100644 --- a/tests/model/VersionedTest.php +++ b/tests/model/VersionedTest.php @@ -117,6 +117,48 @@ class VersionedTest extends SapphireTest { $this->assertEquals(0, DB::query('SELECT COUNT(*) FROM "SiteTree_Live" WHERE "ID" = '.$pageID)->value()); } + function testWritingNewToStage() { + $origStage = Versioned::current_stage(); + + Versioned::reading_stage("Stage"); + $page = new Page(); + $page->Title = "testWritingNewToStage"; + $page->URLSegment = "testWritingNewToStage"; + $page->write(); + + $live = Versioned::get_by_stage('SiteTree', 'Live', "\"SiteTree_Live\".\"ID\"='$page->ID'"); + $this->assertNull($live); + + $stage = Versioned::get_by_stage('SiteTree', 'Stage', "\"SiteTree\".\"ID\"='$page->ID'"); + $this->assertNotNull($stage); + $this->assertEquals($stage->First()->Title, 'testWritingNewToStage'); + + Versioned::reading_stage($origStage); + } + + /** + * This tests for the situation described in the ticket #5596. + * Writing new Page to live first creates a row in SiteTree table (to get the new ID), then "changes + * it's mind" in Versioned and writes SiteTree_Live. It does not remove the SiteTree record though. + */ + function testWritingNewToLive() { + $origStage = Versioned::current_stage(); + + Versioned::reading_stage("Live"); + $page = new Page(); + $page->Title = "testWritingNewToLive"; + $page->URLSegment = "testWritingNewToLive"; + $page->write(); + + $live = Versioned::get_by_stage('SiteTree', 'Live', "\"SiteTree_Live\".\"ID\"='$page->ID'"); + $this->assertNotNull($live->First()); + $this->assertEquals($live->First()->Title, 'testWritingNewToLive'); + + $stage = Versioned::get_by_stage('SiteTree', 'Stage', "\"SiteTree\".\"ID\"='$page->ID'"); + $this->assertNull($stage); + + Versioned::reading_stage($origStage); + } } class VersionedTest_DataObject extends DataObject implements TestOnly {