ContextualLinkManagerTest.php 14.3 KB
Newer Older
1
2
3
4
<?php

namespace Drupal\Tests\Core\Menu;

5
use Drupal\Core\Access\AccessResult;
6
use Drupal\Core\Language\Language;
7
use Drupal\Core\Menu\ContextualLinkDefault;
8
use Drupal\Tests\UnitTestCase;
9
use Symfony\Component\HttpFoundation\RequestStack;
10
11

/**
12
 * @coversDefaultClass \Drupal\Core\Menu\ContextualLinkManager
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
 * @group Menu
 */
class ContextualLinkManagerTest extends UnitTestCase {

  /**
   * The tested contextual link manager.
   *
   * @var \Drupal\Core\Menu\ContextualLinkManager
   */
  protected $contextualLinkManager;

  /**
   * The mocked controller resolver.
   *
   * @var \Symfony\Component\HttpKernel\Controller\ControllerResolverInterface|\Drupal\Core\\PHPUnit_Framework_MockObject_MockObject
   */
  protected $controllerResolver;

  /**
   * The mocked plugin discovery.
   *
   * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $pluginDiscovery;

  /**
   * The plugin factory used in the test.
   *
   * @var \Drupal\Component\Plugin\Factory\FactoryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $factory;

  /**
   * The cache backend used in the test.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $cacheBackend;

  /**
   * The mocked module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $moduleHandler;

  /**
   * The mocked access manager.
   *
62
   * @var \Drupal\Core\Access\AccessManagerInterface|\PHPUnit_Framework_MockObject_MockObject
63
64
65
   */
  protected $accessManager;

66
67
68
69
70
71
72
  /**
   * The mocked account.
   *
   * @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $account;

73
74
75
76
77
78
79
80
81
82
83
  protected function setUp() {
    $this->contextualLinkManager = $this
      ->getMockBuilder('Drupal\Core\Menu\ContextualLinkManager')
      ->disableOriginalConstructor()
      ->setMethods(NULL)
      ->getMock();

    $this->controllerResolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface');
    $this->pluginDiscovery = $this->getMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface');
    $this->factory = $this->getMock('Drupal\Component\Plugin\Factory\FactoryInterface');
    $this->cacheBackend = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
84
    $this->moduleHandler = $this->getMock('\Drupal\Core\Extension\ModuleHandlerInterface');
85
    $this->accessManager = $this->getMock('Drupal\Core\Access\AccessManagerInterface');
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
    $this->account = $this->getMock('Drupal\Core\Session\AccountInterface');

    $property = new \ReflectionProperty('Drupal\Core\Menu\ContextualLinkManager', 'controllerResolver');
    $property->setAccessible(TRUE);
    $property->setValue($this->contextualLinkManager, $this->controllerResolver);

    $property = new \ReflectionProperty('Drupal\Core\Menu\ContextualLinkManager', 'discovery');
    $property->setAccessible(TRUE);
    $property->setValue($this->contextualLinkManager, $this->pluginDiscovery);

    $property = new \ReflectionProperty('Drupal\Core\Menu\ContextualLinkManager', 'factory');
    $property->setAccessible(TRUE);
    $property->setValue($this->contextualLinkManager, $this->factory);

    $property = new \ReflectionProperty('Drupal\Core\Menu\ContextualLinkManager', 'account');
    $property->setAccessible(TRUE);
    $property->setValue($this->contextualLinkManager, $this->account);

    $property = new \ReflectionProperty('Drupal\Core\Menu\ContextualLinkManager', 'accessManager');
    $property->setAccessible(TRUE);
    $property->setValue($this->contextualLinkManager, $this->accessManager);

108
109
110
111
    $property = new \ReflectionProperty('Drupal\Core\Menu\ContextualLinkManager', 'moduleHandler');
    $property->setAccessible(TRUE);
    $property->setValue($this->contextualLinkManager, $this->moduleHandler);

112
    $language_manager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
113
    $language_manager->expects($this->any())
114
      ->method('getCurrentLanguage')
115
116
      ->will($this->returnValue(new Language(array('id' => 'en'))));

117
118
119
120
121
    $request_stack = new RequestStack();
    $property = new \ReflectionProperty('Drupal\Core\Menu\ContextualLinkManager', 'requestStack');
    $property->setAccessible(TRUE);
    $property->setValue($this->contextualLinkManager, $request_stack);

122
123
    $method = new \ReflectionMethod('Drupal\Core\Menu\ContextualLinkManager', 'alterInfo');
    $method->setAccessible(TRUE);
124
    $method->invoke($this->contextualLinkManager, 'contextual_links_plugins');
125

126
    $this->contextualLinkManager->setCacheBackend($this->cacheBackend, 'contextual_links_plugins:en');
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
  }

  /**
   * Tests the getContextualLinkPluginsByGroup method.
   *
   * @see \Drupal\Core\Menu\ContextualLinkManager::getContextualLinkPluginsByGroup()
   */
  public function testGetContextualLinkPluginsByGroup() {
    $definitions = array(
      'test_plugin1' => array(
        'id' => 'test_plugin1',
        'class' => '\Drupal\Core\Menu\ContextualLinkDefault',
        'group' => 'group1',
        'route_name' => 'test_route',
      ),
      'test_plugin2' => array(
        'id' => 'test_plugin2',
        'class' => '\Drupal\Core\Menu\ContextualLinkDefault',
        'group' => 'group1',
        'route_name' => 'test_route2',
      ),
      'test_plugin3' => array(
        'id' => 'test_plugin3',
        'class' => '\Drupal\Core\Menu\ContextualLinkDefault',
        'group' => 'group2',
        'route_name' => 'test_router3',
      ),
    );
    $this->pluginDiscovery->expects($this->once())
      ->method('getDefinitions')
      ->will($this->returnValue($definitions));

    // Test with a non existing group.
    $result = $this->contextualLinkManager->getContextualLinkPluginsByGroup('group_non_existing');
    $this->assertEmpty($result);

    $result = $this->contextualLinkManager->getContextualLinkPluginsByGroup('group1');
    $this->assertEquals(array('test_plugin1', 'test_plugin2'), array_keys($result));

    $result = $this->contextualLinkManager->getContextualLinkPluginsByGroup('group2');
    $this->assertEquals(array('test_plugin3'), array_keys($result));
  }

