From 1ce9fc537f921e87212b7e7daa5741e8e0d9771b Mon Sep 17 00:00:00 2001 From: Majna Date: Sun, 21 Apr 2013 23:29:36 +0200 Subject: [PATCH] Pages controller should render 404 on missing view file --- app/Controller/PagesController.php | 12 ++++++++- .../skel/Controller/PagesController.php | 12 ++++++++- .../Case/Controller/PagesControllerTest.php | 26 +++++++++++++++++++ .../test_app/Controller/PagesController.php | 12 ++++++++- 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/app/Controller/PagesController.php b/app/Controller/PagesController.php index fd2398a4d..e1d3eb660 100644 --- a/app/Controller/PagesController.php +++ b/app/Controller/PagesController.php @@ -50,6 +50,8 @@ class PagesController extends AppController { * * @param mixed What page to display * @return void + * @throws NotFoundException When the view file could not be found + * or MissingViewException in debug mode. */ public function display() { $path = func_get_args(); @@ -70,6 +72,14 @@ class PagesController extends AppController { $title_for_layout = Inflector::humanize($path[$count - 1]); } $this->set(compact('page', 'subpage', 'title_for_layout')); - $this->render(implode('/', $path)); + + try { + $this->render(implode('/', $path)); + } catch (MissingViewException $e) { + if (Configure::read('debug')) { + throw $e; + } + throw new NotFoundException(); + } } } diff --git a/lib/Cake/Console/Templates/skel/Controller/PagesController.php b/lib/Cake/Console/Templates/skel/Controller/PagesController.php index dbe9546a3..b4c26fce7 100644 --- a/lib/Cake/Console/Templates/skel/Controller/PagesController.php +++ b/lib/Cake/Console/Templates/skel/Controller/PagesController.php @@ -42,6 +42,8 @@ class PagesController extends AppController { * * @param mixed What page to display * @return void + * @throws NotFoundException When the view file could not be found + * or MissingViewException in debug mode. */ public function display() { $path = func_get_args(); @@ -62,6 +64,14 @@ class PagesController extends AppController { $title_for_layout = Inflector::humanize($path[$count - 1]); } $this->set(compact('page', 'subpage', 'title_for_layout')); - $this->render(implode('/', $path)); + + try { + $this->render(implode('/', $path)); + } catch (MissingViewException $e) { + if (Configure::read('debug')) { + throw $e; + } + throw new NotFoundException(); + } } } diff --git a/lib/Cake/Test/Case/Controller/PagesControllerTest.php b/lib/Cake/Test/Case/Controller/PagesControllerTest.php index 76e5c5c59..89445bd0e 100644 --- a/lib/Cake/Test/Case/Controller/PagesControllerTest.php +++ b/lib/Cake/Test/Case/Controller/PagesControllerTest.php @@ -51,4 +51,30 @@ class PagesControllerTest extends CakeTestCase { $this->assertEquals('TestTheme', $Pages->viewVars['page']); $this->assertEquals('Posts', $Pages->viewVars['subpage']); } + +/** + * Test that missing view renders 404 page in production + * + * @expectedException NotFoundException + * @expectedExceptionCode 404 + * @return void + */ + public function testMissingView() { + Configure::write('debug', 0); + $Pages = new PagesController(new CakeRequest(null, false), new CakeResponse()); + $Pages->display('non_existing_page'); + } + +/** + * Test that missing view in debug mode renders missing_view error page + * + * @expectedException MissingViewException + * @expectedExceptionCode 500 + * @return void + */ + public function testMissingViewInDebug() { + Configure::write('debug', 1); + $Pages = new PagesController(new CakeRequest(null, false), new CakeResponse()); + $Pages->display('non_existing_page'); + } } diff --git a/lib/Cake/Test/test_app/Controller/PagesController.php b/lib/Cake/Test/test_app/Controller/PagesController.php index c1e60e3f1..3cf3c51e7 100644 --- a/lib/Cake/Test/test_app/Controller/PagesController.php +++ b/lib/Cake/Test/test_app/Controller/PagesController.php @@ -51,6 +51,8 @@ class PagesController extends AppController { * * @param mixed What page to display * @return void + * @throws NotFoundException When the view file could not be found + * or MissingViewException in debug mode. */ public function display() { $path = func_get_args(); @@ -75,7 +77,15 @@ class PagesController extends AppController { 'subpage' => $subpage, 'title_for_layout' => $titleForLayout )); - $this->render(implode('/', $path)); + + try { + $this->render(implode('/', $path)); + } catch (MissingViewException $e) { + if (Configure::read('debug')) { + throw $e; + } + throw new NotFoundException(); + } } }