diff options
-rw-r--r-- | modules/node/node.module | 6 | ||||
-rw-r--r-- | modules/node/node.pages.inc | 13 | ||||
-rw-r--r-- | modules/node/node.test | 54 |
3 files changed, 56 insertions, 17 deletions
diff --git a/modules/node/node.module b/modules/node/node.module index d54f75e0a..1bbeefaa3 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -931,9 +931,8 @@ function node_submit($node) { global $user; // A user might assign the node author by entering a user name in the node - // form, which we then need to translate to a user ID, unless we've already - // been provided a user ID by other means. - if (!empty($node->name) && !isset($node->uid)) { + // form, which we then need to translate to a user ID. + if (isset($node->name)) { if ($account = user_load_by_name($node->name)) { $node->uid = $account->uid; } @@ -941,6 +940,7 @@ function node_submit($node) { $node->uid = 0; } } + $node->created = !empty($node->date) ? strtotime($node->date) : REQUEST_TIME; $node->validated = TRUE; diff --git a/modules/node/node.pages.inc b/modules/node/node.pages.inc index a66352bf2..e5167805c 100644 --- a/modules/node/node.pages.inc +++ b/modules/node/node.pages.inc @@ -230,12 +230,9 @@ function node_form($form, &$form_state, $node) { '#title' => t('Authored on'), '#maxlength' => 25, '#description' => t('Format: %time. The date format is YYYY-MM-DD and %timezone is the timezone offset from UTC. Leave blank to use the time of form submission.', array('%time' => !empty($node->date) ? $node->date : format_date($node->created, 'custom', 'Y-m-d H:i:s O'), '%timezone' => !empty($node->date) ? $node->date : format_date($node->created, 'custom', 'O'))), + '#default_value' => !empty($node->date) ? $node->date : '', ); - if (isset($node->date)) { - $form['author']['date']['#default_value'] = $node->date; - } - // Node options for administrators $form['options'] = array( '#type' => 'fieldset', @@ -265,14 +262,6 @@ function node_form($form, &$form_state, $node) { '#default_value' => $node->sticky, ); - // These values are used when the user has no administrator access. - foreach (array('uid', 'created') as $key) { - $form[$key] = array( - '#type' => 'value', - '#value' => $node->$key, - ); - } - // Add the buttons. $form['actions'] = array( '#type' => 'container', diff --git a/modules/node/node.test b/modules/node/node.test index 0d9b7e03b..8ef4e8eb8 100644 --- a/modules/node/node.test +++ b/modules/node/node.test @@ -212,6 +212,9 @@ class NodeRevisionsTestCase extends DrupalWebTestCase { } class PageEditTestCase extends DrupalWebTestCase { + protected $web_user; + protected $admin_user; + public static function getInfo() { return array( 'name' => 'Node edit', @@ -223,14 +226,16 @@ class PageEditTestCase extends DrupalWebTestCase { function setUp() { parent::setUp(); - $web_user = $this->drupalCreateUser(array('edit own page content', 'create page content')); - $this->drupalLogin($web_user); + $this->web_user = $this->drupalCreateUser(array('edit own page content', 'create page content')); + $this->admin_user = $this->drupalCreateUser(array('bypass node access', 'administer nodes')); } /** * Check node edit functionality. */ function testPageEdit() { + $this->drupalLogin($this->web_user); + $langcode = LANGUAGE_NONE; $title_key = "title"; $body_key = "body[$langcode][0][value]"; @@ -291,6 +296,51 @@ class PageEditTestCase extends DrupalWebTestCase { $second_node_version = node_load($node->nid, $revised_node->vid); $this->assertNotIdentical($first_node_version->revision_uid, $second_node_version->revision_uid, 'Each revision has a distinct user.'); } + + /** + * Check changing node authored by fields. + */ + 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'); + } } class PagePreviewTestCase extends DrupalWebTestCase { |