You are here

public function PathBasedBreadcrumbBuilderTest::testBuildWithThreePathElements in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Unit/Breadcrumbs/PathBasedBreadcrumbBuilderTest.php \Drupal\Tests\system\Unit\Breadcrumbs\PathBasedBreadcrumbBuilderTest::testBuildWithThreePathElements()

Tests the build method with three path elements.

@covers ::build @covers ::getRequestForPath

File

core/modules/system/tests/src/Unit/Breadcrumbs/PathBasedBreadcrumbBuilderTest.php, line 221
Contains \Drupal\Tests\system\Unit\Breadcrumbs\PathBasedBreadcrumbBuilderTest.

Class

PathBasedBreadcrumbBuilderTest
@coversDefaultClass \Drupal\system\PathBasedBreadcrumbBuilder @group system

Namespace

Drupal\Tests\system\Unit\Breadcrumbs

Code

public function testBuildWithThreePathElements() {
  $this->context
    ->expects($this
    ->once())
    ->method('getPathInfo')
    ->will($this
    ->returnValue('/example/bar/baz'));
  $this
    ->setupStubPathProcessor();
  $route_1 = new Route('/example/bar');
  $route_2 = new Route('/example');
  $this->requestMatcher
    ->expects($this
    ->exactly(2))
    ->method('matchRequest')
    ->willReturnCallback(function (Request $request) use ($route_1, $route_2) {
    if ($request
      ->getPathInfo() == '/example/bar') {
      return [
        RouteObjectInterface::ROUTE_NAME => 'example_bar',
        RouteObjectInterface::ROUTE_OBJECT => $route_1,
        '_raw_variables' => new ParameterBag([]),
      ];
    }
    elseif ($request
      ->getPathInfo() == '/example') {
      return [
        RouteObjectInterface::ROUTE_NAME => 'example',
        RouteObjectInterface::ROUTE_OBJECT => $route_2,
        '_raw_variables' => new ParameterBag([]),
      ];
    }
  });
  $this->accessManager
    ->expects($this
    ->any())
    ->method('check')
    ->willReturnOnConsecutiveCalls(AccessResult::allowed()
    ->cachePerPermissions(), AccessResult::allowed()
    ->addCacheContexts([
    'bar',
  ])
    ->addCacheTags([
    'example',
  ]));
  $breadcrumb = $this->builder
    ->build($this
    ->createMock('Drupal\\Core\\Routing\\RouteMatchInterface'));
  $this
    ->assertEquals([
    new Link('Home', new Url('<front>')),
    new Link('Example', new Url('example')),
    new Link('Bar', new Url('example_bar')),
  ], $breadcrumb
    ->getLinks());
  $this
    ->assertEqualsCanonicalizing([
    'bar',
    'url.path.is_front',
    'url.path.parent',
    'user.permissions',
  ], $breadcrumb
    ->getCacheContexts());
  $this
    ->assertEqualsCanonicalizing([
    'example',
  ], $breadcrumb
    ->getCacheTags());
  $this
    ->assertEquals(Cache::PERMANENT, $breadcrumb
    ->getCacheMaxAge());
}