Commit 97147960 authored by catch's avatar catch

Issue #2923915 by tstoeckler, heddn: Stubbing content entities with required...

Issue #2923915 by tstoeckler, heddn: Stubbing content entities with required fields with a default value is broken
parent 80aa325d
......@@ -275,7 +275,7 @@ protected function processStubRow(Row $row) {
if ($field_definition->isRequired() && is_null($row->getDestinationProperty($field_name))) {
// Use the configured default value for this specific field, if any.
if ($default_value = $field_definition->getDefaultValueLiteral()) {
$values[] = $default_value;
$values = $default_value;
}
else {
// Otherwise, ask the field type to generate a sample value.
......
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\migrate\Kernel;
use Drupal\entity_test\Entity\EntityTestMul;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\migrate\MigrateExecutable;
......@@ -44,6 +45,11 @@ class MigrateEntityContentBaseTest extends KernelTestBase {
*/
protected function setUp() {
parent::setUp();
// Enable two required fields with default values: a single-value field and
// a multi-value field.
\Drupal::state()->set('entity_test.required_default_field', TRUE);
\Drupal::state()->set('entity_test.required_multi_default_field', TRUE);
$this->installEntitySchema('entity_test_mul');
ConfigurableLanguage::createFromLangcode('en')->save();
......@@ -265,4 +271,34 @@ public function testEmptyDestinations() {
$this->assertNull($entity->version->value);
}
/**
* Tests stub rows.
*/
public function testStubRows() {
// Create a destination.
$this->createDestination([]);
// Import a stub row.
$row = new Row([], [], TRUE);
$row->setDestinationProperty('type', 'test');
$ids = $this->destination->import($row);
$this->assertCount(1, $ids);
// Make sure the entity was saved.
$entity = EntityTestMul::load(reset($ids));
$this->assertInstanceOf(EntityTestMul::class, $entity);
// Make sure the default value was applied to the required fields.
$single_field_name = 'required_default_field';
$single_default_value = $entity->getFieldDefinition($single_field_name)->getDefaultValueLiteral();
$this->assertSame($single_default_value, $entity->get($single_field_name)->getValue());
$multi_field_name = 'required_multi_default_field';
$multi_default_value = $entity->getFieldDefinition($multi_field_name)->getDefaultValueLiteral();
$count = 3;
$this->assertCount($count, $multi_default_value);
for ($i = 0; $i < $count; ++$i) {
$this->assertSame($multi_default_value[$i], $entity->get($multi_field_name)->get($i)->getValue());
}
}
}
......@@ -114,6 +114,23 @@ function entity_test_entity_base_field_info(EntityTypeInterface $entity_type) {
->setLabel('Internal field')
->setInternal(TRUE);
}
if ($entity_type->id() === 'entity_test_mul' && \Drupal::state()->get('entity_test.required_default_field')) {
$fields['required_default_field'] = BaseFieldDefinition::create('string')
->setLabel('Required field with default value')
->setRequired(TRUE)
->setDefaultValue('this is a default value');
}
if ($entity_type->id() === 'entity_test_mul' && \Drupal::state()->get('entity_test.required_multi_default_field')) {
$fields['required_multi_default_field'] = BaseFieldDefinition::create('string')
->setLabel('Required field with default value')
->setRequired(TRUE)
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setDefaultValue([
['value' => 'this is the first default field item'],
['value' => 'this is the second default value'],
['value' => 'you get the idea...'],
]);
}
if ($entity_type->id() == 'entity_test_mulrev' && \Drupal::state()->get('entity_test.field_test_item')) {
$fields['field_test_item'] = BaseFieldDefinition::create('field_test')
->setLabel(t('Field test'))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment