function ContentModerationStateTest::testMultilingualModeration

Same name in other branches
  1. 9 core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php \Drupal\Tests\content_moderation\Kernel\ContentModerationStateTest::testMultilingualModeration()
  2. 8.9.x core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php \Drupal\Tests\content_moderation\Kernel\ContentModerationStateTest::testMultilingualModeration()
  3. 11.x core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php \Drupal\Tests\content_moderation\Kernel\ContentModerationStateTest::testMultilingualModeration()

Tests basic multilingual content moderation through the API.

File

core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php, line 366

Class

ContentModerationStateTest
Tests links between a content entity and a content_moderation_state entity.

Namespace

Drupal\Tests\content_moderation\Kernel

Code

public function testMultilingualModeration() : void {
    $this->createContentType([
        'type' => 'example',
    ]);
    $workflow = $this->createEditorialWorkflow();
    $this->addEntityTypeAndBundleToWorkflow($workflow, 'node', 'example');
    $english_node = Node::create([
        'type' => 'example',
        'title' => 'Test title',
    ]);
    // Revision 1 (en).
    $english_node->setUnpublished()
        ->save();
    $this->assertEquals('draft', $english_node->moderation_state->value);
    $this->assertFalse($english_node->isPublished());
    // Create a French translation.
    $french_node = $english_node->addTranslation('fr', [
        'title' => 'French title',
    ]);
    $french_node->setUnpublished();
    // Revision 2 (fr).
    $french_node->save();
    $french_node = $this->reloadEntity($english_node)
        ->getTranslation('fr');
    $this->assertEquals('draft', $french_node->moderation_state->value);
    $this->assertFalse($french_node->isPublished());
    // Move English node to create another draft.
    $english_node = $this->reloadEntity($english_node);
    $english_node->moderation_state->value = 'draft';
    // Revision 3 (en, fr).
    $english_node->save();
    $english_node = $this->reloadEntity($english_node);
    $this->assertEquals('draft', $english_node->moderation_state->value);
    // French node should still be in draft.
    $french_node = $this->reloadEntity($english_node)
        ->getTranslation('fr');
    $this->assertEquals('draft', $french_node->moderation_state->value);
    // Publish the French node.
    $french_node->moderation_state->value = 'published';
    // Revision 4 (en, fr).
    $french_node->save();
    $french_node = $this->reloadEntity($french_node)
        ->getTranslation('fr');
    $this->assertTrue($french_node->isPublished());
    $this->assertEquals('published', $french_node->moderation_state->value);
    $this->assertTrue($french_node->isPublished());
    $english_node = $french_node->getTranslation('en');
    $this->assertEquals('draft', $english_node->moderation_state->value);
    // Publish the English node.
    $english_node->moderation_state->value = 'published';
    // Revision 5 (en, fr).
    $english_node->save();
    $english_node = $this->reloadEntity($english_node);
    $this->assertTrue($english_node->isPublished());
    // Move the French node back to draft.
    $french_node = $this->reloadEntity($english_node)
        ->getTranslation('fr');
    $this->assertTrue($french_node->isPublished());
    $french_node->moderation_state->value = 'draft';
    // Revision 6 (en, fr).
    $french_node->save();
    $french_node = $this->reloadEntity($english_node, 6)
        ->getTranslation('fr');
    $this->assertFalse($french_node->isPublished());
    $this->assertTrue($french_node->getTranslation('en')
        ->isPublished());
    // Republish the French node.
    $french_node->moderation_state->value = 'published';
    // Revision 7 (en, fr).
    $french_node->save();
    $french_node = $this->reloadEntity($english_node)
        ->getTranslation('fr');
    $this->assertTrue($french_node->isPublished());
    // Change the EN state without saving the node.
    $content_moderation_state = ContentModerationState::loadFromModeratedEntity($english_node);
    $content_moderation_state->set('moderation_state', 'draft');
    $content_moderation_state->setNewRevision(TRUE);
    // Revision 8 (en, fr).
    $content_moderation_state->save();
    $english_node = $this->reloadEntity($french_node, $french_node->getRevisionId() + 1);
    $this->assertEquals('draft', $english_node->moderation_state->value);
    $french_node = $this->reloadEntity($english_node)
        ->getTranslation('fr');
    $this->assertEquals('published', $french_node->moderation_state->value);
    // This should unpublish the French node.
    $content_moderation_state = ContentModerationState::loadFromModeratedEntity($english_node);
    $content_moderation_state = $content_moderation_state->getTranslation('fr');
    $content_moderation_state->set('moderation_state', 'draft');
    $content_moderation_state->setNewRevision(TRUE);
    // Revision 9 (en, fr).
    $content_moderation_state->save();
    $english_node = $this->reloadEntity($english_node, $english_node->getRevisionId());
    $this->assertEquals('draft', $english_node->moderation_state->value);
    $french_node = $this->reloadEntity($english_node, '9')
        ->getTranslation('fr');
    $this->assertEquals('draft', $french_node->moderation_state->value);
    // Switching the moderation state to an unpublished state should update the
    // entity.
    $this->assertFalse($french_node->isPublished());
    // Check that revision 7 is still the default one for the node.
    $this->assertDefaultRevision($english_node, 7);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.