From 1eee3cc54b01e94ae2ca8783f0e5b0feb67d7662 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Thu, 21 May 2009 00:34:58 +0000 Subject: [PATCH] BUGFIX: Ensure that when a page is deleted from stage or live, its descendants are also deleted. git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@77461 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/SiteTree.php | 22 ++++++++++++++++------ tests/SiteTreeTest.php | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/core/model/SiteTree.php b/core/model/SiteTree.php index 311604975..bd250da70 100644 --- a/core/model/SiteTree.php +++ b/core/model/SiteTree.php @@ -1042,6 +1042,18 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid parent::onAfterWrite(); } + function onBeforeDelete() { + parent::onBeforeDelete(); + + // If deleting this page, delete all its children. + if($children = $this->Children()) { + foreach($children as $child) { + $child->delete(); + } + } + } + + function onAfterDelete() { // Need to flush cache to avoid outdated versionnumber references $this->flushCache(); @@ -1387,8 +1399,8 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid } } - if($this->IsDeletedFromStage) { - if($this->can('CMSEdit')) { + if($this->canEdit()) { + if($this->IsDeletedFromStage) { if($this->ExistsOnLive) { // "restore" $actions->push(new FormAction('revert',_t('CMSMain.RESTORE','Restore'))); @@ -1398,13 +1410,11 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid // "restore" $actions->push(new FormAction('restore',_t('CMSMain.RESTORE','Restore'))); } - } - } else { - if($this->canEdit()) { + } else { // "delete" $actions->push($deleteAction = new FormAction('delete',_t('CMSMain.DELETE','Delete from the draft site'))); $deleteAction->addExtraClass('delete'); - + // "save" $actions->push(new FormAction('save',_t('CMSMain.SAVE','Save'))); } diff --git a/tests/SiteTreeTest.php b/tests/SiteTreeTest.php index 2f037407d..16b139e80 100644 --- a/tests/SiteTreeTest.php +++ b/tests/SiteTreeTest.php @@ -5,7 +5,7 @@ */ class SiteTreeTest extends SapphireTest { static $fixture_file = 'sapphire/tests/SiteTreeTest.yml'; - + /** * Test generation of the URLSegment values. * - Turns things into lowercase-hyphen-format @@ -80,6 +80,8 @@ class SiteTreeTest extends SapphireTest { $checkSiteTree = DataObject::get_one("SiteTree", "URLSegment = 'get-one-test-page'"); $this->assertEquals("V1", $checkSiteTree->Title); + + Versioned::reading_stage($oldStage); } function testChidrenOfRootAreTopLevelPages() { @@ -188,7 +190,7 @@ class SiteTreeTest extends SapphireTest { $this->assertEquals('Page', $requeriedPage->class); - $page2 = $this->objFromFixture('Page', 'staff'); + $page2 = $this->objFromFixture('Page', 'products'); $page2ID = $page2->ID; $page2->doUnpublish(); $page2->delete(); @@ -202,7 +204,7 @@ class SiteTreeTest extends SapphireTest { Versioned::reading_stage('Stage'); $requeriedPage = DataObject::get_by_id("Page", $page2ID); - $this->assertEquals('Staff', $requeriedPage->Title); + $this->assertEquals('Products', $requeriedPage->Title); $this->assertEquals('Page', $requeriedPage->class); } @@ -218,6 +220,32 @@ class SiteTreeTest extends SapphireTest { // Note: One day, it would be more appropriate to return null instead of false for queries such as these $this->assertFalse(SiteTree::get_by_url("home", "1 = 2")); } + + + function testDeleteFromStageOperatesRecursively() { + $parentPage = $this->objFromFixture('Page', 'about'); + $parentPage->delete(); + + $this->assertFalse($this->objFromFixture('Page', 'about')); + $this->assertFalse($this->objFromFixture('Page', 'staff')); + $this->assertFalse($this->objFromFixture('Page', 'staffduplicate')); + } + + function testDeleteFromLiveOperatesRecursively() { + $this->objFromFixture('Page', 'about')->doPublish(); + $this->objFromFixture('Page', 'staff')->doPublish(); + $this->objFromFixture('Page', 'staffduplicate')->doPublish(); + + + $parentPage = $this->objFromFixture('Page', 'about'); + $parentPage->doDeleteFromLive(); + + Versioned::reading_stage('Live'); + $this->assertFalse($this->objFromFixture('Page', 'about')); + $this->assertFalse($this->objFromFixture('Page', 'staff')); + $this->assertFalse($this->objFromFixture('Page', 'staffduplicate')); + Versioned::reading_stage('Stage'); + } }