summaryrefslogtreecommitdiffstats
path: root/core/tests/Drupal/Tests/Core/Menu/LocalTaskDefaultTest.php
blob: 573444b9bd8cacb8e3c9e68c53ffcea955b35956 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php

/**
 * @file
 * Contains \Drupal\Tests\Core\Menu\LocalTaskDefaultTest.
 */

namespace Drupal\Tests\Core\Menu;

use Drupal\Core\Menu\LocalTaskDefault;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;

/**
 * @coversDefaultClass \Drupal\Core\Menu\LocalTaskDefault
 * @group Menu
 */
class LocalTaskDefaultTest extends UnitTestCase {

  /**
   * The tested local task default plugin.
   *
   * @var \Drupal\Core\Menu\LocalTaskDefault
   */
  protected $localTaskBase;

  /**
   * The used plugin configuration.
   *
   * @var array
   */
  protected $config = array();

  /**
   * The used plugin ID.
   *
   * @var string
   */
  protected $pluginId = 'local_task_default';

  /**
   * The used plugin definition.
   *
   * @var array
   */
  protected $pluginDefinition = array(
    'id' => 'local_task_default',
  );

  /**
   * The mocked translator.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $stringTranslation;

  /**
   * The mocked route provider.
   *
   * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $routeProvider;

  protected function setUp() {
    parent::setUp();

    $this->stringTranslation = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface');
    $this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
  }

  /**
   * Setups the local task default.
   */
  protected function setupLocalTaskDefault() {
    $this->localTaskBase = new TestLocalTaskDefault($this->config, $this->pluginId, $this->pluginDefinition);
    $this->localTaskBase
      ->setRouteProvider($this->routeProvider)
      ->setStringTranslation($this->stringTranslation);

  }

  /**
   * @covers ::getRouteParameters
   */
  public function testGetRouteParametersForStaticRoute() {
    $this->pluginDefinition = array(
      'route_name' => 'test_route'
    );

    $this->routeProvider->expects($this->once())
      ->method('getRouteByName')
      ->with('test_route')
      ->will($this->returnValue(new Route('/test-route')));

    $this->setupLocalTaskDefault();

    $route_match = new RouteMatch('', new Route('/'));
    $this->assertEquals(array(), $this->localTaskBase->getRouteParameters($route_match));
  }

  /**
   * @covers ::getRouteParameters
   */
  public function testGetRouteParametersInPluginDefinitions() {
    $this->pluginDefinition = array(
      'route_name' => 'test_route',
      'route_parameters' => array('parameter' => 'example')
    );

    $this->routeProvider->expects($this->once())
      ->method('getRouteByName')
      ->with('test_route')
      ->will($this->returnValue(new Route('/test-route/{parameter}')));

    $this->setupLocalTaskDefault();

    $route_match = new RouteMatch('', new Route('/'));
    $this->assertEquals(array('parameter' => 'example'), $this->localTaskBase->getRouteParameters($route_match));
  }

  /**
   * @covers ::getRouteParameters
   */
  public function testGetRouteParametersForDynamicRouteWithNonUpcastedParameters() {
    $this->pluginDefinition = array(
      'route_name' => 'test_route'
    );

    $route = new Route('/test-route/{parameter}');
    $this->routeProvider->expects($this->once())
      ->method('getRouteByName')
      ->with('test_route')
      ->will($this->returnValue($route));

    $this->setupLocalTaskDefault();

    $route_match = new RouteMatch('', $route, array(), array('parameter' => 'example'));

    $this->assertEquals(array('parameter' => 'example'), $this->localTaskBase->getRouteParameters($route_match));
  }

  /**
   * Tests the getRouteParameters method for a route with upcasted parameters.
   *
   * @covers ::getRouteParameters
   */
  public function testGetRouteParametersForDynamicRouteWithUpcastedParameters() {
    $this->pluginDefinition = array(
      'route_name' => 'test_route'
    );

    $route = new Route('/test-route/{parameter}');
    $this->routeProvider->expects($this->once())
      ->method('getRouteByName')
      ->with('test_route')
      ->will($this->returnValue($route));

    $this->setupLocalTaskDefault();

    $route_match = new RouteMatch('', $route, array('parameter' => (object) 'example2'), array('parameter' => 'example'));
    $this->assertEquals(array('parameter' => 'example'), $this->localTaskBase->getRouteParameters($route_match));
  }

