84b91afe97d7322432d4c64d84bc0e54c8e094aa
[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    * Defines a data provider for testGetWeight().
167    *
168    * @return array
169    *   A list or test plugin definition and expected weight.
170    */
171   public function providerTestGetWeight() {
172     return [
173       // Manually specify a weight, so this is used.
174       [['weight' => 314], 'test_id', 314],
175       // Ensure that a default tab gets a lower weight.
176       [
177         [
178           'base_route' => 'local_task_default',
179           'route_name' => 'local_task_default',
180           'id' => 'local_task_default'
181         ],
182         'local_task_default',
183         -10
184       ],
185       // If the base route is different from the route of the tab, ignore it.
186       [
187         [
188           'base_route' => 'local_task_example',
189           'route_name' => 'local_task_other',
190           'id' => 'local_task_default'
191         ],
192         'local_task_default',
193         0,
194       ],
195       // Ensure that a default tab of a derivative gets the default value.
196       [
197         [
198           'base_route' => 'local_task_example',
199           'id' => 'local_task_derivative_default:example_id',
200           'route_name' => 'local_task_example',
201         ],
202         'local_task_derivative_default:example_id',
203         -10,
204       ],
205     ];
206   }
207
208   /**
209    * @dataProvider providerTestGetWeight
210    * @covers ::getWeight
211    */
212   public function testGetWeight($plugin_definition, $plugin_id, $expected_weight) {
213     $this->pluginDefinition = $plugin_definition;
214     $this->pluginId = $plugin_id;
215     $this->setupLocalTaskDefault();
216
217     $this->assertEquals($expected_weight, $this->localTaskBase->getWeight());
218   }
219
220   /**
221    * @covers ::getActive
222    * @covers ::setActive
223    */
224   public function testActive() {
225     $this->setupLocalTaskDefault();
226
227     $this->assertFalse($this->localTaskBase->getActive());
228     $this->localTaskBase->setActive();
229     $this->assertTrue($this->localTaskBase->getActive());
230   }
231
232   /**
233    * @covers ::getTitle
234    */
235   public function testGetTitle() {
236     $this->pluginDefinition['title'] = (new TranslatableMarkup('Example', [], [], $this->stringTranslation));
237     $this->stringTranslation->expects($this->once())
238       ->method('translateString')
239       ->with($this->pluginDefinition['title'])
240       ->will($this->returnValue('Example translated'));
241
242     $this->setupLocalTaskDefault();
243     $this->assertEquals('Example translated', $this->localTaskBase->getTitle());
244   }
245
246   /**
247    * @covers ::getTitle
248    */
249   public function testGetTitleWithContext() {
250     $title = 'Example';
251     $this->pluginDefinition['title'] = (new TranslatableMarkup($title, [], ['context' => 'context'], $this->stringTranslation));
252     $this->stringTranslation->expects($this->once())
253       ->method('translateString')
254       ->with($this->pluginDefinition['title'])
255       ->will($this->returnValue('Example translated with context'));
256
257     $this->setupLocalTaskDefault();
258     $this->assertEquals('Example translated with context', $this->localTaskBase->getTitle());
259   }
260
261   /**
262    * @covers ::getTitle
263    */
264   public function testGetTitleWithTitleArguments() {
265     $this->pluginDefinition['title'] = (new TranslatableMarkup('Example @test', ['@test' => 'value'], [], $this->stringTranslation));
266     $this->stringTranslation->expects($this->once())
267       ->method('translateString')
268       ->with($this->pluginDefinition['title'])
269       ->will($this->returnValue('Example value'));
270
271     $this->setupLocalTaskDefault();
272     $this->assertEquals('Example value', $this->localTaskBase->getTitle());
273   }
274
275   /**
276    * @covers ::getOptions
277    */
278   public function testGetOptions() {
279     $this->pluginDefinition['options'] = [
280       'attributes' => ['class' => ['example']],
281     ];
282
283     $this->setupLocalTaskDefault();
284
285     $route_match = new RouteMatch('', new Route('/'));
286     $this->assertEquals($this->pluginDefinition['options'], $this->localTaskBase->getOptions($route_match));
287
288     $this->localTaskBase->setActive(TRUE);
289
290     $this->assertEquals([
291       'attributes' => [
292         'class' => [
293           'example',
294           'is-active'
295         ]
296       ]
297     ], $this->localTaskBase->getOptions($route_match));
298   }
299
300   /**
301    * @covers ::getCacheContexts
302    * @covers ::getCacheTags
303    * @covers ::getCacheMaxAge
304    */
305   public function testCacheabilityMetadata() {
306     $this->pluginDefinition['cache_contexts'] = ['route'];
307     $this->pluginDefinition['cache_tags'] = ['kitten'];
308     $this->pluginDefinition['cache_max_age'] = 3600;
309
310     $this->setupLocalTaskDefault();
311
312     $this->assertEquals(['route'], $this->localTaskBase->getCacheContexts());
313     $this->assertEquals(['kitten'], $this->localTaskBase->getCacheTags());
314     $this->assertEquals(3600, $this->localTaskBase->getCacheMaxAge());
315   }
316
317 }
318
319 class TestLocalTaskDefault extends LocalTaskDefault {
320   public function setRouteProvider(RouteProviderInterface $route_provider) {
321     $this->routeProvider = $route_provider;
322     return $this;
323   }
324
325 }