You are here

public function EntityQueryRelationshipTest::testEntityQuery in Dynamic Entity Reference 8

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

Tests entity query for DER.

File

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

Class

EntityQueryRelationshipTest
Tests dynamic entity reference relationship data.

Namespace

Drupal\Tests\dynamic_entity_reference\Kernel

Code

public function testEntityQuery() {

  // Create some test entities which link each other.
  $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 returns the 0th entity as that's only one pointing to the 0th
  // account.
  $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);
}