You are here

public function ViewExecutableTest::testDisplays in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Tests/ViewExecutableTest.php \Drupal\views\Tests\ViewExecutableTest::testDisplays()

Tests the display related methods and properties.

File

core/modules/views/src/Tests/ViewExecutableTest.php, line 201
Contains \Drupal\views\Tests\ViewExecutableTest.

Class

ViewExecutableTest
Tests the ViewExecutable class.

Namespace

Drupal\views\Tests

Code

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

  // Tests Drupal\views\ViewExecutable::initDisplay().
  $view
    ->initDisplay();
  $this
    ->assertTrue($view->displayHandlers instanceof DisplayPluginCollection, 'The displayHandlers property has the right class.');

  // Tests the classes of the instances.
  $this
    ->assertTrue($view->displayHandlers
    ->get('default') instanceof DefaultDisplay);
  $this
    ->assertTrue($view->displayHandlers
    ->get('page_1') instanceof Page);
  $this
    ->assertTrue($view->displayHandlers
    ->get('page_2') instanceof Page);

  // 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();
  $count_before = count($this->assertions);
  $view
    ->setDisplay('invalid');
  $count_after = count($this->assertions);
  $this
    ->assertTrue($count_after - $count_before, 'Error is triggered while calling the wrong display.');
  $this
    ->assertEqual($view->current_display, 'default', 'If setDisplay is called with an invalid display id the default display should be used.');
  $this
    ->assertEqual(spl_object_hash($view->display_handler), spl_object_hash($view->displayHandlers
    ->get('default')));

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

  // @todo Change this rowPlugin type too.
  $this
    ->assertTrue($view->rowPlugin instanceof Fields);

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