public function ViewExecutableTest::testDisplays

Tests the display related methods and properties.

File

drupal/core/modules/views/lib/Drupal/views/Tests/ViewExecutableTest.php, line 166
Contains \Drupal\views\Tests\ViewExecutableTest.

Class

ViewExecutableTest
Tests the ViewExecutable class.

Namespace

Drupal\views\Tests

Code

public function testDisplays() {
  $view = views_get_view('test_executable_displays');

  // Tests Drupal\views\ViewExecutable::initDisplay().
  $view
    ->initDisplay();
  $this
    ->assertTrue($view->displayHandlers instanceof DisplayBag, '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);
}