function WorkspacesContentModerationStateTest::testContentModerationIntegrationWithWorkspaces

Same name and namespace in other branches
  1. 9 core/modules/content_moderation/tests/src/Kernel/WorkspacesContentModerationStateTest.php \Drupal\Tests\content_moderation\Kernel\WorkspacesContentModerationStateTest::testContentModerationIntegrationWithWorkspaces()
  2. 8.9.x core/modules/content_moderation/tests/src/Kernel/WorkspacesContentModerationStateTest.php \Drupal\Tests\content_moderation\Kernel\WorkspacesContentModerationStateTest::testContentModerationIntegrationWithWorkspaces()
  3. 11.x core/modules/content_moderation/tests/src/Kernel/WorkspacesContentModerationStateTest.php \Drupal\Tests\content_moderation\Kernel\WorkspacesContentModerationStateTest::testContentModerationIntegrationWithWorkspaces()

Tests the integration between Content Moderation and Workspaces.

File

core/modules/content_moderation/tests/src/Kernel/WorkspacesContentModerationStateTest.php, line 89

Class

WorkspacesContentModerationStateTest
Tests that Workspaces and Content Moderation work together properly.

Namespace

Drupal\Tests\content_moderation\Kernel

Code

public function testContentModerationIntegrationWithWorkspaces() : void {
  $editorial = $this->createEditorialWorkflow();
  $access_handler = \Drupal::entityTypeManager()->getAccessControlHandler('workspace');
  // Create another workflow which has the same states as the 'editorial' one,
  // but it doesn't create default revisions for the 'archived' state. This
  // covers the case when two bundles of the same entity type use different
  // workflows with same moderation state names but with different settings.
  $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();
  });
  // Create two bundles and assign the two workflows for each of them.
  $this->createContentType([
    'type' => 'page',
  ]);
  $this->addEntityTypeAndBundleToWorkflow($editorial, 'node', 'page');
  $this->createContentType([
    'type' => 'article',
  ]);
  $this->addEntityTypeAndBundleToWorkflow($editorial_2, 'node', 'article');
  // Create three entities for each bundle, covering all the available
  // moderation states.
  $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();
  // We have three items in a non-default moderation state:
  // - $page_draft
  // - $article_archived
  // - $article_draft
  // Therefore the workspace can not be published.
  // This assertion also covers two moderation states from different workflows
  // with the same name ('archived'), but with different default revision
  // settings.
  try {
    $this->workspaces['stage']
      ->publish();
    $this->fail('The expected exception was not thrown.');
  } catch (WorkspacePublishException $e) {
    $this->assertEquals('The Stage workspace can not be published because it contains 3 items in an unpublished moderation state.', $e->getMessage());
  }
  // Get the $page_draft node to a publishable state and try again.
  $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 (WorkspacePublishException $e) {
    $this->assertEquals('The Stage workspace can not be published because it contains 2 items in an unpublished moderation state.', $e->getMessage());
  }
  // Get the $article_archived node to a publishable state and try again.
  $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 (WorkspacePublishException $e) {
    $this->assertEquals('The Stage workspace can not be published because it contains 1 item in an unpublished moderation state.', $e->getMessage());
  }
  // Get the $article_draft node to a publishable state and try again.
  $article_draft->moderation_state->value = 'published';
  $article_draft->save();
  $access_handler->resetCache();
  $this->workspaces['stage']
    ->publish();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.