PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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