function MenuActiveTrailTest::testGetActiveLinkReturnsFrontPageLinkAtTheFrontPage

Tests that getActiveLink() returns a <front> route link for a route that is the front page and has no other links.

@legacy-covers ::getActiveLink

File

core/tests/Drupal/Tests/Core/Menu/MenuActiveTrailTest.php, line 184

Class

MenuActiveTrailTest
Tests the active menu trail service.

Namespace

Drupal\Tests\Core\Menu

Code

public function testGetActiveLinkReturnsFrontPageLinkAtTheFrontPage() : void {
  // Mock the request.
  $mock_route = new Route('');
  $request = new Request();
  $request->attributes
    ->set(RouteObjectInterface::ROUTE_NAME, 'link_1');
  $request->attributes
    ->set(RouteObjectInterface::ROUTE_OBJECT, $mock_route);
  $request->attributes
    ->set('_raw_variables', new InputBag([]));
  $this->requestStack
    ->push($request);
  // Pretend that the current path is the front page.
  $this->pathMatcher
    ->method('isFrontPage')
    ->willReturn(TRUE);
  // Make 'link_1' route to have no links and the '<front>' route to have a link.
  $home_link = MenuLinkMock::createMock([
    'id' => 'home_link',
    'route_name' => 'home_link',
    'title' => 'Home',
    'parent' => NULL,
  ]);
  $this->menuLinkManager
    ->method('loadLinksByRoute')
    ->willReturnCallback(function ($route_name) use ($home_link) {
    return match ($route_name) {  'link_1' => [],
      '<front>' => [
        $home_link,
      ],
    
    };
  });
  // Test.
  $this->assertSame($home_link, $this->menuActiveTrail
    ->getActiveLink());
}

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