nodeapi_example.test

  1. examples
    1. 6 nodeapi_example/nodeapi_example.test
    2. 7 nodeapi_example/nodeapi_example.test
    3. 8 nodeapi_example/nodeapi_example.test

Test case for Testing the node API example module.

This file contains the test cases to check if module is performing as expected.

Classes

NameDescription
NodeApiExampleTestCase@file Test case for Testing the node API example module.

File

nodeapi_example/nodeapi_example.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Test case for Testing the node API example module.
  5. *
  6. * This file contains the test cases to check if module is performing as
  7. * expected.
  8. */
  9. class NodeApiExampleTestCase extends DrupalWebTestCase {
  10. /**
  11. * User object to perform site browsing
  12. * @var object
  13. */
  14. protected $web_user;
  15. /**
  16. * Content type to attach the rating system
  17. * @var string
  18. */
  19. protected $type;
  20. public static function getInfo() {
  21. return array(
  22. 'name' => 'Node API example functionality',
  23. 'description' => 'Demonstrate Node API hooks that allow altering a node. These are the former hook_nodeapi.',
  24. 'group' => 'Examples',
  25. );
  26. }
  27. /**
  28. * Enables modules and create user with specific permissions.
  29. */
  30. function setUp() {
  31. parent::setUp('nodeapi_example');
  32. // Create admin user. This module has no access control, so we can use a
  33. // trusted user. Revision access and revert permissions are required too.
  34. $this->web_user = $this->drupalCreateUser(array(
  35. 'administer nodes', // Required to set revision checkbox
  36. 'administer content types',
  37. 'bypass node access',
  38. 'view revisions',
  39. 'revert revisions'
  40. ));
  41. // Login the admin user.
  42. $this->drupalLogin($this->web_user);
  43. }
  44. /**
  45. * Log user in, creates an example node, and uses the rating system.
  46. */
  47. function testNodeExampleBasic() {
  48. // Login the user.
  49. $this->drupalLogin($this->web_user);
  50. // Create custom content type
  51. $content_type = $this->drupalCreateContentType();
  52. $type = $content_type->type;
  53. // Go to edit the settings of this content type
  54. $this->drupalGet('admin/structure/types/manage/' . $type);
  55. $this->assertResponse(200);
  56. // Check if the new Rating options appear in the settings page
  57. $this->assertText(t('NodeAPI Example Rating'), t('Rating options found in content type.'));
  58. $this->assertFieldByName('nodeapi_example_node_type', 1, t('Rating is Disabled by default.'));
  59. // Disable the rating for this content type: 0 for Disabled, 1 for Enabled.
  60. $content_settings = array(
  61. 'nodeapi_example_node_type' => 0,
  62. );
  63. $this->drupalPost('admin/structure/types/manage/' . $type, $content_settings, t('Save content type'));
  64. $this->assertResponse(200);
  65. $this->assertRaw(' has been updated.', t('Settings modified successfully for content type.'));
  66. // Create an example node
  67. $langcode = LANGUAGE_NONE;
  68. $edit = array(
  69. "title" => $this->randomName(),
  70. );
  71. $this->drupalPost('node/add/' . $type, $edit, t('Save'));
  72. $this->assertResponse(200);
  73. // Check that the rating is not shown, as we have not yet enabled it
  74. $this->assertNoRaw('Rating: <em>', t('Extended rating information is not shown.'));
  75. // Save current current url (we are viewing the new node)
  76. $node_url = $this->getUrl();
  77. // Enable the rating for this content type: 0 for Disabled, 1 for Enabled.
  78. $content_settings = array(
  79. 'nodeapi_example_node_type' => TRUE,
  80. );
  81. $this->drupalPost('admin/structure/types/manage/' . $type, $content_settings, t('Save content type'));
  82. $this->assertResponse(200);
  83. $this->assertRaw(' has been updated.', t('Settings modified successfully for content type.'));
  84. // Check previously create node. It should be not rated
  85. $this->drupalGet($node_url);
  86. $this->assertResponse(200);
  87. $this->assertRaw(t('Rating: %rating', array('%rating' => t('Unrated'))), t('Content is not rated.'));
  88. // Rate the content, 4 is for "Good"
  89. $rate = array(
  90. 'nodeapi_example_rating' => 4,
  91. );
  92. $this->drupalPost($node_url . '/edit', $rate, t('Save'));
  93. $this->assertResponse(200);
  94. // Check that content has been rated
  95. $this->assertRaw(t('Rating: %rating', array('%rating' => t('Good'))), t('Content is successfully rated.'));
  96. }
  97. /**
  98. * Logs user in, creates an example node, and tests rating functionality with
  99. * a node using revisions.
  100. */
  101. function testNodeExampleRevision() {
  102. // Login the user.
  103. $this->drupalLogin($this->web_user);
  104. // Create custom content type
  105. $content_type = $this->drupalCreateContentType();
  106. $type = $content_type->type;
  107. // Go to edit the settings of this content type
  108. $this->drupalGet('admin/structure/types/manage/' . $type);
  109. $this->assertResponse(200);
  110. // Check if the new Rating options appear in the settings page
  111. $this->assertText(t('NodeAPI Example Rating'), t('Rating options found in content type.'));
  112. $this->assertFieldByName('nodeapi_example_node_type', 1, t('Rating is Disabled by default.'));
  113. // Disable the rating for this content type: 0 for Disabled, 1 for Enabled.
  114. $content_settings = array(
  115. 'nodeapi_example_node_type' => 0,
  116. );
  117. $this->drupalPost('admin/structure/types/manage/' . $type, $content_settings, t('Save content type'));
  118. $this->assertResponse(200);
  119. $this->assertRaw(' has been updated.', t('Settings modified successfully for content type.'));
  120. // Create an example node
  121. $langcode = LANGUAGE_NONE;
  122. $edit = array(
  123. "title" => $this->randomName(),
  124. );
  125. $this->drupalPost('node/add/' . $type, $edit, t('Save'));
  126. $this->assertResponse(200);
  127. // Check that the rating is not shown, as we have not yet enabled it
  128. $this->assertNoRaw('Rating: <em>', t('Extended rating information is not shown.'));
  129. // Save current current url (we are viewing the new node)
  130. $node_url = $this->getUrl();
  131. // Enable the rating for this content type: 0 for Disabled, 1 for Enabled.
  132. $content_settings = array(
  133. 'nodeapi_example_node_type' => TRUE,
  134. );
  135. $this->drupalPost('admin/structure/types/manage/' . $type, $content_settings, t('Save content type'));
  136. $this->assertResponse(200);
  137. $this->assertRaw(' has been updated.', t('Settings modified successfully for content type.'));
  138. // Check previously create node. It should be not rated
  139. $this->drupalGet($node_url);
  140. $this->assertResponse(200);
  141. $this->assertRaw(t('Rating: %rating', array('%rating' => t('Unrated'))), t('Content is not rated.'));
  142. // Rate the content, 4 is for "Good"
  143. $rate = array(
  144. 'nodeapi_example_rating' => 4,
  145. );
  146. $this->drupalPost($node_url . '/edit', $rate, t('Save'));
  147. $this->assertResponse(200);
  148. // Check that content has been rated
  149. $this->assertRaw(t('Rating: %rating', array('%rating' => t('Good'))), t('Content is successfully rated.'));
  150. // Rate the content to poor using a new revision, 1 is for "Poor"
  151. $rate = array(
  152. 'nodeapi_example_rating' => 1,
  153. 'revision' => 1,
  154. );
  155. $this->drupalPost($node_url . '/edit', $rate, t('Save'));
  156. $this->assertResponse(200);
  157. // Check that content has been rated
  158. $this->assertRaw(t('Rating: %rating', array('%rating' => t('Poor'))), t('Content is successfully rated.'));
  159. //Now switch back to previous revision of the node.
  160. $this->drupalGet($node_url . '/revisions');
  161. // There is only a revision, so it must work just clicking the first link..
  162. $this->clickLink('revert');
  163. $revert_form = $this->getUrl();
  164. $this->drupalPost($revert_form, array(), t('Revert'));
  165. // Go back to the node page.
  166. $this->drupalGet($node_url);
  167. $this->assertResponse(200);
  168. // Check that content has been rated
  169. $this->assertRaw(t('Rating: %rating', array('%rating' => t('Good'))), t('Content rating matches reverted revision.'));
  170. }
  171. }
Login or register to post comments