PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

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