ViewTest.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. <?php
  2. /**
  3. * ViewTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  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/view/1196/Testing 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('Helper', 'View');
  21. App::uses('Controller', 'Controller');
  22. App::uses('CacheHelper', 'View/Helper');
  23. App::uses('ErrorHandler', 'Error');
  24. /**
  25. * ViewPostsController class
  26. *
  27. * @package Cake.Test.Case.View
  28. */
  29. class ViewPostsController extends Controller {
  30. /**
  31. * name property
  32. *
  33. * @var string 'Posts'
  34. */
  35. public $name = 'Posts';
  36. /**
  37. * uses property
  38. *
  39. * @var mixed null
  40. */
  41. public $uses = null;
  42. /**
  43. * index method
  44. *
  45. * @return void
  46. */
  47. public function index() {
  48. $this->set(array(
  49. 'testData' => 'Some test data',
  50. 'test2' => 'more data',
  51. 'test3' => 'even more data',
  52. ));
  53. }
  54. /**
  55. * nocache_tags_with_element method
  56. *
  57. * @return void
  58. */
  59. public function nocache_multiple_element() {
  60. $this->set('foo', 'this is foo var');
  61. $this->set('bar', 'this is bar var');
  62. }
  63. }
  64. /**
  65. * ThemePostsController class
  66. *
  67. * @package Cake.Test.Case.View
  68. */
  69. class ThemePostsController extends Controller {
  70. /**
  71. * name property
  72. *
  73. * @var string 'ThemePosts'
  74. */
  75. public $name = 'ThemePosts';
  76. public $theme = null;
  77. /**
  78. * index method
  79. *
  80. * @return void
  81. */
  82. public function index() {
  83. $this->set('testData', 'Some test data');
  84. $test2 = 'more data';
  85. $test3 = 'even more data';
  86. $this->set(compact('test2', 'test3'));
  87. }
  88. }
  89. /**
  90. * TestThemeView class
  91. *
  92. * @package Cake.Test.Case.View
  93. */
  94. class TestThemeView extends View {
  95. /**
  96. * renderElement method
  97. *
  98. * @param mixed $name
  99. * @param array $params
  100. * @return void
  101. */
  102. public function renderElement($name, $params = array()) {
  103. return $name;
  104. }
  105. /**
  106. * getViewFileName method
  107. *
  108. * @param mixed $name
  109. * @return void
  110. */
  111. public function getViewFileName($name = null) {
  112. return $this->_getViewFileName($name);
  113. }
  114. /**
  115. * getLayoutFileName method
  116. *
  117. * @param mixed $name
  118. * @return void
  119. */
  120. public function getLayoutFileName($name = null) {
  121. return $this->_getLayoutFileName($name);
  122. }
  123. }
  124. /**
  125. * TestView class
  126. *
  127. * @package Cake.Test.Case.View
  128. */
  129. class TestView extends View {
  130. /**
  131. * getViewFileName method
  132. *
  133. * @param mixed $name
  134. * @return void
  135. */
  136. public function getViewFileName($name = null) {
  137. return $this->_getViewFileName($name);
  138. }
  139. /**
  140. * getLayoutFileName method
  141. *
  142. * @param mixed $name
  143. * @return void
  144. */
  145. public function getLayoutFileName($name = null) {
  146. return $this->_getLayoutFileName($name);
  147. }
  148. /**
  149. * paths method
  150. *
  151. * @param string $plugin
  152. * @param boolean $cached
  153. * @return void
  154. */
  155. public function paths($plugin = null, $cached = true) {
  156. return $this->_paths($plugin, $cached);
  157. }
  158. /**
  159. * Test only function to return instance scripts.
  160. *
  161. * @return array Scripts
  162. */
  163. public function scripts() {
  164. return $this->_scripts;
  165. }
  166. }
  167. /**
  168. * TestAfterHelper class
  169. *
  170. * @package Cake.Test.Case.View
  171. */
  172. class TestAfterHelper extends Helper {
  173. /**
  174. * property property
  175. *
  176. * @var string ''
  177. */
  178. public $property = '';
  179. /**
  180. * beforeLayout method
  181. *
  182. * @return void
  183. */
  184. public function beforeLayout($viewFile) {
  185. $this->property = 'Valuation';
  186. }
  187. /**
  188. * afterLayout method
  189. *
  190. * @return void
  191. */
  192. public function afterLayout($layoutFile) {
  193. $this->_View->output .= 'modified in the afterlife';
  194. }
  195. }
  196. /**
  197. * ViewTest class
  198. *
  199. * @package Cake.Test.Case.View
  200. */
  201. class ViewTest extends CakeTestCase {
  202. /**
  203. * Fixtures used in this test.
  204. *
  205. * @var array
  206. */
  207. public $fixtures = array('core.user', 'core.post');
  208. /**
  209. * setUp method
  210. *
  211. * @return void
  212. */
  213. public function setUp() {
  214. parent::setUp();
  215. $request = $this->getMock('CakeRequest');
  216. $this->Controller = new Controller($request);
  217. $this->PostsController = new ViewPostsController($request);
  218. $this->PostsController->viewPath = 'Posts';
  219. $this->PostsController->index();
  220. $this->View = new View($this->PostsController);
  221. $themeRequest = new CakeRequest('posts/index');
  222. $this->ThemeController = new Controller($themeRequest);
  223. $this->ThemePostsController = new ThemePostsController($themeRequest);
  224. $this->ThemePostsController->viewPath = 'posts';
  225. $this->ThemePostsController->index();
  226. $this->ThemeView = new View($this->ThemePostsController);
  227. App::build(array(
  228. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  229. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  230. ), App::RESET);
  231. App::objects('plugins', null, false);
  232. CakePlugin::load(array('TestPlugin', 'TestPlugin', 'PluginJs'));
  233. Configure::write('debug', 2);
  234. }
  235. /**
  236. * tearDown method
  237. *
  238. * @return void
  239. */
  240. public function tearDown() {
  241. parent::tearDown();
  242. CakePlugin::unload();
  243. unset($this->View);
  244. unset($this->PostsController);
  245. unset($this->Controller);
  246. unset($this->ThemeView);
  247. unset($this->ThemePostsController);
  248. unset($this->ThemeController);
  249. }
  250. /**
  251. * testGetTemplate method
  252. *
  253. * @return void
  254. */
  255. public function testGetTemplate() {
  256. $this->Controller->plugin = null;
  257. $this->Controller->name = 'Pages';
  258. $this->Controller->viewPath = 'Pages';
  259. $this->Controller->action = 'display';
  260. $this->Controller->params['pass'] = array('home');
  261. $ThemeView = new TestThemeView($this->Controller);
  262. $ThemeView->theme = 'TestTheme';
  263. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages' . DS . 'home.ctp';
  264. $result = $ThemeView->getViewFileName('home');
  265. $this->assertEquals($expected, $result);
  266. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Posts' . DS . 'index.ctp';
  267. $result = $ThemeView->getViewFileName('/Posts/index');
  268. $this->assertEquals($expected, $result);
  269. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Layouts' . DS . 'default.ctp';
  270. $result = $ThemeView->getLayoutFileName();
  271. $this->assertEquals($expected, $result);
  272. $ThemeView->layoutPath = 'rss';
  273. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'rss' . DS . 'default.ctp';
  274. $result = $ThemeView->getLayoutFileName();
  275. $this->assertEquals($expected, $result);
  276. $ThemeView->layoutPath = 'Emails' . DS . 'html';
  277. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'Emails' . DS . 'html' . DS . 'default.ctp';
  278. $result = $ThemeView->getLayoutFileName();
  279. $this->assertEquals($expected, $result);
  280. }
  281. /**
  282. * testPluginGetTemplate method
  283. *
  284. * @return void
  285. */
  286. public function testPluginGetTemplate() {
  287. $this->Controller->plugin = 'TestPlugin';
  288. $this->Controller->name = 'TestPlugin';
  289. $this->Controller->viewPath = 'Tests';
  290. $this->Controller->action = 'index';
  291. $View = new TestView($this->Controller);
  292. $expected = CakePlugin::path('TestPlugin') . 'View' . DS . 'Tests' . DS . 'index.ctp';
  293. $result = $View->getViewFileName('index');
  294. $this->assertEquals($expected, $result);
  295. $expected = CakePlugin::path('TestPlugin') . 'View' . DS . 'Layouts' . DS . 'default.ctp';
  296. $result = $View->getLayoutFileName();
  297. $this->assertEquals($expected, $result);
  298. }
  299. /**
  300. * testPluginGetTemplate method
  301. *
  302. * @return void
  303. */
  304. public function testPluginThemedGetTemplate() {
  305. $this->Controller->plugin = 'TestPlugin';
  306. $this->Controller->name = 'TestPlugin';
  307. $this->Controller->viewPath = 'Tests';
  308. $this->Controller->action = 'index';
  309. $this->Controller->theme = 'TestTheme';
  310. $ThemeView = new TestThemeView($this->Controller);
  311. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Tests' . DS . 'index.ctp';
  312. $result = $ThemeView->getViewFileName('index');
  313. $this->assertEquals($expected, $result);
  314. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Layouts' . DS . 'plugin_default.ctp';
  315. $result = $ThemeView->getLayoutFileName('plugin_default');
  316. $this->assertEquals($expected, $result);
  317. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Layouts' . DS . 'default.ctp';
  318. $result = $ThemeView->getLayoutFileName('default');
  319. $this->assertEquals($expected, $result);
  320. }
  321. /**
  322. * test that plugin/$plugin_name is only appended to the paths it should be.
  323. *
  324. * @return void
  325. */
  326. public function testPluginPathGeneration() {
  327. $this->Controller->plugin = 'TestPlugin';
  328. $this->Controller->name = 'TestPlugin';
  329. $this->Controller->viewPath = 'Tests';
  330. $this->Controller->action = 'index';
  331. $View = new TestView($this->Controller);
  332. $paths = $View->paths();
  333. $expected = array_merge(App::path('View'), App::core('View'), App::core('Console/Templates/skel/View'));
  334. $this->assertEquals($expected, $paths);
  335. $paths = $View->paths('TestPlugin');
  336. $pluginPath = CakePlugin::path('TestPlugin');
  337. $expected = array(
  338. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Plugin' . DS . 'TestPlugin' . DS,
  339. $pluginPath . 'View' . DS,
  340. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS,
  341. CAKE . 'View' . DS,
  342. CAKE . 'Console' . DS . 'Templates' . DS . 'skel' . DS . 'View' . DS
  343. );
  344. $this->assertEquals($expected, $paths);
  345. }
  346. /**
  347. * test that CamelCase plugins still find their view files.
  348. *
  349. * @return void
  350. */
  351. public function testCamelCasePluginGetTemplate() {
  352. $this->Controller->plugin = 'TestPlugin';
  353. $this->Controller->name = 'TestPlugin';
  354. $this->Controller->viewPath = 'Tests';
  355. $this->Controller->action = 'index';
  356. $View = new TestView($this->Controller);
  357. App::build(array(
  358. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  359. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  360. ));
  361. $pluginPath = CakePlugin::path('TestPlugin');
  362. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'View' . DS . 'Tests' . DS . 'index.ctp';
  363. $result = $View->getViewFileName('index');
  364. $this->assertEquals($expected, $result);
  365. $expected = $pluginPath . 'View' . DS . 'Layouts' . DS . 'default.ctp';
  366. $result = $View->getLayoutFileName();
  367. $this->assertEquals($expected, $result);
  368. }
  369. /**
  370. * testGetTemplate method
  371. *
  372. * @return void
  373. */
  374. public function testGetViewFileNames() {
  375. $this->Controller->plugin = null;
  376. $this->Controller->name = 'Pages';
  377. $this->Controller->viewPath = 'Pages';
  378. $this->Controller->action = 'display';
  379. $this->Controller->params['pass'] = array('home');
  380. $View = new TestView($this->Controller);
  381. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages' . DS . 'home.ctp';
  382. $result = $View->getViewFileName('home');
  383. $this->assertEquals($expected, $result);
  384. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts' . DS . 'index.ctp';
  385. $result = $View->getViewFileName('/Posts/index');
  386. $this->assertEquals($expected, $result);
  387. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts' . DS . 'index.ctp';
  388. $result = $View->getViewFileName('../Posts/index');
  389. $this->assertEquals($expected, $result);
  390. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages' . DS . 'page.home.ctp';
  391. $result = $View->getViewFileName('page.home');
  392. $this->assertEquals($expected, $result, 'Should not ruin files with dots.');
  393. CakePlugin::load('TestPlugin');
  394. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages' . DS . 'home.ctp';
  395. $result = $View->getViewFileName('TestPlugin.home');
  396. $this->assertEquals($expected, $result, 'Plugin is missing the view, cascade to app.');
  397. $View->viewPath = 'Tests';
  398. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'View' . DS . 'Tests' . DS . 'index.ctp';
  399. $result = $View->getViewFileName('TestPlugin.index');
  400. $this->assertEquals($expected, $result);
  401. }
  402. /**
  403. * Test getting layout filenames
  404. *
  405. * @return void
  406. */
  407. public function testGetLayoutFileName() {
  408. $this->Controller->plugin = null;
  409. $this->Controller->name = 'Pages';
  410. $this->Controller->viewPath = 'Pages';
  411. $this->Controller->action = 'display';
  412. $View = new TestView($this->Controller);
  413. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'default.ctp';
  414. $result = $View->getLayoutFileName();
  415. $this->assertEquals($expected, $result);
  416. $View->layoutPath = 'rss';
  417. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'rss' . DS . 'default.ctp';
  418. $result = $View->getLayoutFileName();
  419. $this->assertEquals($expected, $result);
  420. $View->layoutPath = 'Emails' . DS . 'html';
  421. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'Emails' . DS . 'html' . DS . 'default.ctp';
  422. $result = $View->getLayoutFileName();
  423. $this->assertEquals($expected, $result);
  424. }
  425. /**
  426. * Test getting layout filenames for plugins.
  427. *
  428. * @return void
  429. */
  430. public function testGetLayoutFileNamePlugin() {
  431. $this->Controller->plugin = null;
  432. $this->Controller->name = 'Pages';
  433. $this->Controller->viewPath = 'Pages';
  434. $this->Controller->action = 'display';
  435. $View = new TestView($this->Controller);
  436. CakePlugin::load('TestPlugin');
  437. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'View' . DS . 'Layouts' . DS . 'default.ctp';
  438. $result = $View->getLayoutFileName('TestPlugin.default');
  439. $this->assertEquals($expected, $result);
  440. $View->plugin = 'TestPlugin';
  441. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'View' . DS . 'Layouts' . DS . 'default.ctp';
  442. $result = $View->getLayoutFileName('default');
  443. $this->assertEquals($expected, $result);
  444. }
  445. /**
  446. * testMissingView method
  447. *
  448. * @expectedException MissingViewException
  449. * @return void
  450. */
  451. public function testMissingView() {
  452. $this->Controller->plugin = null;
  453. $this->Controller->name = 'Pages';
  454. $this->Controller->viewPath = 'Pages';
  455. $this->Controller->action = 'display';
  456. $this->Controller->params['pass'] = array('home');
  457. $View = new TestView($this->Controller);
  458. ob_start();
  459. $result = $View->getViewFileName('does_not_exist');
  460. $this->ThemeController->plugin = null;
  461. $this->ThemeController->name = 'Pages';
  462. $this->ThemeController->viewPath = 'Pages';
  463. $this->ThemeController->action = 'display';
  464. $this->ThemeController->theme = 'my_theme';
  465. $this->ThemeController->params['pass'] = array('home');
  466. $View = new TestThemeView($this->ThemeController);
  467. ob_start();
  468. $result = $View->getViewFileName('does_not_exist');
  469. $expected = ob_get_clean();
  470. $this->assertRegExp("/PagesController::/", $expected);
  471. $this->assertRegExp("/views(\/|\\\)themed(\/|\\\)my_theme(\/|\\\)pages(\/|\\\)does_not_exist.ctp/", $expected);
  472. }
  473. /**
  474. * testMissingLayout method
  475. *
  476. * @expectedException MissingLayoutException
  477. * @return void
  478. */
  479. public function testMissingLayout() {
  480. $this->Controller->plugin = null;
  481. $this->Controller->name = 'Posts';
  482. $this->Controller->viewPath = 'Posts';
  483. $this->Controller->layout = 'whatever';
  484. $View = new TestView($this->Controller);
  485. ob_start();
  486. $result = $View->getLayoutFileName();
  487. $expected = ob_get_clean();
  488. $this->ThemeController->plugin = null;
  489. $this->ThemeController->name = 'Posts';
  490. $this->ThemeController->viewPath = 'posts';
  491. $this->ThemeController->layout = 'whatever';
  492. $this->ThemeController->theme = 'my_theme';
  493. $View = new TestThemeView($this->ThemeController);
  494. ob_start();
  495. $result = $View->getLayoutFileName();
  496. $expected = ob_get_clean();
  497. $this->assertRegExp("/Missing Layout/", $expected);
  498. $this->assertRegExp("/views(\/|\\\)themed(\/|\\\)my_theme(\/|\\\)layouts(\/|\\\)whatever.ctp/", $expected);
  499. }
  500. /**
  501. * testViewVars method
  502. *
  503. * @return void
  504. */
  505. public function testViewVars() {
  506. $this->assertEquals(array('testData' => 'Some test data', 'test2' => 'more data', 'test3' => 'even more data'), $this->View->viewVars);
  507. }
  508. /**
  509. * testUUIDGeneration method
  510. *
  511. * @return void
  512. */
  513. public function testUUIDGeneration() {
  514. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  515. $this->assertEquals('form5988016017', $result);
  516. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  517. $this->assertEquals('formc3dc6be854', $result);
  518. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  519. $this->assertEquals('form28f92cc87f', $result);
  520. }
  521. /**
  522. * testAddInlineScripts method
  523. *
  524. * @return void
  525. */
  526. public function testAddInlineScripts() {
  527. $View = new TestView($this->Controller);
  528. $View->addScript('prototype.js');
  529. $View->addScript('prototype.js');
  530. $this->assertEquals(array('prototype.js'), $View->scripts());
  531. $View->addScript('mainEvent', 'Event.observe(window, "load", function() { doSomething(); }, true);');
  532. $this->assertEquals(array('prototype.js', 'mainEvent' => 'Event.observe(window, "load", function() { doSomething(); }, true);'), $View->scripts());
  533. }
  534. /**
  535. * testElement method
  536. *
  537. * @return void
  538. */
  539. public function testElement() {
  540. $result = $this->View->element('test_element');
  541. $this->assertEquals('this is the test element', $result);
  542. $result = $this->View->element('plugin_element', array(), array('plugin' => 'TestPlugin'));
  543. $this->assertEquals('this is the plugin element using params[plugin]', $result);
  544. $result = $this->View->element('plugin_element', array(), array('plugin' => 'test_plugin'));
  545. $this->assertEquals('this is the plugin element using params[plugin]', $result);
  546. $result = $this->View->element('TestPlugin.plugin_element');
  547. $this->assertEquals('this is the plugin element using params[plugin]', $result);
  548. $result = $this->View->element('test_plugin.plugin_element');
  549. $this->assertRegExp('/Not Found:/', $result);
  550. $this->assertRegExp('/test_plugin.plugin_element/', $result);
  551. $this->View->plugin = 'TestPlugin';
  552. $result = $this->View->element('test_plugin_element');
  553. $this->assertEquals('this is the test set using View::$plugin plugin element', $result);
  554. $result = $this->View->element('non_existent_element');
  555. $this->assertRegExp('/Not Found:/', $result);
  556. $this->assertRegExp('/non_existent_element/', $result);
  557. $result = $this->View->element('TestPlugin.plugin_element', array(), array('plugin' => 'test_plugin'));
  558. $this->assertRegExp('/Not Found:/', $result);
  559. $this->assertRegExp('/TestPlugin.plugin_element/', $result);
  560. }
  561. /**
  562. * test that elements can have callbacks
  563. *
  564. */
  565. public function testElementCallbacks() {
  566. $this->getMock('HtmlHelper', array(), array($this->View), 'ElementCallbackMockHtmlHelper');
  567. $this->View->helpers = array('ElementCallbackMockHtml');
  568. $this->View->loadHelpers();
  569. $this->View->ElementCallbackMockHtml->expects($this->at(0))->method('beforeRender');
  570. $this->View->ElementCallbackMockHtml->expects($this->at(1))->method('afterRender');
  571. $this->View->element('test_element', array(), array('callbacks' => true));
  572. $this->mockObjects[] = $this->View->ElementCallbackMockHtml;
  573. }
  574. /**
  575. * test that additional element viewVars don't get overwritten with helpers.
  576. *
  577. * @return void
  578. */
  579. public function testElementParamsDontOverwriteHelpers() {
  580. $Controller = new ViewPostsController();
  581. $Controller->helpers = array('Form');
  582. $View = new View($Controller);
  583. $result = $View->element('type_check', array('form' => 'string'), array('callbacks' => true));
  584. $this->assertEquals('string', $result);
  585. $View->set('form', 'string');
  586. $result = $View->element('type_check', array(), array('callbacks' => true));
  587. $this->assertEquals('string', $result);
  588. }
  589. /**
  590. * testElementCacheHelperNoCache method
  591. *
  592. * @return void
  593. */
  594. public function testElementCacheHelperNoCache() {
  595. $Controller = new ViewPostsController();
  596. $View = new TestView($Controller);
  597. $helpers = $View->loadHelpers();
  598. $result = $View->element('test_element', array('ram' => 'val', 'test' => array('foo', 'bar')));
  599. $this->assertEquals('this is the test element', $result);
  600. }
  601. /**
  602. * testElementCache method
  603. *
  604. * @return void
  605. */
  606. public function testElementCache() {
  607. Cache::drop('test_view');
  608. Cache::config('test_view', array(
  609. 'engine' => 'File',
  610. 'duration' => '+1 day',
  611. 'path' => CACHE . 'views' . DS,
  612. 'prefix' => ''
  613. ));
  614. Cache::clear(true, 'test_view');
  615. $View = new TestView($this->PostsController);
  616. $View->elementCache = 'test_view';
  617. $result = $View->element('test_element', array(), array('cache' => true));
  618. $expected = 'this is the test element';
  619. $this->assertEquals($expected, $result);
  620. $result = Cache::read('element__test_element_cache', 'test_view');
  621. $this->assertEquals($expected, $result);
  622. $result = $View->element('test_element', array('param' => 'one', 'foo' => 'two'), array('cache' => true));
  623. $this->assertEquals($expected, $result);
  624. $result = Cache::read('element__test_element_cache_param_foo', 'test_view');
  625. $this->assertEquals($expected, $result);
  626. $result = $View->element('test_element', array(
  627. 'param' => 'one',
  628. 'foo' => 'two'
  629. ), array(
  630. 'cache' => array('key' => 'custom_key')
  631. ));
  632. $result = Cache::read('element_custom_key', 'test_view');
  633. $this->assertEquals($expected, $result);
  634. $View->elementCache = 'default';
  635. $result = $View->element('test_element', array(
  636. 'param' => 'one',
  637. 'foo' => 'two'
  638. ), array(
  639. 'cache' => array('config' => 'test_view'),
  640. ));
  641. $result = Cache::read('element__test_element_cache_param_foo', 'test_view');
  642. $this->assertEquals($expected, $result);
  643. Cache::clear(true, 'test_view');
  644. Cache::drop('test_view');
  645. }
  646. /**
  647. * test __get allowing access to helpers.
  648. *
  649. * @return void
  650. */
  651. public function testMagicGet() {
  652. $View = new View($this->PostsController);
  653. $View->loadHelper('Html');
  654. $this->assertInstanceOf('HtmlHelper', $View->Html);
  655. }
  656. /**
  657. * test that ctp is used as a fallback file extension for elements
  658. *
  659. * @return void
  660. */
  661. public function testElementCtpFallback() {
  662. $View = new TestView($this->PostsController);
  663. $View->ext = '.missing';
  664. $element = 'test_element';
  665. $expected = 'this is the test element';
  666. $result = $View->element($element);
  667. $this->assertEquals($expected, $result);
  668. }
  669. /**
  670. * testLoadHelpers method
  671. *
  672. * @return void
  673. */
  674. public function testLoadHelpers() {
  675. $View = new View($this->PostsController);
  676. $View->helpers = array('Html', 'Form');
  677. $View->loadHelpers();
  678. $this->assertInstanceOf('HtmlHelper', $View->Html, 'Object type is wrong.');
  679. $this->assertInstanceOf('FormHelper', $View->Form, 'Object type is wrong.');
  680. }
  681. /**
  682. * test the correct triggering of helper callbacks
  683. *
  684. * @return void
  685. */
  686. public function testHelperCallbackTriggering() {
  687. $View = new View($this->PostsController);
  688. $View->helpers = array();
  689. $View->Helpers = $this->getMock('HelperCollection', array('trigger'), array($View));
  690. $View->Helpers->expects($this->at(0))->method('trigger')
  691. ->with(
  692. $this->logicalAnd(
  693. $this->isInstanceOf('CakeEvent'),
  694. $this->attributeEqualTo('_name', 'View.beforeRender'),
  695. $this->attributeEqualTo('_subject', $View)
  696. )
  697. );
  698. $View->Helpers->expects($this->at(1))->method('trigger')
  699. ->with(
  700. $this->logicalAnd(
  701. $this->isInstanceOf('CakeEvent'),
  702. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  703. $this->attributeEqualTo('_subject', $View)
  704. )
  705. );
  706. $View->Helpers->expects($this->at(2))->method('trigger')
  707. ->with(
  708. $this->logicalAnd(
  709. $this->isInstanceOf('CakeEvent'),
  710. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  711. $this->attributeEqualTo('_subject', $View)
  712. )
  713. );
  714. $View->Helpers->expects($this->at(3))->method('trigger')
  715. ->with(
  716. $this->logicalAnd(
  717. $this->isInstanceOf('CakeEvent'),
  718. $this->attributeEqualTo('_name', 'View.afterRender'),
  719. $this->attributeEqualTo('_subject', $View)
  720. )
  721. );
  722. $View->Helpers->expects($this->at(4))->method('trigger')
  723. ->with(
  724. $this->logicalAnd(
  725. $this->isInstanceOf('CakeEvent'),
  726. $this->attributeEqualTo('_name', 'View.beforeLayout'),
  727. $this->attributeEqualTo('_subject', $View)
  728. )
  729. );
  730. $View->Helpers->expects($this->at(5))->method('trigger')
  731. ->with(
  732. $this->logicalAnd(
  733. $this->isInstanceOf('CakeEvent'),
  734. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  735. $this->attributeEqualTo('_subject', $View)
  736. )
  737. );
  738. $View->Helpers->expects($this->at(6))->method('trigger')
  739. ->with(
  740. $this->logicalAnd(
  741. $this->isInstanceOf('CakeEvent'),
  742. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  743. $this->attributeEqualTo('_subject', $View)
  744. )
  745. );
  746. $View->Helpers->expects($this->at(7))->method('trigger')
  747. ->with(
  748. $this->logicalAnd(
  749. $this->isInstanceOf('CakeEvent'),
  750. $this->attributeEqualTo('_name', 'View.afterLayout'),
  751. $this->attributeEqualTo('_subject', $View)
  752. )
  753. );
  754. $View->render('index');
  755. }
  756. /**
  757. * testBeforeLayout method
  758. *
  759. * @return void
  760. */
  761. public function testBeforeLayout() {
  762. $this->PostsController->helpers = array('Session', 'TestAfter', 'Html');
  763. $View = new View($this->PostsController);
  764. $View->render('index');
  765. $this->assertEquals('Valuation', $View->Helpers->TestAfter->property);
  766. }
  767. /**
  768. * testAfterLayout method
  769. *
  770. * @return void
  771. */
  772. public function testAfterLayout() {
  773. $this->PostsController->helpers = array('Session', 'TestAfter', 'Html');
  774. $this->PostsController->set('variable', 'values');
  775. $View = new View($this->PostsController);
  776. ClassRegistry::addObject('afterView', $View);
  777. $content = 'This is my view output';
  778. $result = $View->renderLayout($content, 'default');
  779. $this->assertRegExp('/modified in the afterlife/', $result);
  780. $this->assertRegExp('/This is my view output/', $result);
  781. }
  782. /**
  783. * testRenderLoadHelper method
  784. *
  785. * @return void
  786. */
  787. public function testRenderLoadHelper() {
  788. $this->PostsController->helpers = array('Session', 'Html', 'Form', 'Number');
  789. $View = new TestView($this->PostsController);
  790. $result = $View->render('index', false);
  791. $this->assertEquals('posts index', $result);
  792. $attached = $View->Helpers->attached();
  793. $this->assertEquals(array('Session', 'Html', 'Form', 'Number'), $attached);
  794. $this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
  795. $View = new TestView($this->PostsController);
  796. $result = $View->render('index', false);
  797. $this->assertEquals('posts index', $result);
  798. $attached = $View->Helpers->attached();
  799. $expected = array('Html', 'Form', 'Number', 'PluggedHelper');
  800. $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
  801. }
  802. /**
  803. * testRender method
  804. *
  805. * @return void
  806. */
  807. public function testRender() {
  808. $View = new TestView($this->PostsController);
  809. $result = $View->render('index');
  810. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  811. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  812. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  813. $this->assertTrue(isset($View->viewVars['content_for_layout']), 'content_for_layout should be a view var');
  814. $this->assertTrue(isset($View->viewVars['scripts_for_layout']), 'scripts_for_layout should be a view var');
  815. $this->PostsController->set('url', 'flash');
  816. $this->PostsController->set('message', 'yo what up');
  817. $this->PostsController->set('pause', 3);
  818. $this->PostsController->set('page_title', 'yo what up');
  819. $View = new TestView($this->PostsController);
  820. $result = $View->render(false, 'flash');
  821. $this->assertRegExp("/<title>yo what up<\/title>/", $result);
  822. $this->assertRegExp("/<p><a href=\"flash\">yo what up<\/a><\/p>/", $result);
  823. $this->assertTrue($View->render(false, 'flash'));
  824. $this->PostsController->helpers = array('Session', 'Cache', 'Html');
  825. $this->PostsController->constructClasses();
  826. $this->PostsController->cacheAction = array('index' => 3600);
  827. $this->PostsController->request->params['action'] = 'index';
  828. Configure::write('Cache.check', true);
  829. $View = new TestView($this->PostsController);
  830. $result = $View->render('index');
  831. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  832. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  833. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  834. }
  835. /**
  836. * test that View::$view works
  837. *
  838. * @return void
  839. */
  840. public function testRenderUsingViewProperty() {
  841. $this->PostsController->view = 'cache_form';
  842. $View = new TestView($this->PostsController);
  843. $this->assertEquals('cache_form', $View->view);
  844. $result = $View->render();
  845. $this->assertRegExp('/Add User/', $result);
  846. }
  847. /**
  848. * Test render()ing a file in a subdir from a custom viewPath
  849. * in a plugin.
  850. *
  851. * @return void
  852. */
  853. public function testGetViewFileNameSubdirWithPluginAndViewPath() {
  854. $this->PostsController->plugin = 'TestPlugin';
  855. $this->PostsController->viewPath = 'Elements';
  856. $this->PostsController->name = 'Posts';
  857. $View = new TestView($this->PostsController);
  858. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' .
  859. DS . 'View' . DS . 'Elements' . DS . 'sub_dir' . DS . 'sub_element.ctp';
  860. $this->assertEquals($expected, $View->getViewFileName('sub_dir/sub_element'));
  861. }
  862. /**
  863. * test that view vars can replace the local helper variables
  864. * and not overwrite the $this->Helper references
  865. *
  866. * @return void
  867. */
  868. public function testViewVarOverwritingLocalHelperVar() {
  869. $Controller = new ViewPostsController();
  870. $Controller->helpers = array('Session', 'Html');
  871. $Controller->set('html', 'I am some test html');
  872. $View = new View($Controller);
  873. $result = $View->render('helper_overwrite', false);
  874. $this->assertRegExp('/I am some test html/', $result);
  875. $this->assertRegExp('/Test link/', $result);
  876. }
  877. /**
  878. * testGetViewFileName method
  879. *
  880. * @return void
  881. */
  882. public function testViewFileName() {
  883. $View = new TestView($this->PostsController);
  884. $result = $View->getViewFileName('index');
  885. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  886. $result = $View->getViewFileName('TestPlugin.index');
  887. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  888. $result = $View->getViewFileName('/Pages/home');
  889. $this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
  890. $result = $View->getViewFileName('../Elements/test_element');
  891. $this->assertRegExp('/Elements(\/|\\\)test_element.ctp/', $result);
  892. $result = $View->getViewFileName('../Themed/TestTheme/Posts/index');
  893. $this->assertRegExp('/Themed(\/|\\\)TestTheme(\/|\\\)Posts(\/|\\\)index.ctp/', $result);
  894. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts' . DS . 'index.ctp';
  895. $result = $View->getViewFileName('../Posts/index');
  896. $this->assertEquals($expected, $result);
  897. }
  898. /**
  899. * testRenderCache method
  900. *
  901. * @return void
  902. */
  903. public function testRenderCache() {
  904. $this->skipIf(!is_writable(CACHE . 'views' . DS), 'CACHE/views dir is not writable, cannot test renderCache.');
  905. $view = 'test_view';
  906. $View = new View($this->PostsController);
  907. $path = CACHE . 'views' . DS . 'view_cache_' . $view;
  908. $cacheText = '<!--cachetime:' . time() . '-->some cacheText';
  909. $f = fopen($path, 'w+');
  910. fwrite($f, $cacheText);
  911. fclose($f);
  912. $result = $View->renderCache($path, '+1 second');
  913. $this->assertFalse($result);
  914. if (file_exists($path)) {
  915. unlink($path);
  916. }
  917. $cacheText = '<!--cachetime:' . (time() + 10) . '-->some cacheText';
  918. $f = fopen($path, 'w+');
  919. fwrite($f, $cacheText);
  920. fclose($f);
  921. ob_start();
  922. $View->renderCache($path, '+1 second');
  923. $result = ob_get_clean();
  924. $this->assertRegExp('/^some cacheText/', $result);
  925. @unlink($path);
  926. }
  927. /**
  928. * Test that render() will remove the cake:nocache tags when only the cachehelper is present.
  929. *
  930. * @return void
  931. */
  932. public function testRenderStrippingNoCacheTagsOnlyCacheHelper() {
  933. Configure::write('Cache.check', false);
  934. $View = new View($this->PostsController);
  935. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  936. $View->helpers = array('Html', 'Form', 'Cache');
  937. $View->layout = 'cache_layout';
  938. $result = $View->render('index');
  939. $this->assertNotRegExp('/cake:nocache/', $result);
  940. }
  941. /**
  942. * Test that render() will remove the cake:nocache tags when only the Cache.check is true.
  943. *
  944. * @return void
  945. */
  946. public function testRenderStrippingNoCacheTagsOnlyCacheCheck() {
  947. Configure::write('Cache.check', true);
  948. $View = new View($this->PostsController);
  949. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  950. $View->helpers = array('Html', 'Form');
  951. $View->layout = 'cache_layout';
  952. $result = $View->render('index');
  953. $this->assertNotRegExp('/cake:nocache/', $result);
  954. }
  955. /**
  956. * testSet method
  957. *
  958. * @return void
  959. */
  960. public function testSet() {
  961. $View = new TestView($this->PostsController);
  962. $View->viewVars = array();
  963. $View->set('somekey', 'someValue');
  964. $this->assertSame($View->viewVars, array('somekey' => 'someValue'));
  965. $this->assertSame($View->getVars(), array('somekey'));
  966. $View->viewVars = array();
  967. $keys = array('key1', 'key2');
  968. $values = array('value1', 'value2');
  969. $View->set($keys, $values);
  970. $this->assertSame($View->viewVars, array('key1' => 'value1', 'key2' => 'value2'));
  971. $this->assertSame($View->getVars(), array('key1', 'key2'));
  972. $this->assertSame($View->getVar('key1'), 'value1');
  973. $this->assertNull($View->getVar('key3'));
  974. $View->set(array('key3' => 'value3'));
  975. $this->assertSame($View->getVar('key3'), 'value3');
  976. $View->viewVars = array();
  977. $View->set(array(3 => 'three', 4 => 'four'));
  978. $View->set(array(1 => 'one', 2 => 'two'));
  979. $expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two');
  980. $this->assertEquals($expected, $View->viewVars);
  981. }
  982. /**
  983. * testBadExt method
  984. *
  985. * @expectedException MissingViewException
  986. * @return void
  987. */
  988. public function testBadExt() {
  989. $this->PostsController->action = 'something';
  990. $this->PostsController->ext = '.whatever';
  991. $View = new TestView($this->PostsController);
  992. $View->render('this_is_missing');
  993. }
  994. /**
  995. * testAltExt method
  996. *
  997. * @return void
  998. */
  999. public function testAltExt() {
  1000. $this->PostsController->ext = '.alt';
  1001. $View = new TestView($this->PostsController);
  1002. $result = $View->render('alt_ext', false);
  1003. $this->assertEquals('alt ext', $result);
  1004. }
  1005. /**
  1006. * testAltBadExt method
  1007. *
  1008. * @expectedException MissingViewException
  1009. * @return void
  1010. */
  1011. public function testAltBadExt() {
  1012. $View = new TestView($this->PostsController);
  1013. $View->render('alt_ext');
  1014. }
  1015. /**
  1016. * Test creating a block with capturing output.
  1017. *
  1018. * @return void
  1019. */
  1020. public function testBlockCapture() {
  1021. $this->View->start('test');
  1022. echo 'Block content';
  1023. $this->View->end();
  1024. $result = $this->View->fetch('test');
  1025. $this->assertEquals('Block content', $result);
  1026. }
  1027. /**
  1028. * Test appending to a block with capturing output.
  1029. *
  1030. * @return void
  1031. */
  1032. public function testBlockCaptureAppend() {
  1033. $this->View->start('test');
  1034. echo 'Block';
  1035. $this->View->end();
  1036. $this->View->append('test');
  1037. echo ' content';
  1038. $this->View->end();
  1039. $result = $this->View->fetch('test');
  1040. $this->assertEquals('Block content', $result);
  1041. }
  1042. /**
  1043. * Test setting a block's content.
  1044. *
  1045. * @return void
  1046. */
  1047. public function testBlockSet() {
  1048. $this->View->assign('test', 'Block content');
  1049. $result = $this->View->fetch('test');
  1050. $this->assertEquals('Block content', $result);
  1051. }
  1052. /**
  1053. * Test appending to a block with append.
  1054. *
  1055. * @return void
  1056. */
  1057. public function testBlockAppend() {
  1058. $this->View->assign('test', 'Block');
  1059. $this->View->append('test', ' content');
  1060. $result = $this->View->fetch('test');
  1061. $this->assertEquals('Block content', $result);
  1062. }
  1063. /**
  1064. * You should be able to append to undefined blocks.
  1065. *
  1066. * @return void
  1067. */
  1068. public function testBlockAppendUndefined() {
  1069. $this->View->append('test', 'Unknown');
  1070. $result = $this->View->fetch('test');
  1071. $this->assertEquals('Unknown', $result);
  1072. }
  1073. /**
  1074. * setting an array should cause an exception.
  1075. *
  1076. * @expectedException CakeException
  1077. * @return void
  1078. */
  1079. public function testBlockSetArrayException() {
  1080. $this->View->assign('test', array(1, 2, 3));
  1081. }
  1082. /**
  1083. * Appending an array should cause an exception.
  1084. *
  1085. * @expectedException CakeException
  1086. * @return void
  1087. */
  1088. public function testBlockAppendArrayException() {
  1089. $this->View->append('test', array(1, 2, 3));
  1090. }
  1091. /**
  1092. * Test getting block names
  1093. *
  1094. * @return void
  1095. */
  1096. public function testBlocks() {
  1097. $this->View->append('test', 'one');
  1098. $this->View->assign('test1', 'one');
  1099. $this->assertEquals(array('test', 'test1'), $this->View->blocks());
  1100. }
  1101. /**
  1102. * Test that blocks can be nested.
  1103. *
  1104. * @return void
  1105. */
  1106. public function testNestedBlocks() {
  1107. $this->View->start('first');
  1108. echo 'In first ';
  1109. $this->View->start('second');
  1110. echo 'In second';
  1111. $this->View->end();
  1112. echo 'In first';
  1113. $this->View->end();
  1114. $this->assertEquals('In first In first', $this->View->fetch('first'));
  1115. $this->assertEquals('In second', $this->View->fetch('second'));
  1116. }
  1117. /**
  1118. * Test that an exception gets thrown when you leave a block open at the end
  1119. * of a view.
  1120. *
  1121. * @expectedException CakeException
  1122. * @return void
  1123. */
  1124. public function testExceptionOnOpenBlock() {
  1125. $this->View->render('open_block');
  1126. }
  1127. /**
  1128. * Test nested extended views.
  1129. *
  1130. * @return void
  1131. */
  1132. public function testExtendNested() {
  1133. $this->View->layout = false;
  1134. $content = $this->View->render('nested_extends');
  1135. $expected = <<<TEXT
  1136. This is the second parent.
  1137. This is the first parent.
  1138. This is the first template.
  1139. Sidebar Content.
  1140. TEXT;
  1141. $this->assertEquals($expected, $content);
  1142. }
  1143. /**
  1144. * Make sure that extending the current view with itself causes an exception
  1145. *
  1146. * @expectedException LogicException
  1147. * @return void
  1148. */
  1149. public function testExtendSelf() {
  1150. $this->View->layout = false;
  1151. $this->View->render('extend_self');
  1152. }
  1153. /**
  1154. * Make sure that extending in a loop causes an exception
  1155. *
  1156. * @expectedException LogicException
  1157. * @return void
  1158. */
  1159. public function testExtendLoop() {
  1160. $this->View->layout = false;
  1161. $this->View->render('extend_loop');
  1162. }
  1163. /**
  1164. * Test extend() in an element and a view.
  1165. *
  1166. * @return void
  1167. */
  1168. public function testExtendElement() {
  1169. $this->View->layout = false;
  1170. $content = $this->View->render('extend_element');
  1171. $expected = <<<TEXT
  1172. Parent View.
  1173. View content.
  1174. Parent Element.
  1175. Element content.
  1176. TEXT;
  1177. $this->assertEquals($expected, $content);
  1178. }
  1179. /**
  1180. * Extending an element which doesn't exist should throw a missing view exception
  1181. *
  1182. * @expectedException LogicException
  1183. * @return void
  1184. */
  1185. public function testExtendMissingElement() {
  1186. $this->View->layout = false;
  1187. $this->View->render('extend_missing_element');
  1188. }
  1189. /**
  1190. * Test that setting arbitrary properties still works.
  1191. *
  1192. * @return void
  1193. */
  1194. public function testPropertySetting() {
  1195. $this->assertFalse(isset($this->View->pageTitle));
  1196. $this->View->pageTitle = 'test';
  1197. $this->assertTrue(isset($this->View->pageTitle));
  1198. $this->assertTrue(!empty($this->View->pageTitle));
  1199. $this->assertEquals('test', $this->View->pageTitle);
  1200. }
  1201. /**
  1202. * Test that setting arbitrary properties still works.
  1203. *
  1204. * @return void
  1205. */
  1206. public function testPropertySettingMagicGet() {
  1207. $this->assertFalse(isset($this->View->action));
  1208. $this->View->request->params['action'] = 'login';
  1209. $this->assertEquals('login', $this->View->action);
  1210. $this->assertTrue(isset($this->View->action));
  1211. $this->assertTrue(!empty($this->View->action));
  1212. }
  1213. /**
  1214. * test memory leaks that existed in _paths at one point.
  1215. *
  1216. * @return void
  1217. */
  1218. public function testMemoryLeakInPaths() {
  1219. $this->ThemeController->plugin = null;
  1220. $this->ThemeController->name = 'Posts';
  1221. $this->ThemeController->viewPath = 'posts';
  1222. $this->ThemeController->layout = 'whatever';
  1223. $this->ThemeController->theme = 'TestTheme';
  1224. $View = new View($this->ThemeController);
  1225. $View->element('test_element');
  1226. $start = memory_get_usage();
  1227. for ($i = 0; $i < 10; $i++) {
  1228. $View->element('test_element');
  1229. }
  1230. $end = memory_get_usage();
  1231. $this->assertLessThanOrEqual($start + 5000, $end);
  1232. }
  1233. }