1 node.test PagePreviewTestCase::testPagePreview()

Checks the node preview functionality.

File

core/modules/node/tests/node.test, line 600
Tests for node.module.

Class

PagePreviewTestCase
Tests the node entity preview functionality.

Code

function testPagePreview() {
  $langcode = LANGUAGE_NONE;
  $title_key = "title";
  $body_key = "body[$langcode][0][value]";
  $term_key = "{$this->field_name}[$langcode]";
  $email_key = "{$this->field_email_name}[$langcode][0][email]";

  // Test turning off preview.
  $edit = array(
    'node_preview' => 0,
  );
  $this->backdropPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  // Refresh the field information for the rest of the test.
  field_info_cache_clear();
  $this->backdropGet('node/add/page');
  $this->assertNoFieldById('edit-preview', t('Preview'), 'Preview button not found.');

  // Turn it back on and test functionality.
  $edit = array(
    'node_preview' => 1,
  );
  $this->backdropPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  // Refresh the field information for the rest of the test.
  field_info_cache_clear();

  // 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->tid;
  $edit[$email_key] = 'email_key@example.com';
  $this->backdropPost('node/add/page', $edit, t('Preview'));

  // Check that the preview is displaying the title and body, but not term.
  $this->assertTitle(t('@title | Backdrop CMS', array('@title' => $edit[$title_key])), 'Basic page title is preview.');
  $this->assertLink(t('Back to content editing'));
  $this->assertText($edit[$title_key], 'Title displayed.');
  $this->assertText($edit[$body_key], 'Body displayed.');
  $this->assertText($edit[$email_key], 'Email displayed.');
  $this->assertNoText($this->term->name, 'Term not displayed.');

  // Change the view mode.
  $teaser_edit = array();
  $teaser_edit['view_mode'] = 'teaser';
  $this->backdropPost(NULL, $teaser_edit, t('Switch'));

  // Check that the preview is displaying the title, body, and term.
  $this->assertText($edit[$title_key], 'Title displayed.');
  $this->assertText($edit[$body_key], 'Body displayed.');
  $this->assertNoText($edit[$email_key], 'Email not displayed.');
  $this->assertText($this->term->name, 'Term displayed.');

  // Return to the node edit page and check field values.
  $this->clickLink('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.');
  $this->assertFieldByName($email_key, $edit[$email_key], 'Email field displayed.');

  // Return to preview and save.
  $this->backdropPost(NULL, $edit, t('Preview'));
  $this->assertText($edit[$title_key], 'Title displayed.');
  $this->assertText($edit[$body_key], 'Body displayed.');
  $this->assertText($edit[$email_key], 'Email displayed.');
  $this->assertNoText($this->term->name, 'Term not displayed.');

  $this->backdropPost(NULL, array(), t('Save'));

  $this->assertRaw(t('!post %title has been created.', array('!post' => 'Page', '%title' => $edit["title"])), 'Page created.');
  $this->assertText($edit[$title_key], 'Title displayed.');
  $this->assertText($edit[$body_key], 'Body displayed.');
  $this->assertText($edit[$email_key], 'Email displayed.');
  $this->assertNoText($this->term->name, 'Term not displayed.');

  // Edit an existing node to check preview works.
  $this->clickLink('Edit');
  $this->backdropPost(NULL, $edit, t('Preview'));
  $this->assertText($edit[$title_key], 'Title displayed.');
  $this->assertText($edit[$body_key], 'Body displayed.');
  $this->assertText($edit[$email_key], 'Email displayed.');
  $this->assertNoText($this->term->name, 'Term not displayed.');

  // Login as a second user with path permissions.
  $web_user = $this->backdropCreateUser(array('bypass node access', 'administer nodes'));
  $this->backdropLogin($web_user);

  // Test node settings during preview.
  $edit = array();
  $name = $this->randomName(8);

  $edit[$title_key] = $name;
  $edit[$body_key] = $this->randomName(16);
  $edit['status'] = 0;
  $this->backdropPost('node/add/page', $edit, t('Preview'));

  $this->assertNoText('This Page is unpublished.', 'Page is not in draft state.');

  // Return to the node edit page and check field values.
  $this->clickLink('Back to content editing');

  $this->assertFieldByName('status', 0, 'Status is set to unpublished.');

  // Return to preview and save.
  $this->assertNoText('This Page is unpublished.', 'Page is not in draft state.');
  $this->backdropPost(NULL, array(), t('Save'));
  $node = $this->backdropGetNodeByTitle($name);
  $this->assertEqual($node->status, '0', 'The node is in the state Draft.');

  // Edit the node, check status then preview and save.
  $this->clickLink('Edit');
  $this->assertFieldByName('status', 0, 'Status is set to unpublished.');

  $name = $this->randomName(8);
  $edit = array();
  $edit[$title_key] = $name;
  $edit['status'] = 2;
  $edit['scheduled[date]'] = format_date(REQUEST_TIME + 360, 'custom', 'Y-m-d', $web_user->timezone);
  $edit['scheduled[time]'] = format_date(REQUEST_TIME + 360, 'custom', 'H:i:s', $web_user->timezone);
  $this->backdropPost(NULL, $edit, t('Preview'));
  $this->backdropPost(NULL, array(), t('Save'));
  $node = $this->backdropGetNodeByTitle($name, TRUE);
  $this->assertEqual($node->status, '0', 'The node is in the state Draft.');

  $this->assertNotEqual($node->scheduled, 0, 'The node has a non zero published date.');

  // Test that a user with permissions to create, but not edit, can still 
  // access preview.
  $no_edit_user = $this->backdropCreateUser(array(
    'create page content',
    'access content',
  ));
  $this->backdropLogin($no_edit_user);

  // Fill in node creation form and preview node.
  $name = $this->randomName(8);
  $edit = array();
  $edit[$title_key] = $name;
  $edit[$body_key] = $this->randomName(16);

  $this->backdropPost('node/add/page', $edit, t('Preview'));
  $this->assertText($edit[$title_key], 'Title displayed.');

  // Test that a user with permissions to edit, but not create, can still 
  // access preview.
  $no_create_user = $this->backdropCreateUser(array(
    'edit any page content',
    'access content',
  ));
  $this->backdropLogin($no_create_user);

  $name = $this->randomName(8);
  $node = $this->backdropCreateNode(array('type' => 'page', 'title' => $name));
  $this->backdropGet('node/' . $node->nid);
  $this->clickLink('Edit');
  $this->backdropPost(NULL, array(), t('Preview'));
  $this->assertText($name, 'Title displayed.');
  $url = $this->getUrl();

  // Test that a user with no create or edit permissions cannot access
  // preview.
  $no_create_edit_user = $this->backdropCreateUser(array(
    'access content',
  ));
  $this->backdropLogin($no_create_edit_user);

  $this->backdropGet($url);
  $this->assertResponse(403, 'Received 404 response from user attempting to directly access preview.');

}