nodeapi_example.test 7.5 KB

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