View source
<?php
namespace Drupal\Tests\field_layout\Functional;
use Drupal\Tests\BrowserTestBase;
class FieldLayoutTest extends BrowserTestBase {
protected static $modules = [
'field_layout',
'field_ui',
'node',
'field_layout_test',
];
protected $defaultTheme = 'classy';
protected function setUp() : void {
parent::setUp();
$this
->createContentType([
'type' => 'article',
]);
$this
->createNode([
'type' => 'article',
'title' => 'The node title',
'body' => [
[
'value' => 'The node body',
],
],
]);
$this
->drupalLogin($this
->drupalCreateUser([
'access administration pages',
'administer content types',
'administer nodes',
'administer node fields',
'administer node display',
'administer node form display',
'view the administration theme',
]));
}
public function testNodeView() {
$this
->drupalGet('node/1');
$this
->assertSession()
->elementExists('css', '.layout--onecol');
$this
->assertSession()
->elementExists('css', '.layout__region--content .field--name-body');
$this
->drupalGet('admin/structure/types/manage/article/display');
$this
->assertEquals([
'Content',
'Disabled',
], $this
->getRegionTitles());
$this
->assertSession()
->optionExists('fields[body][region]', 'content');
}
public function testRegionChanges() {
$this
->drupalGet('admin/structure/types/manage/article/display');
$this
->assertEquals([
'Content',
'Disabled',
], $this
->getRegionTitles());
$this
->assertSession()
->optionExists('fields[body][region]', 'content');
\Drupal::state()
->set('field_layout_test.alter_regions', TRUE);
\Drupal::service('plugin.cache_clearer')
->clearCachedDefinitions();
$this
->drupalGet('admin/structure/types/manage/article/display');
$this
->assertEquals([
'Foo',
'Disabled',
], $this
->getRegionTitles());
$this
->assertSession()
->optionExists('fields[body][region]', 'hidden');
}
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;
}
}