BreadcrumbsHelperTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.3.6
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\TestSuite\TestCase;
  17. use Cake\View\Helper\BreadcrumbsHelper;
  18. use Cake\View\View;
  19. class BreadcrumbsHelperTest extends TestCase
  20. {
  21. /**
  22. * Instance of the BreadcrumbsHelper
  23. *
  24. * @var BreadcrumbsHelper
  25. */
  26. public $breadcrumbs;
  27. /**
  28. * setUp method
  29. *
  30. * @return void
  31. */
  32. public function setUp()
  33. {
  34. parent::setUp();
  35. $view = new View();
  36. $this->breadcrumbs = new BreadcrumbsHelper($view);
  37. }
  38. /**
  39. * Test adding crumbs to the trail using add()
  40. * @return void
  41. */
  42. public function testAdd()
  43. {
  44. $this->breadcrumbs
  45. ->add('Home', '/', ['class' => 'first'])
  46. ->add('Some text', ['controller' => 'Some', 'action' => 'text']);
  47. $result = $this->breadcrumbs->getCrumbs();
  48. $expected = [
  49. [
  50. 'title' => 'Home',
  51. 'link' => '/',
  52. 'options' => [
  53. 'class' => 'first'
  54. ]
  55. ],
  56. [
  57. 'title' => 'Some text',
  58. 'link' => [
  59. 'controller' => 'Some',
  60. 'action' => 'text'
  61. ],
  62. 'options' => []
  63. ]
  64. ];
  65. $this->assertEquals($expected, $result);
  66. }
  67. /**
  68. * Test adding multiple crumbs at once to the trail using add()
  69. * @return void
  70. */
  71. public function testAddMultiple()
  72. {
  73. $this->breadcrumbs
  74. ->add([
  75. [
  76. 'title' => 'Home',
  77. 'link' => '/',
  78. 'options' => ['class' => 'first']
  79. ],
  80. [
  81. 'title' => 'Some text',
  82. 'link' => ['controller' => 'Some', 'action' => 'text']
  83. ],
  84. [
  85. 'title' => 'Final',
  86. ],
  87. ]);
  88. $result = $this->breadcrumbs->getCrumbs();
  89. $expected = [
  90. [
  91. 'title' => 'Home',
  92. 'link' => '/',
  93. 'options' => [
  94. 'class' => 'first'
  95. ]
  96. ],
  97. [
  98. 'title' => 'Some text',
  99. 'link' => [
  100. 'controller' => 'Some',
  101. 'action' => 'text'
  102. ],
  103. 'options' => []
  104. ],
  105. [
  106. 'title' => 'Final',
  107. 'link' => null,
  108. 'options' => []
  109. ]
  110. ];
  111. $this->assertEquals($expected, $result);
  112. }
  113. /**
  114. * Test adding crumbs to the trail using prepend()
  115. * @return void
  116. */
  117. public function testPrepend()
  118. {
  119. $this->breadcrumbs
  120. ->add('Home', '/', ['class' => 'first'])
  121. ->prepend('Some text', ['controller' => 'Some', 'action' => 'text'])
  122. ->prepend('The root', '/root', ['data-name' => 'some-name']);
  123. $result = $this->breadcrumbs->getCrumbs();
  124. $expected = [
  125. [
  126. 'title' => 'The root',
  127. 'link' => '/root',
  128. 'options' => ['data-name' => 'some-name']
  129. ],
  130. [
  131. 'title' => 'Some text',
  132. 'link' => [
  133. 'controller' => 'Some',
  134. 'action' => 'text'
  135. ],
  136. 'options' => []
  137. ],
  138. [
  139. 'title' => 'Home',
  140. 'link' => '/',
  141. 'options' => [
  142. 'class' => 'first'
  143. ]
  144. ]
  145. ];
  146. $this->assertEquals($expected, $result);
  147. }
  148. /**
  149. * Test adding crumbs to a specific index
  150. * @return void
  151. */
  152. public function testInsertAt()
  153. {
  154. $this->breadcrumbs
  155. ->add('Home', '/', ['class' => 'first'])
  156. ->prepend('Some text', ['controller' => 'Some', 'action' => 'text'])
  157. ->insertAt(1, 'Insert At', ['controller' => 'Insert', 'action' => 'at'])
  158. ->insertAt(1, 'Insert At Again', ['controller' => 'Insert', 'action' => 'at_again']);
  159. $result = $this->breadcrumbs->getCrumbs();
  160. $expected = [
  161. [
  162. 'title' => 'Some text',
  163. 'link' => [
  164. 'controller' => 'Some',
  165. 'action' => 'text'
  166. ],
  167. 'options' => []
  168. ],
  169. [
  170. 'title' => 'Insert At Again',
  171. 'link' => [
  172. 'controller' => 'Insert',
  173. 'action' => 'at_again'
  174. ],
  175. 'options' => []
  176. ],
  177. [
  178. 'title' => 'Insert At',
  179. 'link' => [
  180. 'controller' => 'Insert',
  181. 'action' => 'at'
  182. ],
  183. 'options' => []
  184. ],
  185. [
  186. 'title' => 'Home',
  187. 'link' => '/',
  188. 'options' => [
  189. 'class' => 'first'
  190. ]
  191. ]
  192. ];
  193. $this->assertEquals($expected, $result);
  194. }
  195. /**
  196. * Test adding crumbs before a specific one
  197. * @return void
  198. */
  199. public function testInsertBefore()
  200. {
  201. $this->breadcrumbs
  202. ->add('Home', '/', ['class' => 'first'])
  203. ->prepend('Some text', ['controller' => 'Some', 'action' => 'text'])
  204. ->prepend('The root', '/root', ['data-name' => 'some-name'])
  205. ->insertBefore('The root', 'The super root');
  206. $result = $this->breadcrumbs->getCrumbs();
  207. $expected = [
  208. [
  209. 'title' => 'The super root',
  210. 'link' => null,
  211. 'options' => []
  212. ],
  213. [
  214. 'title' => 'The root',
  215. 'link' => '/root',
  216. 'options' => ['data-name' => 'some-name']
  217. ],
  218. [
  219. 'title' => 'Some text',
  220. 'link' => [
  221. 'controller' => 'Some',
  222. 'action' => 'text'
  223. ],
  224. 'options' => []
  225. ],
  226. [
  227. 'title' => 'Home',
  228. 'link' => '/',
  229. 'options' => [
  230. 'class' => 'first'
  231. ]
  232. ]
  233. ];
  234. $this->assertEquals($expected, $result);
  235. }
  236. /**
  237. * Test adding crumbs after a specific one
  238. * @return void
  239. */
  240. public function testInsertAfter()
  241. {
  242. $this->breadcrumbs
  243. ->add('Home', '/', ['class' => 'first'])
  244. ->prepend('Some text', ['controller' => 'Some', 'action' => 'text'])
  245. ->prepend('The root', '/root', ['data-name' => 'some-name'])
  246. ->insertAfter('The root', 'The less super root');
  247. $result = $this->breadcrumbs->getCrumbs();
  248. $expected = [
  249. [
  250. 'title' => 'The root',
  251. 'link' => '/root',
  252. 'options' => ['data-name' => 'some-name']
  253. ],
  254. [
  255. 'title' => 'The less super root',
  256. 'link' => null,
  257. 'options' => []
  258. ],
  259. [
  260. 'title' => 'Some text',
  261. 'link' => [
  262. 'controller' => 'Some',
  263. 'action' => 'text'
  264. ],
  265. 'options' => []
  266. ],
  267. [
  268. 'title' => 'Home',
  269. 'link' => '/',
  270. 'options' => [
  271. 'class' => 'first'
  272. ]
  273. ]
  274. ];
  275. $this->assertEquals($expected, $result);
  276. }
  277. /**
  278. * Tests the render method
  279. * @return void
  280. */
  281. public function testRender()
  282. {
  283. $this->breadcrumbs
  284. ->add('Home', '/', ['class' => 'first', 'innerAttrs' => ['data-foo' => 'bar']])
  285. ->add('Some text', ['controller' => 'tests_apps', 'action' => 'some_method'])
  286. ->add('Final crumb', null, ['class' => 'final', 'innerAttrs' => ['class' => 'final-link']]);
  287. $result = $this->breadcrumbs->render(
  288. ['data-stuff' => 'foo and bar'],
  289. ['separator' => '<i class="fa fa-angle-right"></i>', 'class' => 'separator', 'templateVars' => ['custom' => 'custom']]
  290. );
  291. $expected = [
  292. ['ul' => ['data-stuff' => 'foo and bar']],
  293. ['li' => ['class' => 'first']],
  294. ['a' => ['href' => '/', 'data-foo' => 'bar']],
  295. 'Home',
  296. '/a',
  297. '/li',
  298. ['li' => ['class' => 'separator']],
  299. ['span' => []],
  300. 'custom<i class="fa fa-angle-right"></i>',
  301. '/span',
  302. '/li',
  303. ['li' => []],
  304. ['a' => ['href' => '/some_alias']],
  305. 'Some text',
  306. '/a',
  307. '/li',
  308. ['li' => ['class' => 'separator']],
  309. ['span' => []],
  310. 'custom<i class="fa fa-angle-right"></i>',
  311. '/span',
  312. '/li',
  313. ['li' => ['class' => 'final']],
  314. ['span' => ['class' => 'final-link']],
  315. 'Final crumb',
  316. '/span',
  317. '/li',
  318. '/ul'
  319. ];
  320. $this->assertHtml($expected, $result);
  321. }
  322. /**
  323. * Tests the render method with custom templates
  324. * @return void
  325. */
  326. public function testRenderCustomTemplate()
  327. {
  328. $this->breadcrumbs = new BreadcrumbsHelper(new View(), [
  329. 'templates' => [
  330. 'wrapper' => '<ol itemtype="http://schema.org/BreadcrumbList"{{attrs}}>{{content}}</ol>',
  331. 'item' => '<li itemprop="itemListElement" itemtype="http://schema.org/ListItem"{{attrs}}><a itemtype="http://schema.org/Thing" itemprop="item" href="{{link}}"{{innerAttrs}}><span itemprop="name">{{title}}</span></a></li>',
  332. 'itemWithoutLink' => '<li itemprop="itemListElement" itemtype="http://schema.org/ListItem"{{attrs}}><span itemprop="name"{{innerAttrs}}>{{title}}</span></li>',
  333. ]
  334. ]);
  335. $this->breadcrumbs
  336. ->add('Home', '/', ['class' => 'first', 'innerAttrs' => ['data-foo' => 'bar']])
  337. ->add('Final crumb', null, ['class' => 'final', 'innerAttrs' => ['class' => 'final-link']]);
  338. $result = $this->breadcrumbs->render(
  339. ['data-stuff' => 'foo and bar'],
  340. ['separator' => ' > ', 'class' => 'separator']
  341. );
  342. $expected = [
  343. ['ol' => ['itemtype' => 'http://schema.org/BreadcrumbList', 'data-stuff' => 'foo and bar']],
  344. ['li' => ['itemprop' => 'itemListElement', 'itemtype' => 'http://schema.org/ListItem', 'class' => 'first']],
  345. ['a' => ['itemtype' => 'http://schema.org/Thing', 'itemprop' => 'item', 'href' => '/', 'data-foo' => 'bar']],
  346. ['span' => ['itemprop' => 'name']],
  347. 'Home',
  348. '/span',
  349. '/a',
  350. '/li',
  351. ['li' => ['itemprop' => 'itemListElement', 'itemtype' => 'http://schema.org/ListItem', 'class' => 'final']],
  352. ['span' => ['itemprop' => 'name', 'class' => 'final-link']],
  353. 'Final crumb',
  354. '/span',
  355. '/li',
  356. '/ol'
  357. ];
  358. $this->assertHtml($expected, $result, true);
  359. }
  360. /**
  361. * Tests the render method with template vars
  362. * @return void
  363. */
  364. public function testRenderCustomTemplateTemplateVars()
  365. {
  366. $this->breadcrumbs = new BreadcrumbsHelper(new View(), [
  367. 'templates' => [
  368. 'wrapper' => '{{thing}}<ol itemtype="http://schema.org/BreadcrumbList"{{attrs}}>{{content}}</ol>',
  369. 'item' => '<li itemprop="itemListElement" itemtype="http://schema.org/ListItem"{{attrs}}><a itemtype="http://schema.org/Thing" itemprop="item" href="{{link}}"{{innerAttrs}}><span itemprop="name">{{title}}</span></a>{{foo}}</li>',
  370. 'itemWithoutLink' => '<li itemprop="itemListElement" itemtype="http://schema.org/ListItem"{{attrs}}><span itemprop="name"{{innerAttrs}}>{{title}}</span>{{barbaz}}</li>',
  371. ]
  372. ]);
  373. $this->breadcrumbs
  374. ->add('Home', '/', ['class' => 'first', 'innerAttrs' => ['data-foo' => 'bar'], 'templateVars' => ['foo' => 'barbaz']])
  375. ->add('Final crumb', null, ['class' => 'final', 'innerAttrs' => ['class' => 'final-link'], 'templateVars' => ['barbaz' => 'foo']]);
  376. $result = $this->breadcrumbs->render(
  377. ['data-stuff' => 'foo and bar', 'templateVars' => ['thing' => 'somestuff']],
  378. ['separator' => ' > ', 'class' => 'separator']
  379. );
  380. $expected = [
  381. 'somestuff',
  382. ['ol' => ['itemtype' => 'http://schema.org/BreadcrumbList', 'data-stuff' => 'foo and bar']],
  383. ['li' => ['itemprop' => 'itemListElement', 'itemtype' => 'http://schema.org/ListItem', 'class' => 'first']],
  384. ['a' => ['itemtype' => 'http://schema.org/Thing', 'itemprop' => 'item', 'href' => '/', 'data-foo' => 'bar']],
  385. ['span' => ['itemprop' => 'name']],
  386. 'Home',
  387. '/span',
  388. '/a',
  389. 'barbaz',
  390. '/li',
  391. ['li' => ['itemprop' => 'itemListElement', 'itemtype' => 'http://schema.org/ListItem', 'class' => 'final']],
  392. ['span' => ['itemprop' => 'name', 'class' => 'final-link']],
  393. 'Final crumb',
  394. '/span',
  395. 'foo',
  396. '/li',
  397. '/ol'
  398. ];
  399. $this->assertHtml($expected, $result, true);
  400. }
  401. }