View source
<?php
namespace Drupal\Tests\field_layout\FunctionalJavascript;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class FieldLayoutTest extends WebDriverTestBase {
protected static $modules = [
'field_layout',
'field_ui',
'field_layout_test',
'layout_test',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$entity = EntityTest::create([
'name' => 'The name for this entity',
'field_test_text' => [
[
'value' => 'The field test text value',
],
],
]);
$entity
->save();
$this
->drupalLogin($this
->drupalCreateUser([
'access administration pages',
'view test entity',
'administer entity_test content',
'administer entity_test fields',
'administer entity_test display',
'administer entity_test form display',
'view the administration theme',
]));
}
public function testEntityViewModes() {
$this
->drupalGet('entity_test/1/test');
$this
->assertSession()
->elementNotExists('css', '.layout__region--content ');
$this
->drupalGet('entity_test/1');
$this
->assertSession()
->elementNotExists('css', '.layout__region--content');
$this
->drupalGet('entity_test/structure/entity_test/display');
$this
->click('#edit-modes');
$this
->getSession()
->getPage()
->checkField('display_modes_custom[test]');
$this
->submitForm([], 'Save');
$this
->clickLink('configure them');
$this
->getSession()
->getPage()
->pressButton('Show row weights');
$this
->getSession()
->getPage()
->selectFieldOption('fields[field_test_text][region]', 'content');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->submitForm([], 'Save');
$this
->drupalGet('entity_test/1/test');
$this
->assertSession()
->elementTextContains('css', '.layout__region--content', 'The field test text value');
$this
->drupalGet('entity_test/1');
$this
->assertSession()
->elementNotExists('css', '.layout__region--content');
}
public function testEntityForm() {
$this
->drupalGet('entity_test/manage/1/edit');
$this
->assertFieldInRegion('field_test_text[0][value]', 'content');
$this
->drupalGet('entity_test/structure/entity_test/form-display');
$this
->assertEquals([
'Content',
'Disabled',
], $this
->getRegionTitles());
$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/manage/1/edit');
$this
->assertFieldInRegion('field_test_text[0][value]', 'first');
$this
->assertSession()
->elementExists('css', '.layout__region--first .field--name-field-test-text');
$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();
$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.');
$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');
$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
->assertSession()
->assertWaitOnAjaxRequest();
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextContains('Your settings have been saved.');
$this
->drupalGet('entity_test/manage/1/edit');
$this
->assertFieldInRegion('field_test_text[0][value]', 'second');
$this->container
->get('module_installer')
->uninstall([
'field_ui',
]);
$this
->drupalGet('entity_test/manage/1/edit');
$this
->assertFieldInRegion('field_test_text[0][value]', 'second');
}
public function testEntityView() {
$this
->drupalGet('entity_test/structure/entity_test/display');
$this
->assertEquals([
'Content',
'Disabled',
], $this
->getRegionTitles());
$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');
$this
->assertSession()
->elementNotExists('css', '.layout--twocol');
$this
->assertSession()
->elementNotExists('css', '.layout__region');
$this
->assertSession()
->pageTextNotContains('The field test text value');
$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();
$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.');
$this
->drupalGet('entity_test/1');
$this
->assertSession()
->elementExists('css', '.layout--twocol');
$this
->assertSession()
->elementTextContains('css', '.layout__region--first', 'The field test text value');
$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.');
$this
->drupalGet('entity_test/1');
$this
->assertSession()
->elementTextContains('css', '.layout__region--second', 'The field test text value');
$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');
}
public function testLayoutForms() {
$this
->drupalGet('entity_test/structure/entity_test/display');
$this
->click('#edit-field-layouts');
$this
->getSession()
->getPage()
->selectFieldOption('field_layout', 'layout_test_plugin');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->fieldExists('settings_wrapper[layout_settings][setting_1]');
$this
->getSession()
->getPage()
->selectFieldOption('field_layout', 'layout_test_2col');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->fieldNotExists('settings_wrapper[layout_settings][setting_1]');
$this
->getSession()
->getPage()
->selectFieldOption('field_layout', 'layout_test_plugin');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->fieldExists('settings_wrapper[layout_settings][setting_1]');
$this
->getSession()
->getPage()
->pressButton('Show row weights');
$this
->getSession()
->getPage()
->selectFieldOption('fields[field_test_text][region]', 'content');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->submitForm([], 'Save');
$this
->drupalGet('entity_test/1');
$this
->assertSession()
->pageTextContains('Blah: Default');
$this
->drupalGet('entity_test/structure/entity_test/display');
$this
->click('#edit-field-layouts');
$this
->getSession()
->getPage()
->fillField('settings_wrapper[layout_settings][setting_1]', 'Test text');
$this
->submitForm([], 'Save');
$this
->drupalGet('entity_test/1');
$this
->assertSession()
->pageTextContains('Blah: Test text');
}
public function testChangingFormatterAndRegion() {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->drupalGet('entity_test/structure/entity_test/display');
$page
->find('css', '#field-test-text .handle')
->dragTo($page
->find('css', '.region-content-message'));
$assert_session
->assertWaitOnAjaxRequest();
$page
->pressButton('Save');
$assert_session
->fieldValueEquals('fields[field_test_text][region]', 'content');
$assert_session
->fieldValueEquals('fields[field_test_text][type]', 'text_default');
$this
->click('#edit-field-layouts');
$page
->selectFieldOption('field_layout', 'layout_twocol');
$assert_session
->assertWaitOnAjaxRequest();
$page
->pressButton('Save');
$assert_session
->fieldValueEquals('fields[field_test_text][region]', 'first');
$page
->selectFieldOption('fields[field_test_text][type]', 'text_trimmed');
$assert_session
->assertWaitOnAjaxRequest();
$page
->find('css', '#field-test-text .handle')
->dragTo($page
->find('css', '.region-second-message'));
$assert_session
->assertWaitOnAjaxRequest();
$page
->pressButton('Save');
$assert_session
->fieldValueEquals('fields[field_test_text][region]', 'second');
$assert_session
->fieldValueEquals('fields[field_test_text][type]', 'text_trimmed');
}
protected function getRegionTitles() {
$region_titles = [];
$region_title_elements = $this
->getSession()
->getPage()
->findAll('css', '.region-title td');
foreach ($region_title_elements as $region_title_element) {
$region_titles[] = $region_title_element
->getText();
}
return $region_titles;
}
protected function assertFieldInRegion(string $field_selector, string $region_name) : void {
$region_element = $this
->getSession()
->getPage()
->find('css', ".layout__region--{$region_name}");
$this
->assertNotNull($region_element);
$this
->assertSession()
->fieldExists($field_selector, $region_element);
}
}