From 53a0206b1d1216bde067435d0c58fb905a19fe51 Mon Sep 17 00:00:00 2001 From: Nic Horstmeier Date: Fri, 28 Jul 2017 14:02:29 -0500 Subject: [PATCH] BUGFIX check if parent context is SiteTree instance fixes #1913 --- code/Model/SiteTree.php | 5 +++-- tests/model/SiteTreeTest.php | 4 ++++ tests/model/SiteTreeTest.yml | 5 +++++ tests/model/SiteTreeTest_DataObject.php | 28 +++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 tests/model/SiteTreeTest_DataObject.php diff --git a/code/Model/SiteTree.php b/code/Model/SiteTree.php index 66cd0756..754899f4 100755 --- a/code/Model/SiteTree.php +++ b/code/Model/SiteTree.php @@ -1155,7 +1155,8 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi // Check parent (custom canCreate option for SiteTree) // Block children not allowed for this parent type $parent = isset($context['Parent']) ? $context['Parent'] : null; - if ($parent && !in_array(static::class, $parent->allowedChildren())) { + $strictParentInstance = ($parent && $parent instanceof SiteTree); + if ($strictParentInstance && !in_array(static::class, $parent->allowedChildren())) { return false; } @@ -1171,7 +1172,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi } // Fall over to inherited permissions - if ($parent && $parent->exists()) { + if ($strictParentInstance && $parent->exists()) { return $parent->canAddChildren($member); } else { // This doesn't necessarily mean we are creating a root page, but that diff --git a/tests/model/SiteTreeTest.php b/tests/model/SiteTreeTest.php index 2e31fe8e..c74c91aa 100644 --- a/tests/model/SiteTreeTest.php +++ b/tests/model/SiteTreeTest.php @@ -50,6 +50,7 @@ class SiteTreeTest extends SapphireTest SiteTreeTest_ClassCext::class, SiteTreeTest_NotRoot::class, SiteTreeTest_StageStatusInherit::class, + SiteTreeTest_DataObject::class, ); public function testCreateDefaultpages() @@ -632,6 +633,9 @@ class SiteTreeTest extends SapphireTest // Test creation underneath a parent which doesn't exist in the database. This should // fall back to checking whether the user can create pages at the root of the site $this->assertTrue(singleton(SiteTree::class)->canCreate(null, array('Parent' => singleton(SiteTree::class)))); + + //Test we don't check for allowedChildren on parent context if it's not SiteTree instance + $this->assertTrue(singleton(SiteTree::class)->canCreate(null, ['Parent' => $this->objFromFixture('SiteTreeTest_DataObject', 'relations')])); } public function testEditPermissionsOnDraftVsLive() diff --git a/tests/model/SiteTreeTest.yml b/tests/model/SiteTreeTest.yml index 94abe0bb..68a4fff6 100755 --- a/tests/model/SiteTreeTest.yml +++ b/tests/model/SiteTreeTest.yml @@ -123,3 +123,8 @@ SilverStripe\CMS\Model\RedirectorPage: URLSegment: external RedirectionType: External ExternalURL: "http://www.google.com?a&b" + +SiteTreeTest_DataObject: + relations: + Title: 'Linked DataObject' + Pages: =>Page.home,=>Page.about,=>Page.staff \ No newline at end of file diff --git a/tests/model/SiteTreeTest_DataObject.php b/tests/model/SiteTreeTest_DataObject.php new file mode 100644 index 00000000..29ca4080 --- /dev/null +++ b/tests/model/SiteTreeTest_DataObject.php @@ -0,0 +1,28 @@ + 'Varchar', + ]; + + /** + * @var array + */ + private static $many_many = [ + 'Pages' => SiteTree::class, + ]; +}