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 * Defines a data provider for testGetWeight().
169 * A list or test plugin definition and expected weight.
171 public function providerTestGetWeight() {
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.
178 'base_route' => 'local_task_default',
179 'route_name' => 'local_task_default',
180 'id' => 'local_task_default'
182 'local_task_default',
185 // If the base route is different from the route of the tab, ignore it.
188 'base_route' => 'local_task_example',
189 'route_name' => 'local_task_other',
190 'id' => 'local_task_default'
192 'local_task_default',
195 // Ensure that a default tab of a derivative gets the default value.
198 'base_route' => 'local_task_example',
199 'id' => 'local_task_derivative_default:example_id',
200 'route_name' => 'local_task_example',
202 'local_task_derivative_default:example_id',
209 * @dataProvider providerTestGetWeight
210 * @covers ::getWeight
212 public function testGetWeight($plugin_definition, $plugin_id, $expected_weight) {
213 $this->pluginDefinition = $plugin_definition;
214 $this->pluginId = $plugin_id;
215 $this->setupLocalTaskDefault();
217 $this->assertEquals($expected_weight, $this->localTaskBase->getWeight());
221 * @covers ::getActive
222 * @covers ::setActive
224 public function testActive() {
225 $this->setupLocalTaskDefault();
227 $this->assertFalse($this->localTaskBase->getActive());
228 $this->localTaskBase->setActive();
229 $this->assertTrue($this->localTaskBase->getActive());
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'));
242 $this->setupLocalTaskDefault();
243 $this->assertEquals('Example translated', $this->localTaskBase->getTitle());
249 public function testGetTitleWithContext() {
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'));
257 $this->setupLocalTaskDefault();
258 $this->assertEquals('Example translated with context', $this->localTaskBase->getTitle());
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'));
271 $this->setupLocalTaskDefault();
272 $this->assertEquals('Example value', $this->localTaskBase->getTitle());
276 * @covers ::getOptions
278 public function testGetOptions() {
279 $this->pluginDefinition['options'] = [
280 'attributes' => ['class' => ['example']],
283 $this->setupLocalTaskDefault();
285 $route_match = new RouteMatch('', new Route('/'));
286 $this->assertEquals($this->pluginDefinition['options'], $this->localTaskBase->getOptions($route_match));
288 $this->localTaskBase->setActive(TRUE);
290 $this->assertEquals([
297 ], $this->localTaskBase->getOptions($route_match));
301 * @covers ::getCacheContexts
302 * @covers ::getCacheTags
303 * @covers ::getCacheMaxAge
305 public function testCacheabilityMetadata() {
306 $this->pluginDefinition['cache_contexts'] = ['route'];
307 $this->pluginDefinition['cache_tags'] = ['kitten'];
308 $this->pluginDefinition['cache_max_age'] = 3600;
310 $this->setupLocalTaskDefault();
312 $this->assertEquals(['route'], $this->localTaskBase->getCacheContexts());
313 $this->assertEquals(['kitten'], $this->localTaskBase->getCacheTags());
314 $this->assertEquals(3600, $this->localTaskBase->getCacheMaxAge());
319 class TestLocalTaskDefault extends LocalTaskDefault {
320 public function setRouteProvider(RouteProviderInterface $route_provider) {
321 $this->routeProvider = $route_provider;