function ForumListingBreadcrumbBuilderTest::testBuild

Same name in other branches
  1. 9 core/modules/forum/tests/src/Unit/Breadcrumb/ForumListingBreadcrumbBuilderTest.php \Drupal\Tests\forum\Unit\Breadcrumb\ForumListingBreadcrumbBuilderTest::testBuild()
  2. 8.9.x core/modules/forum/tests/src/Unit/Breadcrumb/ForumListingBreadcrumbBuilderTest.php \Drupal\Tests\forum\Unit\Breadcrumb\ForumListingBreadcrumbBuilderTest::testBuild()
  3. 11.x core/modules/forum/tests/src/Unit/Breadcrumb/ForumListingBreadcrumbBuilderTest.php \Drupal\Tests\forum\Unit\Breadcrumb\ForumListingBreadcrumbBuilderTest::testBuild()

Tests ForumListingBreadcrumbBuilder::build().

@covers ::build

See also

\Drupal\forum\ForumListingBreadcrumbBuilder::build()

File

core/modules/forum/tests/src/Unit/Breadcrumb/ForumListingBreadcrumbBuilderTest.php, line 104

Class

ForumListingBreadcrumbBuilderTest
@coversDefaultClass \Drupal\forum\Breadcrumb\ForumListingBreadcrumbBuilder @group forum @group legacy

Namespace

Drupal\Tests\forum\Unit\Breadcrumb

Code

public function testBuild() : void {
    // Build all our dependencies, backwards.
    $translation_manager = $this->getMockBuilder('Drupal\\Core\\StringTranslation\\TranslationInterface')
        ->disableOriginalConstructor()
        ->getMock();
    $prophecy = $this->prophesize('Drupal\\taxonomy\\Entity\\Term');
    $prophecy->label()
        ->willReturn('Something');
    $prophecy->id()
        ->willReturn(1);
    $prophecy->getCacheTags()
        ->willReturn([
        'taxonomy_term:1',
    ]);
    $prophecy->getCacheContexts()
        ->willReturn([]);
    $prophecy->getCacheMaxAge()
        ->willReturn(Cache::PERMANENT);
    $term1 = $prophecy->reveal();
    $prophecy = $this->prophesize('Drupal\\taxonomy\\Entity\\Term');
    $prophecy->label()
        ->willReturn('Something else');
    $prophecy->id()
        ->willReturn(2);
    $prophecy->getCacheTags()
        ->willReturn([
        'taxonomy_term:2',
    ]);
    $prophecy->getCacheContexts()
        ->willReturn([]);
    $prophecy->getCacheMaxAge()
        ->willReturn(Cache::PERMANENT);
    $term2 = $prophecy->reveal();
    $term_storage = $this->getMockBuilder(TermStorageInterface::class)
        ->getMock();
    $term_storage->expects($this->exactly(2))
        ->method('loadAllParents')
        ->willReturnOnConsecutiveCalls([
        $term1,
    ], [
        $term1,
        $term2,
    ]);
    // The root forum.
    $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->getMockBuilder(EntityTypeManagerInterface::class)
        ->disableOriginalConstructor()
        ->getMock();
    $entity_type_manager->expects($this->any())
        ->method('getStorage')
        ->willReturnMap([
        [
            'taxonomy_vocabulary',
            $vocab_storage,
        ],
        [
            'taxonomy_term',
            $term_storage,
        ],
    ]);
    $config_factory = $this->getConfigFactoryStub([
        'forum.settings' => [
            'vocabulary' => 'forums',
        ],
    ]);
    $forum_manager = $this->createMock('Drupal\\forum\\ForumManagerInterface');
    // Build a breadcrumb builder to test.
    $breadcrumb_builder = new ForumListingBreadcrumbBuilder($entity_type_manager, $config_factory, $forum_manager, $translation_manager);
    // Add a translation manager for t().
    $translation_manager = $this->getStringTranslationStub();
    $breadcrumb_builder->setStringTranslation($translation_manager);
    // The forum listing we need a breadcrumb back from.
    $prophecy = $this->prophesize('Drupal\\taxonomy\\Entity\\Term');
    $prophecy->label()
        ->willReturn('You_should_not_see_this');
    $prophecy->id()
        ->willReturn(23);
    $prophecy->getCacheTags()
        ->willReturn([
        'taxonomy_term:23',
    ]);
    $prophecy->getCacheContexts()
        ->willReturn([]);
    $prophecy->getCacheMaxAge()
        ->willReturn(Cache::PERMANENT);
    $forum_listing = $prophecy->reveal();
    // Our data set.
    $route_match = $this->createMock('Drupal\\Core\\Routing\\RouteMatchInterface');
    $route_match->expects($this->exactly(2))
        ->method('getParameter')
        ->with('taxonomy_term')
        ->willReturn($forum_listing);
    // First test.
    $expected1 = [
        Link::createFromRoute('Home', '<front>'),
        Link::createFromRoute('Fora_is_the_plural_of_forum', 'forum.index'),
        Link::createFromRoute('Something', 'forum.page', [
            'taxonomy_term' => 1,
        ]),
    ];
    $breadcrumb = $breadcrumb_builder->build($route_match);
    $this->assertEquals($expected1, $breadcrumb->getLinks());
    $this->assertEqualsCanonicalizing([
        'route',
    ], $breadcrumb->getCacheContexts());
    $this->assertEqualsCanonicalizing([
        'taxonomy_term:1',
        'taxonomy_term:23',
        'taxonomy_vocabulary:5',
    ], $breadcrumb->getCacheTags());
    $this->assertEqualsCanonicalizing(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
    // Second test.
    $expected2 = [
        Link::createFromRoute('Home', '<front>'),
        Link::createFromRoute('Fora_is_the_plural_of_forum', 'forum.index'),
        Link::createFromRoute('Something else', 'forum.page', [
            'taxonomy_term' => 2,
        ]),
        Link::createFromRoute('Something', 'forum.page', [
            'taxonomy_term' => 1,
        ]),
    ];
    $breadcrumb = $breadcrumb_builder->build($route_match);
    $this->assertEquals($expected2, $breadcrumb->getLinks());
    $this->assertEqualsCanonicalizing([
        'route',
    ], $breadcrumb->getCacheContexts());
    $this->assertEqualsCanonicalizing([
        'taxonomy_term:1',
        'taxonomy_term:2',
        'taxonomy_term:23',
        'taxonomy_vocabulary:5',
    ], $breadcrumb->getCacheTags());
    $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
}

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