summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Catchpole2018-06-26 15:07:19 (GMT)
committerNathaniel Catchpole2018-06-26 15:07:19 (GMT)
commit191bf5284943c14ccbb65cc2521f416f2d93e5e6 (patch)
treeeb3160679d89dc17b3b583648f5a7522e140b369
parent679fe1f111558238c1244891823536157d0f9323 (diff)
Issue #2848089 by Sam152, mpotter, gnuget, kriboogh, joelpittet: Unable to set a moderation_state value for unsaved entities that are assigned an ID
-rw-r--r--core/modules/content_moderation/src/ModerationInformation.php4
-rw-r--r--core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php30
2 files changed, 34 insertions, 0 deletions
diff --git a/core/modules/content_moderation/src/ModerationInformation.php b/core/modules/content_moderation/src/ModerationInformation.php
index 9f35046..286104f 100644
--- a/core/modules/content_moderation/src/ModerationInformation.php
+++ b/core/modules/content_moderation/src/ModerationInformation.php
@@ -175,6 +175,10 @@ class ModerationInformation implements ModerationInformationInterface {
public function isDefaultRevisionPublished(ContentEntityInterface $entity) {
$workflow = $this->getWorkflowForEntity($entity);
$default_revision = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->load($entity->id());
+ // If no default revision could be loaded, the entity has not yet been
+ // saved. In this case the moderation_state of the unsaved entity can be
+ // used, since once saved it will become the default.
+ $default_revision = $default_revision ?: $entity;
// Ensure we are checking all translations of the default revision.
if ($default_revision instanceof TranslatableInterface && $default_revision->isTranslatable()) {
diff --git a/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php b/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php
index 5580630..af0bb4f 100644
--- a/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php
+++ b/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php
@@ -251,4 +251,34 @@ class ModerationStateFieldItemListTest extends KernelTestBase {
];
}
+ /**
+ * Test saving a moderated node with an existing ID.
+ *
+ * @dataProvider moderatedEntityWithExistingIdTestCases
+ */
+ public function testModeratedEntityWithExistingId($state) {
+ $node = Node::create([
+ 'title' => 'Test title',
+ 'type' => 'example',
+ 'nid' => 999,
+ 'moderation_state' => $state,
+ ]);
+ $node->save();
+ $this->assertEquals($state, $node->moderation_state->value);
+ }
+
+ /**
+ * Test cases for ::testModeratedEntityWithExistingId.
+ */
+ public function moderatedEntityWithExistingIdTestCases() {
+ return [
+ 'Draft non-default state' => [
+ 'draft',
+ ],
+ 'Published default state' => [
+ 'published',
+ ],
+ ];
+ }
+
}