group.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Group;
  9. use OC\User\User;
  10. class Group extends \PHPUnit_Framework_TestCase {
  11. /**
  12. * @return \PHPUnit_Framework_MockObject_MockObject | \OC\User\Manager
  13. */
  14. protected function getUserManager() {
  15. $userManager = $this->getMock('\OC\User\Manager');
  16. $user1 = new User('user1', null);
  17. $user2 = new User('user2', null);
  18. $user3 = new User('user3', null);
  19. $userManager->expects($this->any())
  20. ->method('get')
  21. ->will($this->returnValueMap(array(
  22. array('user1', $user1),
  23. array('user2', $user2),
  24. array('user3', $user3)
  25. )));
  26. return $userManager;
  27. }
  28. public function testGetUsersSingleBackend() {
  29. $backend = $this->getMock('OC_Group_Database');
  30. $userManager = $this->getUserManager();
  31. $group = new \OC\Group\Group('group1', array($backend), $userManager);
  32. $backend->expects($this->once())
  33. ->method('usersInGroup')
  34. ->with('group1')
  35. ->will($this->returnValue(array('user1', 'user2')));
  36. $users = $group->getUsers();
  37. $this->assertEquals(2, count($users));
  38. $user1 = $users['user1'];
  39. $user2 = $users['user2'];
  40. $this->assertEquals('user1', $user1->getUID());
  41. $this->assertEquals('user2', $user2->getUID());
  42. }
  43. public function testGetUsersMultipleBackends() {
  44. $backend1 = $this->getMock('OC_Group_Database');
  45. $backend2 = $this->getMock('OC_Group_Database');
  46. $userManager = $this->getUserManager();
  47. $group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager);
  48. $backend1->expects($this->once())
  49. ->method('usersInGroup')
  50. ->with('group1')
  51. ->will($this->returnValue(array('user1', 'user2')));
  52. $backend2->expects($this->once())
  53. ->method('usersInGroup')
  54. ->with('group1')
  55. ->will($this->returnValue(array('user2', 'user3')));
  56. $users = $group->getUsers();
  57. $this->assertEquals(3, count($users));
  58. $user1 = $users['user1'];
  59. $user2 = $users['user2'];
  60. $user3 = $users['user3'];
  61. $this->assertEquals('user1', $user1->getUID());
  62. $this->assertEquals('user2', $user2->getUID());
  63. $this->assertEquals('user3', $user3->getUID());
  64. }
  65. public function testInGroupSingleBackend() {
  66. $backend = $this->getMock('OC_Group_Database');
  67. $userManager = $this->getUserManager();
  68. $group = new \OC\Group\Group('group1', array($backend), $userManager);
  69. $backend->expects($this->once())
  70. ->method('inGroup')
  71. ->with('user1', 'group1')
  72. ->will($this->returnValue(true));
  73. $this->assertTrue($group->inGroup(new User('user1', null)));
  74. }
  75. public function testInGroupMultipleBackends() {
  76. $backend1 = $this->getMock('OC_Group_Database');
  77. $backend2 = $this->getMock('OC_Group_Database');
  78. $userManager = $this->getUserManager();
  79. $group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager);
  80. $backend1->expects($this->once())
  81. ->method('inGroup')
  82. ->with('user1', 'group1')
  83. ->will($this->returnValue(false));
  84. $backend2->expects($this->once())
  85. ->method('inGroup')
  86. ->with('user1', 'group1')
  87. ->will($this->returnValue(true));
  88. $this->assertTrue($group->inGroup(new User('user1', null)));
  89. }
  90. public function testAddUser() {
  91. $backend = $this->getMock('OC_Group_Database');
  92. $userManager = $this->getUserManager();
  93. $group = new \OC\Group\Group('group1', array($backend), $userManager);
  94. $backend->expects($this->once())
  95. ->method('inGroup')
  96. ->with('user1', 'group1')
  97. ->will($this->returnValue(false));
  98. $backend->expects($this->any())
  99. ->method('implementsActions')
  100. ->will($this->returnValue(true));
  101. $backend->expects($this->once())
  102. ->method('addToGroup')
  103. ->with('user1', 'group1');
  104. $group->addUser(new User('user1', null));
  105. }
  106. public function testAddUserAlreadyInGroup() {
  107. $backend = $this->getMock('OC_Group_Database');
  108. $userManager = $this->getUserManager();
  109. $group = new \OC\Group\Group('group1', array($backend), $userManager);
  110. $backend->expects($this->once())
  111. ->method('inGroup')
  112. ->with('user1', 'group1')
  113. ->will($this->returnValue(true));
  114. $backend->expects($this->any())
  115. ->method('implementsActions')
  116. ->will($this->returnValue(true));
  117. $backend->expects($this->never())
  118. ->method('addToGroup');
  119. $group->addUser(new User('user1', null));
  120. }
  121. public function testRemoveUser() {
  122. $backend = $this->getMock('OC_Group_Database');
  123. $userManager = $this->getUserManager();
  124. $group = new \OC\Group\Group('group1', array($backend), $userManager);
  125. $backend->expects($this->once())
  126. ->method('inGroup')
  127. ->with('user1', 'group1')
  128. ->will($this->returnValue(true));
  129. $backend->expects($this->any())
  130. ->method('implementsActions')
  131. ->will($this->returnValue(true));
  132. $backend->expects($this->once())
  133. ->method('removeFromGroup')
  134. ->with('user1', 'group1');
  135. $group->removeUser(new User('user1', null));
  136. }
  137. public function testRemoveUserNotInGroup() {
  138. $backend = $this->getMock('OC_Group_Database');
  139. $userManager = $this->getUserManager();
  140. $group = new \OC\Group\Group('group1', array($backend), $userManager);
  141. $backend->expects($this->once())
  142. ->method('inGroup')
  143. ->with('user1', 'group1')
  144. ->will($this->returnValue(false));
  145. $backend->expects($this->any())
  146. ->method('implementsActions')
  147. ->will($this->returnValue(true));
  148. $backend->expects($this->never())
  149. ->method('removeFromGroup');
  150. $group->removeUser(new User('user1', null));
  151. }
  152. public function testRemoveUserMultipleBackends() {
  153. $backend1 = $this->getMock('OC_Group_Database');
  154. $backend2 = $this->getMock('OC_Group_Database');
  155. $userManager = $this->getUserManager();
  156. $group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager);
  157. $backend1->expects($this->once())
  158. ->method('inGroup')
  159. ->with('user1', 'group1')
  160. ->will($this->returnValue(true));
  161. $backend1->expects($this->any())
  162. ->method('implementsActions')
  163. ->will($this->returnValue(true));
  164. $backend1->expects($this->once())
  165. ->method('removeFromGroup')
  166. ->with('user1', 'group1');
  167. $backend2->expects($this->once())
  168. ->method('inGroup')
  169. ->with('user1', 'group1')
  170. ->will($this->returnValue(true));
  171. $backend2->expects($this->any())
  172. ->method('implementsActions')
  173. ->will($this->returnValue(true));
  174. $backend2->expects($this->once())
  175. ->method('removeFromGroup')
  176. ->with('user1', 'group1');
  177. $group->removeUser(new User('user1', null));
  178. }
  179. public function testSearchUsers() {
  180. $backend = $this->getMock('OC_Group_Database');
  181. $userManager = $this->getUserManager();
  182. $group = new \OC\Group\Group('group1', array($backend), $userManager);
  183. $backend->expects($this->once())
  184. ->method('usersInGroup')
  185. ->with('group1', '2')
  186. ->will($this->returnValue(array('user2')));
  187. $users = $group->searchUsers('2');
  188. $this->assertEquals(1, count($users));
  189. $user2 = $users[0];
  190. $this->assertEquals('user2', $user2->getUID());
  191. }
  192. public function testSearchUsersMultipleBackends() {
  193. $backend1 = $this->getMock('OC_Group_Database');
  194. $backend2 = $this->getMock('OC_Group_Database');
  195. $userManager = $this->getUserManager();
  196. $group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager);
  197. $backend1->expects($this->once())
  198. ->method('usersInGroup')
  199. ->with('group1', '2')
  200. ->will($this->returnValue(array('user2')));
  201. $backend2->expects($this->once())
  202. ->method('usersInGroup')
  203. ->with('group1', '2')
  204. ->will($this->returnValue(array('user2')));
  205. $users = $group->searchUsers('2');
  206. $this->assertEquals(1, count($users));
  207. $user2 = $users[0];
  208. $this->assertEquals('user2', $user2->getUID());
  209. }
  210. public function testSearchUsersLimitAndOffset() {
  211. $backend = $this->getMock('OC_Group_Database');
  212. $userManager = $this->getUserManager();
  213. $group = new \OC\Group\Group('group1', array($backend), $userManager);
  214. $backend->expects($this->once())
  215. ->method('usersInGroup')
  216. ->with('group1', 'user', 1, 1)
  217. ->will($this->returnValue(array('user2')));
  218. $users = $group->searchUsers('user', 1, 1);
  219. $this->assertEquals(1, count($users));
  220. $user2 = $users[0];
  221. $this->assertEquals('user2', $user2->getUID());
  222. }
  223. public function testSearchUsersMultipleBackendsLimitAndOffset() {
  224. $backend1 = $this->getMock('OC_Group_Database');
  225. $backend2 = $this->getMock('OC_Group_Database');
  226. $userManager = $this->getUserManager();
  227. $group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager);
  228. $backend1->expects($this->once())
  229. ->method('usersInGroup')
  230. ->with('group1', 'user', 2, 1)
  231. ->will($this->returnValue(array('user2')));
  232. $backend2->expects($this->once())
  233. ->method('usersInGroup')
  234. ->with('group1', 'user', 1, 0)
  235. ->will($this->returnValue(array('user1')));
  236. $users = $group->searchUsers('user', 2, 1);
  237. $this->assertEquals(2, count($users));
  238. $user2 = $users[0];
  239. $user1 = $users[1];
  240. $this->assertEquals('user2', $user2->getUID());
  241. $this->assertEquals('user1', $user1->getUID());
  242. }
  243. public function testDelete() {
  244. $backend = $this->getMock('OC_Group_Database');
  245. $userManager = $this->getUserManager();
  246. $group = new \OC\Group\Group('group1', array($backend), $userManager);
  247. $backend->expects($this->once())
  248. ->method('deleteGroup')
  249. ->with('group1');
  250. $backend->expects($this->any())
  251. ->method('implementsActions')
  252. ->will($this->returnValue(true));
  253. $group->delete();
  254. }
  255. }