Merge pull request #1988 from open-sausages/pulls/3/hierarchy-cascading-deletes

Move cascading delete to onAfterDelete for SiteTree
This commit is contained in:
Damian Mooyman 2017-10-17 09:20:23 +13:00 committed by GitHub
commit 0d1c6b9f0c
2 changed files with 20 additions and 2 deletions

View File

@ -1575,7 +1575,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
parent::onBeforeDelete();
// If deleting this page, delete all its children.
if(SiteTree::config()->enforce_strict_hierarchy && $children = $this->AllChildren()) {
if($this->isInDB() && SiteTree::config()->enforce_strict_hierarchy && $children = $this->AllChildren()) {
foreach($children as $child) {
$child->delete();
}

View File

@ -276,7 +276,25 @@ class SiteTreeTest extends SapphireTest {
}
public function testGetByLink() {
public function testNoCascadingDeleteWithoutID() {
Config::inst()->update('SiteTree', 'enforce_strict_hierarchy', true);
$count = SiteTree::get()->count();
$this->assertNotEmpty($count);
$obj = new SiteTree();
$this->assertFalse($obj->exists());
$fail = true;
try {
$obj->delete();
} catch (LogicException $e) {
$fail = false;
}
if ($fail) {
$this->fail('Failed to throw delete exception');
}
$this->assertCount($count, SiteTree::get());
}
public function testGetByLink() {
$home = $this->objFromFixture('Page', 'home');
$about = $this->objFromFixture('Page', 'about');
$staff = $this->objFromFixture('Page', 'staff');