NavigationTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. require_once dirname(__FILE__) . '/TestAbstract.php';
  23. require_once 'Zend/View/Helper/Navigation.php';
  24. /**
  25. * Tests Zend_View_Helper_Navigation
  26. *
  27. * @category Zend
  28. * @package Zend_View
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_View
  33. * @group Zend_View_Helper
  34. */
  35. class Zend_View_Helper_Navigation_NavigationTest
  36. extends Zend_View_Helper_Navigation_TestAbstract
  37. {
  38. /**
  39. * Class name for view helper to test
  40. *
  41. * @var string
  42. */
  43. protected $_helperName = 'Zend_View_Helper_Navigation';
  44. /**
  45. * View helper
  46. *
  47. * @var Zend_View_Helper_Navigation
  48. */
  49. protected $_helper;
  50. public function testHelperEntryPointWithoutAnyParams()
  51. {
  52. $returned = $this->_helper->navigation();
  53. $this->assertEquals($this->_helper, $returned);
  54. $this->assertEquals($this->_nav1, $returned->getContainer());
  55. }
  56. public function testHelperEntryPointWithContainerParam()
  57. {
  58. $returned = $this->_helper->navigation($this->_nav2);
  59. $this->assertEquals($this->_helper, $returned);
  60. $this->assertEquals($this->_nav2, $returned->getContainer());
  61. }
  62. public function testShouldProxyToMenuHelperByDeafult()
  63. {
  64. // setup
  65. $oldReg = null;
  66. if (Zend_Registry::isRegistered(self::REGISTRY_KEY)) {
  67. $oldReg = Zend_Registry::get(self::REGISTRY_KEY);
  68. }
  69. Zend_Registry::set(self::REGISTRY_KEY, $this->_nav1);
  70. $this->_helper->setContainer(null);
  71. // result
  72. $expected = $this->_getExpected('menu/default1.html');
  73. $actual = $this->_helper->render();
  74. // teardown
  75. Zend_Registry::set(self::REGISTRY_KEY, $oldReg);
  76. $this->assertEquals($expected, $actual);
  77. }
  78. public function testHasContainer()
  79. {
  80. $oldContainer = $this->_helper->getContainer();
  81. $this->_helper->setContainer(null);
  82. $this->assertFalse($this->_helper->hasContainer());
  83. $this->_helper->setContainer($oldContainer);
  84. }
  85. public function testInjectingContainer()
  86. {
  87. // setup
  88. $this->_helper->setContainer($this->_nav2);
  89. $expected = array(
  90. 'menu' => $this->_getExpected('menu/default2.html'),
  91. 'breadcrumbs' => $this->_getExpected('bc/default.html')
  92. );
  93. $actual = array();
  94. // result
  95. $actual['menu'] = $this->_helper->render();
  96. $this->_helper->setContainer($this->_nav1);
  97. $actual['breadcrumbs'] = $this->_helper->breadcrumbs()->render();
  98. $this->assertEquals($expected, $actual);
  99. }
  100. public function testDisablingContainerInjection()
  101. {
  102. // setup
  103. $this->_helper->setInjectContainer(false);
  104. $this->_helper->menu()->setContainer(null);
  105. $this->_helper->breadcrumbs()->setContainer(null);
  106. $this->_helper->setContainer($this->_nav2);
  107. // result
  108. $expected = array(
  109. 'menu' => '',
  110. 'breadcrumbs' => ''
  111. );
  112. $actual = array(
  113. 'menu' => $this->_helper->render(),
  114. 'breadcrumbs' => $this->_helper->breadcrumbs()->render()
  115. );
  116. $this->assertEquals($expected, $actual);
  117. }
  118. public function testInjectingAcl()
  119. {
  120. // setup
  121. $acl = $this->_getAcl();
  122. $this->_helper->setAcl($acl['acl']);
  123. $this->_helper->setRole($acl['role']);
  124. $expected = $this->_getExpected('menu/acl.html');
  125. $actual = $this->_helper->render();
  126. $this->assertEquals($expected, $actual);
  127. }
  128. public function testDisablingAclInjection()
  129. {
  130. // setup
  131. $acl = $this->_getAcl();
  132. $this->_helper->setAcl($acl['acl']);
  133. $this->_helper->setRole($acl['role']);
  134. $this->_helper->setInjectAcl(false);
  135. $expected = $this->_getExpected('menu/default1.html');
  136. $actual = $this->_helper->render();
  137. $this->assertEquals($expected, $actual);
  138. }
  139. public function testInjectingTranslator()
  140. {
  141. $this->_helper->setTranslator($this->_getTranslator());
  142. $expected = $this->_getExpected('menu/translated.html');
  143. $actual = $this->_helper->render();
  144. $this->assertEquals($expected, $actual);
  145. }
  146. public function testDisablingTranslatorInjection()
  147. {
  148. $this->_helper->setTranslator($this->_getTranslator());
  149. $this->_helper->setInjectTranslator(false);
  150. $expected = $this->_getExpected('menu/default1.html');
  151. $actual = $this->_helper->render();
  152. $this->assertEquals($expected, $actual);
  153. }
  154. public function testSpecifyingDefaultProxy()
  155. {
  156. $expected = array(
  157. 'breadcrumbs' => $this->_getExpected('bc/default.html'),
  158. 'menu' => $this->_getExpected('menu/default1.html')
  159. );
  160. $actual = array();
  161. // result
  162. $this->_helper->setDefaultProxy('breadcrumbs');
  163. $actual['breadcrumbs'] = $this->_helper->render($this->_nav1);
  164. $this->_helper->setDefaultProxy('menu');
  165. $actual['menu'] = $this->_helper->render($this->_nav1);
  166. $this->assertEquals($expected, $actual);
  167. }
  168. public function testGetAclReturnsNullIfNoAclInstance()
  169. {
  170. $this->assertNull($this->_helper->getAcl());
  171. }
  172. public function testGetAclReturnsAclInstanceSetWithSetAcl()
  173. {
  174. $acl = new Zend_Acl();
  175. $this->_helper->setAcl($acl);
  176. $this->assertEquals($acl, $this->_helper->getAcl());
  177. }
  178. public function testGetAclReturnsAclInstanceSetWithSetDefaultAcl()
  179. {
  180. $acl = new Zend_Acl();
  181. Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl($acl);
  182. $actual = $this->_helper->getAcl();
  183. Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl(null);
  184. $this->assertEquals($acl, $actual);
  185. }
  186. public function testSetDefaultAclAcceptsNull()
  187. {
  188. $acl = new Zend_Acl();
  189. Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl($acl);
  190. Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl(null);
  191. $this->assertNull($this->_helper->getAcl());
  192. }
  193. public function testSetDefaultAclAcceptsNoParam()
  194. {
  195. $acl = new Zend_Acl();
  196. Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl($acl);
  197. Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl();
  198. $this->assertNull($this->_helper->getAcl());
  199. }
  200. public function testSetRoleAcceptsString()
  201. {
  202. $this->_helper->setRole('member');
  203. $this->assertEquals('member', $this->_helper->getRole());
  204. }
  205. public function testSetRoleAcceptsRoleInterface()
  206. {
  207. $role = new Zend_Acl_Role('member');
  208. $this->_helper->setRole($role);
  209. $this->assertEquals($role, $this->_helper->getRole());
  210. }
  211. public function testSetRoleAcceptsNull()
  212. {
  213. $this->_helper->setRole('member')->setRole(null);
  214. $this->assertNull($this->_helper->getRole());
  215. }
  216. public function testSetRoleAcceptsNoParam()
  217. {
  218. $this->_helper->setRole('member')->setRole();
  219. $this->assertNull($this->_helper->getRole());
  220. }
  221. public function testSetRoleThrowsExceptionWhenGivenAnInt()
  222. {
  223. try {
  224. $this->_helper->setRole(1337);
  225. $this->fail('An invalid argument was given, but a ' .
  226. 'Zend_View_Exception was not thrown');
  227. } catch (Zend_View_Exception $e) {
  228. $this->assertContains('$role must be a string', $e->getMessage());
  229. }
  230. }
  231. public function testSetRoleThrowsExceptionWhenGivenAnArbitraryObject()
  232. {
  233. try {
  234. $this->_helper->setRole(new stdClass());
  235. $this->fail('An invalid argument was given, but a ' .
  236. 'Zend_View_Exception was not thrown');
  237. } catch (Zend_View_Exception $e) {
  238. $this->assertContains('$role must be a string', $e->getMessage());
  239. }
  240. }
  241. public function testSetDefaultRoleAcceptsString()
  242. {
  243. $expected = 'member';
  244. Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole($expected);
  245. $actual = $this->_helper->getRole();
  246. Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole(null);
  247. $this->assertEquals($expected, $actual);
  248. }
  249. public function testSetDefaultRoleAcceptsRoleInterface()
  250. {
  251. $expected = new Zend_Acl_Role('member');
  252. Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole($expected);
  253. $actual = $this->_helper->getRole();
  254. Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole(null);
  255. $this->assertEquals($expected, $actual);
  256. }
  257. public function testSetDefaultRoleAcceptsNull()
  258. {
  259. Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole(null);
  260. $this->assertNull($this->_helper->getRole());
  261. }
  262. public function testSetDefaultRoleAcceptsNoParam()
  263. {
  264. Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole();
  265. $this->assertNull($this->_helper->getRole());
  266. }
  267. public function testSetDefaultRoleThrowsExceptionWhenGivenAnInt()
  268. {
  269. try {
  270. Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole(1337);
  271. $this->fail('An invalid argument was given, but a ' .
  272. 'Zend_View_Exception was not thrown');
  273. } catch (Zend_View_Exception $e) {
  274. $this->assertContains('$role must be', $e->getMessage());
  275. }
  276. }
  277. public function testSetDefaultRoleThrowsExceptionWhenGivenAnArbitraryObject()
  278. {
  279. try {
  280. Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole(new stdClass());
  281. $this->fail('An invalid argument was given, but a ' .
  282. 'Zend_View_Exception was not thrown');
  283. } catch (Zend_View_Exception $e) {
  284. $this->assertContains('$role must be', $e->getMessage());
  285. }
  286. }
  287. private $_errorMessage;
  288. public function toStringErrorHandler($code, $msg, $file, $line, array $c)
  289. {
  290. $this->_errorMessage = $msg;
  291. }
  292. public function testMagicToStringShouldNotThrowException()
  293. {
  294. set_error_handler(array($this, 'toStringErrorHandler'));
  295. $this->_helper->menu()->setPartial(array(1337));
  296. $this->_helper->__toString();
  297. restore_error_handler();
  298. $this->assertContains('array must contain two values', $this->_errorMessage);
  299. }
  300. public function testPageIdShouldBeNormalized()
  301. {
  302. $nl = Zend_View_Helper_Navigation::EOL;
  303. $container = new Zend_Navigation(array(
  304. array(
  305. 'label' => 'Page 1',
  306. 'id' => 'p1',
  307. 'uri' => 'p1'
  308. ),
  309. array(
  310. 'label' => 'Page 2',
  311. 'id' => 'p2',
  312. 'uri' => 'p2'
  313. )
  314. ));
  315. $expected = '<ul class="navigation">' . $nl
  316. . ' <li>' . $nl
  317. . ' <a id="menu-p1" href="p1">Page 1</a>' . $nl
  318. . ' </li>' . $nl
  319. . ' <li>' . $nl
  320. . ' <a id="menu-p2" href="p2">Page 2</a>' . $nl
  321. . ' </li>' . $nl
  322. . '</ul>';
  323. $actual = $this->_helper->render($container);
  324. $this->assertEquals($expected, $actual);
  325. }
  326. /**
  327. * @group ZF-10458
  328. */
  329. public function testFindCustomHelper()
  330. {
  331. $this->_helper->view->addHelperPath(
  332. $this->_files . '/helpers',
  333. 'My_View_Helper_Navigation'
  334. );
  335. $this->assertTrue(
  336. $this->_helper->findHelper('menu') instanceof
  337. My_View_Helper_Navigation_Menu
  338. );
  339. }
  340. /**
  341. * @group ZF-10458
  342. */
  343. public function testAddHelperPath()
  344. {
  345. $this->_helper->view->addHelperPath(
  346. $this->_files . '/helpers',
  347. 'My_View_Helper_Navigation'
  348. );
  349. $expected = array(
  350. 'Zend_View_Helper_' => array(
  351. 'Zend/View/Helper/',
  352. ),
  353. 'My_View_Helper_Navigation_' => array(
  354. $this->_files . '/helpers/',
  355. ),
  356. );
  357. $this->assertSame($expected, $this->_helper->view->getHelperPaths());
  358. }
  359. /**
  360. * @group ZF-10458
  361. */
  362. public function testRenderCustomHelper()
  363. {
  364. $this->_helper->view->addHelperPath(
  365. $this->_files . '/helpers',
  366. 'My_View_Helper_Navigation'
  367. );
  368. $this->assertSame('<menu/>', (string) $this->_helper->menu());
  369. }
  370. /**
  371. * @group ZF-6854
  372. */
  373. public function testRenderInvisibleItem()
  374. {
  375. $container = new Zend_Navigation(array(
  376. array(
  377. 'label' => 'Page 1',
  378. 'id' => 'p1',
  379. 'uri' => 'p1'
  380. ),
  381. array(
  382. 'label' => 'Page 2',
  383. 'id' => 'p2',
  384. 'uri' => 'p2',
  385. 'visible' => false
  386. )
  387. ));
  388. $render = $this->_helper->menu()->render($container);
  389. $this->assertFalse(strpos($render, 'p2'));
  390. $this->_helper->menu()->setRenderInvisible();
  391. $render = $this->_helper->menu()->render($container);
  392. $this->assertTrue(strpos($render, 'p2') !== false);
  393. }
  394. }