View source
<?php
namespace Drupal\field\Tests\EntityReference;
use Drupal\comment\Entity\Comment;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
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\field\Tests\FieldUnitTestBase;
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;
class EntityReferenceItemTest extends FieldUnitTestBase {
use EntityReferenceTestTrait;
public static $modules = [
'node',
'comment',
'file',
'taxonomy',
'text',
'filter',
'views',
'field',
];
protected $vocabulary;
protected $term;
protected $entityStringId;
protected function setUp() {
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->vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => $this
->randomMachineName(),
'vid' => Unicode::strtolower($this
->randomMachineName()),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
$this->vocabulary
->save();
$this->term = entity_create('taxonomy_term', array(
'name' => $this
->randomMachineName(),
'vid' => $this->vocabulary
->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
$this->term
->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');
$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');
}
public function testContentEntityReferenceItem() {
$tid = $this->term
->id();
$entity = entity_create('entity_test');
$entity->field_test_taxonomy_term->target_id = $tid;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$entity = entity_load('entity_test', $entity
->id());
$this
->assertTrue($entity->field_test_taxonomy_term instanceof FieldItemListInterface, 'Field implements interface.');
$this
->assertTrue($entity->field_test_taxonomy_term[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this
->assertEqual($entity->field_test_taxonomy_term->target_id, $tid);
$this
->assertEqual($entity->field_test_taxonomy_term->entity
->getName(), $this->term
->getName());
$this
->assertEqual($entity->field_test_taxonomy_term->entity
->id(), $tid);
$this
->assertEqual($entity->field_test_taxonomy_term->entity
->uuid(), $this->term
->uuid());
$label = $entity->field_test_taxonomy_term
->getFieldDefinition()
->getFieldStorageDefinition()
->getPropertyDefinition('target_id')
->getLabel();
$this
->assertTrue($label instanceof TranslatableMarkup);
$this
->assertEqual($label
->render(), 'Taxonomy term ID');
$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
->assertEqual($term
->getName(), $new_name);
$term2 = entity_create('taxonomy_term', array(
'name' => $this
->randomMachineName(),
'vid' => $this->term
->bundle(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
$term2
->save();
$entity->field_test_taxonomy_term->target_id = $term
->id();
$this
->assertEqual($entity->field_test_taxonomy_term->entity
->id(), $term
->id());
$this
->assertEqual($entity->field_test_taxonomy_term->entity
->getName(), $term
->getName());
$entity->field_test_taxonomy_term = [
[
'target_id' => $term2
->id(),
],
];
$this
->assertEqual($entity->field_test_taxonomy_term->entity
->id(), $term2
->id());
$this
->assertEqual($entity->field_test_taxonomy_term->entity
->getName(), $term2
->getName());
$entity->field_test_taxonomy_term->entity = $term;
$this
->assertEqual($entity->field_test_taxonomy_term->target_id, $term
->id());
$this
->assertEqual($entity->field_test_taxonomy_term->entity
->getName(), $term
->getName());
$entity->field_test_taxonomy_term = [
[
'entity' => $term2,
],
];
$this
->assertEqual($entity->field_test_taxonomy_term->target_id, $term2
->id());
$this
->assertEqual($entity->field_test_taxonomy_term->entity
->getName(), $term2
->getName());
try {
$entity->field_test_taxonomy_term = [
'target_id' => 'invalid',
'entity' => $term2,
];
$this
->fail('Assigning an invalid item throws an exception.');
} catch (\InvalidArgumentException $e) {
$this
->pass('Assigning an invalid item throws an exception.');
}
$term
->delete();
$term2
->delete();
$entity = entity_create('entity_test', array(
'name' => $this
->randomMachineName(),
));
$entity
->save();
$entity = entity_create('entity_test');
$entity->field_test_taxonomy_term
->generateSampleItems();
$entity->field_test_taxonomy_vocabulary
->generateSampleItems();
$this
->entityValidateAndSave($entity);
}
public function testContentEntityReferenceItemWithStringId() {
$entity = EntityTest::create();
$entity->field_test_entity_test_string_id->target_id = $this->entityStringId
->id();
$entity
->save();
$storage = \Drupal::entityManager()
->getStorage('entity_test');
$storage
->resetCache();
$this
->assertEqual($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
->assertTrue($label instanceof TranslatableMarkup);
$this
->assertEqual($label
->render(), 'Taxonomy term ID');
}
public function testConfigEntityReferenceItem() {
$referenced_entity_id = $this->vocabulary
->id();
$entity = entity_create('entity_test');
$entity->field_test_taxonomy_vocabulary->target_id = $referenced_entity_id;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$entity = entity_load('entity_test', $entity
->id());
$this
->assertTrue($entity->field_test_taxonomy_vocabulary instanceof FieldItemListInterface, 'Field implements interface.');
$this
->assertTrue($entity->field_test_taxonomy_vocabulary[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this
->assertEqual($entity->field_test_taxonomy_vocabulary->target_id, $referenced_entity_id);
$this
->assertEqual($entity->field_test_taxonomy_vocabulary->entity
->label(), $this->vocabulary
->label());
$this
->assertEqual($entity->field_test_taxonomy_vocabulary->entity
->id(), $referenced_entity_id);
$this
->assertEqual($entity->field_test_taxonomy_vocabulary->entity
->uuid(), $this->vocabulary
->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
->assertEqual($vocabulary
->label(), $new_name);
$vocabulary2 = entity_create('taxonomy_vocabulary', array(
'name' => $this
->randomMachineName(),
'vid' => Unicode::strtolower($this
->randomMachineName()),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
$vocabulary2
->save();
$entity->field_test_taxonomy_vocabulary->target_id = $vocabulary2
->id();
$this
->assertEqual($entity->field_test_taxonomy_vocabulary->entity
->id(), $vocabulary2
->id());
$this
->assertEqual($entity->field_test_taxonomy_vocabulary->entity
->label(), $vocabulary2
->label());
$this->vocabulary
->delete();
$vocabulary2
->delete();
$entity = entity_create('entity_test', array(
'name' => $this
->randomMachineName(),
));
$entity
->save();
}
public function testEntityAutoCreate() {
$term = Term::create(array(
'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::entityManager()
->loadEntityByUuid($term
->getEntityTypeId(), $term
->uuid());
$this
->assertEqual($entity->field_test_taxonomy_term->entity
->id(), $term
->id());
}
public function testEntitySaveOrder() {
$term = entity_create('taxonomy_term', array(
'name' => $this
->randomMachineName(),
'vid' => $this->term
->bundle(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
$entity = entity_create('entity_test');
$entity->field_test_taxonomy_term->entity = $term;
$entity->name->value = $this
->randomMachineName();
$value = $entity
->get('field_test_taxonomy_term');
$this
->assertTrue(empty($value['target_id']));
$this
->assertNull($entity->field_test_taxonomy_term->target_id);
$entity->field_test_taxonomy_term = $value;
$term
->save();
$entity
->save();
$this
->assertEqual($entity->field_test_taxonomy_term->entity
->id(), $term
->id());
}
public function testSelectionHandlerSettings() {
$field_name = Unicode::strtolower($this
->randomMachineName());
$field_storage = FieldStorageConfig::create(array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'entity_reference',
'settings' => array(
'target_type' => 'entity_test',
),
));
$field_storage
->save();
$field = FieldConfig::create(array(
'field_storage' => $field_storage,
'bundle' => 'entity_test',
));
$field
->save();
$field = FieldConfig::load($field
->id());
$this
->assertEqual($field
->getSetting('handler'), 'default:entity_test');
$field_storage
->setSetting('target_type', 'entity_test_rev');
$field_storage
->save();
$field = FieldConfig::load($field
->id());
$this
->assertEqual($field
->getSetting('handler'), 'default:entity_test_rev');
$field
->setSetting('handler', 'views');
$field
->save();
$field = FieldConfig::load($field
->id());
$this
->assertEqual($field
->getSetting('handler'), 'views');
$field_storage
->setSetting('target_type', 'entity_test_rev');
$field_storage
->save();
$field = FieldConfig::load($field
->id());
$this
->assertEqual($field
->getSetting('handler'), 'views');
}
public function testAutocreateValidation() {
$term = Term::create(array(
'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
->assertEqual(0, count($errors));
$term
->save();
$entity = EntityTest::create([
'field_test_taxonomy_term' => [
'entity' => $term,
'target_id' => NULL,
],
]);
$errors = $entity
->validate();
$this
->assertEqual(1, count($errors));
$this
->assertEqual($errors[0]
->getMessage(), 'This value should not be null.');
$this
->assertEqual($errors[0]
->getPropertyPath(), 'field_test_taxonomy_term.0');
$entity
->save();
$errors = $entity
->validate();
$this
->assertEqual(0, count($errors));
$title = $this
->randomString();
$node = Node::create([
'title' => $title,
'type' => 'node',
'status' => NODE_NOT_PUBLISHED,
]);
$entity = EntityTest::create([
'field_test_node' => [
'entity' => $node,
],
]);
$errors = $entity
->validate();
$this
->assertEqual(1, count($errors));
$this
->assertEqual($errors[0]
->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
'%type' => 'node',
'%label' => $title,
]));
$this
->assertEqual($errors[0]
->getPropertyPath(), 'field_test_node.0.entity');
$node
->setPublished(TRUE);
$errors = $entity
->validate();
$this
->assertEqual(0, count($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
->assertEqual(1, count($errors));
$this
->assertEqual($errors[0]
->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
'%type' => 'comment',
'%label' => $title,
]));
$this
->assertEqual($errors[0]
->getPropertyPath(), 'field_test_comment.0.entity');
$comment
->setPublished(TRUE);
$errors = $entity
->validate();
$this
->assertEqual(0, count($errors));
$name = $this
->randomString();
$user = User::create([
'name' => $name,
'status' => 0,
]);
$entity = EntityTest::create([
'field_test_user' => [
'entity' => $user,
],
]);
$errors = $entity
->validate();
$this
->assertEqual(1, count($errors));
$this
->assertEqual($errors[0]
->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
'%type' => 'user',
'%label' => $name,
]));
$this
->assertEqual($errors[0]
->getPropertyPath(), 'field_test_user.0.entity');
$user
->activate();
$errors = $entity
->validate();
$this
->assertEqual(0, count($errors));
$filename = $this
->randomMachineName() . '.txt';
$file = File::create([
'filename' => $filename,
'status' => 0,
]);
$entity = EntityTest::create([
'field_test_file' => [
'entity' => $file,
],
]);
$errors = $entity
->validate();
$this
->assertEqual(1, count($errors));
$this
->assertEqual($errors[0]
->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
'%type' => 'file',
'%label' => $filename,
]));
$this
->assertEqual($errors[0]
->getPropertyPath(), 'field_test_file.0.entity');
$file
->setPermanent();
$errors = $entity
->validate();
$this
->assertEqual(0, count($errors));
}
}