View source
<?php
namespace Drupal\Tests\content_moderation\Kernel;
use Drupal\Core\Entity\EntityInterface;
use Drupal\node\Entity\Node;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\Tests\workspaces\Kernel\WorkspaceTestTrait;
use Drupal\workflows\Entity\Workflow;
use Drupal\workflows\WorkflowInterface;
use Drupal\workspaces\WorkspaceAccessException;
class WorkspacesContentModerationStateTest extends ContentModerationStateTest {
use ContentModerationTestTrait {
createEditorialWorkflow as traitCreateEditorialWorkflow;
addEntityTypeAndBundleToWorkflow as traitAddEntityTypeAndBundleToWorkflow;
}
use ContentTypeCreationTrait {
createContentType as traitCreateContentType;
}
use UserCreationTrait;
use WorkspaceTestTrait;
protected $revEntityTypeId = 'entity_test_revpub';
protected function setUp() : void {
parent::setUp();
$this
->installSchema('system', [
'sequences',
]);
$this
->initializeWorkspacesModule();
$this
->switchToWorkspace('stage');
}
public function testWorkspaceEntityTypeModeration() {
$moderation_info = \Drupal::service('content_moderation.moderation_information');
$entity_type = \Drupal::entityTypeManager()
->getDefinition('workspace');
$this
->assertFalse($moderation_info
->canModerateEntitiesOfEntityType($entity_type));
}
public function testContentModerationIntegrationWithWorkspaces() {
$editorial = $this
->createEditorialWorkflow();
$access_handler = \Drupal::entityTypeManager()
->getAccessControlHandler('workspace');
$editorial_2_values = $editorial
->toArray();
unset($editorial_2_values['uuid']);
$editorial_2_values['id'] = 'editorial_2';
$editorial_2_values['type_settings']['states']['archived']['default_revision'] = FALSE;
$editorial_2 = Workflow::create($editorial_2_values);
$this->workspaceManager
->executeOutsideWorkspace(function () use ($editorial_2) {
$editorial_2
->save();
});
$this
->createContentType([
'type' => 'page',
]);
$this
->addEntityTypeAndBundleToWorkflow($editorial, 'node', 'page');
$this
->createContentType([
'type' => 'article',
]);
$this
->addEntityTypeAndBundleToWorkflow($editorial_2, 'node', 'article');
$page_archived = Node::create([
'type' => 'page',
'title' => 'Test page - archived',
'moderation_state' => 'archived',
]);
$page_archived
->save();
$page_draft = Node::create([
'type' => 'page',
'title' => 'Test page - draft',
'moderation_state' => 'draft',
]);
$page_draft
->save();
$page_published = Node::create([
'type' => 'page',
'title' => 'Test page - published',
'moderation_state' => 'published',
]);
$page_published
->save();
$article_archived = Node::create([
'type' => 'article',
'title' => 'Test article - archived',
'moderation_state' => 'archived',
]);
$article_archived
->save();
$article_draft = Node::create([
'type' => 'article',
'title' => 'Test article - draft',
'moderation_state' => 'draft',
]);
$article_draft
->save();
$article_published = Node::create([
'type' => 'article',
'title' => 'Test article - published',
'moderation_state' => 'published',
]);
$article_published
->save();
try {
$this->workspaces['stage']
->publish();
$this
->fail('The expected exception was not thrown.');
} catch (WorkspaceAccessException $e) {
$this
->assertEquals('The Stage workspace can not be published because it contains 3 items in an unpublished moderation state.', $e
->getMessage());
}
$page_draft->moderation_state->value = 'published';
$page_draft
->save();
try {
$access_handler
->resetCache();
$this->workspaces['stage']
->publish();
$this
->fail('The expected exception was not thrown.');
} catch (WorkspaceAccessException $e) {
$this
->assertEquals('The Stage workspace can not be published because it contains 2 items in an unpublished moderation state.', $e
->getMessage());
}
$article_archived->moderation_state->value = 'published';
$article_archived
->save();
try {
$access_handler
->resetCache();
$this->workspaces['stage']
->publish();
$this
->fail('The expected exception was not thrown.');
} catch (WorkspaceAccessException $e) {
$this
->assertEquals('The Stage workspace can not be published because it contains 1 item in an unpublished moderation state.', $e
->getMessage());
}
$article_draft->moderation_state->value = 'published';
$article_draft
->save();
$access_handler
->resetCache();
$this->workspaces['stage']
->publish();
}
public function basicModerationTestCases() {
return [
'Nodes' => [
'node',
],
'Block content' => [
'block_content',
],
'Media' => [
'media',
],
'Test entity - revisions, data table, and published interface' => [
'entity_test_mulrevpub',
],
'Entity Test with revisions and published status' => [
'entity_test_revpub',
],
];
}
public function testModerationWithFieldConfigOverride() {
$this
->markTestSkipped();
}
public function testWorkflowDependencies() {
$this
->markTestSkipped();
}
public function testWorkflowNonConfigBundleDependencies() {
$this
->markTestSkipped();
}
public function testGetCurrentUserId() {
$this
->markTestSkipped();
}
protected function createEntity($entity_type_id, $moderation_state = 'published', $create_workflow = TRUE) {
$entity = $this->workspaceManager
->executeOutsideWorkspace(function () use ($entity_type_id, $moderation_state, $create_workflow) {
return parent::createEntity($entity_type_id, $moderation_state, $create_workflow);
});
return $entity;
}
protected function createEditorialWorkflow() {
$workflow = $this->workspaceManager
->executeOutsideWorkspace(function () {
return $this
->traitCreateEditorialWorkflow();
});
return $workflow;
}
protected function addEntityTypeAndBundleToWorkflow(WorkflowInterface $workflow, $entity_type_id, $bundle) {
$this->workspaceManager
->executeOutsideWorkspace(function () use ($workflow, $entity_type_id, $bundle) {
$this
->traitAddEntityTypeAndBundleToWorkflow($workflow, $entity_type_id, $bundle);
});
}
protected function createContentType(array $values = []) {
$note_type = $this->workspaceManager
->executeOutsideWorkspace(function () use ($values) {
return $this
->traitCreateContentType($values);
});
return $note_type;
}
protected function assertDefaultRevision(EntityInterface $entity, $revision_id, $published = TRUE) {
$revision_id = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId())
->load($entity
->id())
->getRevisionId();
$published = NULL;
parent::assertDefaultRevision($entity, $revision_id, $published);
}
}