From f6ce178d858020c923af674b85105a242c0443b7 Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 31 Aug 2009 13:48:32 -0400 Subject: [PATCH] Simplifying message generation. Fixing inflection of model names. Adding tests for flash page generation. Refs #64 --- cake/libs/controller/scaffold.php | 23 +++++------ .../cases/libs/controller/scaffold.test.php | 39 +++++++++++++++++++ 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/cake/libs/controller/scaffold.php b/cake/libs/controller/scaffold.php index afa7e0b35..45c95b7b3 100644 --- a/cake/libs/controller/scaffold.php +++ b/cake/libs/controller/scaffold.php @@ -309,17 +309,13 @@ class Scaffold extends Object { } if (!$this->ScaffoldModel->exists()) { + $message = sprintf(__("Invalid id for %s::edit()", true), Inflector::humanize($this->modelKey)); if ($this->_validSession) { - $this->controller->Session->setFlash(sprintf( - __("Invalid id for %s::edit()", true), - Inflector::humanize($this->modelKey) - )); + $this->controller->Session->setFlash($message); $this->controller->redirect($this->redirect); } else { - return $this->controller->flash(sprintf( - __("Invalid id for %s::edit()", true), - Inflector::humanize($this->modelKey) - ), $this->redirect); + $this->controller->flash($message, $this->redirect); + $this->_output(); } } } @@ -331,14 +327,15 @@ class Scaffold extends Object { if ($this->ScaffoldModel->save($this->controller->data)) { if ($this->controller->_afterScaffoldSave($action)) { + $message = sprintf(__('The %1$s has been %2$s', true), + Inflector::humanize($this->modelKey), + $success + ); if ($this->_validSession) { - $this->controller->Session->setFlash(sprintf( - __('The %1$s has been %2$s', true), - Inflector::humanize($this->modelClass), $success - )); + $this->controller->Session->setFlash($message); $this->controller->redirect($this->redirect); } else { - $this->controller->flash(sprintf(__('The %1$s has been %2$s', true), Inflector::humanize($this->modelClass), $success), $this->redirect); + $this->controller->flash($message, $this->redirect); return $this->_output(); } } else { diff --git a/cake/tests/cases/libs/controller/scaffold.test.php b/cake/tests/cases/libs/controller/scaffold.test.php index 572318c41..ecd3a25bd 100644 --- a/cake/tests/cases/libs/controller/scaffold.test.php +++ b/cake/tests/cases/libs/controller/scaffold.test.php @@ -713,6 +713,45 @@ class ScaffoldTest extends CakeTestCase { $this->assertEqual($result['scaffoldFields'], array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated')); } +/** + * test that scaffold outputs flash messages when sessions are unset. + * + * @return void + **/ + function testScaffoldFlashMessages() { + $this->Controller->action = 'edit'; + $this->Controller->here = '/scaffold_mock'; + $this->Controller->webroot = '/'; + $params = array( + 'plugin' => null, + 'pass' => array(1), + 'form' => array(), + 'named' => array(), + 'url' => array('url' =>'scaffold_mock'), + 'controller' => 'scaffold_mock', + 'action' => 'edit', + ); + //set router. + Router::reload(); + Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/scaffold_mock', 'webroot' => '/'))); + $this->Controller->params = $params; + $this->Controller->controller = 'scaffold_mock'; + $this->Controller->base = '/'; + $this->Controller->data = array( + 'ScaffoldMock' => array( + 'id' => 1, + 'title' => 'New title', + 'body' => 'new body' + ) + ); + $this->Controller->constructClasses(); + unset($this->Controller->Session); + + ob_start(); + new Scaffold($this->Controller, $params); + $result = ob_get_clean(); + $this->assertPattern('/Scaffold Mock has been updated/', $result); + } /** * test that habtm relationship keys get added to scaffoldFields. *