diff --git a/src/Versioned.php b/src/Versioned.php index 23afee21f..ff36ed660 100644 --- a/src/Versioned.php +++ b/src/Versioned.php @@ -1612,6 +1612,7 @@ public function doArchive() } $owner->invokeWithExtensions('onBeforeArchive', $this); + $owner->deleteFromChangeSets(); $owner->doUnpublish(); $owner->deleteFromStage(static::DRAFT); $owner->invokeWithExtensions('onAfterArchive', $this); @@ -1619,6 +1620,23 @@ public function doArchive() return true; } + public function deleteFromChangeSets() + { + $owner = $this->owner; + if (!$owner->canArchive()) { + return false; + } + + $ids = [$owner->ID]; + if ($owner->hasMethod('getDescendantIDList')) { + $ids = array_merge($ids, $owner->getDescendantIDList()); + } + + ChangeSetItem::get() + ->filter(['ObjectID' => $ids]) + ->removeAll(); + } + /** * Removes this record from the live site * diff --git a/tests/php/VersionedTest.php b/tests/php/VersionedTest.php index 78afa54a7..1ef414d84 100644 --- a/tests/php/VersionedTest.php +++ b/tests/php/VersionedTest.php @@ -6,6 +6,8 @@ use SilverStripe\ORM\DataObjectSchema; use SilverStripe\ORM\DB; use SilverStripe\Versioned\Versioned; +use SilverStripe\Versioned\ChangeSet; +use SilverStripe\Versioned\ChangeSetItem; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\FieldType\DBDatetime; use SilverStripe\Core\Convert; @@ -32,6 +34,7 @@ class VersionedTest extends SapphireTest VersionedTest\PublicStage::class, VersionedTest\PublicViaExtension::class, VersionedTest\CustomTable::class, + VersionedTest\ChangeSetTestObject::class, ]; public function testUniqueIndexes() @@ -391,6 +394,46 @@ public function testDeleteFromStage() ); } + public function testDeleteFromChangeSets() + { + $page1 = $this->objFromFixture(VersionedTest\ChangeSetTestObject::class, 'page1'); + $page2 = $this->objFromFixture(VersionedTest\ChangeSetTestObject::class, 'page2'); + $page2a = $this->objFromFixture(VersionedTest\ChangeSetTestObject::class, 'page2a'); + $page2b = $this->objFromFixture(VersionedTest\ChangeSetTestObject::class, 'page2b'); + $page3 = $this->objFromFixture(VersionedTest\ChangeSetTestObject::class, 'page3'); + + $cs1 = new ChangeSet(); + $cs1->write(); + + $cs2 = new ChangeSet(); + $cs2->write(); + + // "cs1" will contain 2 items + $cs1->addObject($page1); + $cs1->addObject($page2); + + // "cs2" will contain 3 items + $cs2->addObject($page2a); + $cs2->addObject($page2b); + $cs2->addObject($page3); + + $this->assertEquals(2, $cs1->Changes()->count()); + $this->assertEquals(3, $cs2->Changes()->count()); + + // "cs1" will now contain 1 item + $page1->deleteFromChangeSets(); + + $this->assertEquals(1, $cs1->Changes()->count()); + $this->assertEquals(3, $cs2->Changes()->count()); + + // "cs2" will now contain 1 ("page3") since deleting "page2" from "cs1" will also + // delete "page2a" and "page2b" from change "cs2" + $page2->deleteFromChangeSets(); + $this->assertEquals(0, $cs1->Changes()->count()); + $this->assertEquals(1, $cs2->Changes()->count()); + $this->assertEquals('Page 3', $cs2->Changes()->first()->Title); + } + public function testWritingNewToStage() { $origReadingMode = Versioned::get_reading_mode(); diff --git a/tests/php/VersionedTest.yml b/tests/php/VersionedTest.yml index 3cd795a2c..91db8c2ad 100644 --- a/tests/php/VersionedTest.yml +++ b/tests/php/VersionedTest.yml @@ -34,3 +34,17 @@ SilverStripe\Versioned\Tests\VersionedTest\AnotherSubclass: SilverStripe\Versioned\Tests\VersionedTest\SingleStage: single: Title: 'Singlestage Title' + +SilverStripe\Versioned\Tests\VersionedTest\ChangeSetTestObject: + page1: + Title: Page 1 + page2: + Title: Page 2 + page3: + Title: Page 3 + page2a: + Parent: =>SilverStripe\Versioned\Tests\VersionedTest\ChangeSetTestObject.page2 + Title: Page 2a + page2b: + Parent: =>SilverStripe\Versioned\Tests\VersionedTest\ChangeSetTestObject.page2 + Title: Page 2b diff --git a/tests/php/VersionedTest/ChangeSetTestObject.php b/tests/php/VersionedTest/ChangeSetTestObject.php new file mode 100644 index 000000000..1cebce3b7 --- /dev/null +++ b/tests/php/VersionedTest/ChangeSetTestObject.php @@ -0,0 +1,46 @@ + "Varchar", + 'Title' => 'Varchar', + ]; + + private static $extensions = [ + Versioned::class, + Hierarchy::class + ]; + + private static $has_one = [ + 'Parent' => ChangeSetTestObject::class, + ]; + + private static $has_many = [ + 'Children' => ChangeSetTestObject::class, + ]; + + private static $many_many = [ + 'Related' => RelatedWithoutversion::class, + ]; +}