  /**
   * Tests the getContextualLinkPluginsByGroup method with a prefilled cache.
   */
  public function testGetContextualLinkPluginsByGroupWithCache() {
    $definitions = array(
      'test_plugin1' => array(
        'id' => 'test_plugin1',
        'class' => '\Drupal\Core\Menu\ContextualLinkDefault',
        'group' => 'group1',
        'route_name' => 'test_route',
      ),
      'test_plugin2' => array(
        'id' => 'test_plugin2',
        'class' => '\Drupal\Core\Menu\ContextualLinkDefault',
        'group' => 'group1',
        'route_name' => 'test_route2',
      ),
    );

    $this->cacheBackend->expects($this->once())
      ->method('get')
      ->with('contextual_links_plugins:en:group1')
      ->will($this->returnValue((object) array('data' => $definitions)));

    $result = $this->contextualLinkManager->getContextualLinkPluginsByGroup('group1');
    $this->assertEquals($definitions, $result);

    // Ensure that the static cache works, so no second cache get is executed.

    $result = $this->contextualLinkManager->getContextualLinkPluginsByGroup('group1');
    $this->assertEquals($definitions, $result);
  }

  /**
   * Tests processDefinition() by passing a plugin definition without a route.
   *
   * @see \Drupal\Core\Menu\ContextualLinkManager::processDefinition()
   *
   * @expectedException \Drupal\Component\Plugin\Exception\PluginException
   */
  public function testProcessDefinitionWithoutRoute() {
    $definition = array(
      'class' => '\Drupal\Core\Menu\ContextualLinkDefault',
      'group' => 'example',
      'id' => 'test_plugin',
    );
    $this->contextualLinkManager->processDefinition($definition, 'test_plugin');
  }

  /**
   * Tests processDefinition() by passing a plugin definition without a group.
   *
   * @see \Drupal\Core\Menu\ContextualLinkManager::processDefinition()
   *
   * @expectedException \Drupal\Component\Plugin\Exception\PluginException
   */
  public function testProcessDefinitionWithoutGroup() {
    $definition = array(
      'class' => '\Drupal\Core\Menu\ContextualLinkDefault',
      'route_name' => 'example',
      'id' => 'test_plugin',
    );
    $this->contextualLinkManager->processDefinition($definition, 'test_plugin');
  }

