33acddff3c0870d779f47eae0a3e4f8c01820b53
[yaffs-website] / web / core / modules / forum / tests / src / Unit / Breadcrumb / ForumNodeBreadcrumbBuilderTest.php
1 <?php
2
3 namespace Drupal\Tests\forum\Unit\Breadcrumb;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Link;
7 use Drupal\taxonomy\TermStorageInterface;
8 use Drupal\Tests\UnitTestCase;
9 use Symfony\Component\DependencyInjection\Container;
10
11 /**
12  * @coversDefaultClass \Drupal\forum\Breadcrumb\ForumNodeBreadcrumbBuilder
13  * @group forum
14  */
15 class ForumNodeBreadcrumbBuilderTest extends UnitTestCase {
16
17   /**
18    * {@inheritdoc}
19    */
20   protected function setUp() {
21     parent::setUp();
22
23     $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
24       ->disableOriginalConstructor()
25       ->getMock();
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);
30   }
31
32   /**
33    * Tests ForumNodeBreadcrumbBuilder::applies().
34    *
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.
41    *
42    * @dataProvider providerTestApplies
43    * @covers ::applies
44    */
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
50     $forum_manager = $this->getMock('Drupal\forum\ForumManagerInterface');
51     $forum_manager->expects($this->any())
52       ->method('checkNodeType')
53       ->will($this->returnValue(TRUE));
54
55     $translation_manager = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface');
56
57     // Make an object to test.
58     $builder = $this->getMockBuilder('Drupal\forum\Breadcrumb\ForumNodeBreadcrumbBuilder')
59       ->setConstructorArgs(
60         [
61           $entity_manager,
62           $config_factory,
63           $forum_manager,
64           $translation_manager,
65         ]
66       )
67       ->setMethods(NULL)
68       ->getMock();
69
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));
77
78     $this->assertEquals($expected, $builder->applies($route_match));
79   }
80
81   /**
82    * Provides test data for testApplies().
83    *
84    * Note that this test is incomplete, because we can't mock NodeInterface.
85    *
86    * @return array
87    *   Array of datasets for testApplies(). Structured as such:
88    *   - ForumNodeBreadcrumbBuilder::applies() expected result.
89    *   - ForumNodeBreadcrumbBuilder::applies() $attributes input array.
90    */
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()
95       ->getMock();
96
97     return [
98       [
99         FALSE,
100       ],
101       [
102         FALSE,
103         'NOT.entity.node.canonical',
104       ],
105       [
106         FALSE,
107         'entity.node.canonical',
108       ],
109       [
110         FALSE,
111         'entity.node.canonical',
112         [['node', NULL]],
113       ],
114       [
115         TRUE,
116         'entity.node.canonical',
117         [['node', $mock_node]],
118       ],
119     ];
120   }
121
122   /**
123    * Tests ForumNodeBreadcrumbBuilder::build().
124    *
125    * @see \Drupal\forum\ForumNodeBreadcrumbBuilder::build()
126    * @covers ::build
127    */
128   public function testBuild() {
129     // Build all our dependencies, backwards.
130     $translation_manager = $this->getMockBuilder('Drupal\Core\StringTranslation\TranslationInterface')
131       ->disableOriginalConstructor()
132       ->getMock();
133
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();
141
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();
149
150     $forum_manager = $this->getMockBuilder('Drupal\forum\ForumManagerInterface')
151       ->disableOriginalConstructor()
152       ->getMock();
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]));
160
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())
169       ->method('load')
170       ->will($this->returnValueMap([
171         ['forums', $prophecy->reveal()],
172       ]));
173
174     $entity_manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManagerInterface')
175       ->disableOriginalConstructor()
176       ->getMock();
177     $entity_manager->expects($this->any())
178       ->method('getStorage')
179       ->will($this->returnValueMap([
180         ['taxonomy_vocabulary', $vocab_storage],
181         ['taxonomy_term', $term_storage],
182       ]));
183
184     $config_factory = $this->getConfigFactoryStub(
185       [
186         'forum.settings' => [
187           'vocabulary' => 'forums',
188         ],
189       ]
190     );
191
192     // Build a breadcrumb builder to test.
193     $breadcrumb_builder = $this->getMock(
194       'Drupal\forum\Breadcrumb\ForumNodeBreadcrumbBuilder', NULL, [
195         $entity_manager,
196         $config_factory,
197         $forum_manager,
198         $translation_manager,
199       ]
200     );
201
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);
207
208     // The forum node we need a breadcrumb back from.
209     $forum_node = $this->getMockBuilder('Drupal\node\Entity\Node')
210       ->disableOriginalConstructor()
211       ->getMock();
212
213     // Our data set.
214     $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
215     $route_match->expects($this->exactly(2))
216       ->method('getParameter')
217       ->with('node')
218       ->will($this->returnValue($forum_node));
219
220     // First test.
221     $expected1 = [
222       Link::createFromRoute('Home', '<front>'),
223       Link::createFromRoute('Forums', 'forum.index'),
224       Link::createFromRoute('Something', 'forum.page', ['taxonomy_term' => 1]),
225     ];
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());
231
232     // Second test.
233     $expected2 = [
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]),
238     ];
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());
244   }
245
246 }