You are here

protected function WorkspaceIntegrationTest::assertEntityLoad in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceIntegrationTest::assertEntityLoad()
  2. 9 core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceIntegrationTest::assertEntityLoad()

Asserts that default revisions are properly swapped in a workspace.

@internal

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.

File

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

Class

WorkspaceIntegrationTest
Tests a complete publishing scenario across different workspaces.

Namespace

Drupal\Tests\workspaces\Kernel

Code

protected function assertEntityLoad(array $expected_values, string $entity_type_id) : void {

  // 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());
  }
}