View source
<?php
namespace Drupal\Tests\content_moderation\Functional;
use Drupal\node\NodeInterface;
class ModerationLocaleTest extends ModerationStateTestBase {
protected static $modules = [
'node',
'content_moderation',
'locale',
'content_translation',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this
->drupalLogin($this->rootUser);
$this
->createContentTypeFromUi('Article', 'article', TRUE);
foreach ([
'fr',
'it',
] as $langcode) {
$edit = [
'predefined_langcode' => $langcode,
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add language');
}
$this
->drupalGet('admin/config/regional/content-language');
$edit = [
'entity_types[node]' => TRUE,
'settings[node][article][translatable]' => TRUE,
'settings[node][article][settings][language][language_alterable]' => TRUE,
];
$this
->submitForm($edit, 'Save configuration');
$this
->rebuildContainer();
}
public function testTranslateModeratedContent() {
$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');
$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)');
$this
->assertSession()
->pageTextContains('Article French node Draft has been updated.');
$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');
$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);
$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());
$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');
$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);
$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());
$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.');
$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.');
$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);
$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());
}
public function testLanguageIndependentContentModeration() {
$this
->drupalGet('node/add/article');
$node = $this
->submitNodeForm('Test 1.1 EN', 'published');
$this
->assertNotLatestVersionPage($node);
$edit_path = $node
->toUrl('edit-form');
$translate_path = $node
->toUrl('drupal:content-translation-overview');
$this
->drupalGet($edit_path);
$this
->submitNodeForm('Test 1.2 EN', 'draft', TRUE);
$this
->assertLatestVersionPage($node);
$this
->drupalGet($translate_path);
$this
->clickLink('Add');
$this
->submitNodeForm('Test 1.3 FR', 'draft');
$fr_node = $this
->loadTranslation($node, 'fr');
$this
->assertLatestVersionPage($fr_node);
$this
->assertModerationForm($node);
$this
->drupalGet($translate_path);
$this
->clickLink('Add');
$this
->submitNodeForm('Test 1.4 IT', 'draft');
$it_node = $this
->loadTranslation($node, 'it');
$this
->assertLatestVersionPage($it_node);
$this
->assertModerationForm($node);
$this
->assertModerationForm($fr_node);
$this
->drupalGet($edit_path);
$this
->submitNodeForm('Test 1.5 EN', 'published', TRUE);
$this
->assertNotLatestVersionPage($node);
$this
->assertModerationForm($fr_node);
$this
->assertModerationForm($it_node);
$this
->drupalGet($translate_path);
$this
->clickLink('Edit', 3);
$this
->submitNodeForm('Test 1.6 IT', 'published');
$this
->assertNotLatestVersionPage($it_node);
$this
->assertNoModerationForm($node);
$this
->assertModerationForm($fr_node);
$this
->drupalGet($translate_path);
$this
->clickLink('Edit', 2);
$this
->submitNodeForm('Test 1.7 FR', 'published');
$this
->assertNotLatestVersionPage($fr_node);
$this
->assertNoModerationForm($node);
$this
->assertNoModerationForm($it_node);
$this
->drupalGet($translate_path);
$this
->clickLink('Edit', 3);
$this
->submitNodeForm('Test 1.8 IT', 'draft');
$this
->assertLatestVersionPage($it_node);
$this
->assertNoModerationForm($node);
$this
->assertNoModerationForm($fr_node);
$this
->drupalGet($translate_path);
$this
->clickLink('Edit', 2);
$this
->submitNodeForm('Test 1.9 FR', 'draft');
$this
->assertLatestVersionPage($fr_node);
$this
->assertNoModerationForm($node);
$this
->assertModerationForm($it_node);
$this
->drupalGet($edit_path);
$this
->submitNodeForm('Test 1.10 EN', 'draft');
$this
->assertLatestVersionPage($node);
$this
->assertModerationForm($fr_node);
$this
->assertModerationForm($it_node);
$this
->drupalGet('node/add/article');
$node2 = $this
->submitNodeForm('Test 2.1 EN', 'draft', TRUE);
$this
->assertNotLatestVersionPage($node2, TRUE);
$edit_path = $node2
->toUrl('edit-form');
$translate_path = $node2
->toUrl('drupal:content-translation-overview');
$this
->drupalGet($translate_path);
$this
->clickLink('Add');
$this
->submitNodeForm('Test 2.2 FR', 'draft');
$fr_node2 = $this
->loadTranslation($node2, 'fr');
$this
->assertNotLatestVersionPage($fr_node2, TRUE);
$this
->assertModerationForm($node2, FALSE);
$this
->drupalGet($translate_path);
$this
->clickLink('Add');
$this
->submitNodeForm('Test 2.3 IT', 'draft');
$it_node2 = $this
->loadTranslation($node2, 'it');
$this
->assertNotLatestVersionPage($it_node2, TRUE);
$this
->assertModerationForm($node2, FALSE);
$this
->assertModerationForm($fr_node2, FALSE);
$this
->drupalGet($edit_path);
$this
->submitNodeForm('Test 2.4 EN', 'published', TRUE);
$this
->assertNotLatestVersionPage($node2);
$this
->assertModerationForm($fr_node2, FALSE);
$this
->assertModerationForm($it_node2, FALSE);
$this
->drupalGet($translate_path);
$this
->clickLink('Edit', 3);
$this
->submitNodeForm('Test 2.5 IT', 'published');
$this
->assertNotLatestVersionPage($it_node2);
$this
->assertNoModerationForm($node2);
$this
->assertModerationForm($fr_node2, FALSE);
$this
->drupalGet($translate_path);
$this
->clickLink('Edit', 2);
$this
->submitNodeForm('Test 2.6 FR', 'published');
$this
->assertNotLatestVersionPage($fr_node2);
$this
->assertNoModerationForm($node2);
$this
->assertNoModerationForm($it_node2);
$storage = $this->container
->get('entity_type.manager')
->getStorage('node');
foreach (range(11, 16) as $revision_id) {
$revision = $storage
->loadRevision($revision_id);
foreach ($revision
->getTranslationLanguages() as $langcode => $language) {
if ($revision
->isRevisionTranslationAffected()) {
$this
->drupalGet($revision
->toUrl('revision'));
$this
->assertFalse($this
->hasModerationForm(), 'Moderation form is not displayed correctly for revision ' . $revision_id);
break;
}
}
}
$this
->drupalGet($translate_path);
$this
->clickLink('Edit', 3);
$this
->submitNodeForm('Test 2.7 IT', 'draft');
$this
->assertLatestVersionPage($it_node2);
$this
->assertNoModerationForm($node2);
$this
->assertNoModerationForm($fr_node2);
$this
->drupalGet($translate_path);
$this
->clickLink('Edit', 2);
$this
->submitNodeForm('Test 2.8 FR', 'draft');
$this
->assertLatestVersionPage($fr_node2);
$this
->assertNoModerationForm($node2);
$this
->assertModerationForm($it_node2);
$this
->drupalGet($edit_path);
$this
->submitNodeForm('Test 2.9 EN', 'draft', TRUE);
$this
->assertLatestVersionPage($node2);
$this
->assertModerationForm($fr_node2);
$this
->assertModerationForm($it_node2);
$this
->drupalGet('node/add/article');
$node3 = $this
->submitNodeForm('Test 3.1 EN', 'published');
$this
->assertNotLatestVersionPage($node3);
$edit_path = $node3
->toUrl('edit-form');
$translate_path = $node3
->toUrl('drupal:content-translation-overview');
$this
->drupalGet($edit_path);
$this
->submitNodeForm('Test 3.2 EN', 'draft', TRUE);
$this
->assertLatestVersionPage($node3);
$this
->drupalGet($translate_path);
$this
->clickLink('Add');
$this
->submitNodeForm('Test 3.3 FR', 'draft');
$fr_node3 = $this
->loadTranslation($node3, 'fr');
$this
->assertLatestVersionPage($fr_node3);
$this
->assertModerationForm($node3);
$this
->drupalGet($translate_path);
$this
->clickLink('Edit', 2);
$this
->submitNodeForm('Test 3.4 FR', 'published');
$this
->assertNotLatestVersionPage($fr_node3);
$this
->assertModerationForm($node3);
}
public function testNewTranslationSourceValues() {
$this
->drupalGet('node/add/article');
$node = $this
->submitNodeForm('Test 1.1 IT', 'published', TRUE, 'it');
$this
->assertNotLatestVersionPage($node);
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->submitNodeForm('Test 1.2 IT', 'draft', TRUE);
$this
->assertLatestVersionPage($node);
$url = $node
->toUrl('drupal:content-translation-add');
$url
->setRouteParameter('source', 'it');
$url
->setRouteParameter('target', 'en');
$this
->drupalGet($url);
$this
->assertSession()
->pageTextContains('Test 1.2 IT');
$this
->submitNodeForm('Test 1.3 EN', 'draft');
$this
->assertLatestVersionPage($node);
$url
->setRouteParameter('target', 'fr');
$this
->drupalGet($url);
$this
->assertSession()
->pageTextContains('Test 1.2 IT');
$url
->setRouteParameter('source', 'en');
$this
->drupalGet($url);
$this
->assertSession()
->pageTextContains('Test 1.3 EN');
}
protected function submitNodeForm($title, $moderation_state, $default_translation = FALSE, $langcode = 'en') {
$is_new = strpos($this
->getSession()
->getCurrentUrl(), '/node/add/') !== FALSE;
$edit = [
'title[0][value]' => $title,
'moderation_state[0][state]' => $moderation_state,
];
if ($is_new) {
$default_translation = TRUE;
$edit['langcode[0][value]'] = $langcode;
}
$submit = $default_translation ? 'Save' : 'Save (this translation)';
$this
->submitForm($edit, $submit);
$message = $is_new ? "Article {$title} has been created." : "Article {$title} has been updated.";
$this
->assertSession()
->pageTextContains($message);
return $is_new ? $this
->drupalGetNodeByTitle($title) : NULL;
}
protected function loadTranslation(NodeInterface $node, $langcode) {
$storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$storage
->resetCache([
$node
->id(),
]);
$node = $storage
->loadRevision($storage
->getLatestRevisionId($node
->id()));
return $node
->getTranslation($langcode);
}
public function assertLatestVersionPage(NodeInterface $node) : void {
$this
->assertEquals($node
->toUrl('latest-version')
->setAbsolute()
->toString(), $this
->getSession()
->getCurrentUrl());
$this
->assertModerationForm($node);
}
public function assertNotLatestVersionPage(NodeInterface $node, bool $moderation_form = FALSE) : void {
$this
->assertNotEquals($node
->toUrl('latest-version')
->setAbsolute()
->toString(), $this
->getSession()
->getCurrentUrl());
if ($moderation_form) {
$this
->assertModerationForm($node, FALSE);
}
else {
$this
->assertNoModerationForm($node);
}
}
public function assertModerationForm(NodeInterface $node, bool $latest_tab = TRUE) : void {
$this
->drupalGet($node
->toUrl());
$this
->assertEquals(!$latest_tab, $this
->hasModerationForm());
$this
->drupalGet($node
->toUrl('latest-version'));
$this
->assertEquals($latest_tab, $this
->hasModerationForm());
}
public function assertNoModerationForm(NodeInterface $node) : void {
$this
->drupalGet($node
->toUrl());
$this
->assertFalse($this
->hasModerationForm());
$this
->drupalGet($node
->toUrl('latest-version'));
$this
->assertEquals(403, $this
->getSession()
->getStatusCode());
}
public function hasModerationForm() {
return (bool) $this
->xpath('//ul[@class="entity-moderation-form"]');
}
}