You are here

public function PagePreviewTest::testPagePreviewWithRevisions in Drupal 8

Same name and namespace in other branches
  1. 9 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 429

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
    ->drupalPostForm('node/add/page', $edit, t('Preview'));

  // Check that the preview is displaying the title, body and term.
  $this
    ->assertTitle($edit[$title_key] . ' | Drupal');
  $this
    ->assertText($edit[$title_key], 'Title displayed.');
  $this
    ->assertText($edit[$body_key], 'Body displayed.');
  $this
    ->assertText($edit[$term_key], 'Term displayed.');

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

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

  // Save the node after coming back from the preview page so we can create a
  // pending revision for it.
  $this
    ->drupalPostForm(NULL, [], t('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');
}