5 * Contains \Drupal\Tests\Core\Menu\LocalTaskDefaultTest.
8 namespace Drupal\Tests\Core\Menu;
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;
18 * @coversDefaultClass \Drupal\Core\Menu\LocalTaskDefault
21 class LocalTaskDefaultTest extends UnitTestCase {
24 * The tested local task default plugin.
26 * @var \Drupal\Core\Menu\LocalTaskDefault
28 protected $localTaskBase;
31 * The used plugin configuration.
35 protected $config = [];
42 protected $pluginId = 'local_task_default';
45 * The used plugin definition.
49 protected $pluginDefinition = [
50 'id' => 'local_task_default',
54 * The mocked translator.
56 * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
58 protected $stringTranslation;
61 * The mocked route provider.
63 * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
65 protected $routeProvider;
67 protected function setUp() {
70 $this->stringTranslation = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface');
71 $this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
75 * Setups the local task default.
77 protected function setupLocalTaskDefault() {
78 $this->localTaskBase = new TestLocalTaskDefault($this->config, $this->pluginId, $this->pluginDefinition);
80 ->setRouteProvider($this->routeProvider);
84 * @covers ::getRouteParameters
86 public function testGetRouteParametersForStaticRoute() {
87 $this->pluginDefinition = [
88 'route_name' => 'test_route',
91 $this->routeProvider->expects($this->once())
92 ->method('getRouteByName')
94 ->will($this->returnValue(new Route('/test-route')));
96 $this->setupLocalTaskDefault();
98 $route_match = new RouteMatch('', new Route('/'));
99 $this->assertEquals([], $this->localTaskBase->getRouteParameters($route_match));
103 * @covers ::getRouteParameters
105 public function testGetRouteParametersInPluginDefinitions() {
106 $this->pluginDefinition = [
107 'route_name' => 'test_route',
108 'route_parameters' => ['parameter' => 'example'],
111 $this->routeProvider->expects($this->once())
112 ->method('getRouteByName')
114 ->will($this->returnValue(new Route('/test-route/{parameter}')));
116 $this->setupLocalTaskDefault();
118 $route_match = new RouteMatch('', new Route('/'));
119 $this->assertEquals(['parameter' => 'example'], $this->localTaskBase->getRouteParameters($route_match));
123 * @covers ::getRouteParameters
125 public function testGetRouteParametersForDynamicRouteWithNonUpcastedParameters() {
126 $this->pluginDefinition = [
127 'route_name' => 'test_route',
130 $route = new Route('/test-route/{parameter}');
131 $this->routeProvider->expects($this->once())
132 ->method('getRouteByName')
134 ->will($this->returnValue($route));
136 $this->setupLocalTaskDefault();
138 $route_match = new RouteMatch('', $route, [], ['parameter' => 'example']);
140 $this->assertEquals(['parameter' => 'example'], $this->localTaskBase->getRouteParameters($route_match));
144 * Tests the getRouteParameters method for a route with upcasted parameters.
146 * @covers ::getRouteParameters
148 public function testGetRouteParametersForDynamicRouteWithUpcastedParameters() {
149 $this->pluginDefinition = [
150 'route_name' => 'test_route',
153 $route = new Route('/test-route/{parameter}');
154 $this->routeProvider->expects($this->once())
155 ->method('getRouteByName')
157 ->will($this->returnValue($route));
159 $this->setupLocalTaskDefault();
161 $route_match = new RouteMatch('', $route, ['parameter' => (object) 'example2'], ['parameter' => 'example']);
162 $this->assertEquals(['parameter' => 'example'], $this->localTaskBase->getRouteParameters($route_match));
166 * Tests the getRouteParameters method for a route with upcasted parameters.
168 * @covers ::getRouteParameters
170 public function testGetRouteParametersForDynamicRouteWithUpcastedParametersEmptyRawParameters() {
171 $this->pluginDefinition = [
172 'route_name' => 'test_route',
175 $route = new Route('/test-route/{parameter}');
176 $this->routeProvider->expects($this->once())
177 ->method('getRouteByName')
179 ->will($this->returnValue($route));
181 $this->setupLocalTaskDefault();
183 $route_match = new RouteMatch('', $route, ['parameter' => (object) 'example2']);
184 $this->assertEquals(['parameter' => (object) 'example2'], $this->localTaskBase->getRouteParameters($route_match));
188 * Defines a data provider for testGetWeight().
191 * A list or test plugin definition and expected weight.
193 public function providerTestGetWeight() {
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.
200 'base_route' => 'local_task_default',
201 'route_name' => 'local_task_default',
202 'id' => 'local_task_default',
204 'local_task_default',
207 // If the base route is different from the route of the tab, ignore it.
210 'base_route' => 'local_task_example',
211 'route_name' => 'local_task_other',
212 'id' => 'local_task_default',
214 'local_task_default',
217 // Ensure that a default tab of a derivative gets the default value.
220 'base_route' => 'local_task_example',
221 'id' => 'local_task_derivative_default:example_id',
222 'route_name' => 'local_task_example',
224 'local_task_derivative_default:example_id',
231 * @dataProvider providerTestGetWeight
232 * @covers ::getWeight
234 public function testGetWeight($plugin_definition, $plugin_id, $expected_weight) {
235 $this->pluginDefinition = $plugin_definition;
236 $this->pluginId = $plugin_id;
237 $this->setupLocalTaskDefault();
239 $this->assertEquals($expected_weight, $this->localTaskBase->getWeight());
243 * @covers ::getActive
244 * @covers ::setActive
246 public function testActive() {
247 $this->setupLocalTaskDefault();
249 $this->assertFalse($this->localTaskBase->getActive());
250 $this->localTaskBase->setActive();
251 $this->assertTrue($this->localTaskBase->getActive());
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'));
264 $this->setupLocalTaskDefault();
265 $this->assertEquals('Example translated', $this->localTaskBase->getTitle());
271 public function testGetTitleWithContext() {
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'));
279 $this->setupLocalTaskDefault();
280 $this->assertEquals('Example translated with context', $this->localTaskBase->getTitle());
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'));
293 $this->setupLocalTaskDefault();
294 $this->assertEquals('Example value', $this->localTaskBase->getTitle());
298 * @covers ::getOptions
300 public function testGetOptions() {
301 $this->pluginDefinition['options'] = [
302 'attributes' => ['class' => ['example']],
305 $this->setupLocalTaskDefault();
307 $route_match = new RouteMatch('', new Route('/'));
308 $this->assertEquals($this->pluginDefinition['options'], $this->localTaskBase->getOptions($route_match));
310 $this->localTaskBase->setActive(TRUE);
312 $this->assertEquals([
319 ], $this->localTaskBase->getOptions($route_match));
323 * @covers ::getCacheContexts
324 * @covers ::getCacheTags
325 * @covers ::getCacheMaxAge
327 public function testCacheabilityMetadata() {
328 $this->pluginDefinition['cache_contexts'] = ['route'];
329 $this->pluginDefinition['cache_tags'] = ['kitten'];
330 $this->pluginDefinition['cache_max_age'] = 3600;
332 $this->setupLocalTaskDefault();
334 $this->assertEquals(['route'], $this->localTaskBase->getCacheContexts());
335 $this->assertEquals(['kitten'], $this->localTaskBase->getCacheTags());
336 $this->assertEquals(3600, $this->localTaskBase->getCacheMaxAge());
341 class TestLocalTaskDefault extends LocalTaskDefault {
343 public function setRouteProvider(RouteProviderInterface $route_provider) {
344 $this->routeProvider = $route_provider;