You are here

public function ModerationFormTest::testModerationForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/content_moderation/tests/src/Functional/ModerationFormTest.php \Drupal\Tests\content_moderation\Functional\ModerationFormTest::testModerationForm()

Tests the moderation form that shows on the latest version page.

The latest version page only shows if there is a pending revision.

See also

\Drupal\content_moderation\EntityOperations

\Drupal\Tests\content_moderation\Functional\ModerationStateBlockTest::testCustomBlockModeration

File

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

Class

ModerationFormTest
Tests the moderation form, specifically on nodes.

Namespace

Drupal\Tests\content_moderation\Functional

Code

public function testModerationForm() {

  // Test the states that appear by default when creating a new item of
  // content.
  $this
    ->drupalGet('node/add/moderated_content');
  $this
    ->assertSession()
    ->optionExists('moderation_state[0][state]', 'draft');
  $this
    ->assertSession()
    ->optionExists('moderation_state[0][state]', 'published');
  $this
    ->assertSession()
    ->optionNotExists('moderation_state[0][state]', 'archived');

  // Previewing a new item of content should not change the available states.
  $this
    ->submitForm([
    'moderation_state[0][state]' => 'published',
    'title[0][value]' => 'Some moderated content',
    'body[0][value]' => 'First version of the content.',
  ], 'Preview');
  $this
    ->clickLink('Back to content editing');
  $this
    ->assertSession()
    ->optionExists('moderation_state[0][state]', 'draft');
  $this
    ->assertSession()
    ->optionExists('moderation_state[0][state]', 'published');
  $this
    ->assertSession()
    ->optionNotExists('moderation_state[0][state]', 'archived');

  // Create new moderated content in draft.
  $this
    ->submitForm([
    'moderation_state[0][state]' => 'draft',
  ], 'Save');
  $node = $this
    ->drupalGetNodeByTitle('Some moderated content');
  $canonical_path = sprintf('node/%d', $node
    ->id());
  $edit_path = sprintf('node/%d/edit', $node
    ->id());
  $latest_version_path = sprintf('node/%d/latest', $node
    ->id());
  $this
    ->assertTrue($this->adminUser
    ->hasPermission('edit any moderated_content content'));

  // The canonical view should have a moderation form, because it is not the
  // live revision.
  $this
    ->drupalGet($canonical_path);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->fieldExists('edit-new-state');

  // The latest version page should not show, because there is no pending
  // revision.
  $this
    ->drupalGet($latest_version_path);
  $this
    ->assertSession()
    ->statusCodeEquals(403);

  // Update the draft.
  $this
    ->drupalGet($edit_path);
  $this
    ->submitForm([
    'body[0][value]' => 'Second version of the content.',
    'moderation_state[0][state]' => 'draft',
  ], 'Save');

  // The canonical view should have a moderation form, because it is not the
  // live revision.
  $this
    ->drupalGet($canonical_path);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->fieldExists('edit-new-state');

  // Preview the draft.
  $this
    ->drupalGet($edit_path);
  $this
    ->submitForm([
    'body[0][value]' => 'Second version of the content.',
    'moderation_state[0][state]' => 'draft',
  ], 'Preview');

  // The preview view should not have a moderation form.
  $preview_url = Url::fromRoute('entity.node.preview', [
    'node_preview' => $node
      ->uuid(),
    'view_mode_id' => 'full',
  ]);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->addressEquals($preview_url);
  $this
    ->assertSession()
    ->fieldNotExists('edit-new-state');

  // The latest version page should not show, because there is still no
  // pending revision.
  $this
    ->drupalGet($latest_version_path);
  $this
    ->assertSession()
    ->statusCodeEquals(403);

  // Publish the draft.
  $this
    ->drupalGet($edit_path);
  $this
    ->submitForm([
    'body[0][value]' => 'Third version of the content.',
    'moderation_state[0][state]' => 'published',
  ], 'Save');

  // Check widget default value.
  $this
    ->drupalGet($edit_path);
  $this
    ->assertSession()
    ->fieldValueEquals('moderation_state[0][state]', 'published');

  // Preview the content while selecting the "draft" state and when the user
  // returns to the edit form, ensure all of the available transitions are
  // still those available from the "published" source state.
  $this
    ->submitForm([
    'moderation_state[0][state]' => 'draft',
  ], 'Preview');
  $this
    ->clickLink('Back to content editing');
  $this
    ->assertSession()
    ->optionExists('moderation_state[0][state]', 'draft');
  $this
    ->assertSession()
    ->optionExists('moderation_state[0][state]', 'published');
  $this
    ->assertSession()
    ->optionExists('moderation_state[0][state]', 'archived');

  // The published view should not have a moderation form, because it is the
  // live revision.
  $this
    ->drupalGet($canonical_path);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->fieldNotExists('edit-new-state');

  // The latest version page should not show, because there is still no
  // pending revision.
  $this
    ->drupalGet($latest_version_path);
  $this
    ->assertSession()
    ->statusCodeEquals(403);

  // Make a pending revision.
  $this
    ->drupalGet($edit_path);
  $this
    ->submitForm([
    'body[0][value]' => 'Fourth version of the content.',
    'moderation_state[0][state]' => 'draft',
  ], 'Save');

  // The published view should not have a moderation form, because it is the
  // live revision.
  $this
    ->drupalGet($canonical_path);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->fieldNotExists('edit-new-state');

  // The latest version page should show the moderation form and have "Draft"
  // status, because the pending revision is in "Draft".
  $this
    ->drupalGet($latest_version_path);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->fieldExists('edit-new-state');
  $this
    ->assertSession()
    ->pageTextContains('Draft');

  // Submit the moderation form to change status to published.
  $this
    ->drupalGet($latest_version_path);
  $this
    ->submitForm([
    'new_state' => 'published',
  ], 'Apply');

  // The latest version page should not show, because there is no
  // pending revision.
  $this
    ->drupalGet($latest_version_path);
  $this
    ->assertSession()
    ->statusCodeEquals(403);
}