ViewTest.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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(
  508. $this->logicalAnd(
  509. $this->isInstanceOf('CakeEvent'),
  510. $this->attributeEqualTo('_name', 'View.beforeRender'),
  511. $this->attributeEqualTo('_subject', $View)
  512. )
  513. );
  514. $View->Helpers->expects($this->at(1))->method('trigger')
  515. ->with(
  516. $this->logicalAnd(
  517. $this->isInstanceOf('CakeEvent'),
  518. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  519. $this->attributeEqualTo('_subject', $View)
  520. )
  521. );
  522. $View->Helpers->expects($this->at(2))->method('trigger')
  523. ->with(
  524. $this->logicalAnd(
  525. $this->isInstanceOf('CakeEvent'),
  526. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  527. $this->attributeEqualTo('_subject', $View)
  528. )
  529. );
  530. $View->Helpers->expects($this->at(3))->method('trigger')
  531. ->with(
  532. $this->logicalAnd(
  533. $this->isInstanceOf('CakeEvent'),
  534. $this->attributeEqualTo('_name', 'View.afterRender'),
  535. $this->attributeEqualTo('_subject', $View)
  536. )
  537. );
  538. $View->Helpers->expects($this->at(4))->method('trigger')
  539. ->with(
  540. $this->logicalAnd(
  541. $this->isInstanceOf('CakeEvent'),
  542. $this->attributeEqualTo('_name', 'View.beforeLayout'),
  543. $this->attributeEqualTo('_subject', $View)
  544. )
  545. );
  546. $View->Helpers->expects($this->at(5))->method('trigger')
  547. ->with(
  548. $this->logicalAnd(
  549. $this->isInstanceOf('CakeEvent'),
  550. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  551. $this->attributeEqualTo('_subject', $View)
  552. )
  553. );
  554. $View->Helpers->expects($this->at(6))->method('trigger')
  555. ->with(
  556. $this->logicalAnd(
  557. $this->isInstanceOf('CakeEvent'),
  558. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  559. $this->attributeEqualTo('_subject', $View)
  560. )
  561. );
  562. $View->Helpers->expects($this->at(7))->method('trigger')
  563. ->with(
  564. $this->logicalAnd(
  565. $this->isInstanceOf('CakeEvent'),
  566. $this->attributeEqualTo('_name', 'View.afterLayout'),
  567. $this->attributeEqualTo('_subject', $View)
  568. )
  569. );
  570. $View->render('index');
  571. }
  572. /**
  573. * testBeforeLayout method
  574. *
  575. * @return void
  576. */
  577. public function testBeforeLayout() {
  578. $this->PostsController->helpers = array('Session', 'TestAfter', 'Html');
  579. $View = new View($this->PostsController);
  580. $View->render('index');
  581. $this->assertEquals($View->Helpers->TestAfter->property, 'Valuation');
  582. }
  583. /**
  584. * testAfterLayout method
  585. *
  586. * @return void
  587. */
  588. public function testAfterLayout() {
  589. $this->PostsController->helpers = array('Session', 'TestAfter', 'Html');
  590. $this->PostsController->set('variable', 'values');
  591. $View = new View($this->PostsController);
  592. ClassRegistry::addObject('afterView', $View);
  593. $content = 'This is my view output';
  594. $result = $View->renderLayout($content, 'default');
  595. $this->assertRegExp('/modified in the afterlife/', $result);
  596. $this->assertRegExp('/This is my view output/', $result);
  597. }
  598. /**
  599. * testRenderLoadHelper method
  600. *
  601. * @return void
  602. */
  603. public function testRenderLoadHelper() {
  604. $this->PostsController->helpers = array('Session', 'Html', 'Form', 'Number');
  605. $View = new TestView($this->PostsController);
  606. $result = $View->render('index', false);
  607. $this->assertEquals($result, 'posts index');
  608. $attached = $View->Helpers->attached();
  609. $this->assertEquals($attached, array('Session', 'Html', 'Form', 'Number'));
  610. $this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
  611. $View = new TestView($this->PostsController);
  612. $result = $View->render('index', false);
  613. $this->assertEquals($result, 'posts index');
  614. $attached = $View->Helpers->attached();
  615. $expected = array('Html', 'Form', 'Number', 'PluggedHelper');
  616. $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
  617. }
  618. /**
  619. * testRender method
  620. *
  621. * @return void
  622. */
  623. public function testRender() {
  624. $View = new TestView($this->PostsController);
  625. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render('index'));
  626. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/><title>/", $result);
  627. $this->assertRegExp("/<div id=\"content\">posts index<\/div>/", $result);
  628. $this->assertRegExp("/<div id=\"content\">posts index<\/div>/", $result);
  629. $this->assertTrue(isset($View->viewVars['content_for_layout']), 'content_for_layout should be a view var');
  630. $this->assertTrue(isset($View->viewVars['scripts_for_layout']), 'scripts_for_layout should be a view var');
  631. $this->PostsController->set('url', 'flash');
  632. $this->PostsController->set('message', 'yo what up');
  633. $this->PostsController->set('pause', 3);
  634. $this->PostsController->set('page_title', 'yo what up');
  635. $View = new TestView($this->PostsController);
  636. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render(false, 'flash'));
  637. $this->assertRegExp("/<title>yo what up<\/title>/", $result);
  638. $this->assertRegExp("/<p><a href=\"flash\">yo what up<\/a><\/p>/", $result);
  639. $this->assertTrue($View->render(false, 'flash'));
  640. $this->PostsController->helpers = array('Session', 'Cache', 'Html');
  641. $this->PostsController->constructClasses();
  642. $this->PostsController->cacheAction = array('index' => 3600);
  643. $this->PostsController->request->params['action'] = 'index';
  644. Configure::write('Cache.check', true);
  645. $View = new TestView($this->PostsController);
  646. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render('index'));
  647. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/><title>/", $result);
  648. $this->assertRegExp("/<div id=\"content\">posts index<\/div>/", $result);
  649. $this->assertRegExp("/<div id=\"content\">posts index<\/div>/", $result);
  650. }
  651. /**
  652. * test that View::$view works
  653. *
  654. * @return void
  655. */
  656. public function testRenderUsingViewProperty() {
  657. $this->PostsController->view = 'cache_form';
  658. $View = new TestView($this->PostsController);
  659. $this->assertEquals('cache_form', $View->view);
  660. $result = $View->render();
  661. $this->assertRegExp('/Add User/', $result);
  662. }
  663. /**
  664. * test that view vars can replace the local helper variables
  665. * and not overwrite the $this->Helper references
  666. *
  667. * @return void
  668. */
  669. public function testViewVarOverwritingLocalHelperVar() {
  670. $Controller = new ViewPostsController();
  671. $Controller->helpers = array('Session', 'Html');
  672. $Controller->set('html', 'I am some test html');
  673. $View = new View($Controller);
  674. $result = $View->render('helper_overwrite', false);
  675. $this->assertRegExp('/I am some test html/', $result);
  676. $this->assertRegExp('/Test link/', $result);
  677. }
  678. /**
  679. * testGetViewFileName method
  680. *
  681. * @return void
  682. */
  683. public function testViewFileName() {
  684. $View = new TestView($this->PostsController);
  685. $result = $View->getViewFileName('index');
  686. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  687. $result = $View->getViewFileName('/Pages/home');
  688. $this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
  689. $result = $View->getViewFileName('../Elements/test_element');
  690. $this->assertRegExp('/Elements(\/|\\\)test_element.ctp/', $result);
  691. $result = $View->getViewFileName('../Themed/TestTheme/Posts/index');
  692. $this->assertRegExp('/Themed(\/|\\\)TestTheme(\/|\\\)Posts(\/|\\\)index.ctp/', $result);
  693. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS .'Posts' . DS .'index.ctp';
  694. $result = $View->getViewFileName('../Posts/index');
  695. $this->assertEquals($expected, $result);
  696. }
  697. /**
  698. * testRenderCache method
  699. *
  700. * @return void
  701. */
  702. public function testRenderCache() {
  703. $this->skipIf(!is_writable(CACHE . 'views' . DS), 'CACHE/views dir is not writable, cannot test renderCache.');
  704. $view = 'test_view';
  705. $View = new View($this->PostsController);
  706. $path = CACHE . 'views' . DS . 'view_cache_' . $view;
  707. $cacheText = '<!--cachetime:' . time() . '-->some cacheText';
  708. $f = fopen($path, 'w+');
  709. fwrite($f, $cacheText);
  710. fclose($f);
  711. $result = $View->renderCache($path, '+1 second');
  712. $this->assertFalse($result);
  713. if (file_exists($path)) {
  714. unlink($path);
  715. }
  716. $cacheText = '<!--cachetime:' . (time() + 10) . '-->some cacheText';
  717. $f = fopen($path, 'w+');
  718. fwrite($f, $cacheText);
  719. fclose($f);
  720. ob_start();
  721. $View->renderCache($path, '+1 second');
  722. $result = ob_get_clean();
  723. $expected = 'some cacheText';
  724. $this->assertRegExp('/^some cacheText/', $result);
  725. @unlink($path);
  726. }
  727. /**
  728. * Test that render() will remove the cake:nocache tags when only the cachehelper is present.
  729. *
  730. * @return void
  731. */
  732. public function testRenderStrippingNoCacheTagsOnlyCacheHelper() {
  733. Configure::write('Cache.check', false);
  734. $View = new View($this->PostsController);
  735. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  736. $View->helpers = array('Html', 'Form', 'Cache');
  737. $View->layout = 'cache_layout';
  738. $result = $View->render('index');
  739. $this->assertNotRegExp('/cake:nocache/', $result);
  740. }
  741. /**
  742. * Test that render() will remove the cake:nocache tags when only the Cache.check is true.
  743. *
  744. * @return void
  745. */
  746. public function testRenderStrippingNoCacheTagsOnlyCacheCheck() {
  747. Configure::write('Cache.check', true);
  748. $View = new View($this->PostsController);
  749. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  750. $View->helpers = array('Html', 'Form');
  751. $View->layout = 'cache_layout';
  752. $result = $View->render('index');
  753. $this->assertNotRegExp('/cake:nocache/', $result);
  754. }
  755. /**
  756. * testRenderNocache method
  757. *
  758. * @return void
  759. */
  760. /* This is a new test case for a pending enhancement
  761. public function testRenderNocache() {
  762. $this->PostsController->helpers = array('Cache', 'Html');
  763. $this->PostsController->constructClasses();
  764. $this->PostsController->cacheAction = 21600;
  765. $this->PostsController->here = '/posts/nocache_multiple_element';
  766. $this->PostsController->action = 'nocache_multiple_element';
  767. $this->PostsController->nocache_multiple_element();
  768. Configure::write('Cache.check', true);
  769. Configure::write('Cache.disable', false);
  770. $filename = CACHE . 'views' . DS . 'posts_nocache_multiple_element.php';
  771. $View = new TestView($this->PostsController);
  772. $View->render();
  773. ob_start();
  774. $View->renderCache($filename, getMicroTime());
  775. $result = ob_get_clean();
  776. @unlink($filename);
  777. $this->assertRegExp('/php echo \$foo;/', $result);
  778. $this->assertRegExp('/php echo \$bar;/', $result);
  779. $this->assertRegExp('/php \$barfoo = \'in sub2\';/', $result);
  780. $this->assertRegExp('/php echo \$barfoo;/', $result);
  781. $this->assertRegExp('/printing: "in sub2"/', $result);
  782. $this->assertRegExp('/php \$foobar = \'in sub1\';/', $result);
  783. $this->assertRegExp('/php echo \$foobar;/', $result);
  784. $this->assertRegExp('/printing: "in sub1"/', $result);
  785. }
  786. */
  787. /**
  788. * testSet method
  789. *
  790. * @return void
  791. */
  792. public function testSet() {
  793. $View = new TestView($this->PostsController);
  794. $View->viewVars = array();
  795. $View->set('somekey', 'someValue');
  796. $this->assertSame($View->viewVars, array('somekey' => 'someValue'));
  797. $this->assertSame($View->getVars(), array('somekey'));
  798. $View->viewVars = array();
  799. $keys = array('key1', 'key2');
  800. $values = array('value1', 'value2');
  801. $View->set($keys, $values);
  802. $this->assertSame($View->viewVars, array('key1' => 'value1', 'key2' => 'value2'));
  803. $this->assertSame($View->getVars(), array('key1', 'key2'));
  804. $this->assertSame($View->getVar('key1'), 'value1');
  805. $this->assertNull($View->getVar('key3'));
  806. $View->set(array('key3' => 'value3'));
  807. $this->assertSame($View->getVar('key3'), 'value3');
  808. $View->viewVars = array();
  809. $View->set(array(3 => 'three', 4 => 'four'));
  810. $View->set(array(1 => 'one', 2 => 'two'));
  811. $expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two');
  812. $this->assertEquals($View->viewVars, $expected);
  813. }
  814. /**
  815. * testBadExt method
  816. *
  817. * @expectedException MissingViewException
  818. * @return void
  819. */
  820. public function testBadExt() {
  821. $this->PostsController->action = 'something';
  822. $this->PostsController->ext = '.whatever';
  823. $View = new TestView($this->PostsController);
  824. $View->render('this_is_missing');
  825. $result = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  826. }
  827. /**
  828. * testAltExt method
  829. *
  830. * @return void
  831. */
  832. public function testAltExt() {
  833. $this->PostsController->ext = '.alt';
  834. $View = new TestView($this->PostsController);
  835. $result = $View->render('alt_ext', false);
  836. $this->assertEquals($result, 'alt ext');
  837. }
  838. /**
  839. * testAltBadExt method
  840. *
  841. * @expectedException MissingViewException
  842. * @return void
  843. */
  844. public function testAltBadExt() {
  845. $View = new TestView($this->PostsController);
  846. $View->render('alt_ext');
  847. $result = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  848. }
  849. /**
  850. * Test creating a block with capturing output.
  851. *
  852. * @return void
  853. */
  854. public function testBlockCapture() {
  855. $this->View->start('test');
  856. echo 'Block content';
  857. $this->View->end();
  858. $result = $this->View->fetch('test');
  859. $this->assertEquals('Block content', $result);
  860. }
  861. /**
  862. * Test appending to a block with capturing output.
  863. *
  864. * @return void
  865. */
  866. public function testBlockCaptureAppend() {
  867. $this->View->start('test');
  868. echo 'Block';
  869. $this->View->end();
  870. $this->View->append('test');
  871. echo ' content';
  872. $this->View->end();
  873. $result = $this->View->fetch('test');
  874. $this->assertEquals('Block content', $result);
  875. }
  876. /**
  877. * Test setting a block's content.
  878. *
  879. * @return void
  880. */
  881. public function testBlockSet() {
  882. $this->View->assign('test', 'Block content');
  883. $result = $this->View->fetch('test');
  884. $this->assertEquals('Block content', $result);
  885. }
  886. /**
  887. * Test appending to a block with append.
  888. *
  889. * @return void
  890. */
  891. public function testBlockAppend() {
  892. $this->View->assign('test', 'Block');
  893. $this->View->append('test', ' content');
  894. $result = $this->View->fetch('test');
  895. $this->assertEquals('Block content', $result);
  896. }
  897. /**
  898. * You should be able to append to undefined blocks.
  899. *
  900. * @return void
  901. */
  902. public function testBlockAppendUndefined() {
  903. $this->View->append('test', 'Unknown');
  904. $result = $this->View->fetch('test');
  905. $this->assertEquals('Unknown', $result);
  906. }
  907. /**
  908. * setting an array should cause an exception.
  909. *
  910. * @expectedException CakeException
  911. * @return void
  912. */
  913. public function testBlockSetArrayException() {
  914. $this->View->assign('test', array(1, 2, 3));
  915. }
  916. /**
  917. * Appending an array should cause an exception.
  918. *
  919. * @expectedException CakeException
  920. * @return void
  921. */
  922. public function testBlockAppendArrayException() {
  923. $this->View->append('test', array(1, 2, 3));
  924. }
  925. /**
  926. * Test getting block names
  927. *
  928. * @return void
  929. */
  930. public function testBlocks() {
  931. $this->View->append('test', 'one');
  932. $this->View->assign('test1', 'one');
  933. $this->assertEquals(array('test', 'test1'), $this->View->blocks());
  934. }
  935. /**
  936. * Test that blocks can be nested.
  937. *
  938. * @return void
  939. */
  940. public function testNestedBlocks() {
  941. $this->View->start('first');
  942. echo 'In first ';
  943. $this->View->start('second');
  944. echo 'In second';
  945. $this->View->end();
  946. echo 'In first';
  947. $this->View->end();
  948. $this->assertEquals('In first In first', $this->View->fetch('first'));
  949. $this->assertEquals('In second', $this->View->fetch('second'));
  950. }
  951. /**
  952. * Test that an exception gets thrown when you leave a block open at the end
  953. * of a view.
  954. *
  955. * @expectedException CakeException
  956. * @return void
  957. */
  958. public function testExceptionOnOpenBlock() {
  959. $this->View->render('open_block');
  960. }
  961. /**
  962. * Test nested extended views.
  963. *
  964. * @return void
  965. */
  966. public function testExtendNested() {
  967. $this->View->layout = false;
  968. $content = $this->View->render('nested_extends');
  969. $expected = <<<TEXT
  970. This is the second parent.
  971. This is the first parent.
  972. This is the first template.
  973. Sidebar Content.
  974. TEXT;
  975. $this->assertEquals($expected, $content);
  976. }
  977. /**
  978. * Test extend() in an element and a view.
  979. *
  980. * @return void
  981. */
  982. public function testExtendElement() {
  983. $this->View->layout = false;
  984. $content = $this->View->render('extend_element');
  985. $expected = <<<TEXT
  986. Parent View.
  987. View content.
  988. Parent Element.
  989. Element content.
  990. TEXT;
  991. $this->assertEquals($expected, $content);
  992. }
  993. }