function FieldLayoutTest::testEntityForm

Same name in other branches
  1. 9 core/modules/field_layout/tests/src/FunctionalJavascript/FieldLayoutTest.php \Drupal\Tests\field_layout\FunctionalJavascript\FieldLayoutTest::testEntityForm()
  2. 8.9.x core/modules/field_layout/tests/src/FunctionalJavascript/FieldLayoutTest.php \Drupal\Tests\field_layout\FunctionalJavascript\FieldLayoutTest::testEntityForm()
  3. 11.x core/modules/field_layout/tests/src/FunctionalJavascript/FieldLayoutTest.php \Drupal\Tests\field_layout\FunctionalJavascript\FieldLayoutTest::testEntityForm()

Tests the use of field layout for entity form displays.

File

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

Class

FieldLayoutTest
Tests using field layout for entity displays.

Namespace

Drupal\Tests\field_layout\FunctionalJavascript

Code

public function testEntityForm() : void {
    // By default, the one-column layout is used.
    $this->drupalGet('entity_test/manage/1/edit');
    $this->assertFieldInRegion('field_test_text[0][value]', 'content');
    // The one-column layout is in use.
    $this->drupalGet('entity_test/structure/entity_test/form-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');
    // The field is moved to the default region for the new layout.
    $this->assertSession()
        ->pageTextContains('Your settings have been saved.');
    $this->assertEquals([
        'Top',
        'First',
        'Second',
        'Bottom',
        'Disabled',
    ], $this->getRegionTitles());
    $this->drupalGet('entity_test/manage/1/edit');
    // No fields are visible, and the regions don't display when empty.
    $this->assertFieldInRegion('field_test_text[0][value]', 'first');
    $this->assertSession()
        ->elementExists('css', '.layout__region--first .field--name-field-test-text');
    // After a refresh the new regions are still there.
    $this->drupalGet('entity_test/structure/entity_test/form-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 second region.
    $field_test_text_row = $this->getSession()
        ->getPage()
        ->find('css', '#field-test-text');
    $second_region_row = $this->getSession()
        ->getPage()
        ->find('css', '.region-second-message');
    $field_test_text_row->find('css', '.handle')
        ->dragTo($second_region_row);
    $this->assertSession()
        ->assertWaitOnAjaxRequest();
    $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/manage/1/edit');
    $this->assertSession()
        ->elementExists('css', '.layout__region--second .field--name-field-test-text');
    $this->assertFieldInRegion('field_test_text[0][value]', 'second');
    // Tests if this layout works in an embedded context.
    $this->drupalGet('/field-layout-embedded-form');
    $this->assertSession()
        ->elementExists('css', '.layout__region--second .field--name-field-test-text');
    $this->assertFieldInRegion('foo[field_test_text][0][value]', 'second');
    // Move the field to the second region without tabledrag.
    $this->drupalGet('entity_test/structure/entity_test/form-display');
    $this->getSession()
        ->getPage()
        ->pressButton('Show row weights');
    $this->getSession()
        ->getPage()
        ->selectFieldOption('fields[field_test_text][region]', 'second');
    $this->submitForm([], 'Save');
    $this->assertSession()
        ->pageTextContains('Your settings have been saved.');
    // The updated region is used.
    $this->drupalGet('entity_test/manage/1/edit');
    $this->assertFieldInRegion('field_test_text[0][value]', 'second');
    // The layout is still in use without Field UI.
    $this->container
        ->get('module_installer')
        ->uninstall([
        'field_ui',
    ]);
    $this->drupalGet('entity_test/manage/1/edit');
    $this->assertFieldInRegion('field_test_text[0][value]', 'second');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.