updating View and Controller::flash, fixes #3749, tests added

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6177 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-12-20 01:19:19 +00:00
parent 1752d936d9
commit 60f4dd360d
4 changed files with 125 additions and 38 deletions

View file

@ -740,19 +740,11 @@ class Controller extends Object {
*/
function flash($message, $url, $pause = 1) {
$this->autoRender = false;
$this->autoLayout = false;
$this->set('url', Router::url($url));
$this->set('message', $message);
$this->set('pause', $pause);
$this->set('page_title', $message);
if (file_exists(VIEWS . 'layouts' . DS . 'flash.ctp')) {
$flash = VIEWS . 'layouts' . DS . 'flash.ctp';
} elseif (file_exists(VIEWS . 'layouts' . DS . 'flash.thtml')) {
$flash = VIEWS . 'layouts' . DS . 'flash.thtml';
} elseif ($flash = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . "layouts" . DS . 'flash.ctp')) {
}
$this->render(null, false, $flash);
$this->render(false, 'flash');
}
/**
* Converts POST'ed model data to a model conditions array, suitable for a find

View file

@ -172,12 +172,18 @@ class View extends Object {
* @access public
*/
var $cacheAction = false;
/**
* holds current errors for the model validation
*
* @var array
*/
var $validationErrors = array();
/**
* True when the view has been rendered.
*
* @var boolean
*/
var $hasRendered = null;
var $hasRendered = false;
/**
* Array of loaded view helpers.
*
@ -314,15 +320,11 @@ class View extends Object {
*/
function render($action = null, $layout = null, $file = null) {
if (isset($this->hasRendered) && $this->hasRendered) {
if ($this->hasRendered) {
return true;
} else {
$this->hasRendered = false;
}
if (!$action) {
$action = $this->action;
}
$out = null;
if ($layout === null) {
$layout = $this->layout;
@ -332,32 +334,32 @@ class View extends Object {
$action = $file;
}
$viewFileName = $this->_getViewFileName($action);
if ($viewFileName && !$this->hasRendered) {
if ($action !== false) {
$viewFileName = $this->_getViewFileName($action);
if (substr($viewFileName, -3) === 'ctp' || substr($viewFileName, -5) === 'thtml') {
$out = View::_render($viewFileName, $this->viewVars);
} else {
$out = $this->_render($viewFileName, $this->viewVars);
}
if ($out !== false) {
if ($layout && $this->autoLayout) {
$out = $this->renderLayout($out, $layout);
if (isset($this->loaded['cache']) && (($this->cacheAction != false)) && (Configure::read('Cache.check') === true)) {
$replace = array('<cake:nocache>', '</cake:nocache>');
$out = str_replace($replace, '', $out);
}
}
print $out;
$this->hasRendered = true;
} else {
$out = $this->_render($viewFileName, $this->viewVars);
trigger_error(sprintf(__("Error in view %s, got: <blockquote>%s</blockquote>", true), $viewFileName, $out), E_USER_ERROR);
}
return true;
}
if ($out !== false) {
if ($layout && $this->autoLayout) {
$out = $this->renderLayout($out, $layout);
if (isset($this->loaded['cache']) && (($this->cacheAction != false)) && (Configure::read('Cache.check') === true)) {
$replace = array('<cake:nocache>', '</cake:nocache>');
$out = str_replace($replace, '', $out);
}
}
print $out;
$this->hasRendered = true;
} else {
$out = $this->_render($viewFileName, $this->viewVars);
trigger_error(sprintf(__("Error in view %s, got: <blockquote>%s</blockquote>", true), $viewFileName, $out), E_USER_ERROR);
}
return true;
}
/**
* Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string.
@ -762,6 +764,7 @@ class View extends Object {
*/
function _getViewFileName($name = null) {
$subDir = null;
if (!is_null($this->webservices)) {
$subDir = strtolower($this->webservices) . DS;
}

View file

@ -102,6 +102,32 @@ class ControllerTest extends CakeTestCase {
$this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
$this->assertEqual($results, array(1, 2, 3));
}
function testFlash() {
$Controller =& new Controller();
ob_start();
$Controller->flash('this should work', '/flash');
$result = ob_get_clean();
$expected = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>this should work</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style><!--
P { text-align:center; font:bold 1.1em sans-serif }
A { color:#444; text-decoration:none }
A:HOVER { text-decoration: underline; color:#44E }
--></style>
</head>
<body>
<p><a href="/flash">this should work</a></p>
</body>
</html>';
$result = str_replace(array("\t", "\r\n", "\n"), "", $result);
$expected = str_replace(array("\t", "\r\n", "\n"), "", $expected);
$this->assertEqual($result, $expected);
}
}
?>

View file

@ -115,7 +115,6 @@ class ViewTest extends UnitTestCase {
$result = $View->getViewFileName('/posts/index');
$this->assertEqual($result, $expected);
$expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'layouts' . DS .'default.ctp';
$result = $View->getLayoutFileName();
$this->assertEqual($result, $expected);
@ -237,7 +236,7 @@ class ViewTest extends UnitTestCase {
$this->assertTrue(is_object($result['TestPluginHelper']->TestPluginOtherHelper));
}
function testRender() {
function testRenderLoadHelper() {
$this->PostsController->helpers = array('Html', 'Form', 'Ajax');
$View = new TestView($this->PostsController);
@ -262,7 +261,74 @@ class ViewTest extends UnitTestCase {
$this->assertTrue(is_object($helpers['form']->Html));
$this->assertTrue(is_object($helpers['ajax']->Html));
$this->assertTrue(is_object($helpers['testPluginHelper']->TestPluginOtherHelper));
}
function testRender() {
$View = new TestView($this->PostsController);
ob_start();
$View->render('index');
$result = ob_get_clean();
//pr($result);
$expected = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
CakePHP: the rapid development php framework: Posts </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="css/cake.generic.css" /> </head>
<body>
<div id="container">
<div id="header">
<h1><a href="http://cakephp.org">CakePHP: the rapid development php framework</a></h1>
</div>
<div id="content">
posts index
</div>
<div id="footer">
<a href="http://www.cakephp.org/" target="_new"><img src="img/cake.power.gif" alt="CakePHP: the rapid development php framework" border="0" /></a> </div>
</div>
</body>
</html>
';
$result = str_replace(array("\t", "\r\n", "\n"), "", $result);
$expected = str_replace(array("\t", "\r\n", "\n"), "", $expected);
$this->assertEqual($result, $expected);
$this->PostsController->set('url', 'flash');
$this->PostsController->set('message', 'yo what up');
$this->PostsController->set('pause', 3);
$this->PostsController->set('page_title', 'yo what up');
$View = new TestView($this->PostsController);
ob_start();
$View->render(false, 'flash');
$result = ob_get_clean();
$expected = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>yo what up</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style><!--
P { text-align:center; font:bold 1.1em sans-serif }
A { color:#444; text-decoration:none }
A:HOVER { text-decoration: underline; color:#44E }
--></style>
</head>
<body>
<p><a href="flash">yo what up</a></p>
</body>
</html>';
$result = str_replace(array("\t", "\r\n", "\n"), "", $result);
$expected = str_replace(array("\t", "\r\n", "\n"), "", $expected);
$this->assertEqual($result, $expected);
}
function tearDown() {