From 88dd3cb8075e6e2ba4b163550a9e4e7d7ebcec9e Mon Sep 17 00:00:00 2001 From: Aaron Carlino Date: Mon, 16 Oct 2017 16:42:57 +1300 Subject: [PATCH] Force SiteTree to check in DB before deleting children --- code/Model/SiteTree.php | 2 +- tests/php/Model/SiteTreeTest.php | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/code/Model/SiteTree.php b/code/Model/SiteTree.php index 49d2c3ad..e6813fa5 100755 --- a/code/Model/SiteTree.php +++ b/code/Model/SiteTree.php @@ -1486,7 +1486,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi 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) { /** @var SiteTree $child */ $child->delete(); diff --git a/tests/php/Model/SiteTreeTest.php b/tests/php/Model/SiteTreeTest.php index 1ad29818..da5cdab2 100644 --- a/tests/php/Model/SiteTreeTest.php +++ b/tests/php/Model/SiteTreeTest.php @@ -26,6 +26,7 @@ use SilverStripe\Versioned\Versioned; use SilverStripe\View\Parsers\Diff; use SilverStripe\View\Parsers\ShortcodeParser; use SilverStripe\View\Parsers\URLSegmentFilter; +use LogicException; class SiteTreeTest extends SapphireTest { @@ -332,6 +333,25 @@ class SiteTreeTest extends SapphireTest $this->assertInstanceOf('Page', $requeriedPage); } + 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');