Browse Source

Merge pull request #16169 from cakephp/backport-16160

Backport: Fix inserting at the end of crumbs in BreadCrumbHelper
Mark Story 4 years ago
parent
commit
6a016a3cec

+ 5 - 3
src/View/Helper/BreadcrumbsHelper.php

@@ -128,8 +128,10 @@ class BreadcrumbsHelper extends Helper
      * Insert a crumb at a specific index.
      *
      * If the index already exists, the new crumb will be inserted,
-     * and the existing element will be shifted one index greater.
-     * If the index is out of bounds, it will throw an exception.
+     * before the existing element, shifting the existing element one index
+     * greater than before.
+     *
+     * If the index is out of bounds, an exception will be thrown.
      *
      * @param int $index The index to insert at.
      * @param string $title Title of the crumb.
@@ -145,7 +147,7 @@ class BreadcrumbsHelper extends Helper
      */
     public function insertAt($index, $title, $url = null, array $options = [])
     {
-        if (!isset($this->crumbs[$index])) {
+        if (!isset($this->crumbs[$index]) && $index !== count($this->crumbs)) {
             throw new LogicException(sprintf("No crumb could be found at index '%s'", $index));
         }
 

+ 27 - 0
tests/TestCase/View/Helper/BreadcrumbsHelperTest.php

@@ -366,6 +366,33 @@ class BreadcrumbsHelperTest extends TestCase
     }
 
     /**
+     * Test adding crumbs after a specific one
+     */
+    public function testInsertAfterLastItem()
+    {
+        $this->breadcrumbs
+            ->add('Home', '/')
+            ->insertAfter('Home', 'Below Home', '/below', ['class' => 'second']);
+
+        $result = $this->breadcrumbs->getCrumbs();
+        $expected = [
+            [
+                'title' => 'Home',
+                'url' => '/',
+                'options' => [],
+            ],
+            [
+                'title' => 'Below Home',
+                'url' => '/below',
+                'options' => [
+                    'class' => 'second',
+                ],
+            ],
+        ];
+        $this->assertEquals($expected, $result);
+    }
+
+    /**
      * Tests the render method
      *
      * @return void