You are here

public function EntityQueryRelationshipTest::testEntityQuery in Dynamic Entity Reference 8.2

Same name and namespace in other branches
  1. 8 tests/src/Kernel/EntityQueryRelationshipTest.php \Drupal\Tests\dynamic_entity_reference\Kernel\EntityQueryRelationshipTest::testEntityQuery()

Tests entity query for DER for entities with integer IDs.

File

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

Class

EntityQueryRelationshipTest
Tests dynamic entity reference relationship data.

Namespace

Drupal\Tests\dynamic_entity_reference\Kernel

Code

public function testEntityQuery() {
  $this
    ->installEntitySchema('entity_test_mul');
  $this
    ->installEntitySchema('entity_test_rev');
  $this->referencedEntityTypes = [
    'entity_test_mul',
    'entity_test_rev',
  ];
  $this
    ->setupDerField();

  // Create some test entities which link each other.
  $referenced_entity_test_mul = EntityTestMul::create([
    'name' => 'Foobar',
  ]);
  $referenced_entity_test_mul
    ->save();
  $referenced_entity_test_mul_2 = EntityTestMul::create([
    'name' => 'Barfoo',
  ]);
  $referenced_entity_test_mul_2
    ->save();
  $referenced_entity_test_rev = EntityTestRev::create([
    'name' => 'Foobar',
  ]);
  $referenced_entity_test_rev
    ->save();
  $referenced_entity_test_rev_2 = EntityTestRev::create([
    'name' => 'Barfoo',
  ]);
  $referenced_entity_test_rev_2
    ->save();
  $entity = EntityTest::create();
  $entity->field_test[] = $referenced_entity_test_mul;
  $entity->field_test[] = $referenced_entity_test_mul_2;
  $entity
    ->save();
  $this
    ->assertEquals($entity->field_test[0]->entity
    ->id(), $referenced_entity_test_mul
    ->id());
  $this
    ->assertEquals($entity->field_test[1]->entity
    ->id(), $referenced_entity_test_mul_2
    ->id());
  $this
    ->assertEquals($entity->field_test[0]->entity
    ->getEntityTypeId(), $referenced_entity_test_mul
    ->getEntityTypeId());
  $this
    ->assertEquals($entity->field_test[1]->entity
    ->getEntityTypeId(), $referenced_entity_test_mul_2
    ->getEntityTypeId());
  $this->entities[] = $entity;
  $entity = EntityTest::create();
  $entity->field_test[] = $referenced_entity_test_rev;
  $entity->field_test[] = $referenced_entity_test_rev_2;
  $entity
    ->save();
  $this
    ->assertEquals($entity->field_test[0]->entity
    ->id(), $referenced_entity_test_rev
    ->id());
  $this
    ->assertEquals($entity->field_test[1]->entity
    ->id(), $referenced_entity_test_rev_2
    ->id());
  $this
    ->assertEquals($entity->field_test[0]->entity
    ->getEntityTypeId(), $referenced_entity_test_rev
    ->getEntityTypeId());
  $this
    ->assertEquals($entity->field_test[1]->entity
    ->getEntityTypeId(), $referenced_entity_test_rev_2
    ->getEntityTypeId());
  $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_mul.name", 'Foobar')
    ->condition("field_test.1.entity:entity_test_mul.name", 'Barfoo');
  $this->queryResults = $query
    ->execute();
  $this
    ->assertEquals([
    1 => 1,
  ], $this->queryResults);
  $this
    ->assertJoinColumn($query, 'field_test', 'entity_test_mul', TRUE);

  // 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_rev.name", 'Foobar')
    ->condition("field_test.1.entity:entity_test_rev.name", 'Barfoo');
  $this->queryResults = $query
    ->execute();
  $this
    ->assertEquals([
    2 => 2,
  ], $this->queryResults);
  $this
    ->assertJoinColumn($query, 'field_test', 'entity_test_rev', TRUE);
}