From 658e24ea45215c46ba541e2d0b536adfb4734f90 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 14 Apr 2010 03:56:22 +0000 Subject: [PATCH] MINOR added enforce_strict_hierarchy option, and tests around not cascading deletions (from r98498) (from r99059) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@102829 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/SiteTree.php | 20 +++++++++++++++++++- tests/SiteTreeTest.php | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/core/model/SiteTree.php b/core/model/SiteTree.php index 929f14cf4..a180922c3 100755 --- a/core/model/SiteTree.php +++ b/core/model/SiteTree.php @@ -193,6 +193,23 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid */ public static $cache_permissions = array(); + /** + * @see SiteTree::enforce_strict_hierarchy() + */ + private static $enforce_strict_hierarchy = true; + + /** + * Getter and setter for enforce_strict_hierarchy. With no + * args it returns current value. Pass arg to set. + * + * @return bool + */ + public static function enforce_strict_hierarchy() { + $args = func_get_args(); + if (count($args)) self::$enforce_strict_hierarchy = (bool) $args[0]; + return self::$enforce_strict_hierarchy; + } + /** * Returns TRUE if nested URLs (e.g. page/sub-page/) are currently enabled on this site. * @@ -1397,7 +1414,8 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid parent::onBeforeDelete(); // If deleting this page, delete all its children. - if($children = $this->Children()) { + if(SiteTree::enforce_strict_hierarchy() && $children = $this->Children()) { + // if($children = $this->Children()) { foreach($children as $child) { $child->delete(); } diff --git a/tests/SiteTreeTest.php b/tests/SiteTreeTest.php index bab8005a5..50adb1cf3 100755 --- a/tests/SiteTreeTest.php +++ b/tests/SiteTreeTest.php @@ -307,6 +307,20 @@ class SiteTreeTest extends SapphireTest { } function testDeleteFromStageOperatesRecursively() { + SiteTree::enforce_strict_hierarchy(false); + $pageAbout = $this->objFromFixture('Page', 'about'); + $pageStaff = $this->objFromFixture('Page', 'staff'); + $pageStaffDuplicate = $this->objFromFixture('Page', 'staffduplicate'); + + $pageAbout->delete(); + + $this->assertFalse(DataObject::get_by_id('Page', $pageAbout->ID)); + $this->assertTrue(DataObject::get_by_id('Page', $pageStaff->ID) instanceof Page); + $this->assertTrue(DataObject::get_by_id('Page', $pageStaffDuplicate->ID) instanceof Page); + SiteTree::enforce_strict_hierarchy(true); + } + + function testDeleteFromStageOperatesRecursivelyStrict() { $pageAbout = $this->objFromFixture('Page', 'about'); $pageStaff = $this->objFromFixture('Page', 'staff'); $pageStaffDuplicate = $this->objFromFixture('Page', 'staffduplicate'); @@ -317,8 +331,30 @@ class SiteTreeTest extends SapphireTest { $this->assertFalse(DataObject::get_by_id('Page', $pageStaff->ID)); $this->assertFalse(DataObject::get_by_id('Page', $pageStaffDuplicate->ID)); } - + function testDeleteFromLiveOperatesRecursively() { + SiteTree::enforce_strict_hierarchy(false); + $this->logInWithPermssion('ADMIN'); + + $pageAbout = $this->objFromFixture('Page', 'about'); + $pageAbout->doPublish(); + $pageStaff = $this->objFromFixture('Page', 'staff'); + $pageStaff->doPublish(); + $pageStaffDuplicate = $this->objFromFixture('Page', 'staffduplicate'); + $pageStaffDuplicate->doPublish(); + + $parentPage = $this->objFromFixture('Page', 'about'); + $parentPage->doDeleteFromLive(); + + Versioned::reading_stage('Live'); + $this->assertFalse(DataObject::get_by_id('Page', $pageAbout->ID)); + $this->assertTrue(DataObject::get_by_id('Page', $pageStaff->ID) instanceof Page); + $this->assertTrue(DataObject::get_by_id('Page', $pageStaffDuplicate->ID) instanceof Page); + Versioned::reading_stage('Stage'); + SiteTree::enforce_strict_hierarchy(true); + } + + function testDeleteFromLiveOperatesRecursivelyStrict() { $this->logInWithPermssion('ADMIN'); $pageAbout = $this->objFromFixture('Page', 'about');