You are here

public function ViewExecutableTest::testInitMethods in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Kernel/ViewExecutableTest.php \Drupal\Tests\views\Kernel\ViewExecutableTest::testInitMethods()
  2. 10 core/modules/views/tests/src/Kernel/ViewExecutableTest.php \Drupal\Tests\views\Kernel\ViewExecutableTest::testInitMethods()

Tests the initDisplay() and initHandlers() methods.

File

core/modules/views/tests/src/Kernel/ViewExecutableTest.php, line 119

Class

ViewExecutableTest
Tests the ViewExecutable class.

Namespace

Drupal\Tests\views\Kernel

Code

public function testInitMethods() {
  $view = Views::getView('test_destroy');
  $view
    ->initDisplay();
  $this
    ->assertInstanceOf(DefaultDisplay::class, $view->display_handler);
  $this
    ->assertInstanceOf(DefaultDisplay::class, $view->displayHandlers
    ->get('default'));
  $view
    ->destroy();
  $view
    ->initHandlers();

  // Check for all handler types.
  $handler_types = array_keys(ViewExecutable::getHandlerTypes());
  foreach ($handler_types as $type) {

    // The views_test integration doesn't have relationships.
    if ($type == 'relationship') {
      continue;
    }
    $this
      ->assertGreaterThan(0, count($view->{$type}), new FormattableMarkup('Make sure a %type instance got instantiated.', [
      '%type' => $type,
    ]));
  }

  // initHandlers() should create display handlers automatically as well.
  $this
    ->assertInstanceOf(DefaultDisplay::class, $view->display_handler);
  $this
    ->assertInstanceOf(DefaultDisplay::class, $view->displayHandlers
    ->get('default'));
  $view_hash = spl_object_hash($view);
  $display_hash = spl_object_hash($view->display_handler);

  // Test the initStyle() method.
  $view
    ->initStyle();
  $this
    ->assertInstanceOf(DefaultStyle::class, $view->style_plugin);

  // Test the plugin has been invited and view have references to the view and
  // display handler.
  $this
    ->assertEquals($view_hash, spl_object_hash($view->style_plugin->view));
  $this
    ->assertEquals($display_hash, spl_object_hash($view->style_plugin->displayHandler));

  // Test the initQuery method().
  $view
    ->initQuery();
  $this
    ->assertInstanceOf(Sql::class, $view->query);
  $this
    ->assertEquals($view_hash, spl_object_hash($view->query->view));
  $this
    ->assertEquals($display_hash, spl_object_hash($view->query->displayHandler));
  $view
    ->destroy();

  // Test the plugin  get methods.
  $display_plugin = $view
    ->getDisplay();
  $this
    ->assertInstanceOf(DefaultDisplay::class, $display_plugin);
  $this
    ->assertInstanceOf(DefaultDisplay::class, $view->display_handler);
  $this
    ->assertSame($display_plugin, $view
    ->getDisplay(), 'The same display plugin instance was returned.');
  $style_plugin = $view
    ->getStyle();
  $this
    ->assertInstanceOf(DefaultStyle::class, $style_plugin);
  $this
    ->assertInstanceOf(DefaultStyle::class, $view->style_plugin);
  $this
    ->assertSame($style_plugin, $view
    ->getStyle(), 'The same style plugin instance was returned.');
  $pager_plugin = $view
    ->getPager();
  $this
    ->assertInstanceOf(PagerPluginBase::class, $pager_plugin);
  $this
    ->assertInstanceOf(PagerPluginBase::class, $view->pager);
  $this
    ->assertSame($pager_plugin, $view
    ->getPager(), 'The same pager plugin instance was returned.');
  $query_plugin = $view
    ->getQuery();
  $this
    ->assertInstanceOf(QueryPluginBase::class, $query_plugin);
  $this
    ->assertInstanceOf(QueryPluginBase::class, $view->query);
  $this
    ->assertSame($query_plugin, $view
    ->getQuery(), 'The same query plugin instance was returned.');
}