ViewTest.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. if (!defined('PHPUnit_MAIN_METHOD')) {
  22. define('PHPUnit_MAIN_METHOD', 'Zend_ViewTest::main');
  23. }
  24. /**
  25. * Test helper
  26. */
  27. require_once dirname(__FILE__) . '/../TestHelper.php';
  28. /**
  29. * Zend_View
  30. */
  31. require_once 'Zend/View.php';
  32. /**
  33. * Zend_View_Interface
  34. */
  35. require_once 'Zend/View/Interface.php';
  36. /**
  37. * Zend_Loader
  38. */
  39. require_once 'Zend/Loader.php';
  40. /**
  41. * @category Zend
  42. * @package Zend_View
  43. * @subpackage UnitTests
  44. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  45. * @license http://framework.zend.com/license/new-bsd New BSD License
  46. */
  47. class Zend_ViewTest extends PHPUnit_Framework_TestCase
  48. {
  49. public static function main()
  50. {
  51. $suite = new PHPUnit_Framework_TestSuite("Zend_ViewTest");
  52. $result = PHPUnit_TextUI_TestRunner::run($suite);
  53. }
  54. public function setUp()
  55. {
  56. $this->notices = array();
  57. $this->errorReporting = error_reporting();
  58. $this->displayErrors = ini_get('display_errors');
  59. }
  60. public function tearDown()
  61. {
  62. error_reporting($this->errorReporting);
  63. ini_set('display_errors', $this->displayErrors);
  64. }
  65. /**
  66. * Tests that the default script path is properly initialized
  67. */
  68. public function testDefaultScriptPath()
  69. {
  70. $this->_testDefaultPath('script', false);
  71. }
  72. /**
  73. * Tests that the default helper path is properly initialized
  74. * and the directory is readable
  75. */
  76. public function testDefaultHelperPath()
  77. {
  78. $this->_testDefaultPath('helper');
  79. }
  80. /**
  81. * Tests that the default filter path is properly initialized
  82. * and the directory is readable
  83. */
  84. public function testDefaultFilterPath()
  85. {
  86. $this->_testDefaultPath('filter', false);
  87. }
  88. /**
  89. * Tests that script paths are added, properly ordered, and that
  90. * directory separators are handled correctly.
  91. */
  92. public function testAddScriptPath()
  93. {
  94. $this->_testAddPath('script');
  95. }
  96. /**
  97. * Tests that helper paths are added, properly ordered, and that
  98. * directory separators are handled correctly.
  99. */
  100. public function testAddHelperPath()
  101. {
  102. $this->_testAddPath('helper');
  103. }
  104. /**
  105. * Tests that filter paths are added, properly ordered, and that
  106. * directory separators are handled correctly.
  107. */
  108. public function testAddFilterPath()
  109. {
  110. $this->_testAddPath('filter');
  111. }
  112. /**
  113. * Tests that the (script|helper|filter) path array is properly
  114. * initialized after instantiation.
  115. *
  116. * @param string $pathType one of "script", "helper", or "filter".
  117. * @param boolean $testReadability check if the path is readable?
  118. */
  119. protected function _testDefaultPath($pathType, $testReadability = true)
  120. {
  121. $view = new Zend_View();
  122. $reflector = $view->getAllPaths();
  123. $paths = $this->_filterPath($reflector[$pathType]);
  124. // test default helper path
  125. $this->assertType('array', $paths);
  126. if ('script' == $pathType) {
  127. $this->assertEquals(0, count($paths));
  128. } else {
  129. $this->assertEquals(1, count($paths));
  130. $prefix = 'Zend_View_' . ucfirst($pathType) . '_';
  131. $this->assertTrue(array_key_exists($prefix, $paths));
  132. if ($testReadability) {
  133. $path = current($paths[$prefix]);
  134. if (substr(PHP_OS, 0, 3) != 'WIN') {
  135. $this->assertTrue(Zend_Loader::isReadable($path));
  136. } else {
  137. $this->assertTrue(is_dir($path));
  138. }
  139. }
  140. }
  141. }
  142. /**
  143. * Tests (script|helper|filter) paths can be added, that they are added
  144. * in the proper order, and that directory separators are properly handled.
  145. *
  146. * @param string $pathType one of "script", "helper", or "filter".
  147. */
  148. protected function _testAddPath($pathType)
  149. {
  150. $view = new Zend_View();
  151. $prefix = 'Zend_View_' . ucfirst($pathType) . '_';
  152. // introspect default paths and build expected results.
  153. $reflector = $view->getAllPaths();
  154. $expectedPaths = $reflector[$pathType];
  155. if ($pathType != 'script') {
  156. $expectedPaths = $this->_filterPath($expectedPaths[$prefix]);
  157. }
  158. array_push($expectedPaths, 'baz');
  159. array_push($expectedPaths, 'bar');
  160. array_push($expectedPaths, 'foo');
  161. // add paths
  162. $func = 'add' . ucfirst($pathType) . 'Path';
  163. $view->$func('baz'); // no separator
  164. $view->$func('bar\\'); // windows
  165. $view->$func('foo/'); // unix
  166. // introspect script paths after adding two new paths
  167. $reflector = $view->getAllPaths();
  168. $actualPaths = $this->_filterPath($reflector[$pathType]);
  169. switch ($pathType) {
  170. case 'script':
  171. $this->assertSame(array_reverse($expectedPaths), $actualPaths);
  172. break;
  173. case 'helper':
  174. case 'filter':
  175. default:
  176. $this->assertTrue(array_key_exists($prefix, $actualPaths));
  177. $this->assertSame($expectedPaths, $actualPaths[$prefix], 'Actual: ' . var_export($actualPaths, 1) . "\nExpected: " . var_export($expectedPaths, 1));
  178. }
  179. }
  180. /**
  181. * Tests that the Zend_View environment is clean of any instance variables
  182. */
  183. public function testSandbox()
  184. {
  185. $view = new Zend_View();
  186. $this->assertSame(array(), get_object_vars($view));
  187. }
  188. /**
  189. * Tests that isset() and empty() work correctly. This is a common problem
  190. * because __isset() was not supported until PHP 5.1.
  191. */
  192. public function testIssetEmpty()
  193. {
  194. $view = new Zend_View();
  195. $this->assertFalse(isset($view->foo));
  196. $this->assertTrue(empty($view->foo));
  197. $view->foo = 'bar';
  198. $this->assertTrue(isset($view->foo));
  199. $this->assertFalse(empty($view->foo));
  200. }
  201. /**
  202. * Tests that a help can be loaded from the search path
  203. *
  204. */
  205. public function testLoadHelper()
  206. {
  207. $view = new Zend_View();
  208. $view->setHelperPath(
  209. array(
  210. dirname(__FILE__) . '/View/_stubs/HelperDir1',
  211. dirname(__FILE__) . '/View/_stubs/HelperDir2'
  212. )
  213. );
  214. $this->assertEquals('foo', $view->stub1(), var_export($view->getHelperPaths(), 1));
  215. $this->assertEquals('bar', $view->stub2());
  216. // erase the paths to the helper stubs
  217. $view->setHelperPath(null);
  218. // verify that object handle of a stub was cache by calling it again
  219. // without its path in the helper search paths
  220. $this->assertEquals( 'foo', $view->stub1() );
  221. }
  222. /**
  223. * Tests that calling a nonexistant helper file throws the expected exception
  224. */
  225. public function testLoadHelperNonexistantFile()
  226. {
  227. $view = new Zend_View();
  228. try {
  229. $view->nonexistantHelper();
  230. // @todo fail if no exception?
  231. } catch (Zend_Exception $e) {
  232. $this->assertContains('not found', $e->getMessage());
  233. }
  234. }
  235. /**
  236. * Tests that calling a helper whose file exists but class is not found within
  237. * throws the expected exception
  238. */
  239. public function testLoadHelperNonexistantClass()
  240. {
  241. $view = new Zend_View();
  242. $view->setHelperPath(array(dirname(__FILE__) . '/View/_stubs/HelperDir1'));
  243. try {
  244. // attempt to load the helper StubEmpty, whose file exists but
  245. // does not contain the expected class within
  246. $view->stubEmpty();
  247. // @todo fail if no exception?
  248. } catch (Zend_Exception $e) {
  249. $this->assertContains("not found", $e->getMessage());
  250. }
  251. }
  252. public function testHelperPathMayBeRegisteredUnderMultiplePrefixes()
  253. {
  254. $view = new Zend_View();
  255. $view->addHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir1', 'Foo_View_Helper');
  256. $view->addHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir1', 'Zend_View_Helper');
  257. $helper = $view->getHelper('Stub1');
  258. $this->assertTrue($helper instanceof Foo_View_Helper_Stub1);
  259. }
  260. /**
  261. * Tests that render() can render a template.
  262. */
  263. public function testRender()
  264. {
  265. $view = new Zend_View();
  266. $view->setScriptPath(dirname(__FILE__) . '/View/_templates');
  267. $view->bar = 'bar';
  268. $this->assertEquals("foo bar baz\n", $view->render('test.phtml') );
  269. }
  270. /**
  271. * Tests that render() works when called within a template, and that
  272. * protected members are not available
  273. */
  274. public function testRenderSubTemplates()
  275. {
  276. $view = new Zend_View();
  277. $view->setScriptPath(dirname(__FILE__) . '/View/_templates');
  278. $view->content = 'testSubTemplate.phtml';
  279. $this->assertEquals('', $view->render('testParent.phtml'));
  280. $logFile = dirname(__FILE__) . '/View/_templates/view.log';
  281. $this->assertTrue(file_exists($logFile));
  282. $log = file_get_contents($logFile);
  283. unlink($logFile); // clean up...
  284. $this->assertContains('This text should not be displayed', $log);
  285. $this->assertNotContains('testSubTemplate.phtml', $log);
  286. }
  287. /**
  288. * Tests that array properties may be modified after being set (see [ZF-460]
  289. * and [ZF-268] for symptoms leading to this test)
  290. */
  291. public function testSetArrayProperty()
  292. {
  293. $view = new Zend_View();
  294. $view->foo = array();
  295. $view->foo[] = 42;
  296. $foo = $view->foo;
  297. $this->assertTrue(is_array($foo));
  298. $this->assertEquals(42, $foo[0], var_export($foo, 1));
  299. $view->assign('bar', array());
  300. $view->bar[] = 'life';
  301. $bar = $view->bar;
  302. $this->assertTrue(is_array($bar));
  303. $this->assertEquals('life', $bar[0], var_export($bar, 1));
  304. $view->assign(array(
  305. 'baz' => array('universe'),
  306. ));
  307. $view->baz[] = 'everything';
  308. $baz = $view->baz;
  309. $this->assertTrue(is_array($baz));
  310. $this->assertEquals('universe', $baz[0]);
  311. $this->assertEquals('everything', $baz[1], var_export($baz, 1));
  312. }
  313. /**
  314. * Test that array properties are cleared following clearVars() call
  315. */
  316. public function testClearVars()
  317. {
  318. $view = new Zend_View();
  319. $view->foo = array();
  320. $view->content = 'content';
  321. $this->assertTrue(is_array($view->foo));
  322. $this->assertEquals('content', $view->content);
  323. $view->clearVars();
  324. $this->assertFalse(isset($view->foo));
  325. $this->assertFalse(isset($view->content));
  326. }
  327. /**
  328. * Test that script paths are cleared following setScriptPath(null) call
  329. */
  330. public function testClearScriptPath()
  331. {
  332. $view = new Zend_View();
  333. // paths should be initially empty
  334. $this->assertSame(array(), $view->getScriptPaths());
  335. // add a path
  336. $view->setScriptPath('foo');
  337. $scriptPaths = $view->getScriptPaths();
  338. $this->assertType('array', $scriptPaths);
  339. $this->assertEquals(1, count($scriptPaths));
  340. // clear paths
  341. $view->setScriptPath(null);
  342. $this->assertSame(array(), $view->getScriptPaths());
  343. }
  344. /**
  345. * Test that an exception is thrown when no script path is set
  346. */
  347. public function testNoPath()
  348. {
  349. $view = new Zend_View();
  350. try {
  351. $view->render('somefootemplate.phtml');
  352. $this->fail('Rendering a template when no script path is set should raise an exception');
  353. } catch (Exception $e) {
  354. // success...
  355. // @todo assert something?
  356. }
  357. }
  358. /**
  359. * Test that getEngine() returns the same object
  360. */
  361. public function testGetEngine()
  362. {
  363. $view = new Zend_View();
  364. $this->assertSame($view, $view->getEngine());
  365. }
  366. public function testInstanceOfInterface()
  367. {
  368. $view = new Zend_View();
  369. $this->assertTrue($view instanceof Zend_View_Interface);
  370. }
  371. public function testGetVars()
  372. {
  373. $view = new Zend_View();
  374. $view->foo = 'bar';
  375. $view->bar = 'baz';
  376. $view->baz = array('foo', 'bar');
  377. $vars = $view->getVars();
  378. $this->assertEquals(3, count($vars));
  379. $this->assertEquals('bar', $vars['foo']);
  380. $this->assertEquals('baz', $vars['bar']);
  381. $this->assertEquals(array('foo', 'bar'), $vars['baz']);
  382. }
  383. /**
  384. * Test set/getEncoding()
  385. */
  386. public function testSetGetEncoding()
  387. {
  388. $view = new Zend_View();
  389. $this->assertEquals('ISO-8859-1', $view->getEncoding());
  390. $view->setEncoding('UTF-8');
  391. $this->assertEquals('UTF-8', $view->getEncoding());
  392. }
  393. public function testEmptyPropertiesReturnAppropriately()
  394. {
  395. $view = new Zend_View();
  396. $view->foo = false;
  397. $view->bar = null;
  398. $view->baz = '';
  399. $this->assertTrue(empty($view->foo));
  400. $this->assertTrue(empty($view->bar));
  401. $this->assertTrue(empty($view->baz));
  402. }
  403. public function testFluentInterfaces()
  404. {
  405. $view = new Zend_View();
  406. try {
  407. $test = $view->setEscape('strip_tags')
  408. ->setFilter('htmlspecialchars')
  409. ->setEncoding('UTF-8')
  410. ->setScriptPath(dirname(__FILE__) . '/View/_templates')
  411. ->setHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir1')
  412. ->setFilterPath(dirname(__FILE__) . '/View/_stubs/HelperDir1')
  413. ->assign('foo', 'bar');
  414. } catch (Exception $e){
  415. $this->fail('Setters should not throw exceptions');
  416. }
  417. $this->assertTrue($test instanceof Zend_View);
  418. }
  419. public function testSetConfigInConstructor()
  420. {
  421. $scriptPath = $this->_filterPath(dirname(__FILE__) . '/View/_templates/');
  422. $helperPath = $this->_filterPath(dirname(__FILE__) . '/View/_stubs/HelperDir1/');
  423. $filterPath = $this->_filterPath(dirname(__FILE__) . '/View/_stubs/HelperDir1/');
  424. $config = array(
  425. 'escape' => 'strip_tags',
  426. 'encoding' => 'UTF-8',
  427. 'scriptPath' => $scriptPath,
  428. 'helperPath' => $helperPath,
  429. 'helperPathPrefix' => 'My_View_Helper',
  430. 'filterPath' => $filterPath,
  431. 'filterPathPrefix' => 'My_View_Filter',
  432. 'filter' => 'urlencode',
  433. );
  434. $view = new Zend_View($config);
  435. $scriptPaths = $view->getScriptPaths();
  436. $helperPaths = $view->getHelperPaths();
  437. $filterPaths = $view->getFilterPaths();
  438. $this->assertContains($this->_filterPath($scriptPath), $this->_filterPath($scriptPaths));
  439. $found = false;
  440. $prefix = false;
  441. foreach ($helperPaths as $helperPrefix => $paths) {
  442. foreach ($paths as $path) {
  443. $path = $this->_filterPath($path);
  444. if (strstr($path, $helperPath)) {
  445. $found = true;
  446. $prefix = $helperPrefix;
  447. }
  448. }
  449. }
  450. $this->assertTrue($found, var_export($helperPaths, 1));
  451. $this->assertEquals('My_View_Helper_', $prefix);
  452. $found = false;
  453. $prefix = false;
  454. foreach ($filterPaths as $classPrefix => $paths) {
  455. foreach ($paths as $pathInfo) {
  456. $path = $this->_filterPath($pathInfo);
  457. if (strstr($pathInfo, $filterPath)) {
  458. $found = true;
  459. $prefix = $classPrefix;
  460. }
  461. }
  462. }
  463. $this->assertTrue($found, var_export($filterPaths, 1));
  464. $this->assertEquals('My_View_Filter_', $prefix);
  465. }
  466. public function testUnset()
  467. {
  468. $view = new Zend_View();
  469. unset($view->_path);
  470. // @todo assert something?
  471. }
  472. public function testSetProtectedThrowsException()
  473. {
  474. $view = new Zend_View();
  475. try {
  476. $view->_path = 'bar';
  477. $this->fail('Should not be able to set protected properties');
  478. } catch (Exception $e) {
  479. // success
  480. // @todo assert something?
  481. }
  482. }
  483. public function testHelperPathWithPrefix()
  484. {
  485. $view = new Zend_View();
  486. $status = $view->addHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir1/', 'My_View_Helper');
  487. $this->assertSame($view, $status);
  488. $helperPaths = $view->getHelperPaths();
  489. $this->assertTrue(array_key_exists('My_View_Helper_', $helperPaths));
  490. $path = $this->_filterPath(current($helperPaths['My_View_Helper_']));
  491. $this->assertEquals($this->_filterPath(dirname(__FILE__) . '/View/_stubs/HelperDir1/'), $path);
  492. $view->setHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir2/', 'Other_View_Helper');
  493. $helperPaths = $view->getHelperPaths();
  494. $this->assertTrue(array_key_exists('Other_View_Helper_', $helperPaths));
  495. $path = $this->_filterPath(current($helperPaths['Other_View_Helper_']));
  496. $this->assertEquals($this->_filterPath(dirname(__FILE__) . '/View/_stubs/HelperDir2/'), $path);
  497. }
  498. public function testHelperPathWithPrefixAndRelativePath()
  499. {
  500. $view = new Zend_View();
  501. $status = $view->addHelperPath('Zend/View/_stubs/HelperDir1/', 'My_View_Helper');
  502. $this->assertSame($view, $status);
  503. $helperPaths = $view->getHelperPaths();
  504. $this->assertTrue(array_key_exists('My_View_Helper_', $helperPaths));
  505. $this->assertContains($this->_filterPath('Zend/View/_stubs/HelperDir1/'), $this->_filterPath(current($helperPaths['My_View_Helper_'])));
  506. }
  507. public function testFilterPathWithPrefix()
  508. {
  509. $view = new Zend_View();
  510. $status = $view->addFilterPath(dirname(__FILE__) . '/View/_stubs/HelperDir1/', 'My_View_Filter');
  511. $this->assertSame($view, $status);
  512. $filterPaths = $view->getFilterPaths();
  513. $this->assertTrue(array_key_exists('My_View_Filter_', $filterPaths));
  514. $this->assertEquals($this->_filterPath(dirname(__FILE__) . '/View/_stubs/HelperDir1/'), $this->_filterPath(current($filterPaths['My_View_Filter_'])));
  515. $view->setFilterPath(dirname(__FILE__) . '/View/_stubs/HelperDir2/', 'Other_View_Filter');
  516. $filterPaths = $view->getFilterPaths();
  517. $this->assertTrue(array_key_exists('Other_View_Filter_', $filterPaths));
  518. $this->assertEquals($this->_filterPath(dirname(__FILE__) . '/View/_stubs/HelperDir2/'), $this->_filterPath(current($filterPaths['Other_View_Filter_'])));
  519. }
  520. public function testAssignThrowsExceptionsOnBadValues()
  521. {
  522. $view = new Zend_View();
  523. try {
  524. $view->assign('_path', dirname(__FILE__) . '/View/_stubs/HelperDir2/');
  525. $this->fail('Protected/private properties cannot be assigned');
  526. } catch (Exception $e) {
  527. // success
  528. // @todo assert something?
  529. }
  530. try {
  531. $view->assign(array('_path' => dirname(__FILE__) . '/View/_stubs/HelperDir2/'));
  532. $this->fail('Protected/private properties cannot be assigned');
  533. } catch (Exception $e) {
  534. // success
  535. // @todo assert something?
  536. }
  537. try {
  538. $view->assign($this);
  539. $this->fail('Assign spec requires string or array');
  540. } catch (Exception $e) {
  541. // success
  542. // @todo assert something?
  543. }
  544. }
  545. public function testEscape()
  546. {
  547. $view = new Zend_View();
  548. $original = "Me, Myself, & I";
  549. $escaped = $view->escape($original);
  550. $this->assertNotEquals($original, $escaped);
  551. $this->assertEquals("Me, Myself, &amp; I", $escaped);
  552. }
  553. public function testCustomEscape()
  554. {
  555. $view = new Zend_View();
  556. $view->setEscape('strip_tags');
  557. $original = "<p>Some text</p>";
  558. $escaped = $view->escape($original);
  559. $this->assertNotEquals($original, $escaped);
  560. $this->assertEquals("Some text", $escaped);
  561. }
  562. public function testZf995UndefinedPropertiesReturnNull()
  563. {
  564. error_reporting(E_ALL | E_STRICT);
  565. ini_set('display_errors', true);
  566. $view = new Zend_View();
  567. $view->setScriptPath(dirname(__FILE__) . '/View/_templates');
  568. ob_start();
  569. echo $view->render('testZf995.phtml');
  570. $content = ob_get_flush();
  571. $this->assertTrue(empty($content));
  572. }
  573. public function testInit()
  574. {
  575. $view = new Zend_ViewTest_Extension();
  576. $this->assertEquals('bar', $view->foo);
  577. $paths = $view->getScriptPaths();
  578. $this->assertEquals(1, count($paths));
  579. $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR . '_templates' . DIRECTORY_SEPARATOR, $paths[0]);
  580. }
  581. public function testHelperViewAccessor()
  582. {
  583. $view = new Zend_View();
  584. $view->addHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir2/');
  585. $view->stub2();
  586. $helpers = $view->getHelpers();
  587. $this->assertEquals(1, count($helpers));
  588. $this->assertTrue(isset($helpers['Stub2']));
  589. $stub2 = $helpers['Stub2'];
  590. $this->assertTrue($stub2 instanceof Zend_View_Helper_Stub2);
  591. $this->assertTrue(isset($stub2->view));
  592. $this->assertSame($view, $stub2->view);
  593. }
  594. public function testSetBasePath()
  595. {
  596. $view = new Zend_View();
  597. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View';
  598. $view->setBasePath($base);
  599. $this->_testBasePath($view, $base);
  600. }
  601. public function testAddBasePath()
  602. {
  603. $view = new Zend_View();
  604. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View';
  605. $view->addBasePath($base);
  606. $this->_testBasePath($view, $base);
  607. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View2';
  608. $view->addBasePath($base);
  609. $this->_testBasePath($view, $base);
  610. }
  611. public function testAddBasePathWithClassPrefix()
  612. {
  613. $view = new Zend_View();
  614. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View';
  615. $view->addBasePath($base, 'My_Foo');
  616. $this->_testBasePath($view, $base, 'My_Foo');
  617. }
  618. public function testSetBasePathFromConstructor()
  619. {
  620. $base = dirname(__FILE__) . '/View';
  621. $view = new Zend_View(array('basePath' => $base));
  622. $this->_testBasePath($view, $base);
  623. }
  624. public function testSetBasePathWithClassPrefix()
  625. {
  626. $view = new Zend_View();
  627. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View';
  628. $view->setBasePath($base, 'My_Foo');
  629. $this->_testBasePath($view, $base, 'My_Foo');
  630. }
  631. public function testSetBasePathFromConstructorWithClassPrefix()
  632. {
  633. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View';
  634. $view = new Zend_View(array('basePath' => $base, 'basePathPrefix' => 'My_Foo'));
  635. $this->_testBasePath($view, $base);
  636. }
  637. protected function _filterPath($path)
  638. {
  639. if (is_array($path)) {
  640. foreach ($path as $k => $p) {
  641. $path[$k] = $this->_filterPath($p);
  642. }
  643. return $path;
  644. }
  645. $path = str_replace(DIRECTORY_SEPARATOR, '/', $path);
  646. $path = str_replace('//', '/', $path);
  647. $path = rtrim($path, '/');
  648. return $path;
  649. }
  650. protected function _testBasePath(Zend_View $view, $base, $classPrefix = null)
  651. {
  652. $base = $this->_filterPath($base);
  653. $scriptPaths = $this->_filterPath($view->getScriptPaths());
  654. $helperPaths = $this->_filterPath($view->getHelperPaths());
  655. $filterPaths = $this->_filterPath($view->getFilterPaths());
  656. $this->assertContains($base . '/scripts', $scriptPaths);
  657. $found = false;
  658. $prefix = false;
  659. foreach ($helperPaths as $pathPrefix => $paths) {
  660. foreach ($paths as $path) {
  661. $path = $this->_filterPath($path);
  662. if ($path == $base . '/helpers') {
  663. $found = true;
  664. $prefix = $pathPrefix;
  665. break;
  666. }
  667. }
  668. }
  669. $this->assertTrue($found, var_export($helperPaths, 1));
  670. if (null !== $classPrefix) {
  671. $this->assertTrue($prefix !== false);
  672. $this->assertEquals($classPrefix . '_Helper_', $prefix);
  673. }
  674. $found = false;
  675. $prefix = false;
  676. foreach ($filterPaths as $pathPrefix => $paths) {
  677. foreach ($paths as $path) {
  678. $path = $this->_filterPath($path);
  679. if ($path == $base . '/filters') {
  680. $found = true;
  681. $prefix = $pathPrefix;
  682. break;
  683. }
  684. }
  685. }
  686. $this->assertTrue($found, var_export($filterPaths, 1));
  687. if (null !== $classPrefix) {
  688. $this->assertTrue($prefix !== false);
  689. $this->assertEquals($classPrefix . '_Filter_', $prefix);
  690. }
  691. }
  692. public function handleNotices($errno, $errstr, $errfile, $errline)
  693. {
  694. if (!isset($this->notices)) {
  695. $this->notices = array();
  696. }
  697. if ($errno === E_USER_NOTICE) {
  698. $this->notices[] = $errstr;
  699. }
  700. }
  701. public function testStrictVars()
  702. {
  703. $view = new Zend_View();
  704. $view->setScriptPath(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR . '_templates');
  705. $view->strictVars(true);
  706. set_error_handler(array($this, 'handleNotices'), E_USER_NOTICE);
  707. $content = $view->render('testStrictVars.phtml');
  708. restore_error_handler();
  709. foreach (array('foo', 'bar') as $key) {
  710. $this->assertContains('Key "' . $key . '" does not exist', $this->notices);
  711. }
  712. }
  713. public function testGetScriptPath()
  714. {
  715. $view = new Zend_View();
  716. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR . '_templates';
  717. $view->setScriptPath($base);
  718. $path = $view->getScriptPath('test.phtml');
  719. $this->assertEquals($base . DIRECTORY_SEPARATOR . 'test.phtml', $path);
  720. }
  721. public function testGetHelper()
  722. {
  723. // require so we can do type hinting
  724. require_once 'Zend/View/Helper/DeclareVars.php';
  725. $view = new Zend_View();
  726. $view->declareVars();
  727. $helper = $view->getHelper('declareVars');
  728. $this->assertTrue($helper instanceof Zend_View_Helper_DeclareVars);
  729. }
  730. public function testGetHelperPath()
  731. {
  732. require_once 'Zend/View/Helper/DeclareVars.php';
  733. $reflection = new ReflectionClass('Zend_View_Helper_DeclareVars');
  734. $expected = $reflection->getFileName();
  735. $view = new Zend_View();
  736. $view->declareVars();
  737. $helperPath = $view->getHelperPath('declareVars');
  738. $this->assertContains($expected, $helperPath);
  739. }
  740. public function testGetFilter()
  741. {
  742. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR;
  743. require_once $base . '_stubs' . DIRECTORY_SEPARATOR . 'FilterDir1' . DIRECTORY_SEPARATOR . 'Foo.php';
  744. $view = new Zend_View();
  745. $view->setScriptPath($base . '_templates');
  746. $view->addFilterPath($base . '_stubs' . DIRECTORY_SEPARATOR . 'FilterDir1');
  747. $filter = $view->getFilter('foo');
  748. $this->assertTrue($filter instanceof Zend_View_Filter_Foo);
  749. }
  750. public function testGetFilterPath()
  751. {
  752. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR;
  753. $expected = $base . '_stubs' . DIRECTORY_SEPARATOR . 'FilterDir1' . DIRECTORY_SEPARATOR . 'Foo.php';
  754. $view = new Zend_View();
  755. $view->setScriptPath($base . '_templates');
  756. $view->addFilterPath($base . '_stubs' . DIRECTORY_SEPARATOR . 'FilterDir1');
  757. $filterPath = $view->getFilterPath('foo');
  758. $this->assertEquals($expected, $filterPath, var_export($filterPath, 1));
  759. }
  760. public function testGetFilters()
  761. {
  762. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR;
  763. $view = new Zend_View();
  764. $view->setScriptPath($base . '_templates');
  765. $view->addFilterPath($base . '_stubs' . DIRECTORY_SEPARATOR . 'FilterDir1');
  766. $view->addFilter('foo');
  767. $filters = $view->getFilters();
  768. $this->assertEquals(1, count($filters));
  769. $this->assertEquals('foo', $filters[0]);
  770. }
  771. public function testMissingViewScriptExceptionText()
  772. {
  773. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR;
  774. $view = new Zend_View();
  775. $view->setScriptPath($base . '_templates');
  776. try {
  777. $view->render('bazbatNotExists.php.tpl');
  778. $this->fail('Non-existent view script should cause an exception');
  779. } catch (Exception $e) {
  780. $this->assertContains($base. '_templates', $e->getMessage());
  781. }
  782. }
  783. public function testGetHelperIsCaseInsensitive()
  784. {
  785. $view = new Zend_View();
  786. $hidden = $view->formHidden('foo', 'bar');
  787. $this->assertContains('<input type="hidden"', $hidden);
  788. $hidden = $view->getHelper('formHidden')->formHidden('foo', 'bar');
  789. $this->assertContains('<input type="hidden"', $hidden);
  790. $hidden = $view->getHelper('FormHidden')->formHidden('foo', 'bar');
  791. $this->assertContains('<input type="hidden"', $hidden);
  792. }
  793. public function testGetHelperUsingDifferentCasesReturnsSameInstance()
  794. {
  795. $view = new Zend_View();
  796. $helper1 = $view->getHelper('formHidden');
  797. $helper2 = $view->getHelper('FormHidden');
  798. $this->assertSame($helper1, $helper2);
  799. }
  800. /**
  801. * @issue ZF-2742
  802. */
  803. public function testGetHelperWorksWithPredefinedClassNames()
  804. {
  805. $view = new Zend_View();
  806. $view->setHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir2');
  807. try {
  808. $view->setHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir1', null);
  809. $this->fail('Exception for empty prefix was expected.');
  810. } catch (Exception $e) {
  811. $this->assertContains('only takes strings', $e->getMessage());
  812. }
  813. try {
  814. $view->setHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir1', null);
  815. $this->fail('Exception for empty prefix was expected.');
  816. } catch (Exception $e) {
  817. $this->assertContains('only takes strings', $e->getMessage());
  818. }
  819. try {
  820. $helper = $view->getHelper('Datetime');
  821. } catch (Exception $e) {
  822. $this->assertContains('not found', $e->getMessage());
  823. }
  824. }
  825. public function testUseStreamWrapperFlagShouldDefaultToFalse()
  826. {
  827. $this->view = new Zend_View();
  828. $this->assertFalse($this->view->useStreamWrapper());
  829. }
  830. public function testUseStreamWrapperStateShouldBeConfigurable()
  831. {
  832. $this->testUseStreamWrapperFlagShouldDefaultToFalse();
  833. $this->view->setUseStreamWrapper(true);
  834. $this->assertTrue($this->view->useStreamWrapper());
  835. $this->view->setUseStreamWrapper(false);
  836. $this->assertFalse($this->view->useStreamWrapper());
  837. }
  838. /**
  839. * @group ZF-5748
  840. */
  841. public function testRenderShouldNotAllowScriptPathsContainingParentDirectoryTraversal()
  842. {
  843. $view = new Zend_View();
  844. try {
  845. $view->render('../foobar.html');
  846. $this->fail('Should not allow parent directory traversal');
  847. } catch (Zend_View_Exception $e) {
  848. $this->assertContains('parent directory traversal', $e->getMessage());
  849. }
  850. try {
  851. $view->render('foo/../foobar.html');
  852. $this->fail('Should not allow parent directory traversal');
  853. } catch (Zend_View_Exception $e) {
  854. $this->assertContains('parent directory traversal', $e->getMessage());
  855. }
  856. try {
  857. $view->render('foo/..\foobar.html');
  858. $this->fail('Should not allow parent directory traversal');
  859. } catch (Zend_View_Exception $e) {
  860. $this->assertContains('parent directory traversal', $e->getMessage());
  861. }
  862. }
  863. /**
  864. * @group ZF-5748
  865. */
  866. public function testLfiProtectionFlagShouldBeEnabledByDefault()
  867. {
  868. $view = new Zend_View();
  869. $this->assertTrue($view->isLfiProtectionOn());
  870. }
  871. /**
  872. * @group ZF-5748
  873. */
  874. public function testLfiProtectionFlagMayBeDisabledViaConstructorOption()
  875. {
  876. $view = new Zend_View(array('lfiProtectionOn' => false));
  877. $this->assertFalse($view->isLfiProtectionOn());
  878. }
  879. /**
  880. * @group ZF-5748
  881. */
  882. public function testLfiProtectionFlagMayBeDisabledViaMethodCall()
  883. {
  884. $view = new Zend_View();
  885. $view->setLfiProtection(false);
  886. $this->assertFalse($view->isLfiProtectionOn());
  887. }
  888. /**
  889. * @group ZF-5748
  890. */
  891. public function testDisablingLfiProtectionAllowsParentDirectoryTraversal()
  892. {
  893. $view = new Zend_View(array(
  894. 'lfiProtectionOn' => false,
  895. 'scriptPath' => dirname(__FILE__) . '/View/_templates/',
  896. ));
  897. try {
  898. $test = $view->render('../_stubs/scripts/LfiProtectionCheck.phtml');
  899. $this->assertContains('LFI', $test);
  900. } catch (Zend_View_Exception $e) {
  901. $this->fail('LFI attack failed: ' . $e->getMessage());
  902. }
  903. }
  904. /**
  905. * @group ZF-6087
  906. */
  907. public function testConstructorShouldAllowPassingArrayOfHelperPaths()
  908. {
  909. $view = new Zend_View(array(
  910. 'helperPath' => array(
  911. 'My_View' => 'My/View/',
  912. ),
  913. ));
  914. $paths = $view->getHelperPaths();
  915. $this->assertTrue(array_key_exists('My_View_', $paths), var_export($paths, 1));
  916. }
  917. /**
  918. * @group ZF-6087
  919. */
  920. public function testConstructorShouldAllowPassingArrayOfFilterPaths()
  921. {
  922. $view = new Zend_View(array(
  923. 'filterPath' => array(
  924. 'My_View' => 'My/View/',
  925. ),
  926. ));
  927. $paths = $view->getFilterPaths();
  928. $this->assertTrue(array_key_exists('My_View_', $paths), var_export($paths, 1));
  929. }
  930. }
  931. /**
  932. * @category Zend
  933. * @package Zend_View
  934. * @subpackage UnitTests
  935. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  936. * @license http://framework.zend.com/license/new-bsd New BSD License
  937. */
  938. class Zend_ViewTest_Extension extends Zend_View
  939. {
  940. public function init()
  941. {
  942. $this->assign('foo', 'bar');
  943. $this->setScriptPath(dirname(__FILE__) . '/View/_templates');
  944. }
  945. }
  946. // Call Zend_ViewTest::main() if this source file is executed directly.
  947. if (PHPUnit_MAIN_METHOD == "Zend_ViewTest::main") {
  948. Zend_ViewTest::main();
  949. }