Verified Commit e179aaec authored by Dave Long's avatar Dave Long
Browse files

fix: #3611529 Moderation state field definitions can have an incorrect target bundle

By: clayfreeman
By: acbramley
(cherry picked from commit 28d2798c)
parent daf0e8e3
Loading
Loading
Loading
Loading
Loading
+1 −6
Original line number Diff line number Diff line
@@ -95,12 +95,7 @@ public function entityBaseFieldInfo(EntityTypeInterface $entity_type): array {
  #[Hook('entity_bundle_field_info')]
  public function entityBundleFieldInfo(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions): array {
    if (isset($base_field_definitions['moderation_state'])) {
      // Add the target bundle to the moderation state field. Since each bundle
      // can be attached to a different moderation workflow, adding this
      // information to the field definition allows the associated workflow to
      // be derived where a field definition is present.
      $base_field_definitions['moderation_state']->setTargetBundle($bundle);
      return ['moderation_state' => $base_field_definitions['moderation_state']];
      return ['moderation_state' => clone $base_field_definitions['moderation_state']];
    }
    return [];
  }
+79 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\content_moderation\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Tests the moderation state field.
 */
#[Group('content_moderation')]
#[RunTestsInSeparateProcesses]
class ModerationStateFieldTest extends KernelTestBase {

  use ContentModerationTestTrait;
  use ContentTypeCreationTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'workflows',
    'content_moderation',
    'node',
    'user',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    $this->installSchema('node', 'node_access');
    $this->installEntitySchema('workflow');
    $this->installEntitySchema('content_moderation_state');
    $this->installEntitySchema('node');
    $this->installEntitySchema('user');
  }

  /**
   * Tests that the moderation state field target bundle is always correct.
   */
  public function testFieldDefinitionBundles(): void {
    $workflow = $this->createEditorialWorkflow();

    /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModerationInterface $workflow_type */
    $workflow_type = $workflow->getTypePlugin();

    $node_types = [];

    for ($i = 0; $i < 3; ++$i) {
      $node_type = $this->createContentType(create_body: FALSE);

      $workflow_type->addEntityTypeAndBundle('node', $node_type->id());

      $node_types[] = $node_type;
    }

    $workflow->save();

    /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
    $entity_field_manager = \Drupal::service('entity_field.manager');

    $this->assertNotEmpty($node_types);

    foreach ($node_types as $node_type) {
      $field_definitions = $entity_field_manager->getFieldDefinitions('node', $node_type->id());

      $this->assertSame($node_type->id(), $field_definitions['moderation_state']->getTargetBundle());
    }
  }

}