function WorkspaceAssociationTest::testMoveTrackedEntitiesOfSpecificType

Tests moving all entities of a specific type while leaving others.

@legacy-covers ::moveTrackedEntities

File

core/modules/workspaces/tests/src/Kernel/WorkspaceAssociationTest.php, line 307

Class

WorkspaceAssociationTest
Tests workspace associations.

Namespace

Drupal\Tests\workspaces\Kernel

Code

public function testMoveTrackedEntitiesOfSpecificType() : void {
  $entity_type_id = 'entity_test_mulrevpub';
  $entity_type_id_string = 'entity_test_mulrevpub_string_id';
  $workspace_association = \Drupal::service('workspaces.association');
  // Get the workspace field name for later assertions.
  $entity_type_string_id = $this->entityTypeManager
    ->getDefinition($entity_type_id_string);
  $workspace_field_string_id = $entity_type_string_id->getRevisionMetadataKey('workspace');
  $storage_string_id = $this->entityTypeManager
    ->getStorage($entity_type_id_string);
  // Create entities of different types in 'preview'.
  $this->switchToWorkspace('preview');
  $this->createEntity($entity_type_id, [
    'name' => 'Integer entity 1',
  ]);
  $this->createEntity($entity_type_id, [
    'name' => 'Integer entity 2',
  ]);
  $entity_string_1 = $this->createEntity($entity_type_id_string, [
    'id' => 'str_1',
    'name' => 'String entity 1',
  ]);
  $entity_string_2 = $this->createEntity($entity_type_id_string, [
    'id' => 'str_2',
    'name' => 'String entity 2',
  ]);
  // Move only the integer ID entities to 'qa'.
  $workspace_association->moveTrackedEntities('preview', 'qa', $entity_type_id);
  // Verify integer ID entities were moved.
  $this->assertEmpty($workspace_association->getTrackedEntities('preview', $entity_type_id)[$entity_type_id] ?? []);
  $this->assertCount(2, $workspace_association->getTrackedEntities('qa', $entity_type_id)[$entity_type_id]);
  // Verify string ID entities remain in 'preview'.
  $this->assertCount(2, $workspace_association->getTrackedEntities('preview', $entity_type_id_string)[$entity_type_id_string]);
  $this->assertEmpty($workspace_association->getTrackedEntities('qa', $entity_type_id_string)[$entity_type_id_string] ?? []);
  $preview_string_entities = $workspace_association->getTrackedEntities('preview', $entity_type_id_string);
  $this->assertContains($entity_string_1->id(), $preview_string_entities[$entity_type_id_string] ?? []);
  $this->assertContains($entity_string_2->id(), $preview_string_entities[$entity_type_id_string] ?? []);
  // Verify string entities workspace field is still 'preview'.
  $string_revision = $storage_string_id->loadRevision($entity_string_1->getRevisionId());
  $this->assertEquals('preview', $string_revision->{$workspace_field_string_id}->target_id);
}

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