View source
<?php
namespace Drupal\Tests\forum\Unit\Breadcrumb;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\Container;
class ForumBreadcrumbBuilderBaseTest extends UnitTestCase {
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);
}
public function testConstructor() {
$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');
$builder = $this
->getMockForAbstractClass('Drupal\\forum\\Breadcrumb\\ForumBreadcrumbBuilderBase', [
$entity_type_manager,
$config_factory,
$forum_manager,
$translation_manager,
]);
$reflector = new \ReflectionClass($builder);
$ref_property = $reflector
->getProperty('config');
$ref_property
->setAccessible(TRUE);
$config = $ref_property
->getValue($builder);
$this
->assertEquals('IAmATestValue', $config
->get('IAmATestKey'));
}
public function testBuild() {
$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')
->will($this
->returnValueMap([
[
'forums',
$prophecy
->reveal(),
],
]));
$entity_type_manager = $this
->createMock(EntityTypeManagerInterface::class);
$entity_type_manager
->expects($this
->any())
->method('getStorage')
->will($this
->returnValueMap([
[
'taxonomy_vocabulary',
$vocab_storage,
],
]));
$config_factory = $this
->getConfigFactoryStub([
'forum.settings' => [
'vocabulary' => 'forums',
],
]);
$breadcrumb_builder = $this
->getMockForAbstractClass('Drupal\\forum\\Breadcrumb\\ForumBreadcrumbBuilderBase', [
$entity_type_manager,
$config_factory,
$forum_manager,
$translation_manager,
]);
$translation_manager = $this
->getStringTranslationStub();
$breadcrumb_builder
->setStringTranslation($translation_manager);
$route_match = $this
->createMock('Drupal\\Core\\Routing\\RouteMatchInterface');
$expected = [
Link::createFromRoute('Home', '<front>'),
Link::createFromRoute('Fora_is_the_plural_of_forum', 'forum.index'),
];
$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());
}
}