ViewTest.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  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 lazy loading helpers
  683. *
  684. * @return void
  685. */
  686. public function testLazyLoadHelpers() {
  687. $View = new View($this->PostsController);
  688. $View->helpers = array();
  689. $this->assertInstanceOf('HtmlHelper', $View->Html, 'Object type is wrong.');
  690. $this->assertInstanceOf('FormHelper', $View->Form, 'Object type is wrong.');
  691. }
  692. /**
  693. * test the correct triggering of helper callbacks
  694. *
  695. * @return void
  696. */
  697. public function testHelperCallbackTriggering() {
  698. $View = new View($this->PostsController);
  699. $View->helpers = array();
  700. $View->Helpers = $this->getMock('HelperCollection', array('trigger'), array($View));
  701. $View->Helpers->expects($this->at(0))->method('trigger')
  702. ->with(
  703. $this->logicalAnd(
  704. $this->isInstanceOf('CakeEvent'),
  705. $this->attributeEqualTo('_name', 'View.beforeRender'),
  706. $this->attributeEqualTo('_subject', $View)
  707. )
  708. );
  709. $View->Helpers->expects($this->at(1))->method('trigger')
  710. ->with(
  711. $this->logicalAnd(
  712. $this->isInstanceOf('CakeEvent'),
  713. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  714. $this->attributeEqualTo('_subject', $View)
  715. )
  716. );
  717. $View->Helpers->expects($this->at(2))->method('trigger')
  718. ->with(
  719. $this->logicalAnd(
  720. $this->isInstanceOf('CakeEvent'),
  721. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  722. $this->attributeEqualTo('_subject', $View)
  723. )
  724. );
  725. $View->Helpers->expects($this->at(3))->method('trigger')
  726. ->with(
  727. $this->logicalAnd(
  728. $this->isInstanceOf('CakeEvent'),
  729. $this->attributeEqualTo('_name', 'View.afterRender'),
  730. $this->attributeEqualTo('_subject', $View)
  731. )
  732. );
  733. $View->Helpers->expects($this->at(4))->method('trigger')
  734. ->with(
  735. $this->logicalAnd(
  736. $this->isInstanceOf('CakeEvent'),
  737. $this->attributeEqualTo('_name', 'View.beforeLayout'),
  738. $this->attributeEqualTo('_subject', $View)
  739. )
  740. );
  741. $View->Helpers->expects($this->at(5))->method('trigger')
  742. ->with(
  743. $this->logicalAnd(
  744. $this->isInstanceOf('CakeEvent'),
  745. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  746. $this->attributeEqualTo('_subject', $View)
  747. )
  748. );
  749. $View->Helpers->expects($this->at(6))->method('trigger')
  750. ->with(
  751. $this->logicalAnd(
  752. $this->isInstanceOf('CakeEvent'),
  753. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  754. $this->attributeEqualTo('_subject', $View)
  755. )
  756. );
  757. $View->Helpers->expects($this->at(7))->method('trigger')
  758. ->with(
  759. $this->logicalAnd(
  760. $this->isInstanceOf('CakeEvent'),
  761. $this->attributeEqualTo('_name', 'View.afterLayout'),
  762. $this->attributeEqualTo('_subject', $View)
  763. )
  764. );
  765. $View->render('index');
  766. }
  767. /**
  768. * testBeforeLayout method
  769. *
  770. * @return void
  771. */
  772. public function testBeforeLayout() {
  773. $this->PostsController->helpers = array('Session', 'TestAfter', 'Html');
  774. $View = new View($this->PostsController);
  775. $View->render('index');
  776. $this->assertEquals('Valuation', $View->Helpers->TestAfter->property);
  777. }
  778. /**
  779. * testAfterLayout method
  780. *
  781. * @return void
  782. */
  783. public function testAfterLayout() {
  784. $this->PostsController->helpers = array('Session', 'TestAfter', 'Html');
  785. $this->PostsController->set('variable', 'values');
  786. $View = new View($this->PostsController);
  787. ClassRegistry::addObject('afterView', $View);
  788. $content = 'This is my view output';
  789. $result = $View->renderLayout($content, 'default');
  790. $this->assertRegExp('/modified in the afterlife/', $result);
  791. $this->assertRegExp('/This is my view output/', $result);
  792. }
  793. /**
  794. * testRenderLoadHelper method
  795. *
  796. * @return void
  797. */
  798. public function testRenderLoadHelper() {
  799. $this->PostsController->helpers = array('Session', 'Html', 'Form', 'Number');
  800. $View = new TestView($this->PostsController);
  801. $result = $View->render('index', false);
  802. $this->assertEquals('posts index', $result);
  803. $attached = $View->Helpers->attached();
  804. $this->assertEquals(array('Session', 'Html', 'Form', 'Number'), $attached);
  805. $this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
  806. $View = new TestView($this->PostsController);
  807. $result = $View->render('index', false);
  808. $this->assertEquals('posts index', $result);
  809. $attached = $View->Helpers->attached();
  810. $expected = array('Html', 'Form', 'Number', 'PluggedHelper');
  811. $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
  812. }
  813. /**
  814. * testRender method
  815. *
  816. * @return void
  817. */
  818. public function testRender() {
  819. $View = new TestView($this->PostsController);
  820. $result = $View->render('index');
  821. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  822. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  823. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  824. $this->assertTrue(isset($View->viewVars['content_for_layout']), 'content_for_layout should be a view var');
  825. $this->assertTrue(isset($View->viewVars['scripts_for_layout']), 'scripts_for_layout should be a view var');
  826. $this->PostsController->set('url', 'flash');
  827. $this->PostsController->set('message', 'yo what up');
  828. $this->PostsController->set('pause', 3);
  829. $this->PostsController->set('page_title', 'yo what up');
  830. $View = new TestView($this->PostsController);
  831. $result = $View->render(false, 'flash');
  832. $this->assertRegExp("/<title>yo what up<\/title>/", $result);
  833. $this->assertRegExp("/<p><a href=\"flash\">yo what up<\/a><\/p>/", $result);
  834. $this->assertTrue($View->render(false, 'flash'));
  835. $this->PostsController->helpers = array('Session', 'Cache', 'Html');
  836. $this->PostsController->constructClasses();
  837. $this->PostsController->cacheAction = array('index' => 3600);
  838. $this->PostsController->request->params['action'] = 'index';
  839. Configure::write('Cache.check', true);
  840. $View = new TestView($this->PostsController);
  841. $result = $View->render('index');
  842. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  843. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  844. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  845. }
  846. /**
  847. * test that View::$view works
  848. *
  849. * @return void
  850. */
  851. public function testRenderUsingViewProperty() {
  852. $this->PostsController->view = 'cache_form';
  853. $View = new TestView($this->PostsController);
  854. $this->assertEquals('cache_form', $View->view);
  855. $result = $View->render();
  856. $this->assertRegExp('/Add User/', $result);
  857. }
  858. /**
  859. * Test render()ing a file in a subdir from a custom viewPath
  860. * in a plugin.
  861. *
  862. * @return void
  863. */
  864. public function testGetViewFileNameSubdirWithPluginAndViewPath() {
  865. $this->PostsController->plugin = 'TestPlugin';
  866. $this->PostsController->viewPath = 'Elements';
  867. $this->PostsController->name = 'Posts';
  868. $View = new TestView($this->PostsController);
  869. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' .
  870. DS . 'View' . DS . 'Elements' . DS . 'sub_dir' . DS . 'sub_element.ctp';
  871. $this->assertEquals($expected, $View->getViewFileName('sub_dir/sub_element'));
  872. }
  873. /**
  874. * test that view vars can replace the local helper variables
  875. * and not overwrite the $this->Helper references
  876. *
  877. * @return void
  878. */
  879. public function testViewVarOverwritingLocalHelperVar() {
  880. $Controller = new ViewPostsController();
  881. $Controller->helpers = array('Session', 'Html');
  882. $Controller->set('html', 'I am some test html');
  883. $View = new View($Controller);
  884. $result = $View->render('helper_overwrite', false);
  885. $this->assertRegExp('/I am some test html/', $result);
  886. $this->assertRegExp('/Test link/', $result);
  887. }
  888. /**
  889. * testGetViewFileName method
  890. *
  891. * @return void
  892. */
  893. public function testViewFileName() {
  894. $View = new TestView($this->PostsController);
  895. $result = $View->getViewFileName('index');
  896. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  897. $result = $View->getViewFileName('TestPlugin.index');
  898. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  899. $result = $View->getViewFileName('/Pages/home');
  900. $this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
  901. $result = $View->getViewFileName('../Elements/test_element');
  902. $this->assertRegExp('/Elements(\/|\\\)test_element.ctp/', $result);
  903. $result = $View->getViewFileName('../Themed/TestTheme/Posts/index');
  904. $this->assertRegExp('/Themed(\/|\\\)TestTheme(\/|\\\)Posts(\/|\\\)index.ctp/', $result);
  905. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts' . DS . 'index.ctp';
  906. $result = $View->getViewFileName('../Posts/index');
  907. $this->assertEquals($expected, $result);
  908. }
  909. /**
  910. * testRenderCache method
  911. *
  912. * @return void
  913. */
  914. public function testRenderCache() {
  915. $this->skipIf(!is_writable(CACHE . 'views' . DS), 'CACHE/views dir is not writable, cannot test renderCache.');
  916. $view = 'test_view';
  917. $View = new View($this->PostsController);
  918. $path = CACHE . 'views' . DS . 'view_cache_' . $view;
  919. $cacheText = '<!--cachetime:' . time() . '-->some cacheText';
  920. $f = fopen($path, 'w+');
  921. fwrite($f, $cacheText);
  922. fclose($f);
  923. $result = $View->renderCache($path, '+1 second');
  924. $this->assertFalse($result);
  925. if (file_exists($path)) {
  926. unlink($path);
  927. }
  928. $cacheText = '<!--cachetime:' . (time() + 10) . '-->some cacheText';
  929. $f = fopen($path, 'w+');
  930. fwrite($f, $cacheText);
  931. fclose($f);
  932. $result = $View->renderCache($path, '+1 second');
  933. $this->assertRegExp('/^some cacheText/', $result);
  934. @unlink($path);
  935. }
  936. /**
  937. * Test that render() will remove the cake:nocache tags when only the cachehelper is present.
  938. *
  939. * @return void
  940. */
  941. public function testRenderStrippingNoCacheTagsOnlyCacheHelper() {
  942. Configure::write('Cache.check', false);
  943. $View = new View($this->PostsController);
  944. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  945. $View->helpers = array('Html', 'Form', 'Cache');
  946. $View->layout = 'cache_layout';
  947. $result = $View->render('index');
  948. $this->assertNotRegExp('/cake:nocache/', $result);
  949. }
  950. /**
  951. * Test that render() will remove the cake:nocache tags when only the Cache.check is true.
  952. *
  953. * @return void
  954. */
  955. public function testRenderStrippingNoCacheTagsOnlyCacheCheck() {
  956. Configure::write('Cache.check', true);
  957. $View = new View($this->PostsController);
  958. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  959. $View->helpers = array('Html', 'Form');
  960. $View->layout = 'cache_layout';
  961. $result = $View->render('index');
  962. $this->assertNotRegExp('/cake:nocache/', $result);
  963. }
  964. /**
  965. * testSet method
  966. *
  967. * @return void
  968. */
  969. public function testSet() {
  970. $View = new TestView($this->PostsController);
  971. $View->viewVars = array();
  972. $View->set('somekey', 'someValue');
  973. $this->assertSame($View->viewVars, array('somekey' => 'someValue'));
  974. $this->assertSame($View->getVars(), array('somekey'));
  975. $View->viewVars = array();
  976. $keys = array('key1', 'key2');
  977. $values = array('value1', 'value2');
  978. $View->set($keys, $values);
  979. $this->assertSame($View->viewVars, array('key1' => 'value1', 'key2' => 'value2'));
  980. $this->assertSame($View->getVars(), array('key1', 'key2'));
  981. $this->assertSame($View->getVar('key1'), 'value1');
  982. $this->assertNull($View->getVar('key3'));
  983. $View->set(array('key3' => 'value3'));
  984. $this->assertSame($View->getVar('key3'), 'value3');
  985. $View->viewVars = array();
  986. $View->set(array(3 => 'three', 4 => 'four'));
  987. $View->set(array(1 => 'one', 2 => 'two'));
  988. $expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two');
  989. $this->assertEquals($expected, $View->viewVars);
  990. }
  991. /**
  992. * testBadExt method
  993. *
  994. * @expectedException MissingViewException
  995. * @return void
  996. */
  997. public function testBadExt() {
  998. $this->PostsController->action = 'something';
  999. $this->PostsController->ext = '.whatever';
  1000. $View = new TestView($this->PostsController);
  1001. $View->render('this_is_missing');
  1002. }
  1003. /**
  1004. * testAltExt method
  1005. *
  1006. * @return void
  1007. */
  1008. public function testAltExt() {
  1009. $this->PostsController->ext = '.alt';
  1010. $View = new TestView($this->PostsController);
  1011. $result = $View->render('alt_ext', false);
  1012. $this->assertEquals('alt ext', $result);
  1013. }
  1014. /**
  1015. * testAltBadExt method
  1016. *
  1017. * @expectedException MissingViewException
  1018. * @return void
  1019. */
  1020. public function testAltBadExt() {
  1021. $View = new TestView($this->PostsController);
  1022. $View->render('alt_ext');
  1023. }
  1024. /**
  1025. * Test creating a block with capturing output.
  1026. *
  1027. * @return void
  1028. */
  1029. public function testBlockCapture() {
  1030. $this->View->start('test');
  1031. echo 'Block content';
  1032. $this->View->end();
  1033. $result = $this->View->fetch('test');
  1034. $this->assertEquals('Block content', $result);
  1035. }
  1036. /**
  1037. * Test appending to a block with capturing output.
  1038. *
  1039. * @return void
  1040. */
  1041. public function testBlockCaptureAppend() {
  1042. $this->View->start('test');
  1043. echo 'Block';
  1044. $this->View->end();
  1045. $this->View->append('test');
  1046. echo ' content';
  1047. $this->View->end();
  1048. $result = $this->View->fetch('test');
  1049. $this->assertEquals('Block content', $result);
  1050. }
  1051. /**
  1052. * Test setting a block's content.
  1053. *
  1054. * @return void
  1055. */
  1056. public function testBlockSet() {
  1057. $this->View->assign('test', 'Block content');
  1058. $result = $this->View->fetch('test');
  1059. $this->assertEquals('Block content', $result);
  1060. }
  1061. /**
  1062. * Test appending to a block with append.
  1063. *
  1064. * @return void
  1065. */
  1066. public function testBlockAppend() {
  1067. $this->View->assign('test', 'Block');
  1068. $this->View->append('test', ' content');
  1069. $result = $this->View->fetch('test');
  1070. $this->assertEquals('Block content', $result);
  1071. }
  1072. /**
  1073. * You should be able to append to undefined blocks.
  1074. *
  1075. * @return void
  1076. */
  1077. public function testBlockAppendUndefined() {
  1078. $this->View->append('test', 'Unknown');
  1079. $result = $this->View->fetch('test');
  1080. $this->assertEquals('Unknown', $result);
  1081. }
  1082. /**
  1083. * setting an array should cause an exception.
  1084. *
  1085. * @expectedException CakeException
  1086. * @return void
  1087. */
  1088. public function testBlockSetArrayException() {
  1089. $this->View->assign('test', array(1, 2, 3));
  1090. }
  1091. /**
  1092. * Appending an array should cause an exception.
  1093. *
  1094. * @expectedException CakeException
  1095. * @return void
  1096. */
  1097. public function testBlockAppendArrayException() {
  1098. $this->View->append('test', array(1, 2, 3));
  1099. }
  1100. /**
  1101. * Test getting block names
  1102. *
  1103. * @return void
  1104. */
  1105. public function testBlocks() {
  1106. $this->View->append('test', 'one');
  1107. $this->View->assign('test1', 'one');
  1108. $this->assertEquals(array('test', 'test1'), $this->View->blocks());
  1109. }
  1110. /**
  1111. * Test that blocks can be nested.
  1112. *
  1113. * @return void
  1114. */
  1115. public function testNestedBlocks() {
  1116. $this->View->start('first');
  1117. echo 'In first ';
  1118. $this->View->start('second');
  1119. echo 'In second';
  1120. $this->View->end();
  1121. echo 'In first';
  1122. $this->View->end();
  1123. $this->assertEquals('In first In first', $this->View->fetch('first'));
  1124. $this->assertEquals('In second', $this->View->fetch('second'));
  1125. }
  1126. /**
  1127. * Test that an exception gets thrown when you leave a block open at the end
  1128. * of a view.
  1129. *
  1130. * @expectedException CakeException
  1131. * @return void
  1132. */
  1133. public function testExceptionOnOpenBlock() {
  1134. $this->View->render('open_block');
  1135. }
  1136. /**
  1137. * Test nested extended views.
  1138. *
  1139. * @return void
  1140. */
  1141. public function testExtendNested() {
  1142. $this->View->layout = false;
  1143. $content = $this->View->render('nested_extends');
  1144. $expected = <<<TEXT
  1145. This is the second parent.
  1146. This is the first parent.
  1147. This is the first template.
  1148. Sidebar Content.
  1149. TEXT;
  1150. $this->assertEquals($expected, $content);
  1151. }
  1152. /**
  1153. * Make sure that extending the current view with itself causes an exception
  1154. *
  1155. * @expectedException LogicException
  1156. * @return void
  1157. */
  1158. public function testExtendSelf() {
  1159. $this->View->layout = false;
  1160. $this->View->render('extend_self');
  1161. }
  1162. /**
  1163. * Make sure that extending in a loop causes an exception
  1164. *
  1165. * @expectedException LogicException
  1166. * @return void
  1167. */
  1168. public function testExtendLoop() {
  1169. $this->View->layout = false;
  1170. $this->View->render('extend_loop');
  1171. }
  1172. /**
  1173. * Test extend() in an element and a view.
  1174. *
  1175. * @return void
  1176. */
  1177. public function testExtendElement() {
  1178. $this->View->layout = false;
  1179. $content = $this->View->render('extend_element');
  1180. $expected = <<<TEXT
  1181. Parent View.
  1182. View content.
  1183. Parent Element.
  1184. Element content.
  1185. TEXT;
  1186. $this->assertEquals($expected, $content);
  1187. }
  1188. /**
  1189. * Extending an element which doesn't exist should throw a missing view exception
  1190. *
  1191. * @expectedException LogicException
  1192. * @return void
  1193. */
  1194. public function testExtendMissingElement() {
  1195. $this->View->layout = false;
  1196. $this->View->render('extend_missing_element');
  1197. }
  1198. /**
  1199. * Test that setting arbitrary properties still works.
  1200. *
  1201. * @return void
  1202. */
  1203. public function testPropertySetting() {
  1204. $this->assertFalse(isset($this->View->pageTitle));
  1205. $this->View->pageTitle = 'test';
  1206. $this->assertTrue(isset($this->View->pageTitle));
  1207. $this->assertTrue(!empty($this->View->pageTitle));
  1208. $this->assertEquals('test', $this->View->pageTitle);
  1209. }
  1210. /**
  1211. * Test that setting arbitrary properties still works.
  1212. *
  1213. * @return void
  1214. */
  1215. public function testPropertySettingMagicGet() {
  1216. $this->assertFalse(isset($this->View->action));
  1217. $this->View->request->params['action'] = 'login';
  1218. $this->assertEquals('login', $this->View->action);
  1219. $this->assertTrue(isset($this->View->action));
  1220. $this->assertTrue(!empty($this->View->action));
  1221. }
  1222. /**
  1223. * test memory leaks that existed in _paths at one point.
  1224. *
  1225. * @return void
  1226. */
  1227. public function testMemoryLeakInPaths() {
  1228. $this->ThemeController->plugin = null;
  1229. $this->ThemeController->name = 'Posts';
  1230. $this->ThemeController->viewPath = 'posts';
  1231. $this->ThemeController->layout = 'whatever';
  1232. $this->ThemeController->theme = 'TestTheme';
  1233. $View = new View($this->ThemeController);
  1234. $View->element('test_element');
  1235. $start = memory_get_usage();
  1236. for ($i = 0; $i < 10; $i++) {
  1237. $View->element('test_element');
  1238. }
  1239. $end = memory_get_usage();
  1240. $this->assertLessThanOrEqual($start + 5000, $end);
  1241. }
  1242. }