You are here

function PageEditTestCase::testPageAuthoredBy in Drupal 7

Tests changing a node's "authored by" field.

File

modules/node/node.test, line 404
Tests for node.module.

Class

PageEditTestCase
Tests the node edit functionality.

Code

function testPageAuthoredBy() {
  $this
    ->drupalLogin($this->admin_user);

  // Create node to edit.
  $langcode = LANGUAGE_NONE;
  $body_key = "body[{$langcode}][0][value]";
  $edit = array();
  $edit['title'] = $this
    ->randomName(8);
  $edit[$body_key] = $this
    ->randomName(16);
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));

  // Check that the node was authored by the currently logged in user.
  $node = $this
    ->drupalGetNodeByTitle($edit['title']);
  $this
    ->assertIdentical($node->uid, $this->admin_user->uid, 'Node authored by admin user.');

  // Try to change the 'authored by' field to an invalid user name.
  $edit = array(
    'name' => 'invalid-name',
  );
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  $this
    ->assertText('The username invalid-name does not exist.');

  // Change the authored by field to an empty string, which should assign
  // authorship to the anonymous user (uid 0).
  $edit['name'] = '';
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  $node = node_load($node->nid, NULL, TRUE);
  $this
    ->assertIdentical($node->uid, '0', 'Node authored by anonymous user.');

  // Change the authored by field to another user's name (that is not
  // logged in).
  $edit['name'] = $this->web_user->name;
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  $node = node_load($node->nid, NULL, TRUE);
  $this
    ->assertIdentical($node->uid, $this->web_user->uid, 'Node authored by normal user.');

  // Check that normal users cannot change the authored by information.
  $this
    ->drupalLogin($this->web_user);
  $this
    ->drupalGet('node/' . $node->nid . '/edit');
  $this
    ->assertNoFieldByName('name');
}