1
0

group.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 \Test\TestCase {
  11. /**
  12. * @return \OC\User\Manager | \OC\User\Manager
  13. */
  14. protected function getUserManager() {
  15. $userManager = $this->getMock('\OC\User\Manager');
  16. $backend = $this->getMock('\OC_User_Backend');
  17. $user1 = new User('user1', $backend);
  18. $user2 = new User('user2', $backend);
  19. $user3 = new User('user3', $backend);
  20. $userManager->expects($this->any())
  21. ->method('get')
  22. ->will($this->returnValueMap(array(
  23. array('user1', $user1),
  24. array('user2', $user2),
  25. array('user3', $user3)
  26. )));
  27. return $userManager;
  28. }
  29. public function testGetUsersSingleBackend() {
  30. $backend = $this->getMock('OC_Group_Database');
  31. $userManager = $this->getUserManager();
  32. $group = new \OC\Group\Group('group1', array($backend), $userManager);
  33. $backend->expects($this->once())
  34. ->method('usersInGroup')
  35. ->with('group1')
  36. ->will($this->returnValue(array('user1', 'user2')));
  37. $users = $group->getUsers();
  38. $this->assertEquals(2, count($users));
  39. $user1 = $users['user1'];
  40. $user2 = $users['user2'];
  41. $this->assertEquals('user1', $user1->getUID());
  42. $this->assertEquals('user2', $user2->getUID());
  43. }
  44. public function testGetUsersMultipleBackends() {
  45. $backend1 = $this->getMock('OC_Group_Database');
  46. $backend2 = $this->getMock('OC_Group_Database');
  47. $userManager = $this->getUserManager();
  48. $group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager);
  49. $backend1->expects($this->once())
  50. ->method('usersInGroup')
  51. ->with('group1')
  52. ->will($this->returnValue(array('user1', 'user2')));
  53. $backend2->expects($this->once())
  54. ->method('usersInGroup')
  55. ->with('group1')
  56. ->will($this->returnValue(array('user2', 'user3')));
  57. $users = $group->getUsers();
  58. $this->assertEquals(3, count($users));
  59. $user1 = $users['user1'];
  60. $user2 = $users['user2'];
  61. $user3 = $users['user3'];
  62. $this->assertEquals('user1', $user1->getUID());
  63. $this->assertEquals('user2', $user2->getUID());
  64. $this->assertEquals('user3', $user3->getUID());
  65. }
  66. public function testInGroupSingleBackend() {
  67. $backend = $this->getMock('OC_Group_Database');
  68. $userManager = $this->getUserManager();
  69. $userBackend = $this->getMock('\OC_User_Backend');
  70. $group = new \OC\Group\Group('group1', array($backend), $userManager);
  71. $backend->expects($this->once())
  72. ->method('inGroup')
  73. ->with('user1', 'group1')
  74. ->will($this->returnValue(true));
  75. $this->assertTrue($group->inGroup(new User('user1', $userBackend)));
  76. }
  77. public function testInGroupMultipleBackends() {
  78. $backend1 = $this->getMock('OC_Group_Database');
  79. $backend2 = $this->getMock('OC_Group_Database');
  80. $userManager = $this->getUserManager();
  81. $userBackend = $this->getMock('\OC_User_Backend');
  82. $group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager);
  83. $backend1->expects($this->once())
  84. ->method('inGroup')
  85. ->with('user1', 'group1')
  86. ->will($this->returnValue(false));
  87. $backend2->expects($this->once())
  88. ->method('inGroup')
  89. ->with('user1', 'group1')
  90. ->will($this->returnValue(true));
  91. $this->assertTrue($group->inGroup(new User('user1', $userBackend)));
  92. }
  93. public function testAddUser() {
  94. $backend = $this->getMock('OC_Group_Database');
  95. $userManager = $this->getUserManager();
  96. $userBackend = $this->getMock('\OC_User_Backend');
  97. $group = new \OC\Group\Group('group1', array($backend), $userManager);
  98. $backend->expects($this->once())
  99. ->method('inGroup')
  100. ->with('user1', 'group1')
  101. ->will($this->returnValue(false));
  102. $backend->expects($this->any())
  103. ->method('implementsActions')
  104. ->will($this->returnValue(true));
  105. $backend->expects($this->once())
  106. ->method('addToGroup')
  107. ->with('user1', 'group1');
  108. $group->addUser(new User('user1', $userBackend));
  109. }
  110. public function testAddUserAlreadyInGroup() {
  111. $backend = $this->getMock('OC_Group_Database');
  112. $userManager = $this->getUserManager();
  113. $userBackend = $this->getMock('\OC_User_Backend');
  114. $group = new \OC\Group\Group('group1', array($backend), $userManager);
  115. $backend->expects($this->once())
  116. ->method('inGroup')
  117. ->with('user1', 'group1')
  118. ->will($this->returnValue(true));
  119. $backend->expects($this->any())
  120. ->method('implementsActions')
  121. ->will($this->returnValue(true));
  122. $backend->expects($this->never())
  123. ->method('addToGroup');
  124. $group->addUser(new User('user1', $userBackend));
  125. }
  126. public function testRemoveUser() {
  127. $backend = $this->getMock('OC_Group_Database');
  128. $userManager = $this->getUserManager();
  129. $userBackend = $this->getMock('\OC_User_Backend');
  130. $group = new \OC\Group\Group('group1', array($backend), $userManager);
  131. $backend->expects($this->once())
  132. ->method('inGroup')
  133. ->with('user1', 'group1')
  134. ->will($this->returnValue(true));
  135. $backend->expects($this->any())
  136. ->method('implementsActions')
  137. ->will($this->returnValue(true));
  138. $backend->expects($this->once())
  139. ->method('removeFromGroup')
  140. ->with('user1', 'group1');
  141. $group->removeUser(new User('user1', $userBackend));
  142. }
  143. public function testRemoveUserNotInGroup() {
  144. $backend = $this->getMock('OC_Group_Database');
  145. $userManager = $this->getUserManager();
  146. $userBackend = $this->getMock('\OC_User_Backend');
  147. $group = new \OC\Group\Group('group1', array($backend), $userManager);
  148. $backend->expects($this->once())
  149. ->method('inGroup')
  150. ->with('user1', 'group1')
  151. ->will($this->returnValue(false));
  152. $backend->expects($this->any())
  153. ->method('implementsActions')
  154. ->will($this->returnValue(true));
  155. $backend->expects($this->never())
  156. ->method('removeFromGroup');
  157. $group->removeUser(new User('user1', $userBackend));
  158. }
  159. public function testRemoveUserMultipleBackends() {
  160. $backend1 = $this->getMock('OC_Group_Database');
  161. $backend2 = $this->getMock('OC_Group_Database');
  162. $userManager = $this->getUserManager();
  163. $userBackend = $this->getMock('\OC_User_Backend');
  164. $group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager);
  165. $backend1->expects($this->once())
  166. ->method('inGroup')
  167. ->with('user1', 'group1')
  168. ->will($this->returnValue(true));
  169. $backend1->expects($this->any())
  170. ->method('implementsActions')
  171. ->will($this->returnValue(true));
  172. $backend1->expects($this->once())
  173. ->method('removeFromGroup')
  174. ->with('user1', 'group1');
  175. $backend2->expects($this->once())
  176. ->method('inGroup')
  177. ->with('user1', 'group1')
  178. ->will($this->returnValue(true));
  179. $backend2->expects($this->any())
  180. ->method('implementsActions')
  181. ->will($this->returnValue(true));
  182. $backend2->expects($this->once())
  183. ->method('removeFromGroup')
  184. ->with('user1', 'group1');
  185. $group->removeUser(new User('user1', $userBackend));
  186. }
  187. public function testSearchUsers() {
  188. $backend = $this->getMock('OC_Group_Database');
  189. $userManager = $this->getUserManager();
  190. $group = new \OC\Group\Group('group1', array($backend), $userManager);
  191. $backend->expects($this->once())
  192. ->method('usersInGroup')
  193. ->with('group1', '2')
  194. ->will($this->returnValue(array('user2')));
  195. $users = $group->searchUsers('2');
  196. $this->assertEquals(1, count($users));
  197. $user2 = $users[0];
  198. $this->assertEquals('user2', $user2->getUID());
  199. }
  200. public function testSearchUsersMultipleBackends() {
  201. $backend1 = $this->getMock('OC_Group_Database');
  202. $backend2 = $this->getMock('OC_Group_Database');
  203. $userManager = $this->getUserManager();
  204. $group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager);
  205. $backend1->expects($this->once())
  206. ->method('usersInGroup')
  207. ->with('group1', '2')
  208. ->will($this->returnValue(array('user2')));
  209. $backend2->expects($this->once())
  210. ->method('usersInGroup')
  211. ->with('group1', '2')
  212. ->will($this->returnValue(array('user2')));
  213. $users = $group->searchUsers('2');
  214. $this->assertEquals(1, count($users));
  215. $user2 = $users[0];
  216. $this->assertEquals('user2', $user2->getUID());
  217. }
  218. public function testSearchUsersLimitAndOffset() {
  219. $backend = $this->getMock('OC_Group_Database');
  220. $userManager = $this->getUserManager();
  221. $group = new \OC\Group\Group('group1', array($backend), $userManager);
  222. $backend->expects($this->once())
  223. ->method('usersInGroup')
  224. ->with('group1', 'user', 1, 1)
  225. ->will($this->returnValue(array('user2')));
  226. $users = $group->searchUsers('user', 1, 1);
  227. $this->assertEquals(1, count($users));
  228. $user2 = $users[0];
  229. $this->assertEquals('user2', $user2->getUID());
  230. }
  231. public function testSearchUsersMultipleBackendsLimitAndOffset() {
  232. $backend1 = $this->getMock('OC_Group_Database');
  233. $backend2 = $this->getMock('OC_Group_Database');
  234. $userManager = $this->getUserManager();
  235. $group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager);
  236. $backend1->expects($this->once())
  237. ->method('usersInGroup')
  238. ->with('group1', 'user', 2, 1)
  239. ->will($this->returnValue(array('user2')));
  240. $backend2->expects($this->once())
  241. ->method('usersInGroup')
  242. ->with('group1', 'user', 2, 1)
  243. ->will($this->returnValue(array('user1')));
  244. $users = $group->searchUsers('user', 2, 1);
  245. $this->assertEquals(2, count($users));
  246. $user2 = $users[0];
  247. $user1 = $users[1];
  248. $this->assertEquals('user2', $user2->getUID());
  249. $this->assertEquals('user1', $user1->getUID());
  250. }
  251. public function testCountUsers() {
  252. $backend1 = $this->getMock('OC_Group_Database');
  253. $userManager = $this->getUserManager();
  254. $group = new \OC\Group\Group('group1', array($backend1), $userManager);
  255. $backend1->expects($this->once())
  256. ->method('countUsersInGroup')
  257. ->with('group1', '2')
  258. ->will($this->returnValue(3));
  259. $backend1->expects($this->any())
  260. ->method('implementsActions')
  261. ->will($this->returnValue(true));
  262. $users = $group->count('2');
  263. $this->assertSame(3, $users);
  264. }
  265. public function testCountUsersMultipleBackends() {
  266. $backend1 = $this->getMock('OC_Group_Database');
  267. $backend2 = $this->getMock('OC_Group_Database');
  268. $userManager = $this->getUserManager();
  269. $group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager);
  270. $backend1->expects($this->once())
  271. ->method('countUsersInGroup')
  272. ->with('group1', '2')
  273. ->will($this->returnValue(3));
  274. $backend1->expects($this->any())
  275. ->method('implementsActions')
  276. ->will($this->returnValue(true));
  277. $backend2->expects($this->once())
  278. ->method('countUsersInGroup')
  279. ->with('group1', '2')
  280. ->will($this->returnValue(4));
  281. $backend2->expects($this->any())
  282. ->method('implementsActions')
  283. ->will($this->returnValue(true));
  284. $users = $group->count('2');
  285. $this->assertSame(7, $users);
  286. }
  287. public function testCountUsersNoMethod() {
  288. $backend1 = $this->getMock('OC_Group_Database');
  289. $userManager = $this->getUserManager();
  290. $group = new \OC\Group\Group('group1', array($backend1), $userManager);
  291. $backend1->expects($this->never())
  292. ->method('countUsersInGroup');
  293. $backend1->expects($this->any())
  294. ->method('implementsActions')
  295. ->will($this->returnValue(false));
  296. $users = $group->count('2');
  297. $this->assertSame(false, $users);
  298. }
  299. public function testDelete() {
  300. $backend = $this->getMock('OC_Group_Database');
  301. $userManager = $this->getUserManager();
  302. $group = new \OC\Group\Group('group1', array($backend), $userManager);
  303. $backend->expects($this->once())
  304. ->method('deleteGroup')
  305. ->with('group1');
  306. $backend->expects($this->any())
  307. ->method('implementsActions')
  308. ->will($this->returnValue(true));
  309. $group->delete();
  310. }
  311. }