diff --git a/src/Cms/App.php b/src/Cms/App.php index 4468601a67..eba05e49d8 100644 --- a/src/Cms/App.php +++ b/src/Cms/App.php @@ -4,6 +4,7 @@ use Kirby\Data\Data; use Kirby\Email\PHPMailer as Emailer; +use Kirby\Exception\ErrorPageException; use Kirby\Exception\InvalidArgumentException; use Kirby\Exception\NotFoundException; use Kirby\Http\Request; @@ -586,7 +587,11 @@ public function io($input) // Pages if (is_a($input, 'Kirby\Cms\Page')) { - $html = $input->render(); + try { + $html = $input->render(); + } catch (ErrorPageException $e) { + return $this->io($e); + } if ($input->isErrorPage() === true) { if ($response->code() === null) { diff --git a/tests/Cms/App/AppIoTest.php b/tests/Cms/App/AppIoTest.php index b72ea02e8c..1036d6b443 100644 --- a/tests/Cms/App/AppIoTest.php +++ b/tests/Cms/App/AppIoTest.php @@ -2,7 +2,7 @@ namespace Kirby\Cms; -use Exception; +use Kirby\Exception\Exception; use Kirby\Http\Response; use PHPUnit\Framework\TestCase; @@ -12,19 +12,42 @@ public function app() { return new App([ 'roots' => [ - 'index' => '/dev/null' + 'index' => '/dev/null', + 'templates' => __DIR__ . '/fixtures/AppIoTest/templates' ] ]); } public function testException() { - $response = $this->app()->io(new Exception('Nope')); + $response = $this->app()->io(new Exception([ + 'fallback' => 'Nope', + 'httpCode' => 501 + ])); - $this->assertEquals(500, $response->code()); + $this->assertEquals(501, $response->code()); $this->assertEquals('Nope', $response->body()); } + public function testExceptionErrorPage() + { + $app = $this->app()->clone([ + 'site' => [ + 'children' => [ + [ + 'slug' => 'error', + 'template' => 'error' + ] + ] + ] + ]); + + $response = $app->io(new Exception('Nope')); + + $this->assertEquals(500, $response->code()); + $this->assertEquals('Error: Nope', $response->body()); + } + public function testExceptionWithInvalidHttpCode() { $response = $this->app()->io(new Exception('Nope', 8000)); @@ -64,6 +87,56 @@ public function testResponse() $this->assertEquals($input, $response); } + public function testPage() + { + $input = new Page([ + 'slug' => 'test', + 'template' => 'test' + ]); + + $response = $this->app()->io($input); + + $this->assertEquals(200, $response->code()); + $this->assertEquals('Test template', $response->body()); + } + + public function testPageErrorPageException() + { + $input = new Page([ + 'slug' => 'test', + 'template' => 'errorpage-exception' + ]); + + $response = $this->app()->io($input); + + $this->assertEquals(403, $response->code()); + $this->assertEquals('Exception message', $response->body()); + } + + public function testPageErrorPageExceptionErrorPage() + { + $app = $this->app()->clone([ + 'site' => [ + 'children' => [ + [ + 'slug' => 'error', + 'template' => 'error' + ] + ] + ] + ]); + + $input = new Page([ + 'slug' => 'test', + 'template' => 'errorpage-exception' + ]); + + $response = $app->io($input); + + $this->assertEquals(403, $response->code()); + $this->assertEquals('Error: Exception message', $response->body()); + } + public function testString() { $response = $this->app()->io('Test'); diff --git a/tests/Cms/App/fixtures/AppIoTest/templates/error.php b/tests/Cms/App/fixtures/AppIoTest/templates/error.php new file mode 100644 index 0000000000..36e326d6c9 --- /dev/null +++ b/tests/Cms/App/fixtures/AppIoTest/templates/error.php @@ -0,0 +1 @@ +Error: \ No newline at end of file diff --git a/tests/Cms/App/fixtures/AppIoTest/templates/errorpage-exception.php b/tests/Cms/App/fixtures/AppIoTest/templates/errorpage-exception.php new file mode 100644 index 0000000000..54d2f25af6 --- /dev/null +++ b/tests/Cms/App/fixtures/AppIoTest/templates/errorpage-exception.php @@ -0,0 +1,6 @@ + 'Exception message', + 'httpCode' => 403 +]); diff --git a/tests/Cms/App/fixtures/AppIoTest/templates/test.php b/tests/Cms/App/fixtures/AppIoTest/templates/test.php new file mode 100644 index 0000000000..518887dfd5 --- /dev/null +++ b/tests/Cms/App/fixtures/AppIoTest/templates/test.php @@ -0,0 +1 @@ +Test template \ No newline at end of file