5 * Contains \Drupal\Tests\Core\Menu\LocalActionManagerTest.
8 namespace Drupal\Tests\Core\Menu;
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;
22 use Drupal\Tests\UnitTestCase;
23 use Symfony\Component\HttpFoundation\Request;
24 use Symfony\Component\HttpFoundation\RequestStack;
25 use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
28 * @coversDefaultClass \Drupal\Core\Menu\LocalActionManager
31 class LocalActionManagerTest extends UnitTestCase {
34 * The mocked controller resolver.
36 * @var \Drupal\Core\Controller\ControllerResolverInterface|\PHPUnit_Framework_MockObject_MockObject
38 protected $controllerResolver;
43 * @var \Symfony\Component\HttpFoundation\Request|\PHPUnit_Framework_MockObject_MockObject
48 * The mocked module handler.
50 * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
52 protected $moduleHandler;
55 * The mocked router provider.
57 * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
59 protected $routeProvider;
62 * The mocked cache backend.
64 * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
66 protected $cacheBackend;
69 * The mocked access manager.
71 * @var \Drupal\Core\Access\AccessManagerInterface|\PHPUnit_Framework_MockObject_MockObject
73 protected $accessManager;
78 * @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
85 * @var \Drupal\Component\Plugin\Factory\FactoryInterface|\PHPUnit_Framework_MockObject_MockObject
90 * The mocked plugin discovery.
92 * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
97 * The tested local action manager
99 * @var \Drupal\Tests\Core\Menu\TestLocalActionManager
101 protected $localActionManager;
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');
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');
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);
129 public function testGetTitle() {
130 $local_action = $this->getMock('Drupal\Core\Menu\LocalActionInterface');
131 $local_action->expects($this->once())
135 $this->controllerResolver->expects($this->once())
136 ->method('getArguments')
137 ->with($this->request, [$local_action, 'getTitle'])
138 ->will($this->returnValue(['test']));
140 $this->localActionManager->getTitle($local_action);
144 * @covers ::getActionsForRoute
146 * @dataProvider getActionsForRouteProvider
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));
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())
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([]));
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];
178 $this->factory->expects($this->any())
179 ->method('createInstance')
180 ->will($this->returnValueMap($map));
182 $this->assertEquals($expected_actions, $this->localActionManager->getActionsForRoute($route_appears));
185 public function getActionsForRouteProvider() {
186 // Single available and single expected plugins.
194 'route_name' => 'test_route_2',
195 'title' => 'Plugin ID 1',
201 'contexts' => ['route'],
204 '#theme' => 'menu_local_action',
206 'title' => 'Plugin ID 1',
207 'url' => Url::fromRoute('test_route_2'),
208 'localized_options' => '',
210 '#access' => AccessResult::forbidden(),
220 // Multiple available and single expected plugins.
228 'route_name' => 'test_route_2',
229 'title' => 'Plugin ID 1',
236 'route_name' => 'test_route_3',
237 'title' => 'Plugin ID 2',
243 'contexts' => ['route'],
246 '#theme' => 'menu_local_action',
248 'title' => 'Plugin ID 1',
249 'url' => Url::fromRoute('test_route_2'),
250 'localized_options' => '',
252 '#access' => AccessResult::forbidden(),
263 // Multiple available and multiple expected plugins and specified weight.
271 'route_name' => 'test_route_2',
272 'title' => 'Plugin ID 1',
279 'route_name' => 'test_route_3',
280 'title' => 'Plugin ID 2',
286 'contexts' => ['route'],
289 '#theme' => 'menu_local_action',
291 'title' => 'Plugin ID 1',
292 'url' => Url::fromRoute('test_route_2'),
293 'localized_options' => '',
295 '#access' => AccessResult::forbidden(),
304 '#theme' => 'menu_local_action',
306 'title' => 'Plugin ID 2',
307 'url' => Url::fromRoute('test_route_3'),
308 'localized_options' => '',
310 '#access' => AccessResult::forbidden(),
321 // Two plugins with the same route name but different route parameters.
329 'route_name' => 'test_route_2',
330 'route_parameters' => ['test1'],
331 'title' => 'Plugin ID 1',
338 'route_name' => 'test_route_2',
339 'route_parameters' => ['test2'],
340 'title' => 'Plugin ID 2',
346 'contexts' => ['route'],
349 '#theme' => 'menu_local_action',
351 'title' => 'Plugin ID 1',
352 'url' => Url::fromRoute('test_route_2', ['test1']),
353 'localized_options' => '',
355 '#access' => AccessResult::forbidden(),
364 '#theme' => 'menu_local_action',
366 'title' => 'Plugin ID 2',
367 'url' => Url::fromRoute('test_route_2', ['test2']),
368 'localized_options' => '',
370 '#access' => AccessResult::forbidden(),
386 class TestLocalActionManager extends LocalActionManager {
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']);