ThemeViewTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * ThemeViewTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('View', 'View');
  20. App::uses('ThemeView', 'View');
  21. App::uses('Controller', 'Controller');
  22. /**
  23. * ThemePosts2Controller class
  24. *
  25. * @package Cake.Test.Case.View
  26. */
  27. class ThemePosts2Controller extends Controller {
  28. /**
  29. * name property
  30. *
  31. * @var string 'ThemePosts'
  32. */
  33. public $name = 'ThemePosts';
  34. public $theme = null;
  35. /**
  36. * index method
  37. *
  38. * @return void
  39. */
  40. public function index() {
  41. $this->set(array(
  42. 'testData' => 'Some test data',
  43. 'test2' => 'more data',
  44. 'test3' => 'even more data',
  45. ));
  46. }
  47. }
  48. /**
  49. * TestTheme2View class
  50. *
  51. * @package Cake.Test.Case.View
  52. */
  53. class TestTheme2View extends ThemeView {
  54. /**
  55. * renderElement method
  56. *
  57. * @param string $name
  58. * @param array $params
  59. * @return void
  60. */
  61. public function renderElement($name, $params = array()) {
  62. return $name;
  63. }
  64. /**
  65. * getViewFileName method
  66. *
  67. * @param string $name
  68. * @return void
  69. */
  70. public function getViewFileName($name = null) {
  71. return $this->_getViewFileName($name);
  72. }
  73. /**
  74. * getLayoutFileName method
  75. *
  76. * @param string $name
  77. * @return void
  78. */
  79. public function getLayoutFileName($name = null) {
  80. return $this->_getLayoutFileName($name);
  81. }
  82. }
  83. /**
  84. * ThemeViewTest class
  85. *
  86. * @package Cake.Test.Case.View
  87. */
  88. class ThemeViewTest extends CakeTestCase {
  89. /**
  90. * setUp method
  91. *
  92. * @return void
  93. */
  94. public function setUp() {
  95. parent::setUp();
  96. $request = new CakeRequest('posts/index');
  97. $this->Controller = new Controller($request);
  98. $this->PostsController = new ThemePosts2Controller($request);
  99. $this->PostsController->viewPath = 'posts';
  100. $this->PostsController->index();
  101. $this->ThemeView = new ThemeView($this->PostsController);
  102. App::build(array(
  103. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  104. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  105. ));
  106. App::objects('plugins', null, false);
  107. CakePlugin::load(array('TestPlugin'));
  108. }
  109. /**
  110. * tearDown method
  111. *
  112. * @return void
  113. */
  114. public function tearDown() {
  115. parent::tearDown();
  116. unset($this->ThemeView);
  117. unset($this->PostsController);
  118. unset($this->Controller);
  119. CakePlugin::unload();
  120. }
  121. /**
  122. * testPluginGetTemplate method
  123. *
  124. * @return void
  125. */
  126. public function testPluginThemedGetTemplate() {
  127. $this->Controller->plugin = 'TestPlugin';
  128. $this->Controller->name = 'TestPlugin';
  129. $this->Controller->viewPath = 'Tests';
  130. $this->Controller->action = 'index';
  131. $this->Controller->theme = 'TestTheme';
  132. $ThemeView = new TestTheme2View($this->Controller);
  133. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Tests' . DS . 'index.ctp';
  134. $result = $ThemeView->getViewFileName('index');
  135. $this->assertEquals($expected, $result);
  136. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Layouts' . DS . 'plugin_default.ctp';
  137. $result = $ThemeView->getLayoutFileName('plugin_default');
  138. $this->assertEquals($expected, $result);
  139. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Layouts' . DS . 'default.ctp';
  140. $result = $ThemeView->getLayoutFileName('default');
  141. $this->assertEquals($expected, $result);
  142. }
  143. /**
  144. * testGetTemplate method
  145. *
  146. * @return void
  147. */
  148. public function testGetTemplate() {
  149. $this->Controller->plugin = null;
  150. $this->Controller->name = 'Pages';
  151. $this->Controller->viewPath = 'Pages';
  152. $this->Controller->action = 'display';
  153. $this->Controller->params['pass'] = array('home');
  154. $ThemeView = new TestTheme2View($this->Controller);
  155. $ThemeView->theme = 'TestTheme';
  156. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages' . DS . 'home.ctp';
  157. $result = $ThemeView->getViewFileName('home');
  158. $this->assertEquals($expected, $result);
  159. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Posts' . DS . 'index.ctp';
  160. $result = $ThemeView->getViewFileName('/Posts/index');
  161. $this->assertEquals($expected, $result);
  162. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Layouts' . DS . 'default.ctp';
  163. $result = $ThemeView->getLayoutFileName();
  164. $this->assertEquals($expected, $result);
  165. $ThemeView->layoutPath = 'rss';
  166. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'rss' . DS . 'default.ctp';
  167. $result = $ThemeView->getLayoutFileName();
  168. $this->assertEquals($expected, $result);
  169. $ThemeView->layoutPath = 'Emails' . DS . 'html';
  170. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'Emails' . DS . 'html' . DS . 'default.ctp';
  171. $result = $ThemeView->getLayoutFileName();
  172. $this->assertEquals($expected, $result);
  173. }
  174. /**
  175. * testMissingView method
  176. *
  177. * @expectedException MissingViewException
  178. * @return void
  179. */
  180. public function testMissingView() {
  181. $this->Controller->plugin = null;
  182. $this->Controller->name = 'Pages';
  183. $this->Controller->viewPath = 'Pages';
  184. $this->Controller->action = 'display';
  185. $this->Controller->theme = 'my_theme';
  186. $this->Controller->params['pass'] = array('home');
  187. $View = new TestTheme2View($this->Controller);
  188. ob_start();
  189. $View->getViewFileName('does_not_exist');
  190. $expected = ob_get_clean();
  191. $this->assertRegExp("/PagesController::/", $expected);
  192. $this->assertRegExp("/views(\/|\\\)themed(\/|\\\)my_theme(\/|\\\)pages(\/|\\\)does_not_exist.ctp/", $expected);
  193. }
  194. /**
  195. * testMissingLayout method
  196. *
  197. * @expectedException MissingLayoutException
  198. * @return void
  199. */
  200. public function testMissingLayout() {
  201. $this->Controller->plugin = null;
  202. $this->Controller->name = 'Posts';
  203. $this->Controller->viewPath = 'posts';
  204. $this->Controller->layout = 'whatever';
  205. $this->Controller->theme = 'my_theme';
  206. $View = new TestTheme2View($this->Controller);
  207. ob_start();
  208. $View->getLayoutFileName();
  209. $expected = ob_get_clean();
  210. $this->assertRegExp("/Missing Layout/", $expected);
  211. $this->assertRegExp("/views(\/|\\\)themed(\/|\\\)my_theme(\/|\\\)layouts(\/|\\\)whatever.ctp/", $expected);
  212. }
  213. /**
  214. * test memory leaks that existed in _paths at one point.
  215. *
  216. * @return void
  217. */
  218. public function testMemoryLeakInPaths() {
  219. $this->Controller->plugin = null;
  220. $this->Controller->name = 'Posts';
  221. $this->Controller->viewPath = 'posts';
  222. $this->Controller->layout = 'whatever';
  223. $this->Controller->theme = 'TestTheme';
  224. $View = new ThemeView($this->Controller);
  225. $View->element('test_element');
  226. $start = memory_get_usage();
  227. for ($i = 0; $i < 10; $i++) {
  228. $View->element('test_element');
  229. }
  230. $end = memory_get_usage();
  231. $this->assertLessThanOrEqual($start + 5000, $end);
  232. }
  233. }