You are here

public function ModerationFormTest::testModerationForm in Drupal 8

Same name and namespace in other branches
  1. 9 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',
  ], t('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
    ->assertField('edit-new-state', 'The node view page has a moderation form.');

  // 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
    ->drupalPostForm($edit_path, [
    'body[0][value]' => 'Second version of the content.',
    'moderation_state[0][state]' => 'draft',
  ], t('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
    ->assertField('edit-new-state', 'The node view page has a moderation form.');

  // Preview the draft.
  $this
    ->drupalPostForm($edit_path, [
    'body[0][value]' => 'Second version of the content.',
    'moderation_state[0][state]' => 'draft',
  ], t('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
    ->assertUrl($preview_url);
  $this
    ->assertNoField('edit-new-state', 'The node preview page has no moderation form.');

  // 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
    ->drupalPostForm($edit_path, [
    'body[0][value]' => 'Third version of the content.',
    'moderation_state[0][state]' => 'published',
  ], t('Save'));

  // Check widget default value.
  $this
    ->drupalGet($edit_path);
  $this
    ->assertFieldByName('moderation_state[0][state]', 'published', 'The moderation default value is set correctly.');

  // 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
    ->assertNoField('edit-new-state', 'The node view page has no moderation form.');

  // 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
    ->drupalPostForm($edit_path, [
    'body[0][value]' => 'Fourth version of the content.',
    'moderation_state[0][state]' => 'draft',
  ], t('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
    ->assertNoField('edit-new-state', 'The node view page has no moderation form.');

  // 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
    ->assertField('edit-new-state', 'The latest-version page has a moderation form.');
  $this
    ->assertText('Draft', 'Correct status found on the latest-version page.');

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

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