You are here

public function ModerationFormTest::testContentTranslationNodeForm in Drupal 9

Same name and namespace in other branches
  1. 8 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 281

Class

ModerationFormTest
Tests the moderation form, specifically on nodes.

Namespace

Drupal\Tests\content_moderation\Functional

Code

public function testContentTranslationNodeForm() {
  $this
    ->drupalLogin($this->rootUser);

  // Add French language.
  $edit = [
    'predefined_langcode' => 'fr',
  ];
  $this
    ->drupalGet('admin/config/regional/language/add');
  $this
    ->submitForm($edit, 'Add language');

  // Enable content translation on articles.
  $this
    ->drupalGet('admin/config/regional/content-language');
  $edit = [
    'entity_types[node]' => TRUE,
    'settings[node][moderated_content][translatable]' => TRUE,
    'settings[node][moderated_content][settings][language][language_alterable]' => TRUE,
  ];
  $this
    ->submitForm($edit, 'Save configuration');

  // 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
    ->assertNotEmpty($this
    ->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
    ->assertEmpty($this
    ->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
    ->assertEmpty($this
    ->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
    ->assertNotEmpty($this
    ->xpath('//ul[@class="entity-moderation-form"]'));
  $this
    ->drupalGet($edit_path);
  $this
    ->clickLink('Delete');
  $this
    ->assertSession()
    ->buttonExists('Delete');
  $this
    ->drupalGet($latest_version_path);
  $this
    ->assertEmpty($this
    ->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
    ->assertEmpty($this
    ->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
    ->assertEmpty($this
    ->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
    ->assertNotEmpty($this
    ->xpath('//ul[@class="entity-moderation-form"]'));
  $this
    ->drupalGet($latest_version_path, [
    'language' => $french,
  ]);
  $this
    ->assertEmpty($this
    ->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
    ->assertEmpty($this
    ->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)');
}