2f59ee1acb8b31b41db2d47356302f1d31a0af9b
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Menu / LocalTaskDefaultTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Core\Menu\LocalTaskDefaultTest.
6  */
7
8 namespace Drupal\Tests\Core\Menu;
9
10 use Drupal\Core\Menu\LocalTaskDefault;
11 use Drupal\Core\Routing\RouteMatch;
12 use Drupal\Core\Routing\RouteProviderInterface;
13 use Drupal\Core\StringTranslation\TranslatableMarkup;
14 use Drupal\Tests\UnitTestCase;
15 use Symfony\Component\Routing\Route;
16
17 /**
18  * @coversDefaultClass \Drupal\Core\Menu\LocalTaskDefault
19  * @group Menu
20  */
21 class LocalTaskDefaultTest extends UnitTestCase {
22
23   /**
24    * The tested local task default plugin.
25    *
26    * @var \Drupal\Core\Menu\LocalTaskDefault
27    */
28   protected $localTaskBase;
29
30   /**
31    * The used plugin configuration.
32    *
33    * @var array
34    */
35   protected $config = [];
36
37   /**
38    * The used plugin ID.
39    *
40    * @var string
41    */
42   protected $pluginId = 'local_task_default';
43
44   /**
45    * The used plugin definition.
46    *
47    * @var array
48    */
49   protected $pluginDefinition = [
50     'id' => 'local_task_default',
51   ];
52
53   /**
54    * The mocked translator.
55    *
56    * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
57    */
58   protected $stringTranslation;
59
60   /**
61    * The mocked route provider.
62    *
63    * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
64    */
65   protected $routeProvider;
66
67   protected function setUp() {
68     parent::setUp();
69
70     $this->stringTranslation = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface');
71     $this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
72   }
73
74   /**
75    * Setups the local task default.
76    */
77   protected function setupLocalTaskDefault() {
78     $this->localTaskBase = new TestLocalTaskDefault($this->config, $this->pluginId, $this->pluginDefinition);
79     $this->localTaskBase
80       ->setRouteProvider($this->routeProvider);
81   }
82
83   /**
84    * @covers ::getRouteParameters
85    */
86   public function testGetRouteParametersForStaticRoute() {
87     $this->pluginDefinition = [
88       'route_name' => 'test_route',
89     ];
90
91     $this->routeProvider->expects($this->once())
92       ->method('getRouteByName')
93       ->with('test_route')
94       ->will($this->returnValue(new Route('/test-route')));
95
96     $this->setupLocalTaskDefault();
97
98     $route_match = new RouteMatch('', new Route('/'));
99     $this->assertEquals([], $this->localTaskBase->getRouteParameters($route_match));
100   }
101
102   /**
103    * @covers ::getRouteParameters
104    */
105   public function testGetRouteParametersInPluginDefinitions() {
106     $this->pluginDefinition = [
107       'route_name' => 'test_route',
108       'route_parameters' => ['parameter' => 'example'],
109     ];
110
111     $this->routeProvider->expects($this->once())
112       ->method('getRouteByName')
113       ->with('test_route')
114       ->will($this->returnValue(new Route('/test-route/{parameter}')));
115
116     $this->setupLocalTaskDefault();
117
118     $route_match = new RouteMatch('', new Route('/'));
119     $this->assertEquals(['parameter' => 'example'], $this->localTaskBase->getRouteParameters($route_match));
120   }
121
122   /**
123    * @covers ::getRouteParameters
124    */
125   public function testGetRouteParametersForDynamicRouteWithNonUpcastedParameters() {
126     $this->pluginDefinition = [
127       'route_name' => 'test_route',
128     ];
129
130     $route = new Route('/test-route/{parameter}');
131     $this->routeProvider->expects($this->once())
132       ->method('getRouteByName')
133       ->with('test_route')
134       ->will($this->returnValue($route));
135
136     $this->setupLocalTaskDefault();
137
138     $route_match = new RouteMatch('', $route, [], ['parameter' => 'example']);
139
140     $this->assertEquals(['parameter' => 'example'], $this->localTaskBase->getRouteParameters($route_match));
141   }
142
143   /**
144    * Tests the getRouteParameters method for a route with upcasted parameters.
145    *
146    * @covers ::getRouteParameters
147    */
148   public function testGetRouteParametersForDynamicRouteWithUpcastedParameters() {
149     $this->pluginDefinition = [
150       'route_name' => 'test_route',
151     ];
152
153     $route = new Route('/test-route/{parameter}');
154     $this->routeProvider->expects($this->once())
155       ->method('getRouteByName')
156       ->with('test_route')
157       ->will($this->returnValue($route));
158
159     $this->setupLocalTaskDefault();
160
161     $route_match = new RouteMatch('', $route, ['parameter' => (object) 'example2'], ['parameter' => 'example']);
162     $this->assertEquals(['parameter' => 'example'], $this->localTaskBase->getRouteParameters($route_match));
163   }
164
165   /**
166    * Tests the getRouteParameters method for a route with upcasted parameters.
167    *
168    * @covers ::getRouteParameters
169    */
170   public function testGetRouteParametersForDynamicRouteWithUpcastedParametersEmptyRawParameters() {
171     $this->pluginDefinition = [
172       'route_name' => 'test_route',
173     ];
174
175     $route = new Route('/test-route/{parameter}');
176     $this->routeProvider->expects($this->once())
177       ->method('getRouteByName')
178       ->with('test_route')
179       ->will($this->returnValue($route));
180
181     $this->setupLocalTaskDefault();
182
183     $route_match = new RouteMatch('', $route, ['parameter' => (object) 'example2']);
184     $this->assertEquals(['parameter' => (object) 'example2'], $this->localTaskBase->getRouteParameters($route_match));
185   }
186
187   /**
188    * Defines a data provider for testGetWeight().
189    *
190    * @return array
191    *   A list or test plugin definition and expected weight.
192    */
193   public function providerTestGetWeight() {
194     return [
195       // Manually specify a weight, so this is used.
196       [['weight' => 314], 'test_id', 314],
197       // Ensure that a default tab gets a lower weight.
198       [
199         [
200           'base_route' => 'local_task_default',
201           'route_name' => 'local_task_default',
202           'id' => 'local_task_default',
203         ],
204         'local_task_default',
205         -10,
206       ],
207       // If the base route is different from the route of the tab, ignore it.
208       [
209         [
210           'base_route' => 'local_task_example',
211           'route_name' => 'local_task_other',
212           'id' => 'local_task_default',
213         ],
214         'local_task_default',
215         0,
216       ],
217       // Ensure that a default tab of a derivative gets the default value.
218       [
219         [
220           'base_route' => 'local_task_example',
221           'id' => 'local_task_derivative_default:example_id',
222           'route_name' => 'local_task_example',
223         ],
224         'local_task_derivative_default:example_id',
225         -10,
226       ],
227     ];
228   }
229
230   /**
231    * @dataProvider providerTestGetWeight
232    * @covers ::getWeight
233    */
234   public function testGetWeight($plugin_definition, $plugin_id, $expected_weight) {
235     $this->pluginDefinition = $plugin_definition;
236     $this->pluginId = $plugin_id;
237     $this->setupLocalTaskDefault();
238
239     $this->assertEquals($expected_weight, $this->localTaskBase->getWeight());
240   }
241
242   /**
243    * @covers ::getActive
244    * @covers ::setActive
245    */
246   public function testActive() {
247     $this->setupLocalTaskDefault();
248
249     $this->assertFalse($this->localTaskBase->getActive());
250     $this->localTaskBase->setActive();
251     $this->assertTrue($this->localTaskBase->getActive());
252   }
253
254   /**
255    * @covers ::getTitle
256    */
257   public function testGetTitle() {
258     $this->pluginDefinition['title'] = (new TranslatableMarkup('Example', [], [], $this->stringTranslation));
259     $this->stringTranslation->expects($this->once())
260       ->method('translateString')
261       ->with($this->pluginDefinition['title'])
262       ->will($this->returnValue('Example translated'));
263
264     $this->setupLocalTaskDefault();
265     $this->assertEquals('Example translated', $this->localTaskBase->getTitle());
266   }
267
268   /**
269    * @covers ::getTitle
270    */
271   public function testGetTitleWithContext() {
272     $title = 'Example';
273     $this->pluginDefinition['title'] = (new TranslatableMarkup($title, [], ['context' => 'context'], $this->stringTranslation));
274     $this->stringTranslation->expects($this->once())
275       ->method('translateString')
276       ->with($this->pluginDefinition['title'])
277       ->will($this->returnValue('Example translated with context'));
278
279     $this->setupLocalTaskDefault();
280     $this->assertEquals('Example translated with context', $this->localTaskBase->getTitle());
281   }
282
283   /**
284    * @covers ::getTitle
285    */
286   public function testGetTitleWithTitleArguments() {
287     $this->pluginDefinition['title'] = (new TranslatableMarkup('Example @test', ['@test' => 'value'], [], $this->stringTranslation));
288     $this->stringTranslation->expects($this->once())
289       ->method('translateString')
290       ->with($this->pluginDefinition['title'])
291       ->will($this->returnValue('Example value'));
292
293     $this->setupLocalTaskDefault();
294     $this->assertEquals('Example value', $this->localTaskBase->getTitle());
295   }
296
297   /**
298    * @covers ::getOptions
299    */
300   public function testGetOptions() {
301     $this->pluginDefinition['options'] = [
302       'attributes' => ['class' => ['example']],
303     ];
304
305     $this->setupLocalTaskDefault();
306
307     $route_match = new RouteMatch('', new Route('/'));
308     $this->assertEquals($this->pluginDefinition['options'], $this->localTaskBase->getOptions($route_match));
309
310     $this->localTaskBase->setActive(TRUE);
311
312     $this->assertEquals([
313       'attributes' => [
314         'class' => [
315           'example',
316           'is-active',
317         ],
318       ],
319     ], $this->localTaskBase->getOptions($route_match));
320   }
321
322   /**
323    * @covers ::getCacheContexts
324    * @covers ::getCacheTags
325    * @covers ::getCacheMaxAge
326    */
327   public function testCacheabilityMetadata() {
328     $this->pluginDefinition['cache_contexts'] = ['route'];
329     $this->pluginDefinition['cache_tags'] = ['kitten'];
330     $this->pluginDefinition['cache_max_age'] = 3600;
331
332     $this->setupLocalTaskDefault();
333
334     $this->assertEquals(['route'], $this->localTaskBase->getCacheContexts());
335     $this->assertEquals(['kitten'], $this->localTaskBase->getCacheTags());
336     $this->assertEquals(3600, $this->localTaskBase->getCacheMaxAge());
337   }
338
339 }
340
341 class TestLocalTaskDefault extends LocalTaskDefault {
342
343   public function setRouteProvider(RouteProviderInterface $route_provider) {
344     $this->routeProvider = $route_provider;
345     return $this;
346   }
347
348 }