function ModerationFormTest::testContentTranslationNodeForm

Same name in other branches
  1. 9 core/modules/content_moderation/tests/src/Functional/ModerationFormTest.php \Drupal\Tests\content_moderation\Functional\ModerationFormTest::testContentTranslationNodeForm()
  2. 8.9.x core/modules/content_moderation/tests/src/Functional/ModerationFormTest.php \Drupal\Tests\content_moderation\Functional\ModerationFormTest::testContentTranslationNodeForm()
  3. 10 core/modules/content_moderation/tests/src/Functional/ModerationFormTest.php \Drupal\Tests\content_moderation\Functional\ModerationFormTest::testContentTranslationNodeForm()

Tests translated and moderated nodes.

File

core/modules/content_moderation/tests/src/Functional/ModerationFormTest.php, line 292

Class

ModerationFormTest
Tests the moderation form, specifically on nodes.

Namespace

Drupal\Tests\content_moderation\Functional

Code

public function testContentTranslationNodeForm() : void {
    $this->drupalLogin($this->rootUser);
    // Add French language.
    static::createLanguageFromLangcode('fr');
    // Enable content translation on moderated_content.
    $this->enableContentTranslation('node', 'moderated_content');
    // Adding languages requires a container rebuild in the test running
    // environment so that multilingual services are used.
    $this->rebuildContainer();
    // Create new moderated content in draft (revision 1).
    $this->drupalGet('node/add/moderated_content');
    $this->submitForm([
        'title[0][value]' => 'Some moderated content',
        'body[0][value]' => 'First version of the content.',
        'moderation_state[0][state]' => 'draft',
    ], 'Save');
    $this->assertSession()
        ->elementExists('xpath', '//ul[@class="entity-moderation-form"]');
    $node = $this->drupalGetNodeByTitle('Some moderated content');
    $this->assertNotEmpty($node->language(), 'en');
    $edit_path = sprintf('node/%d/edit', $node->id());
    $translate_path = sprintf('node/%d/translations/add/en/fr', $node->id());
    $latest_version_path = sprintf('node/%d/latest', $node->id());
    $french = \Drupal::languageManager()->getLanguage('fr');
    $this->drupalGet($latest_version_path);
    $this->assertSession()
        ->statusCodeEquals(403);
    $this->assertSession()
        ->elementNotExists('xpath', '//ul[@class="entity-moderation-form"]');
    // Add french translation (revision 2).
    $this->drupalGet($translate_path);
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()
        ->optionNotExists('moderation_state[0][state]', 'archived');
    $this->submitForm([
        'body[0][value]' => 'Second version of the content.',
        'moderation_state[0][state]' => 'published',
    ], 'Save (this translation)');
    $this->drupalGet($latest_version_path, [
        'language' => $french,
    ]);
    $this->assertSession()
        ->statusCodeEquals(403);
    $this->assertSession()
        ->elementNotExists('xpath', '//ul[@class="entity-moderation-form"]');
    // Add french pending revision (revision 3).
    $this->drupalGet($edit_path, [
        'language' => $french,
    ]);
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'archived');
    // Preview the content while selecting the "draft" state and when the user
    // returns to the edit form, ensure all of the available transitions are
    // still those available from the "published" source state.
    $this->submitForm([
        'moderation_state[0][state]' => 'draft',
    ], 'Preview');
    $this->clickLink('Back to content editing');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'archived');
    $this->submitForm([
        'body[0][value]' => 'Third version of the content.',
        'moderation_state[0][state]' => 'draft',
    ], 'Save (this translation)');
    $this->drupalGet($latest_version_path, [
        'language' => $french,
    ]);
    $this->assertSession()
        ->elementExists('xpath', '//ul[@class="entity-moderation-form"]');
    $this->drupalGet($edit_path);
    $this->clickLink('Delete');
    $this->assertSession()
        ->buttonExists('Delete');
    $this->drupalGet($latest_version_path);
    $this->assertSession()
        ->elementNotExists('xpath', '//ul[@class="entity-moderation-form"]');
    // Publish the french pending revision (revision 4).
    $this->drupalGet($edit_path, [
        'language' => $french,
    ]);
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()
        ->optionNotExists('moderation_state[0][state]', 'archived');
    $this->submitForm([
        'body[0][value]' => 'Fifth version of the content.',
        'moderation_state[0][state]' => 'published',
    ], 'Save (this translation)');
    $this->drupalGet($latest_version_path, [
        'language' => $french,
    ]);
    $this->assertSession()
        ->elementNotExists('xpath', '//ul[@class="entity-moderation-form"]');
    // Publish the English pending revision (revision 5).
    $this->drupalGet($edit_path);
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()
        ->optionNotExists('moderation_state[0][state]', 'archived');
    $this->submitForm([
        'body[0][value]' => 'Sixth version of the content.',
        'moderation_state[0][state]' => 'published',
    ], 'Save (this translation)');
    $this->drupalGet($latest_version_path);
    $this->assertSession()
        ->elementNotExists('xpath', '//ul[@class="entity-moderation-form"]');
    // Make sure we are allowed to create a pending French revision.
    $this->drupalGet($edit_path, [
        'language' => $french,
    ]);
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'archived');
    // Add an English pending revision (revision 6).
    $this->drupalGet($edit_path);
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'archived');
    $this->submitForm([
        'body[0][value]' => 'Seventh version of the content.',
        'moderation_state[0][state]' => 'draft',
    ], 'Save (this translation)');
    $this->drupalGet($latest_version_path);
    $this->assertSession()
        ->elementExists('xpath', '//ul[@class="entity-moderation-form"]');
    $this->drupalGet($latest_version_path, [
        'language' => $french,
    ]);
    $this->assertSession()
        ->elementNotExists('xpath', '//ul[@class="entity-moderation-form"]');
    // Publish the English pending revision (revision 7)
    $this->drupalGet($edit_path);
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()
        ->optionNotExists('moderation_state[0][state]', 'archived');
    $this->submitForm([
        'body[0][value]' => 'Eighth version of the content.',
        'moderation_state[0][state]' => 'published',
    ], 'Save (this translation)');
    $this->drupalGet($latest_version_path);
    $this->assertSession()
        ->elementNotExists('xpath', '//ul[@class="entity-moderation-form"]');
    // Make sure we are allowed to create a pending French revision.
    $this->drupalGet($edit_path, [
        'language' => $french,
    ]);
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'archived');
    // Make sure we are allowed to create a pending English revision.
    $this->drupalGet($edit_path);
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'archived');
    // Create new moderated content (revision 1).
    $this->drupalGet('node/add/moderated_content');
    $this->submitForm([
        'title[0][value]' => 'Third moderated content',
        'moderation_state[0][state]' => 'published',
    ], 'Save');
    $node = $this->drupalGetNodeByTitle('Third moderated content');
    $this->assertNotEmpty($node->language(), 'en');
    $edit_path = sprintf('node/%d/edit', $node->id());
    $translate_path = sprintf('node/%d/translations/add/en/fr', $node->id());
    // Translate it, without updating data (revision 2).
    $this->drupalGet($translate_path);
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'archived');
    $this->submitForm([
        'moderation_state[0][state]' => 'draft',
    ], 'Save (this translation)');
    // Add another draft for the translation (revision 3).
    $this->drupalGet($edit_path, [
        'language' => $french,
    ]);
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()
        ->optionNotExists('moderation_state[0][state]', 'archived');
    $this->submitForm([
        'moderation_state[0][state]' => 'draft',
    ], 'Save (this translation)');
    // Updating and publishing the french translation is still possible.
    $this->drupalGet($edit_path, [
        'language' => $french,
    ]);
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()
        ->optionNotExists('moderation_state[0][state]', 'archived');
    $this->submitForm([
        'moderation_state[0][state]' => 'published',
    ], 'Save (this translation)');
    // Now the french translation is published, an english draft can be added.
    $this->drupalGet($edit_path);
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()
        ->optionExists('moderation_state[0][state]', 'archived');
    $this->submitForm([
        'moderation_state[0][state]' => 'draft',
    ], 'Save (this translation)');
}

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