Version 1
[yaffs-website] / web / core / modules / content_moderation / tests / src / Kernel / EntityStateChangeValidationTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Kernel;
4
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;
10
11 /**
12  * @coversDefaultClass \Drupal\content_moderation\Plugin\Validation\Constraint\ModerationStateConstraintValidator
13  * @group content_moderation
14  */
15 class EntityStateChangeValidationTest extends KernelTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = [
21     'node',
22     'content_moderation',
23     'user',
24     'system',
25     'language',
26     'content_translation',
27     'workflows',
28   ];
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function setUp() {
34     parent::setUp();
35
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');
41   }
42
43   /**
44    * Test valid transitions.
45    *
46    * @covers ::validate
47    */
48   public function testValidTransition() {
49     $node_type = NodeType::create([
50       'type' => 'example',
51     ]);
52     $node_type->save();
53     $workflow = Workflow::load('editorial');
54     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
55     $workflow->save();
56
57     $node = Node::create([
58       'type' => 'example',
59       'title' => 'Test title',
60     ]);
61     $node->moderation_state->value = 'draft';
62     $node->save();
63
64     $node->moderation_state->value = 'published';
65     $this->assertCount(0, $node->validate());
66     $node->save();
67
68     $this->assertEquals('published', $node->moderation_state->value);
69   }
70
71   /**
72    * Test invalid transitions.
73    *
74    * @covers ::validate
75    */
76   public function testInvalidTransition() {
77     $node_type = NodeType::create([
78       'type' => 'example',
79     ]);
80     $node_type->save();
81     $workflow = Workflow::load('editorial');
82     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
83     $workflow->save();
84
85     $node = Node::create([
86       'type' => 'example',
87       'title' => 'Test title',
88     ]);
89     $node->moderation_state->value = 'draft';
90     $node->save();
91
92     $node->moderation_state->value = 'archived';
93     $violations = $node->validate();
94     $this->assertCount(1, $violations);
95
96     $this->assertEquals('Invalid state transition from <em class="placeholder">Draft</em> to <em class="placeholder">Archived</em>', $violations->get(0)->getMessage());
97   }
98
99   /**
100    * Tests that content without prior moderation information can be moderated.
101    */
102   public function testLegacyContent() {
103     $node_type = NodeType::create([
104       'type' => 'example',
105     ]);
106     $node_type->save();
107     /** @var \Drupal\node\NodeInterface $node */
108     $node = Node::create([
109       'type' => 'example',
110       'title' => 'Test title',
111     ]);
112     $node->save();
113
114     $nid = $node->id();
115
116     // Enable moderation for our node type.
117     $workflow = Workflow::load('editorial');
118     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
119     $workflow->save();
120
121     $node = Node::load($nid);
122
123     // Having no previous state should not break validation.
124     $violations = $node->validate();
125
126     $this->assertCount(0, $violations);
127
128     // Having no previous state should not break saving the node.
129     $node->setTitle('New');
130     $node->save();
131   }
132
133   /**
134    * Tests that content without prior moderation information can be translated.
135    */
136   public function testLegacyMultilingualContent() {
137     // Enable French.
138     ConfigurableLanguage::createFromLangcode('fr')->save();
139
140     $node_type = NodeType::create([
141       'type' => 'example',
142     ]);
143     $node_type->save();
144     /** @var \Drupal\node\NodeInterface $node */
145     $node = Node::create([
146       'type' => 'example',
147       'title' => 'Test title',
148       'langcode' => 'en',
149     ]);
150     $node->save();
151
152     $nid = $node->id();
153
154     $node = Node::load($nid);
155
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');
160     $node_fr->save();
161
162     // Enable moderation for our node type.
163     $workflow = Workflow::load('editorial');
164     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
165     $workflow->save();
166
167     // Reload the French version of the node.
168     $node = Node::load($nid);
169     $node_fr = $node->getTranslation('fr');
170
171     /** @var \Drupal\node\NodeInterface $node_fr */
172     $node_fr->setTitle('Nouveau');
173     $node_fr->save();
174   }
175
176 }