Using Drupal SimpleTest To Test Nodes With CCK Values

13th December 2010

SimpleTest is a great way to test your modules, but it can take a bit of setting up. For every test that is run SimpleTest effectively has a fresh install of Drupal, so if you need to test a particular function you need to have everything in place before you run the test. I was recently testing a migration module that I had written and needed to create nodes with a variety of CCK fields, which is quite common in Drupal site setups. In order to test that a CCK field saves the data for a test you need to do the following in the setUp() method, remember that this all happens before any tests are run.

  1. Include the Content module along with any sub modules you might need (eg, Text, Number).
  2. Set up and save your content type.
  3. Set up and save each CCK field you need.
  4. Create and log in a user to run the test with.

CCK stores its fields as an associative array of items that detail how each field should be created and to create a field in CCK you need to pass this array to a function called content_field_instance_create(). This sounds straighforward enough, but this array is quite large and creating it correctly is definately be a lengthy process. Luckily, there is a short-cut to writing this array out by hand, which involves the CCK function content_fields().

Create your node type in the same way as you normally would inside the admin of your Drupal install, with all of the associated CCK fields. Make a note of all of the field names you want to test. For each field you want, use the content_fields() function in the following way.

drupal_set_message('' . var_export(content_fields('FIELD NAME', 'CONTENT TYPE NAME'), true) . '');

So, for a content type I have called "testtype" I have created a text field called "field_test_field", so I would call the function in the following way.

drupal_set_message('' . var_export(content_fields('field_test_field', 'testtype'), true) . '');

This function will print out a large array in the message area containing the definition of the field you have defined, so for the above example I would get the following:

  1. 'field_name' => 'field_test_field',
  2. 'type_name' => 'testtype',
  3. 'display_settings' =>
  4. 'label' =>
  5. 'format' => 'above',
  6. 'exclude' => 0,
  7. ),
  8. 'teaser' =>
  9. 'format' => 'default',
  10. 'exclude' => 0,
  11. ),
  12. 'full' =>
  13. 'format' => 'default',
  14. 'exclude' => 0,
  15. ),
  16. 4 =>
  17. 'format' => 'default',
  18. 'exclude' => 0,
  19. ),
  20. ),
  21. 'widget_active' => '1',
  22. 'type' => 'text',
  23. 'required' => '1',
  24. 'multiple' => '0',
  25. 'db_storage' => '1',
  26. 'module' => 'text',
  27. 'active' => '1',
  28. 'locked' => '0',
  29. 'columns' =>
  30. 'value' =>
  31. 'type' => 'text',
  32. 'size' => 'big',
  33. 'not null' => false,
  34. 'sortable' => true,
  35. 'views' => true,
  36. ),
  37. ),
  38. 'text_processing' => '0',
  39. 'max_length' => '',
  40. 'allowed_values' => '',
  41. 'allowed_values_php' => '',
  42. 'widget' =>
  43. 'rows' => 5,
  44. 'size' => '100',
  45. 'default_value' =>
  46. 0 =>
  47. 'value' => '',
  48. '_error_element' => 'default_value_widget][field_test_field][0][value',
  49. ),
  50. ),
  51. 'default_value_php' => NULL,
  52. 'label' => 'Test',
  53. 'weight' => '31',
  54. 'description' => '',
  55. 'type' => 'text_textfield',
  56. 'module' => 'text',
  57. ),
  58. )

Copy and paste this array definition into your code and assign it to a variable. You can then pass this variable into the content_field_instance_create() function to get CCK to build the field inside your node type.

  1. $field = array (
  2. 'field_name' => 'field_test_field',
  3. 'type_name' => 'testtype',
  4. 'display_settings' =>
  5. // snip....
  6. ),
  7. );
  8.  
  9. content_field_instance_create($field);

The code I have detailed above is useful for creating tests in Drupal SimpleTest, but it can also be used whenever you want to programmatically setup nodes in Drupal.

Before we can test this out we need do three things, the first is add the modules we need to run the tests, the second is to create our custom content type and finally we need create a user with enough privalges to create and view the nodes. With all this in place we now have a setUp() method that looks like the following:

  1. public function setUp() {
  2. // Load modules
  3. parent::setUp('content', 'text');
  4.  
  5. // create node type
  6. $this->drupalCreateContentType(array(
  7. 'type' => 'testtype',
  8. 'name' => 'testtype',
  9. ));
  10.  
  11. // Snipped out the custom content type creation...
  12.  
  13. // Create and log in our privileged user.
  14. $privileges = array(
  15. 'access content',
  16. 'create testtype content',
  17. 'edit any testtype content'
  18. );
  19. $this->privileged_user = $this->drupalCreateUser($privileges);
  20. $this->drupalLogin($this->privileged_user);
  21. }

