ElggGroupTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Elgg Test \ElggGroup
  4. *
  5. * @package Elgg
  6. * @subpackage Test
  7. */
  8. class ElggCoreGroupTest extends \ElggCoreUnitTest {
  9. /**
  10. * @var \ElggGroup
  11. */
  12. protected $group;
  13. /**
  14. * @var \ElggUser
  15. */
  16. protected $user;
  17. /**
  18. * Called before each test method.
  19. */
  20. public function setUp() {
  21. $this->group = new \ElggGroup();
  22. $this->group->membership = ACCESS_PUBLIC;
  23. $this->group->access_id = ACCESS_PUBLIC;
  24. $this->group->save();
  25. $this->user = new \ElggUser();
  26. $this->user->username = 'test_user_' . rand();
  27. $this->user->save();
  28. }
  29. public function testContentAccessMode() {
  30. $unrestricted = \ElggGroup::CONTENT_ACCESS_MODE_UNRESTRICTED;
  31. $membersonly = \ElggGroup::CONTENT_ACCESS_MODE_MEMBERS_ONLY;
  32. // if mode not set, open groups are unrestricted
  33. $this->assertEqual($this->group->getContentAccessMode(), $unrestricted);
  34. // after first check, metadata is set
  35. $this->assertEqual($this->group->content_access_mode, $unrestricted);
  36. // if mode not set, closed groups are membersonly
  37. $this->group->deleteMetadata('content_access_mode');
  38. $this->group->membership = ACCESS_PRIVATE;
  39. $this->assertEqual($this->group->getContentAccessMode(), $membersonly);
  40. // test set
  41. $this->group->setContentAccessMode($unrestricted);
  42. $this->assertEqual($this->group->getContentAccessMode(), $unrestricted);
  43. $this->group->setContentAccessMode($membersonly);
  44. $this->assertEqual($this->group->getContentAccessMode(), $membersonly);
  45. }
  46. public function testGroupItemVisibility() {
  47. $original_user = _elgg_services()->session->getLoggedInUser();
  48. _elgg_services()->session->setLoggedInUser($this->user);
  49. $group_guid = $this->group->guid;
  50. // unrestricted: pass non-members
  51. $this->group->setContentAccessMode(\ElggGroup::CONTENT_ACCESS_MODE_UNRESTRICTED);
  52. $vis = \Elgg\GroupItemVisibility::factory($group_guid, false);
  53. $this->assertFalse($vis->shouldHideItems);
  54. // membersonly: non-members fail
  55. $this->group->setContentAccessMode(\ElggGroup::CONTENT_ACCESS_MODE_MEMBERS_ONLY);
  56. $vis = \Elgg\GroupItemVisibility::factory($group_guid, false);
  57. $this->assertTrue($vis->shouldHideItems);
  58. // members succeed
  59. $this->group->join($this->user);
  60. $vis = \Elgg\GroupItemVisibility::factory($group_guid, false);
  61. $this->assertFalse($vis->shouldHideItems);
  62. // non-member admins succeed - assumes admin logged in
  63. _elgg_services()->session->setLoggedInUser($original_user);
  64. $vis = \Elgg\GroupItemVisibility::factory($group_guid, false);
  65. $this->assertFalse($vis->shouldHideItems);
  66. }
  67. /**
  68. * Called after each test method.
  69. */
  70. public function tearDown() {
  71. $this->group->delete();
  72. $this->user->delete();
  73. }
  74. }