View source
<?php
namespace Drupal\Tests\workbench_moderation\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\user\Traits\UserCreationTrait;
class EntityStateChangeValidationTest extends KernelTestBase {
use UserCreationTrait;
public static $modules = [
'node',
'workbench_moderation',
'user',
'system',
'language',
'content_translation',
];
protected $adminUser;
protected function setUp() {
parent::setUp();
$this
->installSchema('node', 'node_access');
$this
->installSchema('system', [
'sequences',
]);
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installConfig('workbench_moderation');
$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();
$node = Node::create([
'type' => 'example',
'title' => 'Test title',
'moderation_state' => 'draft',
]);
$node
->save();
$node->moderation_state->target_id = 'needs_review';
$this
->assertCount(0, $node
->validate());
}
public function testInvalidTransition() {
$this
->setCurrentUser($this->adminUser);
$node_type = NodeType::create([
'type' => 'example',
]);
$node_type
->setThirdPartySetting('workbench_moderation', 'enabled', TRUE);
$node_type
->save();
$node = Node::create([
'type' => 'example',
'title' => 'Test title',
'moderation_state' => 'draft',
]);
$node
->save();
$node->moderation_state->target_id = '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 testContent() {
$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();
$node_type = NodeType::load('example');
$node_type
->setThirdPartySetting('workbench_moderation', 'enabled', TRUE);
$node_type
->setThirdPartySetting('workbench_moderation', 'allowed_moderation_states', [
'draft',
'needs_review',
'published',
]);
$node_type
->setThirdPartySetting('workbench_moderation', 'default_moderation_state', 'draft');
$node_type
->save();
$node = Node::load($nid);
$violations = $node
->validate();
$this
->assertCount(0, $violations);
$node
->setTitle('New');
$node
->save();
}
public function testMultilingualContent() {
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();
$node_type = NodeType::load('example');
$node_type
->setThirdPartySetting('workbench_moderation', 'enabled', TRUE);
$node_type
->setThirdPartySetting('workbench_moderation', 'allowed_moderation_states', [
'draft',
'needs_review',
'published',
]);
$node_type
->setThirdPartySetting('workbench_moderation', 'default_moderation_state', 'draft');
$node_type
->save();
$node = Node::load($nid);
$node_fr = $node
->getTranslation('fr');
$node_fr
->setTitle('Nouveau');
$node_fr
->save();
}
public function testTransitionAccessValidationNewEntity($permissions, $default_state, $target_state, $messages) {
$this
->setCurrentUser($this
->createUser($permissions));
$this
->createExampleModeratedContentType($default_state, [
'draft',
'needs_review',
'published',
'archived',
]);
$node = Node::create([
'type' => 'example',
'title' => 'Test content',
'moderation_state' => $target_state,
]);
$this
->assertTrue($node
->isNew());
$violations = $node
->validate();
$this
->assertCount(count($messages), $violations);
foreach ($messages as $i => $message) {
$this
->assertEquals($message, $violations
->get($i)
->getMessage());
}
}
public function testTransitionAccessValidationSavedEntity($permissions, $default_state, $target_state, $messages) {
$this
->setCurrentUser($this
->createUser($permissions));
$this
->createExampleModeratedContentType($default_state, [
'draft',
'needs_review',
'published',
'archived',
]);
$node = Node::create([
'type' => 'example',
'title' => 'Test content',
'moderation_state' => $default_state,
]);
$node
->save();
$node->moderation_state = $target_state;
$violations = $node
->validate();
$this
->assertCount(count($messages), $violations);
foreach ($messages as $i => $message) {
$this
->assertEquals($message, $violations
->get($i)
->getMessage());
}
}
public function transitionAccessValidationTestCases() {
return [
'Invalid transition, no permissions validated' => [
[],
'draft',
'archived',
[
'Invalid state transition from <em class="placeholder">Draft</em> to <em class="placeholder">Archived</em>',
],
],
'Valid transition, missing permission' => [
[],
'draft',
'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 draft_published transition',
],
'draft',
'published',
[],
],
'Valid transition, granted draft permission' => [
[
'use draft_draft transition',
],
'draft',
'draft',
[],
],
'Valid transition, incorrect permission granted' => [
[
'use draft_draft transition',
],
'draft',
'published',
[
'You do not have access to transition from <em class="placeholder">Draft</em> to <em class="placeholder">Published</em>',
],
],
'Non-draft default state, incorrect permission granted' => [
[
'use draft_draft transition',
],
'archived',
'published',
[
'You do not have access to transition from <em class="placeholder">Archived</em> to <em class="placeholder">Published</em>',
],
],
'Non-draft default state, correct permission granted' => [
[
'use archived_published transition',
],
'archived',
'published',
[],
],
'Non-draft default state, invalid transition' => [
[
'use published_archived transition',
],
'archived',
'draft',
[
'Invalid state transition from <em class="placeholder">Archived</em> to <em class="placeholder">Draft</em>',
],
],
];
}
protected function createExampleModeratedContentType($default_state, array $allowed_states) {
$node_type = NodeType::create([
'type' => 'example',
]);
$node_type
->save();
$node_type = NodeType::load('example');
$node_type
->setThirdPartySetting('workbench_moderation', 'enabled', TRUE);
$node_type
->setThirdPartySetting('workbench_moderation', 'allowed_moderation_states', $allowed_states);
$node_type
->setThirdPartySetting('workbench_moderation', 'default_moderation_state', $default_state);
$node_type
->save();
}
}