3 namespace Drupal\Tests\forum\Unit\Breadcrumb;
5 use Drupal\Core\Cache\Cache;
7 use Drupal\taxonomy\TermStorageInterface;
8 use Drupal\Tests\UnitTestCase;
9 use Symfony\Component\DependencyInjection\Container;
12 * @coversDefaultClass \Drupal\forum\Breadcrumb\ForumListingBreadcrumbBuilder
15 class ForumListingBreadcrumbBuilderTest extends UnitTestCase {
20 protected function setUp() {
23 $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
24 ->disableOriginalConstructor()
26 $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
27 $container = new Container();
28 $container->set('cache_contexts_manager', $cache_contexts_manager);
29 \Drupal::setContainer($container);
33 * Tests ForumListingBreadcrumbBuilder::applies().
35 * @param bool $expected
36 * ForumListingBreadcrumbBuilder::applies() expected result.
37 * @param string|null $route_name
38 * (optional) A route name.
39 * @param array $parameter_map
40 * (optional) An array of parameter names and values.
42 * @dataProvider providerTestApplies
45 public function testApplies($expected, $route_name = NULL, $parameter_map = []) {
46 // Make some test doubles.
47 $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
48 $config_factory = $this->getConfigFactoryStub([]);
49 $forum_manager = $this->getMock('Drupal\forum\ForumManagerInterface');
50 $translation_manager = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface');
52 // Make an object to test.
53 $builder = $this->getMockBuilder('Drupal\forum\Breadcrumb\ForumListingBreadcrumbBuilder')
54 ->setConstructorArgs([
63 $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
64 $route_match->expects($this->once())
65 ->method('getRouteName')
66 ->will($this->returnValue($route_name));
67 $route_match->expects($this->any())
68 ->method('getParameter')
69 ->will($this->returnValueMap($parameter_map));
71 $this->assertEquals($expected, $builder->applies($route_match));
75 * Provides test data for testApplies().
78 * Array of datasets for testApplies(). Structured as such:
79 * - ForumListBreadcrumbBuilder::applies() expected result.
80 * - ForumListBreadcrumbBuilder::applies() $attributes input array.
82 public function providerTestApplies() {
83 // Send a Node mock, because NodeInterface cannot be mocked.
84 $mock_term = $this->getMockBuilder('Drupal\taxonomy\Entity\Term')
85 ->disableOriginalConstructor()
103 [['taxonomy_term', 'anything']],
108 [['taxonomy_term', $mock_term]],
114 * Tests ForumListingBreadcrumbBuilder::build().
116 * @see \Drupal\forum\ForumListingBreadcrumbBuilder::build()
120 public function testBuild() {
121 // Build all our dependencies, backwards.
122 $translation_manager = $this->getMockBuilder('Drupal\Core\StringTranslation\TranslationInterface')
123 ->disableOriginalConstructor()
126 $prophecy = $this->prophesize('Drupal\taxonomy\Entity\Term');
127 $prophecy->label()->willReturn('Something');
128 $prophecy->id()->willReturn(1);
129 $prophecy->getCacheTags()->willReturn(['taxonomy_term:1']);
130 $prophecy->getCacheContexts()->willReturn([]);
131 $prophecy->getCacheMaxAge()->willReturn(Cache::PERMANENT);
132 $term1 = $prophecy->reveal();
134 $prophecy = $this->prophesize('Drupal\taxonomy\Entity\Term');
135 $prophecy->label()->willReturn('Something else');
136 $prophecy->id()->willReturn(2);
137 $prophecy->getCacheTags()->willReturn(['taxonomy_term:2']);
138 $prophecy->getCacheContexts()->willReturn([]);
139 $prophecy->getCacheMaxAge()->willReturn(Cache::PERMANENT);
140 $term2 = $prophecy->reveal();
142 $term_storage = $this->getMockBuilder(TermStorageInterface::class)->getMock();
143 $term_storage->expects($this->at(0))
144 ->method('loadAllParents')
145 ->will($this->returnValue([$term1]));
146 $term_storage->expects($this->at(1))
147 ->method('loadAllParents')
148 ->will($this->returnValue([$term1, $term2]));
151 $prophecy = $this->prophesize('Drupal\taxonomy\VocabularyInterface');
152 $prophecy->label()->willReturn('Fora_is_the_plural_of_forum');
153 $prophecy->id()->willReturn(5);
154 $prophecy->getCacheTags()->willReturn(['taxonomy_vocabulary:5']);
155 $prophecy->getCacheContexts()->willReturn([]);
156 $prophecy->getCacheMaxAge()->willReturn(Cache::PERMANENT);
157 $vocab_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
158 $vocab_storage->expects($this->any())
160 ->will($this->returnValueMap([
161 ['forums', $prophecy->reveal()],
164 $entity_manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManagerInterface')
165 ->disableOriginalConstructor()
167 $entity_manager->expects($this->any())
168 ->method('getStorage')
169 ->will($this->returnValueMap([
170 ['taxonomy_vocabulary', $vocab_storage],
171 ['taxonomy_term', $term_storage],
174 $config_factory = $this->getConfigFactoryStub(
176 'forum.settings' => [
177 'vocabulary' => 'forums',
182 $forum_manager = $this->getMock('Drupal\forum\ForumManagerInterface');
184 // Build a breadcrumb builder to test.
185 $breadcrumb_builder = $this->getMock(
186 'Drupal\forum\Breadcrumb\ForumListingBreadcrumbBuilder', NULL, [
190 $translation_manager,
194 // Add a translation manager for t().
195 $translation_manager = $this->getStringTranslationStub();
196 $breadcrumb_builder->setStringTranslation($translation_manager);
198 // The forum listing we need a breadcrumb back from.
199 $prophecy = $this->prophesize('Drupal\taxonomy\Entity\Term');
200 $prophecy->label()->willReturn('You_should_not_see_this');
201 $prophecy->id()->willReturn(23);
202 $prophecy->getCacheTags()->willReturn(['taxonomy_term:23']);
203 $prophecy->getCacheContexts()->willReturn([]);
204 $prophecy->getCacheMaxAge()->willReturn(Cache::PERMANENT);
205 $forum_listing = $prophecy->reveal();
208 $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
209 $route_match->expects($this->exactly(2))
210 ->method('getParameter')
211 ->with('taxonomy_term')
212 ->will($this->returnValue($forum_listing));
216 Link::createFromRoute('Home', '<front>'),
217 Link::createFromRoute('Fora_is_the_plural_of_forum', 'forum.index'),
218 Link::createFromRoute('Something', 'forum.page', ['taxonomy_term' => 1]),
220 $breadcrumb = $breadcrumb_builder->build($route_match);
221 $this->assertEquals($expected1, $breadcrumb->getLinks());
222 $this->assertEquals(['route'], $breadcrumb->getCacheContexts());
223 $this->assertEquals(['taxonomy_term:1', 'taxonomy_term:23', 'taxonomy_vocabulary:5'], $breadcrumb->getCacheTags());
224 $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
228 Link::createFromRoute('Home', '<front>'),
229 Link::createFromRoute('Fora_is_the_plural_of_forum', 'forum.index'),
230 Link::createFromRoute('Something else', 'forum.page', ['taxonomy_term' => 2]),
231 Link::createFromRoute('Something', 'forum.page', ['taxonomy_term' => 1]),
233 $breadcrumb = $breadcrumb_builder->build($route_match);
234 $this->assertEquals($expected2, $breadcrumb->getLinks());
235 $this->assertEquals(['route'], $breadcrumb->getCacheContexts());
236 $this->assertEquals(['taxonomy_term:1', 'taxonomy_term:2', 'taxonomy_term:23', 'taxonomy_vocabulary:5'], $breadcrumb->getCacheTags());
237 $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());