ViewTest.php 26 KB

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