3 namespace Drupal\Tests\content_moderation\Kernel;
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;
12 * Tests how Content Moderation handles workflow config changes.
14 * @group content_moderation
16 class ContentModerationWorkflowConfigTest extends KernelTestBase {
18 use ContentModerationTestTrait;
23 public static $modules = [
33 * @var \Drupal\Core\Entity\EntityTypeManager
35 protected $entityTypeManager;
38 * @var \Drupal\Core\Config\ConfigFactoryInterface
40 protected $configFactory;
43 * @var \Drupal\workflows\Entity\Workflow
48 * @var \Drupal\Core\Config\Entity\ConfigEntityStorage
50 protected $workflowStorage;
55 protected function setUp() {
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']);
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');
75 $this->workflow = $workflow;
77 $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
81 * Test deleting a state via config import.
83 public function testDeletingStateViaConfiguration() {
84 $config_sync = \Drupal::service('config.storage.sync');
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);
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);
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);
101 // There are no Nodes with the moderation state test1, so this should run
103 $this->configImporter()->reset()->import();
105 $node = Node::create([
107 'title' => 'Test title',
108 'moderation_state' => 'test2',
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);
117 // Now there is a Node with the moderation state test2, this will fail.
119 $this->configImporter()->reset()->import();
120 $this->fail('ConfigImporterException not thrown, invalid import was not stopped due to deleted state.');
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);
129 \Drupal::service('config.storage.sync')->delete('workflows.workflow.editorial');
131 // An error should be thrown when trying to delete an in use workflow.
133 $this->configImporter()->reset()->import();
134 $this->fail('ConfigImporterException not thrown, invalid import was not stopped due to deleted workflow.');
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();
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.',
143 $this->assertEqual($expected, $error_log);