  /**
   * Tests the getContextualLinksArrayByGroup method.
   *
   * @see \Drupal\Core\Menu\ContextualLinkManager::getContextualLinksArrayByGroup()
   */
  public function testGetContextualLinksArrayByGroup() {
    $definitions = array(
      'test_plugin1' => array(
        'id' => 'test_plugin1',
        'class' => '\Drupal\Core\Menu\ContextualLinkDefault',
        'title' => 'Plugin 1',
        'weight' => 0,
        'group' => 'group1',
        'route_name' => 'test_route',
        'options' => array(),
      ),
      'test_plugin2' => array(
        'id' => 'test_plugin2',
        'class' => '\Drupal\Core\Menu\ContextualLinkDefault',
        'title' => 'Plugin 2',
        'weight' => 2,
        'group' => 'group1',
        'route_name' => 'test_route2',
        'options' => array('key' => 'value'),
      ),
      'test_plugin3' => array(
        'id' => 'test_plugin3',
        'class' => '\Drupal\Core\Menu\ContextualLinkDefault',
        'title' => 'Plugin 3',
        'weight' => 5,
        'group' => 'group2',
        'route_name' => 'test_router3',
        'options' => array(),
      ),
    );

    $this->pluginDiscovery->expects($this->once())
      ->method('getDefinitions')
      ->will($this->returnValue($definitions));

    $this->accessManager->expects($this->any())
      ->method('checkNamedRoute')
277
      ->will($this->returnValue(AccessResult::allowed()));
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348

    // Set up mocking of the plugin factory.
    $map = array();
    foreach ($definitions as $plugin_id => $definition) {
      $plugin = $this->getMock('Drupal\Core\Menu\ContextualLinkInterface');
      $plugin->expects($this->any())
        ->method('getRouteName')
        ->will($this->returnValue($definition['route_name']));
      $plugin->expects($this->any())
        ->method('getTitle')
        ->will($this->returnValue($definition['title']));
      $plugin->expects($this->any())
        ->method('getWeight')
        ->will($this->returnValue($definition['weight']));
      $plugin->expects($this->any())
        ->method('getOptions')
        ->will($this->returnValue($definition['options']));
      $map[] = array($plugin_id, array(), $plugin);
    }
    $this->factory->expects($this->any())
      ->method('createInstance')
      ->will($this->returnValueMap($map));

    $this->moduleHandler->expects($this->at(1))
      ->method('alter')
      ->with($this->equalTo('contextual_links'), new \PHPUnit_Framework_Constraint_Count(2), $this->equalTo('group1'), $this->equalTo(array('key' => 'value')));

    $result = $this->contextualLinkManager->getContextualLinksArrayByGroup('group1', array('key' => 'value'));
    $this->assertCount(2, $result);
    foreach (array('test_plugin1', 'test_plugin2') as $plugin_id) {
      $definition = $definitions[$plugin_id];
      $this->assertEquals($definition['weight'], $result[$plugin_id]['weight']);
      $this->assertEquals($definition['title'], $result[$plugin_id]['title']);
      $this->assertEquals($definition['route_name'], $result[$plugin_id]['route_name']);
    }
  }

  /**
   * Tests the access checking of the getContextualLinksArrayByGroup method.
   *
   * @see \Drupal\Core\Menu\ContextualLinkManager::getContextualLinksArrayByGroup()
   */
  public function testGetContextualLinksArrayByGroupAccessCheck() {
    $definitions = array(
      'test_plugin1' => array(
        'id' => 'test_plugin1',
        'class' => '\Drupal\Core\Menu\ContextualLinkDefault',
        'title' => 'Plugin 1',
        'weight' => 0,
        'group' => 'group1',
        'route_name' => 'test_route',
        'options' => array(),
      ),
      'test_plugin2' => array(
        'id' => 'test_plugin2',
        'class' => '\Drupal\Core\Menu\ContextualLinkDefault',
        'title' => 'Plugin 2',
        'weight' => 2,
        'group' => 'group1',
        'route_name' => 'test_route2',
        'options' => array('key' => 'value'),
      ),
    );

    $this->pluginDiscovery->expects($this->once())
      ->method('getDefinitions')
      ->will($this->returnValue($definitions));

    $this->accessManager->expects($this->any())
      ->method('checkNamedRoute')
      ->will($this->returnValueMap(array(
349
350
        array('test_route', array('key' => 'value'), $this->account, FALSE, TRUE),
        array('test_route2', array('key' => 'value'), $this->account, FALSE, FALSE),
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
      )));

    // Set up mocking of the plugin factory.
    $map = array();
    foreach ($definitions as $plugin_id => $definition) {
      $plugin = $this->getMock('Drupal\Core\Menu\ContextualLinkInterface');
      $plugin->expects($this->any())
        ->method('getRouteName')
        ->will($this->returnValue($definition['route_name']));
      $plugin->expects($this->any())
        ->method('getTitle')
        ->will($this->returnValue($definition['title']));
      $plugin->expects($this->any())
        ->method('getWeight')
        ->will($this->returnValue($definition['weight']));
      $plugin->expects($this->any())
        ->method('getOptions')
        ->will($this->returnValue($definition['options']));
      $map[] = array($plugin_id, array(), $plugin);
    }
    $this->factory->expects($this->any())
      ->method('createInstance')
      ->will($this->returnValueMap($map));

    $result = $this->contextualLinkManager->getContextualLinksArrayByGroup('group1', array('key' => 'value'));

    // Ensure that access checking was respected.
    $this->assertTrue(isset($result['test_plugin1']));
    $this->assertFalse(isset($result['test_plugin2']));
  }

  /**
   * Tests the plugins alter hook.
   */
  public function testPluginDefinitionAlter() {
    $definitions['test_plugin'] = array(
      'id' => 'test_plugin',
388
      'class' => ContextualLinkDefault::class,
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
      'title' => 'Plugin',
      'weight' => 2,
      'group' => 'group1',
      'route_name' => 'test_route',
      'options' => array('key' => 'value'),
    );

    $this->pluginDiscovery->expects($this->once())
      ->method('getDefinitions')
      ->will($this->returnValue($definitions));

    $this->moduleHandler->expects($this->once())
      ->method('alter')
      ->with('contextual_links_plugins', $definitions);

    $this->contextualLinkManager->getDefinition('test_plugin');
  }

}