You are here

public function LayoutBuilderStyleRestrictionsTest::testBlockRestrictions in Layout Builder Styles 8

Block type restrictions should apply to inline & reusable blocks.

File

tests/src/Functional/LayoutBuilderStyleRestrictionsTest.php, line 137

Class

LayoutBuilderStyleRestrictionsTest
Tests the Layout Builder Styles apply as expected.

Namespace

Drupal\Tests\layout_builder_styles\Functional

Code

public function testBlockRestrictions() {
  $assert_session = $this
    ->assertSession();
  $page = $this
    ->getSession()
    ->getPage();
  $block_node = $this
    ->createNode([
    'type' => 'bundle_with_section_field',
    'title' => 'The first node title',
    'body' => [
      [
        'value' => 'The first node body',
      ],
    ],
  ]);
  $this
    ->drupalLogin($this
    ->drupalCreateUser([
    'configure any layout',
    'manage layout builder styles',
    'administer layout builder styles configuration',
    'create and edit custom blocks',
  ]));

  // Create 2 custom block types, with block instances.
  $bundle = BlockContentType::create([
    'id' => 'basic',
    'label' => 'Basic',
  ]);
  $bundle
    ->save();
  $bundle = BlockContentType::create([
    'id' => 'alternate',
    'label' => 'Alternate',
  ]);
  $bundle
    ->save();
  block_content_add_body_field($bundle
    ->id());
  $blocks = [
    'Basic Block 1' => 'basic',
    'Alternate Block 1' => 'alternate',
  ];
  foreach ($blocks as $info => $type) {
    $block = BlockContent::create([
      'info' => $info,
      'type' => $type,
      'body' => [
        [
          'value' => 'This is the block content',
          'format' => filter_default_format(),
        ],
      ],
    ]);
    $block
      ->save();
    $blocks[$info] = $block
      ->uuid();
  }

  // Create block styles for blocks.
  LayoutBuilderStyle::create([
    'id' => 'unrestricted',
    'label' => 'Unrestricted',
    'classes' => 'foo-style-class bar-style-class',
    'type' => 'component',
  ])
    ->save();

  // Restrict the 2nd block style to 'basic' blocks.
  LayoutBuilderStyle::create([
    'id' => 'basic_only',
    'label' => 'Basic only',
    'classes' => 'foo2-style-class bar2-style-class',
    'type' => 'component',
    'block_restrictions' => [
      'inline_block:basic',
    ],
  ])
    ->save();

  // Restrict the 3rd block style to only the 'Promoted to frontpage' block.
  LayoutBuilderStyle::create([
    'id' => 'promoted_only',
    'label' => 'Promoted only',
    'classes' => 'foo3-style-class bar3-style-class',
    'type' => 'component',
    'block_restrictions' => [
      'field_block:node:bundle_with_section_field:promote',
    ],
  ])
    ->save();

  // Restrict the 4th block style to 'alternate' or 'promoted'.
  LayoutBuilderStyle::create([
    'id' => 'multi_allow',
    'label' => 'Alternate and promoted',
    'classes' => 'foo4-style-class bar4-style-class',
    'type' => 'component',
    'block_restrictions' => [
      'inline_block:alternate',
      'field_block:node:bundle_with_section_field:promote',
    ],
  ])
    ->save();

  // Block instances are not allowed to be restricted.
  $this
    ->drupalGet('admin/config/content/layout_builder_style/unrestricted/edit');
  foreach (array_values($blocks) as $uuid) {
    $assert_session
      ->elementNotExists('css', 'input[name="block_restrictions[block_content:' . $uuid . ']"]');
  }

  // Set the configuration to allow multiple styles per block.
  $this
    ->drupalGet('/admin/config/content/layout_builder_style/config');
  $page
    ->selectFieldOption('edit-multiselect-multiple', 'multiple');
  $page
    ->selectFieldOption('edit-form-type-multiple-select', 'multiple-select');
  $page
    ->pressButton('Save configuration');

  // Examine which styles are allowed on basic block type.
  $this
    ->drupalGet($block_node
    ->toUrl());
  $page
    ->clickLink('Layout');
  $page
    ->clickLink('Add block');
  $page
    ->clickLink('Basic Block 1');

  // Basic block can use "Unrestricted" and "Basic only".
  $assert_session
    ->elementExists('css', 'select#edit-layout-builder-style option[value="basic_only"]');
  $assert_session
    ->elementExists('css', 'select#edit-layout-builder-style option[value="unrestricted"]');
  $assert_session
    ->elementNotExists('css', 'select#edit-layout-builder-style option[value="promoted_only"]');
  $assert_session
    ->elementNotExists('css', 'select#edit-layout-builder-style option[value="multi_allow"]');

  // Examine which styles are allowed on alternate block type.
  $this
    ->drupalGet($block_node
    ->toUrl());
  $page
    ->clickLink('Layout');
  $page
    ->clickLink('Add block');
  $page
    ->clickLink('Alternate Block 1');

  // Alternate block can use "Unrestricted" and "Alternate only".
  $assert_session
    ->elementNotExists('css', 'select#edit-layout-builder-style option[value="basic_only"]');
  $assert_session
    ->elementExists('css', 'select#edit-layout-builder-style option[value="unrestricted"]');
  $assert_session
    ->elementNotExists('css', 'select#edit-layout-builder-style option[value="promoted_only"]');
  $assert_session
    ->elementExists('css', 'select#edit-layout-builder-style option[value="multi_allow"]');

  // Examine which styles are allowed on 'Promoted to front page'.
  $this
    ->drupalGet($block_node
    ->toUrl());
  $page
    ->clickLink('Layout');
  $page
    ->clickLink('Add block');
  $page
    ->clickLink('Promoted to front page');

  // Promoted gets "Unrestricted", "Alternate and promoted", & "Promoted".
  $assert_session
    ->elementNotExists('css', 'select#edit-layout-builder-style option[value="basic_only"]');
  $assert_session
    ->elementExists('css', 'select#edit-layout-builder-style option[value="unrestricted"]');
  $assert_session
    ->elementExists('css', 'select#edit-layout-builder-style option[value="promoted_only"]');
  $assert_session
    ->elementExists('css', 'select#edit-layout-builder-style option[value="multi_allow"]');

  // Examine which styles are allowed on inline basic block.
  $this
    ->drupalGet($block_node
    ->toUrl());
  $page
    ->clickLink('Layout');
  $page
    ->clickLink('Add block');
  $page
    ->clickLink('Create custom block');
  $page
    ->clickLink('Basic');

  // Basic block can use "Unrestricted" and "Basic only".
  $assert_session
    ->elementExists('css', 'select#edit-layout-builder-style option[value="basic_only"]');
  $assert_session
    ->elementExists('css', 'select#edit-layout-builder-style option[value="unrestricted"]');
  $assert_session
    ->elementNotExists('css', 'select#edit-layout-builder-style option[value="promoted_only"]');
  $assert_session
    ->elementNotExists('css', 'select#edit-layout-builder-style option[value="multi_allow"]');

  // Examine which styles are allowed on inline alternate block.
  $this
    ->drupalGet($block_node
    ->toUrl());
  $page
    ->clickLink('Layout');
  $page
    ->clickLink('Add block');
  $page
    ->clickLink('Create custom block');
  $page
    ->clickLink('Alternate');

  // Alternate block can use "Unrestricted" and "Alternate only".
  $assert_session
    ->elementNotExists('css', 'select#edit-layout-builder-style option[value="basic_only"]');
  $assert_session
    ->elementExists('css', 'select#edit-layout-builder-style option[value="unrestricted"]');
  $assert_session
    ->elementNotExists('css', 'select#edit-layout-builder-style option[value="promoted_only"]');
  $assert_session
    ->elementExists('css', 'select#edit-layout-builder-style option[value="multi_allow"]');
}