PermissionsTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Tests\tripal\entities;
  3. use StatonLab\TripalTestSuite\DBTransaction;
  4. use StatonLab\TripalTestSuite\TripalTestCase;
  5. use Faker\Factory;
  6. class PermissionsTest extends TripalTestCase {
  7. // Uncomment to auto start and rollback db transactions per test method.
  8. use DBTransaction;
  9. /**
  10. * Test that our new permissions are available.
  11. *
  12. * @group permissions
  13. */
  14. public function testPermissionsAvailable() {
  15. $permissions = module_invoke_all('permission');
  16. // All bundle names are bio_data_##. Content types are created on install
  17. // of tripal_chado and thus all sites should have them available.
  18. $bundle_name = db_query('SELECT name FROM tripal_bundle limit 1')->fetchField();
  19. // check `view [bundle name]`
  20. $tripal_permissions = [
  21. "view $bundle_name",
  22. "create $bundle_name",
  23. "edit $bundle_name",
  24. "delete $bundle_name",
  25. ];
  26. foreach ($tripal_permissions as $permission_name) {
  27. $this->assertArrayHasKey($permission_name, $permissions,
  28. "Tripal permission, $permission_name, was not available.");
  29. }
  30. }
  31. /**
  32. * Test the permission for a given bundle.
  33. *
  34. * NOTE: We only test one bundle since it should be the same for all
  35. * of them (done in a loop).
  36. *
  37. * @group permissions
  38. */
  39. public function testPermissionsForUser() {
  40. $faker = Factory::create();
  41. // Create organism entity for testing.
  42. $bundle_id = db_query("SELECT bundle_id from {chado_bundle} where data_table='organism'")->fetchField();
  43. $bundle_name = 'bio_data_' . $bundle_id;
  44. $bundle = tripal_load_bundle_entity(['name' => $bundle_name]);
  45. $genus = $faker->word(1, TRUE);
  46. $species = $faker->word(2, TRUE);
  47. $values = [
  48. 'bundle' => $bundle_name,
  49. 'term_id' => $bundle->term_id,
  50. 'chado_table' => 'organism',
  51. 'chado_column' => 'organism_id',
  52. ];
  53. $values['taxrank__genus']['und'][0] = [
  54. 'value' => $genus,
  55. 'chado-organism__genus' => $genus,
  56. ];
  57. $values['taxrank__species']['und'][0] = [
  58. 'value' => $species,
  59. 'chado-organism__species' => $species,
  60. ];
  61. $ec = entity_get_controller('TripalEntity');
  62. $entity = $ec->create($values);
  63. $entity = $entity->save();
  64. $entity_id = $entity->id;
  65. // For this test we are only testing entity permissions. Here we are
  66. // we are testing a single bundle.
  67. $tripal_permissions = [
  68. 'view' => "view $bundle_name",
  69. 'create' => "create $bundle_name",
  70. 'edit' => "edit $bundle_name",
  71. 'delete' => "delete $bundle_name",
  72. ];
  73. // All permissions are assigned to users via roles...
  74. // Thus, create two new roles:
  75. // 1) A role which cannot use any of the permissions.
  76. $role_canNOT = new \stdClass();
  77. $role_canNOT->name = $faker->name();
  78. user_role_save($role_canNOT);
  79. // 2) A role which can use all of them.
  80. $role_can = new \stdClass();
  81. $role_can->name = $faker->name();
  82. user_role_save($role_can);
  83. user_role_grant_permissions($role_can->rid, $tripal_permissions);
  84. // Create our users:
  85. // 1) a user without tripal permissions but who is still authenticated.
  86. $email = $faker->email();
  87. $user_canNOT = array(
  88. 'name' => $faker->name(),
  89. 'pass' => $faker->password(), // note: do not md5 the password
  90. 'mail' => $email,
  91. 'status' => 1,
  92. 'init' => $email,
  93. 'roles' => array(
  94. DRUPAL_AUTHENTICATED_RID => 'authenticated user',
  95. $role_canNOT->rid => $role_canNOT->name,
  96. ),
  97. );
  98. $user_canNOT = user_save('', $user_canNOT); // 1st param blank so new user is created.
  99. $user_canNOT_uid = $user_canNOT->uid;
  100. // 2) A user with the role giving them all tripal permissions.
  101. $email = $faker->email();
  102. $user_can = array(
  103. 'name' => $faker->name(),
  104. 'pass' => $faker->password(), // note: do not md5 the password
  105. 'mail' => $email,
  106. 'status' => 1,
  107. 'init' => $email,
  108. 'roles' => array(
  109. DRUPAL_AUTHENTICATED_RID => 'authenticated user',
  110. $role_can->rid => $role_can->name,
  111. ),
  112. );
  113. $user_can = user_save('', $user_can); // 1st param blank so new user is created.
  114. $user_can_uid = $user_can->uid;
  115. $entity_load = entity_load('TripalEntity', [$entity_id]);
  116. $entity = $entity_load[$entity_id];
  117. // Now we need to clear the user_access cache and re-load our users
  118. // in order to see our newly assigned roles and permissions reflected.
  119. drupal_static_reset('user_access');
  120. unset($user_can, $user_canNOT);
  121. $user_can = user_load($user_can_uid, TRUE);
  122. $user_canNOT = user_load($user_canNOT_uid, TRUE);
  123. cache_clear_all();
  124. // Finally, for each Tripal permission...
  125. foreach ($tripal_permissions as $op => $permission_name) {
  126. // Check that our roles were assigned this permission correctly.
  127. $all_roles_with_permission = user_roles(TRUE, $permission_name);
  128. $this->assertArrayHasKey($role_can->rid, $all_roles_with_permission,
  129. "Our newly created role doesn't have the expected permission.");
  130. $this->assertArrayNotHasKey($role_canNOT->rid, $all_roles_with_permission,
  131. "The roles that shouldn't have the permission, does?");
  132. // Check that the user who should be able to access the content, can.
  133. $result = tripal_entity_access($op, $entity, $user_can);
  134. $this->assertTrue($result,
  135. "The current user does not have permission to $op the entity.");
  136. // Check that the user who should NOT be able to access the content, can NOT.
  137. // Note we can only check if this permission is not given to the authenticated user.
  138. $has_authenticated = in_array(
  139. 'authenticated user',
  140. $all_roles_with_permission
  141. );
  142. if ($has_authenticated == FALSE) {
  143. $result = tripal_entity_access($op, $entity, $user_canNOT);
  144. $this->assertFalse($result,
  145. "The current user does but shouldn't have permission to $op the entity.");
  146. }
  147. }
  148. }
  149. }