Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / content_moderation / tests / src / Kernel / ModerationStateFieldItemListTest.php
index c4770166408cc93644ed4719fc8776503e0797e2..7b57bd704913e4e1c55b343c4ea251b328fb7fbb 100644 (file)
@@ -5,7 +5,7 @@ namespace Drupal\Tests\content_moderation\Kernel;
 use Drupal\KernelTests\KernelTestBase;
 use Drupal\node\Entity\Node;
 use Drupal\node\Entity\NodeType;
-use Drupal\workflows\Entity\Workflow;
+use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
 
 /**
  * @coversDefaultClass \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList
@@ -14,6 +14,8 @@ use Drupal\workflows\Entity\Workflow;
  */
 class ModerationStateFieldItemListTest extends KernelTestBase {
 
+  use ContentModerationTestTrait;
+
   /**
    * {@inheritdoc}
    */
@@ -51,7 +53,7 @@ class ModerationStateFieldItemListTest extends KernelTestBase {
       'type' => 'example',
     ]);
     $node_type->save();
-    $workflow = Workflow::load('editorial');
+    $workflow = $this->createEditorialWorkflow();
     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
     $workflow->save();
 
@@ -100,17 +102,70 @@ class ModerationStateFieldItemListTest extends KernelTestBase {
   }
 
   /**
-   * Tests the computed field when it is unset or set to an empty value.
+   * Tests the item list when it is emptied and appended to.
+   */
+  public function testEmptyStateAndAppend() {
+    // This test case mimics the lifecycle of an entity that is being patched in
+    // a rest resource.
+    $this->testNode->moderation_state->setValue([]);
+    $this->assertTrue($this->testNode->moderation_state->isEmpty());
+    $this->assertEmptiedModerationFieldItemList();
+
+    $this->testNode->moderation_state->appendItem();
+    $this->assertEquals(1, $this->testNode->moderation_state->count());
+    $this->assertEquals(NULL, $this->testNode->moderation_state->value);
+    $this->assertEmptiedModerationFieldItemList();
+  }
+
+  /**
+   * Test an empty value assigned to the field item.
    */
-  public function testSetEmptyState() {
+  public function testEmptyFieldItem() {
     $this->testNode->moderation_state->value = '';
-    $this->assertEquals('draft', $this->testNode->moderation_state->value);
+    $this->assertEquals('', $this->testNode->moderation_state->value);
+    $this->assertEmptiedModerationFieldItemList();
+  }
 
+  /**
+   * Test an empty value assigned to the field item list.
+   */
+  public function testEmptyFieldItemList() {
     $this->testNode->moderation_state = '';
-    $this->assertEquals('draft', $this->testNode->moderation_state->value);
+    $this->assertEquals('', $this->testNode->moderation_state->value);
+    $this->assertEmptiedModerationFieldItemList();
+  }
 
+  /**
+   * Test the field item when it is unset.
+   */
+  public function testUnsetItemList() {
     unset($this->testNode->moderation_state);
-    $this->assertEquals('draft', $this->testNode->moderation_state->value);
+    $this->assertEquals(NULL, $this->testNode->moderation_state->value);
+    $this->assertEmptiedModerationFieldItemList();
+  }
+
+  /**
+   * Test the field item when it is assigned NULL.
+   */
+  public function testAssignNullItemList() {
+    $this->testNode->moderation_state = NULL;
+    $this->assertEquals(NULL, $this->testNode->moderation_state->value);
+    $this->assertEmptiedModerationFieldItemList();
+  }
+
+  /**
+   * Assert the set of expectations when the moderation state field is emptied.
+   */
+  protected function assertEmptiedModerationFieldItemList() {
+    $this->assertTrue($this->testNode->moderation_state->isEmpty());
+    // Test the empty value causes a violation in the entity.
+    $violations = $this->testNode->validate();
+    $this->assertCount(1, $violations);
+    $this->assertEquals('This value should not be null.', $violations->get(0)->getMessage());
+    // Test that incorrectly saving the entity regardless will not produce a
+    // change in the moderation state.
+    $this->testNode->save();
+    $this->assertEquals('draft', Node::load($this->testNode->id())->moderation_state->value);
   }
 
   /**
@@ -126,26 +181,63 @@ class ModerationStateFieldItemListTest extends KernelTestBase {
 
     $unmoderated_node->moderation_state = NULL;
     $this->assertEquals(0, $unmoderated_node->moderation_state->count());
+    $this->assertCount(0, $unmoderated_node->validate());
   }
 
   /**
    * Tests that moderation state changes also change the related entity state.
+   *
+   * @dataProvider moderationStateChangesTestCases
    */
-  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());
-
+  public function testModerationStateChanges($initial_state, $final_state, $first_published, $first_is_default, $second_published, $second_is_default) {
+    $this->testNode->moderation_state->value = $initial_state;
+    $this->assertEquals($first_published, $this->testNode->isPublished());
+    $this->assertEquals($first_is_default, $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());
+    $this->testNode->moderation_state->value = $final_state;
+    $this->assertEquals($second_published, $this->testNode->isPublished());
+    $this->assertEquals($second_is_default, $this->testNode->isDefaultRevision());
+  }
+
+  /**
+   * Data provider for ::testModerationStateChanges
+   */
+  public function moderationStateChangesTestCases() {
+    return [
+      'Draft to draft' => [
+        'draft',
+        'draft',
+        FALSE,
+        TRUE,
+        FALSE,
+        TRUE,
+      ],
+      'Draft to published' => [
+        'draft',
+        'published',
+        FALSE,
+        TRUE,
+        TRUE,
+        TRUE,
+      ],
+      'Published to published' => [
+        'published',
+        'published',
+        TRUE,
+        TRUE,
+        TRUE,
+        TRUE,
+      ],
+      'Published to draft' => [
+        'published',
+        'draft',
+        TRUE,
+        TRUE,
+        FALSE,
+        FALSE,
+      ],
+    ];
   }
 
   /**
@@ -175,13 +267,69 @@ class ModerationStateFieldItemListTest extends KernelTestBase {
 
   /**
    * Test the moderation_state field after an entity has been serialized.
+   *
+   * @dataProvider entityUnserializeTestCases
    */
-  public function testEntityUnserialize() {
-    $this->testNode->moderation_state->value = 'draft';
+  public function testEntityUnserialize($state, $default, $published) {
+    $this->testNode->moderation_state->value = $state;
+
+    $this->assertEquals($state, $this->testNode->moderation_state->value);
+    $this->assertEquals($default, $this->testNode->isDefaultRevision());
+    $this->assertEquals($published, $this->testNode->isPublished());
+
     $unserialized = unserialize(serialize($this->testNode));
 
-    $this->assertEquals('Test title', $unserialized->title->value);
-    $this->assertEquals('draft', $unserialized->moderation_state->value);
+    $this->assertEquals($state, $unserialized->moderation_state->value);
+    $this->assertEquals($default, $unserialized->isDefaultRevision());
+    $this->assertEquals($published, $unserialized->isPublished());
+  }
+
+  /**
+   * Test cases for ::testEntityUnserialize.
+   */
+  public function entityUnserializeTestCases() {
+    return [
+      'Default draft state' => [
+        'draft',
+        TRUE,
+        FALSE,
+      ],
+      'Non-default published state' => [
+        'published',
+        TRUE,
+        TRUE,
+      ],
+    ];
+  }
+
+  /**
+   * 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',
+      ],
+    ];
   }
 
 }