Skip to content

Commit 09a4ce5

Browse files
fix: Throw NotFoundException for missing sections in section controllers
1 parent 8a17550 commit 09a4ce5

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/Panel/Controller/SectionController.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Kirby\Blueprint\Section;
66
use Kirby\Cms\Find;
7+
use Kirby\Cms\ModelWithContent;
8+
use Kirby\Exception\NotFoundException;
79
use Kirby\Http\Router;
810
use Kirby\Panel\Area;
911

@@ -36,18 +38,36 @@ public static function factory(
3638
// for page/user/site section dialogs
3739
if ($path === null) {
3840
return new static(
39-
section: Find::parent($model)->blueprint()->section($filename),
41+
section: static::findSection(Find::parent($model), $filename),
4042
path: $section
4143
);
4244
}
4345

4446
// for file section dialogs
4547
return new static(
46-
section: Find::file($model, $filename)->blueprint()->section($section),
48+
section: static::findSection(Find::file($model, $filename), $section),
4749
path: $path
4850
);
4951
}
5052

53+
/**
54+
* @throws NotFoundException If the section cannot be found
55+
*/
56+
protected static function findSection(
57+
ModelWithContent $model,
58+
string $name
59+
): Section {
60+
$section = $model->blueprint()->section($name);
61+
62+
if ($section === null) {
63+
throw new NotFoundException(
64+
message: 'The section "' . $name . '" could not be found'
65+
);
66+
}
67+
68+
return $section;
69+
}
70+
5171
public function load(): mixed
5272
{
5373
return Router::execute($this->path, 'GET', $this->routes());

tests/Panel/Controller/Dialog/SectionDialogControllerTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use Kirby\Blueprint\Section;
77
use Kirby\Cms\Page;
8+
use Kirby\Exception\NotFoundException;
89
use Kirby\Panel\TestCase;
910
use PHPUnit\Framework\Attributes\CoversClass;
1011

@@ -107,6 +108,28 @@ public function testFactoryForFile(): void
107108
$this->assertSame('test', $controller->path);
108109
}
109110

111+
public function testFactoryForMissingSection(): void
112+
{
113+
$this->app = $this->app->clone([
114+
'site' => [
115+
'children' => [
116+
['slug' => 'test']
117+
]
118+
]
119+
]);
120+
121+
$this->app->impersonate('kirby');
122+
123+
$this->expectException(NotFoundException::class);
124+
$this->expectExceptionMessage('The section "test" could not be found');
125+
126+
SectionDialogController::factory(
127+
model: 'pages/test',
128+
filename: 'test',
129+
section: 'test'
130+
);
131+
}
132+
110133
public function testLoad(): void
111134
{
112135
$section = $this->section();

0 commit comments

Comments
 (0)