View source
<?php
namespace Drupal\Tests\field\Kernel\EntityReference;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Entity\CommentType;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Language\LanguageInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\entity_test\Entity\EntityTestStringId;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
use Drupal\taxonomy\TermInterface;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\file\Entity\File;
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\user\Entity\User;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
class EntityReferenceItemTest extends FieldKernelTestBase {
use EntityReferenceTestTrait;
protected static $modules = [
'node',
'comment',
'file',
'taxonomy',
'text',
'filter',
'views',
'field',
];
protected $vocabulary;
protected $term;
protected $entityStringId;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('entity_test_string_id');
$this
->installEntitySchema('taxonomy_term');
$this
->installEntitySchema('node');
$this
->installEntitySchema('comment');
$this
->installEntitySchema('file');
$this
->installSchema('comment', [
'comment_entity_statistics',
]);
$this
->installSchema('node', [
'node_access',
]);
$this->vocabulary = Vocabulary::create([
'name' => $this
->randomMachineName(),
'vid' => mb_strtolower($this
->randomMachineName()),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$this->vocabulary
->save();
$this->term = Term::create([
'name' => $this
->randomMachineName(),
'vid' => $this->vocabulary
->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$this->term
->save();
NodeType::create([
'type' => $this
->randomMachineName(),
])
->save();
CommentType::create([
'id' => $this
->randomMachineName(),
'target_entity_type_id' => 'node',
])
->save();
$this->entityStringId = EntityTestStringId::create([
'id' => $this
->randomMachineName(),
]);
$this->entityStringId
->save();
$this
->createEntityReferenceField('entity_test', 'entity_test', 'field_test_taxonomy_term', 'Test content entity reference', 'taxonomy_term');
$this
->createEntityReferenceField('entity_test', 'entity_test', 'field_test_entity_test_string_id', 'Test content entity reference with string ID', 'entity_test_string_id');
$this
->createEntityReferenceField('entity_test', 'entity_test', 'field_test_taxonomy_vocabulary', 'Test config entity reference', 'taxonomy_vocabulary');
$this
->createEntityReferenceField('entity_test', 'entity_test', 'field_test_node', 'Test node entity reference', 'node', 'default', [], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$this
->createEntityReferenceField('entity_test', 'entity_test', 'field_test_user', 'Test user entity reference', 'user');
$this
->createEntityReferenceField('entity_test', 'entity_test', 'field_test_comment', 'Test comment entity reference', 'comment');
$this
->createEntityReferenceField('entity_test', 'entity_test', 'field_test_file', 'Test file entity reference', 'file');
$this
->createEntityReferenceField('entity_test_string_id', 'entity_test_string_id', 'field_test_entity_test', 'Test content entity reference with string ID', 'entity_test');
}
public function testContentEntityReferenceItem() {
$tid = $this->term
->id();
$entity = EntityTest::create();
$entity->field_test_taxonomy_term->target_id = $tid;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$entity = EntityTest::load($entity
->id());
$this
->assertInstanceOf(FieldItemListInterface::class, $entity->field_test_taxonomy_term);
$this
->assertInstanceOf(FieldItemInterface::class, $entity->field_test_taxonomy_term[0]);
$this
->assertEquals($tid, $entity->field_test_taxonomy_term->target_id);
$this
->assertEquals($this->term
->getName(), $entity->field_test_taxonomy_term->entity
->getName());
$this
->assertEquals($tid, $entity->field_test_taxonomy_term->entity
->id());
$this
->assertEquals($this->term
->uuid(), $entity->field_test_taxonomy_term->entity
->uuid());
$label = $entity->field_test_taxonomy_term
->getFieldDefinition()
->getFieldStorageDefinition()
->getPropertyDefinition('target_id')
->getLabel();
$this
->assertInstanceOf(TranslatableMarkup::class, $label);
$this
->assertEquals('Taxonomy term ID', $label
->render());
$new_name = $this
->randomMachineName();
$entity->field_test_taxonomy_term->entity
->setName($new_name);
$entity->field_test_taxonomy_term->entity
->save();
$term = Term::load($tid);
$this
->assertEquals($new_name, $term
->getName());
$term2 = Term::create([
'name' => $this
->randomMachineName(),
'vid' => $this->term
->bundle(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$term2
->save();
$entity->field_test_taxonomy_term->target_id = $term
->id();
$this
->assertEquals($term
->id(), $entity->field_test_taxonomy_term->entity
->id());
$this
->assertEquals($term
->getName(), $entity->field_test_taxonomy_term->entity
->getName());
$entity->field_test_taxonomy_term = [
[
'target_id' => $term2
->id(),
],
];
$this
->assertEquals($term2
->id(), $entity->field_test_taxonomy_term->entity
->id());
$this
->assertEquals($term2
->getName(), $entity->field_test_taxonomy_term->entity
->getName());
$entity->field_test_taxonomy_term->entity = $term;
$this
->assertEquals($term
->id(), $entity->field_test_taxonomy_term->target_id);
$this
->assertEquals($term
->getName(), $entity->field_test_taxonomy_term->entity
->getName());
$entity->field_test_taxonomy_term = [
[
'entity' => $term2,
],
];
$this
->assertEquals($term2
->id(), $entity->field_test_taxonomy_term->target_id);
$this
->assertEquals($term2
->getName(), $entity->field_test_taxonomy_term->entity
->getName());
try {
$entity->field_test_taxonomy_term = [
'target_id' => 'invalid',
'entity' => $term2,
];
$this
->fail('Assigning an invalid item throws an exception.');
} catch (\Exception $e) {
$this
->assertInstanceOf(\InvalidArgumentException::class, $e);
}
$term
->delete();
$term2
->delete();
$entity = EntityTest::create([
'name' => $this
->randomMachineName(),
]);
$entity
->save();
$entity = EntityTest::create();
$entity->field_test_taxonomy_term
->generateSampleItems();
$entity->field_test_taxonomy_vocabulary
->generateSampleItems();
$this
->entityValidateAndSave($entity);
$user = User::create([
'name' => $this
->randomString(),
]);
$user
->save();
$entity = EntityTest::create([
'user_id' => [
'target_id' => (int) $user
->id(),
'entity' => $user,
],
]);
}
public function testGenerateSampleValue() {
$entity = EntityTest::create();
$entity->field_test_taxonomy_term
->generateSampleItems();
$this
->assertInstanceOf(TermInterface::class, $entity->field_test_taxonomy_term->entity);
$this
->entityValidateAndSave($entity);
$this->term
->delete();
$entity->field_test_taxonomy_term
->generateSampleItems();
$this
->assertInstanceOf(TermInterface::class, $entity->field_test_taxonomy_term->entity);
$this
->entityValidateAndSave($entity);
}
public function testGenerateSampleValueCircularReference() {
$this->entityStringId
->delete();
$entity_storage = \Drupal::entityTypeManager()
->getStorage('entity_test');
$entity = $entity_storage
->createWithSampleValues('entity_test');
$this
->assertInstanceOf(EntityTestStringId::class, $entity->field_test_entity_test_string_id->entity);
$this
->assertInstanceOf(EntityTest::class, $entity->field_test_entity_test_string_id->entity->field_test_entity_test->entity);
}
public function testContentEntityReferenceItemWithStringId() {
$entity = EntityTest::create();
$entity->field_test_entity_test_string_id->target_id = $this->entityStringId
->id();
$entity
->save();
$storage = \Drupal::entityTypeManager()
->getStorage('entity_test');
$storage
->resetCache();
$this
->assertEquals($this->entityStringId
->id(), $storage
->load($entity
->id())->field_test_entity_test_string_id->target_id);
$label = $entity->field_test_taxonomy_term
->getFieldDefinition()
->getFieldStorageDefinition()
->getPropertyDefinition('target_id')
->getLabel();
$this
->assertInstanceOf(TranslatableMarkup::class, $label);
$this
->assertEquals('Taxonomy term ID', $label
->render());
}
public function testConfigEntityReferenceItem() {
$referenced_entity_id = $this->vocabulary
->id();
$entity = EntityTest::create();
$entity->field_test_taxonomy_vocabulary->target_id = $referenced_entity_id;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$entity = EntityTest::load($entity
->id());
$this
->assertInstanceOf(FieldItemListInterface::class, $entity->field_test_taxonomy_vocabulary);
$this
->assertInstanceOf(FieldItemInterface::class, $entity->field_test_taxonomy_vocabulary[0]);
$this
->assertEquals($referenced_entity_id, $entity->field_test_taxonomy_vocabulary->target_id);
$this
->assertEquals($this->vocabulary
->label(), $entity->field_test_taxonomy_vocabulary->entity
->label());
$this
->assertEquals($referenced_entity_id, $entity->field_test_taxonomy_vocabulary->entity
->id());
$this
->assertEquals($this->vocabulary
->uuid(), $entity->field_test_taxonomy_vocabulary->entity
->uuid());
$new_name = $this
->randomMachineName();
$entity->field_test_taxonomy_vocabulary->entity
->set('name', $new_name);
$entity->field_test_taxonomy_vocabulary->entity
->save();
$vocabulary = Vocabulary::load($referenced_entity_id);
$this
->assertEquals($new_name, $vocabulary
->label());
$vocabulary2 = $vocabulary = Vocabulary::create([
'name' => $this
->randomMachineName(),
'vid' => mb_strtolower($this
->randomMachineName()),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$vocabulary2
->save();
$entity->field_test_taxonomy_vocabulary->target_id = $vocabulary2
->id();
$this
->assertEquals($vocabulary2
->id(), $entity->field_test_taxonomy_vocabulary->entity
->id());
$this
->assertEquals($vocabulary2
->label(), $entity->field_test_taxonomy_vocabulary->entity
->label());
$this->vocabulary
->delete();
$vocabulary2
->delete();
$entity = EntityTest::create([
'name' => $this
->randomMachineName(),
]);
$entity
->save();
}
public function testEntityAutoCreate() {
$term = Term::create([
'name' => $this
->randomMachineName(),
'vid' => $this->term
->bundle(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$entity = EntityTest::create();
$entity->field_test_taxonomy_term->entity = $term;
$entity->name->value = $this
->randomMachineName();
$entity = serialize($entity);
$entity = unserialize($entity);
$entity
->save();
$term = \Drupal::service('entity.repository')
->loadEntityByUuid($term
->getEntityTypeId(), $term
->uuid());
$this
->assertEquals($entity->field_test_taxonomy_term->entity
->id(), $term
->id());
}
public function testEntitySaveOrder() {
$term = Term::create([
'name' => $this
->randomMachineName(),
'vid' => $this->term
->bundle(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$entity = EntityTest::create();
$entity->field_test_taxonomy_term->entity = $term;
$entity->name->value = $this
->randomMachineName();
$value = $entity
->get('field_test_taxonomy_term');
$this
->assertArrayNotHasKey('target_id', $value);
$this
->assertNull($entity->field_test_taxonomy_term->target_id);
$entity->field_test_taxonomy_term = $value;
$term
->save();
$entity
->save();
$this
->assertEquals($term
->id(), $entity->field_test_taxonomy_term->entity
->id());
}
public function testSelectionHandlerSettings() {
$field_name = mb_strtolower($this
->randomMachineName());
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'entity_reference',
'settings' => [
'target_type' => 'entity_test',
],
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'entity_test',
]);
$field
->save();
$field = FieldConfig::load($field
->id());
$this
->assertEquals('default:entity_test', $field
->getSetting('handler'));
$field_storage
->setSetting('target_type', 'entity_test_rev');
$field_storage
->save();
$field = FieldConfig::load($field
->id());
$this
->assertEquals('default:entity_test_rev', $field
->getSetting('handler'));
$field
->setSetting('handler', 'views');
$field
->save();
$field = FieldConfig::load($field
->id());
$this
->assertEquals('views', $field
->getSetting('handler'));
$field_storage
->setSetting('target_type', 'entity_test_rev');
$field_storage
->save();
$field = FieldConfig::load($field
->id());
$this
->assertEquals('views', $field
->getSetting('handler'));
}
public function testAutocreateValidation() {
$term = Term::create([
'name' => $this
->randomMachineName(),
'vid' => $this->term
->bundle(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$entity = EntityTest::create([
'field_test_taxonomy_term' => [
'entity' => $term,
'target_id' => NULL,
],
]);
$errors = $entity
->validate();
$this
->assertCount(0, $errors);
$term
->save();
$entity = EntityTest::create([
'field_test_taxonomy_term' => [
'entity' => $term,
'target_id' => NULL,
],
]);
$errors = $entity
->validate();
$this
->assertCount(1, $errors);
$this
->assertEquals('This value should not be null.', $errors[0]
->getMessage());
$this
->assertEquals('field_test_taxonomy_term.0', $errors[0]
->getPropertyPath());
$entity
->save();
$errors = $entity
->validate();
$this
->assertCount(0, $errors);
$title = $this
->randomString();
$node = Node::create([
'title' => $title,
'type' => 'node',
'status' => NodeInterface::NOT_PUBLISHED,
]);
$entity = EntityTest::create([
'field_test_node' => [
'entity' => $node,
],
]);
$errors = $entity
->validate();
$this
->assertCount(1, $errors);
$this
->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
'%type' => 'node',
'%label' => $title,
]), $errors[0]
->getMessage());
$this
->assertEquals('field_test_node.0.entity', $errors[0]
->getPropertyPath());
$node
->setPublished();
$errors = $entity
->validate();
$this
->assertCount(0, $errors);
$unsaved_unpublished_node_title = $this
->randomString();
$unsaved_unpublished_node = Node::create([
'title' => $unsaved_unpublished_node_title,
'type' => 'node',
'status' => NodeInterface::NOT_PUBLISHED,
]);
$saved_unpublished_node_title = $this
->randomString();
$saved_unpublished_node = Node::create([
'title' => $saved_unpublished_node_title,
'type' => 'node',
'status' => NodeInterface::NOT_PUBLISHED,
]);
$saved_unpublished_node
->save();
$saved_published_node_title = $this
->randomString();
$saved_published_node = Node::create([
'title' => $saved_published_node_title,
'type' => 'node',
'status' => NodeInterface::PUBLISHED,
]);
$saved_published_node
->save();
$entity = EntityTest::create([
'field_test_node' => [
[
'entity' => $unsaved_unpublished_node,
],
[
'target_id' => $saved_unpublished_node
->id(),
],
[
'target_id' => $saved_published_node
->id(),
],
],
]);
$errors = $entity
->validate();
$this
->assertCount(2, $errors);
$this
->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
'%type' => 'node',
'%label' => $unsaved_unpublished_node_title,
]), $errors[0]
->getMessage());
$this
->assertEquals('field_test_node.0.entity', $errors[0]
->getPropertyPath());
$this
->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
'%type' => 'node',
'%label' => $saved_unpublished_node
->id(),
]), $errors[1]
->getMessage());
$this
->assertEquals('field_test_node.1.target_id', $errors[1]
->getPropertyPath());
$saved_unpublished_node
->setPublished();
$saved_unpublished_node
->save();
$errors = $entity
->validate();
$this
->assertCount(1, $errors);
$this
->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
'%type' => 'node',
'%label' => $unsaved_unpublished_node_title,
]), $errors[0]
->getMessage());
$this
->assertEquals('field_test_node.0.entity', $errors[0]
->getPropertyPath());
$unsaved_unpublished_node
->setPublished();
$errors = $entity
->validate();
$this
->assertCount(0, $errors);
$title = $this
->randomString();
$comment = Comment::create([
'subject' => $title,
'comment_type' => 'comment',
'status' => 0,
]);
$entity = EntityTest::create([
'field_test_comment' => [
'entity' => $comment,
],
]);
$errors = $entity
->validate();
$this
->assertCount(1, $errors);
$this
->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
'%type' => 'comment',
'%label' => $title,
]), $errors[0]
->getMessage());
$this
->assertEquals('field_test_comment.0.entity', $errors[0]
->getPropertyPath());
$comment
->setPublished();
$errors = $entity
->validate();
$this
->assertCount(0, $errors);
$name = $this
->randomString();
$user = User::create([
'name' => $name,
'status' => 0,
]);
$entity = EntityTest::create([
'field_test_user' => [
'entity' => $user,
],
]);
$errors = $entity
->validate();
$this
->assertCount(1, $errors);
$this
->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
'%type' => 'user',
'%label' => $name,
]), $errors[0]
->getMessage());
$this
->assertEquals('field_test_user.0.entity', $errors[0]
->getPropertyPath());
$user
->activate();
$errors = $entity
->validate();
$this
->assertCount(0, $errors);
$filename = $this
->randomMachineName() . '.txt';
$file = File::create([
'filename' => $filename,
'status' => 0,
]);
$entity = EntityTest::create([
'field_test_file' => [
'entity' => $file,
],
]);
$errors = $entity
->validate();
$this
->assertCount(1, $errors);
$this
->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
'%type' => 'file',
'%label' => $filename,
]), $errors[0]
->getMessage());
$this
->assertEquals('field_test_file.0.entity', $errors[0]
->getPropertyPath());
$file
->setPermanent();
$errors = $entity
->validate();
$this
->assertCount(0, $errors);
}
}