View source
<?php
namespace Drupal\Tests\content_moderation\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
class EntityStateChangeValidationTest extends KernelTestBase {
use ContentModerationTestTrait;
use UserCreationTrait;
protected static $modules = [
'node',
'content_moderation',
'user',
'system',
'language',
'content_translation',
'workflows',
];
protected $adminUser;
protected function setUp() : void {
parent::setUp();
$this
->installSchema('node', 'node_access');
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installEntitySchema('content_moderation_state');
$this
->installConfig('content_moderation');
$this
->installSchema('system', [
'sequences',
]);
$this->adminUser = $this
->createUser(array_keys($this->container
->get('user.permissions')
->getPermissions()));
}
public function testValidTransition() {
$this
->setCurrentUser($this->adminUser);
$node_type = NodeType::create([
'type' => 'example',
]);
$node_type
->save();
$workflow = $this
->createEditorialWorkflow();
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'example');
$workflow
->save();
$node = Node::create([
'type' => 'example',
'title' => 'Test title',
]);
$node->moderation_state->value = 'draft';
$node
->save();
$node->moderation_state->value = 'published';
$this
->assertCount(0, $node
->validate());
$node
->save();
$this
->assertEquals('published', $node->moderation_state->value);
}
public function testInvalidTransition() {
$this
->setCurrentUser($this->adminUser);
$node_type = NodeType::create([
'type' => 'example',
]);
$node_type
->save();
$workflow = $this
->createEditorialWorkflow();
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'example');
$workflow
->save();
$node = Node::create([
'type' => 'example',
'title' => 'Test title',
]);
$node->moderation_state->value = 'draft';
$node
->save();
$node->moderation_state->value = 'archived';
$violations = $node
->validate();
$this
->assertCount(1, $violations);
$this
->assertEquals('Invalid state transition from <em class="placeholder">Draft</em> to <em class="placeholder">Archived</em>', $violations
->get(0)
->getMessage());
}
public function testInvalidState() {
$node_type = NodeType::create([
'type' => 'example',
]);
$node_type
->save();
$workflow = $this
->createEditorialWorkflow();
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'example');
$workflow
->save();
$node = Node::create([
'type' => 'example',
'title' => 'Test title',
]);
$node->moderation_state->value = 'invalid_state';
$violations = $node
->validate();
$this
->assertCount(1, $violations);
$this
->assertEquals('State <em class="placeholder">invalid_state</em> does not exist on <em class="placeholder">Editorial</em> workflow', $violations
->get(0)
->getMessage());
}
public function testInvalidStateWithoutExisting() {
$this
->setCurrentUser($this->adminUser);
$node_type = NodeType::create([
'type' => 'example',
]);
$node_type
->save();
$node = Node::create([
'type' => 'example',
'title' => 'Test title',
]);
$node
->save();
$workflow = $this
->createEditorialWorkflow();
$workflow
->getTypePlugin()
->addState('deleted_state', 'Deleted state');
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'example');
$workflow
->save();
$node = Node::load($node
->id());
$node->moderation_state->value = 'invalid_state';
$violations = $node
->validate();
$this
->assertCount(1, $violations);
$node->moderation_state->value = 'deleted_state';
$node
->save();
$workflow
->getTypePlugin()
->deleteState('deleted_state');
$workflow
->save();
$node->moderation_state->value = 'draft';
$violations = $node
->validate();
$this
->assertCount(0, $violations);
$node->moderation_state->value = 'archived';
$violations = $node
->validate();
$this
->assertCount(1, $violations);
}
public function testInvalidStateMultilingual() {
$this
->setCurrentUser($this->adminUser);
ConfigurableLanguage::createFromLangcode('fr')
->save();
$node_type = NodeType::create([
'type' => 'example',
]);
$node_type
->save();
$workflow = $this
->createEditorialWorkflow();
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'example');
$workflow
->save();
$node = Node::create([
'type' => 'example',
'title' => 'English Published Node',
'langcode' => 'en',
'moderation_state' => 'published',
]);
$node
->save();
$node_fr = $node
->addTranslation('fr', $node
->toArray());
$node_fr
->setTitle('French Published Node');
$node_fr
->save();
$this
->assertEquals('published', $node_fr->moderation_state->value);
$node->moderation_state = 'draft';
$node
->setNewRevision(TRUE);
$node
->isDefaultRevision(FALSE);
$node
->save();
$node->moderation_state = 'archived';
$violations = $node
->validate();
$this
->assertCount(1, $violations);
$this
->assertEquals('Invalid state transition from <em class="placeholder">Draft</em> to <em class="placeholder">Archived</em>', $violations
->get(0)
->getMessage());
$node_fr = Node::load($node
->id())
->getTranslation('fr');
$this
->assertEquals('published', $node_fr->moderation_state->value);
$node_fr->moderation_state = 'archived';
$violations = $node_fr
->validate();
$this
->assertCount(0, $violations);
$node_fr = Node::load($node
->id())
->getTranslation('fr');
$this
->assertEquals('published', $node_fr->moderation_state->value);
$node_fr->moderation_state = 'archived';
$violations = $node_fr
->validate();
$this
->assertCount(0, $violations);
}
public function testExistingContentWithNoModeration() {
$this
->setCurrentUser($this->adminUser);
$node_type = NodeType::create([
'type' => 'example',
]);
$node_type
->save();
$node = Node::create([
'type' => 'example',
'title' => 'Test title',
]);
$node
->save();
$nid = $node
->id();
$workflow = $this
->createEditorialWorkflow();
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'example');
$workflow
->save();
$node = Node::load($nid);
$violations = $node
->validate();
$this
->assertCount(0, $violations);
$node
->setTitle('New');
$node
->save();
}
public function testExistingMultilingualContentWithNoModeration() {
$this
->setCurrentUser($this->adminUser);
ConfigurableLanguage::createFromLangcode('fr')
->save();
$node_type = NodeType::create([
'type' => 'example',
]);
$node_type
->save();
$node = Node::create([
'type' => 'example',
'title' => 'Test title',
'langcode' => 'en',
]);
$node
->save();
$nid = $node
->id();
$node = Node::load($nid);
$node_fr = $node
->addTranslation('fr');
$node_fr
->setTitle('Francais');
$node_fr
->save();
$workflow = $this
->createEditorialWorkflow();
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'example');
$workflow
->save();
$node = Node::load($nid);
$node_fr = $node
->getTranslation('fr');
$node_fr
->setTitle('Nouveau');
$node_fr
->save();
}
public function testTransitionAccessValidation($permissions, $target_state, $messages) {
$node_type = NodeType::create([
'type' => 'example',
]);
$node_type
->save();
$workflow = $this
->createEditorialWorkflow();
$workflow
->getTypePlugin()
->addState('foo', 'Foo');
$workflow
->getTypePlugin()
->addTransition('draft_to_foo', 'Draft to foo', [
'draft',
], 'foo');
$workflow
->getTypePlugin()
->addTransition('foo_to_foo', 'Foo to foo', [
'foo',
], 'foo');
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'example');
$workflow
->save();
$this
->setCurrentUser($this
->createUser($permissions));
$node = Node::create([
'type' => 'example',
'title' => 'Test content',
'moderation_state' => $target_state,
]);
$this
->assertTrue($node
->isNew());
$violations = $node
->validate();
$this
->assertSameSize($messages, $violations);
foreach ($messages as $i => $message) {
$this
->assertEquals($message, $violations
->get($i)
->getMessage());
}
}
public function transitionAccessValidationTestCases() {
return [
'Invalid transition, no permissions validated' => [
[],
'archived',
[
'Invalid state transition from <em class="placeholder">Draft</em> to <em class="placeholder">Archived</em>',
],
],
'Valid transition, missing permission' => [
[],
'published',
[
'You do not have access to transition from <em class="placeholder">Draft</em> to <em class="placeholder">Published</em>',
],
],
'Valid transition, granted published permission' => [
[
'use editorial transition publish',
],
'published',
[],
],
'Valid transition, granted draft permission' => [
[
'use editorial transition create_new_draft',
],
'draft',
[],
],
'Valid transition, incorrect permission granted' => [
[
'use editorial transition create_new_draft',
],
'published',
[
'You do not have access to transition from <em class="placeholder">Draft</em> to <em class="placeholder">Published</em>',
],
],
'Valid transition, granted foo permission' => [
[
'use editorial transition draft_to_foo',
],
'foo',
[],
],
'Valid transition, incorrect foo permission granted' => [
[
'use editorial transition foo_to_foo',
],
'foo',
[
'You do not have access to transition from <em class="placeholder">Draft</em> to <em class="placeholder">Foo</em>',
],
],
];
}
}