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');
47 'type' => 'unmoderated',
50 $node_type = NodeType::create([
54 $workflow = Workflow::load('editorial');
55 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
58 $this->testNode = Node::create([
60 'title' => 'Test title',
62 $this->testNode->save();
63 \Drupal::entityTypeManager()->getStorage('node')->resetCache();
64 $this->testNode = Node::load($this->testNode->id());
68 * Test the field item list when accessing an index.
70 public function testArrayIndex() {
71 $this->assertFalse($this->testNode->isPublished());
72 $this->assertEquals('draft', $this->testNode->moderation_state[0]->value);
76 * Test the field item list when iterating.
78 public function testArrayIteration() {
80 foreach ($this->testNode->moderation_state as $item) {
81 $states[] = $item->value;
83 $this->assertEquals(['draft'], $states);
89 public function testGetValue() {
90 $this->assertEquals([['value' => 'draft']], $this->testNode->moderation_state->getValue());
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);
103 * Tests the computed field when it is unset or set to an empty value.
105 public function testSetEmptyState() {
106 $this->testNode->moderation_state->value = '';
107 $this->assertEquals('draft', $this->testNode->moderation_state->value);
109 $this->testNode->moderation_state = '';
110 $this->assertEquals('draft', $this->testNode->moderation_state->value);
112 unset($this->testNode->moderation_state);
113 $this->assertEquals('draft', $this->testNode->moderation_state->value);
117 * Test the list class with a non moderated entity.
119 public function testNonModeratedEntity() {
120 $unmoderated_node = Node::create([
121 'type' => 'unmoderated',
122 'title' => 'Test title',
124 $unmoderated_node->save();
125 $this->assertEquals(0, $unmoderated_node->moderation_state->count());
127 $unmoderated_node->moderation_state = NULL;
128 $this->assertEquals(0, $unmoderated_node->moderation_state->count());
132 * Tests that moderation state changes also change the related entity state.
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
138 $this->testNode->moderation_state->value = 'published';
140 $this->assertTrue($this->testNode->isPublished());
141 $this->assertTrue($this->testNode->isDefaultRevision());
143 $this->testNode->save();
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());
152 * Test updating the state for an entity without a workflow.
154 public function testEntityWithNoWorkflow() {
155 $node_type = NodeType::create([
156 'type' => 'example_no_workflow',
159 $test_node = Node::create([
160 'type' => 'example_no_workflow',
161 'title' => 'Test node with no workflow',
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);
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());
177 * Test the moderation_state field after an entity has been serialized.
179 public function testEntityUnserialize() {
180 $this->testNode->moderation_state->value = 'draft';
181 $unserialized = unserialize(serialize($this->testNode));
183 $this->assertEquals('Test title', $unserialized->title->value);
184 $this->assertEquals('draft', $unserialized->moderation_state->value);