Now we are ready to run some tests on the CCK field. The following is a test method that creates a node and then makes sure that the data entered is the data retrieved on page. Once this is in place you can run your test from the SimpleTest admin interface.

  1. /**
  2.   * Add a node and test that the content has been added.
  3.   */
  4. public function testCreateTestNode() {
  5.  
  6. // Create node content
  7. $node_array = array(
  8. 'title' => 'Node title',
  9. 'body' => 'Node body',
  10. 'field_test_field[0][value]' => 'wibble',
  11. );
  12.  
  13. // Save node
  14. $this->drupalPost('node/add/testtype', $node_array, t('Save'));
  15.  
  16. // Test deault fields
  17. $this->assertRaw('Node title', t('Title content found.'));
  18. $this->assertRaw('Node body', t('Body content found.'));
  19.  
  20. // Test CCK field content.
  21. $this->assertRaw('wibble', t('Test field content found.'));
  22. }

This won't always work in this way, especially if you have set your fields to not display on the node page. In which case you can load the node directly and change the assertRaw() calls to assertEquals() in order to test.

Note that you can't use the drupalCreateNode() method here to create your node as it only accepts the default node attributes and won't save any CCK data that you give it.

Here is the full test class.

  1. drupalCreateContentType(array(
  2. 'type' => 'testtype',
  3. 'name' => 'testtype',
  4. ));
  5.  
  6. $field = array(
  7. 'field_name' => 'field_test_field',
  8. 'type_name' => 'testtype',
  9. 'display_settings' =>
  10. 'label' =>
  11. 'format' => 'above',
  12. 'exclude' => 0,
  13. ),
  14. 'teaser' =>
  15. 'format' => 'default',
  16. 'exclude' => 0,
  17. ),
  18. 'full' =>
  19. 'format' => 'default',
  20. 'exclude' => 0,
  21. ),
  22. 4 =>
  23. 'format' => 'default',
  24. 'exclude' => 0,
  25. ),
  26. ),
  27. 'widget_active' => '1',
  28. 'type' => 'text',
  29. 'required' => '1',
  30. 'multiple' => '0',
  31. 'db_storage' => '1',
  32. 'module' => 'text',
  33. 'active' => '1',
  34. 'locked' => '0',
  35. 'columns' =>
  36. 'value' =>
  37. 'type' => 'text',
  38. 'size' => 'big',
  39. 'not null' => false,
  40. 'sortable' => true,
  41. 'views' => true,
  42. ),
  43. ),
  44. 'text_processing' => '0',
  45. 'max_length' => '',
  46. 'allowed_values' => '',
  47. 'allowed_values_php' => '',
  48. 'widget' =>
  49. 'rows' => 5,
  50. 'size' => '100',
  51. 'default_value' =>
  52. 0 =>
  53. 'value' => '',
  54. '_error_element' => 'default_value_widget][field_test_field][0][value',
  55. ),
  56. ),
  57. 'default_value_php' => NULL,
  58. 'label' => 'Test',
  59. 'weight' => '31',
  60. 'description' => '',
  61. 'type' => 'text_textfield',
  62. 'module' => 'text',
  63. ),
  64. );
  65.  
  66. content_field_instance_create($field);
  67.  
  68. // Create and log in our privileged user.
  69. $privileges = array(
  70. 'access content',
  71. 'create testtype content',
  72. 'edit any testtype content'
  73. );
  74. $this->privileged_user = $this->drupalCreateUser($privileges);
  75. $this->drupalLogin($this->privileged_user);
  76. }
  77.  
  78. /**
  79.   * Create an array of options for the simpletest module.
  80.   *
  81.   * @return array The array of options
  82.   */
  83. public function getInfo() {
  84. return array(
  85. 'name' => t('Test Module Insert Node Test'),
  86. 'description' => t('Test to insert a node with CCK fields.'),
  87. 'group' => t('Test Module'),
  88. );
  89. }
  90.  
  91. /**
  92.   * Add a node and test that the content has been added.
  93.   */
  94. public function testCreateTestNode() {
  95.  
  96. // Create node content
  97. $node_array = array(
  98. 'title' => 'Node title',
  99. 'body' => 'Node body',
  100. 'field_test_field[0][value]' => 'wibble',
  101. );
  102.  
  103. // Save node
  104. $this->drupalPost('node/add/testtype', $node_array, t('Save'));
  105.  
  106. // Test deault fields
  107. $this->assertRaw('Node title', t('Title content found.'));
  108. $this->assertRaw('Node body', t('Body content found.'));
  109.  
  110. // Test CCK field content.
  111. $this->assertRaw('wibble', t('Test field content found.'));
  112. }
  113.  
  114. }

Comments

Permalink
Thanks a million. I was banging my head on the wall trying to figure this out. This post helped me a lot.

Submitted by a on Fri, 06/19/2015 - 21:00

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.