View source
<?php
namespace Drupal\Tests\field_layout\Kernel;
use Drupal\field_layout\Entity\FieldLayoutEntityViewDisplay;
use Drupal\KernelTests\KernelTestBase;
class FieldLayoutEntityDisplayTest extends KernelTestBase {
protected static $modules = [
'layout_discovery',
'field_layout',
'entity_test',
'field_layout_test',
'system',
];
public function testPreSave() {
$entity_display = FieldLayoutEntityViewDisplay::create([
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
'status' => TRUE,
'content' => [
'foo' => [
'type' => 'visible',
],
'name' => [
'type' => 'hidden',
'region' => 'content',
],
],
'hidden' => [
'bar' => TRUE,
],
]);
$expected = [
'langcode' => 'en',
'status' => TRUE,
'dependencies' => [],
'third_party_settings' => [
'field_layout' => [
'id' => 'layout_onecol',
'settings' => [
'label' => '',
],
],
],
'id' => 'entity_test.entity_test.default',
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
'content' => [
'foo' => [
'type' => 'visible',
],
],
'hidden' => [
'bar' => TRUE,
],
];
$this
->assertEntityValues($expected, $entity_display
->toArray());
$entity_display
->save();
$expected['dependencies']['module'] = [
'entity_test',
'field_layout',
'layout_discovery',
];
$expected['third_party_settings']['entity_test'] = [
'foo' => 'bar',
];
$expected['content']['foo']['region'] = 'content';
$this
->assertEntityValues($expected, $entity_display
->toArray());
$entity_display
->setLayoutId('test_layout_main_and_footer');
$expected['third_party_settings']['field_layout'] = [
'id' => 'test_layout_main_and_footer',
'settings' => [
'setting_1' => 'Default',
],
];
$expected['content']['foo'] = [
'type' => 'visible',
'region' => 'main',
'weight' => -4,
'settings' => [],
'third_party_settings' => [],
];
$this
->assertEntityValues($expected, $entity_display
->toArray());
$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());
$entity_display
->setLayoutId('test_layout_main_and_footer', [
'setting_1' => 'foobar',
]);
$entity_display
->save();
$expected['third_party_settings']['field_layout']['settings']['setting_1'] = 'foobar';
$this
->assertEntityValues($expected, $entity_display
->toArray());
$component = $entity_display
->getComponent('foo');
$component['region'] = 'footer';
$entity_display
->setComponent('foo', $component);
$entity_display
->save();
$expected['content']['foo']['region'] = 'footer';
$this
->assertEntityValues($expected, $entity_display
->toArray());
$entity_display
->setLayoutId('test_layout_content_and_footer');
$entity_display
->save();
$expected['dependencies']['module'] = [
'entity_test',
'field_layout',
'field_layout_test',
];
$expected['third_party_settings']['field_layout'] = [
'id' => 'test_layout_content_and_footer',
'settings' => [
'label' => '',
],
];
$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');
$expected['dependencies']['module'] = [
'entity_test',
];
unset($expected['third_party_settings']['field_layout']);
$expected['content']['foo']['region'] = 'content';
$this
->assertEntityValues($expected, $entity_display
->toArray());
}
public static function assertEntityValues($expected, array $values, $message = '') {
static::assertArrayHasKey('uuid', $values);
unset($values['uuid']);
static::assertEquals($expected, $values, $message);
}
}