function NavigationMenuLinkTreeManipulatorsTest::testAddSecondLevelOverviewLinks

Tests the addSecondLevelOverviewLinks() tree manipulator.

@covers ::addSecondLevelOverviewLinks

File

core/modules/navigation/tests/src/Unit/NavigationMenuLinkTreeManipulatorsTest.php, line 32

Class

NavigationMenuLinkTreeManipulatorsTest
Tests the navigation menu link tree manipulator.

Namespace

Drupal\Tests\navigation\Unit

Code

public function testAddSecondLevelOverviewLinks() : void {
  $routeProvider = $this->createMock(RouteProviderInterface::class);
  // For only the route named 'child_list', return a route object with the
  // SystemController::systemAdminMenuBlockPage as the controller.
  $childListRoute = new Route('/test-child-list', [
    '_controller' => SystemController::class . '::systemAdminMenuBlockPage',
  ]);
  $routeProvider->expects($this->any())
    ->method('getRouteByName')
    ->willReturnCallback(static fn($name) => $name === 'child_list' ? $childListRoute : new Route("/{$name}"));
  $overrides = $this->createMock(StaticMenuLinkOverridesInterface::class);
  $translation = $this->createMock(TranslationInterface::class);
  $translation->method('translateString')
    ->willReturnCallback(static fn($string) => $string);
  $manipulator = new NavigationMenuLinkTreeManipulators($routeProvider, $overrides, $translation);
  $originalTree = $this->mockTree();
  // Make sure overview links do not already exist.
  $this->assertArrayNotHasKey('test.example3.navigation_overview', $originalTree[2]->subtree[3]->subtree);
  $this->assertArrayNotHasKey('test.example6.navigation_overview', $originalTree[5]->subtree[6]->subtree);
  $tree = $manipulator->addSecondLevelOverviewLinks($originalTree);
  // First level menu items should not have any children added.
  $this->assertEmpty($tree[1]->subtree);
  $this->assertEquals($originalTree[2]->subtree, $tree[2]->subtree);
  $this->assertEquals($originalTree[5]->subtree, $tree[5]->subtree);
  $this->assertEquals($originalTree[8]->subtree, $tree[8]->subtree);
  $this->assertEquals($originalTree[11]->subtree, $tree[11]->subtree);
  $this->assertEquals($originalTree[13]->subtree, $tree[13]->subtree);
  $this->assertEquals($originalTree[16]->subtree, $tree[16]->subtree);
  $this->assertEquals($originalTree[19]->subtree, $tree[19]->subtree);
  // Leaves should not have any children added.
  $this->assertEmpty($tree[2]->subtree[3]->subtree[4]->subtree);
  $this->assertEmpty($tree[5]->subtree[6]->subtree[7]->subtree);
  $this->assertEmpty($tree[8]->subtree[9]->subtree[10]->subtree);
  $this->assertEmpty($tree[11]->subtree[12]->subtree);
  $this->assertEmpty($tree[13]->subtree[14]->subtree[15]->subtree);
  $this->assertEmpty($tree[16]->subtree[17]->subtree[18]->subtree);
  $this->assertEmpty($tree[19]->subtree[20]->subtree[21]->subtree);
  $this->assertEmpty($tree[19]->subtree[20]->subtree[22]->subtree);
  // Links 3 and 6 should have overview children, even though 6 is unrouted.
  $this->assertArrayHasKey('test.example3.navigation_overview', $tree[2]->subtree[3]->subtree);
  $this->assertArrayHasKey('test.example6.navigation_overview', $tree[5]->subtree[6]->subtree);
  // Link 9 is a child list page, so it should not have an overview child.
  $this->assertArrayNotHasKey('test.example9.navigation_overview', $tree[8]->subtree[9]->subtree);
  // Link 14 and Link 17 are <nolink> and <button> routes, so they should not
  // have overview children.
  $this->assertArrayNotHasKey('test.example14.navigation_overview', $tree[13]->subtree[14]->subtree);
  $this->assertArrayNotHasKey('test.example17.navigation_overview', $tree[16]->subtree[17]->subtree);
  // Link 20's child links are either inaccessible, disabled, or link to the
  // same route as 20, so it should not have an overview child.
  $this->assertArrayNotHasKey('test.example20.navigation_overview', $tree[19]->subtree[20]->subtree);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.