ViewTest.php 34 KB

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