View source
<?php
namespace Drupal\Tests\Core\Menu;
use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
use Drupal\Core\Plugin\Discovery\YamlDiscovery;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\RequestStack;
abstract class LocalTaskIntegrationTestBase extends UnitTestCase {
protected $directoryList;
protected $moduleHandler;
protected $container;
protected function setUp() {
parent::setUp();
$container = new ContainerBuilder();
$config_factory = $this
->getConfigFactoryStub([]);
$container
->set('config.factory', $config_factory);
$container
->setParameter('app.root', $this->root);
\Drupal::setContainer($container);
$this->container = $container;
}
protected function getLocalTaskManager($module_dirs, $route_name, $route_params) {
$manager = $this
->getMockBuilder('Drupal\\Core\\Menu\\LocalTaskManager')
->disableOriginalConstructor()
->setMethods(NULL)
->getMock();
$argumentResolver = $this
->createMock('Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolverInterface');
$property = new \ReflectionProperty('Drupal\\Core\\Menu\\LocalTaskManager', 'argumentResolver');
$property
->setAccessible(TRUE);
$property
->setValue($manager, $argumentResolver);
$request_stack = new RequestStack();
$property = new \ReflectionProperty('Drupal\\Core\\Menu\\LocalTaskManager', 'requestStack');
$property
->setAccessible(TRUE);
$property
->setValue($manager, $request_stack);
$accessManager = $this
->createMock('Drupal\\Core\\Access\\AccessManagerInterface');
$property = new \ReflectionProperty('Drupal\\Core\\Menu\\LocalTaskManager', 'accessManager');
$property
->setAccessible(TRUE);
$property
->setValue($manager, $accessManager);
$route_provider = $this
->createMock('Drupal\\Core\\Routing\\RouteProviderInterface');
$property = new \ReflectionProperty('Drupal\\Core\\Menu\\LocalTaskManager', 'routeProvider');
$property
->setAccessible(TRUE);
$property
->setValue($manager, $route_provider);
$module_handler = $this
->getMockBuilder('Drupal\\Core\\Extension\\ModuleHandlerInterface')
->disableOriginalConstructor()
->getMock();
$property = new \ReflectionProperty('Drupal\\Core\\Menu\\LocalTaskManager', 'moduleHandler');
$property
->setAccessible(TRUE);
$property
->setValue($manager, $module_handler);
$module_handler
->expects($this
->any())
->method('moduleExists')
->willReturnCallback(function ($module) use ($module_dirs) {
return isset($module_dirs[$module]);
});
$pluginDiscovery = new YamlDiscovery('links.task', $module_dirs);
$pluginDiscovery = new ContainerDerivativeDiscoveryDecorator($pluginDiscovery);
$property = new \ReflectionProperty('Drupal\\Core\\Menu\\LocalTaskManager', 'discovery');
$property
->setAccessible(TRUE);
$property
->setValue($manager, $pluginDiscovery);
$method = new \ReflectionMethod('Drupal\\Core\\Menu\\LocalTaskManager', 'alterInfo');
$method
->setAccessible(TRUE);
$method
->invoke($manager, 'local_tasks');
$plugin_stub = $this
->createMock('Drupal\\Core\\Menu\\LocalTaskInterface');
$factory = $this
->createMock('Drupal\\Component\\Plugin\\Factory\\FactoryInterface');
$factory
->expects($this
->any())
->method('createInstance')
->will($this
->returnValue($plugin_stub));
$property = new \ReflectionProperty('Drupal\\Core\\Menu\\LocalTaskManager', 'factory');
$property
->setAccessible(TRUE);
$property
->setValue($manager, $factory);
$cache_backend = $this
->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
$manager
->setCacheBackend($cache_backend, 'local_task.en', [
'local_task',
]);
return $manager;
}
protected function assertLocalTasks($route_name, $expected_tasks, $route_params = []) {
$directory_list = [];
foreach ($this->directoryList as $key => $value) {
$directory_list[$key] = $this->root . '/' . $value;
}
$manager = $this
->getLocalTaskManager($directory_list, $route_name, $route_params);
$tmp_tasks = $manager
->getLocalTasksForRoute($route_name);
$tasks = [];
foreach ($tmp_tasks as $level => $level_tasks) {
$tasks[$level] = array_keys($level_tasks);
}
$this
->assertEquals($expected_tasks, $tasks);
}
}