BreadcrumbsHelperTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. *
  41. * @return void
  42. */
  43. public function testAdd()
  44. {
  45. $this->breadcrumbs
  46. ->add('Home', '/', ['class' => 'first'])
  47. ->add('Some text', ['controller' => 'Some', 'action' => 'text']);
  48. $result = $this->breadcrumbs->getCrumbs();
  49. $expected = [
  50. [
  51. 'title' => 'Home',
  52. 'url' => '/',
  53. 'options' => [
  54. 'class' => 'first'
  55. ]
  56. ],
  57. [
  58. 'title' => 'Some text',
  59. 'url' => [
  60. 'controller' => 'Some',
  61. 'action' => 'text'
  62. ],
  63. 'options' => []
  64. ]
  65. ];
  66. $this->assertEquals($expected, $result);
  67. }
  68. /**
  69. * Test adding multiple crumbs at once to the trail using add()
  70. *
  71. * @return void
  72. */
  73. public function testAddMultiple()
  74. {
  75. $this->breadcrumbs
  76. ->add([
  77. [
  78. 'title' => 'Home',
  79. 'url' => '/',
  80. 'options' => ['class' => 'first']
  81. ],
  82. [
  83. 'title' => 'Some text',
  84. 'url' => ['controller' => 'Some', 'action' => 'text']
  85. ],
  86. [
  87. 'title' => 'Final',
  88. ],
  89. ]);
  90. $result = $this->breadcrumbs->getCrumbs();
  91. $expected = [
  92. [
  93. 'title' => 'Home',
  94. 'url' => '/',
  95. 'options' => [
  96. 'class' => 'first'
  97. ]
  98. ],
  99. [
  100. 'title' => 'Some text',
  101. 'url' => [
  102. 'controller' => 'Some',
  103. 'action' => 'text'
  104. ],
  105. 'options' => []
  106. ],
  107. [
  108. 'title' => 'Final',
  109. 'url' => null,
  110. 'options' => []
  111. ]
  112. ];
  113. $this->assertEquals($expected, $result);
  114. }
  115. /**
  116. * Test adding crumbs to the trail using prepend()
  117. *
  118. * @return void
  119. */
  120. public function testPrepend()
  121. {
  122. $this->breadcrumbs
  123. ->add('Home', '/', ['class' => 'first'])
  124. ->prepend('Some text', ['controller' => 'Some', 'action' => 'text'])
  125. ->prepend('The root', '/root', ['data-name' => 'some-name']);
  126. $result = $this->breadcrumbs->getCrumbs();
  127. $expected = [
  128. [
  129. 'title' => 'The root',
  130. 'url' => '/root',
  131. 'options' => ['data-name' => 'some-name']
  132. ],
  133. [
  134. 'title' => 'Some text',
  135. 'url' => [
  136. 'controller' => 'Some',
  137. 'action' => 'text'
  138. ],
  139. 'options' => []
  140. ],
  141. [
  142. 'title' => 'Home',
  143. 'url' => '/',
  144. 'options' => [
  145. 'class' => 'first'
  146. ]
  147. ]
  148. ];
  149. $this->assertEquals($expected, $result);
  150. }
  151. /**
  152. * Test adding crumbs to the trail using prepend()
  153. *
  154. * @return void
  155. */
  156. public function testPrependMultiple()
  157. {
  158. $this->breadcrumbs
  159. ->add('Home', '/', ['class' => 'first'])
  160. ->prepend([
  161. ['title' => 'Some text', 'url' => ['controller' => 'Some', 'action' => 'text']],
  162. ['title' => 'The root', 'url' => '/root', 'options' => ['data-name' => 'some-name']]
  163. ]);
  164. $result = $this->breadcrumbs->getCrumbs();
  165. $expected = [
  166. [
  167. 'title' => 'Some text',
  168. 'url' => [
  169. 'controller' => 'Some',
  170. 'action' => 'text'
  171. ],
  172. 'options' => []
  173. ],
  174. [
  175. 'title' => 'The root',
  176. 'url' => '/root',
  177. 'options' => ['data-name' => 'some-name']
  178. ],
  179. [
  180. 'title' => 'Home',
  181. 'url' => '/',
  182. 'options' => [
  183. 'class' => 'first'
  184. ]
  185. ]
  186. ];
  187. $this->assertEquals($expected, $result);
  188. }
  189. /**
  190. * Test adding crumbs to a specific index
  191. *
  192. * @return void
  193. */
  194. public function testInsertAt()
  195. {
  196. $this->breadcrumbs
  197. ->add('Home', '/', ['class' => 'first'])
  198. ->prepend('Some text', ['controller' => 'Some', 'action' => 'text'])
  199. ->insertAt(1, 'Insert At', ['controller' => 'Insert', 'action' => 'at'])
  200. ->insertAt(1, 'Insert At Again', ['controller' => 'Insert', 'action' => 'at_again']);
  201. $result = $this->breadcrumbs->getCrumbs();
  202. $expected = [
  203. [
  204. 'title' => 'Some text',
  205. 'url' => [
  206. 'controller' => 'Some',
  207. 'action' => 'text'
  208. ],
  209. 'options' => []
  210. ],
  211. [
  212. 'title' => 'Insert At Again',
  213. 'url' => [
  214. 'controller' => 'Insert',
  215. 'action' => 'at_again'
  216. ],
  217. 'options' => []
  218. ],
  219. [
  220. 'title' => 'Insert At',
  221. 'url' => [
  222. 'controller' => 'Insert',
  223. 'action' => 'at'
  224. ],
  225. 'options' => []
  226. ],
  227. [
  228. 'title' => 'Home',
  229. 'url' => '/',
  230. 'options' => [
  231. 'class' => 'first'
  232. ]
  233. ]
  234. ];
  235. $this->assertEquals($expected, $result);
  236. }
  237. /**
  238. * Test adding crumbs to a specific index
  239. *
  240. * @expectedException \LogicException
  241. */
  242. public function testInsertAtIndexOutOfBounds()
  243. {
  244. $this->breadcrumbs
  245. ->add('Home', '/', ['class' => 'first'])
  246. ->insertAt(2, 'Insert At Again', ['controller' => 'Insert', 'action' => 'at_again']);
  247. }
  248. /**
  249. * Test adding crumbs before a specific one
  250. *
  251. * @return void
  252. */
  253. public function testInsertBefore()
  254. {
  255. $this->breadcrumbs
  256. ->add('Home', '/', ['class' => 'first'])
  257. ->prepend('Some text', ['controller' => 'Some', 'action' => 'text'])
  258. ->prepend('The root', '/root', ['data-name' => 'some-name'])
  259. ->insertBefore('The root', 'The super root');
  260. $result = $this->breadcrumbs->getCrumbs();
  261. $expected = [
  262. [
  263. 'title' => 'The super root',
  264. 'url' => null,
  265. 'options' => []
  266. ],
  267. [
  268. 'title' => 'The root',
  269. 'url' => '/root',
  270. 'options' => ['data-name' => 'some-name']
  271. ],
  272. [
  273. 'title' => 'Some text',
  274. 'url' => [
  275. 'controller' => 'Some',
  276. 'action' => 'text'
  277. ],
  278. 'options' => []
  279. ],
  280. [
  281. 'title' => 'Home',
  282. 'url' => '/',
  283. 'options' => [
  284. 'class' => 'first'
  285. ]
  286. ]
  287. ];
  288. $this->assertEquals($expected, $result);
  289. }
  290. /**
  291. * Test adding crumbs after a specific one
  292. *
  293. * @return void
  294. */
  295. public function testInsertAfter()
  296. {
  297. $this->breadcrumbs
  298. ->add('Home', '/', ['class' => 'first'])
  299. ->prepend('Some text', ['controller' => 'Some', 'action' => 'text'])
  300. ->prepend('The root', '/root', ['data-name' => 'some-name'])
  301. ->insertAfter('The root', 'The less super root');
  302. $result = $this->breadcrumbs->getCrumbs();
  303. $expected = [
  304. [
  305. 'title' => 'The root',
  306. 'url' => '/root',
  307. 'options' => ['data-name' => 'some-name']
  308. ],
  309. [
  310. 'title' => 'The less super root',
  311. 'url' => null,
  312. 'options' => []
  313. ],
  314. [
  315. 'title' => 'Some text',
  316. 'url' => [
  317. 'controller' => 'Some',
  318. 'action' => 'text'
  319. ],
  320. 'options' => []
  321. ],
  322. [
  323. 'title' => 'Home',
  324. 'url' => '/',
  325. 'options' => [
  326. 'class' => 'first'
  327. ]
  328. ]
  329. ];
  330. $this->assertEquals($expected, $result);
  331. }
  332. /**
  333. * Tests the render method
  334. *
  335. * @return void
  336. */
  337. public function testRender()
  338. {
  339. $this->breadcrumbs
  340. ->add('Home', '/', ['class' => 'first', 'innerAttrs' => ['data-foo' => 'bar']])
  341. ->add('Some text', ['controller' => 'tests_apps', 'action' => 'some_method'])
  342. ->add('Final crumb', null, ['class' => 'final', 'innerAttrs' => ['class' => 'final-link']]);
  343. $result = $this->breadcrumbs->render(
  344. ['data-stuff' => 'foo and bar'],
  345. ['separator' => '<i class="fa fa-angle-right"></i>', 'class' => 'separator']
  346. );
  347. $expected = [
  348. ['ul' => ['data-stuff' => 'foo and bar']],
  349. ['li' => ['class' => 'first']],
  350. ['a' => ['href' => '/', 'data-foo' => 'bar']],
  351. 'Home',
  352. '/a',
  353. '/li',
  354. ['li' => ['class' => 'separator']],
  355. ['span' => []],
  356. ['i' => ['class' => 'fa fa-angle-right']],
  357. '/i',
  358. '/span',
  359. '/li',
  360. ['li' => []],
  361. ['a' => ['href' => '/some_alias']],
  362. 'Some text',
  363. '/a',
  364. '/li',
  365. ['li' => ['class' => 'separator']],
  366. ['span' => []],
  367. ['i' => ['class' => 'fa fa-angle-right']],
  368. '/i',
  369. '/span',
  370. '/li',
  371. ['li' => ['class' => 'final']],
  372. ['span' => ['class' => 'final-link']],
  373. 'Final crumb',
  374. '/span',
  375. '/li',
  376. '/ul'
  377. ];
  378. $this->assertHtml($expected, $result);
  379. }
  380. /**
  381. * Tests the render method with custom templates
  382. *
  383. * @return void
  384. */
  385. public function testRenderCustomTemplate()
  386. {
  387. $this->breadcrumbs = new BreadcrumbsHelper(new View(), [
  388. 'templates' => [
  389. 'wrapper' => '<ol itemtype="http://schema.org/BreadcrumbList"{{attrs}}>{{content}}</ol>',
  390. 'item' => '<li itemprop="itemListElement" itemtype="http://schema.org/ListItem"{{attrs}}><a itemtype="http://schema.org/Thing" itemprop="item" href="{{url}}"{{innerAttrs}}><span itemprop="name">{{title}}</span></a></li>',
  391. 'itemWithoutLink' => '<li itemprop="itemListElement" itemtype="http://schema.org/ListItem"{{attrs}}><span itemprop="name"{{innerAttrs}}>{{title}}</span></li>',
  392. ]
  393. ]);
  394. $this->breadcrumbs
  395. ->add('Home', '/', ['class' => 'first', 'innerAttrs' => ['data-foo' => 'bar']])
  396. ->add('Final crumb', null, ['class' => 'final', 'innerAttrs' => ['class' => 'final-link']]);
  397. $result = $this->breadcrumbs->render(
  398. ['data-stuff' => 'foo and bar'],
  399. ['separator' => ' > ', 'class' => 'separator']
  400. );
  401. $expected = [
  402. ['ol' => ['itemtype' => 'http://schema.org/BreadcrumbList', 'data-stuff' => 'foo and bar']],
  403. ['li' => ['itemprop' => 'itemListElement', 'itemtype' => 'http://schema.org/ListItem', 'class' => 'first']],
  404. ['a' => ['itemtype' => 'http://schema.org/Thing', 'itemprop' => 'item', 'href' => '/', 'data-foo' => 'bar']],
  405. ['span' => ['itemprop' => 'name']],
  406. 'Home',
  407. '/span',
  408. '/a',
  409. '/li',
  410. ['li' => ['itemprop' => 'itemListElement', 'itemtype' => 'http://schema.org/ListItem', 'class' => 'final']],
  411. ['span' => ['itemprop' => 'name', 'class' => 'final-link']],
  412. 'Final crumb',
  413. '/span',
  414. '/li',
  415. '/ol'
  416. ];
  417. $this->assertHtml($expected, $result, true);
  418. }
  419. /**
  420. * Tests the render method with template vars
  421. *
  422. * @return void
  423. */
  424. public function testRenderCustomTemplateTemplateVars()
  425. {
  426. $this->breadcrumbs = new BreadcrumbsHelper(new View(), [
  427. 'templates' => [
  428. 'wrapper' => '{{thing}}<ol itemtype="http://schema.org/BreadcrumbList"{{attrs}}>{{content}}</ol>',
  429. 'item' => '<li itemprop="itemListElement" itemtype="http://schema.org/ListItem"{{attrs}}><a itemtype="http://schema.org/Thing" itemprop="item" href="{{url}}"{{innerAttrs}}><span itemprop="name">{{title}}</span></a>{{foo}}</li>',
  430. 'itemWithoutLink' => '<li itemprop="itemListElement" itemtype="http://schema.org/ListItem"{{attrs}}><span itemprop="name"{{innerAttrs}}>{{title}}</span>{{barbaz}}</li>',
  431. ]
  432. ]);
  433. $this->breadcrumbs
  434. ->add('Home', '/', ['class' => 'first', 'innerAttrs' => ['data-foo' => 'bar'], 'templateVars' => ['foo' => 'barbaz']])
  435. ->add('Final crumb', null, ['class' => 'final', 'innerAttrs' => ['class' => 'final-link'], 'templateVars' => ['barbaz' => 'foo']]);
  436. $result = $this->breadcrumbs->render(
  437. ['data-stuff' => 'foo and bar', 'templateVars' => ['thing' => 'somestuff']],
  438. ['separator' => ' > ', 'class' => 'separator']
  439. );
  440. $expected = [
  441. 'somestuff',
  442. ['ol' => ['itemtype' => 'http://schema.org/BreadcrumbList', 'data-stuff' => 'foo and bar']],
  443. ['li' => ['itemprop' => 'itemListElement', 'itemtype' => 'http://schema.org/ListItem', 'class' => 'first']],
  444. ['a' => ['itemtype' => 'http://schema.org/Thing', 'itemprop' => 'item', 'href' => '/', 'data-foo' => 'bar']],
  445. ['span' => ['itemprop' => 'name']],
  446. 'Home',
  447. '/span',
  448. '/a',
  449. 'barbaz',
  450. '/li',
  451. ['li' => ['itemprop' => 'itemListElement', 'itemtype' => 'http://schema.org/ListItem', 'class' => 'final']],
  452. ['span' => ['itemprop' => 'name', 'class' => 'final-link']],
  453. 'Final crumb',
  454. '/span',
  455. 'foo',
  456. '/li',
  457. '/ol'
  458. ];
  459. $this->assertHtml($expected, $result, true);
  460. }
  461. }