NavigationTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. require_once dirname(__FILE__) . '/TestAbstract.php';
  3. require_once 'Zend/View/Helper/Navigation.php';
  4. /**
  5. * Tests Zend_View_Helper_Navigation
  6. *
  7. */
  8. class Zend_View_Helper_Navigation_NavigationTest
  9. extends Zend_View_Helper_Navigation_TestAbstract
  10. {
  11. /**
  12. * Class name for view helper to test
  13. *
  14. * @var string
  15. */
  16. protected $_helperName = 'Zend_View_Helper_Navigation';
  17. /**
  18. * View helper
  19. *
  20. * @var Zend_View_Helper_Navigation
  21. */
  22. protected $_helper;
  23. public function testHelperEntryPointWithoutAnyParams()
  24. {
  25. $returned = $this->_helper->navigation();
  26. $this->assertEquals($this->_helper, $returned);
  27. $this->assertEquals($this->_nav1, $returned->getContainer());
  28. }
  29. public function testHelperEntryPointWithContainerParam()
  30. {
  31. $returned = $this->_helper->navigation($this->_nav2);
  32. $this->assertEquals($this->_helper, $returned);
  33. $this->assertEquals($this->_nav2, $returned->getContainer());
  34. }
  35. public function testShouldProxyToMenuHelperByDeafult()
  36. {
  37. // setup
  38. $oldReg = null;
  39. if (Zend_Registry::isRegistered(self::REGISTRY_KEY)) {
  40. $oldReg = Zend_Registry::get(self::REGISTRY_KEY);
  41. }
  42. Zend_Registry::set(self::REGISTRY_KEY, $this->_nav1);
  43. $this->_helper->setContainer(null);
  44. // result
  45. $expected = $this->_getExpected('menu/default1.html');
  46. $actual = $this->_helper->render();
  47. // teardown
  48. Zend_Registry::set(self::REGISTRY_KEY, $oldReg);
  49. $this->assertEquals($expected, $actual);
  50. }
  51. public function testHasContainer()
  52. {
  53. $oldContainer = $this->_helper->getContainer();
  54. $this->_helper->setContainer(null);
  55. $this->assertFalse($this->_helper->hasContainer());
  56. $this->_helper->setContainer($oldContainer);
  57. }
  58. public function testInjectingContainer()
  59. {
  60. // setup
  61. $this->_helper->setContainer($this->_nav2);
  62. $expected = array(
  63. 'menu' => $this->_getExpected('menu/default2.html'),
  64. 'breadcrumbs' => $this->_getExpected('bc/default.html')
  65. );
  66. $actual = array();
  67. // result
  68. $actual['menu'] = $this->_helper->render();
  69. $this->_helper->setContainer($this->_nav1);
  70. $actual['breadcrumbs'] = $this->_helper->breadcrumbs()->render();
  71. $this->assertEquals($expected, $actual);
  72. }
  73. public function testDisablingContainerInjection()
  74. {
  75. // setup
  76. $this->_helper->setInjectContainer(false);
  77. $this->_helper->menu()->setContainer(null);
  78. $this->_helper->breadcrumbs()->setContainer(null);
  79. $this->_helper->setContainer($this->_nav2);
  80. // result
  81. $expected = array(
  82. 'menu' => '',
  83. 'breadcrumbs' => ''
  84. );
  85. $actual = array(
  86. 'menu' => $this->_helper->render(),
  87. 'breadcrumbs' => $this->_helper->breadcrumbs()->render()
  88. );
  89. $this->assertEquals($expected, $actual);
  90. }
  91. public function testInjectingAcl()
  92. {
  93. // setup
  94. $acl = $this->_getAcl();
  95. $this->_helper->setAcl($acl['acl']);
  96. $this->_helper->setRole($acl['role']);
  97. $expected = $this->_getExpected('menu/acl.html');
  98. $actual = $this->_helper->render();
  99. $this->assertEquals($expected, $actual);
  100. }
  101. public function testDisablingAclInjection()
  102. {
  103. // setup
  104. $acl = $this->_getAcl();
  105. $this->_helper->setAcl($acl['acl']);
  106. $this->_helper->setRole($acl['role']);
  107. $this->_helper->setInjectAcl(false);
  108. $expected = $this->_getExpected('menu/default1.html');
  109. $actual = $this->_helper->render();
  110. $this->assertEquals($expected, $actual);
  111. }
  112. public function testInjectingTranslator()
  113. {
  114. $this->_helper->setTranslator($this->_getTranslator());
  115. $expected = $this->_getExpected('menu/translated.html');
  116. $actual = $this->_helper->render();
  117. $this->assertEquals($expected, $actual);
  118. }
  119. public function testDisablingTranslatorInjection()
  120. {
  121. $this->_helper->setTranslator($this->_getTranslator());
  122. $this->_helper->setInjectTranslator(false);
  123. $expected = $this->_getExpected('menu/default1.html');
  124. $actual = $this->_helper->render();
  125. $this->assertEquals($expected, $actual);
  126. }
  127. public function testSpecifyingDefaultProxy()
  128. {
  129. $expected = array(
  130. 'breadcrumbs' => $this->_getExpected('bc/default.html'),
  131. 'menu' => $this->_getExpected('menu/default1.html')
  132. );
  133. $actual = array();
  134. // result
  135. $this->_helper->setDefaultProxy('breadcrumbs');
  136. $actual['breadcrumbs'] = $this->_helper->render($this->_nav1);
  137. $this->_helper->setDefaultProxy('menu');
  138. $actual['menu'] = $this->_helper->render($this->_nav1);
  139. $this->assertEquals($expected, $actual);
  140. }
  141. public function testGetAclReturnsNullIfNoAclInstance()
  142. {
  143. $this->assertNull($this->_helper->getAcl());
  144. }
  145. public function testGetAclReturnsAclInstanceSetWithSetAcl()
  146. {
  147. $acl = new Zend_Acl();
  148. $this->_helper->setAcl($acl);
  149. $this->assertEquals($acl, $this->_helper->getAcl());
  150. }
  151. public function testGetAclReturnsAclInstanceSetWithSetDefaultAcl()
  152. {
  153. $acl = new Zend_Acl();
  154. Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl($acl);
  155. $actual = $this->_helper->getAcl();
  156. Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl(null);
  157. $this->assertEquals($acl, $actual);
  158. }
  159. public function testSetDefaultAclAcceptsNull()
  160. {
  161. $acl = new Zend_Acl();
  162. Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl($acl);
  163. Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl(null);
  164. $this->assertNull($this->_helper->getAcl());
  165. }
  166. public function testSetDefaultAclAcceptsNoParam()
  167. {
  168. $acl = new Zend_Acl();
  169. Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl($acl);
  170. Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl();
  171. $this->assertNull($this->_helper->getAcl());
  172. }
  173. public function testSetRoleAcceptsString()
  174. {
  175. $this->_helper->setRole('member');
  176. $this->assertEquals('member', $this->_helper->getRole());
  177. }
  178. public function testSetRoleAcceptsRoleInterface()
  179. {
  180. $role = new Zend_Acl_Role('member');
  181. $this->_helper->setRole($role);
  182. $this->assertEquals($role, $this->_helper->getRole());
  183. }
  184. public function testSetRoleAcceptsNull()
  185. {
  186. $this->_helper->setRole('member')->setRole(null);
  187. $this->assertNull($this->_helper->getRole());
  188. }
  189. public function testSetRoleAcceptsNoParam()
  190. {
  191. $this->_helper->setRole('member')->setRole();
  192. $this->assertNull($this->_helper->getRole());
  193. }
  194. public function testSetRoleThrowsExceptionWhenGivenAnInt()
  195. {
  196. try {
  197. $this->_helper->setRole(1337);
  198. $this->fail('An invalid argument was given, but a ' .
  199. 'Zend_View_Exception was not thrown');
  200. } catch (Zend_View_Exception $e) {
  201. $this->assertContains('$role must be a string', $e->getMessage());
  202. }
  203. }
  204. public function testSetRoleThrowsExceptionWhenGivenAnArbitraryObject()
  205. {
  206. try {
  207. $this->_helper->setRole(new stdClass());
  208. $this->fail('An invalid argument was given, but a ' .
  209. 'Zend_View_Exception was not thrown');
  210. } catch (Zend_View_Exception $e) {
  211. $this->assertContains('$role must be a string', $e->getMessage());
  212. }
  213. }
  214. public function testSetDefaultRoleAcceptsString()
  215. {
  216. $expected = 'member';
  217. Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole($expected);
  218. $actual = $this->_helper->getRole();
  219. Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole(null);
  220. $this->assertEquals($expected, $actual);
  221. }
  222. public function testSetDefaultRoleAcceptsRoleInterface()
  223. {
  224. $expected = new Zend_Acl_Role('member');
  225. Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole($expected);
  226. $actual = $this->_helper->getRole();
  227. Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole(null);
  228. $this->assertEquals($expected, $actual);
  229. }
  230. public function testSetDefaultRoleAcceptsNull()
  231. {
  232. Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole(null);
  233. $this->assertNull($this->_helper->getRole());
  234. }
  235. public function testSetDefaultRoleAcceptsNoParam()
  236. {
  237. Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole();
  238. $this->assertNull($this->_helper->getRole());
  239. }
  240. public function testSetDefaultRoleThrowsExceptionWhenGivenAnInt()
  241. {
  242. try {
  243. Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole(1337);
  244. $this->fail('An invalid argument was given, but a ' .
  245. 'Zend_View_Exception was not thrown');
  246. } catch (Zend_View_Exception $e) {
  247. $this->assertContains('$role must be', $e->getMessage());
  248. }
  249. }
  250. public function testSetDefaultRoleThrowsExceptionWhenGivenAnArbitraryObject()
  251. {
  252. try {
  253. Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole(new stdClass());
  254. $this->fail('An invalid argument was given, but a ' .
  255. 'Zend_View_Exception was not thrown');
  256. } catch (Zend_View_Exception $e) {
  257. $this->assertContains('$role must be', $e->getMessage());
  258. }
  259. }
  260. private $_errorMessage;
  261. public function toStringErrorHandler($code, $msg, $file, $line, array $c)
  262. {
  263. $this->_errorMessage = $msg;
  264. }
  265. public function testMagicToStringShouldNotThrowException()
  266. {
  267. set_error_handler(array($this, 'toStringErrorHandler'));
  268. $this->_helper->menu()->setPartial(array(1337));
  269. $this->_helper->__toString();
  270. restore_error_handler();
  271. $this->assertContains('array must contain two values', $this->_errorMessage);
  272. }
  273. public function testPageIdShouldBeNormalized()
  274. {
  275. $nl = Zend_View_Helper_Navigation::EOL;
  276. $container = new Zend_Navigation(array(
  277. array(
  278. 'label' => 'Page 1',
  279. 'id' => 'p1',
  280. 'uri' => 'p1'
  281. ),
  282. array(
  283. 'label' => 'Page 2',
  284. 'id' => 'p2',
  285. 'uri' => 'p2'
  286. )
  287. ));
  288. $expected = '<ul class="navigation">' . $nl
  289. . ' <li>' . $nl
  290. . ' <a id="menu-p1" href="p1">Page 1</a>' . $nl
  291. . ' </li>' . $nl
  292. . ' <li>' . PHP_EOL
  293. . ' <a id="menu-p2" href="p2">Page 2</a>' . $nl
  294. . ' </li>' . $nl
  295. . '</ul>';
  296. $actual = $this->_helper->render($container);
  297. $this->assertEquals($expected, $actual);
  298. }
  299. }