2
0

ViewTest.php 35 KB

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