You are here

public function MoveBlockCategoryRestrictionTest::testMovePluginBlock in Layout Builder Restrictions 8.2

Tests moving a plugin block.

File

modules/layout_builder_restrictions_by_region/tests/src/FunctionalJavascript/MoveBlockCategoryRestrictionTest.php, line 99

Class

MoveBlockCategoryRestrictionTest
Tests moving blocks via the form.

Namespace

Drupal\Tests\layout_builder_restrictions_by_region\FunctionalJavascript

Code

public function testMovePluginBlock() {
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();
  $page
    ->clickLink('Manage layout');
  $assert_session
    ->addressEquals(static::FIELD_UI_PREFIX . '/display/default/layout');
  $expected_block_order = [
    '.block-extra-field-blocknodebundle-with-section-fieldlinks',
    '.block-field-blocknodebundle-with-section-fieldbody',
  ];
  $this
    ->assertRegionBlocksOrder(0, 'content', $expected_block_order);

  // Add a top section using the Two column layout.
  $page
    ->clickLink('Add section');
  $assert_session
    ->waitForElementVisible('css', '#drupal-off-canvas');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $page
    ->clickLink('Two column');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', 'input[value="Add section"]'));
  $page
    ->pressButton('Add section');
  $this
    ->assertRegionBlocksOrder(1, 'content', $expected_block_order);

  // Add a 'Powered by Drupal' block in the 'first' region of the new section.
  $first_region_block_locator = '[data-layout-delta="0"].layout--twocol-section [data-region="first"] [data-layout-block-uuid]';
  $assert_session
    ->elementNotExists('css', $first_region_block_locator);
  $assert_session
    ->elementExists('css', '[data-layout-delta="0"].layout--twocol-section [data-region="first"] .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');
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', $first_region_block_locator));

  // Ensure the request has completed before the test starts.
  $this
    ->waitForNoElement('#drupal-off-canvas');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();

  // From the manage display page, go to manage the layout.
  $this
    ->drupalGet(static::FIELD_UI_PREFIX . "/display/default");
  $assert_session
    ->linkExists('Manage layout');

  // Only allow one-column and two-column layouts.
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-layouts"]/summary');
  $element
    ->click();
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-layouts-layout-restriction-restricted"]');
  $element
    ->click();
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-layouts-layouts-layout-onecol"]');
  $element
    ->click();
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-layouts-layouts-layout-twocol-section"]');
  $element
    ->click();

  // Add a block restriction after the fact to test basic restriction.
  // Restrict all 'Content' fields from options.
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-onecol"]/summary');
  $element
    ->click();
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-by-layout-layout-onecol-table"]/tbody/tr[@data-region="all_regions"]//a');
  $element
    ->click();
  $assert_session
    ->assertWaitOnAjaxRequest();
  $assert_session
    ->checkboxChecked('Allow all existing & new Content fields blocks.');
  $assert_session
    ->checkboxNotChecked('Allow specific Content fields blocks:');
  $element = $page
    ->find('xpath', '//*[starts-with(@id,"edit-allowed-blocks-content-fields-restriction-restrict-all--")]');
  $element
    ->click();
  $element = $page
    ->find('xpath', '//*[starts-with(@id,"edit-submit--")]');
  $element
    ->click();
  $assert_session
    ->assertWaitOnAjaxRequest();
  $page
    ->pressButton('Save');
  $page
    ->clickLink('Manage layout');
  $expected_block_order_1 = [
    '.block-extra-field-blocknodebundle-with-section-fieldlinks',
    '.block-field-blocknodebundle-with-section-fieldbody',
  ];
  $this
    ->assertRegionBlocksOrder(1, 'content', $expected_block_order_1);
  $assert_session
    ->addressEquals(static::FIELD_UI_PREFIX . '/display/default/layout');

  // Attempt to reorder body field in current region.
  $this
    ->openMoveForm(1, 'content', 'block-field-blocknodebundle-with-section-fieldbody', [
    'Links',
    'Body (current)',
  ]);
  $this
    ->moveBlockWithKeyboard('up', 'Body (current)', [
    'Body (current)*',
    'Links',
  ]);
  $page
    ->pressButton('Move');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // Verify that a validation error is provided.
  $modal = $page
    ->find('css', '#drupal-off-canvas p');
  $this
    ->assertSame("There is a restriction on Body placement in the layout_onecol all_regions region for bundle_with_section_field content.", trim($modal
    ->getText()));
}