Skip to content

Commit a678a60

Browse files
committed
Simplifying message generation.
Fixing inflection of model names. Adding tests for flash page generation. Refs #64
1 parent c0ba43c commit a678a60

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

cake/libs/controller/scaffold.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,12 @@ function __scaffoldSave($params = array(), $action = 'edit') {
267267
}
268268

269269
if (!$this->ScaffoldModel->exists()) {
270+
$message = sprintf(__("Invalid id for %s::edit()", true), Inflector::humanize($this->modelKey));
270271
if (isset($this->controller->Session) && $this->controller->Session->valid() != false) {
271-
$this->controller->Session->setFlash(sprintf(__("Invalid id for %s::edit()", true), Inflector::humanize($this->modelKey)));
272+
$this->controller->Session->setFlash($message);
272273
$this->controller->redirect($this->redirect);
273274
} else {
274-
return $this->controller->flash(sprintf(__("Invalid id for %s::edit()", true), Inflector::humanize($this->modelKey)), $this->redirect);
275+
return $this->controller->flash($message, $this->redirect);
275276
}
276277
}
277278
}
@@ -283,11 +284,15 @@ function __scaffoldSave($params = array(), $action = 'edit') {
283284

284285
if ($this->ScaffoldModel->save($this->controller->data)) {
285286
if ($this->controller->_afterScaffoldSave($action)) {
287+
$message = sprintf(__('The %1$s has been %2$s', true),
288+
Inflector::humanize($this->modelKey),
289+
$success
290+
);
286291
if (isset($this->controller->Session) && $this->controller->Session->valid() != false) {
287-
$this->controller->Session->setFlash(sprintf(__('The %1$s has been %2$s', true), Inflector::humanize($this->modelClass), $success));
292+
$this->controller->Session->setFlash($message);
288293
$this->controller->redirect($this->redirect);
289294
} else {
290-
$this->controller->flash(sprintf(__('The %1$s has been %2$s', true), Inflector::humanize($this->modelClass), $success), $this->redirect);
295+
$this->controller->flash($message, $this->redirect);
291296
return $this->_output();
292297
}
293298
} else {

cake/tests/cases/libs/controller/scaffold.test.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,45 @@ function testScaffoldVariableSetting() {
676676
$this->assertEqual($result['pluralVar'], 'scaffoldMock');
677677
$this->assertEqual($result['scaffoldFields'], array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated'));
678678
}
679+
/**
680+
* test that scaffold outputs flash messages when sessions are unset.
681+
*
682+
* @return void
683+
**/
684+
function testScaffoldFlashMessages() {
685+
$this->Controller->action = 'edit';
686+
$this->Controller->here = '/scaffold_mock';
687+
$this->Controller->webroot = '/';
688+
$params = array(
689+
'plugin' => null,
690+
'pass' => array(1),
691+
'form' => array(),
692+
'named' => array(),
693+
'url' => array('url' =>'scaffold_mock'),
694+
'controller' => 'scaffold_mock',
695+
'action' => 'edit',
696+
);
697+
//set router.
698+
Router::reload();
699+
Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/scaffold_mock', 'webroot' => '/')));
700+
$this->Controller->params = $params;
701+
$this->Controller->controller = 'scaffold_mock';
702+
$this->Controller->base = '/';
703+
$this->Controller->data = array(
704+
'ScaffoldMock' => array(
705+
'id' => 1,
706+
'title' => 'New title',
707+
'body' => 'new body'
708+
)
709+
);
710+
$this->Controller->constructClasses();
711+
unset($this->Controller->Session);
712+
713+
ob_start();
714+
new Scaffold($this->Controller, $params);
715+
$result = ob_get_clean();
716+
$this->assertPattern('/Scaffold Mock has been updated/', $result);
717+
}
679718
/**
680719
* test that habtm relationship keys get added to scaffoldFields.
681720
*

0 commit comments

Comments
 (0)