GroupTest.php 14 KB

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