BreadcrumbsTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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-2015 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/Breadcrumbs.php';
  24. /**
  25. * Tests Zend_View_Helper_Navigation_Breadcrumbs
  26. *
  27. * @category Zend_Tests
  28. * @package Zend_View
  29. * @subpackage Helper
  30. * @copyright Copyright (c) 2005-2015 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_BreadcrumbsTest
  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_Breadcrumbs';
  44. /**
  45. * View helper
  46. *
  47. * @var Zend_View_Helper_Navigation_Breadcrumbs
  48. */
  49. protected $_helper;
  50. public function testHelperEntryPointWithoutAnyParams()
  51. {
  52. $returned = $this->_helper->breadcrumbs();
  53. $this->assertEquals($this->_helper, $returned);
  54. $this->assertEquals($this->_nav1, $returned->getContainer());
  55. }
  56. public function testHelperEntryPointWithContainerParam()
  57. {
  58. $returned = $this->_helper->breadcrumbs($this->_nav2);
  59. $this->assertEquals($this->_helper, $returned);
  60. $this->assertEquals($this->_nav2, $returned->getContainer());
  61. }
  62. public function testNullOutContainer()
  63. {
  64. $old = $this->_helper->getContainer();
  65. $this->_helper->setContainer();
  66. $new = $this->_helper->getContainer();
  67. $this->assertNotEquals($old, $new);
  68. }
  69. public function testAutoloadContainerFromRegistry()
  70. {
  71. $oldReg = null;
  72. if (Zend_Registry::isRegistered(self::REGISTRY_KEY)) {
  73. $oldReg = Zend_Registry::get(self::REGISTRY_KEY);
  74. }
  75. Zend_Registry::set(self::REGISTRY_KEY, $this->_nav1);
  76. $this->_helper->setContainer();
  77. $expected = $this->_getExpected('bc/default.html');
  78. $actual = $this->_helper->render();
  79. Zend_Registry::set(self::REGISTRY_KEY, $oldReg);
  80. $this->assertEquals($expected, $actual);
  81. }
  82. public function testSetSeparator()
  83. {
  84. $this->_helper->setSeparator('foo');
  85. $expected = $this->_getExpected('bc/separator.html');
  86. $this->assertEquals($expected, $this->_helper->render());
  87. }
  88. public function testSetMaxDepth()
  89. {
  90. $this->_helper->setMaxDepth(1);
  91. $expected = $this->_getExpected('bc/maxdepth.html');
  92. $this->assertEquals($expected, $this->_helper->render());
  93. }
  94. public function testSetMinDepth()
  95. {
  96. $this->_helper->setMinDepth(1);
  97. $expected = '';
  98. $this->assertEquals($expected, $this->_helper->render($this->_nav2));
  99. }
  100. public function testLinkLastElement()
  101. {
  102. $this->_helper->setLinkLast(true);
  103. $expected = $this->_getExpected('bc/linklast.html');
  104. $this->assertEquals($expected, $this->_helper->render());
  105. }
  106. public function testSetIndent()
  107. {
  108. $this->_helper->setIndent(8);
  109. $expected = ' <a';
  110. $actual = substr($this->_helper->render(), 0, strlen($expected));
  111. $this->assertEquals($expected, $actual);
  112. }
  113. public function testRenderSuppliedContainerWithoutInterfering()
  114. {
  115. $this->_helper->setMinDepth(0);
  116. $rendered1 = $this->_getExpected('bc/default.html');
  117. $rendered2 = 'Site 2';
  118. $expected = array(
  119. 'registered' => $rendered1,
  120. 'supplied' => $rendered2,
  121. 'registered_again' => $rendered1
  122. );
  123. $actual = array(
  124. 'registered' => $this->_helper->render(),
  125. 'supplied' => $this->_helper->render($this->_nav2),
  126. 'registered_again' => $this->_helper->render()
  127. );
  128. $this->assertEquals($expected, $actual);
  129. }
  130. public function testUseAclResourceFromPages()
  131. {
  132. $acl = $this->_getAcl();
  133. $this->_helper->setAcl($acl['acl']);
  134. $this->_helper->setRole($acl['role']);
  135. $expected = $this->_getExpected('bc/acl.html');
  136. $this->assertEquals($expected, $this->_helper->render());
  137. }
  138. public function testTranslationUsingZendTranslate()
  139. {
  140. $this->_helper->setTranslator($this->_getTranslator());
  141. $expected = $this->_getExpected('bc/translated.html');
  142. $this->assertEquals($expected, $this->_helper->render());
  143. }
  144. public function testTranslationUsingZendTranslateAdapter()
  145. {
  146. $translator = $this->_getTranslator();
  147. $this->_helper->setTranslator($translator->getAdapter());
  148. $expected = $this->_getExpected('bc/translated.html');
  149. $this->assertEquals($expected, $this->_helper->render());
  150. }
  151. public function testTranslationFromTranslatorInRegistry()
  152. {
  153. $oldReg = Zend_Registry::isRegistered('Zend_Translate')
  154. ? Zend_Registry::get('Zend_Translate')
  155. : null;
  156. $translator = $this->_getTranslator();
  157. Zend_Registry::set('Zend_Translate', $translator);
  158. $expected = $this->_getExpected('bc/translated.html');
  159. $actual = $this->_helper->render();
  160. Zend_Registry::set('Zend_Translate', $oldReg);
  161. $this->assertEquals($expected, $actual);
  162. }
  163. public function testDisablingTranslation()
  164. {
  165. $translator = $this->_getTranslator();
  166. $this->_helper->setTranslator($translator);
  167. $this->_helper->setUseTranslator(false);
  168. $expected = $this->_getExpected('bc/default.html');
  169. $this->assertEquals($expected, $this->_helper->render());
  170. }
  171. public function testRenderingPartial()
  172. {
  173. $this->_helper->setPartial('bc.phtml');
  174. $expected = $this->_getExpected('bc/partial.html');
  175. $this->assertEquals($expected, $this->_helper->render());
  176. }
  177. public function testRenderingPartialBySpecifyingAnArrayAsPartial()
  178. {
  179. $this->_helper->setPartial(array('bc.phtml', 'default'));
  180. $expected = $this->_getExpected('bc/partial.html');
  181. $this->assertEquals($expected, $this->_helper->render());
  182. }
  183. public function testRenderingPartialShouldFailOnInvalidPartialArray()
  184. {
  185. $this->_helper->setPartial(array('bc.phtml'));
  186. try {
  187. $this->_helper->render();
  188. $this->fail(
  189. '$partial was invalid, but no Zend_View_Exception was thrown');
  190. } catch (Zend_View_Exception $e) {
  191. }
  192. }
  193. public function testLastBreadcrumbShouldBeEscaped()
  194. {
  195. $container = new Zend_Navigation(array(
  196. array(
  197. 'label' => 'Live & Learn',
  198. 'uri' => '#',
  199. 'active' => true
  200. )
  201. ));
  202. $expected = 'Live &amp; Learn';
  203. $actual = $this->_helper->setMinDepth(0)->render($container);
  204. $this->assertEquals($expected, $actual);
  205. }
  206. /**
  207. * @group ZF-11876
  208. */
  209. public function testRenderingWithCustomHtmlAttribs()
  210. {
  211. $container = new Zend_Navigation(array(
  212. array(
  213. 'label' => 'Page 1',
  214. 'uri' => 'p1',
  215. 'customHtmlAttribs' => array(
  216. 'rel' => 'nofollow',
  217. 'style' => 'font-weight: bold;',
  218. ),
  219. 'pages' => array(
  220. array(
  221. 'label' => 'Page 2',
  222. 'uri' => 'p2',
  223. 'customHtmlAttribs' => array(
  224. 'rel' => 'nofollow',
  225. ),
  226. 'pages' => array(
  227. array(
  228. 'label' => 'Page 3',
  229. 'uri' => 'p3',
  230. 'active' => true,
  231. ),
  232. ),
  233. ),
  234. ),
  235. ),
  236. ));
  237. $expected = '<a href="p1" rel="nofollow" style="font-weight: bold;">Page 1</a>'
  238. . ' &gt; '
  239. . '<a href="p2" rel="nofollow">Page 2</a>'
  240. . ' &gt; '
  241. . 'Page 3';
  242. $actual = $this->_helper->setMinDepth(0)->render($container);
  243. $this->assertEquals($expected, $actual);
  244. }
  245. }