  /**
   * Defines a data provider for testGetWeight().
   *
   * @return array
   *   A list or test plugin definition and expected weight.
   */
  public function providerTestGetWeight() {
    return array(
      // Manually specify a weight, so this is used.
      array(array('weight' => 314), 'test_id', 314),
      // Ensure that a default tab gets a lower weight.
      array(
        array(
          'base_route' => 'local_task_default',
          'route_name' => 'local_task_default',
          'id' => 'local_task_default'
        ),
        'local_task_default',
        -10
      ),
      // If the base route is different from the route of the tab, ignore it.
      array(
        array(
          'base_route' => 'local_task_example',
          'route_name' => 'local_task_other',
          'id' => 'local_task_default'
        ),
        'local_task_default',
        0,
      ),
      // Ensure that a default tab of a derivative gets the default value.
      array(
        array(
          'base_route' => 'local_task_example',
          'id' => 'local_task_derivative_default:example_id',
          'route_name' => 'local_task_example',
        ),
        'local_task_derivative_default:example_id',
        -10,
      ),
    );
  }

  /**
   * @dataProvider providerTestGetWeight
   * @covers ::getWeight
   */
  public function testGetWeight($plugin_definition, $plugin_id, $expected_weight) {
    $this->pluginDefinition = $plugin_definition;
    $this->pluginId = $plugin_id;
    $this->setupLocalTaskDefault();

    $this->assertEquals($expected_weight, $this->localTaskBase->getWeight());
  }

  /**
   * @covers ::getActive
   * @covers ::setActive
   */
  public function testActive() {
    $this->setupLocalTaskDefault();

    $this->assertFalse($this->localTaskBase->getActive());
    $this->localTaskBase->setActive();
    $this->assertTrue($this->localTaskBase->getActive());
  }

  /**
   * @covers ::getTitle
   */
  public function testGetTitleWithoutContext() {
    $this->pluginDefinition['title'] = 'Example';
    $this->stringTranslation->expects($this->once())
      ->method('translate')
      ->with($this->pluginDefinition['title'], array(), array())
      ->will($this->returnValue('Example translated'));

    $this->setupLocalTaskDefault();
    $this->assertEquals('Example translated', $this->localTaskBase->getTitle());
  }

  /**
   * @covers ::getTitle
   */
  public function testGetTitleWithContext() {
    $this->pluginDefinition['title'] = 'Example';
    $this->pluginDefinition['title_context'] = 'context';
    $this->stringTranslation->expects($this->once())
      ->method('translate')
      ->with($this->pluginDefinition['title'], array(), array('context' => 'context'))
      ->will($this->returnValue('Example translated with context'));

    $this->setupLocalTaskDefault();
    $this->assertEquals('Example translated with context', $this->localTaskBase->getTitle());
  }

  /**
   * @covers ::getTitle
   */
  public function testGetTitleWithTitleArguments() {
    $this->pluginDefinition['title'] = 'Example @test';
    $this->pluginDefinition['title_arguments'] = array('@test' => 'value');
    $this->stringTranslation->expects($this->once())
      ->method('translate')
      ->with($this->pluginDefinition['title'], $this->arrayHasKey('@test'), array())
      ->will($this->returnValue('Example value'));

    $this->setupLocalTaskDefault();
    $request = new Request();
    $this->assertEquals('Example value', $this->localTaskBase->getTitle($request));
  }

  /**
   * @covers ::getOptions
   */
  public function testGetOptions() {
    $this->pluginDefinition['options'] = array(
      'attributes' => array('class' => array('example')),
    );

    $this->setupLocalTaskDefault();

    $route_match = new RouteMatch('', new Route('/'));
    $this->assertEquals($this->pluginDefinition['options'], $this->localTaskBase->getOptions($route_match));

    $this->localTaskBase->setActive(TRUE);

    $this->assertEquals(array(
      'attributes' => array(
        'class' => array(
          'example',
          'is-active'
        )
      )
    ), $this->localTaskBase->getOptions($route_match));
  }

}

class TestLocalTaskDefault extends LocalTaskDefault {
  public function setRouteProvider(RouteProviderInterface $route_provider) {
    $this->routeProvider = $route_provider;
    return $this;
  }
}