You are here

abstract class LocalTaskIntegrationTestBase in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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

Expanded class hierarchy of LocalTaskIntegrationTestBase

14 files declare their use of LocalTaskIntegrationTestBase
ActionLocalTasksTest.php in core/modules/action/tests/src/Unit/Menu/ActionLocalTasksTest.php
Contains \Drupal\Tests\action\Unit\Menu\ActionLocalTasksTest.
AggregatorLocalTasksTest.php in core/modules/aggregator/tests/src/Unit/Menu/AggregatorLocalTasksTest.php
Contains \Drupal\Tests\aggregator\Unit\Menu\AggregatorLocalTasksTest.
BlockContentLocalTasksTest.php in core/modules/block_content/tests/src/Unit/Menu/BlockContentLocalTasksTest.php
Contains \Drupal\Tests\block_content\Unit\Menu\BlockContentLocalTasksTest.
BlockLocalTasksTest.php in core/modules/block/tests/src/Unit/Menu/BlockLocalTasksTest.php
Contains \Drupal\Tests\block\Unit\Menu\BlockLocalTasksTest.
BookLocalTasksTest.php in core/modules/book/tests/src/Unit/Menu/BookLocalTasksTest.php
Contains \Drupal\Tests\book\Unit\Menu\BookLocalTasksTest.

... See full list

File

core/tests/Drupal/Tests/Core/Menu/LocalTaskIntegrationTestBase.php, line 22
Contains \Drupal\Tests\Core\Menu\LocalTaskIntegrationTestBase.

Namespace

Drupal\Tests\Core\Menu
View 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() {
    parent::setUp();
    $container = new ContainerBuilder();
    $config_factory = $this
      ->getConfigFactoryStub(array());
    $container
      ->set('config.factory', $config_factory);
    $container
      ->set('app.root', $this->root);
    \Drupal::setContainer($container);
    $this->container = $container;
  }

  /**
   * Sets up the local task manager for the test.
   */
  protected function getLocalTaskManager($module_dirs, $route_name, $route_params) {
    $manager = $this
      ->getMockBuilder('Drupal\\Core\\Menu\\LocalTaskManager')
      ->disableOriginalConstructor()
      ->setMethods(NULL)
      ->getMock();
    $controllerResolver = $this
      ->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
    $property = new \ReflectionProperty('Drupal\\Core\\Menu\\LocalTaskManager', 'controllerResolver');
    $property
      ->setAccessible(TRUE);
    $property
      ->setValue($manager, $controllerResolver);

    // todo mock a request with a route.
    $request_stack = new RequestStack();
    $property = new \ReflectionProperty('Drupal\\Core\\Menu\\LocalTaskManager', 'requestStack');
    $property
      ->setAccessible(TRUE);
    $property
      ->setValue($manager, $request_stack);
    $accessManager = $this
      ->getMock('Drupal\\Core\\Access\\AccessManagerInterface');
    $property = new \ReflectionProperty('Drupal\\Core\\Menu\\LocalTaskManager', 'accessManager');
    $property
      ->setAccessible(TRUE);
    $property
      ->setValue($manager, $accessManager);
    $route_provider = $this
      ->getMock('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);

    // Set all the modules as being existent.
    $module_handler
      ->expects($this
      ->any())
      ->method('moduleExists')
      ->will($this
      ->returnCallback(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
      ->getMock('Drupal\\Core\\Menu\\LocalTaskInterface');
    $factory = $this
      ->getMock('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
      ->getMock('Drupal\\Core\\Cache\\CacheBackendInterface');
    $manager
      ->setCacheBackend($cache_backend, 'local_task.en', array(
      'local_task',
    ));
    return $manager;
  }

  /**
   * Tests integration for local tasks.
   *
   * @param $route_name
   *   Route name to base task building on.
   * @param $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($route_name, $expected_tasks, $route_params = array()) {
    $directory_list = array();
    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 = array();
    foreach ($tmp_tasks as $level => $level_tasks) {
      $tasks[$level] = array_keys($level_tasks);
    }
    $this
      ->assertEquals($expected_tasks, $tasks);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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 14
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.