Unverified Commit 46ad14aa authored by alexpott's avatar alexpott

Issue #3000573 by Sam152, yongt9412, scott_euser, idflood, Berdir, amateescu:...

Issue #3000573 by Sam152, yongt9412, scott_euser, idflood, Berdir, amateescu: After enabling Workflows and Content Moderation there is a fatal error when trying to edit content translation
parent 36cf1a23
......@@ -93,7 +93,7 @@ protected function loadContentModerationStateRevision(ContentEntityInterface $en
if ($entity->getEntityType()->hasKey('langcode')) {
$langcode = $entity->language()->getId();
if (!$content_moderation_state->hasTranslation($langcode)) {
$content_moderation_state->addTranslation($langcode);
$content_moderation_state->addTranslation($langcode, $content_moderation_state->toArray());
}
if ($content_moderation_state->language()->getId() !== $langcode) {
$content_moderation_state = $content_moderation_state->getTranslation($langcode);
......
<?php
namespace Drupal\Tests\content_moderation\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
/**
* Test content_moderation functionality with content_translation.
*
* @group content_moderation
*/
class ModerationContentTranslationTest extends BrowserTestBase {
use ContentModerationTestTrait;
/**
* A user with permission to bypass access content.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $adminUser;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'node',
'locale',
'content_translation',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->rootUser);
// Create an Article content type.
$this->drupalCreateContentType(['type' => 'article', 'name' => 'Article'])->save();
$edit = [
'predefined_langcode' => 'fr',
];
$this->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language');
// Enable content translation on articles.
$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->drupalPostForm(NULL, $edit, 'Save configuration');
// Adding languages requires a container rebuild in the test running
// environment so that multilingual services are used.
$this->rebuildContainer();
}
/**
* Tests existing translations being edited after enabling content moderation.
*/
public function testModerationWithExistingContent() {
// Create a published article in English.
$edit = [
'title[0][value]' => 'Published English node',
'langcode[0][value]' => 'en',
];
$this->drupalPostForm('node/add/article', $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]' => 'Published French node',
];
$this->drupalPostForm(NULL, $edit, 'Save (this translation)');
$this->assertSession()->pageTextContains('Article Published French node has been updated.');
// Install content moderation and enable moderation on Article node type.
\Drupal::service('module_installer')->install(['content_moderation']);
$workflow = $this->createEditorialWorkflow();
$workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'article');
$workflow->save();
$this->drupalLogin($this->rootUser);
// Edit the English node.
$this->drupalGet('node/' . $english_node->id() . '/edit');
$this->assertSession()->statusCodeEquals(200);
$edit = [
'title[0][value]' => 'Published English new node',
];
$this->drupalPostForm(NULL, $edit, 'Save');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('Article Published English new node has been updated.');
// Edit the French translation.
$this->drupalGet('fr/node/' . $english_node->id() . '/edit');
$this->assertSession()->statusCodeEquals(200);
$edit = [
'title[0][value]' => 'Published French new node',
];
$this->drupalPostForm(NULL, $edit, 'Save (this translation)');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('Article Published French new node has been updated.');
}
}
......@@ -296,7 +296,7 @@ public function testMultilingualModeration() {
// Create a French translation.
$french_node = $english_node->addTranslation('fr', ['title' => 'French title']);
$french_node->setUnpublished();
// Revision 1 (fr).
// Revision 2 (fr).
$french_node->save();
$french_node = $this->reloadEntity($english_node)->getTranslation('fr');
$this->assertEquals('draft', $french_node->moderation_state->value);
......@@ -305,7 +305,7 @@ public function testMultilingualModeration() {
// Move English node to create another draft.
$english_node = $this->reloadEntity($english_node);
$english_node->moderation_state->value = 'draft';
// Revision 2 (en, fr).
// Revision 3 (en, fr).
$english_node->save();
$english_node = $this->reloadEntity($english_node);
$this->assertEquals('draft', $english_node->moderation_state->value);
......@@ -316,7 +316,7 @@ public function testMultilingualModeration() {
// Publish the French node.
$french_node->moderation_state->value = 'published';
// Revision 3 (en, fr).
// Revision 4 (en, fr).
$french_node->save();
$french_node = $this->reloadEntity($french_node)->getTranslation('fr');
$this->assertTrue($french_node->isPublished());
......@@ -327,7 +327,7 @@ public function testMultilingualModeration() {
// Publish the English node.
$english_node->moderation_state->value = 'published';
// Revision 4 (en, fr).
// Revision 5 (en, fr).
$english_node->save();
$english_node = $this->reloadEntity($english_node);
$this->assertTrue($english_node->isPublished());
......@@ -336,15 +336,15 @@ public function testMultilingualModeration() {
$french_node = $this->reloadEntity($english_node)->getTranslation('fr');
$this->assertTrue($french_node->isPublished());
$french_node->moderation_state->value = 'draft';
// Revision 5 (en, fr).
// Revision 6 (en, fr).
$french_node->save();
$french_node = $this->reloadEntity($english_node, 5)->getTranslation('fr');
$french_node = $this->reloadEntity($english_node, 6)->getTranslation('fr');
$this->assertFalse($french_node->isPublished());
$this->assertTrue($french_node->getTranslation('en')->isPublished());
// Republish the French node.
$french_node->moderation_state->value = 'published';
// Revision 6 (en, fr).
// Revision 7 (en, fr).
$french_node->save();
$french_node = $this->reloadEntity($english_node)->getTranslation('fr');
$this->assertTrue($french_node->isPublished());
......@@ -353,7 +353,7 @@ public function testMultilingualModeration() {
$content_moderation_state = ContentModerationState::load(1);
$content_moderation_state->set('moderation_state', 'draft');
$content_moderation_state->setNewRevision(TRUE);
// Revision 7 (en, fr).
// Revision 8 (en, fr).
$content_moderation_state->save();
$english_node = $this->reloadEntity($french_node, $french_node->getRevisionId() + 1);
......@@ -366,12 +366,12 @@ public function testMultilingualModeration() {
$content_moderation_state = $content_moderation_state->getTranslation('fr');
$content_moderation_state->set('moderation_state', 'draft');
$content_moderation_state->setNewRevision(TRUE);
// Revision 8 (en, fr).
// Revision 9 (en, fr).
$content_moderation_state->save();
$english_node = $this->reloadEntity($english_node, $english_node->getRevisionId());
$this->assertEquals('draft', $english_node->moderation_state->value);
$french_node = $this->reloadEntity($english_node, '8')->getTranslation('fr');
$french_node = $this->reloadEntity($english_node, '9')->getTranslation('fr');
$this->assertEquals('draft', $french_node->moderation_state->value);
// Switching the moderation state to an unpublished state should update the
// entity.
......@@ -380,7 +380,7 @@ public function testMultilingualModeration() {
// Get the default english node.
$english_node = $this->reloadEntity($english_node);
$this->assertTrue($english_node->isPublished());
$this->assertEquals(6, $english_node->getRevisionId());
$this->assertEquals(7, $english_node->getRevisionId());
}
/**
......
......@@ -3,9 +3,11 @@
namespace Drupal\Tests\content_moderation\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
use Drupal\workflows\Entity\Workflow;
/**
* @coversDefaultClass \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList
......@@ -64,6 +66,8 @@ protected function setUp() {
$this->testNode->save();
\Drupal::entityTypeManager()->getStorage('node')->resetCache();
$this->testNode = Node::load($this->testNode->id());
ConfigurableLanguage::createFromLangcode('de')->save();
}
/**
......@@ -332,4 +336,37 @@ public function moderatedEntityWithExistingIdTestCases() {
];
}
/**
* Test the field item list when used with existing unmoderated content.
*/
public function testWithExistingUnmoderatedContent() {
$node = Node::create([
'title' => 'Test title',
'type' => 'unmoderated',
]);
$node->save();
$translation = $node->addTranslation('de', $node->toArray());
$translation->title = 'Translated';
$translation->save();
$workflow = Workflow::load('editorial');
$workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'unmoderated');
$workflow->save();
// After enabling moderation, both the original node and translation should
// have a published moderation state.
$node = Node::load($node->id());
$translation = $node->getTranslation('de');
$this->assertEquals('published', $node->moderation_state->value);
$this->assertEquals('published', $translation->moderation_state->value);
// After the node has been updated, both the original node and translation
// should still have a value.
$node->title = 'Updated title';
$node->save();
$translation = $node->getTranslation('de');
$this->assertEquals('published', $node->moderation_state->value);
$this->assertEquals('published', $translation->moderation_state->value);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment