override_node_options.test 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * @file
  4. * Unit tests for the override_node_options module.
  5. */
  6. /**
  7. * Defines a base class for testing the Override Node Options module.
  8. */
  9. class OverrideNodeOptionsTestCase extends DrupalWebTestCase {
  10. /**
  11. * A standard user with basic permissions.
  12. *
  13. * @var stdClass
  14. */
  15. protected $normalUser;
  16. /**
  17. * A page node to test against.
  18. *
  19. * @var stdClass
  20. */
  21. protected $node;
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function getInfo() {
  26. return array(
  27. 'name' => 'Override node options',
  28. 'description' => 'Functional tests for overriding options on node forms.',
  29. 'group' => 'Override node options',
  30. );
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function setUp() {
  36. parent::setUp('override_node_options');
  37. $this->normalUser = $this->drupalCreateUser(array('create page content', 'edit any page content'));
  38. $this->node = $this->drupalCreateNode();
  39. }
  40. /**
  41. * Assert that fields in a node were updated to certain values.
  42. *
  43. * @param \stdClass $node
  44. * The node object to check (will be reloaded from the database).
  45. * @param array $fields
  46. * An array of values to check equality, keyed by node object property.
  47. */
  48. private function assertNodeFieldsUpdated(stdClass $node, array $fields) {
  49. // Re-load the node from the database to make sure we have the current
  50. // values.
  51. $node = node_load($node->nid, NULL, TRUE);
  52. foreach ($fields as $field => $value) {
  53. $this->assertEqual(
  54. $node->$field,
  55. $value,
  56. t('Node @field was updated to !value, expected !expected.', array(
  57. '@field' => $field,
  58. '!value' => var_export($node->$field, TRUE),
  59. '!expected' => var_export($value, TRUE),
  60. ))
  61. );
  62. }
  63. }
  64. /**
  65. * Assert that the user cannot access fields on node add and edit forms.
  66. *
  67. * @param \stdClass $node
  68. * The node object, will be used on the node edit form.
  69. * @param array $fields
  70. * An array of form fields to check.
  71. */
  72. private function assertNodeFieldsNoAccess(stdClass $node, array $fields) {
  73. $this->drupalGet("node/add/{$node->type}");
  74. foreach ($fields as $field) {
  75. $this->assertNoFieldByName($field);
  76. }
  77. $this->drupalGet("node/{$this->node->nid}/edit");
  78. foreach ($fields as $field) {
  79. $this->assertNoFieldByName($field);
  80. }
  81. }
  82. /**
  83. * Test the 'Authoring information' fieldset.
  84. */
  85. protected function testNodeOptions() {
  86. $specific_user = $this->drupalCreateUser(array(
  87. 'create page content',
  88. 'edit any page content',
  89. 'override page published option',
  90. 'override page promote to front page option',
  91. 'override page sticky option',
  92. 'override page comment setting option',
  93. ));
  94. $general_user = $this->drupalCreateUser(array(
  95. 'create page content',
  96. 'edit any page content',
  97. 'override all published option',
  98. 'override all promote to front page option',
  99. 'override all sticky option',
  100. 'override all comment setting option',
  101. ));
  102. foreach (array($specific_user, $general_user) as $account) {
  103. $this->drupalLogin($account);
  104. $fields = array(
  105. 'status' => (bool) !$this->node->status,
  106. 'promote' => (bool) !$this->node->promote,
  107. 'sticky' => (bool) !$this->node->sticky,
  108. 'comment' => COMMENT_NODE_OPEN,
  109. );
  110. $this->drupalPost("node/{$this->node->nid}/edit", $fields, t('Save'));
  111. $this->assertNodeFieldsUpdated($this->node, $fields);
  112. }
  113. $this->drupalLogin($this->normalUser);
  114. $this->assertNodeFieldsNoAccess($this->node, array_keys($fields));
  115. }
  116. /**
  117. * Test the 'Revision information' fieldset.
  118. */
  119. protected function testNodeRevisions() {
  120. $specific_user = $this->drupalCreateUser(array(
  121. 'create page content',
  122. 'edit any page content',
  123. 'override page revision option',
  124. ));
  125. $general_user = $this->drupalCreateUser(array(
  126. 'create page content',
  127. 'edit any page content',
  128. 'override all revision option',
  129. ));
  130. foreach (array($specific_user, $general_user) as $account) {
  131. $this->drupalLogin($account);
  132. // Ensure that we have the latest node data.
  133. $node = node_load($this->node->nid, NULL, TRUE);
  134. $fields = array(
  135. 'revision' => TRUE,
  136. );
  137. $this->drupalPost("node/{$node->nid}/edit", $fields, t('Save'));
  138. $this->assertNodeFieldsUpdated($node, array('vid' => $node->vid + 1));
  139. }
  140. $this->drupalLogin($this->normalUser);
  141. $this->assertNodeFieldsNoAccess($this->node, array_keys($fields));
  142. }
  143. /**
  144. * Test the 'Authoring information' fieldset.
  145. */
  146. protected function testNodeAuthor() {
  147. $specific_user = $this->drupalCreateUser(array(
  148. 'create page content',
  149. 'edit any page content',
  150. 'override page authored on option',
  151. 'override page authored by option',
  152. ));
  153. $general_user = $this->drupalCreateUser(array(
  154. 'create page content',
  155. 'edit any page content',
  156. 'override all authored on option',
  157. 'override all authored by option',
  158. ));
  159. foreach (array($specific_user, $general_user) as $account) {
  160. $this->drupalLogin($account);
  161. $this->drupalPost("node/{$this->node->nid}/edit", array('name' => 'invalid-user'), t('Save'));
  162. $this->assertText('The username invalid-user does not exist.');
  163. $this->drupalPost("node/{$this->node->nid}/edit", array('date' => 'invalid-date'), t('Save'));
  164. $this->assertText('You have to specify a valid date.');
  165. $time = time() + 500;
  166. $fields = array(
  167. 'name' => '',
  168. 'date' => format_date($time, 'custom', 'Y-m-d H:i:s O'),
  169. );
  170. $this->drupalPost("node/{$this->node->nid}/edit", $fields, t('Save'));
  171. $this->assertNodeFieldsUpdated($this->node, array('uid' => 0, 'created' => $time));
  172. }
  173. $this->drupalLogin($this->normalUser);
  174. $this->assertNodeFieldsNoAccess($this->node, array_keys($fields));
  175. }
  176. /**
  177. * Ensure that the node created date does not change when the node is edited.
  178. */
  179. public function testNodeCreatedDateDoesNotChange() {
  180. $this->drupalLogin(
  181. $this->drupalCreateUser(array('edit any page content'))
  182. );
  183. $node = $this->drupalCreateNode();
  184. // Update the node.
  185. $this->drupalPost("node/{$node->nid}/edit", array(), t('Save'));
  186. // Load a new instance of the node.
  187. $node2 = node_load($node->nid);
  188. // Ensure that the node was updated by comparing the changed dates, but the
  189. // created dates still match.
  190. $this->assertNotEqual($node->changed, $node2->changed, t('Changed values do not match.'));
  191. $this->assertEqual($node->created, $node2->created, t('Created values do match.'));
  192. }
  193. }