Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / content_moderation / tests / src / Kernel / ModerationStateFieldItemListTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Kernel;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\node\Entity\Node;
7 use Drupal\node\Entity\NodeType;
8 use Drupal\workflows\Entity\Workflow;
9
10 /**
11  * @coversDefaultClass \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList
12  *
13  * @group content_moderation
14  */
15 class ModerationStateFieldItemListTest extends KernelTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = [
21     'node',
22     'content_moderation',
23     'user',
24     'system',
25     'language',
26     'workflows',
27   ];
28
29   /**
30    * @var \Drupal\node\NodeInterface
31    */
32   protected $testNode;
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp() {
38     parent::setUp();
39
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');
45
46     NodeType::create([
47       'type' => 'unmoderated',
48     ])->save();
49
50     $node_type = NodeType::create([
51       'type' => 'example',
52     ]);
53     $node_type->save();
54     $workflow = Workflow::load('editorial');
55     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
56     $workflow->save();
57
58     $this->testNode = Node::create([
59       'type' => 'example',
60       'title' => 'Test title',
61     ]);
62     $this->testNode->save();
63     \Drupal::entityTypeManager()->getStorage('node')->resetCache();
64     $this->testNode = Node::load($this->testNode->id());
65   }
66
67   /**
68    * Test the field item list when accessing an index.
69    */
70   public function testArrayIndex() {
71     $this->assertFalse($this->testNode->isPublished());
72     $this->assertEquals('draft', $this->testNode->moderation_state[0]->value);
73   }
74
75   /**
76    * Test the field item list when iterating.
77    */
78   public function testArrayIteration() {
79     $states = [];
80     foreach ($this->testNode->moderation_state as $item) {
81       $states[] = $item->value;
82     }
83     $this->assertEquals(['draft'], $states);
84   }
85
86   /**
87    * @covers ::getValue
88    */
89   public function testGetValue() {
90     $this->assertEquals([['value' => 'draft']], $this->testNode->moderation_state->getValue());
91   }
92
93   /**
94    * @covers ::get
95    */
96   public function testGet() {
97     $this->assertEquals('draft', $this->testNode->moderation_state->get(0)->value);
98     $this->setExpectedException(\InvalidArgumentException::class);
99     $this->testNode->moderation_state->get(2);
100   }
101
102   /**
103    * Tests the computed field when it is unset or set to an empty value.
104    */
105   public function testSetEmptyState() {
106     $this->testNode->moderation_state->value = '';
107     $this->assertEquals('draft', $this->testNode->moderation_state->value);
108
109     $this->testNode->moderation_state = '';
110     $this->assertEquals('draft', $this->testNode->moderation_state->value);
111
112     unset($this->testNode->moderation_state);
113     $this->assertEquals('draft', $this->testNode->moderation_state->value);
114   }
115
116   /**
117    * Test the list class with a non moderated entity.
118    */
119   public function testNonModeratedEntity() {
120     $unmoderated_node = Node::create([
121       'type' => 'unmoderated',
122       'title' => 'Test title',
123     ]);
124     $unmoderated_node->save();
125     $this->assertEquals(0, $unmoderated_node->moderation_state->count());
126
127     $unmoderated_node->moderation_state = NULL;
128     $this->assertEquals(0, $unmoderated_node->moderation_state->count());
129   }
130
131   /**
132    * Tests that moderation state changes also change the related entity state.
133    */
134   public function testModerationStateChanges() {
135     // Change the moderation state and check that the entity's
136     // 'isDefaultRevision' flag and the publishing status have also been
137     // updated.
138     $this->testNode->moderation_state->value = 'published';
139
140     $this->assertTrue($this->testNode->isPublished());
141     $this->assertTrue($this->testNode->isDefaultRevision());
142
143     $this->testNode->save();
144
145     // Repeat the checks using an 'unpublished' state.
146     $this->testNode->moderation_state->value = 'draft';
147     $this->assertFalse($this->testNode->isPublished());
148     $this->assertFalse($this->testNode->isDefaultRevision());
149   }
150
151   /**
152    * Test updating the state for an entity without a workflow.
153    */
154   public function testEntityWithNoWorkflow() {
155     $node_type = NodeType::create([
156       'type' => 'example_no_workflow',
157     ]);
158     $node_type->save();
159     $test_node = Node::create([
160       'type' => 'example_no_workflow',
161       'title' => 'Test node with no workflow',
162     ]);
163     $test_node->save();
164
165     /** @var \Drupal\content_moderation\ModerationInformationInterface $content_moderation_info */
166     $content_moderation_info = \Drupal::service('content_moderation.moderation_information');
167     $workflow = $content_moderation_info->getWorkflowForEntity($test_node);
168     $this->assertNull($workflow);
169
170     $this->assertTrue($test_node->isPublished());
171     $test_node->moderation_state->setValue('draft');
172     // The entity is still published because there is not a workflow.
173     $this->assertTrue($test_node->isPublished());
174   }
175
176   /**
177    * Test the moderation_state field after an entity has been serialized.
178    */
179   public function testEntityUnserialize() {
180     $this->testNode->moderation_state->value = 'draft';
181     $unserialized = unserialize(serialize($this->testNode));
182
183     $this->assertEquals('Test title', $unserialized->title->value);
184     $this->assertEquals('draft', $unserialized->moderation_state->value);
185   }
186
187 }