write_access.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Test write access arrays
  4. */
  5. class GroupsWriteAccessTest extends ElggCoreUnitTest {
  6. /**
  7. * @var ElggGroup
  8. */
  9. protected $group;
  10. /**
  11. * @var ElggUser
  12. */
  13. protected $user;
  14. /**
  15. * Called before each test method.
  16. */
  17. public function setUp() {
  18. $this->group = new ElggGroup();
  19. $this->group->membership = ACCESS_PUBLIC;
  20. $this->group->access_id = ACCESS_PUBLIC;
  21. $this->group->save();
  22. $this->user = new ElggUser();
  23. $this->user->username = 'test_user_' . rand();
  24. $this->user->save();
  25. }
  26. /**
  27. * https://github.com/Elgg/Elgg/pull/6393
  28. * Hook handlers for 'access:collections:write','all' hook should respect
  29. * group's content access mode and container write permissions
  30. */
  31. public function testWriteAccessArray() {
  32. $membersonly = ElggGroup::CONTENT_ACCESS_MODE_MEMBERS_ONLY;
  33. $unrestricted = ElggGroup::CONTENT_ACCESS_MODE_UNRESTRICTED;
  34. $original_page_owner = elgg_get_page_owner_entity();
  35. elgg_set_page_owner_guid($this->group->guid);
  36. $ia = elgg_set_ignore_access(false);
  37. // User is not a member of the group
  38. // Member-only group
  39. $this->group->setContentAccessMode($membersonly);
  40. $write_access = get_write_access_array($this->user->guid, $this->group->site_guid, true);
  41. $this->assertFalse(array_key_exists($this->group->group_acl, $write_access));
  42. // Unrestricted group
  43. $this->group->setContentAccessMode($unrestricted);
  44. $write_access = get_write_access_array($this->user->guid, $this->group->site_guid, true);
  45. $this->assertFalse(array_key_exists($this->group->group_acl, $write_access));
  46. // User is a member (can write to container)
  47. $this->group->join($this->user);
  48. // Member-only group
  49. $this->group->setContentAccessMode($membersonly);
  50. $write_access = get_write_access_array($this->user->guid, $this->group->site_guid, true);
  51. $this->assertTrue(array_key_exists($this->group->group_acl, $write_access));
  52. // Unrestricted group
  53. $this->group->setContentAccessMode($unrestricted);
  54. $write_access = get_write_access_array($this->user->guid, $this->group->site_guid, true);
  55. $this->assertTrue(array_key_exists($this->group->group_acl, $write_access));
  56. elgg_set_ignore_access($ia);
  57. $this->group->leave($this->user);
  58. $original_page_owner_guid = (elgg_instanceof($original_page_owner)) ? $original_page_owner->guid : 0;
  59. elgg_set_page_owner_guid($original_page_owner_guid);
  60. }
  61. /**
  62. * Called after each test method.
  63. */
  64. public function tearDown() {
  65. $this->group->delete();
  66. $this->user->delete();
  67. }
  68. }