You are here

class ViewsHandlerManagerTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Unit/ViewsHandlerManagerTest.php \Drupal\Tests\views\Unit\ViewsHandlerManagerTest
  2. 10 core/modules/views/tests/src/Unit/ViewsHandlerManagerTest.php \Drupal\Tests\views\Unit\ViewsHandlerManagerTest

Tests the ViewsHandlerManager class.

@group views

@coversDefaultClass \Drupal\views\Plugin\ViewsHandlerManager

Hierarchy

Expanded class hierarchy of ViewsHandlerManagerTest

File

core/modules/views/tests/src/Unit/ViewsHandlerManagerTest.php, line 15

Namespace

Drupal\Tests\views\Unit
View source
class ViewsHandlerManagerTest extends UnitTestCase {

  /**
   * @var \Drupal\views\Plugin\ViewsHandlerManager
   */
  protected $handlerManager;

  /**
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $moduleHandler;

  /**
   * The mocked views data.
   *
   * @var \Drupal\views\ViewsData|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $viewsData;

  /**
   * The mocked factory.
   *
   * @var \Drupal\Component\Plugin\Factory\FactoryInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $factory;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->viewsData = $this
      ->getMockBuilder('Drupal\\views\\ViewsData')
      ->disableOriginalConstructor()
      ->getMock();
    $cache_backend = $this
      ->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
    $this->moduleHandler = $this
      ->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
    $this->handlerManager = new ViewsHandlerManager('test', new \ArrayObject([]), $this->viewsData, $cache_backend, $this->moduleHandler);
  }

  /**
   * Setups of the plugin factory.
   */
  protected function setupMockedFactory() {
    $this->factory = $this
      ->createMock('Drupal\\Component\\Plugin\\Factory\\FactoryInterface');
    $reflection = new \ReflectionClass($this->handlerManager);
    $property = $reflection
      ->getProperty('factory');
    $property
      ->setAccessible(TRUE);
    $property
      ->setValue($this->handlerManager, $this->factory);
  }

  /**
   * Tests that hook_views_plugins_TYPE_alter() is invoked for a handler type.
   *
   * @covers ::__construct
   * @covers ::getDefinitions
   */
  public function testAlterHookInvocation() {
    $this->moduleHandler
      ->expects($this
      ->once())
      ->method('alter')
      ->with('views_plugins_test', []);
    $this->handlerManager
      ->getDefinitions();
  }

  /**
   * Tests getHandler() and its base information propagation.
   */
  public function testGetHandlerBaseInformationPropagation() {
    $this
      ->setupMockedFactory();
    $item = [];
    $item['table'] = 'test_table';
    $item['field'] = 'test_field';
    $views_data = [];
    $views_data['test_field']['test']['id'] = 'test_id';
    $views_data['test_field']['test']['more_information'] = 'test_id';
    $views_data['test_field']['group'] = 'test_group';
    $views_data['test_field']['title'] = 'test title';
    $views_data['test_field']['real field'] = 'test real field';
    $views_data['test_field']['real table'] = 'test real table';
    $views_data['test_field']['entity field'] = 'test entity field';
    $this->viewsData
      ->expects($this
      ->once())
      ->method('get')
      ->with('test_table')
      ->willReturn($views_data);
    $expected_definition = [
      'id' => 'test_id',
      'more_information' => 'test_id',
      'group' => 'test_group',
      'title' => 'test title',
      'real field' => 'test real field',
      'real table' => 'test real table',
      'entity field' => 'test entity field',
    ];
    $plugin = $this
      ->createMock('Drupal\\views\\Plugin\\views\\ViewsHandlerInterface');
    $this->factory
      ->expects($this
      ->once())
      ->method('createInstance')
      ->with('test_id', $expected_definition)
      ->willReturn($plugin);
    $result = $this->handlerManager
      ->getHandler($item);
    $this
      ->assertSame($plugin, $result);
  }

  /**
   * Tests getHandler() with an override.
   */
  public function testGetHandlerOverride() {
    $this
      ->setupMockedFactory();
    $item = [];
    $item['table'] = 'test_table';
    $item['field'] = 'test_field';
    $views_data = [];
    $views_data['test_field']['test']['id'] = 'test_id';
    $this->viewsData
      ->expects($this
      ->once())
      ->method('get')
      ->with('test_table')
      ->willReturn($views_data);
    $plugin = $this
      ->createMock('Drupal\\views\\Plugin\\views\\ViewsHandlerInterface');
    $this->factory
      ->expects($this
      ->once())
      ->method('createInstance')
      ->with('test_override')
      ->willReturn($plugin);
    $result = $this->handlerManager
      ->getHandler($item, 'test_override');
    $this
      ->assertSame($plugin, $result);
  }

  /**
   * Tests getHandler() without an override.
   */
  public function testGetHandlerNoOverride() {
    $this
      ->setupMockedFactory();
    $item = [];
    $item['table'] = 'test_table';
    $item['field'] = 'test_field';
    $views_data = [];
    $views_data['test_field']['test']['id'] = 'test_id';
    $this->viewsData
      ->expects($this
      ->once())
      ->method('get')
      ->with('test_table')
      ->willReturn($views_data);
    $plugin = $this
      ->createMock('Drupal\\views\\Plugin\\views\\ViewsHandlerInterface');
    $this->factory
      ->expects($this
      ->once())
      ->method('createInstance')
      ->with('test_id')
      ->willReturn($plugin);
    $result = $this->handlerManager
      ->getHandler($item);
    $this
      ->assertSame($plugin, $result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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::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.
ViewsHandlerManagerTest::$factory protected property The mocked factory.
ViewsHandlerManagerTest::$handlerManager protected property
ViewsHandlerManagerTest::$moduleHandler protected property
ViewsHandlerManagerTest::$viewsData protected property The mocked views data.
ViewsHandlerManagerTest::setUp protected function Overrides UnitTestCase::setUp
ViewsHandlerManagerTest::setupMockedFactory protected function Setups of the plugin factory.
ViewsHandlerManagerTest::testAlterHookInvocation public function Tests that hook_views_plugins_TYPE_alter() is invoked for a handler type.
ViewsHandlerManagerTest::testGetHandlerBaseInformationPropagation public function Tests getHandler() and its base information propagation.
ViewsHandlerManagerTest::testGetHandlerNoOverride public function Tests getHandler() without an override.
ViewsHandlerManagerTest::testGetHandlerOverride public function Tests getHandler() with an override.