AbstractRendererTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace Knp\Menu\Tests\Renderer;
  3. use Knp\Menu\MenuItem;
  4. use Knp\Menu\MenuFactory;
  5. use Knp\Menu\Matcher\MatcherInterface;
  6. use Knp\Menu\Matcher\Matcher;
  7. use Knp\Menu\Tests\TestCase;
  8. abstract class AbstractRendererTest extends TestCase
  9. {
  10. /**
  11. * @var \Knp\Menu\Renderer\RendererInterface
  12. */
  13. protected $renderer;
  14. /**
  15. * @var MatcherInterface
  16. */
  17. private $matcher;
  18. protected function setUp()
  19. {
  20. parent::setUp();
  21. $this->matcher = new Matcher();
  22. $this->renderer = $this->createRenderer($this->matcher);
  23. }
  24. abstract protected function createRenderer(MatcherInterface $matcher);
  25. protected function tearDown()
  26. {
  27. parent::tearDown();
  28. $this->renderer = null;
  29. }
  30. public function testRenderEmptyRoot()
  31. {
  32. $menu = new MenuItem('test', new MenuFactory());
  33. $rendered = '';
  34. $this->assertEquals($rendered, $this->renderer->render($menu));
  35. }
  36. public function testRenderRootWithAttributes()
  37. {
  38. $menu = new MenuItem('test', new MenuFactory());
  39. $menu->setChildrenAttributes(array('class' => 'test_class'));
  40. $menu->addChild('c1');
  41. $rendered = '<ul class="test_class"><li class="first last"><span>c1</span></li></ul>';
  42. $this->assertEquals($rendered, $this->renderer->render($menu));
  43. }
  44. public function testRenderEncodedAttributes()
  45. {
  46. $menu = new MenuItem('test', new MenuFactory());
  47. $menu->setChildrenAttributes(array('title' => 'encode " me >'));
  48. $menu->addChild('c1');
  49. $rendered = '<ul title="encode &quot; me &gt;"><li class="first last"><span>c1</span></li></ul>';
  50. $this->assertEquals($rendered, $this->renderer->render($menu));
  51. }
  52. public function testRenderLink()
  53. {
  54. $menu = new MenuItem('test', new MenuFactory());
  55. $menu->addChild('About', array('uri' => '/about'));
  56. $rendered = '<ul><li class="first last"><a href="/about">About</a></li></ul>';
  57. $this->assertEquals($rendered, $this->renderer->render($menu));
  58. }
  59. public function testRenderLinkWithAttributes()
  60. {
  61. $menu = new MenuItem('test', new MenuFactory());
  62. $menu->addChild('About', array('uri' => '/about', 'linkAttributes' => array('title' => 'About page')));
  63. $rendered = '<ul><li class="first last"><a href="/about" title="About page">About</a></li></ul>';
  64. $this->assertEquals($rendered, $this->renderer->render($menu));
  65. }
  66. public function testRenderLinkWithEmptyAttributes()
  67. {
  68. $menu = new MenuItem('test', new MenuFactory());
  69. $menu->addChild('About', array(
  70. 'uri' => '/about',
  71. 'linkAttributes' => array('title' => '', 'rel' => null, 'target' => false)
  72. ));
  73. $rendered = '<ul><li class="first last"><a href="/about" title="">About</a></li></ul>';
  74. $this->assertEquals($rendered, $this->renderer->render($menu));
  75. }
  76. public function testRenderLinkWithSpecialAttributes()
  77. {
  78. $menu = new MenuItem('test', new MenuFactory());
  79. $menu->addChild('About', array('uri' => '/about', 'linkAttributes' => array('title' => true)));
  80. $rendered = '<ul><li class="first last"><a href="/about" title="title">About</a></li></ul>';
  81. $this->assertEquals($rendered, $this->renderer->render($menu));
  82. }
  83. public function testRenderChildrenWithAttributes()
  84. {
  85. $menu = new MenuItem('test', new MenuFactory());
  86. $about = $menu->addChild('About');
  87. $about->addChild('Us');
  88. $about->setChildrenAttribute('title', 'About page');
  89. $rendered = '<ul><li class="first last"><span>About</span><ul title="About page" class="menu_level_1"><li class="first last"><span>Us</span></li></ul></li></ul>';
  90. $this->assertEquals($rendered, $this->renderer->render($menu));
  91. }
  92. public function testRenderChildrenWithEmptyAttributes()
  93. {
  94. $menu = new MenuItem('test', new MenuFactory());
  95. $about = $menu->addChild('About');
  96. $about->addChild('Us');
  97. $about->setChildrenAttribute('title', '');
  98. $about->setChildrenAttribute('rel', null);
  99. $about->setChildrenAttribute('target', false);
  100. $rendered = '<ul><li class="first last"><span>About</span><ul title="" class="menu_level_1"><li class="first last"><span>Us</span></li></ul></li></ul>';
  101. $this->assertEquals($rendered, $this->renderer->render($menu));
  102. }
  103. public function testRenderChildrenWithSpecialAttributes()
  104. {
  105. $menu = new MenuItem('test', new MenuFactory());
  106. $about = $menu->addChild('About');
  107. $about->addChild('Us');
  108. $about->setChildrenAttribute('title', true);
  109. $rendered = '<ul><li class="first last"><span>About</span><ul title="title" class="menu_level_1"><li class="first last"><span>Us</span></li></ul></li></ul>';
  110. $this->assertEquals($rendered, $this->renderer->render($menu));
  111. }
  112. public function testRenderLabelWithAttributes()
  113. {
  114. $menu = new MenuItem('test', new MenuFactory());
  115. $menu->addChild('About', array('labelAttributes' => array('title' => 'About page')));
  116. $rendered = '<ul><li class="first last"><span title="About page">About</span></li></ul>';
  117. $this->assertEquals($rendered, $this->renderer->render($menu));
  118. }
  119. public function testRenderLabelWithEmptyAttributes()
  120. {
  121. $menu = new MenuItem('test', new MenuFactory());
  122. $menu->addChild('About', array('labelAttributes' => array('title' => '', 'rel' => null, 'target' => false)));
  123. $rendered = '<ul><li class="first last"><span title="">About</span></li></ul>';
  124. $this->assertEquals($rendered, $this->renderer->render($menu));
  125. }
  126. public function testRenderLabelWithSpecialAttributes()
  127. {
  128. $menu = new MenuItem('test', new MenuFactory());
  129. $menu->addChild('About', array('labelAttributes' => array('title' => true)));
  130. $rendered = '<ul><li class="first last"><span title="title">About</span></li></ul>';
  131. $this->assertEquals($rendered, $this->renderer->render($menu));
  132. }
  133. public function testRenderWeirdLink()
  134. {
  135. $menu = new MenuItem('test', new MenuFactory());
  136. $menu->addChild('About', array('uri' => 'http://en.wikipedia.org/wiki/%22Weird_Al%22_Yankovic?v1=1&v2=2'));
  137. $rendered = '<ul><li class="first last"><a href="http://en.wikipedia.org/wiki/%22Weird_Al%22_Yankovic?v1=1&amp;v2=2">About</a></li></ul>';
  138. $this->assertEquals($rendered, $this->renderer->render($menu));
  139. }
  140. public function testRenderEscapedLabel()
  141. {
  142. $menu = new MenuItem('test', new MenuFactory());
  143. $menu->addChild('About', array('label' => 'Encode " me'));
  144. $menu->addChild('Safe', array('label' => 'Encode " me again', 'extras' => array('safe_label' => true)));
  145. $menu->addChild('Escaped', array('label' => 'Encode " me too', 'extras' => array('safe_label' => false)));
  146. $rendered = '<ul><li class="first"><span>Encode &quot; me</span></li><li><span>Encode &quot; me again</span></li><li class="last"><span>Encode &quot; me too</span></li></ul>';
  147. $this->assertEquals($rendered, $this->renderer->render($menu));
  148. }
  149. public function testRenderSafeLabel()
  150. {
  151. $menu = new MenuItem('test', new MenuFactory());
  152. $menu->addChild('About', array('label' => 'Encode " me'));
  153. $menu->addChild('Safe', array('label' => 'Encode " me again', 'extras' => array('safe_label' => true)));
  154. $menu->addChild('Escaped', array('label' => 'Encode " me too', 'extras' => array('safe_label' => false)));
  155. $rendered = '<ul><li class="first"><span>Encode &quot; me</span></li><li><span>Encode " me again</span></li><li class="last"><span>Encode &quot; me too</span></li></ul>';
  156. $this->assertEquals($rendered, $this->renderer->render($menu, array('allow_safe_labels' => true)));
  157. }
  158. public function testRenderWholeMenu()
  159. {
  160. $rendered = '<ul class="root"><li class="first"><span>Parent 1</span><ul class="menu_level_1"><li class="first"><span>Child 1</span></li><li><span>Child 2</span></li><li class="last"><span>Child 3</span></li></ul></li><li class="last"><span>Parent 2</span><ul class="menu_level_1"><li class="first last"><span>Child 4</span><ul class="menu_level_2"><li class="first last"><span>Grandchild 1</span></li></ul></li></ul></li></ul>';
  161. $this->assertEquals($rendered, $this->renderer->render($this->menu));
  162. }
  163. public function testRenderWithClassAndTitle()
  164. {
  165. $this->pt2->setAttribute('class', 'parent2_class');
  166. $this->pt2->setAttribute('title', 'parent2 title');
  167. $rendered = '<ul class="root"><li class="first"><span>Parent 1</span><ul class="menu_level_1"><li class="first"><span>Child 1</span></li><li><span>Child 2</span></li><li class="last"><span>Child 3</span></li></ul></li><li class="parent2_class last" title="parent2 title"><span>Parent 2</span><ul class="menu_level_1"><li class="first last"><span>Child 4</span><ul class="menu_level_2"><li class="first last"><span>Grandchild 1</span></li></ul></li></ul></li></ul>';
  168. $this->assertEquals($rendered, $this->renderer->render($this->menu));
  169. }
  170. public function testRenderWithCurrentItem()
  171. {
  172. $this->ch2->setCurrent(true);
  173. $rendered = '<ul class="root"><li class="current_ancestor first"><span>Parent 1</span><ul class="menu_level_1"><li class="first"><span>Child 1</span></li><li class="current"><span>Child 2</span></li><li class="last"><span>Child 3</span></li></ul></li><li class="last"><span>Parent 2</span><ul class="menu_level_1"><li class="first last"><span>Child 4</span><ul class="menu_level_2"><li class="first last"><span>Grandchild 1</span></li></ul></li></ul></li></ul>';
  174. $this->assertEquals($rendered, $this->renderer->render($this->menu));
  175. }
  176. public function testRenderWithCurrentItemAsLink()
  177. {
  178. $menu = new MenuItem('test', new MenuFactory());
  179. $about = $menu->addChild('About', array('uri' => '/about'));
  180. $about->setCurrent(true);
  181. $rendered = '<ul><li class="current first last"><a href="/about">About</a></li></ul>';
  182. $this->assertEquals($rendered, $this->renderer->render($menu));
  183. }
  184. public function testRenderWithCurrentItemNotAsLink()
  185. {
  186. $menu = new MenuItem('test', new MenuFactory());
  187. $about = $menu->addChild('About', array('uri' => '/about'));
  188. $about->setCurrent(true);
  189. $rendered = '<ul><li class="current first last"><span>About</span></li></ul>';
  190. $this->assertEquals($rendered, $this->renderer->render($menu, array('currentAsLink' => false)));
  191. }
  192. public function testRenderSubMenuPortionWithClassAndTitle()
  193. {
  194. $this->pt2->setChildrenAttribute('class', 'parent2_class')->setChildrenAttribute('title', 'parent2 title');
  195. $rendered = '<ul class="parent2_class" title="parent2 title"><li class="first last"><span>Child 4</span><ul class="menu_level_2"><li class="first last"><span>Grandchild 1</span></li></ul></li></ul>';
  196. $this->assertEquals($rendered, $this->renderer->render($this->menu['Parent 2']));
  197. }
  198. public function testDoNotShowChildrenRendersNothing()
  199. {
  200. $this->menu->setDisplayChildren(false);
  201. $rendered = '';
  202. $this->assertEquals($rendered, $this->renderer->render($this->menu));
  203. }
  204. public function testDoNotShowChildChildrenRendersPartialMenu()
  205. {
  206. $this->menu['Parent 1']->setDisplayChildren(false);
  207. $rendered = '<ul class="root"><li class="first"><span>Parent 1</span></li><li class="last"><span>Parent 2</span><ul class="menu_level_1"><li class="first last"><span>Child 4</span><ul class="menu_level_2"><li class="first last"><span>Grandchild 1</span></li></ul></li></ul></li></ul>';
  208. $this->assertEquals($rendered, $this->renderer->render($this->menu));
  209. }
  210. public function testDoNotShowChildRendersPartialMenu()
  211. {
  212. $this->menu['Parent 1']->setDisplay(false);
  213. $rendered = '<ul class="root"><li class="first last"><span>Parent 2</span><ul class="menu_level_1"><li class="first last"><span>Child 4</span><ul class="menu_level_2"><li class="first last"><span>Grandchild 1</span></li></ul></li></ul></li></ul>';
  214. $this->assertEquals($rendered, $this->renderer->render($this->menu));
  215. }
  216. public function testDepth0()
  217. {
  218. $rendered = '';
  219. $this->assertEquals($rendered, $this->renderer->render($this->menu, array('depth' => 0)));
  220. }
  221. public function testDepth1()
  222. {
  223. $rendered = '<ul class="root"><li class="first"><span>Parent 1</span></li><li class="last"><span>Parent 2</span></li></ul>';
  224. $this->assertEquals($rendered, $this->renderer->render($this->menu, array('depth' => 1)));
  225. }
  226. public function testDepth2()
  227. {
  228. $rendered = '<ul class="root"><li class="first"><span>Parent 1</span><ul class="menu_level_1"><li class="first"><span>Child 1</span></li><li><span>Child 2</span></li><li class="last"><span>Child 3</span></li></ul></li><li class="last"><span>Parent 2</span><ul class="menu_level_1"><li class="first last"><span>Child 4</span></li></ul></li></ul>';
  229. $this->assertEquals($rendered, $this->renderer->render($this->menu, array('depth' => 2)));
  230. }
  231. public function testDepth2WithNotShowChildChildren()
  232. {
  233. $this->menu['Parent 1']->setDisplayChildren(false);
  234. $rendered = '<ul class="root"><li class="first"><span>Parent 1</span></li><li class="last"><span>Parent 2</span><ul class="menu_level_1"><li class="first last"><span>Child 4</span></li></ul></li></ul>';
  235. $this->assertEquals($rendered, $this->renderer->render($this->menu, array('depth' => 2)));
  236. }
  237. public function testEmptyUncompressed()
  238. {
  239. $rendered = '';
  240. $this->assertEquals($rendered, $this->renderer->render($this->menu, array('depth' => 0, 'compressed' => false)));
  241. }
  242. }