You are here

public function ViewExecutableTest::testDisplays 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::testDisplays()
  2. 10 core/modules/views/tests/src/Kernel/ViewExecutableTest.php \Drupal\Tests\views\Kernel\ViewExecutableTest::testDisplays()

Tests the display related methods and properties.

File

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

Class

ViewExecutableTest
Tests the ViewExecutable class.

Namespace

Drupal\Tests\views\Kernel

Code

public function testDisplays() {
  $view = Views::getView('test_executable_displays');

  // Tests Drupal\views\ViewExecutable::initDisplay().
  $view
    ->initDisplay();
  $this
    ->assertInstanceOf(DisplayPluginCollection::class, $view->displayHandlers);

  // Tests the classes of the instances.
  $this
    ->assertInstanceOf(DefaultDisplay::class, $view->displayHandlers
    ->get('default'));
  $this
    ->assertInstanceOf(Page::class, $view->displayHandlers
    ->get('page_1'));
  $this
    ->assertInstanceOf(Page::class, $view->displayHandlers
    ->get('page_2'));

  // After initializing the default display is the current used display.
  $this
    ->assertEquals('default', $view->current_display);
  $this
    ->assertEquals(spl_object_hash($view->displayHandlers
    ->get('default')), spl_object_hash($view->display_handler));

  // All handlers should have a reference to the default display.
  $this
    ->assertEquals(spl_object_hash($view->displayHandlers
    ->get('default')), spl_object_hash($view->displayHandlers
    ->get('page_1')->default_display));
  $this
    ->assertEquals(spl_object_hash($view->displayHandlers
    ->get('default')), spl_object_hash($view->displayHandlers
    ->get('page_2')->default_display));

  // Tests Drupal\views\ViewExecutable::setDisplay().
  $view
    ->setDisplay();
  $this
    ->assertEquals('default', $view->current_display, 'If setDisplay is called with no parameter the default display should be used.');
  $this
    ->assertEquals(spl_object_hash($view->displayHandlers
    ->get('default')), spl_object_hash($view->display_handler));

  // Set two different valid displays.
  $view
    ->setDisplay('page_1');
  $this
    ->assertEquals('page_1', $view->current_display, 'If setDisplay is called with a valid display id the appropriate display should be used.');
  $this
    ->assertEquals(spl_object_hash($view->displayHandlers
    ->get('page_1')), spl_object_hash($view->display_handler));
  $view
    ->setDisplay('page_2');
  $this
    ->assertEquals('page_2', $view->current_display, 'If setDisplay is called with a valid display id the appropriate display should be used.');
  $this
    ->assertEquals(spl_object_hash($view->displayHandlers
    ->get('page_2')), spl_object_hash($view->display_handler));

  // Destroy the view, so we can start again and test an invalid display.
  $view
    ->destroy();

  // Test the style and row plugins are replaced correctly when setting the
  // display.
  $view
    ->setDisplay('page_1');
  $view
    ->initStyle();
  $this
    ->assertInstanceOf(DefaultStyle::class, $view->style_plugin);
  $this
    ->assertInstanceOf(Fields::class, $view->rowPlugin);
  $view
    ->setDisplay('page_2');
  $view
    ->initStyle();
  $this
    ->assertInstanceOf(Grid::class, $view->style_plugin);

  // @todo Change this rowPlugin type too.
  $this
    ->assertInstanceOf(Fields::class, $view->rowPlugin);

  // Test the newDisplay() method.
  $view = $this->container
    ->get('entity_type.manager')
    ->getStorage('view')
    ->create([
    'id' => 'test_executable_displays',
  ]);
  $executable = $view
    ->getExecutable();
  $executable
    ->newDisplay('page');
  $executable
    ->newDisplay('page');
  $executable
    ->newDisplay('display_test');
  $this
    ->assertInstanceOf(DefaultDisplay::class, $executable->displayHandlers
    ->get('default'));
  $this
    ->assertFalse(isset($executable->displayHandlers
    ->get('default')->default_display));
  $this
    ->assertInstanceOf(Page::class, $executable->displayHandlers
    ->get('page_1'));
  $this
    ->assertInstanceOf(DefaultDisplay::class, $executable->displayHandlers
    ->get('page_1')->default_display);
  $this
    ->assertInstanceOf(Page::class, $executable->displayHandlers
    ->get('page_2'));
  $this
    ->assertInstanceOf(DefaultDisplay::class, $executable->displayHandlers
    ->get('page_2')->default_display);
  $this
    ->assertInstanceOf(DisplayTest::class, $executable->displayHandlers
    ->get('display_test_1'));
  $this
    ->assertInstanceOf(DefaultDisplay::class, $executable->displayHandlers
    ->get('display_test_1')->default_display);
}