1 node.test PageEditTestCase::testPageAuthoredBy()

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

File

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

Class

PageEditTestCase
Tests the node edit functionality.

Code

function testPageAuthoredBy() {
  $this->backdropLogin($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->backdropPost('node/add/page', $edit, t('Save'));

  // Check that the node was authored by the currently logged in user.
  $node = $this->backdropGetNodeByTitle($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->backdropPost('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->backdropPost('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->backdropPost('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->backdropLogin($this->web_user);
  $this->backdropGet('node/' . $node->nid . '/edit');
  $this->assertNoFieldByName('name');
}