GroupTest.php 14 KB

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