PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/view/theme.test.php

https://github.com/Forbin/cakephp2x
PHP | 322 lines | 148 code | 31 blank | 143 comment | 2 complexity | e0b0d1a0e596f93918580c633fbdc01a MD5 | raw file
  1. <?php
  2. /**
  3. * ThemeViewTest file
  4. *
  5. * PHP Version 5.x
  6. *
  7. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  8. * Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The Open Group Test Suite License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  15. * @package cake
  16. * @subpackage cake.tests.cases.libs
  17. * @since CakePHP(tm) v 1.2.0.4206
  18. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  19. */
  20. App::import('Core', array('Theme', 'Controller'));
  21. if (!class_exists('ErrorHandler')) {
  22. App::import('Core', array('Error'));
  23. }
  24. if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
  25. define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
  26. }
  27. /**
  28. * ThemePostsController class
  29. *
  30. * @package cake
  31. * @subpackage cake.tests.cases.libs.view
  32. */
  33. class ThemePostsController extends Controller {
  34. /**
  35. * name property
  36. *
  37. * @var string 'ThemePosts'
  38. * @access public
  39. */
  40. public $name = 'ThemePosts';
  41. /**
  42. * index method
  43. *
  44. * @access public
  45. * @return void
  46. */
  47. public function index() {
  48. $this->set('testData', 'Some test data');
  49. $test2 = 'more data';
  50. $test3 = 'even more data';
  51. $this->set(compact('test2', 'test3'));
  52. }
  53. }
  54. /**
  55. * ThemeViewTestErrorHandler class
  56. *
  57. * @package cake
  58. * @subpackage cake.tests.cases.libs.view
  59. */
  60. class ThemeViewTestErrorHandler extends ErrorHandler {
  61. /**
  62. * stop method
  63. *
  64. * @access public
  65. * @return void
  66. */
  67. function _stop() {
  68. return;
  69. }
  70. }
  71. /**
  72. * TestThemeView class
  73. *
  74. * @package cake
  75. * @subpackage cake.tests.cases.libs.view
  76. */
  77. class TestThemeView extends ThemeView {
  78. /**
  79. * renderElement method
  80. *
  81. * @param mixed $name
  82. * @param array $params
  83. * @access public
  84. * @return void
  85. */
  86. function renderElement($name, $params = array()) {
  87. return $name;
  88. }
  89. /**
  90. * getViewFileName method
  91. *
  92. * @param mixed $name
  93. * @access public
  94. * @return void
  95. */
  96. function getViewFileName($name = null) {
  97. return $this->_getViewFileName($name);
  98. }
  99. /**
  100. * getLayoutFileName method
  101. *
  102. * @param mixed $name
  103. * @access public
  104. * @return void
  105. */
  106. function getLayoutFileName($name = null) {
  107. return $this->_getLayoutFileName($name);
  108. }
  109. /**
  110. * cakeError method
  111. *
  112. * @param mixed $method
  113. * @param mixed $messages
  114. * @access public
  115. * @return void
  116. */
  117. function cakeError($method, $messages) {
  118. $error = new ThemeViewTestErrorHandler($method, $messages);
  119. return $error;
  120. }
  121. }
  122. /**
  123. * ThemeViewTest class
  124. *
  125. * @package cake
  126. * @subpackage cake.tests.cases.libs
  127. */
  128. class ThemeViewTest extends CakeTestCase {
  129. /**
  130. * setUp method
  131. *
  132. * @access public
  133. * @return void
  134. */
  135. function setUp() {
  136. Router::reload();
  137. $this->Controller =& new Controller();
  138. $this->PostsController =& new ThemePostsController();
  139. $this->PostsController->viewPath = 'posts';
  140. $this->PostsController->theme = null;
  141. $this->PostsController->index();
  142. $this->ThemeView =& new ThemeView($this->PostsController);
  143. }
  144. /**
  145. * tearDown method
  146. *
  147. * @access public
  148. * @return void
  149. */
  150. function tearDown() {
  151. unset($this->ThemeView);
  152. unset($this->PostsController);
  153. unset($this->Controller);
  154. ClassRegistry::flush();
  155. }
  156. /**
  157. * test that the theme view can be constructed without going into the registry
  158. *
  159. * @return void
  160. */
  161. function testConstructionNoRegister() {
  162. ClassRegistry::flush();
  163. $controller = null;
  164. $Theme =& new ThemeView($controller, false);
  165. $ThemeTwo =& ClassRegistry::getObject('view');
  166. $this->assertFalse($ThemeTwo);
  167. }
  168. /**
  169. * startTest
  170. *
  171. * @access public
  172. * @return void
  173. */
  174. function startTest() {
  175. App::build(array(
  176. 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
  177. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
  178. ));
  179. $Configure = App::getInstance();
  180. array_shift($Configure->views);
  181. }
  182. /**
  183. * endTest
  184. *
  185. * @access public
  186. * @return void
  187. */
  188. function endTest() {
  189. App::build();
  190. }
  191. /**
  192. * testPluginGetTemplate method
  193. *
  194. * @access public
  195. * @return void
  196. */
  197. function testPluginThemedGetTemplate() {
  198. $this->Controller->plugin = 'test_plugin';
  199. $this->Controller->name = 'TestPlugin';
  200. $this->Controller->viewPath = 'tests';
  201. $this->Controller->action = 'index';
  202. $this->Controller->theme = 'test_theme';
  203. $ThemeView = new TestThemeView($this->Controller);
  204. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'plugins' . DS . 'test_plugin' . DS . 'tests' . DS .'index.ctp';
  205. $result = $ThemeView->getViewFileName('index');
  206. $this->assertEqual($result, $expected);
  207. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'plugins' . DS . 'test_plugin' . DS . 'layouts' . DS .'plugin_default.ctp';
  208. $result = $ThemeView->getLayoutFileName('plugin_default');
  209. $this->assertEqual($result, $expected);
  210. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'layouts' . DS .'default.ctp';
  211. $result = $ThemeView->getLayoutFileName('default');
  212. $this->assertEqual($result, $expected);
  213. }
  214. /**
  215. * testGetTemplate method
  216. *
  217. * @access public
  218. * @return void
  219. */
  220. function testGetTemplate() {
  221. $this->Controller->plugin = null;
  222. $this->Controller->name = 'Pages';
  223. $this->Controller->viewPath = 'pages';
  224. $this->Controller->action = 'display';
  225. $this->Controller->theme = null;
  226. $this->Controller->params['pass'] = array('home');
  227. $ThemeView = new TestThemeView($this->Controller);
  228. $ThemeView->theme = 'test_theme';
  229. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS .'pages' . DS .'home.ctp';
  230. $result = $ThemeView->getViewFileName('home');
  231. $this->assertEqual($result, $expected);
  232. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'posts' . DS .'index.ctp';
  233. $result = $ThemeView->getViewFileName('/posts/index');
  234. $this->assertEqual($result, $expected);
  235. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'layouts' . DS .'default.ctp';
  236. $result = $ThemeView->getLayoutFileName();
  237. $this->assertEqual($result, $expected);
  238. $ThemeView->layoutPath = 'rss';
  239. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'layouts' . DS . 'rss' . DS . 'default.ctp';
  240. $result = $ThemeView->getLayoutFileName();
  241. $this->assertEqual($result, $expected);
  242. $ThemeView->layoutPath = 'email' . DS . 'html';
  243. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'layouts' . DS . 'email' . DS . 'html' . DS . 'default.ctp';
  244. $result = $ThemeView->getLayoutFileName();
  245. $this->assertEqual($result, $expected);
  246. }
  247. /**
  248. * testMissingView method
  249. *
  250. * @access public
  251. * @return void
  252. */
  253. function testMissingView() {
  254. $this->Controller->plugin = null;
  255. $this->Controller->name = 'Pages';
  256. $this->Controller->viewPath = 'pages';
  257. $this->Controller->action = 'display';
  258. $this->Controller->theme = 'my_theme';
  259. $this->Controller->params['pass'] = array('home');
  260. restore_error_handler();
  261. $View = new TestThemeView($this->Controller);
  262. ob_start();
  263. $result = $View->getViewFileName('does_not_exist');
  264. $expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  265. set_error_handler('simpleTestErrorHandler');
  266. $this->assertPattern("/PagesController::/", $expected);
  267. $this->assertPattern("/views(\/|\\\)themed(\/|\\\)my_theme(\/|\\\)pages(\/|\\\)does_not_exist.ctp/", $expected);
  268. }
  269. /**
  270. * testMissingLayout method
  271. *
  272. * @access public
  273. * @return void
  274. */
  275. function testMissingLayout() {
  276. $this->Controller->plugin = null;
  277. $this->Controller->name = 'Posts';
  278. $this->Controller->viewPath = 'posts';
  279. $this->Controller->layout = 'whatever';
  280. $this->Controller->theme = 'my_theme';
  281. restore_error_handler();
  282. $View = new TestThemeView($this->Controller);
  283. ob_start();
  284. $result = $View->getLayoutFileName();
  285. $expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  286. set_error_handler('simpleTestErrorHandler');
  287. $this->assertPattern("/Missing Layout/", $expected);
  288. $this->assertPattern("/views(\/|\\\)themed(\/|\\\)my_theme(\/|\\\)layouts(\/|\\\)whatever.ctp/", $expected);
  289. }
  290. }
  291. ?>