You are here

public function EntityQueryRelationshipTest::testQuery in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityQueryRelationshipTest.php \Drupal\system\Tests\Entity\EntityQueryRelationshipTest::testQuery()

Tests querying.

File

core/modules/system/src/Tests/Entity/EntityQueryRelationshipTest.php, line 119
Contains \Drupal\system\Tests\Entity\EntityQueryRelationshipTest.

Class

EntityQueryRelationshipTest
Tests the Entity Query relationship API.

Namespace

Drupal\system\Tests\Entity

Code

public function testQuery() {

  // This returns the 0th entity as that's only one pointing to the 0th
  // account.
  $this->queryResults = $this->factory
    ->get('entity_test')
    ->condition("user_id.entity.name", $this->accounts[0]
    ->getUsername())
    ->execute();
  $this
    ->assertResults(array(
    0,
  ));

  // This returns the 1st and 2nd entity as those point to the 1st account.
  $this->queryResults = $this->factory
    ->get('entity_test')
    ->condition("user_id.entity.name", $this->accounts[0]
    ->getUsername(), '<>')
    ->execute();
  $this
    ->assertResults(array(
    1,
    2,
  ));

  // This returns all three entities because all of them point to an
  // account.
  $this->queryResults = $this->factory
    ->get('entity_test')
    ->exists("user_id.entity.name")
    ->execute();
  $this
    ->assertResults(array(
    0,
    1,
    2,
  ));

  // This returns no entities because all of them point to an account.
  $this->queryResults = $this->factory
    ->get('entity_test')
    ->notExists("user_id.entity.name")
    ->execute();
  $this
    ->assertEqual(count($this->queryResults), 0);

  // This returns the 0th entity as that's only one pointing to the 0th
  // term (test without specifying the field column).
  $this->queryResults = $this->factory
    ->get('entity_test')
    ->condition("{$this->fieldName}.entity.name", $this->terms[0]->name->value)
    ->execute();
  $this
    ->assertResults(array(
    0,
  ));

  // This returns the 0th entity as that's only one pointing to the 0th
  // term (test with specifying the column name).
  $this->queryResults = $this->factory
    ->get('entity_test')
    ->condition("{$this->fieldName}.target_id.entity.name", $this->terms[0]->name->value)
    ->execute();
  $this
    ->assertResults(array(
    0,
  ));

  // This returns the 1st and 2nd entity as those point to the 1st term.
  $this->queryResults = $this->factory
    ->get('entity_test')
    ->condition("{$this->fieldName}.entity.name", $this->terms[0]->name->value, '<>')
    ->execute();
  $this
    ->assertResults(array(
    1,
    2,
  ));
}