You are here

public function ViewExecutableTest::testDisplays in Drupal 8

Same name and namespace in other branches
  1. 9 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
    ->assertEqual($view->current_display, 'default');
  $this
    ->assertEqual(spl_object_hash($view->display_handler), spl_object_hash($view->displayHandlers
    ->get('default')));

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

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

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

  // 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);
}