EntityQueryRelationshipTest.php in Dynamic Entity Reference 8
File
tests/src/Kernel/EntityQueryRelationshipTest.php
View source
<?php
namespace Drupal\Tests\dynamic_entity_reference\Kernel;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\entity_test\Entity\EntityTestRev;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
class EntityQueryRelationshipTest extends EntityKernelTestBase {
protected static $modules = [
'dynamic_entity_reference',
];
protected $entityType = 'entity_test';
protected $referencedEntityType = 'entity_test_rev';
protected $bundle = 'entity_test';
protected $fieldName = 'field_test';
protected $queryResults;
protected $entities = [];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('entity_test_rev');
FieldStorageConfig::create([
'field_name' => $this->fieldName,
'type' => 'dynamic_entity_reference',
'entity_type' => $this->entityType,
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'settings' => [
'exclude_entity_types' => FALSE,
'entity_type_ids' => [
$this->referencedEntityType,
],
],
])
->save();
FieldConfig::create([
'field_name' => $this->fieldName,
'entity_type' => $this->entityType,
'bundle' => $this->bundle,
'label' => 'Field test',
'settings' => [],
])
->save();
}
public function testEntityQuery() {
$referenced_entity_1 = EntityTestRev::create([
'name' => 'Foobar',
]);
$referenced_entity_1
->save();
$referenced_entity_2 = EntityTestRev::create([
'name' => 'Barfoo',
]);
$referenced_entity_2
->save();
$entity = EntityTest::create();
$entity->field_test[] = $referenced_entity_1;
$entity->field_test[] = $referenced_entity_2;
$entity
->save();
$this
->assertEquals($entity->field_test[0]->entity
->id(), $referenced_entity_1
->id());
$this
->assertEquals($entity->field_test[1]->entity
->id(), $referenced_entity_2
->id());
$this->entities[] = $entity;
$entity = EntityTest::create();
$entity->field_test[] = $referenced_entity_1;
$entity->field_test[] = $referenced_entity_2;
$entity
->save();
$this
->assertEquals($entity->field_test[0]->entity
->id(), $referenced_entity_1
->id());
$this
->assertEquals($entity->field_test[1]->entity
->id(), $referenced_entity_2
->id());
$this->entities[] = $entity;
$this->queryResults = $this->container
->get('entity_type.manager')
->getStorage('entity_test')
->getQuery()
->accessCheck(FALSE)
->condition("field_test.0.entity:entity_test_rev.name", 'Foobar')
->condition("field_test.1.entity:entity_test_rev.name", 'Barfoo')
->execute();
$this
->assertEquals([
1 => 1,
2 => 2,
], $this->queryResults);
}
}