class LocalTaskIntegrationTestBase
Same name and namespace in other branches
- 11.x core/tests/Drupal/Tests/Core/Menu/LocalTaskIntegrationTestBase.php \Drupal\Tests\Core\Menu\LocalTaskIntegrationTestBase
- 10 core/tests/Drupal/Tests/Core/Menu/LocalTaskIntegrationTestBase.php \Drupal\Tests\Core\Menu\LocalTaskIntegrationTestBase
- 9 core/tests/Drupal/Tests/Core/Menu/LocalTaskIntegrationTestBase.php \Drupal\Tests\Core\Menu\LocalTaskIntegrationTestBase
- 8.9.x core/tests/Drupal/Tests/Core/Menu/LocalTaskIntegrationTestBase.php \Drupal\Tests\Core\Menu\LocalTaskIntegrationTestBase
Defines a base unit test for testing existence of local tasks.
@todo Add tests for access checking and URL building, https://www.drupal.org/node/2112245.
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\DrupalTestCaseTrait, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\Core\Menu\LocalTaskIntegrationTestBase extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of LocalTaskIntegrationTestBase
11 files declare their use of LocalTaskIntegrationTestBase
- BlockContentLocalTasksTest.php in core/
modules/ block_content/ tests/ src/ Unit/ Menu/ BlockContentLocalTasksTest.php - BlockLocalTasksTest.php in core/
modules/ block/ tests/ src/ Unit/ Menu/ BlockLocalTasksTest.php - ConfigLocalTasksTest.php in core/
modules/ config/ tests/ src/ Unit/ Menu/ ConfigLocalTasksTest.php - ContentTranslationLocalTasksTest.php in core/
modules/ content_translation/ tests/ src/ Unit/ Menu/ ContentTranslationLocalTasksTest.php - LanguageLocalTasksTest.php in core/
modules/ language/ tests/ src/ Unit/ Menu/ LanguageLocalTasksTest.php
File
-
core/
tests/ Drupal/ Tests/ Core/ Menu/ LocalTaskIntegrationTestBase.php, line 30
Namespace
Drupal\Tests\Core\MenuView source
abstract class LocalTaskIntegrationTestBase extends UnitTestCase {
/**
* A list of module directories used for YAML searching.
*
* @var array
*/
protected $directoryList;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $moduleHandler;
/**
* The container.
*
* @var \Symfony\Component\DependencyInjection\ContainerBuilder
*/
protected $container;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
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;
}
/**
* Sets up the local task manager for the test.
*/
protected function getLocalTaskManager(array $module_dirs, string $route_name, array $route_params) : LocalTaskManager {
$module_handler = $this->createStub(ModuleHandlerInterface::class);
// Set all the modules as being existent.
$module_handler->method('moduleExists')
->willReturnCallback(function ($module) use ($module_dirs) : bool {
return isset($module_dirs[$module]);
});
$language = $this->createStub(LanguageInterface::class);
$language->method('getId')
->willReturn('en');
$language_manager = $this->createStub(LanguageManagerInterface::class);
$language_manager->method('getCurrentLanguage')
->willReturn($language);
$manager = new LocalTaskManager($this->createStub(ArgumentResolverInterface::class), new RequestStack(), $this->createStub(RouteMatchInterface::class), $this->createStub(RouteProviderInterface::class), $module_handler, $this->createStub(CacheBackendInterface::class), $language_manager, $this->createStub(AccessManagerInterface::class), $this->createStub(AccountInterface::class));
$pluginDiscovery = new YamlDiscovery('links.task', $module_dirs);
$pluginDiscovery = new ContainerDerivativeDiscoveryDecorator($pluginDiscovery);
$property = new \ReflectionProperty('Drupal\\Core\\Menu\\LocalTaskManager', 'discovery');
$property->setValue($manager, $pluginDiscovery);
$method = new \ReflectionMethod('Drupal\\Core\\Menu\\LocalTaskManager', 'alterInfo');
$method->invoke($manager, 'local_tasks');
$factory = $this->createStub(FactoryInterface::class);
$factory->method('createInstance')
->willReturn($this->createStub('Drupal\\Core\\Menu\\LocalTaskInterface'));
$property = new \ReflectionProperty('Drupal\\Core\\Menu\\LocalTaskManager', 'factory');
$property->setValue($manager, $factory);
return $manager;
}
/**
* Tests integration for local tasks.
*
* @param string $route_name
* Route name to base task building on.
* @param array $expected_tasks
* A list of tasks groups by level expected at the given route.
* @param array $route_params
* (optional) A list of route parameters used to resolve tasks.
*/
protected function assertLocalTasks(string $route_name, array $expected_tasks, array $route_params = []) : void {
$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);
// At this point we're just testing existence so pull out keys and then
// compare.
//
// Deeper testing would require a functioning factory which because we are
// using the DefaultPluginManager base means we get into dependency soup
// because its factories create method and pulling services off the \Drupal
// container.
$tasks = [];
foreach ($tmp_tasks as $level => $level_tasks) {
$tasks[$level] = array_keys($level_tasks);
}
$this->assertEquals($expected_tasks, $tasks);
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
|---|---|---|---|---|---|
| DrupalTestCaseTrait::checkErrorHandlerOnTearDown | public | function | Checks the test error handler after test execution. | 1 | |
| LocalTaskIntegrationTestBase::$container | protected | property | The container. | ||
| LocalTaskIntegrationTestBase::$directoryList | protected | property | A list of module directories used for YAML searching. | ||
| LocalTaskIntegrationTestBase::$moduleHandler | protected | property | The module handler. | ||
| LocalTaskIntegrationTestBase::assertLocalTasks | protected | function | Tests integration for local tasks. | ||
| LocalTaskIntegrationTestBase::getLocalTaskManager | protected | function | Sets up the local task manager for the test. | ||
| LocalTaskIntegrationTestBase::setUp | protected | function | Overrides UnitTestCase::setUp | 11 | |
| RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | ||
| RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | ||
| RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | ||
| RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | ||
| UnitTestCase::$root | protected | property | The app root. | ||
| UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | ||
| UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | ||
| UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | ||
| UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | ||
| UnitTestCase::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | ||
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.