You are here

public function LayoutDisplayTest::testUseLayoutOnDifferentViewDisplay in Layout builder library 8

Tests using the layout on a specific view display.

File

tests/src/Functional/LayoutDisplayTest.php, line 52

Class

LayoutDisplayTest
Test using a layout from the library on a specific entity view display.

Namespace

Drupal\Tests\layout_library\Functional

Code

public function testUseLayoutOnDifferentViewDisplay() {
  $assert_session = $this
    ->assertSession();
  $page = $this
    ->getSession()
    ->getPage();
  $node = $this
    ->createNode([
    'type' => 'cats',
  ]);

  // Add a layout to the library.
  $this
    ->drupalGet('admin/structure/layouts');
  $this
    ->clickLink('Add layout');
  $page
    ->fillField('label', 'Lion');
  $page
    ->fillField('id', 'lion');
  $page
    ->selectFieldOption('_entity_type', 'node:cats');
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextContains('Edit layout for Lion');
  $page
    ->clickLink('Add section');
  $page
    ->clickLink('One column');
  $page
    ->fillField('layout_settings[label]', 'Lion Section');
  $page
    ->pressButton('Add section');
  $page
    ->clickLink('Add block');
  $page
    ->clickLink('Powered by Drupal');
  $page
    ->fillField('settings[label]', 'This is from the library');
  $page
    ->checkField('settings[label_display]');
  $page
    ->pressButton('Add block');
  $page
    ->pressButton('Save layout');

  // Enable full content display, then enable layout builder and library on
  // the default view display.
  $this
    ->drupalGet('admin/structure/types/manage/cats/display');
  $page
    ->checkField('display_modes_custom[full]');
  $page
    ->pressButton('Save');
  $page
    ->checkField('layout[enabled]');
  $page
    ->checkField('layout[library]');
  $page
    ->pressButton('Save');
  $this
    ->drupalGet($node
    ->toUrl());

  // Edit the page to use the layout from the library.
  $page
    ->clickLink('Edit');
  $page
    ->selectFieldOption('Layout', 'lion');
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextNotContains('This is from the library');

  // Disable the full content display.
  $this
    ->drupalGet('admin/structure/types/manage/cats/display');
  $page
    ->uncheckField('display_modes_custom[full]');
  $page
    ->pressButton('Save');
  $this
    ->drupalGet($node
    ->toUrl());
  $assert_session
    ->pageTextContains('This is from the library');

  // Only available in >= D9.1, see
  // https://www.drupal.org/project/layout_library/issues/3082434.
  if (class_exists('Drupal\\layout_builder\\Event\\PrepareLayoutEvent')) {

    // Enable layout overrides.
    $this
      ->drupalGet('admin/structure/types/manage/cats/display');
    $page
      ->checkField('layout[allow_custom]');
    $page
      ->pressButton('Save');

    // Load the page and override the layout.
    $this
      ->drupalGet($node
      ->toUrl());
    $this
      ->clickLink('Layout');
    $assert_session
      ->pageTextContains('This is from the library');
  }
}