X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fcontent_moderation%2Ftests%2Fsrc%2FKernel%2FModerationStateFieldItemListTest.php;h=c4770166408cc93644ed4719fc8776503e0797e2;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hp=7e9a75c3f412e0d78dcf3afa062472edcf9dae68;hpb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;p=yaffs-website diff --git a/web/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php b/web/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php index 7e9a75c3f..c47701664 100644 --- a/web/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php +++ b/web/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php @@ -43,6 +43,10 @@ class ModerationStateFieldItemListTest extends KernelTestBase { $this->installEntitySchema('content_moderation_state'); $this->installConfig('content_moderation'); + NodeType::create([ + 'type' => 'unmoderated', + ])->save(); + $node_type = NodeType::create([ 'type' => 'example', ]); @@ -64,7 +68,8 @@ class ModerationStateFieldItemListTest extends KernelTestBase { * Test the field item list when accessing an index. */ public function testArrayIndex() { - $this->assertEquals('published', $this->testNode->moderation_state[0]->value); + $this->assertFalse($this->testNode->isPublished()); + $this->assertEquals('draft', $this->testNode->moderation_state[0]->value); } /** @@ -75,7 +80,108 @@ class ModerationStateFieldItemListTest extends KernelTestBase { foreach ($this->testNode->moderation_state as $item) { $states[] = $item->value; } - $this->assertEquals(['published'], $states); + $this->assertEquals(['draft'], $states); + } + + /** + * @covers ::getValue + */ + public function testGetValue() { + $this->assertEquals([['value' => 'draft']], $this->testNode->moderation_state->getValue()); + } + + /** + * @covers ::get + */ + public function testGet() { + $this->assertEquals('draft', $this->testNode->moderation_state->get(0)->value); + $this->setExpectedException(\InvalidArgumentException::class); + $this->testNode->moderation_state->get(2); + } + + /** + * Tests the computed field when it is unset or set to an empty value. + */ + public function testSetEmptyState() { + $this->testNode->moderation_state->value = ''; + $this->assertEquals('draft', $this->testNode->moderation_state->value); + + $this->testNode->moderation_state = ''; + $this->assertEquals('draft', $this->testNode->moderation_state->value); + + unset($this->testNode->moderation_state); + $this->assertEquals('draft', $this->testNode->moderation_state->value); + } + + /** + * Test the list class with a non moderated entity. + */ + public function testNonModeratedEntity() { + $unmoderated_node = Node::create([ + 'type' => 'unmoderated', + 'title' => 'Test title', + ]); + $unmoderated_node->save(); + $this->assertEquals(0, $unmoderated_node->moderation_state->count()); + + $unmoderated_node->moderation_state = NULL; + $this->assertEquals(0, $unmoderated_node->moderation_state->count()); + } + + /** + * Tests that moderation state changes also change the related entity state. + */ + public function testModerationStateChanges() { + // Change the moderation state and check that the entity's + // 'isDefaultRevision' flag and the publishing status have also been + // updated. + $this->testNode->moderation_state->value = 'published'; + + $this->assertTrue($this->testNode->isPublished()); + $this->assertTrue($this->testNode->isDefaultRevision()); + + $this->testNode->save(); + + // Repeat the checks using an 'unpublished' state. + $this->testNode->moderation_state->value = 'draft'; + $this->assertFalse($this->testNode->isPublished()); + $this->assertFalse($this->testNode->isDefaultRevision()); + } + + /** + * Test updating the state for an entity without a workflow. + */ + public function testEntityWithNoWorkflow() { + $node_type = NodeType::create([ + 'type' => 'example_no_workflow', + ]); + $node_type->save(); + $test_node = Node::create([ + 'type' => 'example_no_workflow', + 'title' => 'Test node with no workflow', + ]); + $test_node->save(); + + /** @var \Drupal\content_moderation\ModerationInformationInterface $content_moderation_info */ + $content_moderation_info = \Drupal::service('content_moderation.moderation_information'); + $workflow = $content_moderation_info->getWorkflowForEntity($test_node); + $this->assertNull($workflow); + + $this->assertTrue($test_node->isPublished()); + $test_node->moderation_state->setValue('draft'); + // The entity is still published because there is not a workflow. + $this->assertTrue($test_node->isPublished()); + } + + /** + * Test the moderation_state field after an entity has been serialized. + */ + public function testEntityUnserialize() { + $this->testNode->moderation_state->value = 'draft'; + $unserialized = unserialize(serialize($this->testNode)); + + $this->assertEquals('Test title', $unserialized->title->value); + $this->assertEquals('draft', $unserialized->moderation_state->value); } }