Commit 09b19416 authored by git's avatar git Committed by miro_dietiker

Issue #2711429 by tduong, Berdir: Test ERR default values and configuration dependencies

parent 1074d900
......@@ -40,6 +40,20 @@ entity_reference_revisions.default.handler_settings:
field.storage_settings.entity_reference_revisions:
type: field.storage_settings.entity_reference
field.value.entity_reference_revisions:
type: field.value.entity_reference
label: 'Default value'
mapping:
target_id:
type: integer
label: 'Value'
target_revision_id:
type: integer
label: 'Revision ID'
target_uuid:
type: string
label: 'Target UUID'
field.field_settings.entity_reference_revisions:
type: field.field_settings.entity_reference
......
......@@ -234,10 +234,7 @@ class EntityReferenceRevisionsItem extends EntityReferenceItem implements Option
*/
public function isEmpty() {
// Avoid loading the entity by first checking the 'target_id'.
if ($this->target_id !== NULL) {
return FALSE;
}
if ($this->target_revision_id !== NULL) {
if ($this->target_id !== NULL && $this->target_revision_id !== NULL) {
return FALSE;
}
if ($this->entity && $this->entity instanceof EntityInterface) {
......@@ -259,29 +256,6 @@ class EntityReferenceRevisionsItem extends EntityReferenceItem implements Option
}
}
/**
* {@inheritdoc}
*/
public static function calculateDependencies(FieldDefinitionInterface $field_definition) {
$dependencies = [];
if (is_array($field_definition->getDefaultValueLiteral()) && count($field_definition->getDefaultValueLiteral())) {
$target_entity_type = \Drupal::entityTypeManager()->getDefinition($field_definition->getFieldStorageDefinition()->getSetting('target_type'));
$entity_repository = \Drupal::getContainer()->get('entity.repository');
foreach ($field_definition->getDefaultValueLiteral() as $default_value) {
if (is_array($default_value) && isset($default_value['target_uuid'])) {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $entity_repository->loadEntityByUuid($target_entity_type->id(), $default_value['target_uuid']);
// If the entity does not exist do not create the dependency.
// @see \Drupal\Core\Field\EntityReferenceFieldItemList::processDefaultValue()
if ($entity) {
$dependencies[$target_entity_type->getConfigDependencyKey()][] = $entity->getConfigDependencyName();
}
}
}
}
return $dependencies;
}
/**
* {@inheritdoc}
*/
......
......@@ -3,6 +3,7 @@
namespace Drupal\entity_reference_revisions\Tests;
use Drupal\field_ui\Tests\FieldUiTestTrait;
use Drupal\node\Entity\Node;
use Drupal\simpletest\WebTestBase;
/**
......@@ -54,10 +55,35 @@ class EntityReferenceRevisionsAdminTest extends WebTestBase {
'edit any article content',
));
$this->drupalLogin($admin_user);
// Create entity reference revisions field.
static::fieldUIAddNewField('admin/structure/types/manage/entity_revisions', 'entity_reference_revisions', 'Entity reference revisions', 'entity_reference_revisions', array('settings[target_type]' => 'node', 'cardinality' => '-1'), array('settings[handler_settings][target_bundles][article]' => TRUE));
// Create a test target node used as entity reference by another test node.
$node_target = Node::create([
'title' => 'Target node',
'type' => 'article',
'body' => 'Target body text',
'uuid' => '2d04c2b4-9c3d-4fa6-869e-ecb6fa5c9410',
]);
$node_target->save();
// Add an entity reference revisions field to entity_revisions content type
// with $node_target as default value.
$storage_edit = ['settings[target_type]' => 'node', 'cardinality' => '-1'];
$field_edit = [
'settings[handler_settings][target_bundles][article]' => TRUE,
'default_value_input[field_entity_reference_revisions][0][target_id]' => $node_target->label() . ' (' . $node_target->id() . ')',
];
static::fieldUIAddNewField('admin/structure/types/manage/entity_revisions', 'entity_reference_revisions', 'Entity reference revisions', 'entity_reference_revisions', $storage_edit, $field_edit);
\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
$this->assertText('Saved Entity reference revisions configuration.');
// Resave the target node, so that the default revision is not the one we
// want to use.
$revision_id = $node_target->getRevisionId();
$node_target_after = Node::load($node_target->id());
$node_target_after->setNewRevision();
$node_target_after->save();
$this->assertTrue($node_target_after->getRevisionId() != $revision_id);
// Create an article.
$title = $this->randomMachineName();
$edit = array(
......@@ -70,12 +96,16 @@ class EntityReferenceRevisionsAdminTest extends WebTestBase {
$this->assertText('Revision 1');
$node = $this->drupalGetNodeByTitle($title);
// Create entity revisions content that includes the above article.
$edit = array(
// Check if when creating an entity revisions content the default entity
// reference is set, add also the above article as a new reference.
$this->drupalGet('node/add/entity_revisions');
$this->assertFieldByName('field_entity_reference_revisions[0][target_id]', $node_target->label() . ' (' . $node_target->id() . ')');
$edit = [
'title[0][value]' => 'Entity reference revision content',
'field_entity_reference_revisions[0][target_id]' => $node->label() . ' (' . $node->id() . ')',
);
$this->drupalPostForm('node/add/entity_revisions', $edit, t('Save and publish'));
'field_entity_reference_revisions[1][target_id]' => $node->label() . ' (' . $node->id() . ')',
];
$this->drupalPostForm(NULL, $edit, t('Save and publish'));
$this->assertLinkByHref('node/' . $node_target->id());
$this->assertText('Entity revisions Entity reference revision content has been created.');
$this->assertText('Entity reference revision content');
$this->assertText($title);
......@@ -97,7 +127,7 @@ class EntityReferenceRevisionsAdminTest extends WebTestBase {
$this->assertText('Revision 1');
$this->assertNoText('Revision 2');
// Make sure the non-revisionable entities are not selectable as referenced.
// Make sure the non-revisionable entities are not selectable as referenced
// entities.
$edit = array(
'new_storage_type' => 'entity_reference_revisions',
......@@ -108,8 +138,12 @@ class EntityReferenceRevisionsAdminTest extends WebTestBase {
$this->assertNoOption('edit-settings-target-type', 'user');
$this->assertOption('edit-settings-target-type', 'node');
// Check that the property definitions label are set properly.
$properties = $node->getFieldDefinition('field_entity_reference_revisions')->getFieldStorageDefinition()->getPropertyDefinitions();
// Check ERR default value and property definitions label are set properly.
$field_definition = $node->getFieldDefinition('field_entity_reference_revisions');
$default_value = $field_definition->toArray()['default_value'];
$this->assertEqual($default_value[0]['target_uuid'], $node_target->uuid());
$this->assertEqual($default_value[0]['target_revision_id'], $revision_id);
$properties = $field_definition->getFieldStorageDefinition()->getPropertyDefinitions();
$this->assertEqual((string) $properties['target_revision_id']->getLabel(), 'Content revision ID');
$this->assertEqual((string) $properties['target_id']->getLabel(), 'Content ID');
$this->assertEqual((string) $properties['entity']->getLabel(), 'Content');
......
......@@ -184,4 +184,74 @@ class EntityReferenceRevisionsSaveTest extends KernelTestBase {
static::assertEquals($node_after->composite_reference[0]->target_id, $second_entity_test->id());
static::assertEquals($node_after->composite_reference[0]->target_revision_id, $second_entity_test->getRevisionId());
}
/**
* Tests entity_reference_revisions default value and config dependencies.
*/
public function testEntityReferenceRevisionsDefaultValue() {
// Create a test target node used as entity reference by another test node.
$node_target = Node::create([
'title' => 'Target node',
'type' => 'article',
'body' => 'Target body text',
'uuid' => '2d04c2b4-9c3d-4fa6-869e-ecb6fa5c9410',
]);
$node_target->save();
// Create an entity reference field to reference to the test target node.
/** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
$field_storage = FieldStorageConfig::create([
'field_name' => 'target_node_reference',
'entity_type' => 'node',
'type' => 'entity_reference_revisions',
'settings' => ['target_type' => 'node'],
]);
$field_storage->save();
/** @var \Drupal\field\Entity\FieldConfig $field */
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'article',
'required' => FALSE,
'settings' => ['handler_settings' => ['target_bundles' => ['article' => 'article']]],
]);
// Add reference values to field config that will be used as default value.
$default_value = [
[
'target_id' => $node_target->id(),
'target_revision_id' => $node_target->getRevisionId(),
'target_uuid' => $node_target->uuid(),
],
];
$field->setDefaultValue($default_value)->save();
// Resave the target node, so that the default revision is not the one we
// want to use.
$revision_id = $node_target->getRevisionId();
$node_target_after = Node::load($node_target->id());
$node_target_after->setNewRevision();
$node_target_after->save();
$this->assertTrue($node_target_after->getRevisionId() != $revision_id);
// Create another node.
$node_host = Node::create([
'title' => 'Host node',
'type' => 'article',
'body' => 'Host body text',
'target_node_reference' => $node_target,
]);
$node_host->save();
// Check if the ERR default values are properly created.
$node_host_after = Node::load($node_host->id());
$this->assertEquals($node_host_after->target_node_reference->target_id, $node_target->id());
$this->assertEquals($node_host_after->target_node_reference->target_revision_id, $revision_id);
// Check if the configuration dependencies are properly created.
$dependencies = $field->calculateDependencies()->getDependencies();
$this->assertEquals($dependencies['content'][0], 'node:article:2d04c2b4-9c3d-4fa6-869e-ecb6fa5c9410');
$this->assertEquals($dependencies['config'][0], 'field.storage.node.target_node_reference');
$this->assertEquals($dependencies['config'][1], 'node.type.article');
$this->assertEquals($dependencies['module'][0], 'entity_reference_revisions');
}
}
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