View source
<?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\Tests\content_moderation\Traits\ContentModerationTestTrait;
class ContentModerationWorkflowConfigTest extends KernelTestBase {
use ContentModerationTestTrait;
public static $modules = [
'node',
'content_moderation',
'user',
'system',
'text',
'workflows',
];
protected $entityTypeManager;
protected $configFactory;
protected $workflow;
protected $workflowStorage;
protected function setUp() {
parent::setUp();
$this
->installSchema('node', 'node_access');
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installEntitySchema('content_moderation_state');
$this
->installConfig([
'system',
'content_moderation',
]);
NodeType::create([
'type' => 'example',
])
->save();
$workflow = $this
->createEditorialWorkflow();
$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'));
}
public function testDeletingStateViaConfiguration() {
$config_sync = \Drupal::service('config.storage.sync');
$config_data = $this
->config('workflows.workflow.editorial')
->get();
unset($config_data['type_settings']['states']['test1']);
$config_sync
->write('workflows.workflow.editorial', $config_data);
$config_data = $this
->config('node.type.example')
->get();
$config_data['description'] = 'A new description';
$config_sync
->write('node.type.example', $config_data);
$config_data = $this
->config('core.extension')
->get();
$config_data['module']['node'] = 1;
$config_sync
->write('core.extension', $config_data);
$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);
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.' . PHP_EOL . 'The moderation state Test two is being used, but is not in the source storage.');
$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');
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.' . 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.');
$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);
}
}
}