ContentModerationWorkflowConfigTest.php 4.8 KB

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