From 67bbb0e93eb26bb6510bbe5066a5e496166da488 Mon Sep 17 00:00:00 2001
From: gwoo
%s", true), $viewFileName, $out), E_USER_ERROR); - } - return true; - } - } -/** - * Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string. - * - * This realizes the concept of Elements, (or "partial layouts") - * and the $params array is used to send data to be used in the - * Element. - * - * @link - * @param string $name Name of template file in the/app/views/elements/ folder - * @param array $params Array of data to be made available to the for rendered view (i.e. the Element) - * @return string Rendered output - */ - function renderElement($name, $params = array(), $loadHelpers = false) { - - if (isset($params['plugin'])) { - $this->plugin = $params['plugin']; - $this->pluginPath = 'plugins' . DS . $this->plugin . DS; - $this->pluginPaths = array( - VIEWS . $this->pluginPath, - APP . $this->pluginPath . 'views' . DS, - ); - } - - $paths = Configure::getInstance(); - $viewPaths = array_merge($this->pluginPaths, $paths->viewPaths); - - $file = null; - foreach ($viewPaths as $path) { - if (file_exists($path . 'elements' . DS . $name . $this->ext)) { - $file = $path . 'elements' . DS . $name . $this->ext; - break; - } elseif (file_exists($path . 'elements' . DS . $name . '.thtml')) { - $file = $path . 'elements' . DS . $name . '.thtml'; - break; - } - } - - if (is_null($file)) { - $file = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . 'elements' . DS . $name. '.ctp'); - } - - if ($file) { - $params = array_merge_recursive($params, $this->loaded); - return $this->_render($file, array_merge($this->viewVars, $params), $loadHelpers); - } - - if (!is_null($this->pluginPath)) { - $file = APP . $this->pluginPath . 'views' . DS . 'elements' . DS . $name . $this->ext; - } else { - $file = VIEWS . 'elements' . DS . $name . $this->ext; - } - - if (Configure::read() > 0) { - return "Not Found: " . $file; - } - } - /** * Wrapper for View::renderElement(); * @@ -442,20 +304,113 @@ class View extends Object { } return $this->renderElement($name, $params); } +/** + * Renders view for given action and layout. If $file is given, that is used + * for a view filename (e.g. customFunkyView.ctp). + * + * @param string $action Name of action to render for + * @param string $layout Layout to use + * @param string $file Custom filename for view + */ + function render($action = null, $layout = null, $file = null) { + + if (isset($this->hasRendered) && $this->hasRendered) { + return true; + } else { + $this->hasRendered = false; + } + + if (!$action) { + $action = $this->action; + } + + if ($layout === null) { + $layout = $this->layout; + } + + if ($file != null) { + $action = $file; + } + + $viewFileName = $this->_getViewFileName($action); + + if ($viewFileName && !$this->hasRendered) { + 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 ($this->layout && $this->autoLayout) { + $out = $this->renderLayout($out, $layout); + if (isset($this->loaded['cache']) && (($this->cacheAction != false)) && (Configure::read('Cache.check') === true)) { + $replace = array('
%s", true), $viewFileName, $out), E_USER_ERROR); + } + return true; + } + } +/** + * Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string. + * + * This realizes the concept of Elements, (or "partial layouts") + * and the $params array is used to send data to be used in the + * Element. + * + * @link + * @param string $name Name of template file in the/app/views/elements/ folder + * @param array $params Array of data to be made available to the for rendered view (i.e. the Element) + * @return string Rendered output + */ + function renderElement($name, $params = array(), $loadHelpers = false) { + $file = $plugin = false; + if (isset($params['plugin'])) { + $plugin = $params['plugin']; + } + + $paths = $this->_paths($plugin); + foreach ($paths as $path) { + if (file_exists($path . 'elements' . DS . $name . $this->ext)) { + $file = $path . 'elements' . DS . $name . $this->ext; + break; + } elseif (file_exists($path . 'elements' . DS . $name . '.thtml')) { + $file = $path . 'elements' . DS . $name . '.thtml'; + break; + } + } + + if (is_file($file)) { + $params = array_merge_recursive($params, $this->loaded); + return $this->_render($file, array_merge($this->viewVars, $params), $loadHelpers); + } + + $file = $paths[0] . 'views' . DS . 'elements' . DS . $name . $this->ext; + if (Configure::read() > 0) { + return "Not Found: " . $file; + } + } /** * Renders a layout. Returns output from _render(). Returns false on error. * * @param string $content_for_layout Content to render in a view, wrapped by the surrounding layout. * @return mixed Rendered output, or false on error */ - function renderLayout($content_for_layout) { - $layout_fn = $this->_getLayoutFileName(); + function renderLayout($content_for_layout, $layout = null) { + $layout_fn = $this->_getLayoutFileName($layout); + $debug = ''; if (Configure::read() > 2 && isset($this->viewVars['cakeDebug'])) { $debug = View::_render(LIBS . 'view' . DS . 'templates' . DS . 'elements' . DS . 'dump.ctp', array('controller' => $this->viewVars['cakeDebug']), false); unset($this->viewVars['cakeDebug']); - } else { - $debug = ''; } if ($this->pageTitle !== false) { @@ -473,35 +428,56 @@ class View extends Object { ) ); - if (is_file($layout_fn)) { - if (empty($this->loaded) && !empty($this->helpers)) { - $loadHelpers = true; - } else { - $loadHelpers = false; - $data_for_layout = array_merge($data_for_layout, $this->loaded); - } - - if (substr($layout_fn, -3) === 'ctp' || substr($layout_fn, -5) === 'thtml') { - $out = View::_render($layout_fn, $data_for_layout, $loadHelpers, true); - } else { - $out = $this->_render($layout_fn, $data_for_layout, $loadHelpers); - } - - if ($out === false) { - $out = $this->_render($layout_fn, $data_for_layout); - trigger_error(sprintf(__("Error in layout %s, got:
%s", true), $layout_fn, $out), E_USER_ERROR); - return false; - } else { - return $out; - } + if (empty($this->loaded) && !empty($this->helpers)) { + $loadHelpers = true; } else { - return $this->cakeError('missingLayout', array( - array( - 'layout' => $this->layout, - 'file' => $layout_fn, - 'base' => $this->base - ) - )); + $loadHelpers = false; + $data_for_layout = array_merge($data_for_layout, $this->loaded); + } + + if (substr($layout_fn, -3) === 'ctp' || substr($layout_fn, -5) === 'thtml') { + $out = View::_render($layout_fn, $data_for_layout, $loadHelpers, true); + } else { + $out = $this->_render($layout_fn, $data_for_layout, $loadHelpers); + } + + if ($out === false) { + $out = $this->_render($layout_fn, $data_for_layout); + trigger_error(sprintf(__("Error in layout %s, got:
%s", true), $layout_fn, $out), E_USER_ERROR); + return false; + } + + return $out; + } +/** + * Render cached view + * + * @param string $filename the cache file to include + * @param string $timeStart the page render start time + */ + function renderCache($filename, $timeStart) { + ob_start(); + include ($filename); + + if (Configure::read() > 0 && $this->layout != 'xml') { + echo ""; + } + + $out = ob_get_clean(); + + if (preg_match('/^/', $out, $match)) { + if (time() >= $match['1']) { + @unlink($filename); + unset ($out); + return; + } else { + if ($this->layout === 'xml') { + header('Content-type: text/xml'); + } + $out = str_replace('', '', $out); + e($out); + die(); + } } } /** @@ -582,7 +558,6 @@ class View extends Object { * @return unknown */ function set($one, $two = null) { - $data = null; if (is_array($one)) { if (is_array($two)) { @@ -617,7 +592,7 @@ class View extends Object { function error($code, $name, $message) { header ("HTTP/1.1 {$code} {$name}"); print ($this->_render( - LAYOUTS . 'error.ctp', + $this->_getLayoutFileName('error'), array( 'code' => $code, 'name' => $name, @@ -626,111 +601,6 @@ class View extends Object { )); } -/************************************************************************** - * Private methods. - *************************************************************************/ - -/** - * Returns filename of given action's template file (.ctp) as a string. CamelCased action names will be under_scored! This means that you can have LongActionNames that refer to long_action_names.ctp views. - * - * @param string $action Controller action to find template filename for - * @return string Template filename - * @access private - */ - function _getViewFileName($action) { - $action = Inflector::underscore($action); - - if (!is_null($this->webservices)) { - $type = strtolower($this->webservices) . DS; - } else { - $type = null; - } - - $position = strpos($action, '..'); - if ($position !== false) { - $action = explode('/', $action); - $i = array_search('..', $action); - unset($action[$i - 1]); - unset($action[$i]); - $action = '..' . DS . implode(DS, $action); - } - - $paths = Configure::getInstance(); - $viewPaths = array_merge($this->pluginPaths, $paths->viewPaths); - - $name = $this->viewPath . DS . $this->subDir . $type . $action; - foreach ($viewPaths as $path) { - if (file_exists($path . $name . $this->ext)) { - return $path . $name . $this->ext; - } elseif (file_exists($path . $name . '.thtml')) { - return $path . $name . '.thtml'; - } - } - - if ($viewFileName = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . 'errors' . DS . $type . $action . '.ctp')) { - return $viewFileName; - } elseif ($viewFileName = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . $this->viewPath . DS . $type . $action . '.ctp')) { - return $viewFileName; - } else { - if (!is_null($this->pluginPath)) { - $viewFileName = APP . $this->pluginPath . 'views' . DS . $name . $this->ext; - } else { - $viewFileName = VIEWS . $name . $this->ext; - } - $this->_missingView($viewFileName, $action); - } - return false; - } - -/** - * Returns layout filename for this template as a string. - * - * @return string Filename for layout file (.ctp). - * @access private - */ - function _getLayoutFileName() { - if (isset($this->webservices) && !is_null($this->webservices)) { - $type = strtolower($this->webservices) . DS; - } else { - $type = null; - } - - if (!is_null($this->layoutPath)) { - $type = $this->layoutPath . DS; - } - - $paths = Configure::getInstance(); - $viewPaths = array_merge($this->pluginPaths, $paths->viewPaths); - - $name = $this->subDir . $type . $this->layout; - foreach ($viewPaths as $path) { - if (file_exists($path . 'layouts' . DS . $name . $this->ext)) { - return $path . 'layouts' . DS . $name . $this->ext; - } elseif (file_exists($path . 'layouts' . DS . $name . '.thtml')) { - return $path . 'layouts' . DS . $name . '.thtml'; - } - } - - if (!is_null($this->pluginPath)) { - $layoutFileName = APP . $this->pluginPath . 'views' . DS . 'layouts' . DS . $name . $this->ext; - } else { - $layoutFileName = VIEWS . 'layouts' . DS . $name . $this->ext; - } - - $default = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . 'layouts' . DS . $type . $this->layout . '.ctp'); - if (empty($default) && !empty($type)) { - $default = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . 'layouts' . DS . $type . 'default.ctp'); - } - if (empty($default)) { - $default = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . 'layouts' . DS . $this->layout . '.ctp'); - } - - if (!empty($default)) { - return $default; - } - return $layoutFileName; - } - /** * Renders and returns output for given view filename with its * array of data. @@ -738,7 +608,7 @@ class View extends Object { * @param string $___viewFn Filename of the view * @param array $___dataForView Data to include in rendered view * @return string Rendered output - * @access private + * @access protected */ function _render($___viewFn, $___dataForView, $loadHelpers = true, $cached = false) { if ($this->helpers != false && $loadHelpers === true) { @@ -882,35 +752,90 @@ class View extends Object { return $loaded; } /** - * Render cached view + * Returns filename of given action's template file (.ctp) as a string. + * CamelCased action names will be under_scored! This means that you can have + * LongActionNames that refer to long_action_names.ctp views. * - * @param string $filename the cache file to include - * @param string $timeStart the page render start time + * @param string $action Controller action to find template filename for + * @return string Template filename + * @access protected */ - function renderCache($filename, $timeStart) { - ob_start(); - include ($filename); - - if (Configure::read() > 0 && $this->layout != 'xml') { - echo ""; + function _getViewFileName($name = null) { + $subDir = null; + if (!is_null($this->webservices)) { + $subDir = strtolower($this->webservices) . DS; + } + if (!is_null($this->subDir)) { + $subDir = $this->subDir . DS; } - $out = ob_get_clean(); + if($name === null) { + $name = $this->action; + } - if (preg_match('/^/', $out, $match)) { - if (time() >= $match['1']) { - @unlink($filename); - unset ($out); - return; - } else { - if ($this->layout === 'xml') { - header('Content-type: text/xml'); + if(strpos($name, '/') === false && strpos($name, '..') === false) { + $name = $this->viewPath . DS . $subDir . Inflector::underscore($name); + } elseif (strpos($name, '/') !== false) { + if($name{0} === '/') { + if (is_file($name)) { + return $name; } - $out = str_replace('', '', $out); - e($out); - die(); + $name = trim($name, '/'); + if(DS !== '/') { + $name = implode(DS, explode('/', $name)); + } + } else { + if (is_file($name)) { + return $name; + } + return $this->_missingView($name, 'missingView'); + } + } elseif (strpos($name, '..') !== false) { + $name = explode('/', $name); + $i = array_search('..', $name); + unset($name[$i - 1]); + unset($name[$i]); + $name = '..' . DS . implode(DS, $name); + } + + $paths = $this->_paths($this->plugin); + foreach ($paths as $path) { + if (file_exists($path . $name . $this->ext)) { + return $path . $name . $this->ext; + } elseif (file_exists($path . $name . '.thtml')) { + return $path . $name . '.thtml'; } } + + return $this->_missingView($paths[0] . $name . $this->ext, 'missingView'); + } + +/** + * Returns layout filename for this template as a string. + * + * @return string Filename for layout file (.ctp). + * @access protected + */ + function _getLayoutFileName($name = null) { + if($name === null) { + $name = $this->layout; + } + $subDir = null; + if (!is_null($this->layoutPath)) { + $subDir = $this->layoutPath . DS; + } + + $paths = $this->_paths($this->plugin); + $file = 'layouts' . DS . $subDir . $name; + foreach ($paths as $path) { + if (file_exists($path . $file . $this->ext)) { + return $path . $file . $this->ext; + } elseif (file_exists($path . $file . '.thtml')) { + return $path . $file . '.thtml'; + } + } + + return $this->_missingView($paths[0] . $file . $this->ext, 'missingLayout'); } /** * Return a misssing view error message @@ -918,42 +843,69 @@ class View extends Object { * @param string $viewFileName the filename that should exist * @return cakeError */ - function _missingView($viewFileName = null, $action = null) { - if (!is_file($viewFileName) && !fileExistsInPath($viewFileName) || $viewFileName === '/' || $viewFileName === '\\') { - if (strpos($action, 'missingAction') !== false) { - $errorAction = 'missingAction'; - } else { - $errorAction = 'missingView'; - } - - foreach (array($this->name, 'errors') as $viewDir) { - $errorAction = Inflector::underscore($errorAction); - if (file_exists(VIEWS . $viewDir . DS . $errorAction . $this->ext)) { - $missingViewFileName = VIEWS . $viewDir . DS . $errorAction . $this->ext; - } elseif (file_exists(VIEWS . $viewDir . DS . $errorAction . '.thtml')) { - $missingViewFileName = VIEWS . $viewDir . DS . $errorAction . '.thtml'; - } elseif ($missingViewFileName = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . $viewDir . DS . $errorAction . '.ctp')) { - } else { - $missingViewFileName = false; - } - $missingViewExists = is_file($missingViewFileName); - - if ($missingViewExists) { - break; - } - } - - if (strpos($action, 'missingView') === false) { - return $this->cakeError('missingView', array(array( - 'className' => $this->name, - 'action' => $this->action, - 'file' => $viewFileName, - 'base' => $this->base - ))); - exit(); + function _missingView($file, $error = 'missingView') { + $paths = $this->_paths($this->plugin); + $name = 'errors' . DS . Inflector::underscore($error); + foreach ($paths as $path) { + if (file_exists($path . $name . $this->ext)) { + $name = $path . $name . $this->ext; + break; + } elseif (file_exists($path . $name . '.thtml')) { + $name = $path . $name . '.thtml'; + break; } } + + if ($error === 'missingView') { + return $this->cakeError('missingView', array(array( + 'className' => $this->name, + 'action' => $this->action, + 'file' => $file, + 'base' => $this->base + ))); + } + if ($error === 'missingLayout') { + return $this->cakeError('missingLayout', array(array( + 'layout' => $this->layout, + 'file' => $file, + 'base' => $this->base + ))); + } + return $name; + } +/** + * Return all possible paths to find view files in order + * + * @param string $plugin + * @return array paths + * @access protected + */ + function _paths($plugin = null, $cached = true) { + if($plugin === null && $cached === true && !empty($this->__paths)) { + return $this->__paths; + } + $paths = array(); + $viewPaths = Configure::read('viewPaths'); + if ($plugin !== null) { + $count = count($viewPaths); + for ($i = 0; $i < $count; $i++) { + $paths[] = $viewPaths[$i] . 'plugins' . DS . $plugin . DS; + } + + $pluginPaths = Configure::read('pluginPaths'); + $count = count($pluginPaths); + for ($i = 0; $i < $count; $i++) { + $paths[] = $pluginPaths[$i] . $plugin . DS . 'views' . DS; + } + } + + $paths = array_merge($paths, $viewPaths); + + if(empty($this->__paths)) { + $this->__paths = $paths; + } + + return $paths; } } - ?> \ No newline at end of file diff --git a/cake/tests/cases/libs/configure.test.php b/cake/tests/cases/libs/configure.test.php index 9b03474e6..6a94bd91f 100644 --- a/cake/tests/cases/libs/configure.test.php +++ b/cake/tests/cases/libs/configure.test.php @@ -44,6 +44,24 @@ class ConfigureTest extends UnitTestCase { $this->assertTrue(in_array('Xml', $result)); $this->assertTrue(in_array('Cache', $result)); $this->assertTrue(in_array('HttpSocket', $result)); + + $result = $this->Configure->listObjects('model'); + $this->assertTrue(in_array('Model', $result)); + + $result = $this->Configure->listObjects('behavior'); + $this->assertTrue(in_array('Tree', $result)); + + $result = $this->Configure->listObjects('controller'); + $this->assertTrue(in_array('Pages', $result)); + + $result = $this->Configure->listObjects('component'); + $this->assertTrue(in_array('Auth', $result)); + + $result = $this->Configure->listObjects('view'); + $this->assertTrue(in_array('Media', $result)); + + $result = $this->Configure->listObjects('helper'); + $this->assertTrue(in_array('Html', $result)); } function tearDown() { diff --git a/cake/tests/cases/libs/controller/scaffold.test.php b/cake/tests/cases/libs/controller/scaffold.test.php index 21c418e5b..da859b7ca 100644 --- a/cake/tests/cases/libs/controller/scaffold.test.php +++ b/cake/tests/cases/libs/controller/scaffold.test.php @@ -27,6 +27,23 @@ * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License */ uses('controller' . DS . 'scaffold'); +class ScaffoldMockController extends Controller { + + var $name = 'ScaffoldMock'; + + var $scaffold; +} +class ScaffoldMock extends CakeTestModel { + + var $useTable = 'posts'; + +} +class TestScaffoldView extends ScaffoldView { + + function testGetFilename($action) { + return $this->_getViewFileName($action); + } +} /** * Short description for class. * @@ -35,8 +52,21 @@ uses('controller' . DS . 'scaffold'); */ class ScaffoldTest extends CakeTestCase { - function skip() { - $this->skipif (true, 'ScaffoldTest not implemented'); + var $fixtures = array('core.post'); + + function setUp() { + $this->Controller = new ScaffoldMockController(); + } + + function tearDown() { + unset($this->Controller); + } + function testGetViewFilename() { + $this->Controller->action = 'index'; + $ScaffoldView =& new TestScaffoldView($this->Controller); + $result = $ScaffoldView->testGetFilename('index'); + $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'scaffolds' . DS . 'index.ctp'; + $this->assertEqual($result, $expected); } } ?> \ No newline at end of file diff --git a/cake/tests/cases/libs/view/theme.test.php b/cake/tests/cases/libs/view/theme.test.php new file mode 100644 index 000000000..b6a2c427f --- /dev/null +++ b/cake/tests/cases/libs/view/theme.test.php @@ -0,0 +1,165 @@ + + * Copyright 2005-2007, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.cases.libs + * @since CakePHP(tm) v 1.2.0.4206 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +uses('controller' . DS . 'controller', 'view'.DS.'theme'); + +class PostsController extends Controller { + var $name = 'Posts'; + function index() { + $this->set('testData', 'Some test data'); + $test2 = 'more data'; + $test3 = 'even more data'; + $this->set(compact('test2', 'test3')); + } +} + +class TestView extends ThemeView { + + function renderElement($name, $params = array()) { + return $name; + } + + function getViewFileName($name = null) { + return $this->_getViewFileName($name); + } + function getLayoutFileName($name = null) { + return $this->_getLayoutFileName($name); + } + + function cakeError($name, $params) { + return $name; + } +} + +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs + */ +class ViewTest extends UnitTestCase { + + function setUp() { + Router::reload(); + $this->Controller = new Controller(); + $this->PostsController = new PostsController(); + $this->PostsController->index(); + $this->ThemeView = new View($this->PostsController); + } + + function testPluginGetTemplate() { + $this->Controller->plugin = 'test_plugin'; + $this->Controller->name = 'TestPlugin'; + $this->Controller->viewPath = 'test_plugin'; + $this->Controller->action = 'index'; + $this->Controller->theme = 'test_plugin_theme'; + + $ThemeView = new TestView($this->Controller); + Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)); + Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)); + + $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS .'test_plugin' . DS . 'views' . DS . 'themed' . DS . 'test_plugin_theme' . DS .'test_plugin' . DS .'index.ctp'; + $result = $ThemeView->getViewFileName('index'); + $this->assertEqual($result, $expected); + + $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS .'test_plugin' . DS . 'views' . DS . 'themed' . DS . 'test_plugin_theme' . DS . 'layouts' . DS .'default.ctp'; + $result = $ThemeView->getLayoutFileName(); + $this->assertEqual($result, $expected); + } + + function testGetTemplate() { + $this->Controller->plugin = null; + $this->Controller->name = 'Pages'; + $this->Controller->viewPath = 'pages'; + $this->Controller->action = 'display'; + $this->Controller->params['pass'] = array('home'); + + $ThemeView = new TestView($this->Controller); + $ThemeView->theme = 'test_theme'; + + Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)); + Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS, TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS)); + + $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS .'pages' . DS .'home.ctp'; + $result = $ThemeView->getViewFileName('home'); + $this->assertEqual($result, $expected); + + $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'posts' . DS .'index.ctp'; + $result = $ThemeView->getViewFileName('/posts/index'); + $this->assertEqual($result, $expected); + + $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'layouts' . DS .'default.ctp'; + $result = $ThemeView->getLayoutFileName(); + $this->assertEqual($result, $expected); + + $ThemeView->layoutPath = 'rss'; + $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'layouts' . DS . 'rss' . DS . 'default.ctp'; + $result = $ThemeView->getLayoutFileName(); + $this->assertEqual($result, $expected); + + $ThemeView->layoutPath = 'email' . DS . 'html'; + $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'layouts' . DS . 'email' . DS . 'html' . DS . 'default.ctp'; + $result = $ThemeView->getLayoutFileName(); + $this->assertEqual($result, $expected); + } + + function testMissingView() { + $this->Controller->plugin = null; + $this->Controller->name = 'Pages'; + $this->Controller->viewPath = 'pages'; + $this->Controller->action = 'display'; + $this->Controller->params['pass'] = array('home'); + + $ThemeView = new TestView($this->Controller); + + $expected = 'missingView'; + $result = $ThemeView->getViewFileName('does_not_exist'); + $this->assertEqual($result, $expected); + + } + + function testMissingLayout() { + $this->Controller->plugin = null; + $this->Controller->name = 'Posts'; + $this->Controller->viewPath = 'posts'; + $this->Controller->layout = 'whatever'; + + $ThemeView = new TestView($this->Controller); + $expected = 'missingLayout'; + $result = $ThemeView->getLayoutFileName(); + $this->assertEqual($result, $expected); + } + + function tearDown() { + unset($this->ThemeView); + unset($this->PostsController); + unset($this->Controller); + + } +} +?> \ No newline at end of file diff --git a/cake/tests/cases/libs/view/view.test.php b/cake/tests/cases/libs/view/view.test.php index b4c844e8f..823c37a7d 100644 --- a/cake/tests/cases/libs/view/view.test.php +++ b/cake/tests/cases/libs/view/view.test.php @@ -43,6 +43,17 @@ class TestView extends View { function renderElement($name, $params = array()) { return $name; } + + function getViewFileName($name = null) { + return $this->_getViewFileName($name); + } + function getLayoutFileName($name = null) { + return $this->_getLayoutFileName($name); + } + + function cakeError($name, $params) { + return $name; + } } /** @@ -55,31 +66,113 @@ class ViewTest extends UnitTestCase { function setUp() { Router::reload(); + $this->Controller = new Controller(); $this->PostsController = new PostsController(); $this->PostsController->index(); - $this->view = new View($this->PostsController); + $this->View = new View($this->PostsController); + } + + function testPluginGetTemplate() { + $this->Controller->plugin = 'test_plugin'; + $this->Controller->name = 'TestPlugin'; + $this->Controller->viewPath = 'test_plugin'; + $this->Controller->action = 'index'; + + $View = new TestView($this->Controller); + Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)); + Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)); + + $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS .'test_plugin' . DS . 'views' . DS .'test_plugin' . DS .'index.ctp'; + $result = $View->getViewFileName('index'); + $this->assertEqual($result, $expected); + + $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS .'test_plugin' . DS . 'views' . DS . 'layouts' . DS .'default.ctp'; + $result = $View->getLayoutFileName(); + $this->assertEqual($result, $expected); + } + + function testGetTemplate() { + $this->Controller->plugin = null; + $this->Controller->name = 'Pages'; + $this->Controller->viewPath = 'pages'; + $this->Controller->action = 'display'; + $this->Controller->params['pass'] = array('home'); + + $View = new TestView($this->Controller); + Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)); + Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS, TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS)); + + $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS .'pages' . DS .'home.ctp'; + $result = $View->getViewFileName('home'); + $this->assertEqual($result, $expected); + + $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS .'posts' . DS .'index.ctp'; + $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); + + $View->layoutPath = 'rss'; + $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'layouts' . DS . 'rss' . DS . 'default.ctp'; + $result = $View->getLayoutFileName(); + $this->assertEqual($result, $expected); + + $View->layoutPath = 'email' . DS . 'html'; + $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'layouts' . DS . 'email' . DS . 'html' . DS . 'default.ctp'; + $result = $View->getLayoutFileName(); + $this->assertEqual($result, $expected); + } + + function testMissingView() { + $this->Controller->plugin = null; + $this->Controller->name = 'Pages'; + $this->Controller->viewPath = 'pages'; + $this->Controller->action = 'display'; + $this->Controller->params['pass'] = array('home'); + + $View = new TestView($this->Controller); + + $expected = 'missingView'; + $result = $View->getViewFileName('does_not_exist'); + $this->assertEqual($result, $expected); + + } + + function testMissingLayout() { + $this->Controller->plugin = null; + $this->Controller->name = 'Posts'; + $this->Controller->viewPath = 'posts'; + $this->Controller->layout = 'whatever'; + + $View = new TestView($this->Controller); + $expected = 'missingLayout'; + $result = $View->getLayoutFileName(); + $this->assertEqual($result, $expected); } function testViewVars() { - $this->assertEqual($this->view->viewVars, array('testData' => 'Some test data', 'test2' => 'more data', 'test3' => 'even more data')); + $this->assertEqual($this->View->viewVars, array('testData' => 'Some test data', 'test2' => 'more data', 'test3' => 'even more data')); } function testUUIDGeneration() { - $result = $this->view->uuid('form', array('controller' => 'posts', 'action' => 'index')); + $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index')); $this->assertEqual($result, 'form0425fe3bad'); - $result = $this->view->uuid('form', array('controller' => 'posts', 'action' => 'index')); + $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index')); $this->assertEqual($result, 'forma9918342a7'); - $result = $this->view->uuid('form', array('controller' => 'posts', 'action' => 'index')); + $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index')); $this->assertEqual($result, 'form3ecf2e3e96'); } function testAddInlineScripts() { - $this->view->addScript('prototype.js'); - $this->view->addScript('prototype.js'); - $this->assertEqual($this->view->__scripts, array('prototype.js')); + $this->View->addScript('prototype.js'); + $this->View->addScript('prototype.js'); + $this->assertEqual($this->View->__scripts, array('prototype.js')); - $this->view->addScript('mainEvent', 'Event.observe(window, "load", function() { doSomething(); }, true);'); - $this->assertEqual($this->view->__scripts, array('prototype.js', 'mainEvent' => 'Event.observe(window, "load", function() { doSomething(); }, true);')); + $this->View->addScript('mainEvent', 'Event.observe(window, "load", function() { doSomething(); }, true);'); + $this->assertEqual($this->View->__scripts, array('prototype.js', 'mainEvent' => 'Event.observe(window, "load", function() { doSomething(); }, true);')); } function testElementCache() { @@ -124,8 +217,10 @@ class ViewTest extends UnitTestCase { } function tearDown() { - unset($this->view); + unset($this->View); unset($this->PostsController); + unset($this->Controller); + } } ?> \ No newline at end of file diff --git a/cake/tests/groups/view.group.php b/cake/tests/groups/view.group.php new file mode 100644 index 000000000..ea3a11cd7 --- /dev/null +++ b/cake/tests/groups/view.group.php @@ -0,0 +1,45 @@ + + * Copyright 2005-2007, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.groups + * @since CakePHP(tm) v 1.2.0.4206 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +/** AllCoreHelpersGroupTest + * + * This test group will run all test in the cases/libs/view/helpers directory. + * + * @package cake.tests + * @subpackage cake.tests.groups + */ +class AllCoreViewsGroupTest extends GroupTest { + + var $label = 'All core views'; + + function AllCoreViewsGroupTest() { + TestManager::addTestFile($this, CORE_TEST_CASES . DS . 'libs' . DS . 'view' . DS . 'view', + CORE_TEST_CASES . DS . 'libs' . DS . 'view' . DS . 'theme'); + } +} +?> \ No newline at end of file diff --git a/cake/tests/test_app/controllers/components/empty b/cake/tests/test_app/controllers/components/empty new file mode 100755 index 000000000..e69de29bb diff --git a/cake/tests/test_app/models/behaviors/empty b/cake/tests/test_app/models/behaviors/empty new file mode 100755 index 000000000..e69de29bb diff --git a/cake/tests/test_app/models/datasources/empty b/cake/tests/test_app/models/datasources/empty new file mode 100755 index 000000000..e69de29bb diff --git a/cake/tests/test_app/plugins/test_plugin/views/layouts/default.ctp b/cake/tests/test_app/plugins/test_plugin/views/layouts/default.ctp new file mode 100644 index 000000000..0437f2c6b --- /dev/null +++ b/cake/tests/test_app/plugins/test_plugin/views/layouts/default.ctp @@ -0,0 +1 @@ +test plugin default layout \ No newline at end of file diff --git a/cake/tests/test_app/plugins/test_plugin/views/test_plugin/index.ctp b/cake/tests/test_app/plugins/test_plugin/views/test_plugin/index.ctp new file mode 100644 index 000000000..6260ece2d --- /dev/null +++ b/cake/tests/test_app/plugins/test_plugin/views/test_plugin/index.ctp @@ -0,0 +1 @@ +test plugin index \ No newline at end of file diff --git a/cake/tests/test_app/plugins/test_plugin/views/themed/test_plugin_theme/layouts/default.ctp b/cake/tests/test_app/plugins/test_plugin/views/themed/test_plugin_theme/layouts/default.ctp new file mode 100644 index 000000000..9e1e449c2 --- /dev/null +++ b/cake/tests/test_app/plugins/test_plugin/views/themed/test_plugin_theme/layouts/default.ctp @@ -0,0 +1 @@ +test_plugin test_plugin_theme default layout \ No newline at end of file diff --git a/cake/tests/test_app/plugins/test_plugin/views/themed/test_plugin_theme/test_plugin/index.ctp b/cake/tests/test_app/plugins/test_plugin/views/themed/test_plugin_theme/test_plugin/index.ctp new file mode 100644 index 000000000..edf7ab4b2 --- /dev/null +++ b/cake/tests/test_app/plugins/test_plugin/views/themed/test_plugin_theme/test_plugin/index.ctp @@ -0,0 +1 @@ +test plugin index theme view \ No newline at end of file diff --git a/cake/tests/test_app/vendors/shells/tasks/empty b/cake/tests/test_app/vendors/shells/tasks/empty new file mode 100755 index 000000000..e69de29bb diff --git a/cake/tests/test_app/vendors/shells/templates/empty b/cake/tests/test_app/vendors/shells/templates/empty new file mode 100755 index 000000000..e69de29bb diff --git a/cake/tests/test_app/views/elements/empty b/cake/tests/test_app/views/elements/empty new file mode 100755 index 000000000..e69de29bb diff --git a/cake/tests/test_app/views/errors/empty b/cake/tests/test_app/views/errors/empty new file mode 100755 index 000000000..e69de29bb diff --git a/cake/tests/test_app/views/helpers/empty b/cake/tests/test_app/views/helpers/empty new file mode 100755 index 000000000..e69de29bb diff --git a/cake/tests/test_app/views/layouts/ajax.ctp b/cake/tests/test_app/views/layouts/ajax.ctp new file mode 100755 index 000000000..b69838d65 --- /dev/null +++ b/cake/tests/test_app/views/layouts/ajax.ctp @@ -0,0 +1,27 @@ + + * Copyright 2005-2007, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The MIT License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. + * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project + * @package cake + * @subpackage cake.cake.libs.view.templates.layouts + * @since CakePHP(tm) v 0.10.0.1076 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/mit-license.php The MIT License + */ +?> + \ No newline at end of file diff --git a/cake/tests/test_app/views/layouts/default.ctp b/cake/tests/test_app/views/layouts/default.ctp new file mode 100755 index 000000000..2433b0d35 --- /dev/null +++ b/cake/tests/test_app/views/layouts/default.ctp @@ -0,0 +1,68 @@ + + * Copyright 2005-2007, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The MIT License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. + * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project + * @package cake + * @subpackage cake.cake.libs.view.templates.pages + * @since CakePHP(tm) v 0.10.0.1076 + * @version $Revision$ + * @modifiedby $LastChangedBy$ + * @lastmodified $Date$ + * @license http://www.opensource.org/licenses/mit-license.php The MIT License + */ +?> + + + +
+'; + __('Your tmp directory is writable.'); + echo ''; + else: + echo ''; + __('Your tmp directory is NOT writable.'); + echo ''; + endif; +?> +
++'; + echo sprintf(__('The %s is being used for caching. To change the config edit APP/config/core.php ', true), ''. $settings['engine'] . 'Engine'); + echo ''; + else: + echo ''; + __('Your cache is NOT working. Please check the settings in APP/config/core.php'); + echo ''; + endif; +?> +
+
+';
+ __('Your database configuration file is present.');
+ $filePresent = true;
+ echo '';
+ else:
+ echo '';
+ __('Your database configuration file is NOT present.');
+ echo '
';
+ __('Rename config/database.php.default to config/database.php');
+ echo '';
+ endif;
+?>
+
+isConnected()): + echo ''; + __('Cake is able to connect to the database.'); + echo ''; + else: + echo ''; + __('Cake is NOT able to connect to the database.'); + echo ''; + endif; +?> +
+ + +
+', APP . 'views' . DS . 'layouts' . DS . 'default.ctp.
', APP . 'webroot' . DS . 'css');
+?>
+