Commit a0c92ff1 authored by Francesco Placella's avatar Francesco Placella
Browse files

Issue #2946402 by Sam152, awm, jibran, lamp5, mattshoaf, amateescu: Content...

Issue #2946402 by Sam152, awm, jibran, lamp5, mattshoaf, amateescu: Content moderation incorrectly always assumes a language is being added when the default language of an entity is being changed

(cherry picked from commit dd571111)
parent 8f2862aa
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -184,6 +184,10 @@ protected function updateOrCreateFromEntity(EntityInterface $entity) {
    // Sync translations.
    if ($entity->getEntityType()->hasKey('langcode')) {
      $entity_langcode = $entity->language()->getId();
      if ($entity->isDefaultTranslation()) {
        $content_moderation_state->langcode = $entity_langcode;
      }
      else {
        if (!$content_moderation_state->hasTranslation($entity_langcode)) {
          $content_moderation_state->addTranslation($entity_langcode);
        }
@@ -191,6 +195,7 @@ protected function updateOrCreateFromEntity(EntityInterface $entity) {
          $content_moderation_state = $content_moderation_state->getTranslation($entity_langcode);
        }
      }
    }

    // If a new revision of the content has been created, add a new content
    // moderation state revision.
+60 −2
Original line number Diff line number Diff line
@@ -416,25 +416,83 @@ public function testModerationWithFieldConfigOverride() {

  /**
   * Tests that entities with special languages can be moderated.
   *
   * @dataProvider moderationWithSpecialLanguagesTestCases
   */
  public function testModerationWithSpecialLanguages() {
  public function testModerationWithSpecialLanguages($original_language, $updated_language) {
    $workflow = $this->createEditorialWorkflow();
    $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev');
    $workflow->save();

    // Create a test entity.
    $entity = EntityTestRev::create([
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      'langcode' => $original_language,
    ]);
    $entity->save();
    $this->assertEquals('draft', $entity->moderation_state->value);

    $entity->moderation_state->value = 'published';
    $entity->langcode = $updated_language;
    $entity->save();

    $this->assertEquals('published', EntityTestRev::load($entity->id())->moderation_state->value);
  }

  /**
   * Test cases for ::testModerationWithSpecialLanguages().
   */
  public function moderationWithSpecialLanguagesTestCases() {
    return [
      'Not specified to not specified' => [
        LanguageInterface::LANGCODE_NOT_SPECIFIED,
        LanguageInterface::LANGCODE_NOT_SPECIFIED,
      ],
      'English to not specified' => [
        'en',
        LanguageInterface::LANGCODE_NOT_SPECIFIED,
      ],
      'Not specified to english' => [
        LanguageInterface::LANGCODE_NOT_SPECIFIED,
        'en',
      ],
    ];
  }

  /**
   * Test changing the language of content without adding a translation.
   */
  public function testChangingContentLangcode() {
    ConfigurableLanguage::createFromLangcode('fr')->save();
    NodeType::create([
      'type' => 'test_type',
    ])->save();
    $workflow = $this->createEditorialWorkflow();
    $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'test_type');
    $workflow->save();

    $entity = Node::create([
      'title' => 'Test node',
      'langcode' => 'en',
      'type' => 'test_type',
    ]);
    $entity->save();

    $content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity);
    $this->assertCount(1, $entity->getTranslationLanguages());
    $this->assertCount(1, $content_moderation_state->getTranslationLanguages());
    $this->assertEquals('en', $entity->langcode->value);
    $this->assertEquals('en', $content_moderation_state->langcode->value);

    $entity->langcode = 'fr';
    $entity->save();

    $content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity);
    $this->assertCount(1, $entity->getTranslationLanguages());
    $this->assertCount(1, $content_moderation_state->getTranslationLanguages());
    $this->assertEquals('fr', $entity->langcode->value);
    $this->assertEquals('fr', $content_moderation_state->langcode->value);
  }

  /**
   * Tests that a non-translatable entity type with a langcode can be moderated.
   */