From fe005647ad91823a9bcf2c6fe06cc4c0d7c24b30 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Mon, 22 Mar 2021 11:28:15 +0000
Subject: [PATCH] Issue #3192363 by anmolgoyal74, amateescu: Ensure that
 moderation can not be enabled for the 'workspace' entity type

---
 .../content_moderation/src/EntityTypeInfo.php        |  9 ++++++++-
 .../Kernel/WorkspacesContentModerationStateTest.php  | 12 ++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/core/modules/content_moderation/src/EntityTypeInfo.php b/core/modules/content_moderation/src/EntityTypeInfo.php
index 00ddda2dfcda..f073e25a389e 100644
--- a/core/modules/content_moderation/src/EntityTypeInfo.php
+++ b/core/modules/content_moderation/src/EntityTypeInfo.php
@@ -133,7 +133,14 @@ public function entityTypeAlter(array &$entity_types) {
       // entity type needs to be excluded for now.
       // @todo Enable moderation for path aliases after they become publishable
       //   in https://www.drupal.org/project/drupal/issues/3007669.
-      if ($entity_type->isRevisionable() && !$entity_type->isInternal() && $entity_type_id !== 'path_alias') {
+      // Workspace entities can not be moderated because they use string IDs.
+      // @see \Drupal\content_moderation\Entity\ContentModerationState::baseFieldDefinitions()
+      // where the target entity ID is defined as an integer.
+      $entity_type_to_exclude = [
+        'path_alias',
+        'workspace',
+      ];
+      if ($entity_type->isRevisionable() && !$entity_type->isInternal() && !in_array($entity_type_id, $entity_type_to_exclude)) {
         $entity_types[$entity_type_id] = $this->addModerationToEntityType($entity_type);
       }
     }
diff --git a/core/modules/content_moderation/tests/src/Kernel/WorkspacesContentModerationStateTest.php b/core/modules/content_moderation/tests/src/Kernel/WorkspacesContentModerationStateTest.php
index 17eba99f7c7d..79cf23286c45 100644
--- a/core/modules/content_moderation/tests/src/Kernel/WorkspacesContentModerationStateTest.php
+++ b/core/modules/content_moderation/tests/src/Kernel/WorkspacesContentModerationStateTest.php
@@ -49,6 +49,18 @@ protected function setUp(): void {
     $this->switchToWorkspace('stage');
   }
 
+  /**
+   * Tests that the 'workspace' entity type can not be moderated.
+   *
+   * @see \Drupal\workspaces\EntityTypeInfo::entityTypeAlter()
+   */
+  public function testWorkspaceEntityTypeModeration() {
+    /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
+    $moderation_info = \Drupal::service('content_moderation.moderation_information');
+    $entity_type = \Drupal::entityTypeManager()->getDefinition('workspace');
+    $this->assertFalse($moderation_info->canModerateEntitiesOfEntityType($entity_type));
+  }
+
   /**
    * Tests the integration between Content Moderation and Workspaces.
    *
-- 
GitLab