2
0

ViewTest.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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-2010 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-2010 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. * @group ZF-8715
  388. */
  389. public function testSetGetEncoding()
  390. {
  391. $view = new Zend_View();
  392. $this->assertEquals('UTF-8', $view->getEncoding());
  393. $view->setEncoding('ISO-8859-1');
  394. $this->assertEquals('ISO-8859-1', $view->getEncoding());
  395. }
  396. public function testEmptyPropertiesReturnAppropriately()
  397. {
  398. $view = new Zend_View();
  399. $view->foo = false;
  400. $view->bar = null;
  401. $view->baz = '';
  402. $this->assertTrue(empty($view->foo));
  403. $this->assertTrue(empty($view->bar));
  404. $this->assertTrue(empty($view->baz));
  405. }
  406. public function testFluentInterfaces()
  407. {
  408. $view = new Zend_View();
  409. try {
  410. $test = $view->setEscape('strip_tags')
  411. ->setFilter('htmlspecialchars')
  412. ->setEncoding('UTF-8')
  413. ->setScriptPath(dirname(__FILE__) . '/View/_templates')
  414. ->setHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir1')
  415. ->setFilterPath(dirname(__FILE__) . '/View/_stubs/HelperDir1')
  416. ->assign('foo', 'bar');
  417. } catch (Exception $e){
  418. $this->fail('Setters should not throw exceptions');
  419. }
  420. $this->assertTrue($test instanceof Zend_View);
  421. }
  422. public function testSetConfigInConstructor()
  423. {
  424. $scriptPath = $this->_filterPath(dirname(__FILE__) . '/View/_templates/');
  425. $helperPath = $this->_filterPath(dirname(__FILE__) . '/View/_stubs/HelperDir1/');
  426. $filterPath = $this->_filterPath(dirname(__FILE__) . '/View/_stubs/HelperDir1/');
  427. $config = array(
  428. 'escape' => 'strip_tags',
  429. 'encoding' => 'UTF-8',
  430. 'scriptPath' => $scriptPath,
  431. 'helperPath' => $helperPath,
  432. 'helperPathPrefix' => 'My_View_Helper',
  433. 'filterPath' => $filterPath,
  434. 'filterPathPrefix' => 'My_View_Filter',
  435. 'filter' => 'urlencode',
  436. );
  437. $view = new Zend_View($config);
  438. $scriptPaths = $view->getScriptPaths();
  439. $helperPaths = $view->getHelperPaths();
  440. $filterPaths = $view->getFilterPaths();
  441. $this->assertContains($this->_filterPath($scriptPath), $this->_filterPath($scriptPaths));
  442. $found = false;
  443. $prefix = false;
  444. foreach ($helperPaths as $helperPrefix => $paths) {
  445. foreach ($paths as $path) {
  446. $path = $this->_filterPath($path);
  447. if (strstr($path, $helperPath)) {
  448. $found = true;
  449. $prefix = $helperPrefix;
  450. }
  451. }
  452. }
  453. $this->assertTrue($found, var_export($helperPaths, 1));
  454. $this->assertEquals('My_View_Helper_', $prefix);
  455. $found = false;
  456. $prefix = false;
  457. foreach ($filterPaths as $classPrefix => $paths) {
  458. foreach ($paths as $pathInfo) {
  459. $path = $this->_filterPath($pathInfo);
  460. if (strstr($pathInfo, $filterPath)) {
  461. $found = true;
  462. $prefix = $classPrefix;
  463. }
  464. }
  465. }
  466. $this->assertTrue($found, var_export($filterPaths, 1));
  467. $this->assertEquals('My_View_Filter_', $prefix);
  468. }
  469. public function testUnset()
  470. {
  471. $view = new Zend_View();
  472. unset($view->_path);
  473. // @todo assert something?
  474. }
  475. public function testSetProtectedThrowsException()
  476. {
  477. $view = new Zend_View();
  478. try {
  479. $view->_path = 'bar';
  480. $this->fail('Should not be able to set protected properties');
  481. } catch (Exception $e) {
  482. // success
  483. // @todo assert something?
  484. }
  485. }
  486. public function testHelperPathWithPrefix()
  487. {
  488. $view = new Zend_View();
  489. $status = $view->addHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir1/', 'My_View_Helper');
  490. $this->assertSame($view, $status);
  491. $helperPaths = $view->getHelperPaths();
  492. $this->assertTrue(array_key_exists('My_View_Helper_', $helperPaths));
  493. $path = $this->_filterPath(current($helperPaths['My_View_Helper_']));
  494. $this->assertEquals($this->_filterPath(dirname(__FILE__) . '/View/_stubs/HelperDir1/'), $path);
  495. $view->setHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir2/', 'Other_View_Helper');
  496. $helperPaths = $view->getHelperPaths();
  497. $this->assertTrue(array_key_exists('Other_View_Helper_', $helperPaths));
  498. $path = $this->_filterPath(current($helperPaths['Other_View_Helper_']));
  499. $this->assertEquals($this->_filterPath(dirname(__FILE__) . '/View/_stubs/HelperDir2/'), $path);
  500. }
  501. public function testHelperPathWithPrefixAndRelativePath()
  502. {
  503. $view = new Zend_View();
  504. $status = $view->addHelperPath('Zend/View/_stubs/HelperDir1/', 'My_View_Helper');
  505. $this->assertSame($view, $status);
  506. $helperPaths = $view->getHelperPaths();
  507. $this->assertTrue(array_key_exists('My_View_Helper_', $helperPaths));
  508. $this->assertContains($this->_filterPath('Zend/View/_stubs/HelperDir1/'), $this->_filterPath(current($helperPaths['My_View_Helper_'])));
  509. }
  510. public function testFilterPathWithPrefix()
  511. {
  512. $view = new Zend_View();
  513. $status = $view->addFilterPath(dirname(__FILE__) . '/View/_stubs/HelperDir1/', 'My_View_Filter');
  514. $this->assertSame($view, $status);
  515. $filterPaths = $view->getFilterPaths();
  516. $this->assertTrue(array_key_exists('My_View_Filter_', $filterPaths));
  517. $this->assertEquals($this->_filterPath(dirname(__FILE__) . '/View/_stubs/HelperDir1/'), $this->_filterPath(current($filterPaths['My_View_Filter_'])));
  518. $view->setFilterPath(dirname(__FILE__) . '/View/_stubs/HelperDir2/', 'Other_View_Filter');
  519. $filterPaths = $view->getFilterPaths();
  520. $this->assertTrue(array_key_exists('Other_View_Filter_', $filterPaths));
  521. $this->assertEquals($this->_filterPath(dirname(__FILE__) . '/View/_stubs/HelperDir2/'), $this->_filterPath(current($filterPaths['Other_View_Filter_'])));
  522. }
  523. public function testAssignThrowsExceptionsOnBadValues()
  524. {
  525. $view = new Zend_View();
  526. try {
  527. $view->assign('_path', dirname(__FILE__) . '/View/_stubs/HelperDir2/');
  528. $this->fail('Protected/private properties cannot be assigned');
  529. } catch (Exception $e) {
  530. // success
  531. // @todo assert something?
  532. }
  533. try {
  534. $view->assign(array('_path' => dirname(__FILE__) . '/View/_stubs/HelperDir2/'));
  535. $this->fail('Protected/private properties cannot be assigned');
  536. } catch (Exception $e) {
  537. // success
  538. // @todo assert something?
  539. }
  540. try {
  541. $view->assign($this);
  542. $this->fail('Assign spec requires string or array');
  543. } catch (Exception $e) {
  544. // success
  545. // @todo assert something?
  546. }
  547. }
  548. public function testEscape()
  549. {
  550. $view = new Zend_View();
  551. $original = "Me, Myself, & I";
  552. $escaped = $view->escape($original);
  553. $this->assertNotEquals($original, $escaped);
  554. $this->assertEquals("Me, Myself, &amp; I", $escaped);
  555. }
  556. public function testCustomEscape()
  557. {
  558. $view = new Zend_View();
  559. $view->setEscape('strip_tags');
  560. $original = "<p>Some text</p>";
  561. $escaped = $view->escape($original);
  562. $this->assertNotEquals($original, $escaped);
  563. $this->assertEquals("Some text", $escaped);
  564. }
  565. public function testZf995UndefinedPropertiesReturnNull()
  566. {
  567. error_reporting(E_ALL | E_STRICT);
  568. ini_set('display_errors', true);
  569. $view = new Zend_View();
  570. $view->setScriptPath(dirname(__FILE__) . '/View/_templates');
  571. ob_start();
  572. echo $view->render('testZf995.phtml');
  573. $content = ob_get_flush();
  574. ob_end_clean();
  575. $this->assertTrue(empty($content));
  576. }
  577. public function testInit()
  578. {
  579. $view = new Zend_ViewTest_Extension();
  580. $this->assertEquals('bar', $view->foo);
  581. $paths = $view->getScriptPaths();
  582. $this->assertEquals(1, count($paths));
  583. $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR . '_templates' . DIRECTORY_SEPARATOR, $paths[0]);
  584. }
  585. public function testHelperViewAccessor()
  586. {
  587. $view = new Zend_View();
  588. $view->addHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir2/');
  589. $view->stub2();
  590. $helpers = $view->getHelpers();
  591. $this->assertEquals(1, count($helpers));
  592. $this->assertTrue(isset($helpers['Stub2']));
  593. $stub2 = $helpers['Stub2'];
  594. $this->assertTrue($stub2 instanceof Zend_View_Helper_Stub2);
  595. $this->assertTrue(isset($stub2->view));
  596. $this->assertSame($view, $stub2->view);
  597. }
  598. public function testSetBasePath()
  599. {
  600. $view = new Zend_View();
  601. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View';
  602. $view->setBasePath($base);
  603. $this->_testBasePath($view, $base);
  604. }
  605. public function testAddBasePath()
  606. {
  607. $view = new Zend_View();
  608. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View';
  609. $view->addBasePath($base);
  610. $this->_testBasePath($view, $base);
  611. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View2';
  612. $view->addBasePath($base);
  613. $this->_testBasePath($view, $base);
  614. }
  615. public function testAddBasePathWithClassPrefix()
  616. {
  617. $view = new Zend_View();
  618. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View';
  619. $view->addBasePath($base, 'My_Foo');
  620. $this->_testBasePath($view, $base, 'My_Foo');
  621. }
  622. public function testSetBasePathFromConstructor()
  623. {
  624. $base = dirname(__FILE__) . '/View';
  625. $view = new Zend_View(array('basePath' => $base));
  626. $this->_testBasePath($view, $base);
  627. }
  628. public function testSetBasePathWithClassPrefix()
  629. {
  630. $view = new Zend_View();
  631. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View';
  632. $view->setBasePath($base, 'My_Foo');
  633. $this->_testBasePath($view, $base, 'My_Foo');
  634. }
  635. public function testSetBasePathFromConstructorWithClassPrefix()
  636. {
  637. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View';
  638. $view = new Zend_View(array('basePath' => $base, 'basePathPrefix' => 'My_Foo'));
  639. $this->_testBasePath($view, $base);
  640. }
  641. protected function _filterPath($path)
  642. {
  643. if (is_array($path)) {
  644. foreach ($path as $k => $p) {
  645. $path[$k] = $this->_filterPath($p);
  646. }
  647. return $path;
  648. }
  649. $path = str_replace(DIRECTORY_SEPARATOR, '/', $path);
  650. $path = str_replace('//', '/', $path);
  651. $path = rtrim($path, '/');
  652. return $path;
  653. }
  654. protected function _testBasePath(Zend_View $view, $base, $classPrefix = null)
  655. {
  656. $base = $this->_filterPath($base);
  657. $scriptPaths = $this->_filterPath($view->getScriptPaths());
  658. $helperPaths = $this->_filterPath($view->getHelperPaths());
  659. $filterPaths = $this->_filterPath($view->getFilterPaths());
  660. $this->assertContains($base . '/scripts', $scriptPaths);
  661. $found = false;
  662. $prefix = false;
  663. foreach ($helperPaths as $pathPrefix => $paths) {
  664. foreach ($paths as $path) {
  665. $path = $this->_filterPath($path);
  666. if ($path == $base . '/helpers') {
  667. $found = true;
  668. $prefix = $pathPrefix;
  669. break;
  670. }
  671. }
  672. }
  673. $this->assertTrue($found, var_export($helperPaths, 1));
  674. if (null !== $classPrefix) {
  675. $this->assertTrue($prefix !== false);
  676. $this->assertEquals($classPrefix . '_Helper_', $prefix);
  677. }
  678. $found = false;
  679. $prefix = false;
  680. foreach ($filterPaths as $pathPrefix => $paths) {
  681. foreach ($paths as $path) {
  682. $path = $this->_filterPath($path);
  683. if ($path == $base . '/filters') {
  684. $found = true;
  685. $prefix = $pathPrefix;
  686. break;
  687. }
  688. }
  689. }
  690. $this->assertTrue($found, var_export($filterPaths, 1));
  691. if (null !== $classPrefix) {
  692. $this->assertTrue($prefix !== false);
  693. $this->assertEquals($classPrefix . '_Filter_', $prefix);
  694. }
  695. }
  696. public function handleNotices($errno, $errstr, $errfile, $errline)
  697. {
  698. if (!isset($this->notices)) {
  699. $this->notices = array();
  700. }
  701. if ($errno === E_USER_NOTICE) {
  702. $this->notices[] = $errstr;
  703. }
  704. }
  705. public function testStrictVars()
  706. {
  707. $view = new Zend_View();
  708. $view->setScriptPath(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR . '_templates');
  709. $view->strictVars(true);
  710. set_error_handler(array($this, 'handleNotices'), E_USER_NOTICE);
  711. $content = $view->render('testStrictVars.phtml');
  712. restore_error_handler();
  713. foreach (array('foo', 'bar') as $key) {
  714. $this->assertContains('Key "' . $key . '" does not exist', $this->notices);
  715. }
  716. }
  717. public function testGetScriptPath()
  718. {
  719. $view = new Zend_View();
  720. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR . '_templates';
  721. $view->setScriptPath($base);
  722. $path = $view->getScriptPath('test.phtml');
  723. $this->assertEquals($base . DIRECTORY_SEPARATOR . 'test.phtml', $path);
  724. }
  725. public function testGetHelper()
  726. {
  727. // require so we can do type hinting
  728. require_once 'Zend/View/Helper/DeclareVars.php';
  729. $view = new Zend_View();
  730. $view->declareVars();
  731. $helper = $view->getHelper('declareVars');
  732. $this->assertTrue($helper instanceof Zend_View_Helper_DeclareVars);
  733. }
  734. public function testGetHelperPath()
  735. {
  736. require_once 'Zend/View/Helper/DeclareVars.php';
  737. $reflection = new ReflectionClass('Zend_View_Helper_DeclareVars');
  738. $expected = $reflection->getFileName();
  739. $view = new Zend_View();
  740. $view->declareVars();
  741. $helperPath = $view->getHelperPath('declareVars');
  742. $this->assertContains($expected, $helperPath);
  743. }
  744. public function testGetFilter()
  745. {
  746. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR;
  747. require_once $base . '_stubs' . DIRECTORY_SEPARATOR . 'FilterDir1' . DIRECTORY_SEPARATOR . 'Foo.php';
  748. $view = new Zend_View();
  749. $view->setScriptPath($base . '_templates');
  750. $view->addFilterPath($base . '_stubs' . DIRECTORY_SEPARATOR . 'FilterDir1');
  751. $filter = $view->getFilter('foo');
  752. $this->assertTrue($filter instanceof Zend_View_Filter_Foo);
  753. }
  754. public function testGetFilterPath()
  755. {
  756. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR;
  757. $expected = $base . '_stubs' . DIRECTORY_SEPARATOR . 'FilterDir1' . DIRECTORY_SEPARATOR . 'Foo.php';
  758. $view = new Zend_View();
  759. $view->setScriptPath($base . '_templates');
  760. $view->addFilterPath($base . '_stubs' . DIRECTORY_SEPARATOR . 'FilterDir1');
  761. $filterPath = $view->getFilterPath('foo');
  762. $this->assertEquals($expected, $filterPath, var_export($filterPath, 1));
  763. }
  764. public function testGetFilters()
  765. {
  766. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR;
  767. $view = new Zend_View();
  768. $view->setScriptPath($base . '_templates');
  769. $view->addFilterPath($base . '_stubs' . DIRECTORY_SEPARATOR . 'FilterDir1');
  770. $view->addFilter('foo');
  771. $filters = $view->getFilters();
  772. $this->assertEquals(1, count($filters));
  773. $this->assertEquals('foo', $filters[0]);
  774. }
  775. public function testMissingViewScriptExceptionText()
  776. {
  777. $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR;
  778. $view = new Zend_View();
  779. $view->setScriptPath($base . '_templates');
  780. try {
  781. $view->render('bazbatNotExists.php.tpl');
  782. $this->fail('Non-existent view script should cause an exception');
  783. } catch (Exception $e) {
  784. $this->assertContains($base. '_templates', $e->getMessage());
  785. }
  786. }
  787. public function testGetHelperIsCaseInsensitive()
  788. {
  789. $view = new Zend_View();
  790. $hidden = $view->formHidden('foo', 'bar');
  791. $this->assertContains('<input type="hidden"', $hidden);
  792. $hidden = $view->getHelper('formHidden')->formHidden('foo', 'bar');
  793. $this->assertContains('<input type="hidden"', $hidden);
  794. $hidden = $view->getHelper('FormHidden')->formHidden('foo', 'bar');
  795. $this->assertContains('<input type="hidden"', $hidden);
  796. }
  797. public function testGetHelperUsingDifferentCasesReturnsSameInstance()
  798. {
  799. $view = new Zend_View();
  800. $helper1 = $view->getHelper('formHidden');
  801. $helper2 = $view->getHelper('FormHidden');
  802. $this->assertSame($helper1, $helper2);
  803. }
  804. /**
  805. * @issue ZF-2742
  806. */
  807. public function testGetHelperWorksWithPredefinedClassNames()
  808. {
  809. $view = new Zend_View();
  810. $view->setHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir2');
  811. try {
  812. $view->setHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir1', null);
  813. $this->fail('Exception for empty prefix was expected.');
  814. } catch (Exception $e) {
  815. $this->assertContains('only takes strings', $e->getMessage());
  816. }
  817. try {
  818. $view->setHelperPath(dirname(__FILE__) . '/View/_stubs/HelperDir1', null);
  819. $this->fail('Exception for empty prefix was expected.');
  820. } catch (Exception $e) {
  821. $this->assertContains('only takes strings', $e->getMessage());
  822. }
  823. try {
  824. $helper = $view->getHelper('Datetime');
  825. } catch (Exception $e) {
  826. $this->assertContains('not found', $e->getMessage());
  827. }
  828. }
  829. public function testUseStreamWrapperFlagShouldDefaultToFalse()
  830. {
  831. $this->view = new Zend_View();
  832. $this->assertFalse($this->view->useStreamWrapper());
  833. }
  834. public function testUseStreamWrapperStateShouldBeConfigurable()
  835. {
  836. $this->testUseStreamWrapperFlagShouldDefaultToFalse();
  837. $this->view->setUseStreamWrapper(true);
  838. $this->assertTrue($this->view->useStreamWrapper());
  839. $this->view->setUseStreamWrapper(false);
  840. $this->assertFalse($this->view->useStreamWrapper());
  841. }
  842. /**
  843. * @group ZF-5748
  844. */
  845. public function testRenderShouldNotAllowScriptPathsContainingParentDirectoryTraversal()
  846. {
  847. $view = new Zend_View();
  848. try {
  849. $view->render('../foobar.html');
  850. $this->fail('Should not allow parent directory traversal');
  851. } catch (Zend_View_Exception $e) {
  852. $this->assertContains('parent directory traversal', $e->getMessage());
  853. }
  854. try {
  855. $view->render('foo/../foobar.html');
  856. $this->fail('Should not allow parent directory traversal');
  857. } catch (Zend_View_Exception $e) {
  858. $this->assertContains('parent directory traversal', $e->getMessage());
  859. }
  860. try {
  861. $view->render('foo/..\foobar.html');
  862. $this->fail('Should not allow parent directory traversal');
  863. } catch (Zend_View_Exception $e) {
  864. $this->assertContains('parent directory traversal', $e->getMessage());
  865. }
  866. }
  867. /**
  868. * @group ZF-5748
  869. */
  870. public function testLfiProtectionFlagShouldBeEnabledByDefault()
  871. {
  872. $view = new Zend_View();
  873. $this->assertTrue($view->isLfiProtectionOn());
  874. }
  875. /**
  876. * @group ZF-5748
  877. */
  878. public function testLfiProtectionFlagMayBeDisabledViaConstructorOption()
  879. {
  880. $view = new Zend_View(array('lfiProtectionOn' => false));
  881. $this->assertFalse($view->isLfiProtectionOn());
  882. }
  883. /**
  884. * @group ZF-5748
  885. */
  886. public function testLfiProtectionFlagMayBeDisabledViaMethodCall()
  887. {
  888. $view = new Zend_View();
  889. $view->setLfiProtection(false);
  890. $this->assertFalse($view->isLfiProtectionOn());
  891. }
  892. /**
  893. * @group ZF-5748
  894. */
  895. public function testDisablingLfiProtectionAllowsParentDirectoryTraversal()
  896. {
  897. $view = new Zend_View(array(
  898. 'lfiProtectionOn' => false,
  899. 'scriptPath' => dirname(__FILE__) . '/View/_templates/',
  900. ));
  901. try {
  902. $test = $view->render('../_stubs/scripts/LfiProtectionCheck.phtml');
  903. $this->assertContains('LFI', $test);
  904. } catch (Zend_View_Exception $e) {
  905. $this->fail('LFI attack failed: ' . $e->getMessage());
  906. }
  907. }
  908. /**
  909. * @group ZF-6087
  910. */
  911. public function testConstructorShouldAllowPassingArrayOfHelperPaths()
  912. {
  913. $view = new Zend_View(array(
  914. 'helperPath' => array(
  915. 'My_View' => 'My/View/',
  916. ),
  917. ));
  918. $paths = $view->getHelperPaths();
  919. $this->assertTrue(array_key_exists('My_View_', $paths), var_export($paths, 1));
  920. }
  921. /**
  922. * @group ZF-6087
  923. */
  924. public function testConstructorShouldAllowPassingArrayOfFilterPaths()
  925. {
  926. $view = new Zend_View(array(
  927. 'filterPath' => array(
  928. 'My_View' => 'My/View/',
  929. ),
  930. ));
  931. $paths = $view->getFilterPaths();
  932. $this->assertTrue(array_key_exists('My_View_', $paths), var_export($paths, 1));
  933. }
  934. /**
  935. * @group ZF-8177
  936. */
  937. public function testRegisterHelperShouldRegisterHelperWithView()
  938. {
  939. require_once dirname(__FILE__) . '/View/_stubs/HelperDir1/Stub1.php';
  940. $view = new Zend_View();
  941. $helper = new Foo_View_Helper_Stub1();
  942. $view->registerHelper($helper, 'stub1');
  943. $this->assertEquals($view->getHelper('stub1'), $helper);
  944. $this->assertEquals($view->stub1(), 'foo');
  945. }
  946. /**
  947. * @group ZF-8177
  948. * @expectedException Zend_View_Exception
  949. */
  950. public function testRegisterHelperShouldThrowExceptionIfNotProvidedAnObject()
  951. {
  952. $view = new Zend_View();
  953. $view->registerHelper('Foo', 'foo');
  954. }
  955. /**
  956. * @group ZF-8177
  957. * @expectedException Zend_View_Exception
  958. */
  959. public function testRegisterHelperShouldThrowExceptionIfProvidedANonHelperObject()
  960. {
  961. $view = new Zend_View();
  962. $helper = new stdClass;
  963. $view->registerHelper($helper, 'foo');
  964. }
  965. /**
  966. * @group ZF-8177
  967. */
  968. public function testRegisterHelperShouldRegisterViewObjectWithHelper()
  969. {
  970. require_once 'Zend/View/Helper/Doctype.php';
  971. $view = new Zend_View();
  972. $helper = new Zend_View_Helper_Doctype();
  973. $view->registerHelper($helper, 'doctype');
  974. $this->assertSame($view, $helper->view);
  975. }
  976. }
  977. /**
  978. * @category Zend
  979. * @package Zend_View
  980. * @subpackage UnitTests
  981. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  982. * @license http://framework.zend.com/license/new-bsd New BSD License
  983. */
  984. class Zend_ViewTest_Extension extends Zend_View
  985. {
  986. public function init()
  987. {
  988. $this->assign('foo', 'bar');
  989. $this->setScriptPath(dirname(__FILE__) . '/View/_templates');
  990. }
  991. }
  992. // Call Zend_ViewTest::main() if this source file is executed directly.
  993. if (PHPUnit_MAIN_METHOD == "Zend_ViewTest::main") {
  994. Zend_ViewTest::main();
  995. }