class ForumBreadcrumbBuilderBaseTest

Same name in other branches
  1. 9 core/modules/forum/tests/src/Unit/Breadcrumb/ForumBreadcrumbBuilderBaseTest.php \Drupal\Tests\forum\Unit\Breadcrumb\ForumBreadcrumbBuilderBaseTest
  2. 8.9.x core/modules/forum/tests/src/Unit/Breadcrumb/ForumBreadcrumbBuilderBaseTest.php \Drupal\Tests\forum\Unit\Breadcrumb\ForumBreadcrumbBuilderBaseTest
  3. 10 core/modules/forum/tests/src/Unit/Breadcrumb/ForumBreadcrumbBuilderBaseTest.php \Drupal\Tests\forum\Unit\Breadcrumb\ForumBreadcrumbBuilderBaseTest

@coversDefaultClass \Drupal\forum\Breadcrumb\ForumBreadcrumbBuilderBase @group forum @group legacy

Hierarchy

Expanded class hierarchy of ForumBreadcrumbBuilderBaseTest

File

core/modules/forum/tests/src/Unit/Breadcrumb/ForumBreadcrumbBuilderBaseTest.php, line 18

Namespace

Drupal\Tests\forum\Unit\Breadcrumb
View source
class ForumBreadcrumbBuilderBaseTest extends UnitTestCase {
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $cache_contexts_manager = $this->getMockBuilder('Drupal\\Core\\Cache\\Context\\CacheContextsManager')
            ->disableOriginalConstructor()
            ->getMock();
        $cache_contexts_manager->method('assertValidTokens')
            ->willReturn(TRUE);
        $container = new Container();
        $container->set('cache_contexts_manager', $cache_contexts_manager);
        \Drupal::setContainer($container);
    }
    
    /**
     * Tests ForumBreadcrumbBuilderBase::__construct().
     *
     * @covers ::__construct
     */
    public function testConstructor() {
        // Make some test doubles.
        $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
        $config_factory = $this->getConfigFactoryStub([
            'forum.settings' => [
                'IAmATestKey' => 'IAmATestValue',
            ],
        ]);
        $forum_manager = $this->createMock('Drupal\\forum\\ForumManagerInterface');
        $translation_manager = $this->createMock('Drupal\\Core\\StringTranslation\\TranslationInterface');
        // Make an object to test.
        $builder = $this->getMockForAbstractClass('Drupal\\forum\\Breadcrumb\\ForumBreadcrumbBuilderBase', [
            $entity_type_manager,
            $config_factory,
            $forum_manager,
            $translation_manager,
        ]);
        // Test that the constructor made a config object with our info in it.
        $reflector = new \ReflectionClass($builder);
        $ref_property = $reflector->getProperty('config');
        $config = $ref_property->getValue($builder);
        $this->assertEquals('IAmATestValue', $config->get('IAmATestKey'));
    }
    
    /**
     * Tests ForumBreadcrumbBuilderBase::build().
     *
     * @see \Drupal\forum\Breadcrumb\ForumBreadcrumbBuilderBase::build()
     *
     * @covers ::build
     */
    public function testBuild() {
        // Build all our dependencies, backwards.
        $translation_manager = $this->getMockBuilder('Drupal\\Core\\StringTranslation\\TranslationInterface')
            ->disableOriginalConstructor()
            ->getMock();
        $forum_manager = $this->getMockBuilder('Drupal\\forum\\ForumManagerInterface')
            ->disableOriginalConstructor()
            ->getMock();
        $prophecy = $this->prophesize('Drupal\\taxonomy\\VocabularyInterface');
        $prophecy->label()
            ->willReturn('Fora_is_the_plural_of_forum');
        $prophecy->id()
            ->willReturn(5);
        $prophecy->getCacheTags()
            ->willReturn([
            'taxonomy_vocabulary:5',
        ]);
        $prophecy->getCacheContexts()
            ->willReturn([]);
        $prophecy->getCacheMaxAge()
            ->willReturn(Cache::PERMANENT);
        $vocab_storage = $this->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
        $vocab_storage->expects($this->any())
            ->method('load')
            ->willReturnMap([
            [
                'forums',
                $prophecy->reveal(),
            ],
        ]);
        $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
        $entity_type_manager->expects($this->any())
            ->method('getStorage')
            ->willReturnMap([
            [
                'taxonomy_vocabulary',
                $vocab_storage,
            ],
        ]);
        $config_factory = $this->getConfigFactoryStub([
            'forum.settings' => [
                'vocabulary' => 'forums',
            ],
        ]);
        // Build a breadcrumb builder to test.
        $breadcrumb_builder = $this->getMockForAbstractClass('Drupal\\forum\\Breadcrumb\\ForumBreadcrumbBuilderBase', [
            $entity_type_manager,
            $config_factory,
            $forum_manager,
            $translation_manager,
        ]);
        // Add a translation manager for t().
        $translation_manager = $this->getStringTranslationStub();
        $breadcrumb_builder->setStringTranslation($translation_manager);
        // Our empty data set.
        $route_match = $this->createMock('Drupal\\Core\\Routing\\RouteMatchInterface');
        // Expected result set.
        $expected = [
            Link::createFromRoute('Home', '<front>'),
            Link::createFromRoute('Fora_is_the_plural_of_forum', 'forum.index'),
        ];
        // And finally, the test.
        $breadcrumb = $breadcrumb_builder->build($route_match);
        $this->assertEquals($expected, $breadcrumb->getLinks());
        $this->assertEquals([
            'route',
        ], $breadcrumb->getCacheContexts());
        $this->assertEquals([
            'taxonomy_vocabulary:5',
        ], $breadcrumb->getCacheTags());
        $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
ForumBreadcrumbBuilderBaseTest::setUp protected function Overrides UnitTestCase::setUp
ForumBreadcrumbBuilderBaseTest::testBuild public function Tests ForumBreadcrumbBuilderBase::build().
ForumBreadcrumbBuilderBaseTest::testConstructor public function Tests ForumBreadcrumbBuilderBase::__construct().
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.
UnitTestCase::$root protected property The app root.
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

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