group.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 nukeawhale@gmail.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 testDisplayNamesInGroup() {
  88. OC_Group::useBackend(new OC_Group_Dummy());
  89. $userBackend = new \OC_User_Dummy();
  90. \OC_User::getManager()->registerBackend($userBackend);
  91. $group1 = uniqid();
  92. $user1 = 'uid1';
  93. $user2 = 'uid2';
  94. OC_Group::createGroup($group1);
  95. $userBackend->createUser($user1, '');
  96. $userBackend->createUser($user2, '');
  97. OC_Group::addToGroup($user1, $group1);
  98. OC_Group::addToGroup($user2, $group1);
  99. //Dummy backend does not support setting displaynames, uid will always
  100. //be returned. This checks primarily, that the return format is okay.
  101. $expected = array($user1 => $user1, $user2 => $user2);
  102. $this->assertEquals($expected, OC_Group::displayNamesInGroup($group1));
  103. }
  104. public function testUsersInGroup() {
  105. OC_Group::useBackend(new OC_Group_Dummy());
  106. $userBackend = new \OC_User_Dummy();
  107. \OC_User::getManager()->registerBackend($userBackend);
  108. $group1 = uniqid();
  109. $group2 = uniqid();
  110. $group3 = uniqid();
  111. $user1 = uniqid();
  112. $user2 = uniqid();
  113. $user3 = uniqid();
  114. OC_Group::createGroup($group1);
  115. OC_Group::createGroup($group2);
  116. OC_Group::createGroup($group3);
  117. $userBackend->createUser($user1, '');
  118. $userBackend->createUser($user2, '');
  119. $userBackend->createUser($user3, '');
  120. OC_Group::addToGroup($user1, $group1);
  121. OC_Group::addToGroup($user2, $group1);
  122. OC_Group::addToGroup($user3, $group1);
  123. OC_Group::addToGroup($user3, $group2);
  124. $this->assertEquals(array($user1, $user2, $user3),
  125. OC_Group::usersInGroups(array($group1, $group2, $group3)));
  126. // FIXME: needs more parameter variation
  127. }
  128. function testMultiBackend() {
  129. $userBackend = new \OC_User_Dummy();
  130. \OC_User::getManager()->registerBackend($userBackend);
  131. $backend1 = new OC_Group_Dummy();
  132. $backend2 = new OC_Group_Dummy();
  133. OC_Group::useBackend($backend1);
  134. OC_Group::useBackend($backend2);
  135. $group1 = uniqid();
  136. $group2 = uniqid();
  137. OC_Group::createGroup($group1);
  138. //groups should be added to the first registered backend
  139. $this->assertEquals(array($group1), $backend1->getGroups());
  140. $this->assertEquals(array(), $backend2->getGroups());
  141. $this->assertEquals(array($group1), OC_Group::getGroups());
  142. $this->assertTrue(OC_Group::groupExists($group1));
  143. $this->assertFalse(OC_Group::groupExists($group2));
  144. $backend1->createGroup($group2);
  145. $this->assertEquals(array($group1, $group2), OC_Group::getGroups());
  146. $this->assertTrue(OC_Group::groupExists($group1));
  147. $this->assertTrue(OC_Group::groupExists($group2));
  148. $user1 = uniqid();
  149. $user2 = uniqid();
  150. $userBackend->createUser($user1, '');
  151. $userBackend->createUser($user2, '');
  152. $this->assertFalse(OC_Group::inGroup($user1, $group1));
  153. $this->assertFalse(OC_Group::inGroup($user2, $group1));
  154. $this->assertTrue(OC_Group::addToGroup($user1, $group1));
  155. $this->assertTrue(OC_Group::inGroup($user1, $group1));
  156. $this->assertFalse(OC_Group::inGroup($user2, $group1));
  157. $this->assertFalse($backend2->inGroup($user1, $group1));
  158. OC_Group::addToGroup($user1, $group1);
  159. $this->assertEquals(array($user1), OC_Group::usersInGroup($group1));
  160. $this->assertEquals(array($group1), OC_Group::getUserGroups($user1));
  161. $this->assertEquals(array(), OC_Group::getUserGroups($user2));
  162. OC_Group::deleteGroup($group1);
  163. $this->assertEquals(array(), OC_Group::getUserGroups($user1));
  164. $this->assertEquals(array(), OC_Group::usersInGroup($group1));
  165. $this->assertFalse(OC_Group::inGroup($user1, $group1));
  166. }
  167. }