function EntityQueryRelationshipTest::testQuery

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php \Drupal\KernelTests\Core\Entity\EntityQueryRelationshipTest::testQuery()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php \Drupal\KernelTests\Core\Entity\EntityQueryRelationshipTest::testQuery()
  3. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php \Drupal\KernelTests\Core\Entity\EntityQueryRelationshipTest::testQuery()

Tests querying.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php, line 116

Class

EntityQueryRelationshipTest
Tests the Entity Query relationship API.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testQuery() : void {
    $storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('entity_test');
    // This returns the 0th entity as that's the only one pointing to the 0th
    // account.
    $this->queryResults = $storage->getQuery()
        ->accessCheck(FALSE)
        ->condition("user_id.entity.name", $this->accounts[0]
        ->getAccountName())
        ->execute();
    $this->assertResults([
        0,
    ]);
    // This returns the 1st and 2nd entity as those point to the 1st account.
    $this->queryResults = $storage->getQuery()
        ->accessCheck(FALSE)
        ->condition("user_id.entity.name", $this->accounts[0]
        ->getAccountName(), '<>')
        ->execute();
    $this->assertResults([
        1,
        2,
    ]);
    // This returns all three entities because all of them point to an
    // account.
    $this->queryResults = $storage->getQuery()
        ->accessCheck(FALSE)
        ->exists("user_id.entity.name")
        ->execute();
    $this->assertResults([
        0,
        1,
        2,
    ]);
    // This returns no entities because all of them point to an account.
    $this->queryResults = $storage->getQuery()
        ->accessCheck(FALSE)
        ->notExists("user_id.entity.name")
        ->execute();
    $this->assertCount(0, $this->queryResults);
    // This returns the 0th entity as that's only one pointing to the 0th
    // term (test without specifying the field column).
    $this->queryResults = $storage->getQuery()
        ->accessCheck(FALSE)
        ->condition("{$this->fieldName}.entity.name", $this->terms[0]->name->value)
        ->execute();
    $this->assertResults([
        0,
    ]);
    // This returns the 0th entity as that's only one pointing to the 0th
    // term (test with specifying the column name).
    $this->queryResults = $storage->getQuery()
        ->accessCheck(FALSE)
        ->condition("{$this->fieldName}.target_id.entity.name", $this->terms[0]->name->value)
        ->execute();
    $this->assertResults([
        0,
    ]);
    // This returns the 1st and 2nd entity as those point to the 1st term.
    $this->queryResults = $storage->getQuery()
        ->accessCheck(FALSE)
        ->condition("{$this->fieldName}.entity.name", $this->terms[0]->name->value, '<>')
        ->execute();
    $this->assertResults([
        1,
        2,
    ]);
    // This returns the 0th entity as that's only one pointing to the 0th
    // account.
    $this->queryResults = $storage->getQuery()
        ->accessCheck(FALSE)
        ->condition("user_id.entity:user.name", $this->accounts[0]
        ->getAccountName())
        ->execute();
    $this->assertResults([
        0,
    ]);
    // This returns the 1st and 2nd entity as those point to the 1st account.
    $this->queryResults = $storage->getQuery()
        ->accessCheck(FALSE)
        ->condition("user_id.entity:user.name", $this->accounts[0]
        ->getAccountName(), '<>')
        ->execute();
    $this->assertResults([
        1,
        2,
    ]);
    // This returns all three entities because all of them point to an
    // account.
    $this->queryResults = $storage->getQuery()
        ->accessCheck(FALSE)
        ->exists("user_id.entity:user.name")
        ->execute();
    $this->assertResults([
        0,
        1,
        2,
    ]);
    // This returns no entities because all of them point to an account.
    $this->queryResults = $storage->getQuery()
        ->accessCheck(FALSE)
        ->notExists("user_id.entity:user.name")
        ->execute();
    $this->assertCount(0, $this->queryResults);
    // This returns the 0th entity as that's only one pointing to the 0th
    // term (test without specifying the field column).
    $this->queryResults = $storage->getQuery()
        ->accessCheck(FALSE)
        ->condition("{$this->fieldName}.entity:taxonomy_term.name", $this->terms[0]->name->value)
        ->execute();
    $this->assertResults([
        0,
    ]);
    // This returns the 0th entity as that's only one pointing to the 0th
    // term (test with specifying the column name).
    $this->queryResults = $storage->getQuery()
        ->accessCheck(FALSE)
        ->condition("{$this->fieldName}.target_id.entity:taxonomy_term.name", $this->terms[0]->name->value)
        ->execute();
    $this->assertResults([
        0,
    ]);
    // This returns the 1st and 2nd entity as those point to the 1st term.
    $this->queryResults = $storage->getQuery()
        ->accessCheck(FALSE)
        ->condition("{$this->fieldName}.entity:taxonomy_term.name", $this->terms[0]->name->value, '<>')
        ->execute();
    $this->assertResults([
        1,
        2,
    ]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.