PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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