GroupTest.php 14 KB

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