function EntityQueryTest::testPendingRevisions

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

Test pending revisions.

File

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

Class

EntityQueryTest
Tests Entity Query functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testPendingRevisions() {
    // Ensure entity 14 is returned.
    $result = $this->storage
        ->getQuery()
        ->condition('id', [
        14,
    ], 'IN')
        ->execute();
    $this->assertCount(1, $result);
    // Set a revision on entity 14 that isn't the current default.
    $entity = EntityTestMulRev::load(14);
    $current_values = $entity->{$this->figures}
        ->getValue();
    $entity->setNewRevision(TRUE);
    $entity->isDefaultRevision(FALSE);
    $entity->{$this->figures}
        ->setValue([
        'color' => 'red',
        'shape' => 'square',
    ]);
    $entity->save();
    // Entity query should still return entity 14.
    $result = $this->storage
        ->getQuery()
        ->condition('id', [
        14,
    ], 'IN')
        ->execute();
    $this->assertCount(1, $result);
    // Verify that field conditions on the default and pending revision are
    // work as expected.
    $result = $this->storage
        ->getQuery()
        ->condition('id', [
        14,
    ], 'IN')
        ->condition("{$this->figures}.color", $current_values[0]['color'])
        ->execute();
    $this->assertEqual($result, [
        14 => '14',
    ]);
    $result = $this->storage
        ->getQuery()
        ->condition('id', [
        14,
    ], 'IN')
        ->condition("{$this->figures}.color", 'red')
        ->allRevisions()
        ->execute();
    $this->assertEqual($result, [
        16 => '14',
    ]);
    // Add another pending revision on the same entity and repeat the checks.
    $entity->setNewRevision(TRUE);
    $entity->isDefaultRevision(FALSE);
    $entity->{$this->figures}
        ->setValue([
        'color' => 'red',
        'shape' => 'square',
    ]);
    $entity->save();
    // A non-revisioned entity query should still return entity 14.
    $result = $this->storage
        ->getQuery()
        ->condition('id', [
        14,
    ], 'IN')
        ->execute();
    $this->assertCount(1, $result);
    $this->assertSame([
        14 => '14',
    ], $result);
    // Now check an entity query on the latest revision.
    $result = $this->storage
        ->getQuery()
        ->condition('id', [
        14,
    ], 'IN')
        ->latestRevision()
        ->execute();
    $this->assertCount(1, $result);
    $this->assertSame([
        17 => '14',
    ], $result);
    // Verify that field conditions on the default and pending revision still
    // work as expected.
    $result = $this->storage
        ->getQuery()
        ->condition('id', [
        14,
    ], 'IN')
        ->condition("{$this->figures}.color", $current_values[0]['color'])
        ->execute();
    $this->assertSame([
        14 => '14',
    ], $result);
    // Now there are two revisions with same value for the figure color.
    $result = $this->storage
        ->getQuery()
        ->condition('id', [
        14,
    ], 'IN')
        ->condition("{$this->figures}.color", 'red')
        ->allRevisions()
        ->execute();
    $this->assertSame([
        16 => '14',
        17 => '14',
    ], $result);
    // Check that querying for the latest revision returns the correct one.
    $result = $this->storage
        ->getQuery()
        ->condition('id', [
        14,
    ], 'IN')
        ->condition("{$this->figures}.color", 'red')
        ->latestRevision()
        ->execute();
    $this->assertSame([
        17 => '14',
    ], $result);
}

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