Security update for Core, with self-updated composer
[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     $node_type = NodeType::create([
47       'type' => 'example',
48     ]);
49     $node_type->save();
50     $workflow = Workflow::load('editorial');
51     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
52     $workflow->save();
53
54     $this->testNode = Node::create([
55       'type' => 'example',
56       'title' => 'Test title',
57     ]);
58     $this->testNode->save();
59     \Drupal::entityTypeManager()->getStorage('node')->resetCache();
60     $this->testNode = Node::load($this->testNode->id());
61   }
62
63   /**
64    * Test the field item list when accessing an index.
65    */
66   public function testArrayIndex() {
67     $this->assertFalse($this->testNode->isPublished());
68     $this->assertEquals('draft', $this->testNode->moderation_state[0]->value);
69   }
70
71   /**
72    * Test the field item list when iterating.
73    */
74   public function testArrayIteration() {
75     $states = [];
76     foreach ($this->testNode->moderation_state as $item) {
77       $states[] = $item->value;
78     }
79     $this->assertEquals(['draft'], $states);
80   }
81
82   /**
83    * Tests that moderation state changes also change the related entity state.
84    */
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
88     // updated.
89     $this->testNode->moderation_state->value = 'published';
90
91     $this->assertTrue($this->testNode->isPublished());
92     $this->assertTrue($this->testNode->isDefaultRevision());
93
94     $this->testNode->save();
95
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());
100   }
101
102   /**
103    * Test updating the state for an entity without a workflow.
104    */
105   public function testEntityWithNoWorkflow() {
106     $node_type = NodeType::create([
107       'type' => 'example_no_workflow',
108     ]);
109     $node_type->save();
110     $test_node = Node::create([
111       'type' => 'example_no_workflow',
112       'title' => 'Test node with no workflow',
113     ]);
114     $test_node->save();
115
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);
120
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());
125   }
126
127 }