function DisplayCRUDTest::testDuplicateDisplay

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

Tests the duplicating of a display.

File

core/modules/views_ui/tests/src/Functional/DisplayCRUDTest.php, line 115

Class

DisplayCRUDTest
Tests creation, retrieval, updating, and deletion of displays in the Web UI.

Namespace

Drupal\Tests\views_ui\Functional

Code

public function testDuplicateDisplay() {
    $view = $this->randomView();
    $path_prefix = 'admin/structure/views/view/' . $view['id'] . '/edit';
    $path = $view['page[path]'];
    $this->drupalGet($path_prefix);
    $this->submitForm([], 'Duplicate Page');
    // Verify that the user got redirected to the new display.
    $this->assertSession()
        ->linkByHrefExists($path_prefix . '/page_2', 0, 'Make sure after duplicating the new display appears in the UI');
    $this->assertSession()
        ->addressEquals($path_prefix . '/page_2');
    // Set the title and override the css classes.
    $random_title = $this->randomMachineName();
    $random_css = $this->randomMachineName();
    $this->drupalGet("admin/structure/views/nojs/display/{$view['id']}/page_2/title");
    $this->submitForm([
        'title' => $random_title,
    ], 'Apply');
    $this->drupalGet("admin/structure/views/nojs/display/{$view['id']}/page_2/css_class");
    $this->submitForm([
        'override[dropdown]' => 'page_2',
        'css_class' => $random_css,
    ], 'Apply');
    // Duplicate as a different display type.
    $this->submitForm([], 'Duplicate as Block');
    $this->assertSession()
        ->linkByHrefExists($path_prefix . '/block_1', 0, 'Make sure after duplicating the new display appears in the UI');
    $this->assertSession()
        ->addressEquals($path_prefix . '/block_1');
    $this->assertSession()
        ->pageTextContains('Block settings');
    $this->assertSession()
        ->pageTextNotContains('Page settings');
    $this->submitForm([], 'Save');
    $view = Views::getView($view['id']);
    $view->initDisplay();
    $page_2 = $view->displayHandlers
        ->get('page_2');
    $this->assertNotEmpty($page_2, 'The new page display got saved.');
    $this->assertEquals('Page', $page_2->display['display_title']);
    $this->assertEquals($path, $page_2->display['display_options']['path']);
    $block_1 = $view->displayHandlers
        ->get('block_1');
    $this->assertNotEmpty($block_1, 'The new block display got saved.');
    $this->assertEquals('block', $block_1->display['display_plugin']);
    $this->assertEquals('Block', $block_1->display['display_title'], 'The new display title got generated as expected.');
    $this->assertFalse(isset($block_1->display['display_options']['path']));
    $this->assertEquals($random_title, $block_1->getOption('title'), 'The overridden title option from the display got copied into the duplicate');
    $this->assertEquals($random_css, $block_1->getOption('css_class'), 'The overridden css_class option from the display got copied into the duplicate');
    // Test duplicating a display after changing the machine name.
    $view_id = $view->id();
    $this->drupalGet("admin/structure/views/nojs/display/{$view_id}/page_2/display_id");
    $this->submitForm([
        'display_id' => 'page_new',
    ], 'Apply');
    $this->submitForm([], 'Duplicate as Block');
    $this->submitForm([], 'Save');
    $view = Views::getView($view_id);
    $view->initDisplay();
    $this->assertNotNull($view->displayHandlers
        ->get('page_new'), 'The original display is saved with a changed id');
    $this->assertNotNull($view->displayHandlers
        ->get('block_2'), 'The duplicate display is saved with new id');
}

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