node_submit will reset $node->uid to '0' when $node->name is not set, which is a common occurrence when creating node programmatically. The can be seen in a number of module issue queues and forums support requests.

This patch fixes this issue.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

matt2000’s picture

FileSize
742 bytes

Here's a slightly better version of the patch that can potentially save us an unnecessary user_load().

One other difference from before: If we somehow get a node where the uid and the name disagree, then the uid now wins instead of the name.

Status: Needs review » Needs work

The last submitted patch failed testing.

matt2000’s picture

Status: Needs work » Needs review
FileSize
836 bytes

Here's a re-roll against HEAD. Advice was given on the dev list today about using node_submit() + node_save(), so this is still relevant.

sun’s picture

+++ modules/node/node.module	30 Oct 2009 19:53:08 -0000
@@ -897,12 +897,12 @@ function node_validate($node, $form = ar
- * Prepare node for save and allow modules to make changes.
+ * Prepare node for save by generating UID, if needed, and creation date.

Please revert this.

+++ modules/node/node.module	30 Oct 2009 19:53:08 -0000
@@ -897,12 +897,12 @@ function node_validate($node, $form = ar
-  if (user_access('administer nodes')) {
+  if (user_access('administer nodes') && !isset($node->uid)) {

Please add an inline comment right before the condition that does not repeat the condition, but explains the reasoning for the condition.

I'm on crack. Are you, too?

matt2000’s picture

Note #1:

But node_submit doesn't allow modules to make changes any more. That happens elsewhere. (node_save, etc) There's no more _invoke functions here. So the new doc describes what the function actually does, including the change introduced by this patch.

Note #2:

How about we bump up the comment that follows the line and append as

//Populate the "authored by" field with the User ID, when it is not already present.

sun’s picture

yes, we want to move + extend that comment instead. A note about the actual condition/reasoning wouldn't hurt. I mean, this issue talks about a problem in programmatic creation of nodes, and the existing condition in the code presumes that there is some magic around "administer nodes" permission involved, and somewhere, I saw you talking about a special case when $node->name is defined... So just adding another condition will increase the WTF. Hence, we need a proper inline comment here that properly explains why & when this is happening. To the point, no novel required.

Regarding 1), ok, if that is the case, then yeah, good catch.

matt2000’s picture

Ok, how's this?

sun’s picture

+++ modules/node/node.module	6 Nov 2009 18:07:27 -0000
@@ -897,13 +897,15 @@ function node_validate(stdClass $node, $
- * Prepare node for save and allow modules to make changes.
+ * Prepare node for save by generating UID, if needed, and creation date.

"Prepare node for saving by populating author and creation date."

+++ modules/node/node.module	6 Nov 2009 18:07:27 -0000
@@ -897,13 +897,15 @@ function node_validate(stdClass $node, $
-
...
+  

Trailing white-space introduced here.

+++ modules/node/node.module	6 Nov 2009 18:07:27 -0000
@@ -897,13 +897,15 @@ function node_validate(stdClass $node, $
+  // A user with administer nodes permission might assign the node author by

The permission name should be in quotes.

This review is powered by Dreditor.

matt2000’s picture

Done.

matt2000’s picture

fixed whitespace alter.

sun’s picture

Status: Needs review » Reviewed & tested by the community

Thanks!

Status: Reviewed & tested by the community » Needs work

The last submitted patch failed testing.

sun’s picture

Tests failed. :(

matt2000’s picture

Status: Needs work » Needs review
FileSize
1.04 KB

oops... ~:-S

Try this, testbot.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

looks good.

in another patch, i'd be up for a rename of node_submit(), or merge it into some other function. it is confusing with fapi submit handlers and all

webchick’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Let's get some tests for this. This definitely sounds like an edge case we're bound to break again someday.

And I don't think a rename of node_submit() at this stage makes sense. We've been confusing it with form submit handlers since D4.7, so another release won't kill us. Would be happy to discuss cleaning this entire node pseudo-hook mess up in D8 tho. :)

matt2000’s picture

Status: Needs work » Needs review
FileSize
1.73 KB

Is it OK to modify an existing test? Or does each bug require it's own test function? See attached for proposed solution.

sun’s picture

+++ modules/node/node.test	23 Nov 2009 20:28:32 -0000
@@ -841,6 +841,11 @@ class NodeSaveTestCase extends DrupalWeb
+    node_submit(&$node);

This should have led to an exception, because variables cannot be passed by reference. Please remove the ampersand from $node.

+++ modules/node/node.test	23 Nov 2009 20:28:32 -0000
@@ -841,6 +841,11 @@ class NodeSaveTestCase extends DrupalWeb
+    
+    // Verify UID, per #398110.
+    $this->assertTrue($node->uid == $this->web_user->uid, t('Function node_submit() preserves UID'));
+    

1) (minor) Trailing white-space here; blank lines should be blank.

2) The inline comment should properly describe what is being verified here; without issue number.

3) You want to use assertEqual() here.

This review is powered by Dreditor.

matt2000’s picture

Thanks. Here ya go.

Dries’s picture

+++ modules/node/node.module	24 Nov 2009 17:46:25 -0000
@@ -905,13 +905,15 @@ function node_validate($node, $form = ar
+  // User ID, unless we've already been provided a UID by other means.

Can we consistently use the same term (e.g. user ID) instead of UID?

+++ modules/node/node.test	24 Nov 2009 17:46:25 -0000
@@ -840,7 +840,11 @@ class NodeSaveTestCase extends DrupalWeb
+
+    // Verify that node_submit did not overwrite the UID.
+    $this->assertEqual($node->uid, $this->web_user->uid, t('Function node_submit() preserves UID'));
+

See above.

matt2000’s picture

So say we all.

Dries’s picture

Status: Needs review » Fixed

Committed to CVS HEAD. Thanks.

matt2000’s picture

Version: 7.x-dev » 6.x-dev
Status: Fixed » Patch (to be ported)

This needs to be fixed in D6 even more so, since more happens in node_submit() there. //TODO

clojel’s picture

Status: Patch (to be ported) » Active

In node_submit function (node.module,v 1.1179 2009/12/06 18:27:23)

  if (user_access('administer nodes') && !isset($node->uid)) {
    if ($account = user_load_by_name($node->name)) {
...

The statement !isset($node->uid) would be false by default.

  • For new node, $node->uid is default to $user->uid in node_object_prepare().
  • For existing node, $node->uid would have been populated in node_form().

As a result, even with administer nodes access, user cannot assign alternative author from node form. Please review...

clojel’s picture

Status: Active » Needs review
matt2000’s picture

Version: 6.x-dev » 7.x-dev
Priority: Normal » Critical
Status: Needs review » Needs work
Issue tags: +broken

Hmm. How did that slip by?

it looks like there's quite a bit of redundancy between node_submit() and node_object_prepare(). As per #15 above, maybe it's time to retire node_submit altogether, and do the name conversion in node_form_submit() ?

Then the new recommended procedure for programmatically creating new nodes apart from drupal_execute() would be

$node = new stdClass;
$node = node_object_prepare($node);
$node->my_custom_alterations = $stuff;
node_save($node);
matt2000’s picture

Status: Needs work » Needs review
FileSize
3.02 KB

The bug this patch introduced was reported as #659962: Nodes with a $node->uid assigned can not have author changed.

This patch removes node_submit entirely, and moves it's remaining functionality into node_form_submit_build_node, which is the only function that called it anyway, and which is clearly identified as being related to FORM submission, so as to avoid confusion about programmatically created nodes.

it removes the test created for the old patch, but doesn't provide any new tests... yet. So this is just for testbot to munch on, and to get community feedback on the concept.

Status: Needs review » Needs work

The last submitted patch, , failed testing.

Dave Reid’s picture

Status: Needs work » Needs review

My bad.

Re-test of from comment #2404258 was requested by @user.

Status: Needs review » Needs work

The last submitted patch, 398110_node_submit.goodbye.patch, failed testing.

matt2000’s picture

Status: Needs work » Needs review
FileSize
3.68 KB

This should fix the old test. A new test will come tomorrow.

matt2000’s picture

Here it is with 2 new tests to make sure we don't see this issue or #659962: Nodes with a $node->uid assigned can not have author changed again.

Status: Needs review » Needs work
Issue tags: -Needs tests, -broken

The last submitted patch, 398110_node_submit.goodbye.with-tests.patch, failed testing.

Status: Needs work » Needs review
Issue tags: +Needs tests, +broken

Re-test of 398110_node_submit.goodbye.with-tests.patch from comment #34 was requested by aspilicious.

chx’s picture

Status: Needs review » Reviewed & tested by the community

OMG everyone's hated node_submit dies. Quick commit soon before it resurrects.

sun’s picture

Status: Reviewed & tested by the community » Needs review
+++ modules/node/node.pages.inc	8 Jan 2010 18:17:09 -0000
+++ modules/node/node.pages.inc	8 Jan 2010 18:17:09 -0000
@@ -453,7 +453,23 @@ function node_form_submit_build_node($fo

Actually, I'm not very happy of this move... this is the god-forsaken #builder_function for nodes, and we all know what we don't know, but we all know that we want to eliminate those #builder_functions entirely, because literally no one on this earth really groks that Form API + Field API integration _entirely_.

For example, this builder function is also invoked for AJAX submits and elsewhere. Intended?

+++ modules/node/node.pages.inc	8 Jan 2010 18:17:09 -0000
@@ -453,7 +453,23 @@ function node_form_submit_build_node($fo
+  
...
+  
...
+  

+++ modules/node/node.test	8 Jan 2010 18:17:10 -0000
@@ -245,6 +245,17 @@ class PageEditTestCase extends DrupalWeb
+    
...
+    

Trailing white-space here.

5 days to code freeze. Better review yourself.

matt2000’s picture

All I did was take the code form node_submit and put it into the one and only function in core that still called node submit. The issue really has nothing to do with builder functions. It has everything to do with avoiding confusion with (form)_submit functions, which this solves.

sun.core’s picture

Status: Needs review » Needs work

It's too late for more. We need to fix #24 and be done with it.

David_Rothstein’s picture

Status: Needs work » Fixed

Looks like this was all fixed by the latest commit at #492186: Authoring information is never updated., although the bug report here was older.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

matt2000’s picture

Version: 7.x-dev » 6.x-dev
Status: Closed (fixed) » Patch (to be ported)

Did the original issue ever get fixed in 6.x?

langworthy’s picture

The problem described in OP just bit me using Drupal 6.19

dpearcefl’s picture

Status: Patch (to be ported) » Postponed (maintainer needs more info)

Has this issue been fixed in the latest D6?

sun’s picture

Status: Postponed (maintainer needs more info) » Patch (to be ported)
Issue tags: -broken
Albert Volkman’s picture

Status: Patch (to be ported) » Needs review
FileSize
706 bytes

D6 backport.

rjbrown99’s picture

#47 does this still suffer from the issue raised in #24? After that approach was rolled to D7 there was this issue that came up: #659962: Nodes with a $node->uid assigned can not have author changed. D7 seems to have been fixed here. For D6, perhaps this would be the simplest approach.

jcisio’s picture

Issue summary: View changes
Status: Needs review » Closed (duplicate)

#47 does not work as we could see in D7. So I continue in #492186: Authoring information is never updated. with a backported patch similar to the D6 patch pointed out in #48, but we keep the code in sync with D7.