diff --git a/core/modules/workspaces/src/EntityTypeInfo.php b/core/modules/workspaces/src/EntityTypeInfo.php index 55083bdc590e5c591fa671eff0d04c2822bf2021..f1c71408df759d07b2b42ff6aa4bcd5e59fa53e6 100644 --- a/core/modules/workspaces/src/EntityTypeInfo.php +++ b/core/modules/workspaces/src/EntityTypeInfo.php @@ -62,6 +62,12 @@ public function entityTypeBuild(array &$entity_types) { } } + // The 'file' entity type is allowed to perform CRUD operations inside a + // workspace without being tracked. + if ($entity_type->id() === 'file') { + $entity_type->setHandlerClass('workspace', IgnoredWorkspaceHandler::class); + } + // Internal entity types are allowed to perform CRUD operations inside a // workspace. if ($entity_type->isInternal()) { diff --git a/core/modules/workspaces/tests/modules/workspaces_test/workspaces_test.module b/core/modules/workspaces/tests/modules/workspaces_test/workspaces_test.module new file mode 100644 index 0000000000000000000000000000000000000000..27eef7c5c84f5f15970e999caa8a60634e19a170 --- /dev/null +++ b/core/modules/workspaces/tests/modules/workspaces_test/workspaces_test.module @@ -0,0 +1,19 @@ +<?php + +/** + * @file + * Provides supporting code for testing workspaces. + */ + +/** + * Implements hook_entity_type_alter(). + */ +function workspaces_test_entity_type_alter(array &$entity_types) { + $state = \Drupal::state(); + + // Allow all entity types to have their definition changed dynamically for + // testing purposes. + foreach ($entity_types as $entity_type_id => $entity_type) { + $entity_types[$entity_type_id] = $state->get("$entity_type_id.entity_type", $entity_types[$entity_type_id]); + } +} diff --git a/core/modules/workspaces/tests/src/Kernel/WorkspaceTestTrait.php b/core/modules/workspaces/tests/src/Kernel/WorkspaceTestTrait.php index 0fd6c43b45ab0b69087510bdcb89a43dad90d235..a6e99937ff5074de0a78ffef907faa25952d448c 100644 --- a/core/modules/workspaces/tests/src/Kernel/WorkspaceTestTrait.php +++ b/core/modules/workspaces/tests/src/Kernel/WorkspaceTestTrait.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\workspaces\Kernel; +use Drupal\workspaces\Entity\Handler\IgnoredWorkspaceHandler; use Drupal\workspaces\Entity\Workspace; /** @@ -140,4 +141,17 @@ protected function getUnassociatedRevisions($entity_type_id, $entity_ids = NULL) return $query->execute(); } + /** + * Marks an entity type as ignored in a workspace. + * + * @param string $entity_type_id + * The entity type ID. + */ + protected function ignoreEntityType(string $entity_type_id): void { + $entity_type = clone \Drupal::entityTypeManager()->getDefinition($entity_type_id); + $entity_type->setHandlerClass('workspace', IgnoredWorkspaceHandler::class); + \Drupal::state()->set("$entity_type_id.entity_type", $entity_type); + \Drupal::entityTypeManager()->clearCachedDefinitions(); + } + } diff --git a/core/modules/workspaces/tests/src/Kernel/WorkspacesFileItemTest.php b/core/modules/workspaces/tests/src/Kernel/WorkspacesFileItemTest.php new file mode 100644 index 0000000000000000000000000000000000000000..eee017f9bd65a9c645a409a50ecc468b4ef778fb --- /dev/null +++ b/core/modules/workspaces/tests/src/Kernel/WorkspacesFileItemTest.php @@ -0,0 +1,65 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Tests\workspaces\Kernel; + +use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Tests\file\Kernel\FileItemTest; +use Drupal\Tests\user\Traits\UserCreationTrait; +use Drupal\workspaces\Entity\Workspace; + +/** + * Tests using entity fields of the file field type in a workspace. + * + * @group workspaces + */ +class WorkspacesFileItemTest extends FileItemTest { + + use UserCreationTrait; + use WorkspaceTestTrait; + + /** + * The entity type manager. + */ + protected EntityTypeManagerInterface $entityTypeManager; + + /** + * {@inheritdoc} + */ + protected static $modules = [ + 'file', + 'path_alias', + 'workspaces', + 'workspaces_test', + ]; + + /** + * {@inheritdoc} + */ + protected function setUp(): void { + parent::setUp(); + + $this->entityTypeManager = \Drupal::entityTypeManager(); + + $this->installEntitySchema('workspace'); + $this->installSchema('workspaces', ['workspace_association']); + + // Create a new workspace and activate it. + Workspace::create(['id' => 'stage', 'label' => 'Stage'])->save(); + $this->switchToWorkspace('stage'); + } + + /** + * {@inheritdoc} + */ + public function testFileItem(): void { + // Ignore entity types that are not being tested, in order to fully re-use + // the parent test method. + $this->ignoreEntityType('entity_test'); + $this->ignoreEntityType('entity_view_display'); + + parent::testFileItem(); + } + +}