You are here

public function PagePreviewTest::testPagePreviewWithRevisions in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/node/tests/src/Functional/PagePreviewTest.php \Drupal\Tests\node\Functional\PagePreviewTest::testPagePreviewWithRevisions()

Checks the node preview functionality, when using revisions.

File

core/modules/node/tests/src/Functional/PagePreviewTest.php, line 441

Class

PagePreviewTest
Tests the node entity preview functionality.

Namespace

Drupal\Tests\node\Functional

Code

public function testPagePreviewWithRevisions() {
  $title_key = 'title[0][value]';
  $body_key = 'body[0][value]';
  $term_key = $this->fieldName . '[target_id]';

  // Force revision on "Basic page" content.
  $node_type = NodeType::load('page');
  $node_type
    ->setNewRevision(TRUE);
  $node_type
    ->save();

  // Fill in node creation form and preview node.
  $edit = [];
  $edit[$title_key] = $this
    ->randomMachineName(8);
  $edit[$body_key] = $this
    ->randomMachineName(16);
  $edit[$term_key] = $this->term
    ->id();
  $edit['revision_log[0][value]'] = $this
    ->randomString(32);
  $this
    ->drupalGet('node/add/page');
  $this
    ->submitForm($edit, 'Preview');

  // Check that the preview is displaying the title, body and term.
  $this
    ->assertSession()
    ->titleEquals($edit[$title_key] . ' | Drupal');
  $this
    ->assertSession()
    ->pageTextContains($edit[$title_key]);
  $this
    ->assertSession()
    ->pageTextContains($edit[$body_key]);
  $this
    ->assertSession()
    ->pageTextContains($edit[$term_key]);

  // Check that the title and body fields are displayed with the correct
  // values after going back to the content edit page.
  $this
    ->clickLink('Back to content editing');
  $this
    ->assertSession()
    ->fieldValueEquals($title_key, $edit[$title_key]);
  $this
    ->assertSession()
    ->fieldValueEquals($body_key, $edit[$body_key]);
  $this
    ->assertSession()
    ->fieldValueEquals($term_key, $edit[$term_key]);

  // Check that the revision log field has the correct value.
  $this
    ->assertSession()
    ->fieldValueEquals('revision_log[0][value]', $edit['revision_log[0][value]']);

  // Save the node after coming back from the preview page so we can create a
  // pending revision for it.
  $this
    ->submitForm([], 'Save');
  $node = $this
    ->drupalGetNodeByTitle($edit[$title_key]);

  // Check that previewing a pending revision of a node works. This can not be
  // accomplished through the UI so we have to use API calls.
  // @todo Change this test to use the UI when we will be able to create
  // pending revisions in core.
  // @see https://www.drupal.org/node/2725533
  $node
    ->setNewRevision(TRUE);
  $node
    ->isDefaultRevision(FALSE);

  /** @var \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver */
  $controller_resolver = \Drupal::service('controller_resolver');
  $node_preview_controller = $controller_resolver
    ->getControllerFromDefinition('\\Drupal\\node\\Controller\\NodePreviewController::view');
  $node_preview_controller($node, 'full');
}