function WorkspaceIntegrationTest::assertEntityQuery

Same name and namespace in other branches
  1. 9 core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceIntegrationTest::assertEntityQuery()
  2. 10 core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceIntegrationTest::assertEntityQuery()
  3. 11.x core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceIntegrationTest::assertEntityQuery()

Asserts that entity queries are giving the correct results in a workspace.

Parameters

array $expected_values: An array of expected values, as defined in ::testWorkspaces().

string $entity_type_id: The ID of the entity type to check.

1 call to WorkspaceIntegrationTest::assertEntityQuery()
WorkspaceIntegrationTest::assertWorkspaceStatus in core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php
Checks entity load, entity queries and views results for a test scenario.

File

core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php, line 903

Class

WorkspaceIntegrationTest
Tests a complete deployment scenario across different workspaces.

Namespace

Drupal\Tests\workspaces\Kernel

Code

protected function assertEntityQuery(array $expected_values, $entity_type_id) {
    $storage = $this->entityTypeManager
        ->getStorage($entity_type_id);
    $entity_keys = $this->entityTypeManager
        ->getDefinition($entity_type_id)
        ->getKeys();
    $id_key = $entity_keys['id'];
    $revision_key = $entity_keys['revision'];
    $label_key = $entity_keys['label'];
    $published_key = $entity_keys['published'];
    // Filter the expected values so we can check only the default revisions.
    $expected_default_revisions = array_filter($expected_values, function ($expected_value) {
        return $expected_value['default_revision'] === TRUE;
    });
    // Check entity query counts.
    $result = $storage->getQuery()
        ->count()
        ->execute();
    $this->assertEquals(count($expected_default_revisions), $result);
    $result = $storage->getAggregateQuery()
        ->count()
        ->execute();
    $this->assertEquals(count($expected_default_revisions), $result);
    // Check entity queries with no conditions.
    $result = $storage->getQuery()
        ->execute();
    $expected_result = array_combine(array_column($expected_default_revisions, $revision_key), array_column($expected_default_revisions, $id_key));
    $this->assertEquals($expected_result, $result);
    // Check querying each revision individually.
    foreach ($expected_values as $expected_value) {
        $query = $storage->getQuery();
        $query->condition($entity_keys['id'], $expected_value[$id_key])
            ->condition($entity_keys['label'], $expected_value[$label_key])
            ->condition($entity_keys['published'], (int) $expected_value[$published_key]);
        // If the entity is not expected to be the default revision, we need to
        // query all revisions if we want to find it.
        if (!$expected_value['default_revision']) {
            $query->allRevisions();
        }
        $result = $query->execute();
        $this->assertEquals([
            $expected_value[$revision_key] => $expected_value[$id_key],
        ], $result);
    }
}

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