Update core 8.3.0
This commit is contained in:
parent
da7a7918f8
commit
cd7a898e66
6144 changed files with 132297 additions and 87747 deletions
|
@ -0,0 +1,7 @@
|
|||
layout_plugin.settings.test_layout_main_and_footer:
|
||||
type: layout_plugin.settings
|
||||
label: 'Layout test plugin settings'
|
||||
mapping:
|
||||
setting_1:
|
||||
type: string
|
||||
label: 'Setting 1'
|
|
@ -0,0 +1,8 @@
|
|||
name: 'Field Layout test'
|
||||
type: module
|
||||
description: 'Support module for Field Layout tests.'
|
||||
core: 8.x
|
||||
package: Testing
|
||||
version: VERSION
|
||||
dependencies:
|
||||
- entity_test
|
|
@ -0,0 +1,7 @@
|
|||
entity.entity_test.test_view_mode:
|
||||
path: '/entity_test/{entity_test}/test'
|
||||
defaults:
|
||||
_entity_view: 'entity_test.test'
|
||||
_title: 'Test test view mode'
|
||||
requirements:
|
||||
_entity_access: 'entity_test.view'
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\field_layout_test\Plugin\Layout;
|
||||
|
||||
use Drupal\Core\Layout\LayoutDefault;
|
||||
|
||||
/**
|
||||
* Provides an annotated layout plugin for field_layout tests.
|
||||
*
|
||||
* @Layout(
|
||||
* id = "test_layout_content_and_footer",
|
||||
* label = @Translation("Test plugin: Content and Footer"),
|
||||
* category = @Translation("Layout test"),
|
||||
* description = @Translation("Test layout"),
|
||||
* regions = {
|
||||
* "content" = {
|
||||
* "label" = @Translation("Content Region")
|
||||
* },
|
||||
* "footer" = {
|
||||
* "label" = @Translation("Footer Region")
|
||||
* }
|
||||
* },
|
||||
* )
|
||||
*/
|
||||
class TestLayoutContentFooter extends LayoutDefault {
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\field_layout_test\Plugin\Layout;
|
||||
|
||||
use Drupal\Core\Layout\LayoutDefault;
|
||||
|
||||
/**
|
||||
* Provides an annotated layout plugin for field_layout tests.
|
||||
*
|
||||
* @Layout(
|
||||
* id = "test_layout_main_and_footer",
|
||||
* label = @Translation("Test plugin: Main and Footer"),
|
||||
* category = @Translation("Layout test"),
|
||||
* description = @Translation("Test layout"),
|
||||
* regions = {
|
||||
* "main" = {
|
||||
* "label" = @Translation("Main Region")
|
||||
* },
|
||||
* "footer" = {
|
||||
* "label" = @Translation("Footer Region")
|
||||
* }
|
||||
* },
|
||||
* config_dependencies = {
|
||||
* "module" = {
|
||||
* "dependency_from_annotation",
|
||||
* },
|
||||
* },
|
||||
* )
|
||||
*/
|
||||
class TestLayoutMainFooter extends LayoutDefault {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defaultConfiguration() {
|
||||
return [
|
||||
'setting_1' => 'Default',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function calculateDependencies() {
|
||||
$dependencies = parent::calculateDependencies();
|
||||
$dependencies['module'][] = 'dependency_from_calculateDependencies';
|
||||
return $dependencies;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\field_layout\Functional;
|
||||
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
|
||||
/**
|
||||
* Tests using field layout for entity displays.
|
||||
*
|
||||
* @group field_layout
|
||||
*/
|
||||
class FieldLayoutTest extends BrowserTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['field_layout', 'field_ui', 'node'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
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',
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests an entity type that has fields shown by default.
|
||||
*/
|
||||
public function testNodeView() {
|
||||
// By default, the one-column layout is used.
|
||||
$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');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the region titles on the page.
|
||||
*
|
||||
* @return string[]
|
||||
* An array of region titles.
|
||||
*/
|
||||
protected function getRegionTitles() {
|
||||
$region_titles = [];
|
||||
$region_title_elements = $this->getSession()->getPage()->findAll('css', '.region-title td');
|
||||
/** @var \Behat\Mink\Element\NodeElement[] $region_title_elements */
|
||||
foreach ($region_title_elements as $region_title_element) {
|
||||
$region_titles[] = $region_title_element->getText();
|
||||
}
|
||||
return $region_titles;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,266 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\field_layout\FunctionalJavascript;
|
||||
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
|
||||
|
||||
/**
|
||||
* Tests using field layout for entity displays.
|
||||
*
|
||||
* @group field_layout
|
||||
*/
|
||||
class FieldLayoutTest extends JavascriptTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['field_layout', 'field_ui', 'field_layout_test', 'layout_test'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
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',
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that layouts are unique per view mode.
|
||||
*/
|
||||
public function testEntityViewModes() {
|
||||
// By default, the field is not visible.
|
||||
$this->drupalGet('entity_test/1/test');
|
||||
$this->assertSession()->elementNotExists('css', '.layout-region--content .field--name-field-test-text');
|
||||
$this->drupalGet('entity_test/1');
|
||||
$this->assertSession()->elementNotExists('css', '.layout-region--content .field--name-field-test-text');
|
||||
|
||||
// Change the layout for the "test" view mode. See
|
||||
// core.entity_view_mode.entity_test.test.yml.
|
||||
$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');
|
||||
|
||||
// Each view mode has a different layout.
|
||||
$this->drupalGet('entity_test/1/test');
|
||||
$this->assertSession()->elementExists('css', '.layout-region--content .field--name-field-test-text');
|
||||
$this->drupalGet('entity_test/1');
|
||||
$this->assertSession()->elementNotExists('css', '.layout-region--content .field--name-field-test-text');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the use of field layout for entity form displays.
|
||||
*/
|
||||
public function testEntityForm() {
|
||||
// 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', 'Left', 'Right', '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]', 'left');
|
||||
$this->assertSession()->elementExists('css', '.layout-region--left .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', 'Left', 'Right', 'Bottom', 'Disabled'], $this->getRegionTitles());
|
||||
|
||||
// Drag the field to the right region.
|
||||
$field_test_text_row = $this->getSession()->getPage()->find('css', '#field-test-text');
|
||||
$right_region_row = $this->getSession()->getPage()->find('css', '.region-right-message');
|
||||
$field_test_text_row->find('css', '.handle')->dragTo($right_region_row);
|
||||
$this->assertSession()->assertWaitOnAjaxRequest();
|
||||
$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--right .field--name-field-test-text');
|
||||
$this->assertFieldInRegion('field_test_text[0][value]', 'right');
|
||||
|
||||
// Move the field to the right 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]', 'right');
|
||||
$this->assertSession()->assertWaitOnAjaxRequest();
|
||||
$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]', 'right');
|
||||
|
||||
// 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]', 'right');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the use of field layout for entity view displays.
|
||||
*/
|
||||
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', 'Left', 'Right', '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', 'Left', 'Right', 'Bottom', 'Disabled'], $this->getRegionTitles());
|
||||
|
||||
// Drag the field to the left 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');
|
||||
$left_region_row = $this->getSession()->getPage()->find('css', '.region-left-message');
|
||||
$field_test_text_row->find('css', '.handle')->dragTo($left_region_row);
|
||||
$this->assertSession()->assertWaitOnAjaxRequest();
|
||||
$this->assertFalse($this->assertSession()->optionExists('fields[field_test_text][region]', 'hidden')->isSelected());
|
||||
$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--left .field--name-field-test-text');
|
||||
|
||||
// Move the field to the right 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]', 'right');
|
||||
$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--right .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--right .field--name-field-test-text');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests layout plugins with forms.
|
||||
*/
|
||||
public function testLayoutForms() {
|
||||
$this->drupalGet('entity_test/structure/entity_test/display');
|
||||
// Switch to a field layout with settings.
|
||||
$this->click('#edit-field-layouts');
|
||||
|
||||
// Test switching between layouts with and without forms.
|
||||
$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]');
|
||||
|
||||
// Move the test field to the content region.
|
||||
$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');
|
||||
|
||||
// Update the field layout settings.
|
||||
$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');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the region titles on the page.
|
||||
*
|
||||
* @return string[]
|
||||
* An array of region titles.
|
||||
*/
|
||||
protected function getRegionTitles() {
|
||||
$region_titles = [];
|
||||
$region_title_elements = $this->getSession()->getPage()->findAll('css', '.region-title td');
|
||||
/** @var \Behat\Mink\Element\NodeElement[] $region_title_elements */
|
||||
foreach ($region_title_elements as $region_title_element) {
|
||||
$region_titles[] = $region_title_element->getText();
|
||||
}
|
||||
return $region_titles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that a field exists in a given region.
|
||||
*
|
||||
* @param string $field_selector
|
||||
* The field selector, one of field id|name|label|value.
|
||||
* @param string $region_name
|
||||
* The machine name of the region.
|
||||
*/
|
||||
protected function assertFieldInRegion($field_selector, $region_name) {
|
||||
$region_element = $this->getSession()->getPage()->find('css', ".layout-region--$region_name");
|
||||
$this->assertNotNull($region_element);
|
||||
$this->assertSession()->fieldExists($field_selector, $region_element);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,183 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\field_layout\Kernel;
|
||||
|
||||
use Drupal\field_layout\Entity\FieldLayoutEntityViewDisplay;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\field_layout\Entity\FieldLayoutEntityDisplayTrait
|
||||
* @group field_layout
|
||||
*/
|
||||
class FieldLayoutEntityDisplayTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = ['layout_discovery', 'field_layout', 'entity_test', 'field_layout_test', 'system'];
|
||||
|
||||
/**
|
||||
* @covers ::preSave
|
||||
* @covers ::calculateDependencies
|
||||
*/
|
||||
public function testPreSave() {
|
||||
// Create an entity display with one hidden and one visible field.
|
||||
$entity_display = FieldLayoutEntityViewDisplay::create([
|
||||
'targetEntityType' => 'entity_test',
|
||||
'bundle' => 'entity_test',
|
||||
'mode' => 'default',
|
||||
'status' => TRUE,
|
||||
'content' => [
|
||||
'foo' => ['type' => 'visible'],
|
||||
'bar' => ['type' => 'hidden'],
|
||||
'name' => ['type' => 'hidden', 'region' => 'content'],
|
||||
],
|
||||
]);
|
||||
|
||||
$expected = [
|
||||
'langcode' => 'en',
|
||||
'status' => TRUE,
|
||||
'dependencies' => [],
|
||||
'third_party_settings' => [
|
||||
'field_layout' => [
|
||||
'id' => 'layout_onecol',
|
||||
'settings' => [],
|
||||
],
|
||||
],
|
||||
'id' => 'entity_test.entity_test.default',
|
||||
'targetEntityType' => 'entity_test',
|
||||
'bundle' => 'entity_test',
|
||||
'mode' => 'default',
|
||||
'content' => [
|
||||
'foo' => [
|
||||
'type' => 'visible',
|
||||
],
|
||||
'bar' => [
|
||||
'type' => 'hidden',
|
||||
],
|
||||
],
|
||||
'hidden' => [],
|
||||
];
|
||||
$this->assertEntityValues($expected, $entity_display->toArray());
|
||||
|
||||
// Save the display.
|
||||
// the 'content' property and the visible field has the default region set.
|
||||
$entity_display->save();
|
||||
|
||||
// The dependencies have been updated.
|
||||
$expected['dependencies']['module'] = [
|
||||
'entity_test',
|
||||
'field_layout',
|
||||
'layout_discovery',
|
||||
];
|
||||
// A third party setting is added by the entity_test module.
|
||||
$expected['third_party_settings']['entity_test'] = ['foo' => 'bar'];
|
||||
// The visible field is assigned the default region.
|
||||
$expected['content']['foo']['region'] = 'content';
|
||||
// The hidden field is removed from the list of visible fields, and marked
|
||||
// as hidden.
|
||||
unset($expected['content']['bar']);
|
||||
$expected['hidden'] = ['bar' => TRUE];
|
||||
|
||||
$this->assertEntityValues($expected, $entity_display->toArray());
|
||||
|
||||
// Assign a new layout that has default settings and complex dependencies,
|
||||
// but do not save yet.
|
||||
$entity_display->setLayoutId('test_layout_main_and_footer');
|
||||
|
||||
// The default settings were added.
|
||||
$expected['third_party_settings']['field_layout'] = [
|
||||
'id' => 'test_layout_main_and_footer',
|
||||
'settings' => [
|
||||
'setting_1' => 'Default',
|
||||
],
|
||||
];
|
||||
// The field was moved to the default region.
|
||||
$expected['content']['foo'] = [
|
||||
'type' => 'visible',
|
||||
'region' => 'main',
|
||||
'weight' => -4,
|
||||
'settings' => [],
|
||||
'third_party_settings' => [],
|
||||
];
|
||||
$this->assertEntityValues($expected, $entity_display->toArray());
|
||||
|
||||
// After saving, the dependencies have been updated.
|
||||
$entity_display->save();
|
||||
$expected['dependencies']['module'] = [
|
||||
'dependency_from_annotation',
|
||||
'dependency_from_calculateDependencies',
|
||||
'entity_test',
|
||||
'field_layout',
|
||||
'field_layout_test',
|
||||
];
|
||||
$this->assertEntityValues($expected, $entity_display->toArray());
|
||||
|
||||
// Assign a layout with provided settings.
|
||||
$entity_display->setLayoutId('test_layout_main_and_footer', ['setting_1' => 'foobar']);
|
||||
$entity_display->save();
|
||||
|
||||
// The setting overrides the default value.
|
||||
$expected['third_party_settings']['field_layout']['settings']['setting_1'] = 'foobar';
|
||||
$this->assertEntityValues($expected, $entity_display->toArray());
|
||||
|
||||
// Move a field to the non-default region.
|
||||
$component = $entity_display->getComponent('foo');
|
||||
$component['region'] = 'footer';
|
||||
$entity_display->setComponent('foo', $component);
|
||||
$entity_display->save();
|
||||
|
||||
// The field region is saved.
|
||||
$expected['content']['foo']['region'] = 'footer';
|
||||
$this->assertEntityValues($expected, $entity_display->toArray());
|
||||
|
||||
// Assign a different layout that shares the same non-default region.
|
||||
$entity_display->setLayoutId('test_layout_content_and_footer');
|
||||
$entity_display->save();
|
||||
|
||||
// The dependencies have been updated.
|
||||
$expected['dependencies']['module'] = [
|
||||
'entity_test',
|
||||
'field_layout',
|
||||
'field_layout_test',
|
||||
];
|
||||
// The layout has been updated.
|
||||
$expected['third_party_settings']['field_layout'] = [
|
||||
'id' => 'test_layout_content_and_footer',
|
||||
'settings' => [],
|
||||
];
|
||||
// The field remains in its current region instead of moving to the default.
|
||||
$this->assertEntityValues($expected, $entity_display->toArray());
|
||||
|
||||
$this->container->get('module_installer')->uninstall(['field_layout']);
|
||||
|
||||
$entity_storage = $this->container->get('entity_type.manager')->getStorage('entity_view_display');
|
||||
$entity_display = $entity_storage->load('entity_test.entity_test.default');
|
||||
|
||||
// The dependencies have been updated.
|
||||
$expected['dependencies']['module'] = [
|
||||
'entity_test',
|
||||
];
|
||||
// All field_layout settings were removed.
|
||||
unset($expected['third_party_settings']['field_layout']);
|
||||
// The field has returned to the default content region.
|
||||
$expected['content']['foo']['region'] = 'content';
|
||||
$this->assertEntityValues($expected, $entity_display->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts than an entity has the correct values.
|
||||
*
|
||||
* @param mixed $expected
|
||||
* @param array $values
|
||||
* @param string $message
|
||||
*/
|
||||
public static function assertEntityValues($expected, array $values, $message = '') {
|
||||
|
||||
static::assertArrayHasKey('uuid', $values);
|
||||
unset($values['uuid']);
|
||||
|
||||
static::assertEquals($expected, $values, $message);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,308 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\field_layout\Unit;
|
||||
|
||||
use Drupal\Core\Entity\EntityFieldManagerInterface;
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\field_layout\Display\EntityDisplayWithLayoutInterface;
|
||||
use Drupal\field_layout\FieldLayoutBuilder;
|
||||
use Drupal\Core\Layout\LayoutPluginManagerInterface;
|
||||
use Drupal\Core\Layout\LayoutDefault;
|
||||
use Drupal\Core\Layout\LayoutDefinition;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Prophecy\Argument;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\field_layout\FieldLayoutBuilder
|
||||
* @group field_layout
|
||||
*/
|
||||
class FieldLayoutBuilderTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* @var \Drupal\Core\Layout\LayoutPluginManager|\Prophecy\Prophecy\ProphecyInterface
|
||||
*/
|
||||
protected $layoutPluginManager;
|
||||
|
||||
/**
|
||||
* @var \Drupal\Core\Entity\EntityFieldManagerInterface|\Prophecy\Prophecy\ProphecyInterface
|
||||
*/
|
||||
protected $entityFieldManager;
|
||||
|
||||
/**
|
||||
* @var \Drupal\field_layout\FieldLayoutBuilder
|
||||
*/
|
||||
protected $fieldLayoutBuilder;
|
||||
|
||||
/**
|
||||
* @var \Drupal\Core\Layout\LayoutInterface
|
||||
*/
|
||||
protected $layoutPlugin;
|
||||
|
||||
/**
|
||||
* @var \Drupal\Core\Layout\LayoutDefinition
|
||||
*/
|
||||
protected $pluginDefinition;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->pluginDefinition = new LayoutDefinition([
|
||||
'library' => 'field_layout/drupal.layout.twocol',
|
||||
'theme_hook' => 'layout__twocol',
|
||||
'regions' => [
|
||||
'left' => [
|
||||
'label' => 'Left',
|
||||
],
|
||||
'right' => [
|
||||
'label' => 'Right',
|
||||
],
|
||||
],
|
||||
]);
|
||||
$this->layoutPlugin = new LayoutDefault([], 'two_column', $this->pluginDefinition);
|
||||
|
||||
$this->layoutPluginManager = $this->prophesize(LayoutPluginManagerInterface::class);
|
||||
$this->layoutPluginManager->getDefinition('unknown', FALSE)->willReturn(NULL);
|
||||
$this->layoutPluginManager->getDefinition('two_column', FALSE)->willReturn($this->pluginDefinition);
|
||||
|
||||
$this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class);
|
||||
|
||||
$this->fieldLayoutBuilder = new FieldLayoutBuilder($this->layoutPluginManager->reveal(), $this->entityFieldManager->reveal());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::buildView
|
||||
* @covers ::getFields
|
||||
*/
|
||||
public function testBuildView() {
|
||||
$definitions = [];
|
||||
$non_configurable_field_definition = $this->prophesize(FieldDefinitionInterface::class);
|
||||
$non_configurable_field_definition->isDisplayConfigurable('view')->willReturn(FALSE);
|
||||
$definitions['non_configurable_field'] = $non_configurable_field_definition->reveal();
|
||||
$definitions['non_configurable_field_with_extra_field'] = $non_configurable_field_definition->reveal();
|
||||
$this->entityFieldManager->getFieldDefinitions('the_entity_type_id', 'the_entity_type_bundle')->willReturn($definitions);
|
||||
$extra_fields = [];
|
||||
$extra_fields['non_configurable_field_with_extra_field'] = [
|
||||
'label' => 'This non-configurable field is also defined in hook_entity_extra_field_info()',
|
||||
];
|
||||
$this->entityFieldManager->getExtraFields('the_entity_type_id', 'the_entity_type_bundle')->willReturn($extra_fields);
|
||||
|
||||
$build = [
|
||||
'test1' => [
|
||||
'#markup' => 'Test1',
|
||||
],
|
||||
'non_configurable_field' => [
|
||||
'#markup' => 'Non-configurable',
|
||||
],
|
||||
'non_configurable_field_with_extra_field' => [
|
||||
'#markup' => 'Non-configurable with extra field',
|
||||
],
|
||||
];
|
||||
|
||||
$display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
|
||||
$display->getTargetEntityTypeId()->willReturn('the_entity_type_id');
|
||||
$display->getTargetBundle()->willReturn('the_entity_type_bundle');
|
||||
$display->getLayout()->willReturn($this->layoutPlugin);
|
||||
$display->getLayoutId()->willReturn('two_column');
|
||||
$display->getLayoutSettings()->willReturn([]);
|
||||
$display->getComponents()->willReturn([
|
||||
'test1' => [
|
||||
'region' => 'right',
|
||||
],
|
||||
'non_configurable_field' => [
|
||||
'region' => 'left',
|
||||
],
|
||||
'non_configurable_field_with_extra_field' => [
|
||||
'region' => 'left',
|
||||
],
|
||||
]);
|
||||
|
||||
$expected = [
|
||||
'non_configurable_field' => [
|
||||
'#markup' => 'Non-configurable',
|
||||
],
|
||||
'_field_layout' => [
|
||||
'left' => [
|
||||
'non_configurable_field_with_extra_field' => [
|
||||
'#markup' => 'Non-configurable with extra field',
|
||||
],
|
||||
],
|
||||
'right' => [
|
||||
'test1' => [
|
||||
'#markup' => 'Test1',
|
||||
],
|
||||
],
|
||||
'#settings' => [],
|
||||
'#layout' => $this->pluginDefinition,
|
||||
'#theme' => 'layout__twocol',
|
||||
'#attached' => [
|
||||
'library' => [
|
||||
'field_layout/drupal.layout.twocol',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$this->fieldLayoutBuilder->buildView($build, $display->reveal());
|
||||
$this->assertEquals($expected, $build);
|
||||
$this->assertSame($expected, $build);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::buildForm
|
||||
* @covers ::getFields
|
||||
*/
|
||||
public function testBuildForm() {
|
||||
$definitions = [];
|
||||
$non_configurable_field_definition = $this->prophesize(FieldDefinitionInterface::class);
|
||||
$non_configurable_field_definition->isDisplayConfigurable('form')->willReturn(FALSE);
|
||||
$definitions['non_configurable_field'] = $non_configurable_field_definition->reveal();
|
||||
$this->entityFieldManager->getFieldDefinitions('the_entity_type_id', 'the_entity_type_bundle')->willReturn($definitions);
|
||||
$this->entityFieldManager->getExtraFields('the_entity_type_id', 'the_entity_type_bundle')->willReturn([]);
|
||||
|
||||
$build = [
|
||||
'test1' => [
|
||||
'#markup' => 'Test1',
|
||||
],
|
||||
'test2' => [
|
||||
'#markup' => 'Test2',
|
||||
'#group' => 'existing_group',
|
||||
],
|
||||
'field_layout' => [
|
||||
'#markup' => 'Field created through the UI happens to be named "Layout"',
|
||||
],
|
||||
'non_configurable_field' => [
|
||||
'#markup' => 'Non-configurable',
|
||||
],
|
||||
];
|
||||
|
||||
$display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
|
||||
$display->getTargetEntityTypeId()->willReturn('the_entity_type_id');
|
||||
$display->getTargetBundle()->willReturn('the_entity_type_bundle');
|
||||
$display->getLayout()->willReturn($this->layoutPlugin);
|
||||
$display->getLayoutId()->willReturn('two_column');
|
||||
$display->getLayoutSettings()->willReturn([]);
|
||||
$display->getComponents()->willReturn([
|
||||
'test1' => [
|
||||
'region' => 'right',
|
||||
],
|
||||
'test2' => [
|
||||
'region' => 'left',
|
||||
],
|
||||
'field_layout' => [
|
||||
'region' => 'right',
|
||||
],
|
||||
'non_configurable_field' => [
|
||||
'region' => 'left',
|
||||
],
|
||||
]);
|
||||
|
||||
$expected = [
|
||||
'test1' => [
|
||||
'#markup' => 'Test1',
|
||||
'#group' => 'right',
|
||||
],
|
||||
'test2' => [
|
||||
'#markup' => 'Test2',
|
||||
'#group' => 'existing_group',
|
||||
],
|
||||
'field_layout' => [
|
||||
'#markup' => 'Field created through the UI happens to be named "Layout"',
|
||||
'#group' => 'right',
|
||||
],
|
||||
'non_configurable_field' => [
|
||||
'#markup' => 'Non-configurable',
|
||||
],
|
||||
'_field_layout' => [
|
||||
'left' => [
|
||||
'#process' => ['\Drupal\Core\Render\Element\RenderElement::processGroup'],
|
||||
'#pre_render' => ['\Drupal\Core\Render\Element\RenderElement::preRenderGroup'],
|
||||
],
|
||||
'right' => [
|
||||
'#process' => ['\Drupal\Core\Render\Element\RenderElement::processGroup'],
|
||||
'#pre_render' => ['\Drupal\Core\Render\Element\RenderElement::preRenderGroup'],
|
||||
],
|
||||
'#settings' => [],
|
||||
'#layout' => $this->pluginDefinition,
|
||||
'#theme' => 'layout__twocol',
|
||||
'#attached' => [
|
||||
'library' => [
|
||||
'field_layout/drupal.layout.twocol',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$this->fieldLayoutBuilder->buildForm($build, $display->reveal());
|
||||
$this->assertEquals($expected, $build);
|
||||
$this->assertSame($expected, $build);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::buildForm
|
||||
*/
|
||||
public function testBuildFormEmpty() {
|
||||
$definitions = [];
|
||||
$non_configurable_field_definition = $this->prophesize(FieldDefinitionInterface::class);
|
||||
$non_configurable_field_definition->isDisplayConfigurable('form')->willReturn(FALSE);
|
||||
$definitions['non_configurable_field'] = $non_configurable_field_definition->reveal();
|
||||
$this->entityFieldManager->getFieldDefinitions('the_entity_type_id', 'the_entity_type_bundle')->willReturn($definitions);
|
||||
$this->entityFieldManager->getExtraFields('the_entity_type_id', 'the_entity_type_bundle')->willReturn([]);
|
||||
|
||||
$build = [
|
||||
'non_configurable_field' => [
|
||||
'#markup' => 'Non-configurable',
|
||||
],
|
||||
];
|
||||
|
||||
$display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
|
||||
$display->getTargetEntityTypeId()->willReturn('the_entity_type_id');
|
||||
$display->getTargetBundle()->willReturn('the_entity_type_bundle');
|
||||
$display->getLayout()->willReturn($this->layoutPlugin);
|
||||
$display->getLayoutId()->willReturn('two_column');
|
||||
$display->getLayoutSettings()->willReturn([]);
|
||||
$display->getComponents()->willReturn([
|
||||
'test1' => [
|
||||
'region' => 'right',
|
||||
],
|
||||
'non_configurable_field' => [
|
||||
'region' => 'left',
|
||||
],
|
||||
]);
|
||||
|
||||
$expected = [
|
||||
'non_configurable_field' => [
|
||||
'#markup' => 'Non-configurable',
|
||||
],
|
||||
];
|
||||
$this->fieldLayoutBuilder->buildForm($build, $display->reveal());
|
||||
$this->assertSame($expected, $build);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::buildForm
|
||||
*/
|
||||
public function testBuildFormNoLayout() {
|
||||
$this->entityFieldManager->getFieldDefinitions(Argument::any(), Argument::any())->shouldNotBeCalled();
|
||||
|
||||
$build = [
|
||||
'test1' => [
|
||||
'#markup' => 'Test1',
|
||||
],
|
||||
];
|
||||
|
||||
$display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
|
||||
$display->getLayoutId()->willReturn('unknown');
|
||||
$display->getLayoutSettings()->willReturn([]);
|
||||
$display->getComponents()->shouldNotBeCalled();
|
||||
|
||||
$expected = [
|
||||
'test1' => [
|
||||
'#markup' => 'Test1',
|
||||
],
|
||||
];
|
||||
$this->fieldLayoutBuilder->buildForm($build, $display->reveal());
|
||||
$this->assertSame($expected, $build);
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue