function ModerationLocaleTest::testTranslateModeratedContent

Same name and namespace in other branches
  1. 9 core/modules/content_moderation/tests/src/Functional/ModerationLocaleTest.php \Drupal\Tests\content_moderation\Functional\ModerationLocaleTest::testTranslateModeratedContent()
  2. 8.9.x core/modules/content_moderation/tests/src/Functional/ModerationLocaleTest.php \Drupal\Tests\content_moderation\Functional\ModerationLocaleTest::testTranslateModeratedContent()
  3. 11.x core/modules/content_moderation/tests/src/Functional/ModerationLocaleTest.php \Drupal\Tests\content_moderation\Functional\ModerationLocaleTest::testTranslateModeratedContent()

Tests article translations can be moderated separately.

File

core/modules/content_moderation/tests/src/Functional/ModerationLocaleTest.php, line 69

Class

ModerationLocaleTest
Test content_moderation functionality with localization and translation.

Namespace

Drupal\Tests\content_moderation\Functional

Code

public function testTranslateModeratedContent() : void {
  // Create a published article in English.
  $edit = [
    'title[0][value]' => 'Published English node',
    'langcode[0][value]' => 'en',
    'moderation_state[0][state]' => 'published',
  ];
  $this->drupalGet('node/add/article');
  $this->submitForm($edit, 'Save');
  $this->assertSession()
    ->pageTextContains('Article Published English node has been created.');
  $english_node = $this->drupalGetNodeByTitle('Published English node');
  // Add a French translation.
  $this->drupalGet('node/' . $english_node->id() . '/translations');
  $this->clickLink('Add');
  $edit = [
    'title[0][value]' => 'French node Draft',
    'moderation_state[0][state]' => 'draft',
  ];
  $this->submitForm($edit, 'Save (this translation)');
  // Here the error has occurred "The website encountered an unexpected error.
  // Try again later."
  // If the translation has got lost.
  $this->assertSession()
    ->pageTextContains('Article French node Draft has been updated.');
  // Create an article in English.
  $edit = [
    'title[0][value]' => 'English node',
    'langcode[0][value]' => 'en',
    'moderation_state[0][state]' => 'draft',
  ];
  $this->drupalGet('node/add/article');
  $this->submitForm($edit, 'Save');
  $this->assertSession()
    ->pageTextContains('Article English node has been created.');
  $english_node = $this->drupalGetNodeByTitle('English node');
  // Add a French translation.
  $this->drupalGet('node/' . $english_node->id() . '/translations');
  $this->clickLink('Add');
  $edit = [
    'title[0][value]' => 'French node',
    'moderation_state[0][state]' => 'draft',
  ];
  $this->submitForm($edit, 'Save (this translation)');
  $this->assertSession()
    ->pageTextContains('Article French node has been updated.');
  $english_node = $this->drupalGetNodeByTitle('English node', TRUE);
  // Publish the English article and check that the translation stays
  // unpublished.
  $this->drupalGet('node/' . $english_node->id() . '/edit');
  $this->submitForm([
    'moderation_state[0][state]' => 'published',
  ], 'Save (this translation)');
  $this->assertSession()
    ->pageTextContains('Article English node has been updated.');
  $english_node = $this->drupalGetNodeByTitle('English node', TRUE);
  $french_node = $english_node->getTranslation('fr');
  $this->assertEquals('French node', $french_node->label());
  $this->assertEquals('published', $english_node->moderation_state->value);
  $this->assertTrue($english_node->isPublished());
  $this->assertEquals('draft', $french_node->moderation_state->value);
  $this->assertFalse($french_node->isPublished());
  // Create another article with its translation. This time we will publish
  // the translation first.
  $edit = [
    'title[0][value]' => 'Another node',
    'moderation_state[0][state]' => 'draft',
  ];
  $this->drupalGet('node/add/article');
  $this->submitForm($edit, 'Save');
  $this->assertSession()
    ->pageTextContains('Article Another node has been created.');
  $english_node = $this->drupalGetNodeByTitle('Another node');
  // Add a French translation.
  $this->drupalGet('node/' . $english_node->id() . '/translations');
  $this->clickLink('Add');
  $edit = [
    'title[0][value]' => 'Translated node',
    'moderation_state[0][state]' => 'draft',
  ];
  $this->submitForm($edit, 'Save (this translation)');
  $this->assertSession()
    ->pageTextContains('Article Translated node has been updated.');
  $english_node = $this->drupalGetNodeByTitle('Another node', TRUE);
  // Publish the translation and check that the source language version stays
  // unpublished.
  $this->drupalGet('fr/node/' . $english_node->id() . '/edit');
  $this->submitForm([
    'moderation_state[0][state]' => 'published',
  ], 'Save (this translation)');
  $this->assertSession()
    ->pageTextContains('Article Translated node has been updated.');
  $english_node = $this->drupalGetNodeByTitle('Another node', TRUE);
  $french_node = $english_node->getTranslation('fr');
  $this->assertEquals('published', $french_node->moderation_state->value);
  $this->assertTrue($french_node->isPublished());
  $this->assertEquals('draft', $english_node->moderation_state->value);
  $this->assertFalse($english_node->isPublished());
  // Now check that we can create a new draft of the translation.
  $edit = [
    'title[0][value]' => 'New draft of translated node',
    'moderation_state[0][state]' => 'draft',
  ];
  $this->drupalGet('fr/node/' . $english_node->id() . '/edit');
  $this->submitForm($edit, 'Save (this translation)');
  $this->assertSession()
    ->pageTextContains('Article New draft of translated node has been updated.');
  $english_node = $this->drupalGetNodeByTitle('Another node', TRUE);
  $french_node = $english_node->getTranslation('fr');
  $this->assertEquals('published', $french_node->moderation_state->value);
  $this->assertTrue($french_node->isPublished());
  $this->assertEquals('Translated node', $french_node->getTitle(), 'The default revision of the published translation remains the same.');
  // Publish the French article before testing the archive transition.
  $this->drupalGet('fr/node/' . $english_node->id() . '/edit');
  $this->submitForm([
    'moderation_state[0][state]' => 'published',
  ], 'Save (this translation)');
  $this->assertSession()
    ->pageTextContains('Article New draft of translated node has been updated.');
  $english_node = $this->drupalGetNodeByTitle('Another node', TRUE);
  $french_node = $english_node->getTranslation('fr');
  $this->assertEquals('published', $french_node->moderation_state->value);
  $this->assertTrue($french_node->isPublished());
  $this->assertEquals('New draft of translated node', $french_node->getTitle(), 'The draft has replaced the published revision.');
  // Publish the English article before testing the archive transition.
  $this->drupalGet('node/' . $english_node->id() . '/edit');
  $this->submitForm([
    'moderation_state[0][state]' => 'published',
  ], 'Save (this translation)');
  $this->assertSession()
    ->pageTextContains('Article Another node has been updated.');
  $english_node = $this->drupalGetNodeByTitle('Another node', TRUE);
  $this->assertEquals('published', $english_node->moderation_state->value);
  // Archive the node and its translation.
  $this->drupalGet('node/' . $english_node->id() . '/edit');
  $this->submitForm([
    'moderation_state[0][state]' => 'archived',
  ], 'Save (this translation)');
  $this->assertSession()
    ->pageTextContains('Article Another node has been updated.');
  $this->drupalGet('fr/node/' . $english_node->id() . '/edit');
  $this->submitForm([
    'moderation_state[0][state]' => 'archived',
  ], 'Save (this translation)');
  $this->assertSession()
    ->pageTextContains('Article New draft of translated node has been updated.');
  $english_node = $this->drupalGetNodeByTitle('Another node', TRUE);
  $french_node = $english_node->getTranslation('fr');
  $this->assertEquals('archived', $english_node->moderation_state->value);
  $this->assertFalse($english_node->isPublished());
  $this->assertEquals('archived', $french_node->moderation_state->value);
  $this->assertFalse($french_node->isPublished());
}

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