Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / content_moderation / tests / src / Kernel / ContentModerationWorkflowConfigTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Kernel;
4
5 use Drupal\Core\Config\ConfigImporterException;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\workflows\Entity\Workflow;
10
11 /**
12  * Tests how Content Moderation handles workflow config changes.
13  *
14  * @group content_moderation
15  */
16 class ContentModerationWorkflowConfigTest extends KernelTestBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   public static $modules = [
22     'node',
23     'content_moderation',
24     'user',
25     'system',
26     'text',
27     'workflows',
28   ];
29
30   /**
31    * @var \Drupal\Core\Entity\EntityTypeManager
32    */
33   protected $entityTypeManager;
34
35   /**
36    * @var \Drupal\Core\Config\ConfigFactoryInterface
37    */
38   protected $configFactory;
39
40   /**
41    * @var \Drupal\workflows\Entity\Workflow
42    */
43   protected $workflow;
44
45   /**
46    * @var \Drupal\Core\Config\Entity\ConfigEntityStorage
47    */
48   protected $workflowStorage;
49
50   /**
51    * {@inheritdoc}
52    */
53   protected function setUp() {
54     parent::setUp();
55
56     $this->installSchema('node', 'node_access');
57     $this->installEntitySchema('node');
58     $this->installEntitySchema('user');
59     $this->installEntitySchema('content_moderation_state');
60     $this->installConfig('content_moderation');
61
62     NodeType::create([
63       'type' => 'example',
64     ])->save();
65
66     $workflow = Workflow::load('editorial');
67     $workflow->getTypePlugin()
68       ->addState('test1', 'Test one')
69       ->addState('test2', 'Test two')
70       ->addState('test3', 'Test three')
71       ->addEntityTypeAndBundle('node', 'example');
72     $workflow->save();
73     $this->workflow = $workflow;
74
75     $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
76   }
77
78   /**
79    * Test deleting a state via config import.
80    */
81   public function testDeletingStateViaConfiguration() {
82     $config_sync = \Drupal::service('config.storage.sync');
83
84     // Alter the workflow data.
85     $config_data = $this->config('workflows.workflow.editorial')->get();
86     unset($config_data['type_settings']['states']['test1']);
87     $config_sync->write('workflows.workflow.editorial', $config_data);
88
89     // Alter the data of another entity type.
90     $config_data = $this->config('node.type.example')->get();
91     $config_data['description'] = 'A new description';
92     $config_sync->write('node.type.example', $config_data);
93
94     // Alter the values of simple config.
95     $config_data = $this->config('core.extension')->get();
96     $config_data['module']['node'] = 1;
97     $config_sync->write('core.extension', $config_data);
98
99     // There are no Nodes with the moderation state test1, so this should run
100     // with no errors.
101     $this->configImporter()->reset()->import();
102
103     $node = Node::create([
104       'type' => 'example',
105       'title' => 'Test title',
106       'moderation_state' => 'test2',
107     ]);
108     $node->save();
109
110     $config_data = $this->config('workflows.workflow.editorial')->get();
111     unset($config_data['type_settings']['states']['test2']);
112     unset($config_data['type_settings']['states']['test3']);
113     \Drupal::service('config.storage.sync')->write('workflows.workflow.editorial', $config_data);
114
115     // Now there is a Node with the moderation state test2, this will fail.
116     try {
117       $this->configImporter()->reset()->import();
118       $this->fail('ConfigImporterException not thrown, invalid import was not stopped due to deleted state.');
119     }
120     catch (ConfigImporterException $e) {
121       $this->assertEqual($e->getMessage(), 'There were errors validating the config synchronization.');
122       $error_log = $this->configImporter->getErrors();
123       $expected = ['The moderation state Test two is being used, but is not in the source storage.'];
124       $this->assertEqual($expected, $error_log);
125     }
126
127     \Drupal::service('config.storage.sync')->delete('workflows.workflow.editorial');
128
129     // An error should be thrown when trying to delete an in use workflow.
130     try {
131       $this->configImporter()->reset()->import();
132       $this->fail('ConfigImporterException not thrown, invalid import was not stopped due to deleted workflow.');
133     }
134     catch (ConfigImporterException $e) {
135       $this->assertEqual($e->getMessage(), 'There were errors validating the config synchronization.');
136       $error_log = $this->configImporter->getErrors();
137       $expected = [
138         'The moderation state Test two is being used, but is not in the source storage.',
139         'The workflow Editorial is being used, and cannot be deleted.',
140       ];
141       $this->assertEqual($expected, $error_log);
142     }
143   }
144
145 }