PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/view/view.test.php

https://github.com/msadouni/cakephp2x
PHP | 906 lines | 448 code | 120 blank | 338 comment | 5 complexity | 948137af54f0456bc1767f6bdda1d41a MD5 | raw file
  1. <?php
  2. /**
  3. * ViewTest file
  4. *
  5. * Long description for file
  6. *
  7. * PHP Version 5.x
  8. *
  9. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  10. * Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The Open Group Test Suite License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  17. * @package cake
  18. * @subpackage cake.tests.cases.libs
  19. * @since CakePHP(tm) v 1.2.0.4206
  20. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  21. */
  22. App::import('Core', array('View', 'Controller'));
  23. App::import('Helper', 'Cache');
  24. Mock::generate('Helper', 'CallbackMockHelper');
  25. Mock::generate('CacheHelper', 'ViewTestMockCacheHelper');
  26. if (!class_exists('ErrorHandler')) {
  27. App::import('Core', array('Error'));
  28. }
  29. /**
  30. * ViewPostsController class
  31. *
  32. * @package cake
  33. * @subpackage cake.tests.cases.libs.view
  34. */
  35. class ViewPostsController extends Controller {
  36. /**
  37. * name property
  38. *
  39. * @var string 'Posts'
  40. * @access public
  41. */
  42. public $name = 'Posts';
  43. /**
  44. * uses property
  45. *
  46. * @var mixed null
  47. * @access public
  48. */
  49. public $uses = null;
  50. /**
  51. * index method
  52. *
  53. * @access public
  54. * @return void
  55. */
  56. public function index() {
  57. $this->set('testData', 'Some test data');
  58. $test2 = 'more data';
  59. $test3 = 'even more data';
  60. $this->set(compact('test2', 'test3'));
  61. }
  62. /**
  63. * nocache_tags_with_element method
  64. *
  65. * @access public
  66. * @return void
  67. */
  68. public function nocache_multiple_element() {
  69. $this->set('foo', 'this is foo var');
  70. $this->set('bar', 'this is bar var');
  71. }
  72. }
  73. /**
  74. * ViewTestErrorHandler class
  75. *
  76. * @package cake
  77. * @subpackage cake.tests.cases.libs.view
  78. */
  79. class ViewTestErrorHandler extends ErrorHandler {
  80. /**
  81. * stop method
  82. *
  83. * @access public
  84. * @return void
  85. */
  86. function _stop() {
  87. return;
  88. }
  89. }
  90. /**
  91. * TestView class
  92. *
  93. * @package cake
  94. * @subpackage cake.tests.cases.libs.view
  95. */
  96. class TestView extends View {
  97. /**
  98. * getViewFileName method
  99. *
  100. * @param mixed $name
  101. * @access public
  102. * @return void
  103. */
  104. function getViewFileName($name = null) {
  105. return $this->_getViewFileName($name);
  106. }
  107. /**
  108. * getLayoutFileName method
  109. *
  110. * @param mixed $name
  111. * @access public
  112. * @return void
  113. */
  114. function getLayoutFileName($name = null) {
  115. return $this->_getLayoutFileName($name);
  116. }
  117. /**
  118. * loadHelpers method
  119. *
  120. * @param mixed $loaded
  121. * @param mixed $helpers
  122. * @param mixed $parent
  123. * @access public
  124. * @return void
  125. */
  126. function loadHelpers(&$loaded, $helpers, $parent = null) {
  127. return $this->_loadHelpers($loaded, $helpers, $parent);
  128. }
  129. /**
  130. * paths method
  131. *
  132. * @param string $plugin
  133. * @param boolean $cached
  134. * @access public
  135. * @return void
  136. */
  137. function paths($plugin = null, $cached = true) {
  138. return $this->_paths($plugin, $cached);
  139. }
  140. /**
  141. * cakeError method
  142. *
  143. * @param mixed $method
  144. * @param mixed $messages
  145. * @access public
  146. * @return void
  147. */
  148. function cakeError($method, $messages) {
  149. $error = new ViewTestErrorHandler($method, $messages);
  150. return $error;
  151. }
  152. }
  153. /**
  154. * TestAfterHelper class
  155. *
  156. * @package cake
  157. * @subpackage cake.tests.cases.libs.view
  158. */
  159. class TestAfterHelper extends Helper {
  160. /**
  161. * property property
  162. *
  163. * @var string ''
  164. * @access public
  165. */
  166. var $property = '';
  167. /**
  168. * beforeLayout method
  169. *
  170. * @access public
  171. * @return void
  172. */
  173. function beforeLayout() {
  174. $this->property = 'Valuation';
  175. }
  176. /**
  177. * afterLayout method
  178. *
  179. * @access public
  180. * @return void
  181. */
  182. function afterLayout() {
  183. $View = ClassRegistry::getObject('afterView');
  184. $View->output .= 'modified in the afterlife';
  185. }
  186. }
  187. /**
  188. * ViewTest class
  189. *
  190. * @package cake
  191. * @subpackage cake.tests.cases.libs
  192. */
  193. class ViewTest extends CakeTestCase {
  194. /**
  195. * setUp method
  196. *
  197. * @access public
  198. * @return void
  199. */
  200. function setUp() {
  201. Router::reload();
  202. $this->Controller = new Controller();
  203. $this->PostsController = new ViewPostsController();
  204. $this->PostsController->viewPath = 'posts';
  205. $this->PostsController->index();
  206. $this->View = new View($this->PostsController);
  207. }
  208. /**
  209. * tearDown method
  210. *
  211. * @access public
  212. * @return void
  213. */
  214. function tearDown() {
  215. unset($this->View);
  216. unset($this->PostsController);
  217. unset($this->Controller);
  218. }
  219. /**
  220. * endTest
  221. *
  222. * @access public
  223. * @return void
  224. */
  225. function startTest() {
  226. App::build(array(
  227. 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
  228. 'views' => array(
  229. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS,
  230. TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS
  231. )
  232. ), true);
  233. }
  234. /**
  235. * endTest
  236. *
  237. * @access public
  238. * @return void
  239. */
  240. function endTest() {
  241. App::build();
  242. }
  243. /**
  244. * testPluginGetTemplate method
  245. *
  246. * @access public
  247. * @return void
  248. */
  249. function testPluginGetTemplate() {
  250. $this->Controller->plugin = 'test_plugin';
  251. $this->Controller->name = 'TestPlugin';
  252. $this->Controller->viewPath = 'tests';
  253. $this->Controller->action = 'index';
  254. $View = new TestView($this->Controller);
  255. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS .'test_plugin' . DS . 'views' . DS .'tests' . DS .'index.ctp';
  256. $result = $View->getViewFileName('index');
  257. $this->assertEqual($result, $expected);
  258. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS .'test_plugin' . DS . 'views' . DS . 'layouts' . DS .'default.ctp';
  259. $result = $View->getLayoutFileName();
  260. $this->assertEqual($result, $expected);
  261. }
  262. /**
  263. * test that plugin/$plugin_name is only appended to the paths it should be.
  264. *
  265. * @return void
  266. */
  267. function testPluginPathGeneration() {
  268. $this->Controller->plugin = 'test_plugin';
  269. $this->Controller->name = 'TestPlugin';
  270. $this->Controller->viewPath = 'tests';
  271. $this->Controller->action = 'index';
  272. $View = new TestView($this->Controller);
  273. $paths = $View->paths();
  274. $this->assertEqual($paths, App::path('views'));
  275. $paths = $View->paths('test_plugin');
  276. $expected = array(
  277. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'plugins' . DS . 'test_plugin' . DS,
  278. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin' . DS . 'views' . DS,
  279. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS,
  280. TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS
  281. );
  282. $this->assertEqual($paths, $expected);
  283. }
  284. /**
  285. * test that CamelCase plugins still find their view files.
  286. *
  287. * @return void
  288. */
  289. function testCamelCasePluginGetTemplate() {
  290. $this->Controller->plugin = 'TestPlugin';
  291. $this->Controller->name = 'TestPlugin';
  292. $this->Controller->viewPath = 'tests';
  293. $this->Controller->action = 'index';
  294. $View = new TestView($this->Controller);
  295. App::build(array(
  296. 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
  297. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
  298. ));
  299. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS .'test_plugin' . DS . 'views' . DS .'tests' . DS .'index.ctp';
  300. $result = $View->getViewFileName('index');
  301. $this->assertEqual($result, $expected);
  302. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS .'test_plugin' . DS . 'views' . DS . 'layouts' . DS .'default.ctp';
  303. $result = $View->getLayoutFileName();
  304. $this->assertEqual($result, $expected);
  305. }
  306. /**
  307. * testGetTemplate method
  308. *
  309. * @access public
  310. * @return void
  311. */
  312. function testGetTemplate() {
  313. $this->Controller->plugin = null;
  314. $this->Controller->name = 'Pages';
  315. $this->Controller->viewPath = 'pages';
  316. $this->Controller->action = 'display';
  317. $this->Controller->params['pass'] = array('home');
  318. $View = new TestView($this->Controller);
  319. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS .'pages' . DS .'home.ctp';
  320. $result = $View->getViewFileName('home');
  321. $this->assertEqual($result, $expected);
  322. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS .'posts' . DS .'index.ctp';
  323. $result = $View->getViewFileName('/posts/index');
  324. $this->assertEqual($result, $expected);
  325. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS .'posts' . DS .'index.ctp';
  326. $result = $View->getViewFileName('../posts/index');
  327. $this->assertEqual($result, $expected);
  328. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'layouts' . DS .'default.ctp';
  329. $result = $View->getLayoutFileName();
  330. $this->assertEqual($result, $expected);
  331. $View->layoutPath = 'rss';
  332. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'layouts' . DS . 'rss' . DS . 'default.ctp';
  333. $result = $View->getLayoutFileName();
  334. $this->assertEqual($result, $expected);
  335. $View->layoutPath = 'email' . DS . 'html';
  336. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'layouts' . DS . 'email' . DS . 'html' . DS . 'default.ctp';
  337. $result = $View->getLayoutFileName();
  338. $this->assertEqual($result, $expected);
  339. }
  340. /**
  341. * testMissingView method
  342. *
  343. * @access public
  344. * @return void
  345. */
  346. function testMissingView() {
  347. $this->Controller->plugin = null;
  348. $this->Controller->name = 'Pages';
  349. $this->Controller->viewPath = 'pages';
  350. $this->Controller->action = 'display';
  351. $this->Controller->params['pass'] = array('home');
  352. $View = new TestView($this->Controller);
  353. ob_start();
  354. $result = $View->getViewFileName('does_not_exist');
  355. $expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  356. $this->assertPattern("/PagesController::/", $expected);
  357. $this->assertPattern("/pages(\/|\\\)does_not_exist.ctp/", $expected);
  358. }
  359. /**
  360. * testMissingLayout method
  361. *
  362. * @access public
  363. * @return void
  364. */
  365. function testMissingLayout() {
  366. $this->Controller->plugin = null;
  367. $this->Controller->name = 'Posts';
  368. $this->Controller->viewPath = 'posts';
  369. $this->Controller->layout = 'whatever';
  370. $View = new TestView($this->Controller);
  371. ob_start();
  372. $result = $View->getLayoutFileName();
  373. $expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  374. $this->assertPattern("/Missing Layout/", $expected);
  375. $this->assertPattern("/layouts(\/|\\\)whatever.ctp/", $expected);
  376. }
  377. /**
  378. * testUUIDGeneration method
  379. *
  380. * @access public
  381. * @return void
  382. */
  383. function testUUIDGeneration() {
  384. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  385. $this->assertEqual($result, 'form5988016017');
  386. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  387. $this->assertEqual($result, 'formc3dc6be854');
  388. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  389. $this->assertEqual($result, 'form28f92cc87f');
  390. }
  391. /**
  392. * testAddInlineScripts method
  393. *
  394. * @access public
  395. * @return void
  396. */
  397. function testAddInlineScripts() {
  398. $this->View->addScript('prototype.js');
  399. $this->View->addScript('prototype.js');
  400. $this->assertEqual($this->View->scripts(), array('prototype.js'));
  401. $this->View->addScript('mainEvent', 'Event.observe(window, "load", function() { doSomething(); }, true);');
  402. $this->assertEqual($this->View->scripts(), array('prototype.js', 'mainEvent' => 'Event.observe(window, "load", function() { doSomething(); }, true);'));
  403. }
  404. /**
  405. * testElement method
  406. *
  407. * @access public
  408. * @return void
  409. */
  410. function testElement() {
  411. $result = $this->View->element('test_element');
  412. $this->assertEqual($result, 'this is the test element');
  413. $result = $this->View->element('plugin_element', array('plugin' => 'test_plugin'));
  414. $this->assertEqual($result, 'this is the plugin element using params[plugin]');
  415. $this->View->plugin = 'test_plugin';
  416. $result = $this->View->element('test_plugin_element');
  417. $this->assertEqual($result, 'this is the test set using View::$plugin plugin element');
  418. $result = $this->View->element('non_existant_element');
  419. $this->assertPattern('/Not Found:/', $result);
  420. $this->assertPattern('/non_existant_element/', $result);
  421. }
  422. /**
  423. * testElementCacheHelperNoCache method
  424. *
  425. * @access public
  426. * @return void
  427. */
  428. function testElementCacheHelperNoCache() {
  429. $Controller = new ViewPostsController();
  430. $View = new View($Controller);
  431. $empty = array();
  432. $helpers = $View->_loadHelpers($empty, array('cache'));
  433. $View->loaded = $helpers;
  434. $result = $View->element('test_element', array('ram' => 'val', 'test' => array('foo', 'bar')));
  435. $this->assertEqual($result, 'this is the test element');
  436. }
  437. /**
  438. * testElementCache method
  439. *
  440. * @access public
  441. * @return void
  442. */
  443. function testElementCache() {
  444. $View = new TestView($this->PostsController);
  445. $element = 'test_element';
  446. $expected = 'this is the test element';
  447. $result = $View->element($element);
  448. $this->assertEqual($result, $expected);
  449. $cached = false;
  450. $result = $View->element($element, array('cache'=>'+1 second'));
  451. if (file_exists(CACHE . 'views' . DS . 'element_cache_'.$element)) {
  452. $cached = true;
  453. unlink(CACHE . 'views' . DS . 'element_cache_'.$element);
  454. }
  455. $this->assertTrue($cached);
  456. $cached = false;
  457. $result = $View->element($element, array('cache'=>'+1 second', 'other_param'=> true, 'anotherParam'=> true));
  458. if (file_exists(CACHE . 'views' . DS . 'element_cache_other_param_anotherParam_'.$element)) {
  459. $cached = true;
  460. unlink(CACHE . 'views' . DS . 'element_cache_other_param_anotherParam_'.$element);
  461. }
  462. $this->assertTrue($cached);
  463. $cached = false;
  464. $result = $View->element($element, array('cache'=>array('time'=>'+1 second', 'key'=>'/whatever/here')));
  465. if (file_exists(CACHE . 'views' . DS . 'element_'.Inflector::slug('/whatever/here').'_'.$element)) {
  466. $cached = true;
  467. unlink(CACHE . 'views' . DS . 'element_'.Inflector::slug('/whatever/here').'_'.$element);
  468. }
  469. $this->assertTrue($cached);
  470. $cached = false;
  471. $result = $View->element($element, array('cache'=>array('time'=>'+1 second', 'key'=>'whatever_here')));
  472. if (file_exists(CACHE . 'views' . DS . 'element_whatever_here_'.$element)) {
  473. $cached = true;
  474. unlink(CACHE . 'views' . DS . 'element_whatever_here_'.$element);
  475. }
  476. $this->assertTrue($cached);
  477. $this->assertEqual($result, $expected);
  478. }
  479. /**
  480. * testLoadHelpers method
  481. *
  482. * @access public
  483. * @return void
  484. */
  485. function testLoadHelpers() {
  486. $View = new TestView($this->PostsController);
  487. $loaded = array();
  488. $result = $View->loadHelpers($loaded, array('Html', 'Form', 'Ajax'));
  489. $this->assertTrue(is_object($result['Html']));
  490. $this->assertTrue(is_object($result['Form']));
  491. $this->assertTrue(is_object($result['Form']->Html));
  492. $this->assertTrue(is_object($result['Ajax']->Html));
  493. $View->plugin = 'test_plugin';
  494. $result = $View->loadHelpers($loaded, array('TestPlugin.PluggedHelper'));
  495. $this->assertTrue(is_object($result['PluggedHelper']));
  496. $this->assertTrue(is_object($result['PluggedHelper']->OtherHelper));
  497. }
  498. /**
  499. * test the correct triggering of helper callbacks
  500. *
  501. * @return void
  502. */
  503. function testHelperCallbackTriggering() {
  504. $this->PostsController->helpers = array('Html', 'CallbackMock');
  505. $View = new TestView($this->PostsController);
  506. $loaded = array();
  507. $View->loaded = $View->loadHelpers($loaded, $this->PostsController->helpers);
  508. $View->loaded['CallbackMock']->expectOnce('beforeRender');
  509. $View->loaded['CallbackMock']->expectOnce('afterRender');
  510. $View->loaded['CallbackMock']->expectOnce('beforeLayout');
  511. $View->loaded['CallbackMock']->expectOnce('afterLayout');
  512. $View->render('index');
  513. }
  514. /**
  515. * testBeforeLayout method
  516. *
  517. * @access public
  518. * @return void
  519. */
  520. function testBeforeLayout() {
  521. $this->PostsController->helpers = array('TestAfter', 'Html');
  522. $View = new View($this->PostsController);
  523. $out = $View->render('index');
  524. $this->assertEqual($View->loaded['testAfter']->property, 'Valuation');
  525. }
  526. /**
  527. * testAfterLayout method
  528. *
  529. * @access public
  530. * @return void
  531. */
  532. function testAfterLayout() {
  533. $this->PostsController->helpers = array('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->assertPattern('/modified in the afterlife/', $result);
  540. $this->assertPattern('/This is my view output/', $result);
  541. }
  542. /**
  543. * testRenderLoadHelper method
  544. *
  545. * @access public
  546. * @return void
  547. */
  548. function testRenderLoadHelper() {
  549. $this->PostsController->helpers = array('Html', 'Form', 'Ajax');
  550. $View = new TestView($this->PostsController);
  551. $result = $View->render($View->getViewFileName('index'), array());
  552. $this->assertEqual($result, 'posts index');
  553. $helpers = $View->loaded;
  554. $this->assertTrue(is_object($helpers['html']));
  555. $this->assertTrue(is_object($helpers['form']));
  556. $this->assertTrue(is_object($helpers['form']->Html));
  557. $this->assertTrue(is_object($helpers['ajax']->Html));
  558. $this->PostsController->helpers = array('Html', 'Form', 'Ajax', 'TestPlugin.PluggedHelper');
  559. $View = new TestView($this->PostsController);
  560. $result = $View->render($View->getViewFileName('index'), array());
  561. $this->assertEqual($result, 'posts index');
  562. $helpers = $View->loaded;
  563. $this->assertTrue(is_object($helpers['html']));
  564. $this->assertTrue(is_object($helpers['form']));
  565. $this->assertTrue(is_object($helpers['form']->Html));
  566. $this->assertTrue(is_object($helpers['ajax']->Html));
  567. $this->assertTrue(is_object($helpers['pluggedHelper']->OtherHelper));
  568. $this->assertTrue(is_object($View->Html));
  569. $this->assertTrue(is_object($View->Form));
  570. $this->assertTrue(is_object($View->Form->Html));
  571. $this->assertTrue(is_object($View->PluggedHelper->OtherHelper));
  572. $this->assertReference($View->Form, $View->loaded['form']);
  573. $this->assertReference($View->Html, $View->loaded['html']);
  574. $this->assertReference($View->PluggedHelper->OtherHelper, $View->loaded['otherHelper']);
  575. }
  576. /**
  577. * testRender method
  578. *
  579. * @access public
  580. * @return void
  581. */
  582. function testRender() {
  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. $this->PostsController->set('url', 'flash');
  589. $this->PostsController->set('message', 'yo what up');
  590. $this->PostsController->set('pause', 3);
  591. $this->PostsController->set('page_title', 'yo what up');
  592. $View = new TestView($this->PostsController);
  593. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render(false, 'flash'));
  594. $this->assertPattern("/<title>yo what up<\/title>/", $result);
  595. $this->assertPattern("/<p><a href=\"flash\">yo what up<\/a><\/p>/", $result);
  596. $this->assertTrue($View->render(false, 'flash'));
  597. $this->PostsController->helpers = array('Cache', 'Html');
  598. $this->PostsController->constructClasses();
  599. $this->PostsController->cacheAction = array('index' => 3600);
  600. Configure::write('Cache.check', true);
  601. $View = new TestView($this->PostsController);
  602. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render('index'));
  603. $this->assertPattern("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/><title>/", $result);
  604. $this->assertPattern("/<div id=\"content\">posts index<\/div>/", $result);
  605. $this->assertPattern("/<div id=\"content\">posts index<\/div>/", $result);
  606. }
  607. /**
  608. * test rendering layout with cache helper loaded
  609. *
  610. * @return void
  611. */
  612. function testRenderLayoutWithMockCacheHelper() {
  613. $_check = Configure::read('Cache.check');
  614. Configure::write('Cache.check', true);
  615. $Controller = new ViewPostsController();
  616. $Controller->cacheAction = '1 day';
  617. $View = new View($Controller);
  618. $View->loaded['cache'] = new ViewTestMockCacheHelper();
  619. $View->loaded['cache']->expectCallCount('cache', 2);
  620. $result = $View->render('index');
  621. $this->assertPattern('/posts index/', $result);
  622. Configure::write('Cache.check', $_check);
  623. }
  624. /**
  625. * test that view vars can replace the local helper variables
  626. * and not overwrite the $this->Helper references
  627. *
  628. * @return void
  629. */
  630. function testViewVarOverwritingLocalHelperVar() {
  631. $Controller = new ViewPostsController();
  632. $Controller->helpers = array('Html');
  633. $Controller->set('html', 'I am some test html');
  634. $View = new View($Controller);
  635. $result = $View->render('helper_overwrite', false);
  636. $this->assertPattern('/I am some test html/', $result);
  637. $this->assertPattern('/Test link/', $result);
  638. }
  639. /**
  640. * testGetViewFileName method
  641. *
  642. * @access public
  643. * @return void
  644. */
  645. function testViewFileName() {
  646. $View = new TestView($this->PostsController);
  647. $result = $View->getViewFileName('index');
  648. $this->assertPattern('/posts(\/|\\\)index.ctp/', $result);
  649. $result = $View->getViewFileName('/pages/home');
  650. $this->assertPattern('/pages(\/|\\\)home.ctp/', $result);
  651. $result = $View->getViewFileName('../elements/test_element');
  652. $this->assertPattern('/elements(\/|\\\)test_element.ctp/', $result);
  653. $result = $View->getViewFileName('../themed/test_theme/posts/index');
  654. $this->assertPattern('/themed(\/|\\\)test_theme(\/|\\\)posts(\/|\\\)index.ctp/', $result);
  655. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS .'posts' . DS .'index.ctp';
  656. $result = $View->getViewFileName('../posts/index');
  657. $this->assertEqual($result, $expected);
  658. }
  659. /**
  660. * testRenderCache method
  661. *
  662. * @access public
  663. * @return void
  664. */
  665. function testRenderCache() {
  666. $view = 'test_view';
  667. $View = new View($this->PostsController);
  668. $path = CACHE . 'views' . DS . 'view_cache_'.$view;
  669. $cacheText = '<!--cachetime:'.time().'-->some cacheText';
  670. $f = fopen($path, 'w+');
  671. fwrite($f, $cacheText);
  672. fclose($f);
  673. $result = $View->renderCache($path, '+1 second');
  674. $this->assertFalse($result);
  675. @unlink($path);
  676. $cacheText = '<!--cachetime:'.(time() + 10).'-->some cacheText';
  677. $f = fopen($path, 'w+');
  678. fwrite($f, $cacheText);
  679. fclose($f);
  680. ob_start();
  681. $View->renderCache($path, '+1 second');
  682. $result = ob_get_clean();
  683. $expected = 'some cacheText';
  684. $this->assertPattern('/^some cacheText/', $result);
  685. @unlink($path);
  686. }
  687. /**
  688. * testRenderNocache method
  689. *
  690. * @access public
  691. * @return void
  692. */
  693. /* This is a new test case for a pending enhancement
  694. function testRenderNocache() {
  695. $this->PostsController->helpers = array('Cache', 'Html');
  696. $this->PostsController->constructClasses();
  697. $this->PostsController->cacheAction = 21600;
  698. $this->PostsController->here = '/posts/nocache_multiple_element';
  699. $this->PostsController->action = 'nocache_multiple_element';
  700. $this->PostsController->nocache_multiple_element();
  701. Configure::write('Cache.check', true);
  702. Configure::write('Cache.disable', false);
  703. $filename = CACHE . 'views' . DS . 'posts_nocache_multiple_element.php';
  704. $View = new TestView($this->PostsController);
  705. $View->render();
  706. ob_start();
  707. $View->renderCache($filename, microtime(true));
  708. $result = ob_get_clean();
  709. @unlink($filename);
  710. $this->assertPattern('/php echo \$foo;/', $result);
  711. $this->assertPattern('/php echo \$bar;/', $result);
  712. $this->assertPattern('/php \$barfoo = \'in sub2\';/', $result);
  713. $this->assertPattern('/php echo \$barfoo;/', $result);
  714. $this->assertPattern('/printing: "in sub2"/', $result);
  715. $this->assertPattern('/php \$foobar = \'in sub1\';/', $result);
  716. $this->assertPattern('/php echo \$foobar;/', $result);
  717. $this->assertPattern('/printing: "in sub1"/', $result);
  718. }
  719. */
  720. /**
  721. * testSet method
  722. *
  723. * @access public
  724. * @return void
  725. */
  726. function testSet() {
  727. $View = new TestView($this->PostsController);
  728. $View->viewVars = array();
  729. $View->set('somekey', 'someValue');
  730. $this->assertIdentical($View->viewVars, array('somekey' => 'someValue'));
  731. $this->assertIdentical($View->getVars(), array('somekey'));
  732. $View->viewVars = array();
  733. $keys = array('key1', 'key2');
  734. $values = array('value1', 'value2');
  735. $View->set($keys, $values);
  736. $this->assertIdentical($View->viewVars, array('key1' => 'value1', 'key2' => 'value2'));
  737. $this->assertIdentical($View->getVars(), array('key1', 'key2'));
  738. $this->assertIdentical($View->getVar('key1'), 'value1');
  739. $this->assertNull($View->getVar('key3'));
  740. $View->set(array('key3' => 'value3'));
  741. $this->assertIdentical($View->getVar('key3'), 'value3');
  742. }
  743. /**
  744. * testEntityReference method
  745. *
  746. * @access public
  747. * @return void
  748. */
  749. function testEntityReference() {
  750. $View = new TestView($this->PostsController);
  751. $View->model = 'Post';
  752. $View->field = 'title';
  753. $this->assertEqual($View->entity(), array('Post', 'title'));
  754. $View->association = 'Comment';
  755. $View->field = 'user_id';
  756. $this->assertEqual($View->entity(), array('Comment', 'user_id'));
  757. }
  758. /**
  759. * testBadExt method
  760. *
  761. * @access public
  762. * @return void
  763. */
  764. function testBadExt() {
  765. $this->PostsController->action = 'something';
  766. $this->PostsController->ext = '.whatever';
  767. restore_error_handler();
  768. ob_start();
  769. $View = new TestView($this->PostsController);
  770. $View->render('this_is_missing');
  771. $result = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  772. set_error_handler('simpleTestErrorHandler');
  773. $this->assertPattern("/<em>PostsController::<\/em><em>something\(\)<\/em>/", $result);
  774. $this->assertPattern("/posts(\/|\\\)this_is_missing.whatever/", $result);
  775. $this->PostsController->ext = ".bad";
  776. $View = new TestView($this->PostsController);
  777. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render('index'));
  778. $this->assertPattern("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/><title>/", $result);
  779. $this->assertPattern("/<div id=\"content\">posts index<\/div>/", $result);
  780. }
  781. }
  782. ?>