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
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\Tests\content_moderation\Traits\ContentModerationTestTrait;
9
10 /**
11  * @coversDefaultClass \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList
12  *
13  * @group content_moderation
14  */
15 class ModerationStateFieldItemListTest extends KernelTestBase {
16
17   use ContentModerationTestTrait;
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = [
23     'node',
24     'content_moderation',
25     'user',
26     'system',
27     'language',
28     'workflows',
29   ];
30
31   /**
32    * @var \Drupal\node\NodeInterface
33    */
34   protected $testNode;
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUp() {
40     parent::setUp();
41
42     $this->installSchema('node', 'node_access');
43     $this->installEntitySchema('node');
44     $this->installEntitySchema('user');
45     $this->installEntitySchema('content_moderation_state');
46     $this->installConfig('content_moderation');
47
48     NodeType::create([
49       'type' => 'unmoderated',
50     ])->save();
51
52     $node_type = NodeType::create([
53       'type' => 'example',
54     ]);
55     $node_type->save();
56     $workflow = $this->createEditorialWorkflow();
57     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
58     $workflow->save();
59
60     $this->testNode = Node::create([
61       'type' => 'example',
62       'title' => 'Test title',
63     ]);
64     $this->testNode->save();
65     \Drupal::entityTypeManager()->getStorage('node')->resetCache();
66     $this->testNode = Node::load($this->testNode->id());
67   }
68
69   /**
70    * Test the field item list when accessing an index.
71    */
72   public function testArrayIndex() {
73     $this->assertFalse($this->testNode->isPublished());
74     $this->assertEquals('draft', $this->testNode->moderation_state[0]->value);
75   }
76
77   /**
78    * Test the field item list when iterating.
79    */
80   public function testArrayIteration() {
81     $states = [];
82     foreach ($this->testNode->moderation_state as $item) {
83       $states[] = $item->value;
84     }
85     $this->assertEquals(['draft'], $states);
86   }
87
88   /**
89    * @covers ::getValue
90    */
91   public function testGetValue() {
92     $this->assertEquals([['value' => 'draft']], $this->testNode->moderation_state->getValue());
93   }
94
95   /**
96    * @covers ::get
97    */
98   public function testGet() {
99     $this->assertEquals('draft', $this->testNode->moderation_state->get(0)->value);
100     $this->setExpectedException(\InvalidArgumentException::class);
101     $this->testNode->moderation_state->get(2);
102   }
103
104   /**
105    * Tests the item list when it is emptied and appended to.
106    */
107   public function testEmptyStateAndAppend() {
108     // This test case mimics the lifecycle of an entity that is being patched in
109     // a rest resource.
110     $this->testNode->moderation_state->setValue([]);
111     $this->assertTrue($this->testNode->moderation_state->isEmpty());
112     $this->assertEmptiedModerationFieldItemList();
113
114     $this->testNode->moderation_state->appendItem();
115     $this->assertEquals(1, $this->testNode->moderation_state->count());
116     $this->assertEquals(NULL, $this->testNode->moderation_state->value);
117     $this->assertEmptiedModerationFieldItemList();
118   }
119
120   /**
121    * Test an empty value assigned to the field item.
122    */
123   public function testEmptyFieldItem() {
124     $this->testNode->moderation_state->value = '';
125     $this->assertEquals('', $this->testNode->moderation_state->value);
126     $this->assertEmptiedModerationFieldItemList();
127   }
128
129   /**
130    * Test an empty value assigned to the field item list.
131    */
132   public function testEmptyFieldItemList() {
133     $this->testNode->moderation_state = '';
134     $this->assertEquals('', $this->testNode->moderation_state->value);
135     $this->assertEmptiedModerationFieldItemList();
136   }
137
138   /**
139    * Test the field item when it is unset.
140    */
141   public function testUnsetItemList() {
142     unset($this->testNode->moderation_state);
143     $this->assertEquals(NULL, $this->testNode->moderation_state->value);
144     $this->assertEmptiedModerationFieldItemList();
145   }
146
147   /**
148    * Test the field item when it is assigned NULL.
149    */
150   public function testAssignNullItemList() {
151     $this->testNode->moderation_state = NULL;
152     $this->assertEquals(NULL, $this->testNode->moderation_state->value);
153     $this->assertEmptiedModerationFieldItemList();
154   }
155
156   /**
157    * Assert the set of expectations when the moderation state field is emptied.
158    */
159   protected function assertEmptiedModerationFieldItemList() {
160     $this->assertTrue($this->testNode->moderation_state->isEmpty());
161     // Test the empty value causes a violation in the entity.
162     $violations = $this->testNode->validate();
163     $this->assertCount(1, $violations);
164     $this->assertEquals('This value should not be null.', $violations->get(0)->getMessage());
165     // Test that incorrectly saving the entity regardless will not produce a
166     // change in the moderation state.
167     $this->testNode->save();
168     $this->assertEquals('draft', Node::load($this->testNode->id())->moderation_state->value);
169   }
170
171   /**
172    * Test the list class with a non moderated entity.
173    */
174   public function testNonModeratedEntity() {
175     $unmoderated_node = Node::create([
176       'type' => 'unmoderated',
177       'title' => 'Test title',
178     ]);
179     $unmoderated_node->save();
180     $this->assertEquals(0, $unmoderated_node->moderation_state->count());
181
182     $unmoderated_node->moderation_state = NULL;
183     $this->assertEquals(0, $unmoderated_node->moderation_state->count());
184     $this->assertCount(0, $unmoderated_node->validate());
185   }
186
187   /**
188    * Tests that moderation state changes also change the related entity state.
189    *
190    * @dataProvider moderationStateChangesTestCases
191    */
192   public function testModerationStateChanges($initial_state, $final_state, $first_published, $first_is_default, $second_published, $second_is_default) {
193     $this->testNode->moderation_state->value = $initial_state;
194     $this->assertEquals($first_published, $this->testNode->isPublished());
195     $this->assertEquals($first_is_default, $this->testNode->isDefaultRevision());
196     $this->testNode->save();
197
198     $this->testNode->moderation_state->value = $final_state;
199     $this->assertEquals($second_published, $this->testNode->isPublished());
200     $this->assertEquals($second_is_default, $this->testNode->isDefaultRevision());
201   }
202
203   /**
204    * Data provider for ::testModerationStateChanges
205    */
206   public function moderationStateChangesTestCases() {
207     return [
208       'Draft to draft' => [
209         'draft',
210         'draft',
211         FALSE,
212         TRUE,
213         FALSE,
214         TRUE,
215       ],
216       'Draft to published' => [
217         'draft',
218         'published',
219         FALSE,
220         TRUE,
221         TRUE,
222         TRUE,
223       ],
224       'Published to published' => [
225         'published',
226         'published',
227         TRUE,
228         TRUE,
229         TRUE,
230         TRUE,
231       ],
232       'Published to draft' => [
233         'published',
234         'draft',
235         TRUE,
236         TRUE,
237         FALSE,
238         FALSE,
239       ],
240     ];
241   }
242
243   /**
244    * Test updating the state for an entity without a workflow.
245    */
246   public function testEntityWithNoWorkflow() {
247     $node_type = NodeType::create([
248       'type' => 'example_no_workflow',
249     ]);
250     $node_type->save();
251     $test_node = Node::create([
252       'type' => 'example_no_workflow',
253       'title' => 'Test node with no workflow',
254     ]);
255     $test_node->save();
256
257     /** @var \Drupal\content_moderation\ModerationInformationInterface $content_moderation_info */
258     $content_moderation_info = \Drupal::service('content_moderation.moderation_information');
259     $workflow = $content_moderation_info->getWorkflowForEntity($test_node);
260     $this->assertNull($workflow);
261
262     $this->assertTrue($test_node->isPublished());
263     $test_node->moderation_state->setValue('draft');
264     // The entity is still published because there is not a workflow.
265     $this->assertTrue($test_node->isPublished());
266   }
267
268   /**
269    * Test the moderation_state field after an entity has been serialized.
270    *
271    * @dataProvider entityUnserializeTestCases
272    */
273   public function testEntityUnserialize($state, $default, $published) {
274     $this->testNode->moderation_state->value = $state;
275
276     $this->assertEquals($state, $this->testNode->moderation_state->value);
277     $this->assertEquals($default, $this->testNode->isDefaultRevision());
278     $this->assertEquals($published, $this->testNode->isPublished());
279
280     $unserialized = unserialize(serialize($this->testNode));
281
282     $this->assertEquals($state, $unserialized->moderation_state->value);
283     $this->assertEquals($default, $unserialized->isDefaultRevision());
284     $this->assertEquals($published, $unserialized->isPublished());
285   }
286
287   /**
288    * Test cases for ::testEntityUnserialize.
289    */
290   public function entityUnserializeTestCases() {
291     return [
292       'Default draft state' => [
293         'draft',
294         TRUE,
295         FALSE,
296       ],
297       'Non-default published state' => [
298         'published',
299         TRUE,
300         TRUE,
301       ],
302     ];
303   }
304
305   /**
306    * Test saving a moderated node with an existing ID.
307    *
308    * @dataProvider moderatedEntityWithExistingIdTestCases
309    */
310   public function testModeratedEntityWithExistingId($state) {
311     $node = Node::create([
312       'title' => 'Test title',
313       'type' => 'example',
314       'nid' => 999,
315       'moderation_state' => $state,
316     ]);
317     $node->save();
318     $this->assertEquals($state, $node->moderation_state->value);
319   }
320
321   /**
322    * Test cases for ::testModeratedEntityWithExistingId.
323    */
324   public function moderatedEntityWithExistingIdTestCases() {
325     return [
326       'Draft non-default state' => [
327         'draft',
328       ],
329       'Published default state' => [
330         'published',
331       ],
332     ];
333   }
334
335 }