override_node_options.test 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @file
  4. * Unit tests for the override_node_options module.
  5. */
  6. class OverrideNodeOptionsTestCase extends DrupalWebTestCase {
  7. protected $normal_user;
  8. protected $admin_user;
  9. protected $node;
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Override node options',
  13. 'description' => 'Functional tests for overridding options on node forms.',
  14. 'group' => 'Override node options',
  15. );
  16. }
  17. public function setUp() {
  18. parent::setUp('override_node_options');
  19. $this->normal_user = $this->drupalCreateUser(array('create page content', 'edit any page content'));
  20. $this->node = $this->drupalCreateNode();
  21. }
  22. /**
  23. * Assert that fields in a node were updated to certail values.
  24. *
  25. * @param $node
  26. * The node object to check (will be reloaded from the database).
  27. * @param $fields
  28. * An array of values to check equality, keyed by node object property.
  29. */
  30. function assertNodeFieldsUpdated(stdClass $node, array $fields) {
  31. // Re-load the node from the database to make sure we have the current
  32. // values.
  33. $node = node_load($node->nid, NULL, TRUE);
  34. foreach ($fields as $field => $value) {
  35. $this->assertEqual($node->$field, $value, t('Node @field was updated to !value, expected !expected.', array('@field' => $field, '!value' => var_export($node->$field, TRUE), '!expected' => var_export($value, TRUE))));
  36. }
  37. }
  38. /**
  39. * Assert that the user cannot access fields on node add and edit forms.
  40. *
  41. * @param $node
  42. * The node object, will be used on the node edit form.
  43. * @param $fields
  44. * An array of form fields to check.
  45. */
  46. function assertNodeFieldsNoAccess(stdClass $node, array $fields) {
  47. $this->drupalGet('node/add/' . $node->type);
  48. foreach ($fields as $field) {
  49. $this->assertNoFieldByName($field);
  50. }
  51. $this->drupalGet('node/' . $this->node->nid . '/edit');
  52. foreach ($fields as $field) {
  53. $this->assertNoFieldByName($field);
  54. }
  55. }
  56. /**
  57. * Test the 'Authoring information' fieldset.
  58. */
  59. function testNodeOptions() {
  60. $this->admin_user = $this->drupalCreateUser(array('create page content', 'edit any page content', 'override page published option', 'override page promote to front page option', 'override page sticky option'));
  61. $this->drupalLogin($this->admin_user);
  62. $fields = array(
  63. 'status' => (bool) !$this->node->status,
  64. 'promote' => (bool) !$this->node->promote,
  65. 'sticky' => (bool) !$this->node->sticky,
  66. );
  67. $this->drupalPost('node/' . $this->node->nid . '/edit', $fields, t('Save'));
  68. $this->assertNodeFieldsUpdated($this->node, $fields);
  69. $this->drupalLogin($this->normal_user);
  70. $this->assertNodeFieldsNoAccess($this->node, array_keys($fields));
  71. }
  72. /**
  73. * Test the 'Revision information' fieldset.
  74. */
  75. function testNodeRevisions() {
  76. $this->admin_user = $this->drupalCreateUser(array('create page content', 'edit any page content', 'override page revision option'));
  77. $this->drupalLogin($this->admin_user);
  78. $fields = array(
  79. 'revision' => TRUE,
  80. );
  81. $this->drupalPost('node/' . $this->node->nid . '/edit', $fields, t('Save'));
  82. $this->assertNodeFieldsUpdated($this->node, array('vid' => $this->node->vid + 1));
  83. $this->drupalLogin($this->normal_user);
  84. $this->assertNodeFieldsNoAccess($this->node, array_keys($fields));
  85. }
  86. /**
  87. * Test the 'Authoring information' fieldset.
  88. */
  89. function testNodeAuthor() {
  90. $this->admin_user = $this->drupalCreateUser(array('create page content', 'edit any page content', 'override page authored on option', 'override page authored by option'));
  91. $this->drupalLogin($this->admin_user);
  92. $this->drupalPost('node/' . $this->node->nid . '/edit', array('name' => 'invalid-user'), t('Save'));
  93. $this->assertText('The username invalid-user does not exist.');
  94. $this->drupalPost('node/' . $this->node->nid . '/edit', array('date' => 'invalid-date'), t('Save'));
  95. $this->assertText('You have to specify a valid date.');
  96. $time = time() + 500;
  97. $fields = array(
  98. 'name' => '',
  99. 'date' => format_date($time, 'custom', 'Y-m-d H:i:s O'),
  100. );
  101. $this->drupalPost('node/' . $this->node->nid . '/edit', $fields, t('Save'));
  102. $this->assertNodeFieldsUpdated($this->node, array('uid' => 0, 'created' => $time));
  103. $this->drupalLogin($this->normal_user);
  104. $this->assertNodeFieldsNoAccess($this->node, array_keys($fields));
  105. }
  106. }