LegacyGroupTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. namespace Test\Group;
  25. use OC_Group;
  26. use OC_User;
  27. /**
  28. * Class LegacyGroupTest
  29. *
  30. * @package Test\Group
  31. * @group DB
  32. */
  33. class LegacyGroupTest extends \Test\TestCase {
  34. protected function setUp() {
  35. parent::setUp();
  36. OC_Group::clearBackends();
  37. OC_User::clearBackends();
  38. }
  39. public function testSingleBackend() {
  40. $userBackend = new \Test\Util\User\Dummy();
  41. \OC::$server->getUserManager()->registerBackend($userBackend);
  42. OC_Group::useBackend(new \Test\Util\Group\Dummy());
  43. $group1 = $this->getUniqueID();
  44. $group2 = $this->getUniqueID();
  45. OC_Group::createGroup($group1);
  46. OC_Group::createGroup($group2);
  47. $user1 = $this->getUniqueID();
  48. $user2 = $this->getUniqueID();
  49. $userBackend->createUser($user1, '');
  50. $userBackend->createUser($user2, '');
  51. $this->assertFalse(OC_Group::inGroup($user1, $group1), 'Asserting that user1 is not in group1');
  52. $this->assertFalse(OC_Group::inGroup($user2, $group1), 'Asserting that user2 is not in group1');
  53. $this->assertFalse(OC_Group::inGroup($user1, $group2), 'Asserting that user1 is not in group2');
  54. $this->assertFalse(OC_Group::inGroup($user2, $group2), 'Asserting that user2 is not in group2');
  55. $this->assertTrue(OC_Group::addToGroup($user1, $group1));
  56. $this->assertTrue(OC_Group::inGroup($user1, $group1), 'Asserting that user1 is in group1');
  57. $this->assertFalse(OC_Group::inGroup($user2, $group1), 'Asserting that user2 is not in group1');
  58. $this->assertFalse(OC_Group::inGroup($user1, $group2), 'Asserting that user1 is not in group2');
  59. $this->assertFalse(OC_Group::inGroup($user2, $group2), 'Asserting that user2 is not in group2');
  60. $this->assertTrue(OC_Group::addToGroup($user1, $group1));
  61. $this->assertEquals(array($user1), OC_Group::usersInGroup($group1));
  62. $this->assertEquals(array(), OC_Group::usersInGroup($group2));
  63. $this->assertEquals(array($group1), OC_Group::getUserGroups($user1));
  64. $this->assertEquals(array(), OC_Group::getUserGroups($user2));
  65. OC_Group::deleteGroup($group1);
  66. $this->assertEquals(array(), OC_Group::getUserGroups($user1));
  67. $this->assertEquals(array(), OC_Group::usersInGroup($group1));
  68. $this->assertFalse(OC_Group::inGroup($user1, $group1));
  69. }
  70. public function testNoEmptyGIDs() {
  71. OC_Group::useBackend(new \Test\Util\Group\Dummy());
  72. $emptyGroup = null;
  73. $this->assertFalse(OC_Group::createGroup($emptyGroup));
  74. }
  75. public function testNoGroupsTwice() {
  76. OC_Group::useBackend(new \Test\Util\Group\Dummy());
  77. $group = $this->getUniqueID();
  78. OC_Group::createGroup($group);
  79. $groupCopy = $group;
  80. OC_Group::createGroup($groupCopy);
  81. $this->assertEquals(array($group), OC_Group::getGroups());
  82. }
  83. public function testDontDeleteAdminGroup() {
  84. OC_Group::useBackend(new \Test\Util\Group\Dummy());
  85. $adminGroup = 'admin';
  86. OC_Group::createGroup($adminGroup);
  87. $this->assertFalse(OC_Group::deleteGroup($adminGroup));
  88. $this->assertEquals(array($adminGroup), OC_Group::getGroups());
  89. }
  90. public function testDontAddUserToNonexistentGroup() {
  91. OC_Group::useBackend(new \Test\Util\Group\Dummy());
  92. $groupNonExistent = 'notExistent';
  93. $user = $this->getUniqueID();
  94. $this->assertEquals(false, OC_Group::addToGroup($user, $groupNonExistent));
  95. $this->assertEquals(array(), OC_Group::getGroups());
  96. }
  97. public function testUsersInGroup() {
  98. OC_Group::useBackend(new \Test\Util\Group\Dummy());
  99. $userBackend = new \Test\Util\User\Dummy();
  100. \OC::$server->getUserManager()->registerBackend($userBackend);
  101. $group1 = $this->getUniqueID();
  102. $group2 = $this->getUniqueID();
  103. $group3 = $this->getUniqueID();
  104. $user1 = $this->getUniqueID();
  105. $user2 = $this->getUniqueID();
  106. $user3 = $this->getUniqueID();
  107. OC_Group::createGroup($group1);
  108. OC_Group::createGroup($group2);
  109. OC_Group::createGroup($group3);
  110. $userBackend->createUser($user1, '');
  111. $userBackend->createUser($user2, '');
  112. $userBackend->createUser($user3, '');
  113. OC_Group::addToGroup($user1, $group1);
  114. OC_Group::addToGroup($user2, $group1);
  115. OC_Group::addToGroup($user3, $group1);
  116. OC_Group::addToGroup($user3, $group2);
  117. $this->assertEquals(array($user1, $user2, $user3),
  118. OC_Group::usersInGroups(array($group1, $group2, $group3)));
  119. // FIXME: needs more parameter variation
  120. }
  121. public function testMultiBackend() {
  122. $userBackend = new \Test\Util\User\Dummy();
  123. \OC::$server->getUserManager()->registerBackend($userBackend);
  124. $backend1 = new \Test\Util\Group\Dummy();
  125. $backend2 = new \Test\Util\Group\Dummy();
  126. OC_Group::useBackend($backend1);
  127. OC_Group::useBackend($backend2);
  128. $group1 = $this->getUniqueID();
  129. $group2 = $this->getUniqueID();
  130. OC_Group::createGroup($group1);
  131. //groups should be added to the first registered backend
  132. $this->assertEquals(array($group1), $backend1->getGroups());
  133. $this->assertEquals(array(), $backend2->getGroups());
  134. $this->assertEquals(array($group1), OC_Group::getGroups());
  135. $this->assertTrue(OC_Group::groupExists($group1));
  136. $this->assertFalse(OC_Group::groupExists($group2));
  137. $backend1->createGroup($group2);
  138. $this->assertEquals(array($group1, $group2), OC_Group::getGroups());
  139. $this->assertTrue(OC_Group::groupExists($group1));
  140. $this->assertTrue(OC_Group::groupExists($group2));
  141. $user1 = $this->getUniqueID();
  142. $user2 = $this->getUniqueID();
  143. $userBackend->createUser($user1, '');
  144. $userBackend->createUser($user2, '');
  145. $this->assertFalse(OC_Group::inGroup($user1, $group1));
  146. $this->assertFalse(OC_Group::inGroup($user2, $group1));
  147. $this->assertTrue(OC_Group::addToGroup($user1, $group1));
  148. $this->assertTrue(OC_Group::inGroup($user1, $group1));
  149. $this->assertFalse(OC_Group::inGroup($user2, $group1));
  150. $this->assertFalse($backend2->inGroup($user1, $group1));
  151. OC_Group::addToGroup($user1, $group1);
  152. $this->assertEquals(array($user1), OC_Group::usersInGroup($group1));
  153. $this->assertEquals(array($group1), OC_Group::getUserGroups($user1));
  154. $this->assertEquals(array(), OC_Group::getUserGroups($user2));
  155. OC_Group::deleteGroup($group1);
  156. $this->assertEquals(array(), OC_Group::getUserGroups($user1));
  157. $this->assertEquals(array(), OC_Group::usersInGroup($group1));
  158. $this->assertFalse(OC_Group::inGroup($user1, $group1));
  159. }
  160. }