3 namespace Drupal\Tests\content_moderation\Kernel;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\node\Entity\Node;
7 use Drupal\node\Entity\NodeType;
8 use Drupal\workflows\Entity\Workflow;
11 * @coversDefaultClass \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList
13 * @group content_moderation
15 class ModerationStateFieldItemListTest extends KernelTestBase {
20 public static $modules = [
30 * @var \Drupal\node\NodeInterface
37 protected function setUp() {
40 $this->installSchema('node', 'node_access');
41 $this->installEntitySchema('node');
42 $this->installEntitySchema('user');
43 $this->installEntitySchema('content_moderation_state');
44 $this->installConfig('content_moderation');
46 $node_type = NodeType::create([
50 $workflow = Workflow::load('editorial');
51 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
54 $this->testNode = Node::create([
56 'title' => 'Test title',
58 $this->testNode->save();
59 \Drupal::entityTypeManager()->getStorage('node')->resetCache();
60 $this->testNode = Node::load($this->testNode->id());
64 * Test the field item list when accessing an index.
66 public function testArrayIndex() {
67 $this->assertFalse($this->testNode->isPublished());
68 $this->assertEquals('draft', $this->testNode->moderation_state[0]->value);
72 * Test the field item list when iterating.
74 public function testArrayIteration() {
76 foreach ($this->testNode->moderation_state as $item) {
77 $states[] = $item->value;
79 $this->assertEquals(['draft'], $states);
83 * Tests that moderation state changes also change the related entity state.
85 public function testModerationStateChanges() {
86 // Change the moderation state and check that the entity's
87 // 'isDefaultRevision' flag and the publishing status have also been
89 $this->testNode->moderation_state->value = 'published';
91 $this->assertTrue($this->testNode->isPublished());
92 $this->assertTrue($this->testNode->isDefaultRevision());
94 $this->testNode->save();
96 // Repeat the checks using an 'unpublished' state.
97 $this->testNode->moderation_state->value = 'draft';
98 $this->assertFalse($this->testNode->isPublished());
99 $this->assertFalse($this->testNode->isDefaultRevision());
103 * Test updating the state for an entity without a workflow.
105 public function testEntityWithNoWorkflow() {
106 $node_type = NodeType::create([
107 'type' => 'example_no_workflow',
110 $test_node = Node::create([
111 'type' => 'example_no_workflow',
112 'title' => 'Test node with no workflow',
116 /** @var \Drupal\content_moderation\ModerationInformationInterface $content_moderation_info */
117 $content_moderation_info = \Drupal::service('content_moderation.moderation_information');
118 $workflow = $content_moderation_info->getWorkflowForEntity($test_node);
119 $this->assertNull($workflow);
121 $this->assertTrue($test_node->isPublished());
122 $test_node->moderation_state->setValue('draft');
123 // The entity is still published because there is not a workflow.
124 $this->assertTrue($test_node->isPublished());