group.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @author Bernhard Posselt
  7. * @copyright 2012 Robin Appelman <icewind@owncloud.com>
  8. * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  12. * License as published by the Free Software Foundation; either
  13. * version 3 of the License, or any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public
  21. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. class Test_Group extends \Test\TestCase {
  25. protected function setUp() {
  26. parent::setUp();
  27. OC_Group::clearBackends();
  28. OC_User::clearBackends();
  29. }
  30. public function testSingleBackend() {
  31. $userBackend = new \OC_User_Dummy();
  32. \OC_User::getManager()->registerBackend($userBackend);
  33. OC_Group::useBackend(new OC_Group_Dummy());
  34. $group1 = $this->getUniqueID();
  35. $group2 = $this->getUniqueID();
  36. OC_Group::createGroup($group1);
  37. OC_Group::createGroup($group2);
  38. $user1 = $this->getUniqueID();
  39. $user2 = $this->getUniqueID();
  40. $userBackend->createUser($user1, '');
  41. $userBackend->createUser($user2, '');
  42. $this->assertFalse(OC_Group::inGroup($user1, $group1), 'Asserting that user1 is not in group1');
  43. $this->assertFalse(OC_Group::inGroup($user2, $group1), 'Asserting that user2 is not in group1');
  44. $this->assertFalse(OC_Group::inGroup($user1, $group2), 'Asserting that user1 is not in group2');
  45. $this->assertFalse(OC_Group::inGroup($user2, $group2), 'Asserting that user2 is not in group2');
  46. $this->assertTrue(OC_Group::addToGroup($user1, $group1));
  47. $this->assertTrue(OC_Group::inGroup($user1, $group1), 'Asserting that user1 is in group1');
  48. $this->assertFalse(OC_Group::inGroup($user2, $group1), 'Asserting that user2 is not in group1');
  49. $this->assertFalse(OC_Group::inGroup($user1, $group2), 'Asserting that user1 is not in group2');
  50. $this->assertFalse(OC_Group::inGroup($user2, $group2), 'Asserting that user2 is not in group2');
  51. $this->assertTrue(OC_Group::addToGroup($user1, $group1));
  52. $this->assertEquals(array($user1), OC_Group::usersInGroup($group1));
  53. $this->assertEquals(array(), OC_Group::usersInGroup($group2));
  54. $this->assertEquals(array($group1), OC_Group::getUserGroups($user1));
  55. $this->assertEquals(array(), OC_Group::getUserGroups($user2));
  56. OC_Group::deleteGroup($group1);
  57. $this->assertEquals(array(), OC_Group::getUserGroups($user1));
  58. $this->assertEquals(array(), OC_Group::usersInGroup($group1));
  59. $this->assertFalse(OC_Group::inGroup($user1, $group1));
  60. }
  61. public function testNoEmptyGIDs() {
  62. OC_Group::useBackend(new OC_Group_Dummy());
  63. $emptyGroup = null;
  64. $this->assertFalse(OC_Group::createGroup($emptyGroup));
  65. }
  66. public function testNoGroupsTwice() {
  67. OC_Group::useBackend(new OC_Group_Dummy());
  68. $group = $this->getUniqueID();
  69. OC_Group::createGroup($group);
  70. $groupCopy = $group;
  71. OC_Group::createGroup($groupCopy);
  72. $this->assertEquals(array($group), OC_Group::getGroups());
  73. }
  74. public function testDontDeleteAdminGroup() {
  75. OC_Group::useBackend(new OC_Group_Dummy());
  76. $adminGroup = 'admin';
  77. OC_Group::createGroup($adminGroup);
  78. $this->assertFalse(OC_Group::deleteGroup($adminGroup));
  79. $this->assertEquals(array($adminGroup), OC_Group::getGroups());
  80. }
  81. public function testDontAddUserToNonexistentGroup() {
  82. OC_Group::useBackend(new OC_Group_Dummy());
  83. $groupNonExistent = 'notExistent';
  84. $user = $this->getUniqueID();
  85. $this->assertEquals(false, OC_Group::addToGroup($user, $groupNonExistent));
  86. $this->assertEquals(array(), OC_Group::getGroups());
  87. }
  88. public function testUsersInGroup() {
  89. OC_Group::useBackend(new OC_Group_Dummy());
  90. $userBackend = new \OC_User_Dummy();
  91. \OC_User::getManager()->registerBackend($userBackend);
  92. $group1 = $this->getUniqueID();
  93. $group2 = $this->getUniqueID();
  94. $group3 = $this->getUniqueID();
  95. $user1 = $this->getUniqueID();
  96. $user2 = $this->getUniqueID();
  97. $user3 = $this->getUniqueID();
  98. OC_Group::createGroup($group1);
  99. OC_Group::createGroup($group2);
  100. OC_Group::createGroup($group3);
  101. $userBackend->createUser($user1, '');
  102. $userBackend->createUser($user2, '');
  103. $userBackend->createUser($user3, '');
  104. OC_Group::addToGroup($user1, $group1);
  105. OC_Group::addToGroup($user2, $group1);
  106. OC_Group::addToGroup($user3, $group1);
  107. OC_Group::addToGroup($user3, $group2);
  108. $this->assertEquals(array($user1, $user2, $user3),
  109. OC_Group::usersInGroups(array($group1, $group2, $group3)));
  110. // FIXME: needs more parameter variation
  111. }
  112. public function testMultiBackend() {
  113. $userBackend = new \OC_User_Dummy();
  114. \OC_User::getManager()->registerBackend($userBackend);
  115. $backend1 = new OC_Group_Dummy();
  116. $backend2 = new OC_Group_Dummy();
  117. OC_Group::useBackend($backend1);
  118. OC_Group::useBackend($backend2);
  119. $group1 = $this->getUniqueID();
  120. $group2 = $this->getUniqueID();
  121. OC_Group::createGroup($group1);
  122. //groups should be added to the first registered backend
  123. $this->assertEquals(array($group1), $backend1->getGroups());
  124. $this->assertEquals(array(), $backend2->getGroups());
  125. $this->assertEquals(array($group1), OC_Group::getGroups());
  126. $this->assertTrue(OC_Group::groupExists($group1));
  127. $this->assertFalse(OC_Group::groupExists($group2));
  128. $backend1->createGroup($group2);
  129. $this->assertEquals(array($group1, $group2), OC_Group::getGroups());
  130. $this->assertTrue(OC_Group::groupExists($group1));
  131. $this->assertTrue(OC_Group::groupExists($group2));
  132. $user1 = $this->getUniqueID();
  133. $user2 = $this->getUniqueID();
  134. $userBackend->createUser($user1, '');
  135. $userBackend->createUser($user2, '');
  136. $this->assertFalse(OC_Group::inGroup($user1, $group1));
  137. $this->assertFalse(OC_Group::inGroup($user2, $group1));
  138. $this->assertTrue(OC_Group::addToGroup($user1, $group1));
  139. $this->assertTrue(OC_Group::inGroup($user1, $group1));
  140. $this->assertFalse(OC_Group::inGroup($user2, $group1));
  141. $this->assertFalse($backend2->inGroup($user1, $group1));
  142. OC_Group::addToGroup($user1, $group1);
  143. $this->assertEquals(array($user1), OC_Group::usersInGroup($group1));
  144. $this->assertEquals(array($group1), OC_Group::getUserGroups($user1));
  145. $this->assertEquals(array(), OC_Group::getUserGroups($user2));
  146. OC_Group::deleteGroup($group1);
  147. $this->assertEquals(array(), OC_Group::getUserGroups($user1));
  148. $this->assertEquals(array(), OC_Group::usersInGroup($group1));
  149. $this->assertFalse(OC_Group::inGroup($user1, $group1));
  150. }
  151. }