GroupTest.php 14 KB

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