ViewTest.php 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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 testGetTemplate() {
  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 . 'Layouts' . DS .'default.ctp';
  282. $result = $View->getLayoutFileName();
  283. $this->assertEquals($expected, $result);
  284. $View->layoutPath = 'rss';
  285. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'rss' . DS . 'default.ctp';
  286. $result = $View->getLayoutFileName();
  287. $this->assertEquals($expected, $result);
  288. $View->layoutPath = 'Emails' . DS . 'html';
  289. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'Emails' . DS . 'html' . DS . 'default.ctp';
  290. $result = $View->getLayoutFileName();
  291. $this->assertEquals($expected, $result);
  292. }
  293. /**
  294. * testMissingView method
  295. *
  296. * @expectedException MissingViewException
  297. * @return void
  298. */
  299. public function testMissingView() {
  300. $this->Controller->plugin = null;
  301. $this->Controller->name = 'Pages';
  302. $this->Controller->viewPath = 'Pages';
  303. $this->Controller->action = 'display';
  304. $this->Controller->params['pass'] = array('home');
  305. $View = new TestView($this->Controller);
  306. ob_start();
  307. $result = $View->getViewFileName('does_not_exist');
  308. }
  309. /**
  310. * testMissingLayout method
  311. *
  312. * @expectedException MissingLayoutException
  313. * @return void
  314. */
  315. public function testMissingLayout() {
  316. $this->Controller->plugin = null;
  317. $this->Controller->name = 'Posts';
  318. $this->Controller->viewPath = 'Posts';
  319. $this->Controller->layout = 'whatever';
  320. $View = new TestView($this->Controller);
  321. ob_start();
  322. $result = $View->getLayoutFileName();
  323. $expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  324. }
  325. /**
  326. * testViewVars method
  327. *
  328. * @return void
  329. */
  330. public function testViewVars() {
  331. $this->assertEquals($this->View->viewVars, array('testData' => 'Some test data', 'test2' => 'more data', 'test3' => 'even more data'));
  332. }
  333. /**
  334. * testUUIDGeneration method
  335. *
  336. * @return void
  337. */
  338. public function testUUIDGeneration() {
  339. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  340. $this->assertEquals($result, 'form5988016017');
  341. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  342. $this->assertEquals($result, 'formc3dc6be854');
  343. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  344. $this->assertEquals($result, 'form28f92cc87f');
  345. }
  346. /**
  347. * testAddInlineScripts method
  348. *
  349. * @return void
  350. */
  351. public function testAddInlineScripts() {
  352. $View = new TestView($this->Controller);
  353. $View->addScript('prototype.js');
  354. $View->addScript('prototype.js');
  355. $this->assertEquals($View->scripts(), array('prototype.js'));
  356. $View->addScript('mainEvent', 'Event.observe(window, "load", function() { doSomething(); }, true);');
  357. $this->assertEquals($View->scripts(), array('prototype.js', 'mainEvent' => 'Event.observe(window, "load", function() { doSomething(); }, true);'));
  358. }
  359. /**
  360. * testElement method
  361. *
  362. * @return void
  363. */
  364. public function testElement() {
  365. $result = $this->View->element('test_element');
  366. $this->assertEquals($result, 'this is the test element');
  367. $result = $this->View->element('plugin_element', array(), array('plugin' => 'TestPlugin'));
  368. $this->assertEquals($result, 'this is the plugin element using params[plugin]');
  369. $result = $this->View->element('plugin_element', array(), array('plugin' => 'test_plugin'));
  370. $this->assertEquals($result, 'this is the plugin element using params[plugin]');
  371. $this->View->plugin = 'TestPlugin';
  372. $result = $this->View->element('test_plugin_element');
  373. $this->assertEquals($result, 'this is the test set using View::$plugin plugin element');
  374. $result = $this->View->element('non_existant_element');
  375. $this->assertRegExp('/Not Found:/', $result);
  376. $this->assertRegExp('/non_existant_element/', $result);
  377. }
  378. /**
  379. * test that elements can have callbacks
  380. *
  381. */
  382. public function testElementCallbacks() {
  383. $this->getMock('HtmlHelper', array(), array($this->View), 'ElementCallbackMockHtmlHelper');
  384. $this->View->helpers = array('ElementCallbackMockHtml');
  385. $this->View->loadHelpers();
  386. $this->View->ElementCallbackMockHtml->expects($this->at(0))->method('beforeRender');
  387. $this->View->ElementCallbackMockHtml->expects($this->at(1))->method('afterRender');
  388. $this->View->element('test_element', array(), array('callbacks' => true));
  389. $this->mockObjects[] = $this->View->ElementCallbackMockHtml;
  390. }
  391. /**
  392. * test that additional element viewVars don't get overwritten with helpers.
  393. *
  394. * @return void
  395. */
  396. public function testElementParamsDontOverwriteHelpers() {
  397. $Controller = new ViewPostsController();
  398. $Controller->helpers = array('Form');
  399. $View = new View($Controller);
  400. $result = $View->element('type_check', array('form' => 'string'), array('callbacks' => true));
  401. $this->assertEquals('string', $result);
  402. $View->set('form', 'string');
  403. $result = $View->element('type_check', array(), array('callbacks' => true));
  404. $this->assertEquals('string', $result);
  405. }
  406. /**
  407. * testElementCacheHelperNoCache method
  408. *
  409. * @return void
  410. */
  411. public function testElementCacheHelperNoCache() {
  412. $Controller = new ViewPostsController();
  413. $View = new TestView($Controller);
  414. $helpers = $View->loadHelpers();
  415. $result = $View->element('test_element', array('ram' => 'val', 'test' => array('foo', 'bar')));
  416. $this->assertEquals($result, 'this is the test element');
  417. }
  418. /**
  419. * testElementCache method
  420. *
  421. * @return void
  422. */
  423. public function testElementCache() {
  424. Cache::drop('test_view');
  425. Cache::config('test_view', array(
  426. 'engine' => 'File',
  427. 'duration' => '+1 day',
  428. 'path' => CACHE . 'views' . DS,
  429. 'prefix' => ''
  430. ));
  431. Cache::clear('test_view');
  432. $View = new TestView($this->PostsController);
  433. $View->elementCache = 'test_view';
  434. $result = $View->element('test_element', array(), array('cache' => true));
  435. $expected = 'this is the test element';
  436. $this->assertEquals($expected, $result);
  437. $result = Cache::read('element__test_element_cache', 'test_view');
  438. $this->assertEquals($expected, $result);
  439. $result = $View->element('test_element', array('param' => 'one', 'foo' => 'two'), array('cache' => true));
  440. $this->assertEquals($expected, $result);
  441. $result = Cache::read('element__test_element_cache_param_foo', 'test_view');
  442. $this->assertEquals($expected, $result);
  443. $result = $View->element('test_element', array(
  444. 'param' => 'one',
  445. 'foo' => 'two'
  446. ), array(
  447. 'cache' => array('key' => 'custom_key')
  448. ));
  449. $result = Cache::read('element_custom_key', 'test_view');
  450. $this->assertEquals($expected, $result);
  451. $View->elementCache = 'default';
  452. $result = $View->element('test_element', array(
  453. 'param' => 'one',
  454. 'foo' => 'two'
  455. ), array(
  456. 'cache' => array('config' => 'test_view'),
  457. ));
  458. $result = Cache::read('element__test_element_cache_param_foo', 'test_view');
  459. $this->assertEquals($expected, $result);
  460. Cache::drop('test_view');
  461. }
  462. /**
  463. * test __get allowing access to helpers.
  464. *
  465. * @return void
  466. */
  467. public function test__get() {
  468. $View = new View($this->PostsController);
  469. $View->loadHelper('Html');
  470. $this->assertInstanceOf('HtmlHelper', $View->Html);
  471. }
  472. /**
  473. * test that ctp is used as a fallback file extension for elements
  474. *
  475. * @return void
  476. */
  477. public function testElementCtpFallback() {
  478. $View = new TestView($this->PostsController);
  479. $View->ext = '.missing';
  480. $element = 'test_element';
  481. $expected = 'this is the test element';
  482. $result = $View->element($element);
  483. $this->assertEquals($expected, $result);
  484. }
  485. /**
  486. * testLoadHelpers method
  487. *
  488. * @return void
  489. */
  490. public function testLoadHelpers() {
  491. $View = new View($this->PostsController);
  492. $View->helpers = array('Html', 'Form');
  493. $View->loadHelpers();
  494. $this->assertInstanceOf('HtmlHelper', $View->Html, 'Object type is wrong.');
  495. $this->assertInstanceOf('FormHelper', $View->Form, 'Object type is wrong.');
  496. }
  497. /**
  498. * test the correct triggering of helper callbacks
  499. *
  500. * @return void
  501. */
  502. public function testHelperCallbackTriggering() {
  503. $View = new View($this->PostsController);
  504. $View->helpers = array();
  505. $View->Helpers = $this->getMock('HelperCollection', array('trigger'), array($View));
  506. $View->Helpers->expects($this->at(0))->method('trigger')
  507. ->with('beforeRender', $this->anything());
  508. $View->Helpers->expects($this->at(1))->method('trigger')
  509. ->with('beforeRenderFile', $this->anything());
  510. $View->Helpers->expects($this->at(2))->method('trigger')
  511. ->with('afterRenderFile', $this->anything())
  512. ->will($this->returnValue(''));
  513. $View->Helpers->expects($this->at(3))->method('trigger')
  514. ->with('afterRender', $this->anything());
  515. $View->Helpers->expects($this->at(4))->method('trigger')
  516. ->with('beforeLayout', $this->anything());
  517. $View->Helpers->expects($this->at(5))->method('trigger')
  518. ->with('beforeRenderFile', $this->anything());
  519. $View->Helpers->expects($this->at(6))->method('trigger')
  520. ->with('afterRenderFile', $this->anything())
  521. ->will($this->returnValue('')) ;
  522. $View->Helpers->expects($this->at(7))->method('trigger')
  523. ->with('afterLayout', $this->anything());
  524. $View->render('index');
  525. }
  526. /**
  527. * testBeforeLayout method
  528. *
  529. * @return void
  530. */
  531. public function testBeforeLayout() {
  532. $this->PostsController->helpers = array('Session', 'TestAfter', 'Html');
  533. $View = new View($this->PostsController);
  534. $View->render('index');
  535. $this->assertEquals($View->Helpers->TestAfter->property, 'Valuation');
  536. }
  537. /**
  538. * testAfterLayout method
  539. *
  540. * @return void
  541. */
  542. public function testAfterLayout() {
  543. $this->PostsController->helpers = array('Session', 'TestAfter', 'Html');
  544. $this->PostsController->set('variable', 'values');
  545. $View = new View($this->PostsController);
  546. ClassRegistry::addObject('afterView', $View);
  547. $content = 'This is my view output';
  548. $result = $View->renderLayout($content, 'default');
  549. $this->assertRegExp('/modified in the afterlife/', $result);
  550. $this->assertRegExp('/This is my view output/', $result);
  551. }
  552. /**
  553. * testRenderLoadHelper method
  554. *
  555. * @return void
  556. */
  557. public function testRenderLoadHelper() {
  558. $this->PostsController->helpers = array('Session', 'Html', 'Form', 'Number');
  559. $View = new TestView($this->PostsController);
  560. $result = $View->render('index', false);
  561. $this->assertEquals($result, 'posts index');
  562. $attached = $View->Helpers->attached();
  563. $this->assertEquals($attached, array('Session', 'Html', 'Form', 'Number'));
  564. $this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
  565. $View = new TestView($this->PostsController);
  566. $result = $View->render('index', false);
  567. $this->assertEquals($result, 'posts index');
  568. $attached = $View->Helpers->attached();
  569. $expected = array('Html', 'Form', 'Number', 'PluggedHelper');
  570. $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
  571. }
  572. /**
  573. * testRender method
  574. *
  575. * @return void
  576. */
  577. public function testRender() {
  578. $View = new TestView($this->PostsController);
  579. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render('index'));
  580. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/><title>/", $result);
  581. $this->assertRegExp("/<div id=\"content\">posts index<\/div>/", $result);
  582. $this->assertRegExp("/<div id=\"content\">posts index<\/div>/", $result);
  583. $this->assertTrue(isset($View->viewVars['content_for_layout']), 'content_for_layout should be a view var');
  584. $this->assertTrue(isset($View->viewVars['scripts_for_layout']), 'scripts_for_layout should be a view var');
  585. $this->PostsController->set('url', 'flash');
  586. $this->PostsController->set('message', 'yo what up');
  587. $this->PostsController->set('pause', 3);
  588. $this->PostsController->set('page_title', 'yo what up');
  589. $View = new TestView($this->PostsController);
  590. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render(false, 'flash'));
  591. $this->assertRegExp("/<title>yo what up<\/title>/", $result);
  592. $this->assertRegExp("/<p><a href=\"flash\">yo what up<\/a><\/p>/", $result);
  593. $this->assertTrue($View->render(false, 'flash'));
  594. $this->PostsController->helpers = array('Session', 'Cache', 'Html');
  595. $this->PostsController->constructClasses();
  596. $this->PostsController->cacheAction = array('index' => 3600);
  597. $this->PostsController->request->params['action'] = 'index';
  598. Configure::write('Cache.check', true);
  599. $View = new TestView($this->PostsController);
  600. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render('index'));
  601. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/><title>/", $result);
  602. $this->assertRegExp("/<div id=\"content\">posts index<\/div>/", $result);
  603. $this->assertRegExp("/<div id=\"content\">posts index<\/div>/", $result);
  604. }
  605. /**
  606. * test that View::$view works
  607. *
  608. * @return void
  609. */
  610. public function testRenderUsingViewProperty() {
  611. $this->PostsController->view = 'cache_form';
  612. $View = new TestView($this->PostsController);
  613. $this->assertEquals('cache_form', $View->view);
  614. $result = $View->render();
  615. $this->assertRegExp('/Add User/', $result);
  616. }
  617. /**
  618. * test that view vars can replace the local helper variables
  619. * and not overwrite the $this->Helper references
  620. *
  621. * @return void
  622. */
  623. public function testViewVarOverwritingLocalHelperVar() {
  624. $Controller = new ViewPostsController();
  625. $Controller->helpers = array('Session', 'Html');
  626. $Controller->set('html', 'I am some test html');
  627. $View = new View($Controller);
  628. $result = $View->render('helper_overwrite', false);
  629. $this->assertRegExp('/I am some test html/', $result);
  630. $this->assertRegExp('/Test link/', $result);
  631. }
  632. /**
  633. * testGetViewFileName method
  634. *
  635. * @return void
  636. */
  637. public function testViewFileName() {
  638. $View = new TestView($this->PostsController);
  639. $result = $View->getViewFileName('index');
  640. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  641. $result = $View->getViewFileName('/Pages/home');
  642. $this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
  643. $result = $View->getViewFileName('../Elements/test_element');
  644. $this->assertRegExp('/Elements(\/|\\\)test_element.ctp/', $result);
  645. $result = $View->getViewFileName('../Themed/TestTheme/Posts/index');
  646. $this->assertRegExp('/Themed(\/|\\\)TestTheme(\/|\\\)Posts(\/|\\\)index.ctp/', $result);
  647. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS .'Posts' . DS .'index.ctp';
  648. $result = $View->getViewFileName('../Posts/index');
  649. $this->assertEquals($expected, $result);
  650. }
  651. /**
  652. * testRenderCache method
  653. *
  654. * @return void
  655. */
  656. public function testRenderCache() {
  657. $this->skipIf(!is_writable(CACHE . 'views' . DS), 'CACHE/views dir is not writable, cannot test renderCache.');
  658. $view = 'test_view';
  659. $View = new View($this->PostsController);
  660. $path = CACHE . 'views' . DS . 'view_cache_' . $view;
  661. $cacheText = '<!--cachetime:' . time() . '-->some cacheText';
  662. $f = fopen($path, 'w+');
  663. fwrite($f, $cacheText);
  664. fclose($f);
  665. $result = $View->renderCache($path, '+1 second');
  666. $this->assertFalse($result);
  667. if (file_exists($path)) {
  668. unlink($path);
  669. }
  670. $cacheText = '<!--cachetime:' . (time() + 10) . '-->some cacheText';
  671. $f = fopen($path, 'w+');
  672. fwrite($f, $cacheText);
  673. fclose($f);
  674. ob_start();
  675. $View->renderCache($path, '+1 second');
  676. $result = ob_get_clean();
  677. $expected = 'some cacheText';
  678. $this->assertRegExp('/^some cacheText/', $result);
  679. @unlink($path);
  680. }
  681. /**
  682. * Test that render() will remove the cake:nocache tags when only the cachehelper is present.
  683. *
  684. * @return void
  685. */
  686. public function testRenderStrippingNoCacheTagsOnlyCacheHelper() {
  687. Configure::write('Cache.check', false);
  688. $View = new View($this->PostsController);
  689. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  690. $View->helpers = array('Html', 'Form', 'Cache');
  691. $View->layout = 'cache_layout';
  692. $result = $View->render('index');
  693. $this->assertNotRegExp('/cake:nocache/', $result);
  694. }
  695. /**
  696. * Test that render() will remove the cake:nocache tags when only the Cache.check is true.
  697. *
  698. * @return void
  699. */
  700. public function testRenderStrippingNoCacheTagsOnlyCacheCheck() {
  701. Configure::write('Cache.check', true);
  702. $View = new View($this->PostsController);
  703. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  704. $View->helpers = array('Html', 'Form');
  705. $View->layout = 'cache_layout';
  706. $result = $View->render('index');
  707. $this->assertNotRegExp('/cake:nocache/', $result);
  708. }
  709. /**
  710. * testRenderNocache method
  711. *
  712. * @return void
  713. */
  714. /* This is a new test case for a pending enhancement
  715. public function testRenderNocache() {
  716. $this->PostsController->helpers = array('Cache', 'Html');
  717. $this->PostsController->constructClasses();
  718. $this->PostsController->cacheAction = 21600;
  719. $this->PostsController->here = '/posts/nocache_multiple_element';
  720. $this->PostsController->action = 'nocache_multiple_element';
  721. $this->PostsController->nocache_multiple_element();
  722. Configure::write('Cache.check', true);
  723. Configure::write('Cache.disable', false);
  724. $filename = CACHE . 'views' . DS . 'posts_nocache_multiple_element.php';
  725. $View = new TestView($this->PostsController);
  726. $View->render();
  727. ob_start();
  728. $View->renderCache($filename, getMicroTime());
  729. $result = ob_get_clean();
  730. @unlink($filename);
  731. $this->assertRegExp('/php echo \$foo;/', $result);
  732. $this->assertRegExp('/php echo \$bar;/', $result);
  733. $this->assertRegExp('/php \$barfoo = \'in sub2\';/', $result);
  734. $this->assertRegExp('/php echo \$barfoo;/', $result);
  735. $this->assertRegExp('/printing: "in sub2"/', $result);
  736. $this->assertRegExp('/php \$foobar = \'in sub1\';/', $result);
  737. $this->assertRegExp('/php echo \$foobar;/', $result);
  738. $this->assertRegExp('/printing: "in sub1"/', $result);
  739. }
  740. */
  741. /**
  742. * testSet method
  743. *
  744. * @return void
  745. */
  746. public function testSet() {
  747. $View = new TestView($this->PostsController);
  748. $View->viewVars = array();
  749. $View->set('somekey', 'someValue');
  750. $this->assertSame($View->viewVars, array('somekey' => 'someValue'));
  751. $this->assertSame($View->getVars(), array('somekey'));
  752. $View->viewVars = array();
  753. $keys = array('key1', 'key2');
  754. $values = array('value1', 'value2');
  755. $View->set($keys, $values);
  756. $this->assertSame($View->viewVars, array('key1' => 'value1', 'key2' => 'value2'));
  757. $this->assertSame($View->getVars(), array('key1', 'key2'));
  758. $this->assertSame($View->getVar('key1'), 'value1');
  759. $this->assertNull($View->getVar('key3'));
  760. $View->set(array('key3' => 'value3'));
  761. $this->assertSame($View->getVar('key3'), 'value3');
  762. $View->viewVars = array();
  763. $View->set(array(3 => 'three', 4 => 'four'));
  764. $View->set(array(1 => 'one', 2 => 'two'));
  765. $expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two');
  766. $this->assertEquals($View->viewVars, $expected);
  767. }
  768. /**
  769. * testBadExt method
  770. *
  771. * @expectedException MissingViewException
  772. * @return void
  773. */
  774. public function testBadExt() {
  775. $this->PostsController->action = 'something';
  776. $this->PostsController->ext = '.whatever';
  777. $View = new TestView($this->PostsController);
  778. $View->render('this_is_missing');
  779. $result = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  780. }
  781. /**
  782. * testAltExt method
  783. *
  784. * @return void
  785. */
  786. public function testAltExt() {
  787. $this->PostsController->ext = '.alt';
  788. $View = new TestView($this->PostsController);
  789. $result = $View->render('alt_ext', false);
  790. $this->assertEquals($result, 'alt ext');
  791. }
  792. /**
  793. * testAltBadExt method
  794. *
  795. * @expectedException MissingViewException
  796. * @return void
  797. */
  798. public function testAltBadExt() {
  799. $View = new TestView($this->PostsController);
  800. $View->render('alt_ext');
  801. $result = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  802. }
  803. /**
  804. * Test creating a block with capturing output.
  805. *
  806. * @return void
  807. */
  808. public function testBlockCapture() {
  809. $this->View->start('test');
  810. echo 'Block content';
  811. $this->View->end();
  812. $result = $this->View->fetch('test');
  813. $this->assertEquals('Block content', $result);
  814. }
  815. /**
  816. * Test appending to a block with capturing output.
  817. *
  818. * @return void
  819. */
  820. public function testBlockCaptureAppend() {
  821. $this->View->start('test');
  822. echo 'Block';
  823. $this->View->end();
  824. $this->View->append('test');
  825. echo ' content';
  826. $this->View->end();
  827. $result = $this->View->fetch('test');
  828. $this->assertEquals('Block content', $result);
  829. }
  830. /**
  831. * Test setting a block's content.
  832. *
  833. * @return void
  834. */
  835. public function testBlockSet() {
  836. $this->View->assign('test', 'Block content');
  837. $result = $this->View->fetch('test');
  838. $this->assertEquals('Block content', $result);
  839. }
  840. /**
  841. * Test appending to a block with append.
  842. *
  843. * @return void
  844. */
  845. public function testBlockAppend() {
  846. $this->View->assign('test', 'Block');
  847. $this->View->append('test', ' content');
  848. $result = $this->View->fetch('test');
  849. $this->assertEquals('Block content', $result);
  850. }
  851. /**
  852. * You should be able to append to undefined blocks.
  853. *
  854. * @return void
  855. */
  856. public function testBlockAppendUndefined() {
  857. $this->View->append('test', 'Unknown');
  858. $result = $this->View->fetch('test');
  859. $this->assertEquals('Unknown', $result);
  860. }
  861. /**
  862. * setting an array should cause an exception.
  863. *
  864. * @expectedException CakeException
  865. * @return void
  866. */
  867. public function testBlockSetArrayException() {
  868. $this->View->assign('test', array(1, 2, 3));
  869. }
  870. /**
  871. * Appending an array should cause an exception.
  872. *
  873. * @expectedException CakeException
  874. * @return void
  875. */
  876. public function testBlockAppendArrayException() {
  877. $this->View->append('test', array(1, 2, 3));
  878. }
  879. /**
  880. * Test getting block names
  881. *
  882. * @return void
  883. */
  884. public function testBlocks() {
  885. $this->View->append('test', 'one');
  886. $this->View->assign('test1', 'one');
  887. $this->assertEquals(array('test', 'test1'), $this->View->blocks());
  888. }
  889. /**
  890. * Test that blocks can be nested.
  891. *
  892. * @return void
  893. */
  894. public function testNestedBlocks() {
  895. $this->View->start('first');
  896. echo 'In first ';
  897. $this->View->start('second');
  898. echo 'In second';
  899. $this->View->end();
  900. echo 'In first';
  901. $this->View->end();
  902. $this->assertEquals('In first In first', $this->View->fetch('first'));
  903. $this->assertEquals('In second', $this->View->fetch('second'));
  904. }
  905. /**
  906. * Test that an exception gets thrown when you leave a block open at the end
  907. * of a view.
  908. *
  909. * @expectedException CakeException
  910. * @return void
  911. */
  912. public function testExceptionOnOpenBlock() {
  913. $this->View->render('open_block');
  914. }
  915. /**
  916. * Test nested extended views.
  917. *
  918. * @return void
  919. */
  920. public function testExtendNested() {
  921. $this->View->layout = false;
  922. $content = $this->View->render('nested_extends');
  923. $expected = <<<TEXT
  924. This is the second parent.
  925. This is the first parent.
  926. This is the first template.
  927. Sidebar Content.
  928. TEXT;
  929. $this->assertEquals($expected, $content);
  930. }
  931. /**
  932. * Test extend() in an element and a view.
  933. *
  934. * @return void
  935. */
  936. public function testExtendElement() {
  937. $this->View->layout = false;
  938. $content = $this->View->render('extend_element');
  939. $expected = <<<TEXT
  940. Parent View.
  941. View content.
  942. Parent Element.
  943. Element content.
  944. TEXT;
  945. $this->assertEquals($expected, $content);
  946. }
  947. }