Verified Commit 2d61a0ef authored by Dave Long's avatar Dave Long
Browse files

Issue #3047110 by pavlosdan, Rajeshreeputra, Manuel Garcia, slydevil,...

Issue #3047110 by pavlosdan, Rajeshreeputra, Manuel Garcia, slydevil, hugronaphor, dhirendra.mishra, vsujeetkumar, amateescu, n4r3n, kreynen, acbramley, quietone: Enable Content Moderation integration for taxonomy terms

(cherry picked from commit 07473ef2)
parent b727c14d
Loading
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\content_moderation\Entity\Handler;

use Drupal\Core\Form\FormStateInterface;

/**
 * Customizations for taxonomy term entities.
 *
 * @internal
 */
class TaxonomyTermModerationHandler extends ModerationHandler {

  /**
   * {@inheritdoc}
   */
  public function enforceRevisionsEntityFormAlter(array &$form, FormStateInterface $form_state, $form_id): void {
    $form['revision']['#default_value'] = TRUE;
    $form['revision']['#disabled'] = TRUE;
    $form['revision']['#description'] = $this->t('Revisions must be required when moderation is enabled.');
  }

  /**
   * {@inheritdoc}
   */
  public function enforceRevisionsBundleFormAlter(array &$form, FormStateInterface $form_state, $form_id): void {
    $form['revision']['#default_value'] = TRUE;
    $form['revision']['#disabled'] = TRUE;
    $form['revision']['#description'] = $this->t('Revisions must be required when moderation is enabled.');
  }

}
+2 −1
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
use Drupal\content_moderation\Entity\Handler\BlockContentModerationHandler;
use Drupal\content_moderation\Entity\Handler\ModerationHandler;
use Drupal\content_moderation\Entity\Handler\NodeModerationHandler;
use Drupal\content_moderation\Entity\Handler\TaxonomyTermModerationHandler;
use Drupal\content_moderation\Entity\Routing\EntityModerationRouteProvider;
use Symfony\Component\DependencyInjection\ContainerInterface;

@@ -80,6 +81,7 @@ class EntityTypeInfo implements ContainerInjectionInterface {
  protected $moderationHandlers = [
    'node' => NodeModerationHandler::class,
    'block_content' => BlockContentModerationHandler::class,
    'taxonomy_term' => TaxonomyTermModerationHandler::class,
  ];

  /**
@@ -144,7 +146,6 @@ public function entityTypeAlter(array &$entity_types) {
      $entity_type_to_exclude = [
        'path_alias',
        'workspace',
        'taxonomy_term',
      ];
      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);
+74 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\content_moderation\Functional;

use Drupal\Core\Language\LanguageInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;

/**
 * Tests the taxonomy term moderation handler.
 *
 * @group content_moderation
 */
class ModerationStateTaxonomyTermTest extends ModerationStateTestBase {

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

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

    // Create a "Tags" vocabulary.
    $bundle = Vocabulary::create([
      'vid' => 'tags',
      'name' => 'Tags',
      'new_revision' => FALSE,
    ])->save();
  }

  /**
   * Tests the taxonomy term moderation handler alters the forms as intended.
   *
   * @covers \Drupal\content_moderation\Entity\Handler\TaxonomyTermModerationHandler::enforceRevisionsEntityFormAlter
   * @covers \Drupal\content_moderation\Entity\Handler\TaxonomyTermModerationHandler::enforceRevisionsBundleFormAlter
   */
  public function testEnforceRevisionsEntityFormAlter(): void {
    $this->drupalLogin($this->adminUser);

    // Enable moderation for the tags vocabulary.
    $edit['bundles[tags]'] = TRUE;
    $this->drupalGet('/admin/config/workflow/workflows/manage/editorial/type/taxonomy_term');
    $this->submitForm($edit, 'Save');

    // Check that revision is checked by default when content moderation is
    // enabled for the vocabulary.
    $this->drupalGet('/admin/structure/taxonomy/manage/tags');
    $this->assertSession()->checkboxChecked('revision');
    $this->assertSession()->pageTextContains('Revisions must be required when moderation is enabled.');
    $this->assertSession()->fieldDisabled('revision');

    // Create a taxonomy term and save it as draft.
    $term = Term::create([
      'name' => 'Test tag',
      'vid' => 'tags',
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ]);
    $term->save();

    // Check that revision is checked by default when editing a term and
    // content moderation is enabled for the term's vocabulary.
    $this->drupalGet($term->toUrl('edit-form'));
    $this->assertSession()->checkboxChecked('revision');
    $this->assertSession()->pageTextContains('Revisions must be required when moderation is enabled.');
    $this->assertSession()->fieldDisabled('revision');
  }

}
+2 −0
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ abstract class ModerationStateTestBase extends BrowserTestBase {
    'use editorial transition archive',
    'use editorial transition archived_draft',
    'use editorial transition archived_published',
    'administer taxonomy',
  ];

  /**
@@ -69,6 +70,7 @@ abstract class ModerationStateTestBase extends BrowserTestBase {
    'block_content',
    'node',
    'entity_test',
    'taxonomy',
  ];

  /**
+5 −11
Original line number Diff line number Diff line
@@ -94,6 +94,7 @@ protected function setUp(): void {
    $this->installEntitySchema('block_content');
    $this->installEntitySchema('media');
    $this->installEntitySchema('file');
    $this->installEntitySchema('taxonomy_term');
    $this->installEntitySchema('content_moderation_state');
    $this->installConfig('content_moderation');
    $this->installSchema('file', 'file_usage');
@@ -174,6 +175,9 @@ public static function basicModerationTestCases() {
      'Nodes' => [
        'node',
      ],
      'Taxonomy term' => [
        'taxonomy_term',
      ],
      'Block content' => [
        'block_content',
      ],
@@ -298,7 +302,7 @@ public function testContentModerationStateTranslationDataRemoval($entity_type_id
      /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
      $entity = $this->createEntity($entity_type_id, 'published');
      $langcode = 'fr';
      $translation = $entity->addTranslation($langcode, ['title' => 'French title test']);
      $translation = $entity->addTranslation($langcode, [$entity->getEntityType()->getKey('label') => 'French title test']);
      // Make sure we add values for all of the required fields.
      if ($entity_type_id == 'block_content') {
        $translation->info = $this->randomString();
@@ -813,14 +817,4 @@ protected function assertDefaultRevision(EntityInterface $entity, int $revision_
    }
  }

  /**
   * Tests that the 'taxonomy_term' entity type cannot be moderated.
   */
  public function testTaxonomyTermEntityTypeModeration() {
    /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
    $moderation_info = \Drupal::service('content_moderation.moderation_information');
    $entity_type = \Drupal::entityTypeManager()->getDefinition('taxonomy_term');
    $this->assertFalse($moderation_info->canModerateEntitiesOfEntityType($entity_type));
  }

}
Loading