PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Cake/Test/Case/View/ViewTest.php

http://github.com/cakephp/cakephp
PHP | 929 lines | 477 code | 132 blank | 320 comment | 1 complexity | 90454b7e1936afbc3f7eb2877cff3178 MD5 | raw file
Possible License(s): JSON
  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('Html', 'Session');
  505. $View->Helpers = $this->getMock('HelperCollection', array('trigger'), array($View));
  506. $View->Helpers->expects($this->at(0))->method('trigger')
  507. ->with('beforeRender', $this->anything());
  508. $View->Helpers->expects($this->at(1))->method('trigger')
  509. ->with('afterRender', $this->anything());
  510. $View->Helpers->expects($this->at(2))->method('trigger')
  511. ->with('beforeLayout', $this->anything());
  512. $View->Helpers->expects($this->at(3))->method('trigger')
  513. ->with('afterLayout', $this->anything());
  514. $View->render('index');
  515. }
  516. /**
  517. * testBeforeLayout method
  518. *
  519. * @return void
  520. */
  521. public function testBeforeLayout() {
  522. $this->PostsController->helpers = array('Session', 'TestAfter', 'Html');
  523. $View = new View($this->PostsController);
  524. $View->render('index');
  525. $this->assertEquals($View->Helpers->TestAfter->property, 'Valuation');
  526. }
  527. /**
  528. * testAfterLayout method
  529. *
  530. * @return void
  531. */
  532. public function testAfterLayout() {
  533. $this->PostsController->helpers = array('Session', 'TestAfter', 'Html');
  534. $this->PostsController->set('variable', 'values');
  535. $View = new View($this->PostsController);
  536. ClassRegistry::addObject('afterView', $View);
  537. $content = 'This is my view output';
  538. $result = $View->renderLayout($content, 'default');
  539. $this->assertRegExp('/modified in the afterlife/', $result);
  540. $this->assertRegExp('/This is my view output/', $result);
  541. }
  542. /**
  543. * testRenderLoadHelper method
  544. *
  545. * @return void
  546. */
  547. public function testRenderLoadHelper() {
  548. $this->PostsController->helpers = array('Session', 'Html', 'Form', 'Number');
  549. $View = new TestView($this->PostsController);
  550. $result = $View->render('index', false);
  551. $this->assertEquals($result, 'posts index');
  552. $attached = $View->Helpers->attached();
  553. $this->assertEquals($attached, array('Session', 'Html', 'Form', 'Number'));
  554. $this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
  555. $View = new TestView($this->PostsController);
  556. $result = $View->render('index', false);
  557. $this->assertEquals($result, 'posts index');
  558. $attached = $View->Helpers->attached();
  559. $expected = array('Html', 'Form', 'Number', 'PluggedHelper');
  560. $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
  561. }
  562. /**
  563. * testRender method
  564. *
  565. * @return void
  566. */
  567. public function testRender() {
  568. $View = new TestView($this->PostsController);
  569. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render('index'));
  570. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/><title>/", $result);
  571. $this->assertRegExp("/<div id=\"content\">posts index<\/div>/", $result);
  572. $this->assertRegExp("/<div id=\"content\">posts index<\/div>/", $result);
  573. $this->assertTrue(isset($View->viewVars['content_for_layout']), 'content_for_layout should be a view var');
  574. $this->assertTrue(isset($View->viewVars['scripts_for_layout']), 'scripts_for_layout should be a view var');
  575. $this->PostsController->set('url', 'flash');
  576. $this->PostsController->set('message', 'yo what up');
  577. $this->PostsController->set('pause', 3);
  578. $this->PostsController->set('page_title', 'yo what up');
  579. $View = new TestView($this->PostsController);
  580. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render(false, 'flash'));
  581. $this->assertRegExp("/<title>yo what up<\/title>/", $result);
  582. $this->assertRegExp("/<p><a href=\"flash\">yo what up<\/a><\/p>/", $result);
  583. $this->assertTrue($View->render(false, 'flash'));
  584. $this->PostsController->helpers = array('Session', 'Cache', 'Html');
  585. $this->PostsController->constructClasses();
  586. $this->PostsController->cacheAction = array('index' => 3600);
  587. $this->PostsController->request->params['action'] = 'index';
  588. Configure::write('Cache.check', true);
  589. $View = new TestView($this->PostsController);
  590. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render('index'));
  591. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/><title>/", $result);
  592. $this->assertRegExp("/<div id=\"content\">posts index<\/div>/", $result);
  593. $this->assertRegExp("/<div id=\"content\">posts index<\/div>/", $result);
  594. }
  595. /**
  596. * test that View::$view works
  597. *
  598. * @return void
  599. */
  600. public function testRenderUsingViewProperty() {
  601. $this->PostsController->view = 'cache_form';
  602. $View = new TestView($this->PostsController);
  603. $this->assertEquals('cache_form', $View->view);
  604. $result = $View->render();
  605. $this->assertRegExp('/Add User/', $result);
  606. }
  607. /**
  608. * test that view vars can replace the local helper variables
  609. * and not overwrite the $this->Helper references
  610. *
  611. * @return void
  612. */
  613. public function testViewVarOverwritingLocalHelperVar() {
  614. $Controller = new ViewPostsController();
  615. $Controller->helpers = array('Session', 'Html');
  616. $Controller->set('html', 'I am some test html');
  617. $View = new View($Controller);
  618. $result = $View->render('helper_overwrite', false);
  619. $this->assertRegExp('/I am some test html/', $result);
  620. $this->assertRegExp('/Test link/', $result);
  621. }
  622. /**
  623. * testGetViewFileName method
  624. *
  625. * @return void
  626. */
  627. public function testViewFileName() {
  628. $View = new TestView($this->PostsController);
  629. $result = $View->getViewFileName('index');
  630. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  631. $result = $View->getViewFileName('/Pages/home');
  632. $this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
  633. $result = $View->getViewFileName('../Elements/test_element');
  634. $this->assertRegExp('/Elements(\/|\\\)test_element.ctp/', $result);
  635. $result = $View->getViewFileName('../Themed/TestTheme/Posts/index');
  636. $this->assertRegExp('/Themed(\/|\\\)TestTheme(\/|\\\)Posts(\/|\\\)index.ctp/', $result);
  637. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS .'Posts' . DS .'index.ctp';
  638. $result = $View->getViewFileName('../Posts/index');
  639. $this->assertEquals($expected, $result);
  640. }
  641. /**
  642. * testRenderCache method
  643. *
  644. * @return void
  645. */
  646. public function testRenderCache() {
  647. $this->skipIf(!is_writable(CACHE . 'views' . DS), 'CACHE/views dir is not writable, cannot test renderCache.');
  648. $view = 'test_view';
  649. $View = new View($this->PostsController);
  650. $path = CACHE . 'views' . DS . 'view_cache_' . $view;
  651. $cacheText = '<!--cachetime:' . time() . '-->some cacheText';
  652. $f = fopen($path, 'w+');
  653. fwrite($f, $cacheText);
  654. fclose($f);
  655. $result = $View->renderCache($path, '+1 second');
  656. $this->assertFalse($result);
  657. if (file_exists($path)) {
  658. unlink($path);
  659. }
  660. $cacheText = '<!--cachetime:' . (time() + 10) . '-->some cacheText';
  661. $f = fopen($path, 'w+');
  662. fwrite($f, $cacheText);
  663. fclose($f);
  664. ob_start();
  665. $View->renderCache($path, '+1 second');
  666. $result = ob_get_clean();
  667. $expected = 'some cacheText';
  668. $this->assertRegExp('/^some cacheText/', $result);
  669. @unlink($path);
  670. }
  671. /**
  672. * Test that render() will remove the cake:nocache tags when only the cachehelper is present.
  673. *
  674. * @return void
  675. */
  676. public function testRenderStrippingNoCacheTagsOnlyCacheHelper() {
  677. Configure::write('Cache.check', false);
  678. $View = new View($this->PostsController);
  679. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  680. $View->helpers = array('Html', 'Form', 'Cache');
  681. $View->layout = 'cache_layout';
  682. $result = $View->render('index');
  683. $this->assertNotRegExp('/cake:nocache/', $result);
  684. }
  685. /**
  686. * Test that render() will remove the cake:nocache tags when only the Cache.check is true.
  687. *
  688. * @return void
  689. */
  690. public function testRenderStrippingNoCacheTagsOnlyCacheCheck() {
  691. Configure::write('Cache.check', true);
  692. $View = new View($this->PostsController);
  693. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  694. $View->helpers = array('Html', 'Form');
  695. $View->layout = 'cache_layout';
  696. $result = $View->render('index');
  697. $this->assertNotRegExp('/cake:nocache/', $result);
  698. }
  699. /**
  700. * testRenderNocache method
  701. *
  702. * @return void
  703. */
  704. /* This is a new test case for a pending enhancement
  705. public function testRenderNocache() {
  706. $this->PostsController->helpers = array('Cache', 'Html');
  707. $this->PostsController->constructClasses();
  708. $this->PostsController->cacheAction = 21600;
  709. $this->PostsController->here = '/posts/nocache_multiple_element';
  710. $this->PostsController->action = 'nocache_multiple_element';
  711. $this->PostsController->nocache_multiple_element();
  712. Configure::write('Cache.check', true);
  713. Configure::write('Cache.disable', false);
  714. $filename = CACHE . 'views' . DS . 'posts_nocache_multiple_element.php';
  715. $View = new TestView($this->PostsController);
  716. $View->render();
  717. ob_start();
  718. $View->renderCache($filename, getMicroTime());
  719. $result = ob_get_clean();
  720. @unlink($filename);
  721. $this->assertRegExp('/php echo \$foo;/', $result);
  722. $this->assertRegExp('/php echo \$bar;/', $result);
  723. $this->assertRegExp('/php \$barfoo = \'in sub2\';/', $result);
  724. $this->assertRegExp('/php echo \$barfoo;/', $result);
  725. $this->assertRegExp('/printing: "in sub2"/', $result);
  726. $this->assertRegExp('/php \$foobar = \'in sub1\';/', $result);
  727. $this->assertRegExp('/php echo \$foobar;/', $result);
  728. $this->assertRegExp('/printing: "in sub1"/', $result);
  729. }
  730. */
  731. /**
  732. * testSet method
  733. *
  734. * @return void
  735. */
  736. public function testSet() {
  737. $View = new TestView($this->PostsController);
  738. $View->viewVars = array();
  739. $View->set('somekey', 'someValue');
  740. $this->assertSame($View->viewVars, array('somekey' => 'someValue'));
  741. $this->assertSame($View->getVars(), array('somekey'));
  742. $View->viewVars = array();
  743. $keys = array('key1', 'key2');
  744. $values = array('value1', 'value2');
  745. $View->set($keys, $values);
  746. $this->assertSame($View->viewVars, array('key1' => 'value1', 'key2' => 'value2'));
  747. $this->assertSame($View->getVars(), array('key1', 'key2'));
  748. $this->assertSame($View->getVar('key1'), 'value1');
  749. $this->assertNull($View->getVar('key3'));
  750. $View->set(array('key3' => 'value3'));
  751. $this->assertSame($View->getVar('key3'), 'value3');
  752. $View->viewVars = array();
  753. $View->set(array(3 => 'three', 4 => 'four'));
  754. $View->set(array(1 => 'one', 2 => 'two'));
  755. $expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two');
  756. $this->assertEquals($View->viewVars, $expected);
  757. }
  758. /**
  759. * testBadExt method
  760. *
  761. * @expectedException MissingViewException
  762. * @return void
  763. */
  764. public function testBadExt() {
  765. $this->PostsController->action = 'something';
  766. $this->PostsController->ext = '.whatever';
  767. $View = new TestView($this->PostsController);
  768. $View->render('this_is_missing');
  769. $result = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  770. }
  771. /**
  772. * testAltExt method
  773. *
  774. * @return void
  775. */
  776. public function testAltExt() {
  777. $this->PostsController->ext = '.alt';
  778. $View = new TestView($this->PostsController);
  779. $result = $View->render('alt_ext', false);
  780. $this->assertEquals($result, 'alt ext');
  781. }
  782. /**
  783. * testAltBadExt method
  784. *
  785. * @expectedException MissingViewException
  786. * @return void
  787. */
  788. public function testAltBadExt() {
  789. $View = new TestView($this->PostsController);
  790. $View->render('alt_ext');
  791. $result = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  792. }
  793. }