function PagePreviewTest::testPagePreview

Checks the node preview functionality.

File

drupal/core/modules/node/lib/Drupal/node/Tests/PagePreviewTest.php, line 107
Definition of Drupal\node\Tests\PagePreviewTest.

Class

PagePreviewTest
Tests the node entity preview functionality.

Namespace

Drupal\node\Tests

Code

function testPagePreview() {
  $langcode = Language::LANGCODE_NOT_SPECIFIED;
  $title_key = "title";
  $body_key = "body[{$langcode}][0][value]";
  $term_key = "{$this->field_name}[{$langcode}]";

  // Fill in node creation form and preview node.
  $edit = array();
  $edit[$title_key] = $this
    ->randomName(8);
  $edit[$body_key] = $this
    ->randomName(16);
  $edit[$term_key] = $this->term
    ->label();
  $this
    ->drupalPost('node/add/page', $edit, t('Preview'));

  // Check that the preview is displaying the title, body and term.
  $this
    ->assertTitle(t('Preview | Drupal'), 'Basic page title is preview.');
  $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, body and term fields are displayed with the
  // correct values.
  $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.');

  // Save the node.
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));
  $node = $this
    ->drupalGetNodeByTitle($edit[$title_key]);

  // Check the term was displayed on the saved node.
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertText($edit[$term_key], 'Term displayed.');

  // Check the term appears again on the edit form.
  $this
    ->drupalGet('node/' . $node->nid . '/edit');
  $this
    ->assertFieldByName($term_key, $edit[$term_key], 'Term field displayed.');

  // Check with two new terms on the edit form, additionally to the existing
  // one.
  $edit = array();
  $newterm1 = $this
    ->randomName(8);
  $newterm2 = $this
    ->randomName(8);
  $edit[$term_key] = $this->term
    ->label() . ', ' . $newterm1 . ', ' . $newterm2;
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Preview'));
  $this
    ->assertRaw('>' . $newterm1 . '<', 'First new term displayed.');
  $this
    ->assertRaw('>' . $newterm2 . '<', 'Second new term displayed.');

  // The first term should be displayed as link, the others not.
  $this
    ->assertLink($this->term
    ->label());
  $this
    ->assertNoLink($newterm1);
  $this
    ->assertNoLink($newterm2);
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));

  // Check with one more new term, keeping old terms, removing the existing
  // one.
  $edit = array();
  $newterm3 = $this
    ->randomName(8);
  $edit[$term_key] = $newterm1 . ', ' . $newterm3 . ', ' . $newterm2;
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Preview'));
  $this
    ->assertRaw('>' . $newterm1 . '<', 'First existing term displayed.');
  $this
    ->assertRaw('>' . $newterm2 . '<', 'Second existing term displayed.');
  $this
    ->assertRaw('>' . $newterm3 . '<', 'Third new term displayed.');
  $this
    ->assertNoText($this->term
    ->label());
  $this
    ->assertNoLink($newterm1);
  $this
    ->assertNoLink($newterm2);
  $this
    ->assertNoLink($newterm3);
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));
}