diff --git a/core/modules/menu_link/lib/Drupal/menu_link/MenuTree.php b/core/modules/menu_link/lib/Drupal/menu_link/MenuTree.php index a4d5d7e..3362b42 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/MenuTree.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/MenuTree.php @@ -596,6 +596,9 @@ public function buildTreeData(array $links, array $parents = array(), $depth = 1 return $data['tree']; } + /** + * Prepares the data for calling $this->treeDataRecursive(). + */ protected function doBuildTreeData(array $links, array $parents = array(), $depth = 1) { // Reverse the array so we can use the more efficient array_pop() function. $links = array_reverse($links); diff --git a/core/modules/menu_link/tests/Drupal/menu_link/Tests/MenuTreeTest.php b/core/modules/menu_link/tests/Drupal/menu_link/Tests/MenuTreeTest.php index 162dead..5d69c20 100644 --- a/core/modules/menu_link/tests/Drupal/menu_link/Tests/MenuTreeTest.php +++ b/core/modules/menu_link/tests/Drupal/menu_link/Tests/MenuTreeTest.php @@ -155,19 +155,27 @@ public function testBuildTreeDataWithSingleLevel() { $items[] = array( 'mlid' => 1, 'depth' => 1, + 'weight' => 0, + 'title' => '', 'route_name' => 'example1', + 'access' => TRUE, ); $items[] = array( 'mlid' => 2, 'depth' => 1, + 'weight' => 0, + 'title' => '', 'route_name' => 'example2', + 'access' => TRUE, ); $result = $this->menuTree->buildTreeData($items, array(), 1); $this->assertCount(2, $result); - $this->assertEquals(array('mlid' => 1, 'depth' => 1, 'route_name' => 'example1', 'in_active_trail' => FALSE), $result[1]['link']); - $this->assertEquals(array('mlid' => 2, 'depth' => 1, 'route_name' => 'example2', 'in_active_trail' => FALSE), $result[2]['link']); + $result1 = array_shift($result); + $this->assertEquals($items[0] + array('in_active_trail' => FALSE), $result1['link']); + $result2 = array_shift($result); + $this->assertEquals($items[1] + array('in_active_trail' => FALSE), $result2['link']); } /** @@ -181,19 +189,27 @@ public function testBuildTreeDataWithSingleLevelAndActiveItem() { $items[] = array( 'mlid' => 1, 'depth' => 1, + 'weight' => 0, + 'title' => '', 'route_name' => 'example1', + 'access' => TRUE, ); $items[] = array( 'mlid' => 2, 'depth' => 1, + 'weight' => 0, + 'title' => '', 'route_name' => 'example2', + 'access' => TRUE, ); $result = $this->menuTree->buildTreeData($items, array(1), 1); $this->assertCount(2, $result); - $this->assertEquals(array('mlid' => 1, 'depth' => 1, 'route_name' => 'example1', 'in_active_trail' => TRUE), $result[1]['link']); - $this->assertEquals(array('mlid' => 2, 'depth' => 1, 'route_name' => 'example2', 'in_active_trail' => FALSE), $result[2]['link']); + $result1 = array_shift($result); + $this->assertEquals($items[0] + array('in_active_trail' => TRUE), $result1['link']); + $result2 = array_shift($result); + $this->assertEquals($items[1] + array('in_active_trail' => FALSE), $result2['link']); } /** @@ -207,45 +223,27 @@ public function testBuildTreeDataWithSingleLevelAndNoActiveItem() { $items[] = array( 'mlid' => 1, 'depth' => 1, + 'weight' => 0, + 'title' => '', 'route_name' => 'example1', + 'access' => TRUE, ); $items[] = array( 'mlid' => 2, 'depth' => 1, + 'weight' => 0, + 'title' => '', 'route_name' => 'example2', + 'access' => TRUE, ); $result = $this->menuTree->buildTreeData($items, array(3), 1); $this->assertCount(2, $result); - $this->assertEquals(array('mlid' => 1, 'depth' => 1, 'route_name' => 'example1', 'in_active_trail' => FALSE), $result[1]['link']); - $this->assertEquals(array('mlid' => 2, 'depth' => 1, 'route_name' => 'example2', 'in_active_trail' => FALSE), $result[2]['link']); - } - - /** - * Tests buildTreeData with multiple levels. - * - * @covers ::buildTreeData - * @covers ::doBuildTreeData - */ - public function testBuildTreeDataWithMultipleLevels() { - $items = array(); - $items[] = array( - 'mlid' => 1, - 'depth' => 1, - 'route_name' => 'example1', - ); - $items[] = array( - 'mlid' => 2, - 'depth' => 1, - 'route_name' => 'example2', - ); - - $result = $this->menuTree->buildTreeData($items, array(3), 1); - - $this->assertCount(2, $result); - $this->assertEquals(array('mlid' => 1, 'depth' => 1, 'route_name' => 'example1', 'in_active_trail' => FALSE), $result[1]['link']); - $this->assertEquals(array('mlid' => 2, 'depth' => 1, 'route_name' => 'example2', 'in_active_trail' => FALSE), $result[2]['link']); + $result1 = array_shift($result); + $this->assertEquals($items[0] + array('in_active_trail' => FALSE), $result1['link']); + $result2 = array_shift($result); + $this->assertEquals($items[1] + array('in_active_trail' => FALSE), $result2['link']); } /** @@ -256,22 +254,22 @@ public function testBuildTreeDataWithMultipleLevels() { */ public function testBuildTreeWithComplexData() { $items = array( - 1 => array('mlid' => 1, 'depth' => 1), - 2 => array('mlid' => 2, 'depth' => 1), - 3 => array('mlid' => 3, 'depth' => 2), - 4 => array('mlid' => 4, 'depth' => 3), - 5 => array('mlid' => 5, 'depth' => 1), + 1 => array('mlid' => 1, 'depth' => 1, 'route_name' => 'example1', 'access' => TRUE, 'weight' => 0, 'title' => ''), + 2 => array('mlid' => 2, 'depth' => 1, 'route_name' => 'example2', 'access' => TRUE, 'weight' => 0, 'title' => ''), + 3 => array('mlid' => 3, 'depth' => 2, 'route_name' => 'example3', 'access' => TRUE, 'weight' => 0, 'title' => ''), + 4 => array('mlid' => 4, 'depth' => 3, 'route_name' => 'example4', 'access' => TRUE, 'weight' => 0, 'title' => ''), + 5 => array('mlid' => 5, 'depth' => 1, 'route_name' => 'example5', 'access' => TRUE, 'weight' => 0, 'title' => ''), ); $tree = $this->menuTree->buildTreeData($items); // Validate that parent items #1, #2, and #5 exist on the root level. - $this->assertEquals($items[1]['mlid'], $tree[1]['link']['mlid']); - $this->assertEquals($items[2]['mlid'], $tree[2]['link']['mlid']); - $this->assertEquals($items[5]['mlid'], $tree[5]['link']['mlid']); + $this->assertEquals($items[1]['mlid'], $tree['50000 1']['link']['mlid']); + $this->assertEquals($items[2]['mlid'], $tree['50000 2']['link']['mlid']); + $this->assertEquals($items[5]['mlid'], $tree['50000 5']['link']['mlid']); // Validate that child item #4 exists at the correct location in the hierarchy. - $this->assertEquals($items[4]['mlid'], $tree[2]['below'][3]['below'][4]['link']['mlid']); + $this->assertEquals($items[4]['mlid'], $tree['50000 2']['below']['50000 3']['below']['50000 4']['link']['mlid']); } /** @@ -354,7 +352,6 @@ public function testCheckAccessWithSingleLevel() { // Build the menu tree and check access for all of the items. $tree = $this->menuTree->buildTreeData($items); - $this->menuTree->checkAccess($tree); $this->assertCount(1, $tree); $item = reset($tree);