Making baked code throw 404 errors when you try to edit, delete, or view records that do not exist. Updated tests.

This commit is contained in:
mark_story 2010-12-12 12:37:02 -05:00
parent 44c080d5ad
commit daffe3adb2
2 changed files with 15 additions and 28 deletions

View file

@ -25,13 +25,9 @@
} }
public function <?php echo $admin ?>view($id = null) { public function <?php echo $admin ?>view($id = null) {
if (!$id) { $this-><?php echo $currentModelName; ?>->id = $id;
<?php if ($wannaUseSession): ?> if (!$this-><?php echo $currentModelName; ?>->exists()) {
$this->Session->setFlash(__('Invalid <?php echo strtolower($singularHumanName) ?>')); throw new NotFoundException(__('Invalid <?php echo strtolower($singularHumanName); ?>'));
$this->redirect(array('action' => 'index'));
<?php else: ?>
$this->flash(__('Invalid <?php echo strtolower($singularHumanName); ?>'), array('action' => 'index'));
<?php endif; ?>
} }
$this->set('<?php echo $singularName; ?>', $this-><?php echo $currentModelName; ?>->read(null, $id)); $this->set('<?php echo $singularName; ?>', $this-><?php echo $currentModelName; ?>->read(null, $id));
} }
@ -72,13 +68,9 @@
<?php $compact = array(); ?> <?php $compact = array(); ?>
public function <?php echo $admin; ?>edit($id = null) { public function <?php echo $admin; ?>edit($id = null) {
if (!$id && empty($this->request->data)) { $this-><?php echo $currentModelName; ?>->id = $id;
<?php if ($wannaUseSession): ?> if (!$this-><?php echo $currentModelName; ?>->exists()) {
$this->Session->setFlash(__('Invalid <?php echo strtolower($singularHumanName); ?>')); throw new NotFoundException(__('Invalid <?php echo strtolower($singularHumanName); ?>'));
$this->redirect(array('action' => 'index'));
<?php else: ?>
$this->flash(__('Invalid <?php echo strtolower($singularHumanName); ?>'), array('action' => 'index'));
<?php endif; ?>
} }
if ($this->request->is('post')) { if ($this->request->is('post')) {
if ($this-><?php echo $currentModelName; ?>->save($this->request->data)) { if ($this-><?php echo $currentModelName; ?>->save($this->request->data)) {
@ -93,8 +85,7 @@
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> could not be saved. Please, try again.')); $this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> could not be saved. Please, try again.'));
<?php endif; ?> <?php endif; ?>
} }
} } else {
if (!$this->request->is('post')) {
$this->data = $this-><?php echo $currentModelName; ?>->read(null, $id); $this->data = $this-><?php echo $currentModelName; ?>->read(null, $id);
} }
<?php <?php
@ -118,15 +109,11 @@
if (!$this->request->is('post')) { if (!$this->request->is('post')) {
throw new MethodNotAllowedException(); throw new MethodNotAllowedException();
} }
if (!$id) { $this-><?php echo $currentModelName; ?>->id = $id;
<?php if ($wannaUseSession): ?> if (!$this-><?php echo $currentModelName; ?>->exists()) {
$this->Session->setFlash(__('Invalid id for <?php echo strtolower($singularHumanName); ?>')); throw new NotFoundException(__('Invalid <?php echo strtolower($singularHumanName); ?>'));
$this->redirect(array('action'=>'index'));
<?php else: ?>
$this->flash(__('Invalid <?php echo strtolower($singularHumanName); ?>'), array('action' => 'index'));
<?php endif; ?>
} }
if ($this-><?php echo $currentModelName; ?>->delete($id)) { if ($this-><?php echo $currentModelName; ?>->delete()) {
<?php if ($wannaUseSession): ?> <?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('<?php echo ucfirst(strtolower($singularHumanName)); ?> deleted')); $this->Session->setFlash(__('<?php echo ucfirst(strtolower($singularHumanName)); ?> deleted'));
$this->redirect(array('action'=>'index')); $this->redirect(array('action'=>'index'));

View file

@ -340,7 +340,7 @@ class ControllerTaskTest extends CakeTestCase {
$this->assertContains("\$this->set('bakeArticles', \$this->paginate());", $result); $this->assertContains("\$this->set('bakeArticles', \$this->paginate());", $result);
$this->assertContains('function view($id = null)', $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("\$this->set('bakeArticle', \$this->BakeArticle->read(null, \$id)", $result);
$this->assertContains('function add()', $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("\$this->Session->setFlash(__('The bake article could not be saved. Please, try again.'));", $result);
$this->assertContains('function delete($id = null)', $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); $this->assertContains("\$this->Session->setFlash(__('Bake article deleted'));", $result);
$result = $this->Task->bakeActions('BakeArticles', 'admin_', true); $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("\$this->set('bakeArticles', \$this->paginate());", $result);
$this->assertContains('function view($id = null)', $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("\$this->set('bakeArticle', \$this->BakeArticle->read(null, \$id)", $result);
$this->assertContains('function add()', $result); $this->assertContains('function add()', $result);
@ -396,7 +396,7 @@ class ControllerTaskTest extends CakeTestCase {
$this->assertContains("\$this->set(compact('bakeTags'))", $result); $this->assertContains("\$this->set(compact('bakeTags'))", $result);
$this->assertContains('function delete($id = null)', $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); $this->assertContains("\$this->flash(__('Bake article deleted'), array('action' => 'index'))", $result);
} }