function WorkspaceIntegrationTest::assertEntityLoad

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

Asserts that default revisions are properly swapped 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::assertEntityLoad()
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 825

Class

WorkspaceIntegrationTest
Tests a complete deployment scenario across different workspaces.

Namespace

Drupal\Tests\workspaces\Kernel

Code

protected function assertEntityLoad(array $expected_values, $entity_type_id) {
    // 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;
    });
    $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'];
    // Check \Drupal\Core\Entity\EntityStorageInterface::loadMultiple().
    
    /** @var \Drupal\Core\Entity\RevisionableInterface[]|\Drupal\Core\Entity\EntityPublishedInterface[] $entities */
    $entities = $this->entityTypeManager
        ->getStorage($entity_type_id)
        ->loadMultiple(array_column($expected_default_revisions, $id_key));
    foreach ($expected_default_revisions as $expected_default_revision) {
        $entity_id = $expected_default_revision[$id_key];
        $this->assertEquals($expected_default_revision[$revision_key], $entities[$entity_id]->getRevisionId());
        $this->assertEquals($expected_default_revision[$label_key], $entities[$entity_id]->label());
        $this->assertEquals($expected_default_revision[$published_key], $entities[$entity_id]->isPublished());
    }
    // Check loading entities one by one. It is important to do these checks
    // after the "multiple load" ones above so we can test with a fully warmed
    // static cache.
    foreach ($expected_default_revisions as $expected_default_revision) {
        $entity_id = $expected_default_revision[$id_key];
        $entities = $this->entityTypeManager
            ->getStorage($entity_type_id)
            ->loadMultiple([
            $entity_id,
        ]);
        $this->assertCount(1, $entities);
        $this->assertEquals($expected_default_revision[$revision_key], $entities[$entity_id]->getRevisionId());
        $this->assertEquals($expected_default_revision[$label_key], $entities[$entity_id]->label());
        $this->assertEquals($expected_default_revision[$published_key], $entities[$entity_id]->isPublished());
    }
    // Check \Drupal\Core\Entity\EntityStorageInterface::loadUnchanged().
    foreach ($expected_default_revisions as $expected_default_revision) {
        
        /** @var \Drupal\Core\Entity\RevisionableInterface|\Drupal\Core\Entity\EntityPublishedInterface $entity */
        $entity = $this->entityTypeManager
            ->getStorage($entity_type_id)
            ->loadUnchanged($expected_default_revision[$id_key]);
        $this->assertEquals($expected_default_revision[$revision_key], $entity->getRevisionId());
        $this->assertEquals($expected_default_revision[$label_key], $entity->label());
        $this->assertEquals($expected_default_revision[$published_key], $entity->isPublished());
    }
}

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