Commit 0577d20a authored by webchick's avatar webchick

Issue #2366877 by alexpott: Fixed Entity Reference field schema incorrect.

parent 13aef438
......@@ -556,8 +556,11 @@ field.entity_reference.value:
label: 'Default'
mapping:
target_id:
type: integer
type: string
label: 'Value'
target_uuid:
type: string
label: 'Target UUID'
# Schema for the configuration of the Boolean field type.
......
......@@ -8,6 +8,7 @@
namespace Drupal\entity_reference\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\config\Tests\SchemaCheckTestTrait;
use Drupal\simpletest\WebTestBase;
/**
......@@ -16,6 +17,7 @@
* @group entity_reference
*/
class EntityReferenceFieldDefaultValueTest extends WebTestBase {
use SchemaCheckTestTrait;
/**
* Modules to install.
......@@ -88,6 +90,12 @@ function testEntityReferenceDefaultValue() {
// Create a new node to check that UUID has been converted to numeric ID.
$new_node = entity_create('node', array('type' => 'reference_content'));
$this->assertEqual($new_node->get($field_name)->offsetGet(0)->target_id, $referenced_node->id());
// Ensure that the entity reference config schemas are correct.
$field_config = \Drupal::config('field.field.node.reference_content.' . $field_name);
$this->assertConfigSchema(\Drupal::service('config.typed'), $field_config->getName(), $field_config->get());
$field_storage_config = \Drupal::config('field.storage.node.' . $field_name);
$this->assertConfigSchema(\Drupal::service('config.typed'), $field_storage_config->getName(), $field_storage_config->get());
}
}
......@@ -7,6 +7,8 @@
namespace Drupal\entity_reference\Tests;
use Drupal\config\Tests\SchemaCheckTestTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\system\Tests\Entity\EntityUnitTestBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
......@@ -16,6 +18,7 @@
* @group entity_reference
*/
class EntityReferenceFieldTest extends EntityUnitTestBase {
use SchemaCheckTestTrait;
/**
* The entity type used in this test.
......@@ -147,4 +150,47 @@ public function testReferencedEntitiesMultipleLoad() {
}
}
/**
* Tests referencing entities with string IDs.
*/
public function testReferencedEntitiesStringId() {
$field_name = 'entity_reference_string_id';
$this->installEntitySchema('entity_test_string_id');
entity_reference_create_field(
$this->entityType,
$this->bundle,
$field_name,
'Field test',
'entity_test_string_id',
'default',
array('target_bundles' => array($this->bundle)),
FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
);
// Create the parent entity.
$entity = entity_create($this->entityType, array('type' => $this->bundle));
// Create the default target entity.
$target_entity = entity_create('entity_test_string_id', array('id' => $this->randomString(), 'type' => $this->bundle));
$target_entity->save();
// Set the field value.
$entity->{$field_name}->setValue(array(array('target_id' => $target_entity->id())));
// Load the target entities using EntityReferenceField::referencedEntities().
$entities = $entity->{$field_name}->referencedEntities();
$this->assertEqual($entities[0]->id(), $target_entity->id());
// Test that a string ID works as a default value and the field's config
// schema is correct.
$field = FieldConfig::loadByName($this->entityType, $this->bundle, $field_name);
$field->setDefaultValue($target_entity->id());
$field->save();
$this->assertConfigSchema(\Drupal::service('config.typed'), 'field.field.' . $field->id(), $field->toArray());
// Test that the default value works.
$entity = entity_create($this->entityType, array('type' => $this->bundle));
$entities = $entity->{$field_name}->referencedEntities();
$this->assertEqual($entities[0]->id(), $target_entity->id());
}
}
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