function StyleTest::testStyle

Same name in other branches
  1. 9 core/modules/views/tests/src/Kernel/Plugin/StyleTest.php \Drupal\Tests\views\Kernel\Plugin\StyleTest::testStyle()
  2. 10 core/modules/views/tests/src/Kernel/Plugin/StyleTest.php \Drupal\Tests\views\Kernel\Plugin\StyleTest::testStyle()
  3. 11.x core/modules/views/tests/src/Kernel/Plugin/StyleTest.php \Drupal\Tests\views\Kernel\Plugin\StyleTest::testStyle()

Tests the general rendering of styles.

File

core/modules/views/tests/src/Kernel/Plugin/StyleTest.php, line 29

Class

StyleTest
Tests general style functionality.

Namespace

Drupal\Tests\views\Kernel\Plugin

Code

public function testStyle() {
    $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->assertInstanceOf(Rowtest::class, $view->rowPlugin);
    $random_text = $this->randomMachineName();
    $view->rowPlugin
        ->setOutput($random_text);
    $output = $view->preview();
    $output = $renderer->renderRoot($output);
    $this->assertStringContainsString($random_text, (string) $output);
    // 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->assertInstanceOf(StyleTestPlugin::class, $view->style_plugin);
    $this->assertInstanceOf(Fields::class, $view->rowPlugin);
    $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->assertStringContainsString($random_text, (string) $output);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.