You are here

public function EntityQueryRelationshipTest::testEntityQueryString in Dynamic Entity Reference 8.2

Tests entity query for DER for entities with string IDs.

File

tests/src/Kernel/EntityQueryRelationshipTest.php, line 136

Class

EntityQueryRelationshipTest
Tests dynamic entity reference relationship data.

Namespace

Drupal\Tests\dynamic_entity_reference\Kernel

Code

public function testEntityQueryString() {
  $this
    ->installEntitySchema('entity_test_string_id');
  $this->referencedEntityTypes = [
    'entity_test_string_id',
  ];
  $this
    ->setupDerField();

  // Create some test entities which link each other.
  $referenced_entity_1 = EntityTestStringId::create([
    'name' => 'Foobar',
    'id' => mb_strtolower($this
      ->randomMachineName()),
  ]);
  $referenced_entity_1
    ->save();
  $referenced_entity_2 = EntityTestStringId::create([
    'name' => 'Barfoo',
    'id' => mb_strtolower($this
      ->randomMachineName()),
  ]);
  $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 returns the 0th entity as that's only one pointing to the 0th
  // account.
  $query = $this->container
    ->get('entity_type.manager')
    ->getStorage('entity_test')
    ->getQuery()
    ->accessCheck(FALSE)
    ->condition("field_test.0.entity:entity_test_string_id.name", 'Foobar')
    ->condition("field_test.1.entity:entity_test_string_id.name", 'Barfoo');
  $this->queryResults = $query
    ->execute();
  $this
    ->assertEquals([
    1 => 1,
    2 => 2,
  ], $this->queryResults);
  $this
    ->assertJoinColumn($query, 'field_test', 'entity_test_string_id', FALSE);
}