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

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

http://github.com/Datawalke/Coordino
PHP | 1026 lines | 526 code | 133 blank | 367 comment | 8 complexity | 4c062de64a8b745fb8f4b949ea49effc 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-2012, 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-2012, 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. * Test that alternate extensions work with duplicated elements.
  431. *
  432. * @return void
  433. */
  434. function testElementExtensions() {
  435. $this->View->ext = '.xml';
  436. $result = $this->View->element('test_element');
  437. $this->assertEqual(trim($result), '<p>test element</p>');
  438. }
  439. /**
  440. * test that additional element viewVars don't get overwritten with helpers.
  441. *
  442. * @return void
  443. */
  444. function testElementParamsDontOverwriteHelpers() {
  445. $Controller = new ViewPostsController();
  446. $Controller->helpers = array('Form');
  447. $View = new View($Controller);
  448. $result = $View->element('type_check', array('form' => 'string'), true);
  449. $this->assertEqual('string', $result);
  450. $View->set('form', 'string');
  451. $result = $View->element('type_check', array(), true);
  452. $this->assertEqual('string', $result);
  453. }
  454. /**
  455. * testElementCacheHelperNoCache method
  456. *
  457. * @access public
  458. * @return void
  459. */
  460. function testElementCacheHelperNoCache() {
  461. $Controller = new ViewPostsController();
  462. $View = new View($Controller);
  463. $empty = array();
  464. $helpers = $View->_loadHelpers($empty, array('cache'));
  465. $View->loaded = $helpers;
  466. $result = $View->element('test_element', array('ram' => 'val', 'test' => array('foo', 'bar')));
  467. $this->assertEqual($result, 'this is the test element');
  468. }
  469. /**
  470. * testElementCache method
  471. *
  472. * @access public
  473. * @return void
  474. */
  475. function testElementCache() {
  476. $writable = is_writable(CACHE . 'views' . DS);
  477. if ($this->skipIf(!$writable, 'CACHE/views dir is not writable, cannot test elementCache. %s')) {
  478. return;
  479. }
  480. $View = new TestView($this->PostsController);
  481. $element = 'test_element';
  482. $expected = 'this is the test element';
  483. $result = $View->element($element);
  484. $this->assertEqual($result, $expected);
  485. $cached = false;
  486. $result = $View->element($element, array('cache'=>'+1 second'));
  487. if (file_exists(CACHE . 'views' . DS . 'element_cache_'.$element)) {
  488. $cached = true;
  489. unlink(CACHE . 'views' . DS . 'element_cache_'.$element);
  490. }
  491. $this->assertTrue($cached);
  492. $cached = false;
  493. $result = $View->element($element, array('cache' => true, 'param' => 'val'));
  494. if (file_exists(CACHE . 'views' . DS . 'element_cache_param_'.$element)) {
  495. $cached = true;
  496. unlink(CACHE . 'views' . DS . 'element_cache_param_'.$element);
  497. }
  498. $this->assertTrue($cached);
  499. $cached = false;
  500. $result = $View->element($element, array('cache'=>'+1 second', 'other_param'=> true, 'anotherParam'=> true));
  501. if (file_exists(CACHE . 'views' . DS . 'element_cache_other_param_anotherParam_'.$element)) {
  502. $cached = true;
  503. unlink(CACHE . 'views' . DS . 'element_cache_other_param_anotherParam_'.$element);
  504. }
  505. $this->assertTrue($cached);
  506. $cached = false;
  507. $result = $View->element($element, array('cache'=>array('time'=>'+1 second', 'key'=>'/whatever/here')));
  508. if (file_exists(CACHE . 'views' . DS . 'element_'.Inflector::slug('/whatever/here').'_'.$element)) {
  509. $cached = true;
  510. unlink(CACHE . 'views' . DS . 'element_'.Inflector::slug('/whatever/here').'_'.$element);
  511. }
  512. $this->assertTrue($cached);
  513. $cached = false;
  514. $result = $View->element($element, array('cache'=>array('time'=>'+1 second', 'key'=>'whatever_here')));
  515. if (file_exists(CACHE . 'views' . DS . 'element_whatever_here_'.$element)) {
  516. $cached = true;
  517. unlink(CACHE . 'views' . DS . 'element_whatever_here_'.$element);
  518. }
  519. $this->assertTrue($cached);
  520. $this->assertEqual($result, $expected);
  521. }
  522. /**
  523. * test that ctp is used as a fallback file extension for elements
  524. *
  525. * @return void
  526. */
  527. function testElementCtpFallback() {
  528. $View = new TestView($this->PostsController);
  529. $View->ext = '.missing';
  530. $element = 'test_element';
  531. $expected = 'this is the test element';
  532. $result = $View->element($element);
  533. $this->assertEqual($expected, $result);
  534. }
  535. /**
  536. * testLoadHelpers method
  537. *
  538. * @access public
  539. * @return void
  540. */
  541. function testLoadHelpers() {
  542. $View = new TestView($this->PostsController);
  543. $loaded = array();
  544. $result = $View->loadHelpers($loaded, array('Html', 'Form', 'Ajax'));
  545. $this->assertTrue(is_object($result['Html']));
  546. $this->assertTrue(is_object($result['Form']));
  547. $this->assertTrue(is_object($result['Form']->Html));
  548. $this->assertTrue(is_object($result['Ajax']->Html));
  549. $View->plugin = 'test_plugin';
  550. $result = $View->loadHelpers($loaded, array('TestPlugin.PluggedHelper'));
  551. $this->assertTrue(is_object($result['PluggedHelper']));
  552. $this->assertTrue(is_object($result['PluggedHelper']->OtherHelper));
  553. }
  554. /**
  555. * test the correct triggering of helper callbacks
  556. *
  557. * @return void
  558. */
  559. function testHelperCallbackTriggering() {
  560. $this->PostsController->helpers = array('Session', 'Html', 'CallbackMock');
  561. $View =& new TestView($this->PostsController);
  562. $loaded = array();
  563. $View->loaded = $View->loadHelpers($loaded, $this->PostsController->helpers);
  564. $View->loaded['CallbackMock']->expectOnce('beforeRender');
  565. $View->loaded['CallbackMock']->expectOnce('afterRender');
  566. $View->loaded['CallbackMock']->expectOnce('beforeLayout');
  567. $View->loaded['CallbackMock']->expectOnce('afterLayout');
  568. $View->render('index');
  569. }
  570. /**
  571. * testBeforeLayout method
  572. *
  573. * @access public
  574. * @return void
  575. */
  576. function testBeforeLayout() {
  577. $this->PostsController->helpers = array('Session', 'TestAfter', 'Html');
  578. $View =& new View($this->PostsController);
  579. $out = $View->render('index');
  580. $this->assertEqual($View->loaded['testAfter']->property, 'Valuation');
  581. }
  582. /**
  583. * testAfterLayout method
  584. *
  585. * @access public
  586. * @return void
  587. */
  588. function testAfterLayout() {
  589. $this->PostsController->helpers = array('Session', 'TestAfter', 'Html');
  590. $this->PostsController->set('variable', 'values');
  591. $View =& new View($this->PostsController);
  592. ClassRegistry::addObject('afterView', $View);
  593. $content = 'This is my view output';
  594. $result = $View->renderLayout($content, 'default');
  595. $this->assertPattern('/modified in the afterlife/', $result);
  596. $this->assertPattern('/This is my view output/', $result);
  597. }
  598. /**
  599. * testRenderLoadHelper method
  600. *
  601. * @access public
  602. * @return void
  603. */
  604. function testRenderLoadHelper() {
  605. $this->PostsController->helpers = array('Session', 'Html', 'Form', 'Ajax');
  606. $View =& new TestView($this->PostsController);
  607. $result = $View->_render($View->getViewFileName('index'), array());
  608. $this->assertEqual($result, 'posts index');
  609. $helpers = $View->loaded;
  610. $this->assertTrue(is_object($helpers['html']));
  611. $this->assertTrue(is_object($helpers['form']));
  612. $this->assertTrue(is_object($helpers['form']->Html));
  613. $this->assertTrue(is_object($helpers['ajax']->Html));
  614. $this->PostsController->helpers = array('Html', 'Form', 'Ajax', 'TestPlugin.PluggedHelper');
  615. $View =& new TestView($this->PostsController);
  616. $result = $View->_render($View->getViewFileName('index'), array());
  617. $this->assertEqual($result, 'posts index');
  618. $helpers = $View->loaded;
  619. $this->assertTrue(is_object($helpers['html']));
  620. $this->assertTrue(is_object($helpers['form']));
  621. $this->assertTrue(is_object($helpers['form']->Html));
  622. $this->assertTrue(is_object($helpers['ajax']->Html));
  623. $this->assertTrue(is_object($helpers['pluggedHelper']->OtherHelper));
  624. $this->assertTrue(is_object($View->Html));
  625. $this->assertTrue(is_object($View->Form));
  626. $this->assertTrue(is_object($View->Form->Html));
  627. $this->assertTrue(is_object($View->PluggedHelper->OtherHelper));
  628. $this->assertReference($View->Form, $View->loaded['form']);
  629. $this->assertReference($View->Html, $View->loaded['html']);
  630. $this->assertReference($View->PluggedHelper->OtherHelper, $View->loaded['otherHelper']);
  631. }
  632. /**
  633. * testRender method
  634. *
  635. * @access public
  636. * @return void
  637. */
  638. function testRender() {
  639. $View =& new TestView($this->PostsController);
  640. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render('index'));
  641. $this->assertPattern("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/><title>/", $result);
  642. $this->assertPattern("/<div id=\"content\">posts index<\/div>/", $result);
  643. $this->assertPattern("/<div id=\"content\">posts index<\/div>/", $result);
  644. $this->PostsController->set('url', 'flash');
  645. $this->PostsController->set('message', 'yo what up');
  646. $this->PostsController->set('pause', 3);
  647. $this->PostsController->set('page_title', 'yo what up');
  648. $View =& new TestView($this->PostsController);
  649. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render(false, 'flash'));
  650. $this->assertPattern("/<title>yo what up<\/title>/", $result);
  651. $this->assertPattern("/<p><a href=\"flash\">yo what up<\/a><\/p>/", $result);
  652. $this->assertTrue($View->render(false, 'flash'));
  653. $this->PostsController->helpers = array('Session', 'Cache', 'Html');
  654. $this->PostsController->constructClasses();
  655. $this->PostsController->cacheAction = array('index' => 3600);
  656. $this->PostsController->params['action'] = 'index';
  657. Configure::write('Cache.check', true);
  658. $View =& new TestView($this->PostsController);
  659. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render('index'));
  660. $this->assertPattern("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/><title>/", $result);
  661. $this->assertPattern("/<div id=\"content\">posts index<\/div>/", $result);
  662. $this->assertPattern("/<div id=\"content\">posts index<\/div>/", $result);
  663. }
  664. /**
  665. * test rendering layout with cache helper loaded
  666. *
  667. * @return void
  668. */
  669. function testRenderLayoutWithMockCacheHelper() {
  670. $_check = Configure::read('Cache.check');
  671. Configure::write('Cache.check', true);
  672. $Controller =& new ViewPostsController();
  673. $Controller->cacheAction = '+1 day';
  674. $View =& new View($Controller);
  675. $View->loaded['cache'] = new ViewTestMockCacheHelper();
  676. $View->loaded['cache']->expectCallCount('cache', 2);
  677. $View->render('index');
  678. Configure::write('Cache.check', $_check);
  679. }
  680. /**
  681. * test that view vars can replace the local helper variables
  682. * and not overwrite the $this->Helper references
  683. *
  684. * @return void
  685. */
  686. function testViewVarOverwritingLocalHelperVar() {
  687. $Controller =& new ViewPostsController();
  688. $Controller->helpers = array('Session', 'Html');
  689. $Controller->set('html', 'I am some test html');
  690. $View =& new View($Controller);
  691. $result = $View->render('helper_overwrite', false);
  692. $this->assertPattern('/I am some test html/', $result);
  693. $this->assertPattern('/Test link/', $result);
  694. }
  695. /**
  696. * testGetViewFileName method
  697. *
  698. * @access public
  699. * @return void
  700. */
  701. function testViewFileName() {
  702. $View =& new TestView($this->PostsController);
  703. $result = $View->getViewFileName('index');
  704. $this->assertPattern('/posts(\/|\\\)index.ctp/', $result);
  705. $result = $View->getViewFileName('/pages/home');
  706. $this->assertPattern('/pages(\/|\\\)home.ctp/', $result);
  707. $result = $View->getViewFileName('../elements/test_element');
  708. $this->assertPattern('/elements(\/|\\\)test_element.ctp/', $result);
  709. $result = $View->getViewFileName('../themed/test_theme/posts/index');
  710. $this->assertPattern('/themed(\/|\\\)test_theme(\/|\\\)posts(\/|\\\)index.ctp/', $result);
  711. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS .'posts' . DS .'index.ctp';
  712. $result = $View->getViewFileName('../posts/index');
  713. $this->assertEqual($result, $expected);
  714. }
  715. /**
  716. * testRenderCache method
  717. *
  718. * @access public
  719. * @return void
  720. */
  721. function testRenderCache() {
  722. $writable = is_writable(CACHE . 'views' . DS);
  723. if ($this->skipIf(!$writable, 'CACHE/views dir is not writable, cannot test renderCache. %s')) {
  724. return;
  725. }
  726. $view = 'test_view';
  727. $View =& new View($this->PostsController);
  728. $path = CACHE . 'views' . DS . 'view_cache_'.$view;
  729. $cacheText = '<!--cachetime:'.time().'-->some cacheText';
  730. $f = fopen($path, 'w+');
  731. fwrite($f, $cacheText);
  732. fclose($f);
  733. $result = $View->renderCache($path, '+1 second');
  734. $this->assertFalse($result);
  735. @unlink($path);
  736. $cacheText = '<!--cachetime:'.(time() + 10).'-->some cacheText';
  737. $f = fopen($path, 'w+');
  738. fwrite($f, $cacheText);
  739. fclose($f);
  740. ob_start();
  741. $View->renderCache($path, '+1 second');
  742. $result = ob_get_clean();
  743. $expected = 'some cacheText';
  744. $this->assertPattern('/^some cacheText/', $result);
  745. @unlink($path);
  746. }
  747. /**
  748. * Test that render() will remove the cake:nocache tags when only the cachehelper is present.
  749. *
  750. * @return void
  751. */
  752. function testRenderStrippingNoCacheTagsOnlyCacheHelper() {
  753. Configure::write('Cache.check', false);
  754. $View =& new View($this->PostsController);
  755. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  756. $View->helpers = array('Html', 'Form', 'Cache');
  757. $View->layout = 'cache_layout';
  758. $result = $View->render('index');
  759. $this->assertNoPattern('/cake:nocache/', $result);
  760. }
  761. /**
  762. * Test that render() will remove the cake:nocache tags when only the Cache.check is true.
  763. *
  764. * @return void
  765. */
  766. function testRenderStrippingNoCacheTagsOnlyCacheCheck() {
  767. Configure::write('Cache.check', true);
  768. $View =& new View($this->PostsController);
  769. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  770. $View->helpers = array('Html', 'Form');
  771. $View->layout = 'cache_layout';
  772. $result = $View->render('index');
  773. $this->assertNoPattern('/cake:nocache/', $result);
  774. }
  775. /**
  776. * testRenderNocache method
  777. *
  778. * @access public
  779. * @return void
  780. */
  781. /* This is a new test case for a pending enhancement
  782. function testRenderNocache() {
  783. $this->PostsController->helpers = array('Cache', 'Html');
  784. $this->PostsController->constructClasses();
  785. $this->PostsController->cacheAction = 21600;
  786. $this->PostsController->here = '/posts/nocache_multiple_element';
  787. $this->PostsController->action = 'nocache_multiple_element';
  788. $this->PostsController->nocache_multiple_element();
  789. Configure::write('Cache.check', true);
  790. Configure::write('Cache.disable', false);
  791. $filename = CACHE . 'views' . DS . 'posts_nocache_multiple_element.php';
  792. $View = new TestView($this->PostsController);
  793. $View->render();
  794. ob_start();
  795. $View->renderCache($filename, getMicroTime());
  796. $result = ob_get_clean();
  797. @unlink($filename);
  798. $this->assertPattern('/php echo \$foo;/', $result);
  799. $this->assertPattern('/php echo \$bar;/', $result);
  800. $this->assertPattern('/php \$barfoo = \'in sub2\';/', $result);
  801. $this->assertPattern('/php echo \$barfoo;/', $result);
  802. $this->assertPattern('/printing: "in sub2"/', $result);
  803. $this->assertPattern('/php \$foobar = \'in sub1\';/', $result);
  804. $this->assertPattern('/php echo \$foobar;/', $result);
  805. $this->assertPattern('/printing: "in sub1"/', $result);
  806. }
  807. */
  808. /**
  809. * testSet method
  810. *
  811. * @access public
  812. * @return void
  813. */
  814. function testSet() {
  815. $View =& new TestView($this->PostsController);
  816. $View->viewVars = array();
  817. $View->set('somekey', 'someValue');
  818. $this->assertIdentical($View->viewVars, array('somekey' => 'someValue'));
  819. $this->assertIdentical($View->getVars(), array('somekey'));
  820. $View->viewVars = array();
  821. $keys = array('key1', 'key2');
  822. $values = array('value1', 'value2');
  823. $View->set($keys, $values);
  824. $this->assertIdentical($View->viewVars, array('key1' => 'value1', 'key2' => 'value2'));
  825. $this->assertIdentical($View->getVars(), array('key1', 'key2'));
  826. $this->assertIdentical($View->getVar('key1'), 'value1');
  827. $this->assertNull($View->getVar('key3'));
  828. $View->set(array('key3' => 'value3'));
  829. $this->assertIdentical($View->getVar('key3'), 'value3');
  830. $View->viewVars = array();
  831. $View->set(array(3 => 'three', 4 => 'four'));
  832. $View->set(array(1 => 'one', 2 => 'two'));
  833. $expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two');
  834. $this->assertEqual($View->viewVars, $expected);
  835. }
  836. /**
  837. * testEntityReference method
  838. *
  839. * @access public
  840. * @return void
  841. */
  842. function testEntityReference() {
  843. $View =& new TestView($this->PostsController);
  844. $View->model = 'Post';
  845. $View->field = 'title';
  846. $this->assertEqual($View->entity(), array('Post', 'title'));
  847. $View->association = 'Comment';
  848. $View->field = 'user_id';
  849. $this->assertEqual($View->entity(), array('Comment', 'user_id'));
  850. $View->model = 0;
  851. $View->association = null;
  852. $View->field = 'Node';
  853. $View->fieldSuffix = 'title';
  854. $View->entityPath = '0.Node.title';
  855. $expected = array(0, 'Node', 'title');
  856. $this->assertEqual($View->entity(), $expected);
  857. $View->model = 'HelperTestTag';
  858. $View->field = 'HelperTestTag';
  859. $View->modelId = null;
  860. $View->association = null;
  861. $View->fieldSuffix = null;
  862. $View->entityPath = 'HelperTestTag';
  863. $expected = array('HelperTestTag', 'HelperTestTag');
  864. $this->assertEqual($View->entity(), $expected);
  865. }
  866. /**
  867. * testBadExt method
  868. *
  869. * @access public
  870. * @return void
  871. */
  872. function testBadExt() {
  873. $this->PostsController->action = 'something';
  874. $this->PostsController->ext = '.whatever';
  875. restore_error_handler();
  876. ob_start();
  877. $View = new TestView($this->PostsController);
  878. $View->render('this_is_missing');
  879. $result = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
  880. set_error_handler('simpleTestErrorHandler');
  881. $this->assertPattern("/<em>PostsController::<\/em><em>something\(\)<\/em>/", $result);
  882. $this->assertPattern("/posts(\/|\\\)this_is_missing.whatever/", $result);
  883. $this->PostsController->ext = ".bad";
  884. $View =& new TestView($this->PostsController);
  885. $result = str_replace(array("\t", "\r\n", "\n"), "", $View->render('index'));
  886. $this->assertPattern("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/><title>/", $result);
  887. $this->assertPattern("/<div id=\"content\">posts index<\/div>/", $result);
  888. }
  889. }