ModerationRevisionRevertTest.php 2.89 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Drupal\Tests\content_moderation\Functional;

use Drupal\simpletest\ContentTypeCreationTrait;
use Drupal\Tests\BrowserTestBase;
7
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
8 9 10 11 12 13 14 15 16

/**
 * Test revision revert.
 *
 * @group content_moderation
 */
class ModerationRevisionRevertTest extends BrowserTestBase {

  use ContentTypeCreationTrait;
17
  use ContentModerationTestTrait;
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = [
    'content_moderation',
    'node',
  ];

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    $moderated_bundle = $this->createContentType(['type' => 'moderated_bundle']);
    $moderated_bundle->save();

38
    $workflow = $this->createEditorialWorkflow();
39 40 41
    $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'moderated_bundle');
    $workflow->save();

42 43 44 45
    /** @var \Drupal\Core\Routing\RouteBuilderInterface $router_builder */
    $router_builder = $this->container->get('router.builder');
    $router_builder->rebuildIfNeeded();

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    $admin = $this->drupalCreateUser([
      'access content overview',
      'administer nodes',
      'bypass node access',
      'view all revisions',
      'use editorial transition create_new_draft',
      'use editorial transition publish',
    ]);
    $this->drupalLogin($admin);
  }

  /**
   * Test that reverting a revision works.
   */
  public function testEditingAfterRevertRevision() {
    // Create a draft.
62 63 64 65
    $this->drupalPostForm('node/add/moderated_bundle', [
      'title[0][value]' => 'First draft node',
      'moderation_state[0][state]' => 'draft',
    ], t('Save'));
66 67

    // Now make it published.
68 69 70 71
    $this->drupalPostForm('node/1/edit', [
      'title[0][value]' => 'Published node',
      'moderation_state[0][state]' => 'published',
    ], t('Save'));
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

    // Check the editing form that show the published title.
    $this->drupalGet('node/1/edit');
    $this->assertSession()
      ->pageTextContains('Published node');

    // Revert the first revision.
    $revision_url = 'node/1/revisions/1/revert';
    $this->drupalGet($revision_url);
    $this->assertSession()->elementExists('css', '.form-submit');
    $this->click('.form-submit');

    // Check that it reverted.
    $this->drupalGet('node/1/edit');
    $this->assertSession()
      ->pageTextContains('First draft node');
    // Try to save the node.
89 90 91
    $this->drupalPostForm('node/1/edit', [
      'moderation_state[0][state]' => 'draft',
    ], t('Save'));
92 93 94 95 96 97 98 99 100 101 102

    // Check if the submission passed the EntityChangedConstraintValidator.
    $this->assertSession()
      ->pageTextNotContains('The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.');

    // Check the node has been saved.
    $this->assertSession()
      ->pageTextContains('moderated_bundle First draft node has been updated');
  }

}