You are here

public function StyleTest::testStyle in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/views/src/Tests/Plugin/StyleTest.php \Drupal\views\Tests\Plugin\StyleTest::testStyle()

Tests the general rendering of styles.

File

core/modules/views/src/Tests/Plugin/StyleTest.php, line 48
Contains \Drupal\views\Tests\Plugin\StyleTest.

Class

StyleTest
Tests general style functionality.

Namespace

Drupal\views\Tests\Plugin

Code

public function testStyle() {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = $this->container
    ->get('renderer');

  // This run use the test row plugin and render with it.
  $view = Views::getView('test_view');
  $view
    ->setDisplay();
  $style = $view->display_handler
    ->getOption('style');
  $style['type'] = 'test_style';
  $view->display_handler
    ->setOption('style', $style);
  $row = $view->display_handler
    ->getOption('row');
  $row['type'] = 'test_row';
  $view->display_handler
    ->setOption('row', $row);
  $view
    ->initDisplay();
  $view
    ->initStyle();

  // Reinitialize the style as it supports row plugins now.
  $view->style_plugin
    ->init($view, $view->display_handler);
  $this
    ->assertTrue($view->rowPlugin instanceof RowTest, 'Make sure the right row plugin class is loaded.');
  $random_text = $this
    ->randomMachineName();
  $view->rowPlugin
    ->setOutput($random_text);
  $output = $view
    ->preview();
  $output = $renderer
    ->renderRoot($output);
  $this
    ->assertTrue(strpos($output, $random_text) !== FALSE, 'Make sure that the rendering of the row plugin appears in the output of the view.');

  // Test without row plugin support.
  $view = Views::getView('test_view');
  $view
    ->setDisplay();
  $style = $view->display_handler
    ->getOption('style');
  $style['type'] = 'test_style';
  $view->display_handler
    ->setOption('style', $style);
  $view
    ->initDisplay();
  $view
    ->initStyle();
  $view->style_plugin
    ->setUsesRowPlugin(FALSE);
  $this
    ->assertTrue($view->style_plugin instanceof StyleTestPlugin, 'Make sure the right style plugin class is loaded.');
  $this
    ->assertTrue($view->rowPlugin instanceof Fields, 'Make sure that rowPlugin is now a fields instance.');
  $random_text = $this
    ->randomMachineName();

  // Set some custom text to the output and make sure that this value is
  // rendered.
  $view->style_plugin
    ->setOutput($random_text);
  $output = $view
    ->preview();
  $output = $renderer
    ->renderRoot($output);
  $this
    ->assertTrue(strpos($output, $random_text) !== FALSE, 'Make sure that the rendering of the style plugin appears in the output of the view.');
}