You are here

public function FieldLayoutTest::testEntityView in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field_layout/tests/src/FunctionalJavascript/FieldLayoutTest.php \Drupal\Tests\field_layout\FunctionalJavascript\FieldLayoutTest::testEntityView()

Tests the use of field layout for entity view displays.

File

core/modules/field_layout/tests/src/FunctionalJavascript/FieldLayoutTest.php, line 151

Class

FieldLayoutTest
Tests using field layout for entity displays.

Namespace

Drupal\Tests\field_layout\FunctionalJavascript

Code

public function testEntityView() {

  // The one-column layout is in use.
  $this
    ->drupalGet('entity_test/structure/entity_test/display');
  $this
    ->assertEquals([
    'Content',
    'Disabled',
  ], $this
    ->getRegionTitles());

  // Switch the layout to two columns.
  $this
    ->click('#edit-field-layouts');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('field_layout', 'layout_twocol');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Your settings have been saved.');
  $this
    ->assertEquals([
    'Top',
    'First',
    'Second',
    'Bottom',
    'Disabled',
  ], $this
    ->getRegionTitles());
  $this
    ->drupalGet('entity_test/1');

  // No fields are visible, and the regions don't display when empty.
  $this
    ->assertSession()
    ->elementNotExists('css', '.layout--twocol');
  $this
    ->assertSession()
    ->elementNotExists('css', '.layout__region');
  $this
    ->assertSession()
    ->elementNotExists('css', '.field--name-field-test-text');

  // After a refresh the new regions are still there.
  $this
    ->drupalGet('entity_test/structure/entity_test/display');
  $this
    ->assertEquals([
    'Top',
    'First',
    'Second',
    'Bottom',
    'Disabled',
  ], $this
    ->getRegionTitles());
  $this
    ->assertSession()
    ->waitForElement('css', '.tabledrag-handle');
  $id = $this
    ->getSession()
    ->getPage()
    ->find('css', '[name="form_build_id"]')
    ->getValue();

  // Drag the field to the first region.
  $this
    ->assertTrue($this
    ->assertSession()
    ->optionExists('fields[field_test_text][region]', 'hidden')
    ->isSelected());
  $field_test_text_row = $this
    ->getSession()
    ->getPage()
    ->find('css', '#field-test-text');
  $first_region_row = $this
    ->getSession()
    ->getPage()
    ->find('css', '.region-first-message');
  $field_test_text_row
    ->find('css', '.handle')
    ->dragTo($first_region_row);
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertFalse($this
    ->assertSession()
    ->optionExists('fields[field_test_text][region]', 'hidden')
    ->isSelected());
  $this
    ->assertSession()
    ->waitForElement('css', "[name='form_build_id']:not([value='{$id}'])");
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Your settings have been saved.');

  // The new layout is used.
  $this
    ->drupalGet('entity_test/1');
  $this
    ->assertSession()
    ->elementExists('css', '.layout--twocol');
  $this
    ->assertSession()
    ->elementExists('css', '.layout__region--first .field--name-field-test-text');

  // Move the field to the second region without tabledrag.
  $this
    ->drupalGet('entity_test/structure/entity_test/display');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Show row weights');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('fields[field_test_text][region]', 'second');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Your settings have been saved.');

  // The updated region is used.
  $this
    ->drupalGet('entity_test/1');
  $this
    ->assertSession()
    ->elementExists('css', '.layout__region--second .field--name-field-test-text');

  // The layout is still in use without Field UI.
  $this->container
    ->get('module_installer')
    ->uninstall([
    'field_ui',
  ]);
  $this
    ->drupalGet('entity_test/1');
  $this
    ->assertSession()
    ->elementExists('css', '.layout--twocol');
  $this
    ->assertSession()
    ->elementExists('css', '.layout__region--second .field--name-field-test-text');
}