class BreadcrumbManagerTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Breadcrumb/BreadcrumbManagerTest.php \Drupal\Tests\Core\Breadcrumb\BreadcrumbManagerTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Breadcrumb/BreadcrumbManagerTest.php \Drupal\Tests\Core\Breadcrumb\BreadcrumbManagerTest
  3. 11.x core/tests/Drupal/Tests/Core/Breadcrumb/BreadcrumbManagerTest.php \Drupal\Tests\Core\Breadcrumb\BreadcrumbManagerTest

@coversDefaultClass \Drupal\Core\Breadcrumb\BreadcrumbManager @group Breadcrumb

Hierarchy

Expanded class hierarchy of BreadcrumbManagerTest

File

core/tests/Drupal/Tests/Core/Breadcrumb/BreadcrumbManagerTest.php, line 18

Namespace

Drupal\Tests\Core\Breadcrumb
View source
class BreadcrumbManagerTest extends UnitTestCase {
    
    /**
     * The dependency injection container.
     *
     * @var \Symfony\Component\DependencyInjection\ContainerBuilder
     */
    protected $container;
    
    /**
     * The breadcrumb object.
     *
     * @var \Drupal\Core\Breadcrumb\Breadcrumb
     */
    protected $breadcrumb;
    
    /**
     * The tested breadcrumb manager.
     *
     * @var \Drupal\Core\Breadcrumb\BreadcrumbManager
     */
    protected $breadcrumbManager;
    
    /**
     * The mocked module handler.
     *
     * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $moduleHandler;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->moduleHandler = $this->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
        $this->breadcrumbManager = new BreadcrumbManager($this->moduleHandler);
        $this->breadcrumb = new Breadcrumb();
        $this->container = new ContainerBuilder();
        $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
        $cache_contexts_manager->assertValidTokens()
            ->willReturn(TRUE);
        $cache_contexts_manager->reveal();
        $this->container
            ->set('cache_contexts_manager', $cache_contexts_manager);
        \Drupal::setContainer($this->container);
    }
    
    /**
     * Tests the breadcrumb manager without any set breadcrumb.
     */
    public function testBuildWithoutBuilder() : void {
        $route_match = $this->createMock('Drupal\\Core\\Routing\\RouteMatchInterface');
        $this->moduleHandler
            ->expects($this->once())
            ->method('alter')
            ->with('system_breadcrumb', $this->breadcrumb, $route_match, [
            'builder' => NULL,
        ]);
        $breadcrumb = $this->breadcrumbManager
            ->build($this->createMock('Drupal\\Core\\Routing\\RouteMatchInterface'));
        $this->assertEquals([], $breadcrumb->getLinks());
        $this->assertEquals([], $breadcrumb->getCacheContexts());
        $this->assertEquals([], $breadcrumb->getCacheTags());
        $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
    }
    
    /**
     * Tests the build method with a single breadcrumb builder.
     */
    public function testBuildWithSingleBuilder() : void {
        $builder = $this->createMock('Drupal\\Core\\Breadcrumb\\BreadcrumbBuilderInterface');
        $links = [
            '<a href="/example">Test</a>',
        ];
        $this->breadcrumb
            ->setLinks($links);
        $this->breadcrumb
            ->addCacheContexts([
            'foo',
        ])
            ->addCacheTags([
            'bar',
        ]);
        $builder->expects($this->once())
            ->method('applies')
            ->willReturn(TRUE);
        $builder->expects($this->once())
            ->method('build')
            ->willReturn($this->breadcrumb);
        $route_match = $this->createMock('Drupal\\Core\\Routing\\RouteMatchInterface');
        $this->moduleHandler
            ->expects($this->once())
            ->method('alter')
            ->with('system_breadcrumb', $this->breadcrumb, $route_match, [
            'builder' => $builder,
        ]);
        $this->breadcrumbManager
            ->addBuilder($builder, 0);
        $breadcrumb = $this->breadcrumbManager
            ->build($route_match);
        $this->assertEquals($links, $breadcrumb->getLinks());
        $this->assertEquals([
            'foo',
        ], $breadcrumb->getCacheContexts());
        $this->assertEquals([
            'bar',
        ], $breadcrumb->getCacheTags());
        $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
    }
    
