More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Menu / LocalActionManagerTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Core\Menu\LocalActionManagerTest.
6  */
7
8 namespace Drupal\Tests\Core\Menu;
9
10 use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
11 use Drupal\Component\Plugin\Factory\FactoryInterface;
12 use Drupal\Core\Access\AccessManagerInterface;
13 use Drupal\Core\Access\AccessResult;
14 use Drupal\Core\Access\AccessResultForbidden;
15 use Drupal\Core\Cache\CacheBackendInterface;
16 use Drupal\Core\Extension\ModuleHandlerInterface;
17 use Drupal\Core\Menu\LocalActionManager;
18 use Drupal\Core\Routing\RouteMatchInterface;
19 use Drupal\Core\Routing\RouteProviderInterface;
20 use Drupal\Core\Session\AccountInterface;
21 use Drupal\Core\Url;
22 use Drupal\Tests\UnitTestCase;
23 use Symfony\Component\HttpFoundation\Request;
24 use Symfony\Component\HttpFoundation\RequestStack;
25 use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
26
27 /**
28  * @coversDefaultClass \Drupal\Core\Menu\LocalActionManager
29  * @group Menu
30  */
31 class LocalActionManagerTest extends UnitTestCase {
32
33   /**
34    * The mocked controller resolver.
35    *
36    * @var \Drupal\Core\Controller\ControllerResolverInterface|\PHPUnit_Framework_MockObject_MockObject
37    */
38   protected $controllerResolver;
39
40   /**
41    * The mocked request.
42    *
43    * @var \Symfony\Component\HttpFoundation\Request|\PHPUnit_Framework_MockObject_MockObject
44    */
45   protected $request;
46
47   /**
48    * The mocked module handler.
49    *
50    * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
51    */
52   protected $moduleHandler;
53
54   /**
55    * The mocked router provider.
56    *
57    * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
58    */
59   protected $routeProvider;
60
61   /**
62    * The mocked cache backend.
63    *
64    * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
65    */
66   protected $cacheBackend;
67
68   /**
69    * The mocked access manager.
70    *
71    * @var \Drupal\Core\Access\AccessManagerInterface|\PHPUnit_Framework_MockObject_MockObject
72    */
73   protected $accessManager;
74
75   /**
76    * The mocked account.
77    *
78    * @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
79    */
80   protected $account;
81
82   /**
83    * The mocked factory.
84    *
85    * @var \Drupal\Component\Plugin\Factory\FactoryInterface|\PHPUnit_Framework_MockObject_MockObject
86    */
87   protected $factory;
88
89   /**
90    * The mocked plugin discovery.
91    *
92    * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
93    */
94   protected $discovery;
95
96   /**
97    * The tested local action manager
98    *
99    * @var \Drupal\Tests\Core\Menu\TestLocalActionManager
100    */
101   protected $localActionManager;
102
103   /**
104    * {@inheritdoc}
105    */
106   protected function setUp() {
107     $this->controllerResolver = $this->getMock('Drupal\Core\Controller\ControllerResolverInterface');
108     $this->request = $this->getMock('Symfony\Component\HttpFoundation\Request');
109     $this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
110     $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
111     $this->cacheBackend = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
112
113     $access_result = new AccessResultForbidden();
114     $this->accessManager = $this->getMock('Drupal\Core\Access\AccessManagerInterface');
115     $this->accessManager->expects($this->any())
116       ->method('checkNamedRoute')
117       ->willReturn($access_result);
118     $this->account = $this->getMock('Drupal\Core\Session\AccountInterface');
119     $this->discovery = $this->getMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface');
120     $this->factory = $this->getMock('Drupal\Component\Plugin\Factory\FactoryInterface');
121     $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
122
123     $this->localActionManager = new TestLocalActionManager($this->controllerResolver, $this->request, $route_match, $this->routeProvider, $this->moduleHandler, $this->cacheBackend, $this->accessManager, $this->account, $this->discovery, $this->factory);
124   }
125
126   /**
127    * @covers ::getTitle
128    */
129   public function testGetTitle() {
130     $local_action = $this->getMock('Drupal\Core\Menu\LocalActionInterface');
131     $local_action->expects($this->once())
132       ->method('getTitle')
133       ->with('test');
134
135     $this->controllerResolver->expects($this->once())
136       ->method('getArguments')
137       ->with($this->request, [$local_action, 'getTitle'])
138       ->will($this->returnValue(['test']));
139
140     $this->localActionManager->getTitle($local_action);
141   }
142
143   /**
144    * @covers ::getActionsForRoute
145    *
146    * @dataProvider getActionsForRouteProvider
147    */
148   public function testGetActionsForRoute($route_appears, array $plugin_definitions, array $expected_actions) {
149     $this->discovery->expects($this->any())
150       ->method('getDefinitions')
151       ->will($this->returnValue($plugin_definitions));
152     $map = [];
153     foreach ($plugin_definitions as $plugin_id => $plugin_definition) {
154       $plugin = $this->getMock('Drupal\Core\Menu\LocalActionInterface');
155       $plugin->expects($this->any())
156         ->method('getRouteName')
157         ->will($this->returnValue($plugin_definition['route_name']));
158       $plugin->expects($this->any())
159         ->method('getRouteParameters')
160         ->will($this->returnValue(isset($plugin_definition['route_parameters']) ? $plugin_definition['route_parameters'] : []));
161       $plugin->expects($this->any())
162         ->method('getTitle')
163         ->will($this->returnValue($plugin_definition['title']));
164       $this->controllerResolver->expects($this->any())
165         ->method('getArguments')
166         ->with($this->request, [$plugin, 'getTitle'])
167         ->will($this->returnValue([]));
168
169       $plugin->expects($this->any())
170         ->method('getWeight')
171         ->will($this->returnValue($plugin_definition['weight']));
172       $this->controllerResolver->expects($this->any())
173         ->method('getArguments')
174         ->with($this->request, [$plugin, 'getTitle'])
175         ->will($this->returnValue([]));
176       $map[] = [$plugin_id, [], $plugin];
177     }
178     $this->factory->expects($this->any())
179       ->method('createInstance')
180       ->will($this->returnValueMap($map));
181
182     $this->assertEquals($expected_actions, $this->localActionManager->getActionsForRoute($route_appears));
183   }
184
185   public function getActionsForRouteProvider() {
186     // Single available and single expected plugins.
187     $data[] = [
188       'test_route',
189       [
190         'plugin_id_1' => [
191           'appears_on' => [
192             'test_route',
193           ],
194           'route_name' => 'test_route_2',
195           'title' => 'Plugin ID 1',
196           'weight' => 0,
197         ],
198       ],
199       [
200         '#cache' => [
201           'contexts' => ['route'],
202         ],
203         'plugin_id_1' => [
204           '#theme' => 'menu_local_action',
205           '#link' => [
206             'title' => 'Plugin ID 1',
207             'url' => Url::fromRoute('test_route_2'),
208             'localized_options' => '',
209           ],
210           '#access' => AccessResult::forbidden(),
211           '#weight' => 0,
212           '#cache' => [
213             'contexts' => [],
214             'tags' => [],
215             'max-age' => 0,
216           ],
217         ],
218       ],
219     ];
220     // Multiple available and single expected plugins.
221     $data[] = [
222       'test_route',
223       [
224         'plugin_id_1' => [
225           'appears_on' => [
226             'test_route',
227           ],
228           'route_name' => 'test_route_2',
229           'title' => 'Plugin ID 1',
230           'weight' => 0,
231         ],
232         'plugin_id_2' => [
233           'appears_on' => [
234             'test_route2',
235           ],
236           'route_name' => 'test_route_3',
237           'title' => 'Plugin ID 2',
238           'weight' => 0,
239         ],
240       ],
241       [
242         '#cache' => [
243           'contexts' => ['route'],
244         ],
245         'plugin_id_1' => [
246           '#theme' => 'menu_local_action',
247           '#link' => [
248             'title' => 'Plugin ID 1',
249             'url' => Url::fromRoute('test_route_2'),
250             'localized_options' => '',
251           ],
252           '#access' => AccessResult::forbidden(),
253           '#weight' => 0,
254           '#cache' => [
255             'contexts' => [],
256             'tags' => [],
257             'max-age' => 0,
258           ],
259         ],
260       ],
261     ];
262
263     // Multiple available and multiple expected plugins and specified weight.
264     $data[] = [
265       'test_route',
266       [
267         'plugin_id_1' => [
268           'appears_on' => [
269             'test_route',
270           ],
271           'route_name' => 'test_route_2',
272           'title' => 'Plugin ID 1',
273           'weight' => 1,
274         ],
275         'plugin_id_2' => [
276           'appears_on' => [
277             'test_route',
278           ],
279           'route_name' => 'test_route_3',
280           'title' => 'Plugin ID 2',
281           'weight' => 0,
282         ],
283       ],
284       [
285         '#cache' => [
286           'contexts' => ['route'],
287         ],
288         'plugin_id_1' => [
289           '#theme' => 'menu_local_action',
290           '#link' => [
291             'title' => 'Plugin ID 1',
292             'url' => Url::fromRoute('test_route_2'),
293             'localized_options' => '',
294           ],
295           '#access' => AccessResult::forbidden(),
296           '#weight' => 1,
297           '#cache' => [
298             'contexts' => [],
299             'tags' => [],
300             'max-age' => 0,
301           ],
302         ],
303         'plugin_id_2' => [
304           '#theme' => 'menu_local_action',
305           '#link' => [
306             'title' => 'Plugin ID 2',
307             'url' => Url::fromRoute('test_route_3'),
308             'localized_options' => '',
309           ],
310           '#access' => AccessResult::forbidden(),
311           '#weight' => 0,
312           '#cache' => [
313             'contexts' => [],
314             'tags' => [],
315             'max-age' => 0,
316           ],
317         ],
318       ],
319     ];
320
321     // Two plugins with the same route name but different route parameters.
322     $data[] = [
323       'test_route',
324       [
325         'plugin_id_1' => [
326           'appears_on' => [
327             'test_route',
328           ],
329           'route_name' => 'test_route_2',
330           'route_parameters' => ['test1'],
331           'title' => 'Plugin ID 1',
332           'weight' => 1,
333         ],
334         'plugin_id_2' => [
335           'appears_on' => [
336             'test_route',
337           ],
338           'route_name' => 'test_route_2',
339           'route_parameters' => ['test2'],
340           'title' => 'Plugin ID 2',
341           'weight' => 0,
342         ],
343       ],
344       [
345         '#cache' => [
346           'contexts' => ['route'],
347         ],
348         'plugin_id_1' => [
349           '#theme' => 'menu_local_action',
350           '#link' => [
351             'title' => 'Plugin ID 1',
352             'url' => Url::fromRoute('test_route_2', ['test1']),
353             'localized_options' => '',
354           ],
355           '#access' => AccessResult::forbidden(),
356           '#weight' => 1,
357           '#cache' => [
358             'contexts' => [],
359             'tags' => [],
360             'max-age' => 0,
361           ],
362         ],
363         'plugin_id_2' => [
364           '#theme' => 'menu_local_action',
365           '#link' => [
366             'title' => 'Plugin ID 2',
367             'url' => Url::fromRoute('test_route_2', ['test2']),
368             'localized_options' => '',
369           ],
370           '#access' => AccessResult::forbidden(),
371           '#weight' => 0,
372           '#cache' => [
373             'contexts' => [],
374             'tags' => [],
375             'max-age' => 0,
376           ],
377         ],
378       ],
379     ];
380
381     return $data;
382   }
383
384 }
385
386 class TestLocalActionManager extends LocalActionManager {
387
388   public function __construct(ControllerResolverInterface $controller_resolver, Request $request, RouteMatchInterface $route_match, RouteProviderInterface $route_provider, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, AccessManagerInterface $access_manager, AccountInterface $account, DiscoveryInterface $discovery, FactoryInterface $factory) {
389     $this->discovery = $discovery;
390     $this->factory = $factory;
391     $this->routeProvider = $route_provider;
392     $this->accessManager = $access_manager;
393     $this->account = $account;
394     $this->controllerResolver = $controller_resolver;
395     $this->requestStack = new RequestStack();
396     $this->requestStack->push($request);
397     $this->routeMatch = $route_match;
398     $this->moduleHandler = $module_handler;
399     $this->alterInfo('menu_local_actions');
400     $this->setCacheBackend($cache_backend, 'local_action_plugins', ['local_action']);
401   }
402
403 }