47a0113d1f8874a26e2688234eaa0b8983e7344b
[yaffs-website] / web / core / modules / forum / tests / src / Unit / Breadcrumb / ForumListingBreadcrumbBuilderTest.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\ForumListingBreadcrumbBuilder
13  * @group forum
14  */
15 class ForumListingBreadcrumbBuilderTest 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 ForumListingBreadcrumbBuilder::applies().
34    *
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.
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     $forum_manager = $this->getMock('Drupal\forum\ForumManagerInterface');
50     $translation_manager = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface');
51
52     // Make an object to test.
53     $builder = $this->getMockBuilder('Drupal\forum\Breadcrumb\ForumListingBreadcrumbBuilder')
54       ->setConstructorArgs([
55         $entity_manager,
56         $config_factory,
57         $forum_manager,
58         $translation_manager,
59       ])
60       ->setMethods(NULL)
61       ->getMock();
62
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));
70
71     $this->assertEquals($expected, $builder->applies($route_match));
72   }
73
74   /**
75    * Provides test data for testApplies().
76    *
77    * @return array
78    *   Array of datasets for testApplies(). Structured as such:
79    *   - ForumListBreadcrumbBuilder::applies() expected result.
80    *   - ForumListBreadcrumbBuilder::applies() $attributes input array.
81    */
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()
86       ->getMock();
87
88     return [
89       [
90         FALSE,
91       ],
92       [
93         FALSE,
94         'NOT.forum.page',
95       ],
96       [
97         FALSE,
98         'forum.page',
99       ],
100       [
101         TRUE,
102         'forum.page',
103         [['taxonomy_term', 'anything']],
104       ],
105       [
106         TRUE,
107         'forum.page',
108         [['taxonomy_term', $mock_term]],
109       ],
110     ];
111   }
112
113   /**
114    * Tests ForumListingBreadcrumbBuilder::build().
115    *
116    * @see \Drupal\forum\ForumListingBreadcrumbBuilder::build()
117    *
118    * @covers ::build
119    */
120   public function testBuild() {
121     // Build all our dependencies, backwards.
122     $translation_manager = $this->getMockBuilder('Drupal\Core\StringTranslation\TranslationInterface')
123       ->disableOriginalConstructor()
124       ->getMock();
125
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();
133
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();
141
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]));
149
150     // The root forum.
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())
159       ->method('load')
160       ->will($this->returnValueMap([
161         ['forums', $prophecy->reveal()],
162       ]));
163
164     $entity_manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManagerInterface')
165       ->disableOriginalConstructor()
166       ->getMock();
167     $entity_manager->expects($this->any())
168       ->method('getStorage')
169       ->will($this->returnValueMap([
170         ['taxonomy_vocabulary', $vocab_storage],
171         ['taxonomy_term', $term_storage],
172       ]));
173
174     $config_factory = $this->getConfigFactoryStub(
175       [
176         'forum.settings' => [
177           'vocabulary' => 'forums',
178         ],
179       ]
180     );
181
182     $forum_manager = $this->getMock('Drupal\forum\ForumManagerInterface');
183
184     // Build a breadcrumb builder to test.
185     $breadcrumb_builder = $this->getMock(
186       'Drupal\forum\Breadcrumb\ForumListingBreadcrumbBuilder', NULL, [
187         $entity_manager,
188         $config_factory,
189         $forum_manager,
190         $translation_manager,
191       ]
192     );
193
194     // Add a translation manager for t().
195     $translation_manager = $this->getStringTranslationStub();
196     $breadcrumb_builder->setStringTranslation($translation_manager);
197
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();
206
207     // Our data set.
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));
213
214     // First test.
215     $expected1 = [
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]),
219     ];
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());
225
226     // Second test.
227     $expected2 = [
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]),
232     ];
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());
238
239   }
240
241 }