public function DynamicEntityReferenceFieldTest::testNormalEntityReference in Dynamic Entity Reference 8.2
Tests with normal entity reference fields.
File
- tests/
src/ Kernel/ DynamicEntityReferenceFieldTest.php, line 491
Class
- DynamicEntityReferenceFieldTest
- Tests for the dynamic entity reference field.
Namespace
Drupal\Tests\dynamic_entity_reference\KernelCode
public function testNormalEntityReference() {
// Create a field.
FieldStorageConfig::create([
'field_name' => 'field_normal_er',
'type' => 'entity_reference',
'entity_type' => $this->entityType,
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'settings' => [
'exclude_entity_types' => FALSE,
'entity_type_ids' => [
'user',
],
],
])
->save();
FieldConfig::create([
'field_name' => 'field_normal_er',
'entity_type' => $this->entityType,
'bundle' => $this->bundle,
'label' => 'Field test',
'settings' => [
$this->referencedEntityType => [
'handler' => 'default:user',
'handler_settings' => [
'target_bundles' => NULL,
],
],
],
])
->save();
// Add some users and test entities.
$accounts = $entities = [];
foreach (range(1, 3) as $i) {
$accounts[$i] = $this
->createUser();
$entity = EntityTest::create();
// Add reference to user 2 for entities 2 and 3.
if ($i > 1) {
$entity->field_normal_er = $accounts[2];
}
$entity
->save();
$entities[$i] = $entity;
}
$result = \Drupal::entityTypeManager()
->getStorage('entity_test')
->getQuery()
->accessCheck(FALSE)
->condition('field_normal_er.entity:user.status', 1)
->sort('id')
->execute();
$expected = [
$entities[2]
->id() => $entities[2]
->id(),
$entities[3]
->id() => $entities[3]
->id(),
];
$this
->assertSame($expected, $result);
}