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

/tests/Zend/ViewTest.php

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