function FieldLayoutTest::testEntityView

Same name in other branches
  1. 8.9.x core/modules/field_layout/tests/src/FunctionalJavascript/FieldLayoutTest.php \Drupal\Tests\field_layout\FunctionalJavascript\FieldLayoutTest::testEntityView()
  2. 10 core/modules/field_layout/tests/src/FunctionalJavascript/FieldLayoutTest.php \Drupal\Tests\field_layout\FunctionalJavascript\FieldLayoutTest::testEntityView()
  3. 11.x 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()
        ->pageTextNotContains('The field test text value');
    // 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()
        ->elementTextContains('css', '.layout__region--first', 'The field test text value');
    // 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()
        ->elementTextContains('css', '.layout__region--second', 'The field test text value');
    // 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()
        ->elementTextContains('css', '.layout__region--second', 'The field test text value');
}

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