View source
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Core\Entity\Display\EntityDisplayInterface;
use Drupal\Core\Form\FormState;
use Drupal\field_ui\Form\EntityViewDisplayEditForm;
use Drupal\KernelTests\KernelTestBase;
class EntityDisplayFormBaseTest extends KernelTestBase {
protected static $modules = [
'entity_test',
];
public function testCopyFormValuesToEntity() {
$field_values = [];
$entity = $this
->prophesize(EntityDisplayInterface::class);
$entity
->getPluginCollections()
->willReturn([]);
$entity
->getTargetEntityTypeId()
->willReturn('entity_test_with_bundle');
$entity
->getTargetBundle()
->willReturn('target_bundle');
$entity
->getComponent('new_field_mismatch_type_visible')
->willReturn([]);
$field_values['new_field_mismatch_type_visible'] = [
'weight' => 0,
'type' => 'textfield',
'region' => 'hidden',
];
$entity
->removeComponent('new_field_mismatch_type_visible')
->will(function ($args) {
$this
->getComponent($args[0])
->willReturn([]);
})
->shouldBeCalled();
$entity
->getComponent('field_visible_no_changes')
->willReturn([
'weight' => 0,
'type' => 'textfield',
'region' => 'content',
]);
$field_values['field_visible_no_changes'] = [
'weight' => 0,
'type' => 'textfield',
'region' => 'content',
];
$entity
->setComponent('field_visible_no_changes', [
'weight' => 0,
'type' => 'textfield',
'region' => 'content',
])
->shouldBeCalled();
$entity
->getComponent('field_start_visible_change_region')
->willReturn([
'weight' => 0,
'type' => 'textfield',
'region' => 'content',
]);
$field_values['field_start_visible_change_region'] = [
'weight' => 0,
'type' => 'textfield',
'region' => 'hidden',
];
$entity
->removeComponent('field_start_visible_change_region')
->will(function ($args) {
$this
->getComponent($args[0])
->willReturn([]);
})
->shouldBeCalled();
$entity
->getComponent('field_plugin_settings_update')
->willReturn([
'weight' => 0,
'type' => 'textfield',
'region' => 'content',
]);
$field_values['field_plugin_settings_update'] = [
'weight' => 0,
'type' => 'textfield',
'region' => 'content',
'settings_edit_form' => [
'third_party_settings' => [
'foo' => 'bar',
],
],
];
$entity
->setComponent('field_plugin_settings_update', [
'weight' => 0,
'type' => 'textfield',
'region' => 'content',
])
->will(function ($args) {
$this
->getComponent($args[0])
->willReturn($args[1]);
$args[1] += [
'settings' => [],
'third_party_settings' => [
'foo' => 'bar',
],
];
$this
->setComponent($args[0], $args[1])
->shouldBeCalled();
})
->shouldBeCalled();
$form_object = new EntityViewDisplayEditForm($this->container
->get('plugin.manager.field.field_type'), $this->container
->get('plugin.manager.field.formatter'), $this->container
->get('entity_display.repository'), $this->container
->get('entity_field.manager'));
$form_object
->setEntity($entity
->reveal());
$form = [
'#fields' => array_keys($field_values),
'#extra' => [],
];
$form_state = new FormState();
$form_state
->setValues([
'fields' => $field_values,
]);
$form_state
->setProcessInput();
$form_object
->buildEntity($form, $form_state);
$form_state
->setSubmitted();
$form_state
->set('plugin_settings_update', 'field_plugin_settings_update');
$form_object
->buildEntity($form, $form_state);
}
}