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