View source
<?php
namespace Drupal\Tests\dynamic_entity_reference\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\user\Entity\User;
class DynamicEntityReferenceItemTest extends FieldKernelTestBase {
protected static $modules = [
'dynamic_entity_reference',
'taxonomy',
];
protected $vocabulary;
protected $term;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('taxonomy_term');
$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();
FieldStorageConfig::create([
'field_name' => 'field_der',
'type' => 'dynamic_entity_reference',
'entity_type' => 'entity_test',
'cardinality' => 1,
'settings' => [
'exclude_entity_types' => FALSE,
'entity_type_ids' => [
'taxonomy_term',
],
],
])
->save();
FieldConfig::create([
'field_name' => 'field_der',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'label' => 'Foo Bar',
'settings' => [
'taxonomy_term' => [
'handler' => 'default:taxonomy_term',
'handler_settings' => [
'target_bundles' => NULL,
],
],
],
])
->save();
}
public function testContentEntityReferenceItem() {
$tid = $this->term
->id();
$entity_type_id = $this->term
->getEntityTypeId();
$entity = EntityTest::create();
$entity->field_der->target_type = $entity_type_id;
$entity->field_der->target_id = $tid;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$entity = EntityTest::load($entity
->id());
$this
->assertTrue($entity->field_der instanceof FieldItemListInterface, 'Field implements interface.');
$this
->assertTrue($entity->field_der[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this
->assertEquals($entity->field_der->target_id, $tid);
$this
->assertEquals($entity->field_der->target_type, $entity_type_id);
$this
->assertEquals($entity->field_der->entity
->getName(), $this->term
->getName());
$this
->assertEquals($entity->field_der->entity
->id(), $tid);
$this
->assertEquals($entity->field_der->entity
->uuid(), $this->term
->uuid());
$new_name = $this
->randomMachineName();
$entity->field_der->entity
->setName($new_name);
$entity->field_der->entity
->save();
$term = Term::load($tid);
$this
->assertEquals($term
->getName(), $new_name);
$term2 = Term::create([
'name' => $this
->randomMachineName(),
'vid' => $this->term
->bundle(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$term2
->save();
$entity->field_der->target_type = $entity_type_id;
$entity->field_der->target_id = $term
->id();
$this
->assertEquals($entity->field_der->entity
->id(), $term
->id());
$this
->assertEquals($entity->field_der->entity
->getName(), $term
->getName());
$entity->field_der = [
'target_id' => $term2
->id(),
'target_type' => $entity_type_id,
];
$this
->assertEquals($entity->field_der->entity
->id(), $term2
->id());
$this
->assertEquals($entity->field_der->entity
->getName(), $term2
->getName());
$entity->field_der->entity = $term;
$this
->assertEquals($entity->field_der->target_id, $term
->id());
$this
->assertEquals($entity->field_der->entity
->getName(), $term
->getName());
$entity->field_der
->appendItem($term2);
$this
->assertEquals($entity->field_der[1]->target_id, $term2
->id());
$this
->assertEquals($entity->field_der[1]->entity
->getName(), $term2
->getName());
$entity->field_der = [
'entity' => $term2,
];
$this
->assertEquals($entity->field_der->target_id, $term2
->id());
$this
->assertEquals($entity->field_der->entity
->getName(), $term2
->getName());
$entity->field_der
->appendItem([
'entity' => $term,
]);
$this
->assertEquals($entity->field_der[1]->target_id, $term
->id());
$this
->assertEquals($entity->field_der[1]->entity
->getName(), $term
->getName());
try {
$entity->field_der = [
'target_id' => $term
->id(),
'target_type' => '',
];
$this
->assertTrue(FALSE, 'Assigning an item without target type throws an exception.');
} catch (\InvalidArgumentException $e) {
$this
->assertTrue(TRUE, 'Assigning an item without target type throws an exception.');
}
try {
$entity->field_der = [
'target_id' => 'invalid',
'target_type' => $entity_type_id,
'entity' => $term2,
];
$this
->assertTrue(FALSE, 'Assigning an invalid item throws an exception.');
} catch (\InvalidArgumentException $e) {
$this
->assertTrue(TRUE, 'Assigning an invalid item throws an exception.');
}
try {
$entity->field_der = [
'target_id' => $term2
->id(),
'target_type' => 'invalid',
'entity' => $term2,
];
$this
->fail('Assigning an invalid item throws an exception.');
} catch (\InvalidArgumentException $e) {
$this
->assertTrue(TRUE, 'Assigning an invalid item throws an exception.');
}
$entity->field_der->target_type = $entity_type_id;
$entity->field_der->target_id = $term2
->id();
$this
->assertEquals($entity->field_der->entity
->id(), $term2
->id());
$this
->assertEquals($entity->field_der->entity
->getName(), $term2
->getName());
$term
->delete();
$term2
->delete();
$entity = EntityTest::create([
'name' => $this
->randomMachineName(),
]);
$entity
->save();
$entity = EntityTest::create();
Term::create([
'name' => $this
->randomMachineName(),
'vid' => $this->term
->bundle(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
])
->save();
$entity->field_der
->generateSampleItems();
$entity->field_der
->generateSampleItems();
$this
->entityValidateAndSave($entity);
}
public function testEntitySaveOrder() {
$term = Term::create([
'name' => $this
->randomMachineName(),
'vid' => $this->term
->bundle(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$entity = EntityTest::create();
$entity->field_der->entity = $term;
$entity->name->value = $this
->randomMachineName();
$value = $entity
->get('field_der');
$this
->assertTrue(empty($value['target_id']));
$this
->assertTrue(!isset($entity->field_der->target_id));
$entity->field_der = $value;
$term
->save();
$entity
->save();
$this
->assertEquals($entity->field_der->entity
->id(), $term
->id());
}
public function testEntityAutoCreate() {
$term = Term::create([
'name' => $this
->randomMachineName(),
'vid' => $this->term
->bundle(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$entity = EntityTest::create();
$entity->field_der->entity = $term;
$entity->name->value = $this
->randomMachineName();
$entity = serialize($entity);
$entity = unserialize($entity);
$entity
->save();
$term = $this->container
->get('entity.repository')
->loadEntityByUuid($term
->getEntityTypeId(), $term
->uuid());
$this
->assertEquals($entity->field_der->entity
->id(), $term
->id());
}
public function testEntityReferenceWithProperty() {
$term = Term::create([
'name' => $this
->randomMachineName(),
'vid' => $this->term
->bundle(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$term
->save();
$entity = EntityTest::create();
$entity->field_der->target_id = $term
->id();
$entity->field_der->target_type = $term
->getEntityTypeId();
$entity->name->value = $this
->randomMachineName();
$entity = serialize($entity);
$entity = unserialize($entity);
$entity
->save();
$term = $this->container
->get('entity.repository')
->loadEntityByUuid($term
->getEntityTypeId(), $term
->uuid());
$this
->assertEquals($entity->field_der->entity
->id(), $term
->id());
}
public function testMultipleEntityReferenceItem() {
$field_storage = FieldStorageConfig::loadByName('entity_test', 'field_der');
$field_storage
->set('settings', [
'exclude_entity_types' => FALSE,
'entity_type_ids' => [
'taxonomy_term',
'user',
],
])
->set('cardinality', FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->save();
$entity = EntityTest::create();
$account = User::load(1);
$entity->field_der[] = [
'entity' => $this->term,
];
$entity->field_der[] = [
'entity' => $account,
];
$entity
->save();
$this
->assertEquals($entity->field_der[0]->target_id, $this->term
->id());
$this
->assertEquals($entity->field_der[0]->target_type, $this->term
->getEntityTypeId());
$this
->assertEquals($entity->field_der[0]->entity
->getName(), $this->term
->getName());
$this
->assertEquals($entity->field_der[0]->entity
->id(), $this->term
->id());
$this
->assertEquals($entity->field_der[0]->entity
->uuid(), $this->term
->uuid());
$this
->assertEquals($entity->field_der[1]->target_id, $account
->id());
$this
->assertEquals($entity->field_der[1]->target_type, $account
->getEntityTypeId());
$this
->assertEquals($entity->field_der[1]->entity
->id(), $account
->id());
$this
->assertEquals($entity->field_der[1]->entity
->uuid(), $account
->uuid());
}
public function testSelectionHandlerSettings() {
$field_name = mb_strtolower($this
->randomMachineName());
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'dynamic_entity_reference',
'settings' => [
'exclude_entity_types' => FALSE,
'entity_type_ids' => [
'entity_test',
],
],
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'entity_test',
]);
$field
->save();
$field = FieldConfig::load($field
->id());
$field_settings = $field
->getSettings();
$this
->assertTrue($field_settings['entity_test']['handler'] == 'default:entity_test');
$field_settings['entity_test']['handler'] = 'views';
$field
->setSettings($field_settings);
$field
->save();
$field = FieldConfig::load($field
->id());
$field_settings = $field
->getSettings();
$this
->assertTrue($field_settings['entity_test']['handler'] == 'views');
}
public function testValidation() {
$term = Term::create([
'name' => $this
->randomMachineName(),
'vid' => $this->term
->bundle(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$entity = EntityTest::create([
'field_der' => [
'entity' => $term,
'target_id' => NULL,
'target_type' => $term
->getEntityTypeId(),
],
]);
$errors = $entity
->validate();
$this
->assertCount(0, $errors);
$term
->save();
$entity = EntityTest::create([
'field_der' => [
'entity' => $term,
'target_id' => NULL,
'target_type' => $term
->getEntityTypeId(),
],
]);
$errors = $entity
->validate();
$this
->assertCount(1, $errors);
$this
->assertEquals($errors[0]
->getMessage(), (string) new FormattableMarkup('%property should not be null.', [
'%property' => 'target_id',
]));
$this
->assertEquals($errors[0]
->getPropertyPath(), 'field_der.0.target_id');
$entity
->save();
$errors = $entity
->validate();
$this
->assertCount(0, $errors);
}
}