function ModerationFormTest::testWorkflowInUse

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

Tests that workflows and states can not be deleted if they are in use.

@covers \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration::workflowHasData
@covers \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration::workflowStateHasData

File

core/modules/content_moderation/tests/src/Functional/ModerationFormTest.php, line 515

Class

ModerationFormTest
Tests the moderation form, specifically on nodes.

Namespace

Drupal\Tests\content_moderation\Functional

Code

public function testWorkflowInUse() : void {
  $user = $this->createUser([
    'administer workflows',
    'create moderated_content content',
    'edit own moderated_content content',
    'use editorial transition create_new_draft',
    'use editorial transition publish',
    'use editorial transition archive',
  ]);
  $this->drupalLogin($user);
  $paths = [
    'archived_state' => 'admin/config/workflow/workflows/manage/editorial/state/archived/delete',
    'editorial_workflow' => 'admin/config/workflow/workflows/manage/editorial/delete',
  ];
  $messages = [
    'archived_state' => 'This workflow state is in use. You cannot remove this workflow state until you have removed all content using it.',
    'editorial_workflow' => 'This workflow is in use. You cannot remove this workflow until you have removed all content using it.',
  ];
  foreach ($paths as $path) {
    $this->drupalGet($path);
    $this->assertSession()
      ->buttonExists('Delete');
  }
  // Create new moderated content in draft.
  $this->drupalGet('node/add/moderated_content');
  $this->submitForm([
    'title[0][value]' => 'Some moderated content',
    'body[0][value]' => 'First version of the content.',
    'moderation_state[0][state]' => 'draft',
  ], 'Save');
  // The archived state is not used yet, so can still be deleted.
  $this->drupalGet($paths['archived_state']);
  $this->assertSession()
    ->buttonExists('Delete');
  // The workflow is being used, so can't be deleted.
  $this->drupalGet($paths['editorial_workflow']);
  $this->assertSession()
    ->buttonNotExists('Delete');
  $this->assertSession()
    ->statusCodeEquals(200);
  $this->assertSession()
    ->pageTextContains($messages['editorial_workflow']);
  $node = $this->drupalGetNodeByTitle('Some moderated content');
  $this->drupalGet('node/' . $node->id() . '/edit');
  $this->submitForm([
    'moderation_state[0][state]' => 'published',
  ], 'Save');
  $this->drupalGet('node/' . $node->id() . '/edit');
  $this->submitForm([
    'moderation_state[0][state]' => 'archived',
  ], 'Save');
  // Now the archived state is being used so it can not be deleted either.
  foreach ($paths as $type => $path) {
    $this->drupalGet($path);
    $this->assertSession()
      ->buttonNotExists('Delete');
    $this->assertSession()
      ->statusCodeEquals(200);
    $this->assertSession()
      ->pageTextContains($messages[$type]);
  }
}

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