Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / content_moderation / tests / src / Kernel / ModerationStateFieldItemListTest.php
index 6281ab82bfa0f18603bb42a08e8647911a380963..c4770166408cc93644ed4719fc8776503e0797e2 100644 (file)
@@ -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',
     ]);
@@ -79,6 +83,51 @@ class ModerationStateFieldItemListTest extends KernelTestBase {
     $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.
    */
@@ -124,4 +173,15 @@ class ModerationStateFieldItemListTest extends KernelTestBase {
     $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);
+  }
+
 }