publishcontent.test 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * @file
  4. * Unit tests for Publish Content module.
  5. * prerequesite: make sure that 'authenticated user' does not have any access like
  6. * 'publish [content type] content' or 'unpublish [content type] content'
  7. *
  8. * @note: We test to ensure we are not messing up with the default Drupal access for view node
  9. * i.e. a owner of a node can view it even if unpublished.
  10. */
  11. class PublishContentWebCaseTest extends DrupalWebTestCase {
  12. /**
  13. * Drupal SimpleTest method: return metadata about the test.
  14. */
  15. function getInfo() {
  16. return array(
  17. 'name' => t('Publish Content: access control'),
  18. 'description' => t('Executes test suite for Publish Content module.'),
  19. 'group' => t('Publish Content'),
  20. );
  21. }
  22. function setUp() {
  23. parent::setUp('publishcontent');
  24. }
  25. function publishcontent_do_operation($nid, $op, $expected_status, $msg = NULL) {
  26. $this->drupalGet("node/$nid/$op");
  27. $node = node_load($nid, NULL, TRUE);
  28. $this->assertEqual($node->status, $expected_status, $msg);
  29. }
  30. function assert_access_node($node, $msg = NULL) {
  31. $this->drupalGet('node/'. $node->nid);
  32. $this->assertResponse(200);
  33. $this->assertTitle($node->title . ' | '. variable_get('site_name', 'Drupal'), $msg);
  34. }
  35. function assert_access_denied($url, $msg = NULL) {
  36. $this->drupalGet($url);
  37. $this->assertResponse(403);
  38. $this->assertText('Access denied' . ' | '. variable_get('site_name', 'Drupal'), $msg);
  39. }
  40. function assert_node_status($nid, $status, $msg = 'node status mismatches') {
  41. $result = node_load($nid, NULL, TRUE);
  42. $this->assertEqual($result->status, $status, $msg);
  43. }
  44. function set_node_status(&$node, $status, $msg = 'unable to set correct node status') {
  45. $node->status = $status;
  46. node_save($node);
  47. $this->assert_node_status($node->nid, $status, $msg);
  48. }
  49. function assert_current_user_cannot_publish_node(&$node) {
  50. $this->assertEqual($node->status, 1, 'pre-requesite: status MUST be 1');
  51. $this->assert_access_denied("node/{$node->nid}/publish", "no publish permission --> access denied");
  52. $this->assert_node_status($node->nid, 1, 'node should be still published');
  53. $this->assert_access_node($node, 'node MUST BE viewable');
  54. $this->set_node_status($node, 0);
  55. $this->assert_access_denied("node/{$node->nid}/publish", "no publish permission --> access denied");
  56. $this->assert_node_status($node->nid, 0, 'node should be still unpublished');
  57. $this->set_node_status($node, 1, 'post-requesite: status MUST be 1');
  58. }
  59. function assert_current_user_cannot_unpublish_node(&$node) {
  60. $this->assertEqual($node->status, 1, 'pre-requesite: status MUST be 1');
  61. $this->assert_access_denied("node/{$node->nid}/unpublish", "no unpublish permission --> access denied");
  62. $this->assert_node_status($node->nid, 1, 'node should be still published');
  63. $this->assert_access_node($node, 'node MUST BE viewable');
  64. $this->set_node_status($node, 0);
  65. $this->assert_access_denied("node/{$node->nid}/unpublish", "no unpublish permission --> access denied");
  66. $this->assert_node_status($node->nid, 0, 'node should be still unpublished');
  67. $this->set_node_status($node, 1, 'post-requesite: status MUST be 1');
  68. }
  69. function assert_current_user_can_publish_node(&$node) {
  70. $this->assertEqual($node->status, 1, 'pre-requesite: status MUST be 1');
  71. $this->publishcontent_do_operation($node->nid, 'publish', 1, 'node should be still published');
  72. $this->assert_access_node($node, 'node MUST BE viewable');
  73. $this->set_node_status($node, 0);
  74. $this->assert_access_node($node, 'node MUST BE viewable even if unpublished');
  75. $this->publishcontent_do_operation($node->nid, 'publish', 1, 'node should be now published');
  76. $this->assertText(_publishcontent_get_message($node->nid, $node->title, TRUE),
  77. 'drupal_set_message not working for publish.');
  78. $this->set_node_status($node, 1, 'post-requesite: status MUST be 1');
  79. }
  80. function assert_current_user_can_unpublish_node(&$node) {
  81. $this->assertEqual($node->status, 1, 'pre-requesite: status MUST be 1');
  82. $this->publishcontent_do_operation($node->nid, 'unpublish', 0, 'node should be published');
  83. $this->assertText(_publishcontent_get_message($node->nid, $node->title, FALSE),
  84. 'drupal_set_message not working for unpublish.');
  85. $this->assert_access_node($node, 'node MUST BE viewable even if unpublished');
  86. $this->publishcontent_do_operation($node->nid, 'unpublish', 0, 'node should be still unpublished');
  87. $this->set_node_status($node, 1, 'post-requesite: status MUST be 1');
  88. }
  89. /**
  90. * Test the access for the owner of a node without the permission to
  91. * publish or unpublish.
  92. *
  93. * @note: node's owner can see it even if unpublished by default in Drupal
  94. */
  95. function testNoPermissionByOwner() {
  96. // Prepare a user to do the stuff
  97. $web_user = $this->drupalCreateUser(array('access content'));
  98. $this->drupalLogin($web_user);
  99. $node = $this->drupalCreateNode(
  100. array(
  101. 'type' => 'page',
  102. 'uid' => $web_user->uid,
  103. 'status' => 1,
  104. )
  105. );
  106. $this->assert_current_user_cannot_publish_node($node);
  107. $this->assert_current_user_cannot_unpublish_node($node);
  108. $this->set_node_status($node, 0);
  109. $this->assert_access_node($node, 'node MUST BE viewable if unpublished');
  110. }
  111. function testNoPermissionAndNotOwner() {
  112. $node = $this->drupalCreateNode(
  113. array(
  114. 'type' => 'page',
  115. 'uid' => 0,
  116. 'status' => 1,
  117. )
  118. );
  119. $this->drupalLogin($this->drupalCreateUser(array('access content')));
  120. $this->assert_current_user_cannot_publish_node($node);
  121. $this->assert_current_user_cannot_unpublish_node($node);
  122. }
  123. function testDoPublishByNodeOwner() {
  124. $type = 'page';
  125. $web_user = $this->drupalCreateUser(array('publish any '. $type .' content'));
  126. $this->drupalLogin($web_user);
  127. $node = $this->drupalCreateNode(
  128. array(
  129. 'type' => $type,
  130. 'uid' => $web_user->uid,
  131. 'status' => 1,
  132. )
  133. );
  134. $this->assert_current_user_can_publish_node($node);
  135. $this->assert_current_user_cannot_unpublish_node($node);
  136. }
  137. function testDoUnpublishByNodeOwner() {
  138. $type = 'page';
  139. $web_user = $this->drupalCreateUser(array('unpublish any '. $type .' content'));
  140. $this->drupalLogin($web_user);
  141. $node = $this->drupalCreateNode(
  142. array(
  143. 'type' => $type,
  144. 'uid' => $web_user->uid,
  145. 'status' => 1,
  146. )
  147. );
  148. $this->assert_current_user_cannot_publish_node($node);
  149. $this->assert_current_user_can_unpublish_node($node);
  150. }
  151. function testDoPublishAndUnpublishNotByNodeOwner() {
  152. $type = 'page';
  153. $node = $this->drupalCreateNode(
  154. array(
  155. 'type' => $type,
  156. 'uid' => 0,
  157. 'status' => 1,
  158. )
  159. );
  160. $this->drupalLogin($this->drupalCreateUser(array('publish any '. $type .' content')));
  161. $this->assert_current_user_can_publish_node($node);
  162. $this->assert_current_user_cannot_unpublish_node($node);
  163. $this->drupalLogin($this->drupalCreateUser(array('unpublish any '. $type .' content')));
  164. $this->assert_current_user_cannot_publish_node($node);
  165. $this->assert_current_user_can_unpublish_node($node);
  166. $this->drupalLogin($this->drupalCreateUser(array('publish any content')));
  167. $this->assert_current_user_can_publish_node($node);
  168. $this->assert_current_user_cannot_unpublish_node($node);
  169. $this->drupalLogin($this->drupalCreateUser(array('unpublish any content')));
  170. $this->assert_current_user_cannot_publish_node($node);
  171. $this->assert_current_user_can_unpublish_node($node);
  172. }
  173. }