ViewTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake.tests.cases.libs
  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.tests.cases.libs.view
  28. */
  29. class ViewPostsController extends Controller {
  30. /**
  31. * name property
  32. *
  33. * @var string 'Posts'
  34. * @access public
  35. */
  36. public $name = 'Posts';
  37. /**
  38. * uses property
  39. *
  40. * @var mixed null
  41. * @access public
  42. */
  43. public $uses = null;
  44. /**
  45. * index method
  46. *
  47. * @access public
  48. * @return void
  49. */
  50. function index() {
  51. $this->set('testData', 'Some test data');
  52. $test2 = 'more data';
  53. $test3 = 'even more data';
  54. $this->set(compact('test2', 'test3'));
  55. }
  56. /**
  57. * nocache_tags_with_element method
  58. *
  59. * @access public
  60. * @return void
  61. */
  62. function nocache_multiple_element() {
  63. $this->set('foo', 'this is foo var');
  64. $this->set('bar', 'this is bar var');
  65. }
  66. }
  67. /**
  68. * TestView class
  69. *
  70. * @package cake.tests.cases.libs.view
  71. */
  72. class TestView extends View {
  73. /**
  74. * getViewFileName method
  75. *
  76. * @param mixed $name
  77. * @access public
  78. * @return void
  79. */
  80. function getViewFileName($name = null) {
  81. return $this->_getViewFileName($name);
  82. }
  83. /**
  84. * getLayoutFileName method
  85. *
  86. * @param mixed $name
  87. * @access public
  88. * @return void
  89. */
  90. function getLayoutFileName($name = null) {
  91. return $this->_getLayoutFileName($name);
  92. }
  93. /**
  94. * paths method
  95. *
  96. * @param string $plugin
  97. * @param boolean $cached
  98. * @access public
  99. * @return void
  100. */
  101. function paths($plugin = null, $cached = true) {
  102. return $this->_paths($plugin, $cached);
  103. }
  104. /**
  105. * _render wrapper for testing (temporary).
  106. *
  107. * @param string $___viewFn
  108. * @param string $___dataForView
  109. * @param string $loadHelpers
  110. * @param string $cached
  111. * @return void
  112. */
  113. function render_($___viewFn, $___dataForView, $loadHelpers = true, $cached = false) {
  114. return $this->_render($___viewFn, $___dataForView, $loadHelpers, $cached);
  115. }
  116. /**
  117. * Test only function to return instance scripts.
  118. *
  119. * @return array Scripts
  120. */
  121. function scripts() {
  122. return $this->_scripts;
  123. }
  124. }
  125. /**
  126. * TestAfterHelper class
  127. *
  128. * @package cake.tests.cases.libs.view
  129. */
  130. class TestAfterHelper extends Helper {
  131. /**
  132. * property property
  133. *
  134. * @var string ''
  135. */
  136. public $property = '';
  137. /**
  138. * beforeLayout method
  139. *
  140. * @access public
  141. * @return void
  142. */
  143. function beforeLayout($viewFile) {
  144. $this->property = 'Valuation';
  145. }
  146. /**
  147. * afterLayout method
  148. *
  149. * @access public
  150. * @return void
  151. */
  152. function afterLayout($layoutFile) {
  153. $this->_View->output .= 'modified in the afterlife';
  154. }
  155. }
  156. /**
  157. * ViewTest class
  158. *
  159. * @package cake.tests.cases.libs
  160. */
  161. class ViewTest extends CakeTestCase {
  162. /**
  163. * setUp method
  164. *
  165. * @access public
  166. * @return void
  167. */
  168. function setUp() {
  169. parent::setUp();
  170. $request = $this->getMock('CakeRequest');
  171. $this->Controller = new Controller($request);
  172. $this->PostsController = new ViewPostsController($request);
  173. $this->PostsController->viewPath = 'posts';
  174. $this->PostsController->index();
  175. $this->View = new View($this->PostsController);
  176. App::build(array(
  177. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  178. 'View' => array(
  179. CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS
  180. )
  181. ), true);
  182. CakePlugin::loadAll();
  183. Configure::write('debug', 2);
  184. }
  185. /**
  186. * tearDown method
  187. *
  188. * @access public
  189. * @return void
  190. */
  191. function tearDown() {
  192. parent::tearDown();
  193. CakePlugin::unload();
  194. unset($this->View);
  195. unset($this->PostsController);
  196. unset($this->Controller);
  197. }
  198. /**
  199. * testPluginGetTemplate method
  200. *
  201. * @access public
  202. * @return void
  203. */
  204. function testPluginGetTemplate() {
  205. $this->Controller->plugin = 'TestPlugin';
  206. $this->Controller->name = 'TestPlugin';
  207. $this->Controller->viewPath = 'Tests';
  208. $this->Controller->action = 'index';
  209. $View = new TestView($this->Controller);
  210. $expected = CakePlugin::path('TestPlugin') . 'View' . DS .'Tests' . DS .'index.ctp';
  211. $result = $View->getViewFileName('index');
  212. $this->assertEqual($result, $expected);
  213. $expected = CakePlugin::path('TestPlugin') . 'View' . DS . 'Layouts' . DS .'default.ctp';
  214. $result = $View->getLayoutFileName();
  215. $this->assertEqual($result, $expected);
  216. }
  217. /**
  218. * test that plugin/$plugin_name is only appended to the paths it should be.
  219. *
  220. * @return void
  221. */
  222. function testPluginPathGeneration() {
  223. $this->Controller->plugin = 'TestPlugin';
  224. $this->Controller->name = 'TestPlugin';
  225. $this->Controller->viewPath = 'Tests';
  226. $this->Controller->action = 'index';
  227. $View = new TestView($this->Controller);
  228. $paths = $View->paths();
  229. $expected = array_merge(App::path('View'), App::core('View'));
  230. $this->assertEqual($paths, $expected);
  231. $paths = $View->paths('TestPlugin');
  232. $pluginPath = CakePlugin::path('TestPlugin');
  233. $expected = array(
  234. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Plugins' . DS . 'TestPlugin' . DS,
  235. $pluginPath . 'View' . DS,
  236. $pluginPath . 'views' . DS,
  237. $pluginPath . 'Lib' . DS . 'View' . DS,
  238. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS,
  239. CAKE . 'View' . DS
  240. );
  241. $this->assertEqual($paths, $expected);
  242. }
  243. /**
  244. * test that CamelCase plugins still find their view files.
  245. *
  246. * @return void
  247. */
  248. function testCamelCasePluginGetTemplate() {
  249. $this->Controller->plugin = 'TestPlugin';
  250. $this->Controller->name = 'TestPlugin';
  251. $this->Controller->viewPath = 'Tests';
  252. $this->Controller->action = 'index';
  253. $View = new TestView($this->Controller);
  254. App::build(array(
  255. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  256. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  257. ));
  258. $pluginPath = CakePlugin::path('TestPlugin');
  259. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS .'TestPlugin' . DS . 'View' . DS .'Tests' . DS .'index.ctp';
  260. $result = $View->getViewFileName('index');
  261. $this->assertEqual($result, $expected);
  262. $expected = $pluginPath. 'View' . DS . 'Layouts' . DS .'default.ctp';
  263. $result = $View->getLayoutFileName();
  264. $this->assertEqual($result, $expected);
  265. }
  266. /**
  267. * testGetTemplate method
  268. *
  269. * @access public
  270. * @return void
  271. */
  272. function testGetTemplate() {
  273. $this->Controller->plugin = null;
  274. $this->Controller->name = 'Pages';
  275. $this->Controller->viewPath = 'Pages';
  276. $this->Controller->action = 'display';
  277. $this->Controller->params['pass'] = array('home');
  278. $View = new TestView($this->Controller);
  279. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS .'Pages' . DS .'home.ctp';
  280. $result = $View->getViewFileName('home');
  281. $this->assertEqual($result, $expected);
  282. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS .'Posts' . DS .'index.ctp';
  283. $result = $View->getViewFileName('/Posts/index');
  284. $this->assertEqual($result, $expected);
  285. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS .'Posts' . DS .'index.ctp';
  286. $result = $View->getViewFileName('../Posts/index');
  287. $this->assertEqual($result, $expected);
  288. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS .'default.ctp';
  289. $result = $View->getLayoutFileName();
  290. $this->assertEqual($result, $expected);
  291. $View->layoutPath = 'rss';
  292. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'rss' . DS . 'default.ctp';
  293. $result = $View->getLayoutFileName();
  294. $this->assertEqual($result, $expected);
  295. $View->layoutPath = 'emails' . DS . 'html';
  296. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'emails' . DS . 'html' . DS . 'default.ctp';
  297. $result = $View->getLayoutFileName();
  298. $this->assertEqual($result, $expected);
  299. }
  300. /**
  301. * testMissingView method
  302. *
  303. * @expectedException MissingViewException
  304. * @access public
  305. * @return void
  306. */
  307. function testMissingView() {
  308. $this->Controller->plugin = null;
  309. $this->Controller->name = 'Pages';
  310. $this->Controller->viewPath = 'pages';
  311. $this->Controller->action = 'display';
  312. $this->Controller->params['pass'] = array('home');
  313. $View = new TestView($this->Controller);
  314. ob_start();
  315. $result = $View->getViewFileName('does_not_exist');
  316. }
  317. /**
  318. * testMissingLayout method
  319. *
  320. * @expectedException MissingLayoutException
  321. * @access public
  322. * @return void
  323. */
  324. function testMissingLayout() {
  325. $this->Controller->plugin = null;
  326. $this->Controller->name = 'Posts';
  327. $this->Controller->viewPath = 'posts';
  328. $this->Controller->layout = 'whatever';
  329. $View = new TestView($this->Controller);
  330. ob_start();
  331. $result = $View->getLayoutFileName();
  332. $expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  333. }
  334. /**
  335. * testViewVars method
  336. *
  337. * @access public
  338. * @return void
  339. */
  340. function testViewVars() {
  341. $this->assertEqual($this->View->viewVars, array('testData' => 'Some test data', 'test2' => 'more data', 'test3' => 'even more data'));
  342. }
  343. /**
  344. * testUUIDGeneration method
  345. *
  346. * @access public
  347. * @return void
  348. */
  349. function testUUIDGeneration() {
  350. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  351. $this->assertEqual($result, 'form5988016017');
  352. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  353. $this->assertEqual($result, 'formc3dc6be854');
  354. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  355. $this->assertEqual($result, 'form28f92cc87f');
  356. }
  357. /**
  358. * testAddInlineScripts method
  359. *
  360. * @access public
  361. * @return void
  362. */
  363. function testAddInlineScripts() {
  364. $View = new TestView($this->Controller);
  365. $View->addScript('prototype.js');
  366. $View->addScript('prototype.js');
  367. $this->assertEqual($View->scripts(), array('prototype.js'));
  368. $View->addScript('mainEvent', 'Event.observe(window, "load", function() { doSomething(); }, true);');
  369. $this->assertEqual($View->scripts(), array('prototype.js', 'mainEvent' => 'Event.observe(window, "load", function() { doSomething(); }, true);'));
  370. }
  371. /**
  372. * testElement method
  373. *
  374. * @access public
  375. * @return void
  376. */
  377. function testElement() {
  378. $result = $this->View->element('test_element');
  379. $this->assertEqual($result, 'this is the test element');
  380. $result = $this->View->element('plugin_element', array(), array('plugin' => 'TestPlugin'));
  381. $this->assertEqual($result, 'this is the plugin element using params[plugin]');
  382. $this->View->plugin = 'TestPlugin';
  383. $result = $this->View->element('test_plugin_element');
  384. $this->assertEqual($result, 'this is the test set using View::$plugin plugin element');
  385. $result = $this->View->element('non_existant_element');
  386. $this->assertPattern('/Not Found:/', $result);
  387. $this->assertPattern('/non_existant_element/', $result);
  388. }
  389. /**
  390. * test that elements can have callbacks
  391. *
  392. */
  393. function testElementCallbacks() {
  394. $this->getMock('HtmlHelper', array(), array($this->View), 'ElementCallbackMockHtmlHelper');
  395. $this->View->helpers = array('ElementCallbackMockHtml');
  396. $this->View->loadHelpers();
  397. $this->View->ElementCallbackMockHtml->expects($this->at(0))->method('beforeRender');
  398. $this->View->ElementCallbackMockHtml->expects($this->at(1))->method('afterRender');
  399. $this->View->element('test_element', array(), array('callbacks' => true));
  400. $this->mockObjects[] = $this->View->ElementCallbackMockHtml;
  401. }
  402. /**
  403. * test that additional element viewVars don't get overwritten with helpers.
  404. *
  405. * @return void
  406. */
  407. function testElementParamsDontOverwriteHelpers() {
  408. $Controller = new ViewPostsController();
  409. $Controller->helpers = array('Form');
  410. $View = new View($Controller);
  411. $result = $View->element('type_check', array('form' => 'string'), array('callbacks' => true));
  412. $this->assertEqual('string', $result);
  413. $View->set('form', 'string');
  414. $result = $View->element('type_check', array(), array('callbacks' => true));
  415. $this->assertEqual('string', $result);
  416. }
  417. /**
  418. * testElementCacheHelperNoCache method
  419. *
  420. * @access public
  421. * @return void
  422. */
  423. function testElementCacheHelperNoCache() {
  424. $Controller = new ViewPostsController();
  425. $View = new TestView($Controller);
  426. $helpers = $View->loadHelpers();
  427. $result = $View->element('test_element', array('ram' => 'val', 'test' => array('foo', 'bar')));
  428. $this->assertEqual($result, 'this is the test element');
  429. }
  430. /**
  431. * testElementCache method
  432. *
  433. * @access public
  434. * @return void
  435. */
  436. function testElementCache() {
  437. Cache::drop('test_view');
  438. Cache::config('test_view', array(
  439. 'engine' => 'File',
  440. 'duration' => '+1 day',
  441. 'path' => CACHE . 'views' . DS,
  442. 'prefix' => ''
  443. ));
  444. Cache::clear('test_view');
  445. $View = new TestView($this->PostsController);
  446. $View->elementCache = 'test_view';
  447. $result = $View->element('test_element', array(), array('cache' => true));
  448. $expected = 'this is the test element';
  449. $this->assertEquals($expected, $result);
  450. $result = Cache::read('element__test_element_cache', 'test_view');
  451. $this->assertEquals($expected, $result);
  452. $result = $View->element('test_element', array('param' => 'one', 'foo' => 'two'), array('cache' => true));
  453. $this->assertEquals($expected, $result);
  454. $result = Cache::read('element__test_element_cache_param_foo', 'test_view');
  455. $this->assertEquals($expected, $result);
  456. $result = $View->element('test_element', array(
  457. 'param' => 'one',
  458. 'foo' => 'two'
  459. ), array(
  460. 'cache' => array('key' => 'custom_key')
  461. ));
  462. $result = Cache::read('element_custom_key', 'test_view');
  463. $this->assertEquals($expected, $result);
  464. $View->elementCache = 'default';
  465. $result = $View->element('test_element', array(
  466. 'param' => 'one',
  467. 'foo' => 'two'
  468. ), array(
  469. 'cache' => array('config' => 'test_view'),
  470. ));
  471. $result = Cache::read('element__test_element_cache_param_foo', 'test_view');
  472. $this->assertEquals($expected, $result);
  473. Cache::drop('test_view');
  474. }
  475. /**
  476. * test __get allowing access to helpers.
  477. *
  478. * @return void
  479. */
  480. function test__get() {
  481. $View = new View($this->PostsController);
  482. $View->loadHelper('Html');
  483. $this->assertInstanceOf('HtmlHelper', $View->Html);
  484. }
  485. /**
  486. * test that ctp is used as a fallback file extension for elements
  487. *
  488. * @return void
  489. */
  490. function testElementCtpFallback() {
  491. $View = new TestView($this->PostsController);
  492. $View->ext = '.missing';
  493. $element = 'test_element';
  494. $expected = 'this is the test element';
  495. $result = $View->element($element);
  496. $this->assertEqual($expected, $result);
  497. }
  498. /**
  499. * testLoadHelpers method
  500. *
  501. * @access public
  502. * @return void
  503. */
  504. function testLoadHelpers() {
  505. $View = new View($this->PostsController);
  506. $View->helpers = array('Html', 'Form');
  507. $View->loadHelpers();
  508. $this->assertInstanceOf('HtmlHelper', $View->Html, 'Object type is wrong.');
  509. $this->assertInstanceOf('FormHelper', $View->Form, 'Object type is wrong.');
  510. }
  511. /**
  512. * test the correct triggering of helper callbacks
  513. *
  514. * @return void
  515. */
  516. function testHelperCallbackTriggering() {
  517. $View = new View($this->PostsController);
  518. $View->helpers = array('Html', 'Session');
  519. $View->Helpers = $this->getMock('HelperCollection', array('trigger'), array($View));
  520. $View->Helpers->expects($this->at(0))->method('trigger')
  521. ->with('beforeRender', new PHPUnit_Framework_Constraint_IsAnything());
  522. $View->Helpers->expects($this->at(1))->method('trigger')
  523. ->with('afterRender', new PHPUnit_Framework_Constraint_IsAnything());
  524. $View->Helpers->expects($this->at(2))->method('trigger')
  525. ->with('beforeLayout', new PHPUnit_Framework_Constraint_IsAnything());
  526. $View->Helpers->expects($this->at(3))->method('trigger')
  527. ->with('afterLayout', new PHPUnit_Framework_Constraint_IsAnything());
  528. $View->render('index');
  529. }
  530. /**
  531. * testBeforeLayout method
  532. *
  533. * @access public
  534. * @return void
  535. */
  536. function testBeforeLayout() {
  537. $this->PostsController->helpers = array('Session', 'TestAfter', 'Html');
  538. $View = new View($this->PostsController);
  539. $View->render('index');
  540. $this->assertEqual($View->Helpers->TestAfter->property, 'Valuation');
  541. }
  542. /**
  543. * testAfterLayout method
  544. *
  545. * @access public
  546. * @return void
  547. */
  548. function testAfterLayout() {
  549. $this->PostsController->helpers = array('Session', 'TestAfter', 'Html');
  550. $this->PostsController->set('variable', 'values');
  551. $View = new View($this->PostsController);
  552. ClassRegistry::addObject('afterView', $View);
  553. $content = 'This is my view output';
  554. $result = $View->renderLayout($content, 'default');
  555. $this->assertPattern('/modified in the afterlife/', $result);
  556. $this->assertPattern('/This is my view output/', $result);
  557. }
  558. /**
  559. * testRenderLoadHelper method
  560. *
  561. * @access public
  562. * @return void
  563. */
  564. function testRenderLoadHelper() {
  565. $this->PostsController->helpers = array('Session', 'Html', 'Form', 'Number');
  566. $View = new TestView($this->PostsController);
  567. $result = $View->render('index', false);
  568. $this->assertEqual($result, 'posts index');
  569. $attached = $View->Helpers->attached();
  570. $this->assertEquals($attached, array('Session', 'Html', 'Form', 'Number'));
  571. $this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
  572. $View = new TestView($this->PostsController);
  573. $result = $View->render('index', false);
  574. $this->assertEqual($result, 'posts index');
  575. $attached = $View->Helpers->attached();
  576. $expected = array('Html', 'Form', 'Number', 'PluggedHelper');
  577. $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
  578. }
  579. /**
  580. * testRender method
  581. *
  582. * @access public
  583. * @return void
  584. */
  585. function testRender() {
  586. $View = new TestView($this->PostsController);
  587. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render('index'));
  588. $this->assertPattern("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/><title>/", $result);
  589. $this->assertPattern("/<div id=\"content\">posts index<\/div>/", $result);
  590. $this->assertPattern("/<div id=\"content\">posts index<\/div>/", $result);
  591. $this->assertTrue(isset($View->viewVars['content_for_layout']), 'content_for_layout should be a view var');
  592. $this->assertTrue(isset($View->viewVars['scripts_for_layout']), 'scripts_for_layout should be a view var');
  593. $this->PostsController->set('url', 'flash');
  594. $this->PostsController->set('message', 'yo what up');
  595. $this->PostsController->set('pause', 3);
  596. $this->PostsController->set('page_title', 'yo what up');
  597. $View = new TestView($this->PostsController);
  598. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render(false, 'flash'));
  599. $this->assertPattern("/<title>yo what up<\/title>/", $result);
  600. $this->assertPattern("/<p><a href=\"flash\">yo what up<\/a><\/p>/", $result);
  601. $this->assertTrue($View->render(false, 'flash'));
  602. $this->PostsController->helpers = array('Session', 'Cache', 'Html');
  603. $this->PostsController->constructClasses();
  604. $this->PostsController->cacheAction = array('index' => 3600);
  605. $this->PostsController->request->params['action'] = 'index';
  606. Configure::write('Cache.check', true);
  607. $View = new TestView($this->PostsController);
  608. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render('index'));
  609. $this->assertPattern("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/><title>/", $result);
  610. $this->assertPattern("/<div id=\"content\">posts index<\/div>/", $result);
  611. $this->assertPattern("/<div id=\"content\">posts index<\/div>/", $result);
  612. }
  613. /**
  614. * test that View::$view works
  615. *
  616. * @return void
  617. */
  618. function testRenderUsingViewProperty() {
  619. $this->PostsController->view = 'cache_form';
  620. $View = new TestView($this->PostsController);
  621. $this->assertEquals('cache_form', $View->view);
  622. $result = $View->render();
  623. $this->assertRegExp('/Add User/', $result);
  624. }
  625. /**
  626. * test that view vars can replace the local helper variables
  627. * and not overwrite the $this->Helper references
  628. *
  629. * @return void
  630. */
  631. function testViewVarOverwritingLocalHelperVar() {
  632. $Controller = new ViewPostsController();
  633. $Controller->helpers = array('Session', 'Html');
  634. $Controller->set('html', 'I am some test html');
  635. $View = new View($Controller);
  636. $result = $View->render('helper_overwrite', false);
  637. $this->assertPattern('/I am some test html/', $result);
  638. $this->assertPattern('/Test link/', $result);
  639. }
  640. /**
  641. * testGetViewFileName method
  642. *
  643. * @access public
  644. * @return void
  645. */
  646. function testViewFileName() {
  647. $View = new TestView($this->PostsController);
  648. $result = $View->getViewFileName('index');
  649. $this->assertPattern('/posts(\/|\\\)index.ctp/', $result);
  650. $result = $View->getViewFileName('/Pages/home');
  651. $this->assertPattern('/Pages(\/|\\\)home.ctp/', $result);
  652. $result = $View->getViewFileName('../Elements/test_element');
  653. $this->assertPattern('/Elements(\/|\\\)test_element.ctp/', $result);
  654. $result = $View->getViewFileName('../Themed/TestTheme/Posts/index');
  655. $this->assertPattern('/Themed(\/|\\\)TestTheme(\/|\\\)Posts(\/|\\\)index.ctp/', $result);
  656. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS .'Posts' . DS .'index.ctp';
  657. $result = $View->getViewFileName('../Posts/index');
  658. $this->assertEqual($result, $expected);
  659. }
  660. /**
  661. * testRenderCache method
  662. *
  663. * @access public
  664. * @return void
  665. */
  666. function testRenderCache() {
  667. $writable = is_writable(CACHE . 'views' . DS);
  668. if ($this->skipIf(!$writable, 'CACHE/views dir is not writable, cannot test renderCache. %s')) {
  669. return;
  670. }
  671. $view = 'test_view';
  672. $View = new View($this->PostsController);
  673. $path = CACHE . 'views' . DS . 'view_cache_' . $view;
  674. $cacheText = '<!--cachetime:' . time() . '-->some cacheText';
  675. $f = fopen($path, 'w+');
  676. fwrite($f, $cacheText);
  677. fclose($f);
  678. $result = $View->renderCache($path, '+1 second');
  679. $this->assertFalse($result);
  680. @unlink($path);
  681. $cacheText = '<!--cachetime:' . (time() + 10) . '-->some cacheText';
  682. $f = fopen($path, 'w+');
  683. fwrite($f, $cacheText);
  684. fclose($f);
  685. ob_start();
  686. $View->renderCache($path, '+1 second');
  687. $result = ob_get_clean();
  688. $expected = 'some cacheText';
  689. $this->assertPattern('/^some cacheText/', $result);
  690. @unlink($path);
  691. }
  692. /**
  693. * Test that render() will remove the cake:nocache tags when only the cachehelper is present.
  694. *
  695. * @return void
  696. */
  697. function testRenderStrippingNoCacheTagsOnlyCacheHelper() {
  698. Configure::write('Cache.check', false);
  699. $View = new View($this->PostsController);
  700. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  701. $View->helpers = array('Html', 'Form', 'Cache');
  702. $View->layout = 'cache_layout';
  703. $result = $View->render('index');
  704. $this->assertNoPattern('/cake:nocache/', $result);
  705. }
  706. /**
  707. * Test that render() will remove the cake:nocache tags when only the Cache.check is true.
  708. *
  709. * @return void
  710. */
  711. function testRenderStrippingNoCacheTagsOnlyCacheCheck() {
  712. Configure::write('Cache.check', true);
  713. $View = new View($this->PostsController);
  714. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  715. $View->helpers = array('Html', 'Form');
  716. $View->layout = 'cache_layout';
  717. $result = $View->render('index');
  718. $this->assertNoPattern('/cake:nocache/', $result);
  719. }
  720. /**
  721. * testRenderNocache method
  722. *
  723. * @access public
  724. * @return void
  725. */
  726. /* This is a new test case for a pending enhancement
  727. function testRenderNocache() {
  728. $this->PostsController->helpers = array('Cache', 'Html');
  729. $this->PostsController->constructClasses();
  730. $this->PostsController->cacheAction = 21600;
  731. $this->PostsController->here = '/posts/nocache_multiple_element';
  732. $this->PostsController->action = 'nocache_multiple_element';
  733. $this->PostsController->nocache_multiple_element();
  734. Configure::write('Cache.check', true);
  735. Configure::write('Cache.disable', false);
  736. $filename = CACHE . 'views' . DS . 'posts_nocache_multiple_element.php';
  737. $View = new TestView($this->PostsController);
  738. $View->render();
  739. ob_start();
  740. $View->renderCache($filename, getMicroTime());
  741. $result = ob_get_clean();
  742. @unlink($filename);
  743. $this->assertPattern('/php echo \$foo;/', $result);
  744. $this->assertPattern('/php echo \$bar;/', $result);
  745. $this->assertPattern('/php \$barfoo = \'in sub2\';/', $result);
  746. $this->assertPattern('/php echo \$barfoo;/', $result);
  747. $this->assertPattern('/printing: "in sub2"/', $result);
  748. $this->assertPattern('/php \$foobar = \'in sub1\';/', $result);
  749. $this->assertPattern('/php echo \$foobar;/', $result);
  750. $this->assertPattern('/printing: "in sub1"/', $result);
  751. }
  752. */
  753. /**
  754. * testSet method
  755. *
  756. * @access public
  757. * @return void
  758. */
  759. function testSet() {
  760. $View = new TestView($this->PostsController);
  761. $View->viewVars = array();
  762. $View->set('somekey', 'someValue');
  763. $this->assertIdentical($View->viewVars, array('somekey' => 'someValue'));
  764. $this->assertIdentical($View->getVars(), array('somekey'));
  765. $View->viewVars = array();
  766. $keys = array('key1', 'key2');
  767. $values = array('value1', 'value2');
  768. $View->set($keys, $values);
  769. $this->assertIdentical($View->viewVars, array('key1' => 'value1', 'key2' => 'value2'));
  770. $this->assertIdentical($View->getVars(), array('key1', 'key2'));
  771. $this->assertIdentical($View->getVar('key1'), 'value1');
  772. $this->assertNull($View->getVar('key3'));
  773. $View->set(array('key3' => 'value3'));
  774. $this->assertIdentical($View->getVar('key3'), 'value3');
  775. $View->viewVars = array();
  776. $View->set(array(3 => 'three', 4 => 'four'));
  777. $View->set(array(1 => 'one', 2 => 'two'));
  778. $expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two');
  779. $this->assertEqual($View->viewVars, $expected);
  780. }
  781. /**
  782. * testEntityReference method
  783. *
  784. * @access public
  785. * @return void
  786. */
  787. function testEntityReference() {
  788. $View = new TestView($this->PostsController);
  789. $View->model = 'Post';
  790. $View->field = 'title';
  791. $this->assertEqual($View->entity(), array('Post', 'title'));
  792. $View->association = 'Comment';
  793. $View->field = 'user_id';
  794. $this->assertEqual($View->entity(), array('Comment', 'user_id'));
  795. $View->model = 0;
  796. $View->association = null;
  797. $View->field = 'Node';
  798. $View->fieldSuffix = 'title';
  799. $View->entityPath = '0.Node.title';
  800. $expected = array(0, 'Node', 'title');
  801. $this->assertEqual($View->entity(), $expected);
  802. $View->model = 'HelperTestTag';
  803. $View->field = 'HelperTestTag';
  804. $View->modelId = null;
  805. $View->association = null;
  806. $View->fieldSuffix = null;
  807. $View->entityPath = 'HelperTestTag';
  808. $expected = array('HelperTestTag', 'HelperTestTag');
  809. $this->assertEqual($View->entity(), $expected);
  810. }
  811. /**
  812. * testBadExt method
  813. *
  814. * @expectedException MissingViewException
  815. * @access public
  816. * @return void
  817. */
  818. function testBadExt() {
  819. $this->PostsController->action = 'something';
  820. $this->PostsController->ext = '.whatever';
  821. $View = new TestView($this->PostsController);
  822. $View->render('this_is_missing');
  823. $result = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  824. }
  825. /**
  826. * testAltExt method
  827. *
  828. * @access public
  829. * @return void
  830. */
  831. function testAltExt() {
  832. $this->PostsController->ext = '.alt';
  833. $View = new TestView($this->PostsController);
  834. $result = $View->render('alt_ext', false);
  835. $this->assertEqual($result, 'alt ext');
  836. }
  837. /**
  838. * testAltBadExt method
  839. *
  840. * @expectedException MissingViewException
  841. * @access public
  842. * @return void
  843. */
  844. 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. }