function EntityQueryTest::testEntityQuery

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

Tests basic functionality.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php, line 178

Class

EntityQueryTest
Tests Entity Query functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testEntityQuery() : void {
    $greetings = $this->greetings;
    $figures = $this->figures;
    $this->queryResults = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->exists($greetings, 'tr')
        ->condition("{$figures}.color", 'red')
        ->sort('id')
        ->execute();
    // As unit 0 was the red triangle and unit 2 was the turkish greeting,
    // bit 0 and bit 2 needs to be set.
    $this->assertResult(5, 7, 13, 15);
    $query = $this->storage
        ->getQuery('OR')
        ->accessCheck(FALSE)
        ->exists($greetings, 'tr')
        ->condition("{$figures}.color", 'red')
        ->sort('id');
    $count_query = clone $query;
    $this->assertSame(12, $count_query->count()
        ->execute());
    $this->queryResults = $query->execute();
    // Now bit 0 (1, 3, 5, 7, 9, 11, 13, 15) or bit 2 (4, 5, 6, 7, 12, 13, 14,
    // 15) needs to be set.
    $this->assertResult(1, 3, 4, 5, 6, 7, 9, 11, 12, 13, 14, 15);
    // Test cloning of query conditions.
    $query = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition("{$figures}.color", 'red')
        ->sort('id');
    $cloned_query = clone $query;
    $cloned_query->condition("{$figures}.shape", 'circle');
    // Bit 0 (1, 3, 5, 7, 9, 11, 13, 15) needs to be set.
    $this->queryResults = $query->execute();
    $this->assertResult(1, 3, 5, 7, 9, 11, 13, 15);
    // No red color has a circle shape.
    $this->queryResults = $cloned_query->execute();
    $this->assertResult();
    $query = $this->storage
        ->getQuery()
        ->accessCheck(FALSE);
    $group = $query->orConditionGroup()
        ->exists($greetings, 'tr')
        ->condition("{$figures}.color", 'red');
    $this->queryResults = $query->condition($group)
        ->condition("{$greetings}.value", 'sie', 'STARTS_WITH')
        ->sort('revision_id')
        ->execute();
    // Bit 3 and (bit 0 or 2) -- the above 8 part of the above.
    $this->assertResult(9, 11, 12, 13, 14, 15);
    // No figure has both the colors blue and red at the same time.
    $this->queryResults = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition("{$figures}.color", 'blue')
        ->condition("{$figures}.color", 'red')
        ->sort('id')
        ->execute();
    $this->assertResult();
    // But an entity might have a red and a blue figure both.
    $query = $this->storage
        ->getQuery()
        ->accessCheck(FALSE);
    $group_blue = $query->andConditionGroup()
        ->condition("{$figures}.color", 'blue');
    $group_red = $query->andConditionGroup()
        ->condition("{$figures}.color", 'red');
    $this->queryResults = $query->condition($group_blue)
        ->condition($group_red)
        ->sort('revision_id')
        ->execute();
    // Unit 0 and unit 1, so bits 0 1.
    $this->assertResult(3, 7, 11, 15);
    // Do the same test but with IN operator.
    $query = $this->storage
        ->getQuery()
        ->accessCheck(FALSE);
    $group_blue = $query->andConditionGroup()
        ->condition("{$figures}.color", [
        'blue',
    ], 'IN');
    $group_red = $query->andConditionGroup()
        ->condition("{$figures}.color", [
        'red',
    ], 'IN');
    $this->queryResults = $query->condition($group_blue)
        ->condition($group_red)
        ->sort('id')
        ->execute();
    // Unit 0 and unit 1, so bits 0 1.
    $this->assertResult(3, 7, 11, 15);
    // An entity might have either red or blue figure.
    $this->queryResults = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition("{$figures}.color", [
        'blue',
        'red',
    ], 'IN')
        ->sort('id')
        ->execute();
    // Bit 0 or 1 is on.
    $this->assertResult(1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15);
    $this->queryResults = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->exists("{$figures}.color")
        ->notExists("{$greetings}.value")
        ->sort('id')
        ->execute();
    // Bit 0 or 1 is on but 2 and 3 are not.
    $this->assertResult(1, 2, 3);
    // Now update the 'merhaba' string to xsiemax which is not a meaningful
    // word but allows us to test revisions and string operations.
    $ids = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition("{$greetings}.value", 'merhaba')
        ->sort('id')
        ->execute();
    $entities = EntityTestMulRev::loadMultiple($ids);
    $first_entity = reset($entities);
    $old_name = $first_entity->name->value;
    foreach ($entities as $entity) {
        $entity->setNewRevision();
        $entity->getTranslation('tr')->{$greetings}->value = 'xsiemax';
        $entity->name->value .= 'x';
        $entity->save();
    }
    // Test querying all revisions with a condition on the revision ID field.
    $this->queryResults = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('revision_id', $first_entity->getRevisionId())
        ->allRevisions()
        ->execute();
    $this->assertCount(1, $this->queryResults);
    $this->assertEquals($first_entity->getRevisionId(), key($this->queryResults));
    // We changed the entity names, so the current revision should not match.
    $this->queryResults = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('name.value', $old_name)
        ->execute();
    $this->assertResult();
    // Only if all revisions are queried, we find the old revision.
    $this->queryResults = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('name.value', $old_name)
        ->allRevisions()
        ->sort('revision_id')
        ->execute();
    $this->assertRevisionResult([
        $first_entity->id(),
    ], [
        $first_entity->id(),
    ]);
    // When querying current revisions, this string is no longer found.
    $this->queryResults = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition("{$greetings}.value", 'merhaba')
        ->execute();
    $this->assertResult();
    $this->queryResults = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition("{$greetings}.value", 'merhaba')
        ->allRevisions()
        ->sort('revision_id')
        ->execute();
    // The query only matches the original revisions.
    $this->assertRevisionResult([
        4,
        5,
        6,
        7,
        12,
        13,
        14,
        15,
    ], [
        4,
        5,
        6,
        7,
        12,
        13,
        14,
        15,
    ]);
    $results = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition("{$greetings}.value", 'siema', 'CONTAINS')
        ->sort('id')
        ->execute();
    // This matches both the original and new current revisions, multiple
    // revisions are returned for some entities.
    $assert = [
        16 => '4',
        17 => '5',
        18 => '6',
        19 => '7',
        8 => '8',
        9 => '9',
        10 => '10',
        11 => '11',
        20 => '12',
        21 => '13',
        22 => '14',
        23 => '15',
    ];
    $this->assertSame($assert, $results);
    $results = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition("{$greetings}.value", 'siema', 'STARTS_WITH')
        ->sort('revision_id')
        ->execute();
    // Now we only get the ones that originally were siema, entity id 8 and
    // above.
    $this->assertSame(array_slice($assert, 4, 8, TRUE), $results);
    $results = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition("{$greetings}.value", 'a', 'ENDS_WITH')
        ->sort('revision_id')
        ->execute();
    // It is very important that we do not get the ones which only have
    // xsiemax despite originally they were merhaba, ie. ended with a.
    $this->assertSame(array_slice($assert, 4, 8, TRUE), $results);
    $results = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition("{$greetings}.value", 'a', 'ENDS_WITH')
        ->allRevisions()
        ->sort('id')
        ->sort('revision_id')
        ->execute();
    // Now we get everything.
    $assert = [
        4 => '4',
        5 => '5',
        6 => '6',
        7 => '7',
        8 => '8',
        9 => '9',
        10 => '10',
        11 => '11',
        12 => '12',
        20 => '12',
        13 => '13',
        21 => '13',
        14 => '14',
        22 => '14',
        15 => '15',
        23 => '15',
    ];
    $this->assertSame($assert, $results);
    // Check that a query on the latest revisions without any condition returns
    // the correct results.
    $results = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->latestRevision()
        ->sort('id')
        ->sort('revision_id')
        ->execute();
    $expected = [
        1 => '1',
        2 => '2',
        3 => '3',
        16 => '4',
        17 => '5',
        18 => '6',
        19 => '7',
        8 => '8',
        9 => '9',
        10 => '10',
        11 => '11',
        20 => '12',
        21 => '13',
        22 => '14',
        23 => '15',
    ];
    $this->assertSame($expected, $results);
}

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