PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/webpolis/hurli
PHP | 343 lines | 161 code | 34 blank | 148 comment | 4 complexity | a8a85207317387540c07a2ce96234dc1 MD5 | raw file
  1. <?php
  2. /**
  3. * ThemeViewTest file
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing 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. var $name = 'ThemePosts';
  41. /**
  42. * index method
  43. *
  44. * @access public
  45. * @return void
  46. */
  47. 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->index();
  141. $this->ThemeView =& new ThemeView($this->PostsController);
  142. }
  143. /**
  144. * tearDown method
  145. *
  146. * @access public
  147. * @return void
  148. */
  149. function tearDown() {
  150. unset($this->ThemeView);
  151. unset($this->PostsController);
  152. unset($this->Controller);
  153. ClassRegistry::flush();
  154. }
  155. /**
  156. * test that the theme view can be constructed without going into the registry
  157. *
  158. * @return void
  159. */
  160. function testConstructionNoRegister() {
  161. ClassRegistry::flush();
  162. $controller = null;
  163. $Theme =& new ThemeView($controller, false);
  164. $ThemeTwo =& ClassRegistry::getObject('view');
  165. $this->assertFalse($ThemeTwo);
  166. }
  167. /**
  168. * startTest
  169. *
  170. * @access public
  171. * @return void
  172. */
  173. function startTest() {
  174. App::build(array(
  175. 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
  176. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
  177. ));
  178. }
  179. /**
  180. * endTest
  181. *
  182. * @access public
  183. * @return void
  184. */
  185. function endTest() {
  186. App::build();
  187. }
  188. /**
  189. * testPluginGetTemplate method
  190. *
  191. * @access public
  192. * @return void
  193. */
  194. function testPluginThemedGetTemplate() {
  195. $this->Controller->plugin = 'test_plugin';
  196. $this->Controller->name = 'TestPlugin';
  197. $this->Controller->viewPath = 'tests';
  198. $this->Controller->action = 'index';
  199. $this->Controller->theme = 'test_theme';
  200. $ThemeView =& new TestThemeView($this->Controller);
  201. $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';
  202. $result = $ThemeView->getViewFileName('index');
  203. $this->assertEqual($result, $expected);
  204. $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';
  205. $result = $ThemeView->getLayoutFileName('plugin_default');
  206. $this->assertEqual($result, $expected);
  207. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'layouts' . DS .'default.ctp';
  208. $result = $ThemeView->getLayoutFileName('default');
  209. $this->assertEqual($result, $expected);
  210. }
  211. /**
  212. * testGetTemplate method
  213. *
  214. * @access public
  215. * @return void
  216. */
  217. function testGetTemplate() {
  218. $this->Controller->plugin = null;
  219. $this->Controller->name = 'Pages';
  220. $this->Controller->viewPath = 'pages';
  221. $this->Controller->action = 'display';
  222. $this->Controller->params['pass'] = array('home');
  223. $ThemeView =& new TestThemeView($this->Controller);
  224. $ThemeView->theme = 'test_theme';
  225. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS .'pages' . DS .'home.ctp';
  226. $result = $ThemeView->getViewFileName('home');
  227. $this->assertEqual($result, $expected);
  228. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'posts' . DS .'index.ctp';
  229. $result = $ThemeView->getViewFileName('/posts/index');
  230. $this->assertEqual($result, $expected);
  231. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'layouts' . DS .'default.ctp';
  232. $result = $ThemeView->getLayoutFileName();
  233. $this->assertEqual($result, $expected);
  234. $ThemeView->layoutPath = 'rss';
  235. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'layouts' . DS . 'rss' . DS . 'default.ctp';
  236. $result = $ThemeView->getLayoutFileName();
  237. $this->assertEqual($result, $expected);
  238. $ThemeView->layoutPath = 'email' . DS . 'html';
  239. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'layouts' . DS . 'email' . DS . 'html' . DS . 'default.ctp';
  240. $result = $ThemeView->getLayoutFileName();
  241. $this->assertEqual($result, $expected);
  242. }
  243. /**
  244. * testMissingView method
  245. *
  246. * @access public
  247. * @return void
  248. */
  249. function testMissingView() {
  250. $this->Controller->plugin = null;
  251. $this->Controller->name = 'Pages';
  252. $this->Controller->viewPath = 'pages';
  253. $this->Controller->action = 'display';
  254. $this->Controller->theme = 'my_theme';
  255. $this->Controller->params['pass'] = array('home');
  256. restore_error_handler();
  257. $View =& new TestThemeView($this->Controller);
  258. ob_start();
  259. $result = $View->getViewFileName('does_not_exist');
  260. $expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  261. set_error_handler('simpleTestErrorHandler');
  262. $this->assertPattern("/PagesController::/", $expected);
  263. $this->assertPattern("/views(\/|\\\)themed(\/|\\\)my_theme(\/|\\\)pages(\/|\\\)does_not_exist.ctp/", $expected);
  264. }
  265. /**
  266. * testMissingLayout method
  267. *
  268. * @access public
  269. * @return void
  270. */
  271. function testMissingLayout() {
  272. $this->Controller->plugin = null;
  273. $this->Controller->name = 'Posts';
  274. $this->Controller->viewPath = 'posts';
  275. $this->Controller->layout = 'whatever';
  276. $this->Controller->theme = 'my_theme';
  277. restore_error_handler();
  278. $View =& new TestThemeView($this->Controller);
  279. ob_start();
  280. $result = $View->getLayoutFileName();
  281. $expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  282. set_error_handler('simpleTestErrorHandler');
  283. $this->assertPattern("/Missing Layout/", $expected);
  284. $this->assertPattern("/views(\/|\\\)themed(\/|\\\)my_theme(\/|\\\)layouts(\/|\\\)whatever.ctp/", $expected);
  285. }
  286. /**
  287. * test memory leaks that existed in _paths at one point.
  288. *
  289. * @return void
  290. */
  291. function testMemoryLeakInPaths() {
  292. if ($this->skipIf(!function_exists('memory_get_usage'), 'No memory measurement function, cannot test for possible memory leak. %s')) {
  293. return;
  294. }
  295. $this->Controller->plugin = null;
  296. $this->Controller->name = 'Posts';
  297. $this->Controller->viewPath = 'posts';
  298. $this->Controller->layout = 'whatever';
  299. $this->Controller->theme = 'test_theme';
  300. $View =& new ThemeView($this->Controller);
  301. $View->element('test_element');
  302. $start = memory_get_usage();
  303. for ($i = 0; $i < 10; $i++) {
  304. $View->element('test_element');
  305. }
  306. $end = memory_get_usage();
  307. $this->assertWithinMargin($start, $end, 3500);
  308. }
  309. }