    /**
     * Tests multiple breadcrumb builder with different priority.
     */
    public function testBuildWithMultipleApplyingBuilders() : void {
        $builder1 = $this->createMock('Drupal\\Core\\Breadcrumb\\BreadcrumbBuilderInterface');
        $builder1->expects($this->never())
            ->method('applies');
        $builder1->expects($this->never())
            ->method('build');
        $builder2 = $this->createMock('Drupal\\Core\\Breadcrumb\\BreadcrumbBuilderInterface');
        $links2 = [
            '<a href="/example2">Test2</a>',
        ];
        $this->breadcrumb
            ->setLinks($links2);
        $this->breadcrumb
            ->addCacheContexts([
            'baz',
        ])
            ->addCacheTags([
            'qux',
        ]);
        $builder2->expects($this->once())
            ->method('applies')
            ->willReturn(TRUE);
        $builder2->expects($this->once())
            ->method('build')
            ->willReturn($this->breadcrumb);
        $route_match = $this->createMock('Drupal\\Core\\Routing\\RouteMatchInterface');
        $this->moduleHandler
            ->expects($this->once())
            ->method('alter')
            ->with('system_breadcrumb', $this->breadcrumb, $route_match, [
            'builder' => $builder2,
        ]);
        $this->breadcrumbManager
            ->addBuilder($builder1, 0);
        $this->breadcrumbManager
            ->addBuilder($builder2, 10);
        $breadcrumb = $this->breadcrumbManager
            ->build($route_match);
        $this->assertEquals($links2, $breadcrumb->getLinks());
        $this->assertEquals([
            'baz',
        ], $breadcrumb->getCacheContexts());
        $this->assertEquals([
            'qux',
        ], $breadcrumb->getCacheTags());
        $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
    }
    
    /**
     * Tests multiple breadcrumb builders of which one returns NULL.
     */
    public function testBuildWithOneNotApplyingBuilders() : void {
        $builder1 = $this->createMock('Drupal\\Core\\Breadcrumb\\BreadcrumbBuilderInterface');
        $builder1->expects($this->once())
            ->method('applies')
            ->willReturn(FALSE);
        $builder1->expects($this->never())
            ->method('build');
        $builder2 = $this->createMock('Drupal\\Core\\Breadcrumb\\BreadcrumbBuilderInterface');
        $links2 = [
            '<a href="/example2">Test2</a>',
        ];
        $this->breadcrumb
            ->setLinks($links2);
        $this->breadcrumb
            ->addCacheContexts([
            'baz',
        ])
            ->addCacheTags([
            'qux',
        ]);
        $builder2->expects($this->once())
            ->method('applies')
            ->willReturn(TRUE);
        $builder2->expects($this->once())
            ->method('build')
            ->willReturn($this->breadcrumb);
        $route_match = $this->createMock('Drupal\\Core\\Routing\\RouteMatchInterface');
        $this->moduleHandler
            ->expects($this->once())
            ->method('alter')
            ->with('system_breadcrumb', $this->breadcrumb, $route_match, [
            'builder' => $builder2,
        ]);
        $this->breadcrumbManager
            ->addBuilder($builder1, 10);
        $this->breadcrumbManager
            ->addBuilder($builder2, 0);
        $breadcrumb = $this->breadcrumbManager
            ->build($route_match);
        $this->assertEquals($links2, $breadcrumb->getLinks());
        $this->assertEquals([
            'baz',
        ], $breadcrumb->getCacheContexts());
        $this->assertEquals([
            'qux',
        ], $breadcrumb->getCacheTags());
        $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
    }
    
    /**
     * Tests a breadcrumb builder with a bad return value.
     */
    public function testBuildWithInvalidBreadcrumbResult() : void {
        $builder = $this->createMock('Drupal\\Core\\Breadcrumb\\BreadcrumbBuilderInterface');
        $builder->expects($this->once())
            ->method('applies')
            ->willReturn(TRUE);
        $builder->expects($this->once())
            ->method('build')
            ->willReturn('invalid_result');
        $this->breadcrumbManager
            ->addBuilder($builder, 0);
        $this->expectException(\UnexpectedValueException::class);
        $this->breadcrumbManager
            ->build($this->createMock('Drupal\\Core\\Routing\\RouteMatchInterface'));
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
BreadcrumbManagerTest::$breadcrumb protected property The breadcrumb object.
BreadcrumbManagerTest::$breadcrumbManager protected property The tested breadcrumb manager.
BreadcrumbManagerTest::$container protected property The dependency injection container.
BreadcrumbManagerTest::$moduleHandler protected property The mocked module handler.
BreadcrumbManagerTest::setUp protected function Overrides UnitTestCase::setUp
BreadcrumbManagerTest::testBuildWithInvalidBreadcrumbResult public function Tests a breadcrumb builder with a bad return value.
BreadcrumbManagerTest::testBuildWithMultipleApplyingBuilders public function Tests multiple breadcrumb builder with different priority.
BreadcrumbManagerTest::testBuildWithOneNotApplyingBuilders public function Tests multiple breadcrumb builders of which one returns NULL.
BreadcrumbManagerTest::testBuildWithoutBuilder public function Tests the breadcrumb manager without any set breadcrumb.
BreadcrumbManagerTest::testBuildWithSingleBuilder public function Tests the build method with a single breadcrumb builder.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

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