Skip to content

Commit

Permalink
Section parent: only allow objects #1938
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico Hoffmann committed Sep 15, 2019
1 parent 3e63f93 commit 3e42806
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
2 changes: 0 additions & 2 deletions config/sections/files.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

use Kirby\Cms\File;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;

return [
'mixins' => [
Expand Down
10 changes: 8 additions & 2 deletions config/sections/mixins/parent.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use Kirby\Toolkit\Str;

return [
'props' => [
/**
Expand All @@ -22,6 +20,14 @@
if (!$parent) {
throw new Exception('The parent for the query "' . $query . '" cannot be found in the section "' . $this->name() . '"');
}

if (
is_a($parent, 'Kirby\Cms\Page') === false &&
is_a($parent, 'Kirby\Cms\Site') === false &&
is_a($parent, 'Kirby\Cms\User') === false
) {
throw new Exception('The parent for the section "' . $this->name() . '" has to be a page, site or user object');
}
}

if ($parent === null) {
Expand Down
1 change: 0 additions & 1 deletion config/sections/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Kirby\Cms\Blueprint;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;

return [
'mixins' => [
Expand Down
25 changes: 25 additions & 0 deletions tests/Cms/Sections/FilesSectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,31 @@ public function testParent()
$this->assertEquals('pages/b/files', $section->upload()['api']);
}

public function testParentCollectionFail()
{
$this->expectException('Exception');
$this->expectExceptionMessage('The parent for the section "files" has to be a page, site or user object');

$app = new App([
'site' => [
'children' => [
[
'slug' => 'a'
],
[
'slug' => 'b'
]
]
]
]);

$section = new Section('files', [
'model' => $app->page('a'),
'parent' => 'site.index'
]);
$section->parentModel();
}

public function testEmpty()
{
$section = new Section('files', [
Expand Down

0 comments on commit 3e42806

Please sign in to comment.