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