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\workflows\Entity\Workflow;
12 * Tests how Content Moderation handles workflow config changes.
14 * @group content_moderation
16 class ContentModerationWorkflowConfigTest extends KernelTestBase {
21 public static $modules = [
31 * @var \Drupal\Core\Entity\EntityTypeManager
33 protected $entityTypeManager;
36 * @var \Drupal\Core\Config\ConfigFactoryInterface
38 protected $configFactory;
41 * @var \Drupal\workflows\Entity\Workflow
46 * @var \Drupal\Core\Config\Entity\ConfigEntityStorage
48 protected $workflowStorage;
53 protected function setUp() {
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');
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');
73 $this->workflow = $workflow;
75 $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
79 * Test deleting a state via config import.
81 public function testDeletingStateViaConfiguration() {
82 $config_sync = \Drupal::service('config.storage.sync');
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);
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);
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);
99 // There are no Nodes with the moderation state test1, so this should run
101 $this->configImporter()->reset()->import();
103 $node = Node::create([
105 'title' => 'Test title',
106 'moderation_state' => 'test2',
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);
115 // Now there is a Node with the moderation state test2, this will fail.
117 $this->configImporter()->reset()->import();
118 $this->fail('ConfigImporterException not thrown, invalid import was not stopped due to deleted state.');
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);
127 \Drupal::service('config.storage.sync')->delete('workflows.workflow.editorial');
129 // An error should be thrown when trying to delete an in use workflow.
131 $this->configImporter()->reset()->import();
132 $this->fail('ConfigImporterException not thrown, invalid import was not stopped due to deleted workflow.');
134 catch (ConfigImporterException $e) {
135 $this->assertEqual($e->getMessage(), 'There were errors validating the config synchronization.');
136 $error_log = $this->configImporter->getErrors();
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.',
141 $this->assertEqual($expected, $error_log);