PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

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

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