You are here

public function LayoutBuilderStyleTest::testSectionStyles in Layout Builder Styles 8

Test Layout Builder section styles can be created and applied.

File

tests/src/Functional/LayoutBuilderStyleTest.php, line 56

Class

LayoutBuilderStyleTest
Tests the Layout Builder Styles apply as expected.

Namespace

Drupal\Tests\layout_builder_styles\Functional

Code

public function testSectionStyles() {
  $assert_session = $this
    ->assertSession();
  $page = $this
    ->getSession()
    ->getPage();
  $section_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 styles for section.
  LayoutBuilderStyle::create([
    'id' => 'Foobar',
    'label' => 'Foobar',
    'classes' => 'foo-style-class bar-style-class',
    'type' => 'section',
  ])
    ->save();
  LayoutBuilderStyle::create([
    'id' => 'Foobar2',
    'label' => 'Foobar2',
    'classes' => 'foo2-style-class bar2-style-class',
    'type' => 'section',
  ])
    ->save();

  // Add section to node with new styles.
  $this
    ->drupalGet('node/' . $section_node
    ->id());
  $assert_session
    ->responseNotContains('foo-style-class bar-style-class');
  $assert_session
    ->responseNotContains('foo2-style-class bar2-style-class');
  $page
    ->clickLink('Layout');
  $page
    ->clickLink('Add section');
  $page
    ->clickLink('Two column');

  // Verify that only a single option may be selected.
  $assert_session
    ->elementExists('css', 'select#edit-layout-builder-style option');
  $page
    ->selectFieldOption('edit-layout-builder-style', 'Foobar');
  $page
    ->pressButton('Add section');

  // Confirm section element contains the proper classes.
  $page
    ->pressButton('Save layout');
  $assert_session
    ->responseContains('foo-style-class bar-style-class');
  $assert_session
    ->responseNotContains('foo2-style-class bar2-style-class');

  // 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');
  $this
    ->drupalGet('layout_builder/configure/section/overrides/node.' . $section_node
    ->id() . '/0');
  $page
    ->selectFieldOption('edit-layout-builder-style', 'Foobar', TRUE);
  $page
    ->selectFieldOption('edit-layout-builder-style', 'Foobar2', TRUE);
  $page
    ->pressButton('Update');
  $assert_session
    ->responseContains('foo-style-class bar-style-class');
  $assert_session
    ->responseContains('foo2-style-class bar2-style-class');
}