forum-1.x-dev/tests/src/Unit/Breadcrumb/ForumBreadcrumbBuilderBaseTest.php
tests/src/Unit/Breadcrumb/ForumBreadcrumbBuilderBaseTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\forum\Unit\Breadcrumb;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\forum\Breadcrumb\ForumNodeBreadcrumbBuilder;
use Drupal\taxonomy\TermStorageInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\Container;
/**
* @coversDefaultClass \Drupal\forum\Breadcrumb\ForumBreadcrumbBuilderBase
* @group forum
*/
class ForumBreadcrumbBuilderBaseTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$cache_contexts_manager = $this->createMock('Drupal\Core\Cache\Context\CacheContextsManager');
$cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
$container = new Container();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
}
/**
* Tests ForumBreadcrumbBuilderBase::build().
*
* @see \Drupal\forum\Breadcrumb\ForumBreadcrumbBuilderBase::build()
*
* @covers ::build
*/
public function testBuild(): void {
// Build all our dependencies, backwards.
$translation_manager = $this->createMock('Drupal\Core\StringTranslation\TranslationInterface');
$forum_manager = $this->createMock('Drupal\forum\ForumManagerInterface');
$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()],
]);
$term_storage = $this->createMock(TermStorageInterface::class);
$term_storage->expects($this->any())
->method('loadAllParents')
->willReturn([]);
$entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
$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',
],
]
);
// Build a breadcrumb builder to test.
$breadcrumb_builder = new ForumNodeBreadcrumbBuilder(
$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());
}
}
