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
This commit is contained in:
Ingo Schommer 2010-04-14 03:56:22 +00:00
parent 9c5303285a
commit 658e24ea45
2 changed files with 56 additions and 2 deletions

View File

@ -193,6 +193,23 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
*/ */
public static $cache_permissions = array(); 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. * 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(); parent::onBeforeDelete();
// If deleting this page, delete all its children. // 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) { foreach($children as $child) {
$child->delete(); $child->delete();
} }

View File

@ -307,6 +307,20 @@ class SiteTreeTest extends SapphireTest {
} }
function testDeleteFromStageOperatesRecursively() { 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'); $pageAbout = $this->objFromFixture('Page', 'about');
$pageStaff = $this->objFromFixture('Page', 'staff'); $pageStaff = $this->objFromFixture('Page', 'staff');
$pageStaffDuplicate = $this->objFromFixture('Page', 'staffduplicate'); $pageStaffDuplicate = $this->objFromFixture('Page', 'staffduplicate');
@ -319,6 +333,28 @@ class SiteTreeTest extends SapphireTest {
} }
function testDeleteFromLiveOperatesRecursively() { 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'); $this->logInWithPermssion('ADMIN');
$pageAbout = $this->objFromFixture('Page', 'about'); $pageAbout = $this->objFromFixture('Page', 'about');