Issue #3025785 by amateescu, mallezie, s_leu, smustgrave, tim.plunkett: Cannot create entity with image in a workspace

(cherry picked from commit 4229a611e5)
merge-requests/6829/merge
catch 2024-03-12 11:11:27 +00:00
parent 081b69b53e
commit 36465e605c
4 changed files with 104 additions and 0 deletions

View File

@ -62,6 +62,12 @@ class EntityTypeInfo implements ContainerInjectionInterface {
} }
} }
// 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 // Internal entity types are allowed to perform CRUD operations inside a
// workspace. // workspace.
if ($entity_type->isInternal()) { if ($entity_type->isInternal()) {

View File

@ -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]);
}
}

View File

@ -2,6 +2,7 @@
namespace Drupal\Tests\workspaces\Kernel; namespace Drupal\Tests\workspaces\Kernel;
use Drupal\workspaces\Entity\Handler\IgnoredWorkspaceHandler;
use Drupal\workspaces\Entity\Workspace; use Drupal\workspaces\Entity\Workspace;
/** /**
@ -140,4 +141,17 @@ trait WorkspaceTestTrait {
return $query->execute(); 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();
}
} }

View File

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