content_access_test_help.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * @file
  4. * Helper class with auxiliary functions for content access module tests
  5. */
  6. class ContentAccessTestCase extends DrupalWebTestCase {
  7. var $test_user;
  8. var $rid;
  9. var $admin_user;
  10. var $content_type;
  11. var $url_content_type_name;
  12. var $node1;
  13. var $node2;
  14. /**
  15. * Preparation work that is done before each test.
  16. * Test users, content types, nodes etc. are created.
  17. */
  18. function setUp($module = '') {
  19. if (empty($module)) {
  20. // Enable content access module
  21. parent::setUp('content_access');
  22. }
  23. else {
  24. // Enable content access module plus another module
  25. parent::setUp('content_access', $module);
  26. // Stop setup when module could not be enabled
  27. if (!module_exists($module)) {
  28. $this->pass('No ' . $module . ' module present, skipping test');
  29. return;
  30. }
  31. }
  32. // Create test user with seperate role
  33. $this->test_user = $this->drupalCreateUser();
  34. // Get the value of the new role
  35. // Needed in D7 because it's by default create two roles for new users
  36. // one role is Authenticated and the second is new default one
  37. // @see drupalCreateUser()
  38. foreach ($this->test_user->roles as $rid => $role) {
  39. if (!in_array($rid, array(DRUPAL_AUTHENTICATED_RID))) {
  40. $this->rid = $rid;
  41. break;
  42. }
  43. }
  44. // Create admin user
  45. $this->admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'grant content access', 'grant own content access', 'administer nodes', 'access administration pages'));
  46. $this->drupalLogin($this->admin_user);
  47. // Rebuild content access permissions
  48. node_access_rebuild();
  49. // Create test content type
  50. $this->content_type = $this->drupalCreateContentType();
  51. }
  52. /**
  53. * Change access permissions for a content type
  54. */
  55. function changeAccessContentType($access_settings) {
  56. $this->drupalPost('admin/structure/types/manage/'. $this->content_type->type .'/access', $access_settings, t('Submit'));
  57. $this->assertText(t('Your changes have been saved.'), 'access rules of content type were updated successfully');
  58. }
  59. /**
  60. * Change access permissions for a content type by a given keyword (view, update or delete)
  61. * for the role of the user
  62. */
  63. function changeAccessContentTypeKeyword($keyword, $access = TRUE, $user = NULL) {
  64. if ($user === NULL) {
  65. $user = $this->test_user;
  66. $roles[$this->rid] = $user->roles[$this->rid];
  67. } else {
  68. foreach ($user->roles as $rid => $role) {
  69. if (!in_array($rid, array(DRUPAL_AUTHENTICATED_RID))) {
  70. $roles[$rid] = $user->roles[$rid];
  71. break;
  72. }
  73. }
  74. }
  75. $access_settings = array(
  76. $keyword .'['. key($roles) .']' => $access,
  77. );
  78. $this->changeAccessContentType($access_settings);
  79. }
  80. /**
  81. * Change the per node access setting for a content type
  82. */
  83. function changeAccessPerNode($access = TRUE) {
  84. $access_permissions = array(
  85. 'per_node' => $access,
  86. );
  87. $this->changeAccessContentType($access_permissions);
  88. }
  89. /**
  90. * Change access permissions for a node by a given keyword (view, update or delete)
  91. */
  92. function changeAccessNodeKeyword($node, $keyword, $access = TRUE) {
  93. $user = $this->test_user;
  94. $roles[$this->rid] = $user->roles[$this->rid];
  95. $access_settings = array(
  96. $keyword .'['. key($roles) .']' => $access,
  97. );
  98. $this->changeAccessNode($node, $access_settings);
  99. }
  100. /**
  101. * Change access permission for a node
  102. */
  103. function changeAccessNode($node, $access_settings) {
  104. $this->drupalPost('node/'. $node->nid .'/access', $access_settings, t('Submit'));
  105. $this->assertText(t('Your changes have been saved.'), 'access rules of node were updated successfully');
  106. }
  107. }