function DisplayTest::testDisplayPlugin

Same name and namespace in other branches
  1. 8.9.x core/modules/views/tests/src/Functional/Plugin/DisplayTest.php \Drupal\Tests\views\Functional\Plugin\DisplayTest::testDisplayPlugin()
  2. 10 core/modules/views/tests/src/Functional/Plugin/DisplayTest.php \Drupal\Tests\views\Functional\Plugin\DisplayTest::testDisplayPlugin()
  3. 11.x core/modules/views/tests/src/Functional/Plugin/DisplayTest.php \Drupal\Tests\views\Functional\Plugin\DisplayTest::testDisplayPlugin()

Tests the display test plugin.

See also

\Drupal\views_test_data\Plugin\views\display\DisplayTest

File

core/modules/views/tests/src/Functional/Plugin/DisplayTest.php, line 58

Class

DisplayTest
Tests the basic display plugin.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

public function testDisplayPlugin() {
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container
        ->get('renderer');
    $view = Views::getView('test_view');
    // Add a new 'display_test' display and test it's there.
    $view->storage
        ->addDisplay('display_test');
    $displays = $view->storage
        ->get('display');
    $this->assertTrue(isset($displays['display_test_1']), 'Added display has been assigned to "display_test_1"');
    // Check the display options are like expected.
    $options = [
        'display_options' => [],
        'display_plugin' => 'display_test',
        'id' => 'display_test_1',
        'display_title' => 'Display test',
        'position' => 1,
    ];
    $this->assertEquals($options, $displays['display_test_1']);
    // Add another one to ensure that position is counted up.
    $view->storage
        ->addDisplay('display_test');
    $displays = $view->storage
        ->get('display');
    $options = [
        'display_options' => [],
        'display_plugin' => 'display_test',
        'id' => 'display_test_2',
        'display_title' => 'Display test 2',
        'position' => 2,
    ];
    $this->assertEquals($options, $displays['display_test_2']);
    // Move the second display before the first one in order to test custom
    // sorting.
    $displays['display_test_1']['position'] = 2;
    $displays['display_test_2']['position'] = 1;
    $view->storage
        ->set('display', $displays);
    $view->save();
    $view->setDisplay('display_test_1');
    $this->assertInstanceOf(DisplayTestPlugin::class, $view->display_handler);
    // Check the test option.
    $this->assertSame('', $view->display_handler
        ->getOption('test_option'));
    $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);
    $output = $view->preview();
    $output = $renderer->renderRoot($output);
    $this->assertStringContainsString('<h1></h1>', $output, 'An empty value for test_option found in output.');
    // Change this option and check the title of out output.
    $view->display_handler
        ->overrideOption('test_option', 'Test option title');
    $view->save();
    $output = $view->preview();
    $output = $renderer->renderRoot($output);
    // Test we have our custom <h1> tag in the output of the view.
    $this->assertStringContainsString('<h1>Test option title</h1>', $output, 'The test_option value found in display output title.');
    // Test that the display category/summary is in the UI.
    $this->drupalGet('admin/structure/views/view/test_view/edit/display_test_1');
    $this->assertSession()
        ->pageTextContains('Display test settings');
    // Ensure that the order is as expected.
    $result = $this->xpath('//ul[@id="views-display-menu-tabs"]/li/a/child::text()');
    $this->assertEquals('Display test 2', $result[0]->getText());
    $this->assertEquals('Display test', $result[1]->getText());
    $this->clickLink('Test option title');
    $test_option = $this->randomString();
    $this->submitForm([
        'test_option' => $test_option,
    ], 'Apply');
    // Check the new value has been saved by checking the UI summary text.
    $this->drupalGet('admin/structure/views/view/test_view/edit/display_test_1');
    $this->assertSession()
        ->linkExists($test_option);
    // Test the enable/disable status of a display.
    $view->display_handler
        ->setOption('enabled', FALSE);
    $this->assertFalse($view->display_handler
        ->isEnabled(), 'Make sure that isEnabled returns FALSE on a disabled display.');
    $view->display_handler
        ->setOption('enabled', TRUE);
    $this->assertTrue($view->display_handler
        ->isEnabled(), 'Make sure that isEnabled returns TRUE on a disabled display.');
}

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