2
0

ViewTest.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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. ob_end_clean();
  572. $this->assertTrue(empty($content));
  573. }
  574. public function testInit()
  575. {
  576. $view = new Zend_ViewTest_Extension();
  577. $this->assertEquals('bar', $view->foo);
  578. $paths = $view->getScriptPaths();
  579. $this->assertEquals(1, count($paths));
  580. $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR . '_templates' . DIRECTORY_SEPARATOR, $paths[0]);
  581. }
  582. public function testHelperViewAccessor()
  583. {
  584. $view = new Zend_View();
  585. $view->addHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir2/');
  586. $view->stub2();
  587. $helpers = $view->getHelpers();
  588. $this->assertEquals(1, count($helpers));
  589. $this->assertTrue(isset($helpers['Stub2']));
  590. $stub2 = $helpers['Stub2'];
  591. $this->assertTrue($stub2 instanceof Zend_View_Helper_Stub2);
  592. $this->assertTrue(isset($stub2->view));
  593. $this->assertSame($view, $stub2->view);
  594. }
  595. public function testSetBasePath()
  596. {
  597. $view = new Zend_View();
  598. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View';
  599. $view->setBasePath($base);
  600. $this->_testBasePath($view, $base);
  601. }
  602. public function testAddBasePath()
  603. {
  604. $view = new Zend_View();
  605. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View';
  606. $view->addBasePath($base);
  607. $this->_testBasePath($view, $base);
  608. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View2';
  609. $view->addBasePath($base);
  610. $this->_testBasePath($view, $base);
  611. }
  612. public function testAddBasePathWithClassPrefix()
  613. {
  614. $view = new Zend_View();
  615. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View';
  616. $view->addBasePath($base, 'My_Foo');
  617. $this->_testBasePath($view, $base, 'My_Foo');
  618. }
  619. public function testSetBasePathFromConstructor()
  620. {
  621. $base = dirname(__FILE__) . '/View';
  622. $view = new Zend_View(array('basePath' => $base));
  623. $this->_testBasePath($view, $base);
  624. }
  625. public function testSetBasePathWithClassPrefix()
  626. {
  627. $view = new Zend_View();
  628. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View';
  629. $view->setBasePath($base, 'My_Foo');
  630. $this->_testBasePath($view, $base, 'My_Foo');
  631. }
  632. public function testSetBasePathFromConstructorWithClassPrefix()
  633. {
  634. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View';
  635. $view = new Zend_View(array('basePath' => $base, 'basePathPrefix' => 'My_Foo'));
  636. $this->_testBasePath($view, $base);
  637. }
  638. protected function _filterPath($path)
  639. {
  640. if (is_array($path)) {
  641. foreach ($path as $k => $p) {
  642. $path[$k] = $this->_filterPath($p);
  643. }
  644. return $path;
  645. }
  646. $path = str_replace(DIRECTORY_SEPARATOR, '/', $path);
  647. $path = str_replace('//', '/', $path);
  648. $path = rtrim($path, '/');
  649. return $path;
  650. }
  651. protected function _testBasePath(Zend_View $view, $base, $classPrefix = null)
  652. {
  653. $base = $this->_filterPath($base);
  654. $scriptPaths = $this->_filterPath($view->getScriptPaths());
  655. $helperPaths = $this->_filterPath($view->getHelperPaths());
  656. $filterPaths = $this->_filterPath($view->getFilterPaths());
  657. $this->assertContains($base . '/scripts', $scriptPaths);
  658. $found = false;
  659. $prefix = false;
  660. foreach ($helperPaths as $pathPrefix => $paths) {
  661. foreach ($paths as $path) {
  662. $path = $this->_filterPath($path);
  663. if ($path == $base . '/helpers') {
  664. $found = true;
  665. $prefix = $pathPrefix;
  666. break;
  667. }
  668. }
  669. }
  670. $this->assertTrue($found, var_export($helperPaths, 1));
  671. if (null !== $classPrefix) {
  672. $this->assertTrue($prefix !== false);
  673. $this->assertEquals($classPrefix . '_Helper_', $prefix);
  674. }
  675. $found = false;
  676. $prefix = false;
  677. foreach ($filterPaths as $pathPrefix => $paths) {
  678. foreach ($paths as $path) {
  679. $path = $this->_filterPath($path);
  680. if ($path == $base . '/filters') {
  681. $found = true;
  682. $prefix = $pathPrefix;
  683. break;
  684. }
  685. }
  686. }
  687. $this->assertTrue($found, var_export($filterPaths, 1));
  688. if (null !== $classPrefix) {
  689. $this->assertTrue($prefix !== false);
  690. $this->assertEquals($classPrefix . '_Filter_', $prefix);
  691. }
  692. }
  693. public function handleNotices($errno, $errstr, $errfile, $errline)
  694. {
  695. if (!isset($this->notices)) {
  696. $this->notices = array();
  697. }
  698. if ($errno === E_USER_NOTICE) {
  699. $this->notices[] = $errstr;
  700. }
  701. }
  702. public function testStrictVars()
  703. {
  704. $view = new Zend_View();
  705. $view->setScriptPath(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR . '_templates');
  706. $view->strictVars(true);
  707. set_error_handler(array($this, 'handleNotices'), E_USER_NOTICE);
  708. $content = $view->render('testStrictVars.phtml');
  709. restore_error_handler();
  710. foreach (array('foo', 'bar') as $key) {
  711. $this->assertContains('Key "' . $key . '" does not exist', $this->notices);
  712. }
  713. }
  714. public function testGetScriptPath()
  715. {
  716. $view = new Zend_View();
  717. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR . '_templates';
  718. $view->setScriptPath($base);
  719. $path = $view->getScriptPath('test.phtml');
  720. $this->assertEquals($base . DIRECTORY_SEPARATOR . 'test.phtml', $path);
  721. }
  722. public function testGetHelper()
  723. {
  724. // require so we can do type hinting
  725. require_once 'Zend/View/Helper/DeclareVars.php';
  726. $view = new Zend_View();
  727. $view->declareVars();
  728. $helper = $view->getHelper('declareVars');
  729. $this->assertTrue($helper instanceof Zend_View_Helper_DeclareVars);
  730. }
  731. public function testGetHelperPath()
  732. {
  733. require_once 'Zend/View/Helper/DeclareVars.php';
  734. $reflection = new ReflectionClass('Zend_View_Helper_DeclareVars');
  735. $expected = $reflection->getFileName();
  736. $view = new Zend_View();
  737. $view->declareVars();
  738. $helperPath = $view->getHelperPath('declareVars');
  739. $this->assertContains($expected, $helperPath);
  740. }
  741. public function testGetFilter()
  742. {
  743. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR;
  744. require_once $base . '_stubs' . DIRECTORY_SEPARATOR . 'FilterDir1' . DIRECTORY_SEPARATOR . 'Foo.php';
  745. $view = new Zend_View();
  746. $view->setScriptPath($base . '_templates');
  747. $view->addFilterPath($base . '_stubs' . DIRECTORY_SEPARATOR . 'FilterDir1');
  748. $filter = $view->getFilter('foo');
  749. $this->assertTrue($filter instanceof Zend_View_Filter_Foo);
  750. }
  751. public function testGetFilterPath()
  752. {
  753. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR;
  754. $expected = $base . '_stubs' . DIRECTORY_SEPARATOR . 'FilterDir1' . DIRECTORY_SEPARATOR . 'Foo.php';
  755. $view = new Zend_View();
  756. $view->setScriptPath($base . '_templates');
  757. $view->addFilterPath($base . '_stubs' . DIRECTORY_SEPARATOR . 'FilterDir1');
  758. $filterPath = $view->getFilterPath('foo');
  759. $this->assertEquals($expected, $filterPath, var_export($filterPath, 1));
  760. }
  761. public function testGetFilters()
  762. {
  763. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR;
  764. $view = new Zend_View();
  765. $view->setScriptPath($base . '_templates');
  766. $view->addFilterPath($base . '_stubs' . DIRECTORY_SEPARATOR . 'FilterDir1');
  767. $view->addFilter('foo');
  768. $filters = $view->getFilters();
  769. $this->assertEquals(1, count($filters));
  770. $this->assertEquals('foo', $filters[0]);
  771. }
  772. public function testMissingViewScriptExceptionText()
  773. {
  774. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR;
  775. $view = new Zend_View();
  776. $view->setScriptPath($base . '_templates');
  777. try {
  778. $view->render('bazbatNotExists.php.tpl');
  779. $this->fail('Non-existent view script should cause an exception');
  780. } catch (Exception $e) {
  781. $this->assertContains($base. '_templates', $e->getMessage());
  782. }
  783. }
  784. public function testGetHelperIsCaseInsensitive()
  785. {
  786. $view = new Zend_View();
  787. $hidden = $view->formHidden('foo', 'bar');
  788. $this->assertContains('<input type="hidden"', $hidden);
  789. $hidden = $view->getHelper('formHidden')->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. }
  794. public function testGetHelperUsingDifferentCasesReturnsSameInstance()
  795. {
  796. $view = new Zend_View();
  797. $helper1 = $view->getHelper('formHidden');
  798. $helper2 = $view->getHelper('FormHidden');
  799. $this->assertSame($helper1, $helper2);
  800. }
  801. /**
  802. * @issue ZF-2742
  803. */
  804. public function testGetHelperWorksWithPredefinedClassNames()
  805. {
  806. $view = new Zend_View();
  807. $view->setHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir2');
  808. try {
  809. $view->setHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir1', null);
  810. $this->fail('Exception for empty prefix was expected.');
  811. } catch (Exception $e) {
  812. $this->assertContains('only takes strings', $e->getMessage());
  813. }
  814. try {
  815. $view->setHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir1', null);
  816. $this->fail('Exception for empty prefix was expected.');
  817. } catch (Exception $e) {
  818. $this->assertContains('only takes strings', $e->getMessage());
  819. }
  820. try {
  821. $helper = $view->getHelper('Datetime');
  822. } catch (Exception $e) {
  823. $this->assertContains('not found', $e->getMessage());
  824. }
  825. }
  826. public function testUseStreamWrapperFlagShouldDefaultToFalse()
  827. {
  828. $this->view = new Zend_View();
  829. $this->assertFalse($this->view->useStreamWrapper());
  830. }
  831. public function testUseStreamWrapperStateShouldBeConfigurable()
  832. {
  833. $this->testUseStreamWrapperFlagShouldDefaultToFalse();
  834. $this->view->setUseStreamWrapper(true);
  835. $this->assertTrue($this->view->useStreamWrapper());
  836. $this->view->setUseStreamWrapper(false);
  837. $this->assertFalse($this->view->useStreamWrapper());
  838. }
  839. /**
  840. * @group ZF-5748
  841. */
  842. public function testRenderShouldNotAllowScriptPathsContainingParentDirectoryTraversal()
  843. {
  844. $view = new Zend_View();
  845. try {
  846. $view->render('../foobar.html');
  847. $this->fail('Should not allow parent directory traversal');
  848. } catch (Zend_View_Exception $e) {
  849. $this->assertContains('parent directory traversal', $e->getMessage());
  850. }
  851. try {
  852. $view->render('foo/../foobar.html');
  853. $this->fail('Should not allow parent directory traversal');
  854. } catch (Zend_View_Exception $e) {
  855. $this->assertContains('parent directory traversal', $e->getMessage());
  856. }
  857. try {
  858. $view->render('foo/..\foobar.html');
  859. $this->fail('Should not allow parent directory traversal');
  860. } catch (Zend_View_Exception $e) {
  861. $this->assertContains('parent directory traversal', $e->getMessage());
  862. }
  863. }
  864. /**
  865. * @group ZF-5748
  866. */
  867. public function testLfiProtectionFlagShouldBeEnabledByDefault()
  868. {
  869. $view = new Zend_View();
  870. $this->assertTrue($view->isLfiProtectionOn());
  871. }
  872. /**
  873. * @group ZF-5748
  874. */
  875. public function testLfiProtectionFlagMayBeDisabledViaConstructorOption()
  876. {
  877. $view = new Zend_View(array('lfiProtectionOn' => false));
  878. $this->assertFalse($view->isLfiProtectionOn());
  879. }
  880. /**
  881. * @group ZF-5748
  882. */
  883. public function testLfiProtectionFlagMayBeDisabledViaMethodCall()
  884. {
  885. $view = new Zend_View();
  886. $view->setLfiProtection(false);
  887. $this->assertFalse($view->isLfiProtectionOn());
  888. }
  889. /**
  890. * @group ZF-5748
  891. */
  892. public function testDisablingLfiProtectionAllowsParentDirectoryTraversal()
  893. {
  894. $view = new Zend_View(array(
  895. 'lfiProtectionOn' => false,
  896. 'scriptPath' => dirname(__FILE__) . '/View/_templates/',
  897. ));
  898. try {
  899. $test = $view->render('../_stubs/scripts/LfiProtectionCheck.phtml');
  900. $this->assertContains('LFI', $test);
  901. } catch (Zend_View_Exception $e) {
  902. $this->fail('LFI attack failed: ' . $e->getMessage());
  903. }
  904. }
  905. /**
  906. * @group ZF-6087
  907. */
  908. public function testConstructorShouldAllowPassingArrayOfHelperPaths()
  909. {
  910. $view = new Zend_View(array(
  911. 'helperPath' => array(
  912. 'My_View' => 'My/View/',
  913. ),
  914. ));
  915. $paths = $view->getHelperPaths();
  916. $this->assertTrue(array_key_exists('My_View_', $paths), var_export($paths, 1));
  917. }
  918. /**
  919. * @group ZF-6087
  920. */
  921. public function testConstructorShouldAllowPassingArrayOfFilterPaths()
  922. {
  923. $view = new Zend_View(array(
  924. 'filterPath' => array(
  925. 'My_View' => 'My/View/',
  926. ),
  927. ));
  928. $paths = $view->getFilterPaths();
  929. $this->assertTrue(array_key_exists('My_View_', $paths), var_export($paths, 1));
  930. }
  931. }
  932. /**
  933. * @category Zend
  934. * @package Zend_View
  935. * @subpackage UnitTests
  936. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  937. * @license http://framework.zend.com/license/new-bsd New BSD License
  938. */
  939. class Zend_ViewTest_Extension extends Zend_View
  940. {
  941. public function init()
  942. {
  943. $this->assign('foo', 'bar');
  944. $this->setScriptPath(dirname(__FILE__) . '/View/_templates');
  945. }
  946. }
  947. // Call Zend_ViewTest::main() if this source file is executed directly.
  948. if (PHPUnit_MAIN_METHOD == "Zend_ViewTest::main") {
  949. Zend_ViewTest::main();
  950. }