function MoveBlockFormTest::testMoveBlock

Same name in other branches
  1. 9 core/modules/layout_builder/tests/src/FunctionalJavascript/MoveBlockFormTest.php \Drupal\Tests\layout_builder\FunctionalJavascript\MoveBlockFormTest::testMoveBlock()
  2. 8.9.x core/modules/layout_builder/tests/src/FunctionalJavascript/MoveBlockFormTest.php \Drupal\Tests\layout_builder\FunctionalJavascript\MoveBlockFormTest::testMoveBlock()
  3. 10 core/modules/layout_builder/tests/src/FunctionalJavascript/MoveBlockFormTest.php \Drupal\Tests\layout_builder\FunctionalJavascript\MoveBlockFormTest::testMoveBlock()

Tests moving a block.

File

core/modules/layout_builder/tests/src/FunctionalJavascript/MoveBlockFormTest.php, line 101

Class

MoveBlockFormTest
Tests moving blocks via the form.

Namespace

Drupal\Tests\layout_builder\FunctionalJavascript

Code

public function testMoveBlock() : void {
    $page = $this->getSession()
        ->getPage();
    $assert_session = $this->assertSession();
    // Reorder body field in current region.
    $this->openBodyMoveForm(1, 'content', [
        'Links',
        'Body (current)',
    ]);
    $this->moveBlockWithKeyboard('up', 'Body (current)', [
        'Body (current)*',
        'Links',
    ]);
    $page->pressButton('Move');
    $expected_block_order = [
        '.block-field-blocknodebundle-with-section-fieldbody',
        '.block-extra-field-blocknodebundle-with-section-fieldlinks',
    ];
    $this->assertRegionBlocksOrder(1, 'content', $expected_block_order);
    $page->pressButton('Save layout');
    $page->clickLink('Layout');
    $this->assertRegionBlocksOrder(1, 'content', $expected_block_order);
    // Move the body block into the first region above existing block.
    $this->openBodyMoveForm(1, 'content', [
        'Body (current)',
        'Links',
    ]);
    $page->selectFieldOption('Region', '0:first');
    $this->markTestSkipped("Skipped temporarily for random fails.");
    $this->assertBlockTable([
        'Powered by Drupal',
        'Body (current)',
    ]);
    $this->moveBlockWithKeyboard('up', 'Body', [
        'Body (current)*',
        'Powered by Drupal',
    ]);
    $page->pressButton('Move');
    $expected_block_order = [
        '.block-field-blocknodebundle-with-section-fieldbody',
        '.block-system-powered-by-block',
    ];
    $this->assertRegionBlocksOrder(0, 'first', $expected_block_order);
    // Ensure the body block is no longer in the content region.
    $this->assertRegionBlocksOrder(1, 'content', [
        '.block-extra-field-blocknodebundle-with-section-fieldlinks',
    ]);
    $page->pressButton('Save layout');
    $page->clickLink('Layout');
    $this->assertRegionBlocksOrder(0, 'first', $expected_block_order);
    // Move into the second region that has no existing blocks.
    $this->openBodyMoveForm(0, 'first', [
        'Body (current)',
        'Powered by Drupal',
    ]);
    $page->selectFieldOption('Region', '0:second');
    $this->assertBlockTable([
        'Body (current)',
    ]);
    $page->pressButton('Move');
    $this->assertRegionBlocksOrder(0, 'second', [
        '.block-field-blocknodebundle-with-section-fieldbody',
    ]);
    // The weight element uses -10 to 10 by default, which can cause bugs.
    // Add 25 'Powered by Drupal' blocks to a new section.
    $page->clickLink('Add section');
    $assert_session->waitForElementVisible('css', '#drupal-off-canvas');
    $assert_session->assertWaitOnAjaxRequest();
    $page->clickLink('One column');
    $assert_session->assertWaitOnAjaxRequest();
    $this->assertNotEmpty($assert_session->waitForElementVisible('css', 'input[value="Add section"]'));
    $page->pressButton('Add section');
    $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas');
    $large_block_number = 25;
    for ($i = 0; $i < $large_block_number; $i++) {
        $assert_session->elementExists('css', '[data-layout-delta="0"].layout--onecol [data-region="content"] .layout-builder__add-block')
            ->click();
        $this->assertNotEmpty($assert_session->waitForElementVisible('css', '#drupal-off-canvas a:contains("Powered by Drupal")'));
        $assert_session->assertWaitOnAjaxRequest();
        $page->clickLink('Powered by Drupal');
        $this->assertNotEmpty($assert_session->waitForElementVisible('css', 'input[value="Add block"]'));
        $assert_session->assertWaitOnAjaxRequest();
        $page->pressButton('Add block');
        $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas');
    }
    $first_region_block_locator = '[data-layout-delta="0"].layout--onecol [data-region="content"] [data-layout-block-uuid]';
    $assert_session->elementsCount('css', $first_region_block_locator, $large_block_number);
    // Move the Body block to the end of the new section.
    $this->openBodyMoveForm(1, 'second', [
        'Body (current)',
    ]);
    $page->selectFieldOption('Region', '0:content');
    $expected_block_table = array_fill(0, $large_block_number, 'Powered by Drupal');
    $expected_block_table[] = 'Body (current)';
    $this->assertBlockTable($expected_block_table);
    $expected_block_table = array_fill(0, $large_block_number - 1, 'Powered by Drupal');
    $expected_block_table[] = 'Body (current)*';
    $expected_block_table[] = 'Powered by Drupal';
    $this->moveBlockWithKeyboard('up', 'Body', $expected_block_table);
    $page->pressButton('Move');
    $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas');
    // Get all blocks currently in the region.
    $blocks = $page->findAll('css', $first_region_block_locator);
    // The second to last $block should be the body.
    $this->assertTrue($blocks[count($blocks) - 2]->hasClass('block-field-blocknodebundle-with-section-fieldbody'));
}

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