summaryrefslogtreecommitdiffstats
path: root/core/modules/content_moderation/tests/src/Kernel/ContentModerationWorkflowConfigTest.php
blob: 931056e3d67ca82ecb37d94e07de237dcd4a336d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php

namespace Drupal\Tests\content_moderation\Kernel;

use Drupal\Core\Config\ConfigImporterException;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\workflows\Entity\Workflow;

/**
 * Tests how Content Moderation handles workflow config changes.
 *
 * @group content_moderation
 */
class ContentModerationWorkflowConfigTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'node',
    'content_moderation',
    'user',
    'system',
    'text',
    'workflows',
  ];

  /**
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * @var \Drupal\workflows\Entity\Workflow
   */
  protected $workflow;

  /**
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorage
   */
  protected $workflowStorage;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->installSchema('node', 'node_access');
    $this->installEntitySchema('node');
    $this->installEntitySchema('user');
    $this->installEntitySchema('content_moderation_state');
    $this->installConfig('content_moderation');

    NodeType::create([
      'type' => 'example',
    ])->save();

    $workflow = Workflow::load('editorial');
    $workflow->getTypePlugin()
      ->addState('test1', 'Test one')
      ->addState('test2', 'Test two')
      ->addState('test3', 'Test three')
      ->addEntityTypeAndBundle('node', 'example');
    $workflow->save();
    $this->workflow = $workflow;

    $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
  }

  /**
   * Test deleting a state via config import.
   */
  public function testDeletingStateViaConfiguration() {
    $config_sync = \Drupal::service('config.storage.sync');

    // Alter the workflow data.
    $config_data = $this->config('workflows.workflow.editorial')->get();
    unset($config_data['type_settings']['states']['test1']);
    $config_sync->write('workflows.workflow.editorial', $config_data);

    // Alter the data of another entity type.
    $config_data = $this->config('node.type.example')->get();
    $config_data['description'] = 'A new description';
    $config_sync->write('node.type.example', $config_data);

    // Alter the values of simple config.
    $config_data = $this->config('core.extension')->get();
    $config_data['module']['node'] = 1;
    $config_sync->write('core.extension', $config_data);

    // There are no Nodes with the moderation state test1, so this should run
    // with no errors.
    $this->configImporter()->reset()->import();

    $node = Node::create([
      'type' => 'example',
      'title' => 'Test title',
      'moderation_state' => 'test2',
    ]);
    $node->save();

    $config_data = $this->config('workflows.workflow.editorial')->get();
    unset($config_data['type_settings']['states']['test2']);
    unset($config_data['type_settings']['states']['test3']);
    \Drupal::service('config.storage.sync')->write('workflows.workflow.editorial', $config_data);

    // Now there is a Node with the moderation state test2, this will fail.
    try {
      $this->configImporter()->reset()->import();
      $this->fail('ConfigImporterException not thrown, invalid import was not stopped due to deleted state.');
    }
    catch (ConfigImporterException $e) {
      $this->assertEqual($e->getMessage(), 'There were errors validating the config synchronization.');
      $error_log = $this->configImporter->getErrors();
      $expected = ['The moderation state Test two is being used, but is not in the source storage.'];
      $this->assertEqual($expected, $error_log);
    }

    \Drupal::service('config.storage.sync')->delete('workflows.workflow.editorial');

    // An error should be thrown when trying to delete an in use workflow.
    try {
      $this->configImporter()->reset()->import();
      $this->fail('ConfigImporterException not thrown, invalid import was not stopped due to deleted workflow.');
    }
    catch (ConfigImporterException $e) {
      $this->assertEqual($e->getMessage(), 'There were errors validating the config synchronization.');
      $error_log = $this->configImporter->getErrors();
      $expected = [
        'The moderation state Test two is being used, but is not in the source storage.',
        'The workflow Editorial is being used, and cannot be deleted.',
      ];
      $this->assertEqual($expected, $error_log);
    }
  }

}