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

/code/ryzom/tools/server/www/webtt/cake/tests/cases/libs/view/view.test.php

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