Provide helper function for creating nodes.

cwgordon7 - December 25, 2007 - 17:17
Project:SimpleTest
Version:6.x-1.x-dev
Component:Code
Category:task
Priority:normal
Assigned:cwgordon7
Status:closed
Description

Attached is a patch that would at a helper function to DrupalTestCase which would allow tests to easily create nodes.

AttachmentSize
drupal_test_case_create_node_02.patch3.03 KB

#1

cwgordon7 - December 25, 2007 - 17:25

Thanks dmitrig01, patch rerolled.

AttachmentSize
drupal_test_case_create_node_03.patch 3.07 KB

#2

cwgordon7 - December 25, 2007 - 18:08

The best patches are always shorter (thanks dmitrig01!)

AttachmentSize
drupal_test_case_create_node_05.patch 2.57 KB

#3

cwgordon7 - December 25, 2007 - 18:24

Oops, rerolled.

AttachmentSize
drupal_test_case_create_node_06.patch 2.67 KB

#4

cwgordon7 - December 26, 2007 - 22:08

Patch rerolled after discussion with chx.

AttachmentSize
drupal_test_case_create_node_07.patch 2.58 KB

#5

cwgordon7 - December 27, 2007 - 10:17

Here is a rerolled patch that includes revised .test files to use the new function.

AttachmentSize
simpletest_node_creation_01.patch 13.36 KB

#6

webchick - January 1, 2008 - 01:10

Oh, yes PLEASE! I'll try and test this a bit later.

#7

webchick - January 1, 2008 - 15:55

Ok, suggestions upon reading the patch. Haven't tried it yet.

+  function drupalCreateNode($settings = array(), $account = NULL, $mod_node = NULL) {

1. It seems to me that $account is a redundant argument. You could simply do $settings['uid'] = 4, no?
2. I don't think mod_node is appropriate here. If we want the ability to clone nodes, let's add a drupalCloneNode function.

I would do something like this instead:

+  function drupalCreateNode($properties = array()) {

Then I could call it like this:

<?php
$properties
= array(
 
'uid' => 4,
 
'type' => 'event',
 
'field_start_date' => '2007-12-20',
);
$node = $this->drupalCreateNode($properties);
?>

Otherwise, great addition, and it seems like this will remove TONS of code.

#8

cwgordon7 - January 5, 2008 - 02:06

In response to webchick:

It seems to me that $account is a redundant argument.

No, it's not. It's there for convenience; you can, of course, do 'uid' => 4, but if you pass in a user, the user's id as well as the user's name will be used. This is very convenient if you, say, have a user you want to have creating the node.

I don't think mod_node is appropriate here. If we want the ability to clone nodes, let's add a drupalCloneNode function.

You completely misunderstood the purpose of $mod_node; it's not a node to clone, but rather a node to resubmit, as you would do upon creating a revision.

The "TONS of code" you mentioned is there for convenience to the programmer, and I think it is acceptable.

-cwgordon7

#9

Rok Žlender - January 5, 2008 - 12:49
Status:needs review» needs work

No, it's not. It's there for convenience; you can, of course, do 'uid' => 4, but if you pass in a user, the user's id as well as the user's name will be used. This is very convenient if you, say, have a user you want to have creating the node.

I don't understand why you would need users name. Only thing saved in db is uid.

You completely misunderstood the purpose of $mod_node; it's not a node to clone, but rather a node to resubmit, as you would do upon creating a revision.

For creating new revision you could simply do

<?php

$properties
= array(
 
'uid' => 4,
 
'revision' => '1',
);
$node = $this->drupalCreateNode($properties);
?>

I think webchick didn't have your tons of code in mind but rather tons of code in tests that is replaced by simple call to drupalCreateNode.

#10

webchick - January 6, 2008 - 04:10

I think webchick didn't have your tons of code in mind but rather tons of code in tests that is replaced by simple call to drupalCreateNode.

Yep, that's right. Sorry about the miscommunication. And I still think simple is better on this function, although after looking more closely at the code, I'd change it to require specifying a node type as well; not all test environments can be guaranteed to have a 'page' type, and we ought to use the node type's defaults rather than some arbitrary defaults we define.

Working on an alternate patch..

#11

Rok Žlender - January 8, 2008 - 09:56

I had something like attached patch in mind. There is one problem with this. Node revisions uid is taken directly from global $user and not from $node->uid so only way to force it to use another user would be to change global $user temporarily which seems a bit ugly to me. If anyone has another idea...

AttachmentSize
simpletest_createNode.patch 8.58 KB

#12

webchick - January 10, 2008 - 04:33

Awesome. That is exactly what I had in mind too. :)

I don't have any thoughts atm about that revisions problem. Hrm.

#13

webchick - January 10, 2008 - 07:55

Well.

What about something like this (completely untested):

...
+    $node = ($settings + $defaults);
+    $node = (object)$node;
+
+    node_save($node);
+
+    // Compensate for node_revision's hard-coded $user->uid.
+    db_query('UPDATE {node_revision} SET uid = %d WHERE vid = %d', $node->uid, $node->vid);
...

Also, I think that 'type' should be a (required?) parameter into the function, rather than assuming that all nodes we want to create are pages. I can list off a dozen use cases for this off the top of my head, for example the OG's SimpleTest addOg function and Revision Moderation SimpleTest Tutorial (which it looks like I'll have to rewrite next week based on all the activity SimpleTest module's seen lately. That's a good thing. :D).

Additionally, although I haven't had a chance to test this patch yet, when I tried to do this myself I was running into issues with node type defaults (comments disabled, etc.) not being set properly. I got around this by doing a drupalPostRequest() or whatever that function is, to 'node/add/$type', which has the added bonus of testing your entire node save process. :) Not sure if this is the desired way to fix this, but just something to double-check as you're going through.

Thanks for picking this up again; sorry I dropped the ball.

#14

Rok Žlender - January 13, 2008 - 16:20

Committed to HEAD thanks cwgordon07 for your patch and webchick for reviews and ideas. New nodes content type can also be set in $properties sent to drupalCreateNode I tried with story and it worked.

#15

Rok Žlender - January 13, 2008 - 16:21
Status:needs work» fixed

#16

webchick - January 20, 2008 - 19:01
Status:fixed» patch (to be ported)

#17

boombatower - March 18, 2008 - 00:13
Status:patch (to be ported)» fixed

Doesn't seem anyone is porting and I'm not sure what it is to be ported to.

#18

Anonymous (not verified) - April 1, 2008 - 00:22
Status:fixed» closed

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

 
 

Drupal is a registered trademark of Dries Buytaert.