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\ForumNodeBreadcrumbBuilder
15 class ForumNodeBreadcrumbBuilderTest 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 ForumNodeBreadcrumbBuilder::applies().
35 * @param bool $expected
36 * ForumNodeBreadcrumbBuilder::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([]);
50 $forum_manager = $this->getMock('Drupal\forum\ForumManagerInterface');
51 $forum_manager->expects($this->any())
52 ->method('checkNodeType')
53 ->will($this->returnValue(TRUE));
55 $translation_manager = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface');
57 // Make an object to test.
58 $builder = $this->getMockBuilder('Drupal\forum\Breadcrumb\ForumNodeBreadcrumbBuilder')
70 $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
71 $route_match->expects($this->once())
72 ->method('getRouteName')
73 ->will($this->returnValue($route_name));
74 $route_match->expects($this->any())
75 ->method('getParameter')
76 ->will($this->returnValueMap($parameter_map));
78 $this->assertEquals($expected, $builder->applies($route_match));
82 * Provides test data for testApplies().
84 * Note that this test is incomplete, because we can't mock NodeInterface.
87 * Array of datasets for testApplies(). Structured as such:
88 * - ForumNodeBreadcrumbBuilder::applies() expected result.
89 * - ForumNodeBreadcrumbBuilder::applies() $attributes input array.
91 public function providerTestApplies() {
92 // Send a Node mock, because NodeInterface cannot be mocked.
93 $mock_node = $this->getMockBuilder('Drupal\node\Entity\Node')
94 ->disableOriginalConstructor()
103 'NOT.entity.node.canonical',
107 'entity.node.canonical',
111 'entity.node.canonical',
116 'entity.node.canonical',
117 [['node', $mock_node]],
123 * Tests ForumNodeBreadcrumbBuilder::build().
125 * @see \Drupal\forum\ForumNodeBreadcrumbBuilder::build()
128 public function testBuild() {
129 // Build all our dependencies, backwards.
130 $translation_manager = $this->getMockBuilder('Drupal\Core\StringTranslation\TranslationInterface')
131 ->disableOriginalConstructor()
134 $prophecy = $this->prophesize('Drupal\taxonomy\Entity\Term');
135 $prophecy->label()->willReturn('Something');
136 $prophecy->id()->willReturn(1);
137 $prophecy->getCacheTags()->willReturn(['taxonomy_term:1']);
138 $prophecy->getCacheContexts()->willReturn([]);
139 $prophecy->getCacheMaxAge()->willReturn(Cache::PERMANENT);
140 $term1 = $prophecy->reveal();
142 $prophecy = $this->prophesize('Drupal\taxonomy\Entity\Term');
143 $prophecy->label()->willReturn('Something else');
144 $prophecy->id()->willReturn(2);
145 $prophecy->getCacheTags()->willReturn(['taxonomy_term:2']);
146 $prophecy->getCacheContexts()->willReturn([]);
147 $prophecy->getCacheMaxAge()->willReturn(Cache::PERMANENT);
148 $term2 = $prophecy->reveal();
150 $forum_manager = $this->getMockBuilder('Drupal\forum\ForumManagerInterface')
151 ->disableOriginalConstructor()
153 $term_storage = $this->getMockBuilder(TermStorageInterface::class)->getMock();
154 $term_storage->expects($this->at(0))
155 ->method('loadAllParents')
156 ->will($this->returnValue([$term1]));
157 $term_storage->expects($this->at(1))
158 ->method('loadAllParents')
159 ->will($this->returnValue([$term1, $term2]));
161 $prophecy = $this->prophesize('Drupal\taxonomy\VocabularyInterface');
162 $prophecy->label()->willReturn('Forums');
163 $prophecy->id()->willReturn(5);
164 $prophecy->getCacheTags()->willReturn(['taxonomy_vocabulary:5']);
165 $prophecy->getCacheContexts()->willReturn([]);
166 $prophecy->getCacheMaxAge()->willReturn(Cache::PERMANENT);
167 $vocab_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
168 $vocab_storage->expects($this->any())
170 ->will($this->returnValueMap([
171 ['forums', $prophecy->reveal()],
174 $entity_manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManagerInterface')
175 ->disableOriginalConstructor()
177 $entity_manager->expects($this->any())
178 ->method('getStorage')
179 ->will($this->returnValueMap([
180 ['taxonomy_vocabulary', $vocab_storage],
181 ['taxonomy_term', $term_storage],
184 $config_factory = $this->getConfigFactoryStub(
186 'forum.settings' => [
187 'vocabulary' => 'forums',
192 // Build a breadcrumb builder to test.
193 $breadcrumb_builder = $this->getMock(
194 'Drupal\forum\Breadcrumb\ForumNodeBreadcrumbBuilder', NULL, [
198 $translation_manager,
202 // Add a translation manager for t().
203 $translation_manager = $this->getStringTranslationStub();
204 $property = new \ReflectionProperty('Drupal\forum\Breadcrumb\ForumNodeBreadcrumbBuilder', 'stringTranslation');
205 $property->setAccessible(TRUE);
206 $property->setValue($breadcrumb_builder, $translation_manager);
208 // The forum node we need a breadcrumb back from.
209 $forum_node = $this->getMockBuilder('Drupal\node\Entity\Node')
210 ->disableOriginalConstructor()
214 $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
215 $route_match->expects($this->exactly(2))
216 ->method('getParameter')
218 ->will($this->returnValue($forum_node));
222 Link::createFromRoute('Home', '<front>'),
223 Link::createFromRoute('Forums', 'forum.index'),
224 Link::createFromRoute('Something', 'forum.page', ['taxonomy_term' => 1]),
226 $breadcrumb = $breadcrumb_builder->build($route_match);
227 $this->assertEquals($expected1, $breadcrumb->getLinks());
228 $this->assertEquals(['route'], $breadcrumb->getCacheContexts());
229 $this->assertEquals(['taxonomy_term:1', 'taxonomy_vocabulary:5'], $breadcrumb->getCacheTags());
230 $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
234 Link::createFromRoute('Home', '<front>'),
235 Link::createFromRoute('Forums', 'forum.index'),
236 Link::createFromRoute('Something else', 'forum.page', ['taxonomy_term' => 2]),
237 Link::createFromRoute('Something', 'forum.page', ['taxonomy_term' => 1]),
239 $breadcrumb = $breadcrumb_builder->build($route_match);
240 $this->assertEquals($expected2, $breadcrumb->getLinks());
241 $this->assertEquals(['route'], $breadcrumb->getCacheContexts());
242 $this->assertEquals(['taxonomy_term:1', 'taxonomy_term:2', 'taxonomy_vocabulary:5'], $breadcrumb->getCacheTags());
243 $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());