3 namespace Drupal\Tests\content_moderation\Kernel;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\workflows\Entity\Workflow;
12 * @coversDefaultClass \Drupal\content_moderation\Plugin\Validation\Constraint\ModerationStateConstraintValidator
13 * @group content_moderation
15 class EntityStateChangeValidationTest extends KernelTestBase {
20 public static $modules = [
26 'content_translation',
33 protected function setUp() {
36 $this->installSchema('node', 'node_access');
37 $this->installEntitySchema('node');
38 $this->installEntitySchema('user');
39 $this->installEntitySchema('content_moderation_state');
40 $this->installConfig('content_moderation');
44 * Test valid transitions.
48 public function testValidTransition() {
49 $node_type = NodeType::create([
53 $workflow = Workflow::load('editorial');
54 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
57 $node = Node::create([
59 'title' => 'Test title',
61 $node->moderation_state->value = 'draft';
64 $node->moderation_state->value = 'published';
65 $this->assertCount(0, $node->validate());
68 $this->assertEquals('published', $node->moderation_state->value);
72 * Test invalid transitions.
76 public function testInvalidTransition() {
77 $node_type = NodeType::create([
81 $workflow = Workflow::load('editorial');
82 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
85 $node = Node::create([
87 'title' => 'Test title',
89 $node->moderation_state->value = 'draft';
92 $node->moderation_state->value = 'archived';
93 $violations = $node->validate();
94 $this->assertCount(1, $violations);
96 $this->assertEquals('Invalid state transition from <em class="placeholder">Draft</em> to <em class="placeholder">Archived</em>', $violations->get(0)->getMessage());
100 * Tests that content without prior moderation information can be moderated.
102 public function testLegacyContent() {
103 $node_type = NodeType::create([
107 /** @var \Drupal\node\NodeInterface $node */
108 $node = Node::create([
110 'title' => 'Test title',
116 // Enable moderation for our node type.
117 $workflow = Workflow::load('editorial');
118 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
121 $node = Node::load($nid);
123 // Having no previous state should not break validation.
124 $violations = $node->validate();
126 $this->assertCount(0, $violations);
128 // Having no previous state should not break saving the node.
129 $node->setTitle('New');
134 * Tests that content without prior moderation information can be translated.
136 public function testLegacyMultilingualContent() {
138 ConfigurableLanguage::createFromLangcode('fr')->save();
140 $node_type = NodeType::create([
144 /** @var \Drupal\node\NodeInterface $node */
145 $node = Node::create([
147 'title' => 'Test title',
154 $node = Node::load($nid);
156 // Creating a translation shouldn't break, even though there's no previous
157 // moderated revision for the new language.
158 $node_fr = $node->addTranslation('fr');
159 $node_fr->setTitle('Francais');
162 // Enable moderation for our node type.
163 $workflow = Workflow::load('editorial');
164 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
167 // Reload the French version of the node.
168 $node = Node::load($nid);
169 $node_fr = $node->getTranslation('fr');
171 /** @var \Drupal\node\NodeInterface $node_fr */
172 $node_fr->setTitle('Nouveau');