From daffe3adb27260c2e8b80d09ecc184c219d2fe99 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 12 Dec 2010 12:37:02 -0500 Subject: [PATCH] Making baked code throw 404 errors when you try to edit, delete, or view records that do not exist. Updated tests. --- .../default/actions/controller_actions.ctp | 35 ++++++------------- .../console/shells/tasks/controller.test.php | 8 ++--- 2 files changed, 15 insertions(+), 28 deletions(-) diff --git a/cake/console/templates/default/actions/controller_actions.ctp b/cake/console/templates/default/actions/controller_actions.ctp index 370879e66..b02ad7647 100644 --- a/cake/console/templates/default/actions/controller_actions.ctp +++ b/cake/console/templates/default/actions/controller_actions.ctp @@ -25,13 +25,9 @@ } public function view($id = null) { - if (!$id) { - - $this->Session->setFlash(__('Invalid ')); - $this->redirect(array('action' => 'index')); - - $this->flash(__('Invalid '), array('action' => 'index')); - + $this->->id = $id; + if (!$this->->exists()) { + throw new NotFoundException(__('Invalid ')); } $this->set('', $this->->read(null, $id)); } @@ -72,13 +68,9 @@ public function edit($id = null) { - if (!$id && empty($this->request->data)) { - - $this->Session->setFlash(__('Invalid ')); - $this->redirect(array('action' => 'index')); - - $this->flash(__('Invalid '), array('action' => 'index')); - + $this->->id = $id; + if (!$this->->exists()) { + throw new NotFoundException(__('Invalid ')); } if ($this->request->is('post')) { if ($this->->save($this->request->data)) { @@ -93,8 +85,7 @@ $this->Session->setFlash(__('The could not be saved. Please, try again.')); } - } - if (!$this->request->is('post')) { + } else { $this->data = $this->->read(null, $id); } request->is('post')) { throw new MethodNotAllowedException(); } - if (!$id) { - - $this->Session->setFlash(__('Invalid id for ')); - $this->redirect(array('action'=>'index')); - - $this->flash(__('Invalid '), array('action' => 'index')); - + $this->->id = $id; + if (!$this->->exists()) { + throw new NotFoundException(__('Invalid ')); } - if ($this->->delete($id)) { + if ($this->->delete()) { $this->Session->setFlash(__(' deleted')); $this->redirect(array('action'=>'index')); diff --git a/cake/tests/cases/console/shells/tasks/controller.test.php b/cake/tests/cases/console/shells/tasks/controller.test.php index f3601376e..86ff8fb77 100644 --- a/cake/tests/cases/console/shells/tasks/controller.test.php +++ b/cake/tests/cases/console/shells/tasks/controller.test.php @@ -340,7 +340,7 @@ class ControllerTaskTest extends CakeTestCase { $this->assertContains("\$this->set('bakeArticles', \$this->paginate());", $result); $this->assertContains('function view($id = null)', $result); - $this->assertContains("\$this->Session->setFlash(__('Invalid bake article'));", $result); + $this->assertContains("throw new NotFoundException(__('Invalid bake article'));", $result); $this->assertContains("\$this->set('bakeArticle', \$this->BakeArticle->read(null, \$id)", $result); $this->assertContains('function add()', $result); @@ -352,7 +352,7 @@ class ControllerTaskTest extends CakeTestCase { $this->assertContains("\$this->Session->setFlash(__('The bake article could not be saved. Please, try again.'));", $result); $this->assertContains('function delete($id = null)', $result); - $this->assertContains('if ($this->BakeArticle->delete($id))', $result); + $this->assertContains('if ($this->BakeArticle->delete())', $result); $this->assertContains("\$this->Session->setFlash(__('Bake article deleted'));", $result); $result = $this->Task->bakeActions('BakeArticles', 'admin_', true); @@ -382,7 +382,7 @@ class ControllerTaskTest extends CakeTestCase { $this->assertContains("\$this->set('bakeArticles', \$this->paginate());", $result); $this->assertContains('function view($id = null)', $result); - $this->assertContains("\$this->flash(__('Invalid bake article'), array('action' => 'index'))", $result); + $this->assertContains("throw new NotFoundException(__('Invalid bake article'));", $result); $this->assertContains("\$this->set('bakeArticle', \$this->BakeArticle->read(null, \$id)", $result); $this->assertContains('function add()', $result); @@ -396,7 +396,7 @@ class ControllerTaskTest extends CakeTestCase { $this->assertContains("\$this->set(compact('bakeTags'))", $result); $this->assertContains('function delete($id = null)', $result); - $this->assertContains('if ($this->BakeArticle->delete($id))', $result); + $this->assertContains('if ($this->BakeArticle->delete())', $result); $this->assertContains("\$this->flash(__('Bake article deleted'), array('action' => 'index'))", $result); }