You are here

public function LayoutBuilderRestrictionsTest::testBlockRestriction in Layout Builder Restrictions 8

Verify that the UI can restrict blocks in Layout Builder settings tray.

File

tests/src/FunctionalJavascript/LayoutBuilderRestrictionsTest.php, line 47

Class

LayoutBuilderRestrictionsTest
Demonstrate that blocks can be individually restricted.

Namespace

Drupal\Tests\layout_builder_restrictions\FunctionalJavascript

Code

public function testBlockRestriction() {
  $assert_session = $this
    ->assertSession();
  $page = $this
    ->getSession()
    ->getPage();
  $field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field';

  // From the manage display page, go to manage the layout.
  $this
    ->drupalGet("{$field_ui_prefix}/display/default");

  // Checking is_enable will show allow_custom.
  $page
    ->checkField('layout[enabled]');
  $page
    ->checkField('layout[allow_custom]');
  $page
    ->pressButton('Save');
  $assert_session
    ->linkExists('Manage layout');
  $this
    ->clickLink('Manage layout');
  $this
    ->clickLink('Add Block');
  $assert_session
    ->assertWaitOnAjaxRequest();

  // Establish that initially, the body field is available.
  $assert_session
    ->linkExists('Body');

  // Restrict all 'Content' fields from options.
  $this
    ->drupalGet("{$field_ui_prefix}/display/default");
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-layout-builder-restrictions-allowed-blocks"]/summary');
  $element
    ->click();
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-content-fields-restriction-all"]');
  $assert_session
    ->checkboxChecked('edit-layout-builder-restrictions-allowed-blocks-content-fields-restriction-all');
  $assert_session
    ->checkboxNotChecked('edit-layout-builder-restrictions-allowed-blocks-content-fields-restriction-restricted');
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-builder-restrictions-allowed-blocks-content-fields-restriction-restricted"]');
  $element
    ->click();
  $page
    ->pressButton('Save');

  // Establish that the 'body' field is no longer present.
  $this
    ->drupalGet("{$field_ui_prefix}/display/default");
  $assert_session
    ->linkExists('Manage layout');
  $this
    ->clickLink('Manage layout');
  $assert_session
    ->addressEquals("{$field_ui_prefix}/display/default/layout");

  // The "body" field is no longer present.
  $assert_session
    ->elementExists('css', '.field--name-body');
  $this
    ->clickLink('Add Block');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $assert_session
    ->linkNotExists('Body');

  // Allow only 'body' field as an option.
  $this
    ->drupalGet("{$field_ui_prefix}/display/default");
  $element = $page
    ->find('xpath', '//*[@id="edit-layout-layout-builder-restrictions-allowed-blocks"]/summary');
  $element
    ->click();
  $assert_session
    ->checkboxChecked('edit-layout-builder-restrictions-allowed-blocks-content-fields-restriction-restricted');
  $page
    ->checkField('layout_builder_restrictions[allowed_blocks][Content fields][field_block:node:bundle_with_section_field:body]');
  $page
    ->pressButton('Save');
  $this
    ->drupalGet("{$field_ui_prefix}/display/default");
  $assert_session
    ->linkExists('Manage layout');
  $this
    ->clickLink('Manage layout');
  $assert_session
    ->addressEquals("{$field_ui_prefix}/display/default/layout");

  // The "body" field is once again present.
  $assert_session
    ->elementExists('css', '.field--name-body');
  $this
    ->clickLink('Add Block');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $assert_session
    ->linkExists('Body');

  // ... but other 'content' fields aren't.
  $assert_session
    ->linkNotExists('Promoted to front page');
  $assert_session
    ->linkNotExists('Sticky at top of lists');
}