You are here

public function ModerationFormTest::testContentTranslationNodeForm in Drupal 8

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

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
    ->drupalPostForm('admin/config/regional/language/add', $edit, t('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
    ->drupalPostForm(NULL, $edit, t('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
    ->drupalPostForm('node/add/moderated_content', [
    'title[0][value]' => 'Some moderated content',
    'body[0][value]' => 'First version of the content.',
    'moderation_state[0][state]' => 'draft',
  ], t('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
    ->drupalPostForm(NULL, [
    'body[0][value]' => 'Second version of the content.',
    'moderation_state[0][state]' => 'published',
  ], t('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
    ->drupalPostForm(NULL, [
    'body[0][value]' => 'Third version of the content.',
    'moderation_state[0][state]' => 'draft',
  ], t('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
    ->drupalPostForm(NULL, [
    'body[0][value]' => 'Fifth version of the content.',
    'moderation_state[0][state]' => 'published',
  ], t('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
    ->drupalPostForm(NULL, [
    'body[0][value]' => 'Sixth version of the content.',
    'moderation_state[0][state]' => 'published',
  ], t('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
    ->drupalPostForm(NULL, [
    'body[0][value]' => 'Seventh version of the content.',
    'moderation_state[0][state]' => 'draft',
  ], t('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
    ->drupalPostForm(NULL, [
    'body[0][value]' => 'Eighth version of the content.',
    'moderation_state[0][state]' => 'published',
  ], t('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
    ->drupalPostForm('node/add/moderated_content', [
    'title[0][value]' => 'Third moderated content',
    'moderation_state[0][state]' => 'published',
  ], t('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
    ->drupalPostForm(NULL, [
    'moderation_state[0][state]' => 'draft',
  ], t('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
    ->drupalPostForm(NULL, [
    'moderation_state[0][state]' => 'draft',
  ], t('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
    ->drupalPostForm(NULL, [
    'moderation_state[0][state]' => 'published',
  ], t('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
    ->drupalPostForm(NULL, [
    'moderation_state[0][state]' => 'draft',
  ], t('Save (this translation)'));
}