X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fcontent_moderation%2Ftests%2Fsrc%2FKernel%2FEntityStateChangeValidationTest.php;fp=web%2Fcore%2Fmodules%2Fcontent_moderation%2Ftests%2Fsrc%2FKernel%2FEntityStateChangeValidationTest.php;h=2fefc0148c56a1b483a84c52e64c6f9513e5f522;hp=7c4f97d391e7a541350d880e94fad0b02d2a1ff3;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hpb=aea91e65e895364e460983b890e295aa5d5540a5
diff --git a/web/core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php b/web/core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php
index 7c4f97d39..2fefc0148 100644
--- a/web/core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php
+++ b/web/core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php
@@ -96,6 +96,124 @@ class EntityStateChangeValidationTest extends KernelTestBase {
$this->assertEquals('Invalid state transition from Draft to Archived', $violations->get(0)->getMessage());
}
+ /**
+ * Test validation with an invalid state.
+ */
+ public function testInvalidState() {
+ $node_type = NodeType::create([
+ 'type' => 'example',
+ ]);
+ $node_type->save();
+ $workflow = Workflow::load('editorial');
+ $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
+ $workflow->save();
+
+ $node = Node::create([
+ 'type' => 'example',
+ 'title' => 'Test title',
+ ]);
+ $node->moderation_state->value = 'invalid_state';
+ $violations = $node->validate();
+
+ $this->assertCount(1, $violations);
+ $this->assertEquals('State invalid_state does not exist on Editorial workflow', $violations->get(0)->getMessage());
+ }
+
+ /**
+ * Test validation with content that has no initial state or an invalid state.
+ */
+ public function testInvalidStateWithoutExisting() {
+ // Create content without moderation enabled for the content type.
+ $node_type = NodeType::create([
+ 'type' => 'example',
+ ]);
+ $node_type->save();
+ $node = Node::create([
+ 'type' => 'example',
+ 'title' => 'Test title',
+ ]);
+ $node->save();
+
+ // Enable moderation to test validation on existing content, with no
+ // explicit state.
+ $workflow = Workflow::load('editorial');
+ $workflow->getTypePlugin()->addState('deleted_state', 'Deleted state');
+ $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
+ $workflow->save();
+
+ // Validate the invalid state.
+ $node->moderation_state->value = 'invalid_state';
+ $violations = $node->validate();
+ $this->assertCount(1, $violations);
+
+ // Assign the node to a state we're going to delete.
+ $node->moderation_state->value = 'deleted_state';
+ $node->save();
+
+ // Delete the state so $node->original contains an invalid state when
+ // validating.
+ $workflow->getTypePlugin()->deleteState('deleted_state');
+ $workflow->save();
+ $node->moderation_state->value = 'draft';
+ $violations = $node->validate();
+ $this->assertCount(0, $violations);
+ }
+
+ /**
+ * Test state transition validation with multiple languages.
+ */
+ public function testInvalidStateMultilingual() {
+ ConfigurableLanguage::createFromLangcode('fr')->save();
+ $node_type = NodeType::create([
+ 'type' => 'example',
+ ]);
+ $node_type->save();
+
+ $workflow = Workflow::load('editorial');
+ $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
+ $workflow->save();
+
+ $node = Node::create([
+ 'type' => 'example',
+ 'title' => 'English Published Node',
+ 'langcode' => 'en',
+ 'moderation_state' => 'published',
+ ]);
+ $node->save();
+
+ $node_fr = $node->addTranslation('fr');
+ $node_fr->setTitle('French Published Node');
+ $node_fr->save();
+ $this->assertEquals('published', $node_fr->moderation_state->value);
+
+ // Create a pending revision of the original node.
+ $node->moderation_state = 'draft';
+ $node->setNewRevision(TRUE);
+ $node->isDefaultRevision(FALSE);
+ $node->save();
+
+ // For the pending english revision, there should be a violation from draft
+ // to archived.
+ $node->moderation_state = 'archived';
+ $violations = $node->validate();
+ $this->assertCount(1, $violations);
+ $this->assertEquals('Invalid state transition from Draft to Archived', $violations->get(0)->getMessage());
+
+ // From the default french published revision, there should be none.
+ $node_fr = Node::load($node->id())->getTranslation('fr');
+ $this->assertEquals('published', $node_fr->moderation_state->value);
+ $node_fr->moderation_state = 'archived';
+ $violations = $node_fr->validate();
+ $this->assertCount(0, $violations);
+
+ // From the latest french revision, there should also be no violation.
+ $node_fr = $node->getTranslation('fr');
+ $this->assertEquals('published', $node_fr->moderation_state->value);
+ $node_fr->moderation_state = 'archived';
+ $violations = $node_fr->validate();
+ $this->assertCount(0, $violations);
+ }
+
/**
* Tests that content without prior moderation information can be moderated.
*/