ManagerTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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-only
  6. */
  7. namespace Test\Group;
  8. use OC\Group\Database;
  9. use OC\User\Manager;
  10. use OC\User\User;
  11. use OCP\EventDispatcher\IEventDispatcher;
  12. use OCP\Group\Backend\ISearchableGroupBackend;
  13. use OCP\GroupInterface;
  14. use OCP\ICacheFactory;
  15. use OCP\IUser;
  16. use OCP\Security\Ip\IRemoteAddress;
  17. use PHPUnit\Framework\MockObject\MockObject;
  18. use Psr\Log\LoggerInterface;
  19. use Test\TestCase;
  20. interface ISearchableGroupInterface extends ISearchableGroupBackend, GroupInterface {
  21. }
  22. class ManagerTest extends TestCase {
  23. /** @var Manager|MockObject */
  24. protected $userManager;
  25. /** @var IEventDispatcher|MockObject */
  26. protected $dispatcher;
  27. /** @var LoggerInterface|MockObject */
  28. protected $logger;
  29. /** @var ICacheFactory|MockObject */
  30. private $cache;
  31. /** @var IRemoteAddress|MockObject */
  32. private $remoteIpAddress;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->userManager = $this->createMock(Manager::class);
  36. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  37. $this->logger = $this->createMock(LoggerInterface::class);
  38. $this->cache = $this->createMock(ICacheFactory::class);
  39. $this->remoteIpAddress = $this->createMock(IRemoteAddress::class);
  40. $this->remoteIpAddress->method('allowsAdminActions')->willReturn(true);
  41. }
  42. private function getTestUser($userId) {
  43. $mockUser = $this->createMock(IUser::class);
  44. $mockUser->expects($this->any())
  45. ->method('getUID')
  46. ->willReturn($userId);
  47. $mockUser->expects($this->any())
  48. ->method('getDisplayName')
  49. ->willReturn($userId);
  50. return $mockUser;
  51. }
  52. /**
  53. * @param null|int $implementedActions
  54. * @return \PHPUnit\Framework\MockObject\MockObject
  55. */
  56. private function getTestBackend($implementedActions = null) {
  57. if ($implementedActions === null) {
  58. $implementedActions =
  59. GroupInterface::ADD_TO_GROUP |
  60. GroupInterface::REMOVE_FROM_GOUP |
  61. GroupInterface::COUNT_USERS |
  62. GroupInterface::CREATE_GROUP |
  63. GroupInterface::DELETE_GROUP;
  64. }
  65. // need to declare it this way due to optional methods
  66. // thanks to the implementsActions logic
  67. $backend = $this->getMockBuilder(ISearchableGroupInterface::class)
  68. ->disableOriginalConstructor()
  69. ->setMethods([
  70. 'getGroupDetails',
  71. 'implementsActions',
  72. 'getUserGroups',
  73. 'inGroup',
  74. 'getGroups',
  75. 'groupExists',
  76. 'groupsExists',
  77. 'usersInGroup',
  78. 'createGroup',
  79. 'addToGroup',
  80. 'removeFromGroup',
  81. 'searchInGroup',
  82. ])
  83. ->getMock();
  84. $backend->expects($this->any())
  85. ->method('implementsActions')
  86. ->willReturnCallback(function ($actions) use ($implementedActions) {
  87. return (bool)($actions & $implementedActions);
  88. });
  89. return $backend;
  90. }
  91. public function testGet(): void {
  92. /**
  93. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  94. */
  95. $backend = $this->getTestBackend();
  96. $backend->expects($this->any())
  97. ->method('groupExists')
  98. ->with('group1')
  99. ->willReturn(true);
  100. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  101. $manager->addBackend($backend);
  102. $group = $manager->get('group1');
  103. $this->assertNotNull($group);
  104. $this->assertEquals('group1', $group->getGID());
  105. }
  106. public function testGetNoBackend(): void {
  107. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  108. $this->assertNull($manager->get('group1'));
  109. }
  110. public function testGetNotExists(): void {
  111. /**
  112. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  113. */
  114. $backend = $this->getTestBackend();
  115. $backend->expects($this->once())
  116. ->method('groupExists')
  117. ->with('group1')
  118. ->willReturn(false);
  119. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  120. $manager->addBackend($backend);
  121. $this->assertNull($manager->get('group1'));
  122. }
  123. public function testGetDeleted(): void {
  124. $backend = new \Test\Util\Group\Dummy();
  125. $backend->createGroup('group1');
  126. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  127. $manager->addBackend($backend);
  128. $group = $manager->get('group1');
  129. $group->delete();
  130. $this->assertNull($manager->get('group1'));
  131. }
  132. public function testGetMultipleBackends(): void {
  133. /**
  134. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend1
  135. */
  136. $backend1 = $this->getTestBackend();
  137. $backend1->expects($this->any())
  138. ->method('groupExists')
  139. ->with('group1')
  140. ->willReturn(false);
  141. /**
  142. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend2
  143. */
  144. $backend2 = $this->getTestBackend();
  145. $backend2->expects($this->any())
  146. ->method('groupExists')
  147. ->with('group1')
  148. ->willReturn(true);
  149. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  150. $manager->addBackend($backend1);
  151. $manager->addBackend($backend2);
  152. $group = $manager->get('group1');
  153. $this->assertNotNull($group);
  154. $this->assertEquals('group1', $group->getGID());
  155. }
  156. public function testCreate(): void {
  157. /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */
  158. $backendGroupCreated = false;
  159. $backend = $this->getTestBackend();
  160. $backend->expects($this->any())
  161. ->method('groupExists')
  162. ->with('group1')
  163. ->willReturnCallback(function () use (&$backendGroupCreated) {
  164. return $backendGroupCreated;
  165. });
  166. $backend->expects($this->once())
  167. ->method('createGroup')
  168. ->willReturnCallback(function () use (&$backendGroupCreated) {
  169. $backendGroupCreated = true;
  170. return true;
  171. });
  172. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  173. $manager->addBackend($backend);
  174. $group = $manager->createGroup('group1');
  175. $this->assertEquals('group1', $group->getGID());
  176. }
  177. public function testCreateFailure(): void {
  178. /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */
  179. $backendGroupCreated = false;
  180. $backend = $this->getTestBackend(
  181. GroupInterface::ADD_TO_GROUP |
  182. GroupInterface::REMOVE_FROM_GOUP |
  183. GroupInterface::COUNT_USERS |
  184. GroupInterface::CREATE_GROUP |
  185. GroupInterface::DELETE_GROUP |
  186. GroupInterface::GROUP_DETAILS
  187. );
  188. $backend->expects($this->any())
  189. ->method('groupExists')
  190. ->with('group1')
  191. ->willReturn(false);
  192. $backend->expects($this->once())
  193. ->method('createGroup')
  194. ->willReturn(false);
  195. $backend->expects($this->once())
  196. ->method('getGroupDetails')
  197. ->willReturn([]);
  198. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  199. $manager->addBackend($backend);
  200. $group = $manager->createGroup('group1');
  201. $this->assertEquals(null, $group);
  202. }
  203. public function testCreateTooLong(): void {
  204. /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */
  205. $backendGroupCreated = false;
  206. $backend = $this->getTestBackend(
  207. GroupInterface::ADD_TO_GROUP |
  208. GroupInterface::REMOVE_FROM_GOUP |
  209. GroupInterface::COUNT_USERS |
  210. GroupInterface::CREATE_GROUP |
  211. GroupInterface::DELETE_GROUP |
  212. GroupInterface::GROUP_DETAILS
  213. );
  214. $groupName = str_repeat('x', 256);
  215. $backend->expects($this->any())
  216. ->method('groupExists')
  217. ->with($groupName)
  218. ->willReturn(false);
  219. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  220. $manager->addBackend($backend);
  221. $this->expectException(\Exception::class);
  222. $group = $manager->createGroup($groupName);
  223. }
  224. public function testCreateExists(): void {
  225. /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */
  226. $backend = $this->getTestBackend();
  227. $backend->expects($this->any())
  228. ->method('groupExists')
  229. ->with('group1')
  230. ->willReturn(true);
  231. $backend->expects($this->never())
  232. ->method('createGroup');
  233. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  234. $manager->addBackend($backend);
  235. $group = $manager->createGroup('group1');
  236. $this->assertEquals('group1', $group->getGID());
  237. }
  238. public function testSearch(): void {
  239. /**
  240. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  241. */
  242. $backend = $this->getTestBackend();
  243. $backend->expects($this->once())
  244. ->method('getGroups')
  245. ->with('1')
  246. ->willReturn(['group1']);
  247. $backend->expects($this->once())
  248. ->method('groupExists')
  249. ->with('group1')
  250. ->willReturn(true);
  251. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  252. $manager->addBackend($backend);
  253. $groups = $manager->search('1');
  254. $this->assertCount(1, $groups);
  255. $group1 = reset($groups);
  256. $this->assertEquals('group1', $group1->getGID());
  257. }
  258. public function testSearchMultipleBackends(): void {
  259. /**
  260. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend1
  261. */
  262. $backend1 = $this->getTestBackend();
  263. $backend1->expects($this->once())
  264. ->method('getGroups')
  265. ->with('1')
  266. ->willReturn(['group1']);
  267. $backend1->expects($this->any())
  268. ->method('groupExists')
  269. ->willReturn(true);
  270. /**
  271. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend2
  272. */
  273. $backend2 = $this->getTestBackend();
  274. $backend2->expects($this->once())
  275. ->method('getGroups')
  276. ->with('1')
  277. ->willReturn(['group12', 'group1']);
  278. $backend2->expects($this->any())
  279. ->method('groupExists')
  280. ->willReturn(true);
  281. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  282. $manager->addBackend($backend1);
  283. $manager->addBackend($backend2);
  284. $groups = $manager->search('1');
  285. $this->assertCount(2, $groups);
  286. $group1 = reset($groups);
  287. $group12 = next($groups);
  288. $this->assertEquals('group1', $group1->getGID());
  289. $this->assertEquals('group12', $group12->getGID());
  290. }
  291. public function testSearchMultipleBackendsLimitAndOffset(): void {
  292. /**
  293. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend1
  294. */
  295. $backend1 = $this->getTestBackend();
  296. $backend1->expects($this->once())
  297. ->method('getGroups')
  298. ->with('1', 2, 1)
  299. ->willReturn(['group1']);
  300. $backend1->expects($this->any())
  301. ->method('groupExists')
  302. ->willReturn(true);
  303. /**
  304. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend2
  305. */
  306. $backend2 = $this->getTestBackend();
  307. $backend2->expects($this->once())
  308. ->method('getGroups')
  309. ->with('1', 2, 1)
  310. ->willReturn(['group12']);
  311. $backend2->expects($this->any())
  312. ->method('groupExists')
  313. ->willReturn(true);
  314. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  315. $manager->addBackend($backend1);
  316. $manager->addBackend($backend2);
  317. $groups = $manager->search('1', 2, 1);
  318. $this->assertCount(2, $groups);
  319. $group1 = reset($groups);
  320. $group12 = next($groups);
  321. $this->assertEquals('group1', $group1->getGID());
  322. $this->assertEquals('group12', $group12->getGID());
  323. }
  324. public function testSearchResultExistsButGroupDoesNot(): void {
  325. /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */
  326. $backend = $this->createMock(Database::class);
  327. $backend->expects($this->once())
  328. ->method('getGroups')
  329. ->with('1')
  330. ->willReturn(['group1']);
  331. $backend->expects($this->never())
  332. ->method('groupExists');
  333. $backend->expects($this->once())
  334. ->method('getGroupsDetails')
  335. ->with(['group1'])
  336. ->willReturn([]);
  337. /** @var \OC\User\Manager $userManager */
  338. $userManager = $this->createMock(Manager::class);
  339. $manager = new \OC\Group\Manager($userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  340. $manager->addBackend($backend);
  341. $groups = $manager->search('1');
  342. $this->assertEmpty($groups);
  343. }
  344. public function testGetUserGroups(): void {
  345. /**
  346. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  347. */
  348. $backend = $this->getTestBackend();
  349. $backend->expects($this->once())
  350. ->method('getUserGroups')
  351. ->with('user1')
  352. ->willReturn(['group1']);
  353. $backend->expects($this->any())
  354. ->method('groupExists')
  355. ->with('group1')
  356. ->willReturn(true);
  357. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  358. $manager->addBackend($backend);
  359. $groups = $manager->getUserGroups($this->getTestUser('user1'));
  360. $this->assertCount(1, $groups);
  361. $group1 = reset($groups);
  362. $this->assertEquals('group1', $group1->getGID());
  363. }
  364. public function testGetUserGroupIds(): void {
  365. /**
  366. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  367. */
  368. $backend = $this->getTestBackend();
  369. $backend->method('getUserGroups')
  370. ->with('myUID')
  371. ->willReturn(['123', 'abc']);
  372. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  373. $manager->addBackend($backend);
  374. /** @var \OC\User\User|\PHPUnit\Framework\MockObject\MockObject $user */
  375. $user = $this->createMock(IUser::class);
  376. $user->method('getUID')
  377. ->willReturn('myUID');
  378. $groups = $manager->getUserGroupIds($user);
  379. $this->assertCount(2, $groups);
  380. foreach ($groups as $group) {
  381. $this->assertIsString($group);
  382. }
  383. }
  384. public function testGetUserGroupsWithDeletedGroup(): void {
  385. /**
  386. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  387. */
  388. $backend = $this->createMock(Database::class);
  389. $backend->expects($this->once())
  390. ->method('getUserGroups')
  391. ->with('user1')
  392. ->willReturn(['group1']);
  393. $backend->expects($this->any())
  394. ->method('groupExists')
  395. ->with('group1')
  396. ->willReturn(false);
  397. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  398. $manager->addBackend($backend);
  399. /** @var \OC\User\User|\PHPUnit\Framework\MockObject\MockObject $user */
  400. $user = $this->createMock(IUser::class);
  401. $user->expects($this->atLeastOnce())
  402. ->method('getUID')
  403. ->willReturn('user1');
  404. $groups = $manager->getUserGroups($user);
  405. $this->assertEmpty($groups);
  406. }
  407. public function testInGroup(): void {
  408. /**
  409. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  410. */
  411. $backend = $this->getTestBackend();
  412. $backend->expects($this->once())
  413. ->method('getUserGroups')
  414. ->with('user1')
  415. ->willReturn(['group1', 'admin', 'group2']);
  416. $backend->expects($this->any())
  417. ->method('groupExists')
  418. ->willReturn(true);
  419. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  420. $manager->addBackend($backend);
  421. $this->assertTrue($manager->isInGroup('user1', 'group1'));
  422. }
  423. public function testIsAdmin(): void {
  424. /**
  425. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  426. */
  427. $backend = $this->getTestBackend();
  428. $backend->expects($this->once())
  429. ->method('getUserGroups')
  430. ->with('user1')
  431. ->willReturn(['group1', 'admin', 'group2']);
  432. $backend->expects($this->any())
  433. ->method('groupExists')
  434. ->willReturn(true);
  435. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  436. $manager->addBackend($backend);
  437. $this->assertTrue($manager->isAdmin('user1'));
  438. }
  439. public function testNotAdmin(): void {
  440. /**
  441. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  442. */
  443. $backend = $this->getTestBackend();
  444. $backend->expects($this->once())
  445. ->method('getUserGroups')
  446. ->with('user1')
  447. ->willReturn(['group1', 'group2']);
  448. $backend->expects($this->any())
  449. ->method('groupExists')
  450. ->willReturn(true);
  451. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  452. $manager->addBackend($backend);
  453. $this->assertFalse($manager->isAdmin('user1'));
  454. }
  455. public function testGetUserGroupsMultipleBackends(): void {
  456. /**
  457. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend1
  458. */
  459. $backend1 = $this->getTestBackend();
  460. $backend1->expects($this->once())
  461. ->method('getUserGroups')
  462. ->with('user1')
  463. ->willReturn(['group1']);
  464. $backend1->expects($this->any())
  465. ->method('groupExists')
  466. ->willReturn(true);
  467. /**
  468. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend2
  469. */
  470. $backend2 = $this->getTestBackend();
  471. $backend2->expects($this->once())
  472. ->method('getUserGroups')
  473. ->with('user1')
  474. ->willReturn(['group1', 'group2']);
  475. $backend1->expects($this->any())
  476. ->method('groupExists')
  477. ->willReturn(true);
  478. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  479. $manager->addBackend($backend1);
  480. $manager->addBackend($backend2);
  481. $groups = $manager->getUserGroups($this->getTestUser('user1'));
  482. $this->assertCount(2, $groups);
  483. $group1 = reset($groups);
  484. $group2 = next($groups);
  485. $this->assertEquals('group1', $group1->getGID());
  486. $this->assertEquals('group2', $group2->getGID());
  487. }
  488. public function testDisplayNamesInGroupWithOneUserBackend(): void {
  489. /**
  490. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  491. */
  492. $backend = $this->getTestBackend();
  493. $backend->expects($this->exactly(1))
  494. ->method('groupExists')
  495. ->with('testgroup')
  496. ->willReturn(true);
  497. $backend->expects($this->any())
  498. ->method('inGroup')
  499. ->willReturnCallback(function ($uid, $gid) {
  500. switch ($uid) {
  501. case 'user1': return false;
  502. case 'user2': return true;
  503. case 'user3': return false;
  504. case 'user33': return true;
  505. default:
  506. return null;
  507. }
  508. });
  509. $this->userManager->expects($this->any())
  510. ->method('searchDisplayName')
  511. ->with('user3')
  512. ->willReturnCallback(function ($search, $limit, $offset) {
  513. switch ($offset) {
  514. case 0: return ['user3' => $this->getTestUser('user3'),
  515. 'user33' => $this->getTestUser('user33')];
  516. case 2: return [];
  517. }
  518. return null;
  519. });
  520. $this->userManager->expects($this->any())
  521. ->method('get')
  522. ->willReturnCallback(function ($uid) {
  523. switch ($uid) {
  524. case 'user1': return $this->getTestUser('user1');
  525. case 'user2': return $this->getTestUser('user2');
  526. case 'user3': return $this->getTestUser('user3');
  527. case 'user33': return $this->getTestUser('user33');
  528. default:
  529. return null;
  530. }
  531. });
  532. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  533. $manager->addBackend($backend);
  534. $users = $manager->displayNamesInGroup('testgroup', 'user3');
  535. $this->assertCount(1, $users);
  536. $this->assertFalse(isset($users['user1']));
  537. $this->assertFalse(isset($users['user2']));
  538. $this->assertFalse(isset($users['user3']));
  539. $this->assertTrue(isset($users['user33']));
  540. }
  541. public function testDisplayNamesInGroupWithOneUserBackendWithLimitSpecified(): void {
  542. /**
  543. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  544. */
  545. $backend = $this->getTestBackend();
  546. $backend->expects($this->exactly(1))
  547. ->method('groupExists')
  548. ->with('testgroup')
  549. ->willReturn(true);
  550. $backend->expects($this->any())
  551. ->method('inGroup')
  552. ->willReturnCallback(function ($uid, $gid) {
  553. switch ($uid) {
  554. case 'user1': return false;
  555. case 'user2': return true;
  556. case 'user3': return false;
  557. case 'user33': return true;
  558. case 'user333': return true;
  559. default:
  560. return null;
  561. }
  562. });
  563. $this->userManager->expects($this->any())
  564. ->method('searchDisplayName')
  565. ->with('user3')
  566. ->willReturnCallback(function ($search, $limit, $offset) {
  567. switch ($offset) {
  568. case 0: return ['user3' => $this->getTestUser('user3'),
  569. 'user33' => $this->getTestUser('user33')];
  570. case 2: return ['user333' => $this->getTestUser('user333')];
  571. }
  572. return null;
  573. });
  574. $this->userManager->expects($this->any())
  575. ->method('get')
  576. ->willReturnCallback(function ($uid) {
  577. switch ($uid) {
  578. case 'user1': return $this->getTestUser('user1');
  579. case 'user2': return $this->getTestUser('user2');
  580. case 'user3': return $this->getTestUser('user3');
  581. case 'user33': return $this->getTestUser('user33');
  582. case 'user333': return $this->getTestUser('user333');
  583. default:
  584. return null;
  585. }
  586. });
  587. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  588. $manager->addBackend($backend);
  589. $users = $manager->displayNamesInGroup('testgroup', 'user3', 1);
  590. $this->assertCount(1, $users);
  591. $this->assertFalse(isset($users['user1']));
  592. $this->assertFalse(isset($users['user2']));
  593. $this->assertFalse(isset($users['user3']));
  594. $this->assertTrue(isset($users['user33']));
  595. $this->assertFalse(isset($users['user333']));
  596. }
  597. public function testDisplayNamesInGroupWithOneUserBackendWithLimitAndOffsetSpecified(): void {
  598. /**
  599. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  600. */
  601. $backend = $this->getTestBackend();
  602. $backend->expects($this->exactly(1))
  603. ->method('groupExists')
  604. ->with('testgroup')
  605. ->willReturn(true);
  606. $backend->expects($this->any())
  607. ->method('inGroup')
  608. ->willReturnCallback(function ($uid) {
  609. switch ($uid) {
  610. case 'user1': return false;
  611. case 'user2': return true;
  612. case 'user3': return false;
  613. case 'user33': return true;
  614. case 'user333': return true;
  615. default:
  616. return null;
  617. }
  618. });
  619. $this->userManager->expects($this->any())
  620. ->method('searchDisplayName')
  621. ->with('user3')
  622. ->willReturnCallback(function ($search, $limit, $offset) {
  623. switch ($offset) {
  624. case 0:
  625. return [
  626. 'user3' => $this->getTestUser('user3'),
  627. 'user33' => $this->getTestUser('user33'),
  628. 'user333' => $this->getTestUser('user333')
  629. ];
  630. }
  631. return null;
  632. });
  633. $this->userManager->expects($this->any())
  634. ->method('get')
  635. ->willReturnCallback(function ($uid) {
  636. switch ($uid) {
  637. case 'user1': return $this->getTestUser('user1');
  638. case 'user2': return $this->getTestUser('user2');
  639. case 'user3': return $this->getTestUser('user3');
  640. case 'user33': return $this->getTestUser('user33');
  641. case 'user333': return $this->getTestUser('user333');
  642. default:
  643. return null;
  644. }
  645. });
  646. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  647. $manager->addBackend($backend);
  648. $users = $manager->displayNamesInGroup('testgroup', 'user3', 1, 1);
  649. $this->assertCount(1, $users);
  650. $this->assertFalse(isset($users['user1']));
  651. $this->assertFalse(isset($users['user2']));
  652. $this->assertFalse(isset($users['user3']));
  653. $this->assertFalse(isset($users['user33']));
  654. $this->assertTrue(isset($users['user333']));
  655. }
  656. public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmpty(): void {
  657. /**
  658. * @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend
  659. */
  660. $backend = $this->getTestBackend();
  661. $backend->expects($this->exactly(1))
  662. ->method('groupExists')
  663. ->with('testgroup')
  664. ->willReturn(true);
  665. $backend->expects($this->once())
  666. ->method('searchInGroup')
  667. ->with('testgroup', '', -1, 0)
  668. ->willReturn(['user2' => $this->getTestUser('user2'), 'user33' => $this->getTestUser('user33')]);
  669. $this->userManager->expects($this->never())->method('get');
  670. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  671. $manager->addBackend($backend);
  672. $users = $manager->displayNamesInGroup('testgroup', '');
  673. $this->assertCount(2, $users);
  674. $this->assertFalse(isset($users['user1']));
  675. $this->assertTrue(isset($users['user2']));
  676. $this->assertFalse(isset($users['user3']));
  677. $this->assertTrue(isset($users['user33']));
  678. }
  679. public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitSpecified(): void {
  680. /**
  681. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  682. */
  683. $backend = $this->getTestBackend();
  684. $backend->expects($this->exactly(1))
  685. ->method('groupExists')
  686. ->with('testgroup')
  687. ->willReturn(true);
  688. $backend->expects($this->once())
  689. ->method('searchInGroup')
  690. ->with('testgroup', '', 1, 0)
  691. ->willReturn([new User('user2', null, $this->dispatcher)]);
  692. $this->userManager->expects($this->never())->method('get');
  693. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  694. $manager->addBackend($backend);
  695. $users = $manager->displayNamesInGroup('testgroup', '', 1);
  696. $this->assertCount(1, $users);
  697. $this->assertFalse(isset($users['user1']));
  698. $this->assertTrue(isset($users['user2']));
  699. $this->assertFalse(isset($users['user3']));
  700. $this->assertFalse(isset($users['user33']));
  701. }
  702. public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitAndOffsetSpecified(): void {
  703. /**
  704. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  705. */
  706. $backend = $this->getTestBackend();
  707. $backend->expects($this->exactly(1))
  708. ->method('groupExists')
  709. ->with('testgroup')
  710. ->willReturn(true);
  711. $backend->expects($this->once())
  712. ->method('searchInGroup')
  713. ->with('testgroup', '', 1, 1)
  714. ->willReturn(['user33' => $this->getTestUser('user33')]);
  715. $this->userManager->expects($this->never())->method('get');
  716. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  717. $manager->addBackend($backend);
  718. $users = $manager->displayNamesInGroup('testgroup', '', 1, 1);
  719. $this->assertCount(1, $users);
  720. $this->assertFalse(isset($users['user1']));
  721. $this->assertFalse(isset($users['user2']));
  722. $this->assertFalse(isset($users['user3']));
  723. $this->assertTrue(isset($users['user33']));
  724. }
  725. public function testGetUserGroupsWithAddUser(): void {
  726. /**
  727. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  728. */
  729. $backend = $this->getTestBackend();
  730. $expectedGroups = [];
  731. $backend->expects($this->any())
  732. ->method('getUserGroups')
  733. ->with('user1')
  734. ->willReturnCallback(function () use (&$expectedGroups) {
  735. return $expectedGroups;
  736. });
  737. $backend->expects($this->any())
  738. ->method('groupExists')
  739. ->with('group1')
  740. ->willReturn(true);
  741. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  742. $manager->addBackend($backend);
  743. // prime cache
  744. $user1 = $this->getTestUser('user1');
  745. $groups = $manager->getUserGroups($user1);
  746. $this->assertEquals([], $groups);
  747. // add user
  748. $group = $manager->get('group1');
  749. $group->addUser($user1);
  750. $expectedGroups[] = 'group1';
  751. // check result
  752. $groups = $manager->getUserGroups($user1);
  753. $this->assertCount(1, $groups);
  754. $group1 = reset($groups);
  755. $this->assertEquals('group1', $group1->getGID());
  756. }
  757. public function testGetUserGroupsWithRemoveUser(): void {
  758. /**
  759. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  760. */
  761. $backend = $this->getTestBackend();
  762. $expectedGroups = ['group1'];
  763. $backend->expects($this->any())
  764. ->method('getUserGroups')
  765. ->with('user1')
  766. ->willReturnCallback(function () use (&$expectedGroups) {
  767. return $expectedGroups;
  768. });
  769. $backend->expects($this->any())
  770. ->method('groupExists')
  771. ->with('group1')
  772. ->willReturn(true);
  773. $backend->expects($this->once())
  774. ->method('inGroup')
  775. ->willReturn(true);
  776. $backend->expects($this->once())
  777. ->method('removeFromGroup')
  778. ->willReturn(true);
  779. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  780. $manager->addBackend($backend);
  781. // prime cache
  782. $user1 = $this->getTestUser('user1');
  783. $groups = $manager->getUserGroups($user1);
  784. $this->assertCount(1, $groups);
  785. $group1 = reset($groups);
  786. $this->assertEquals('group1', $group1->getGID());
  787. // remove user
  788. $group = $manager->get('group1');
  789. $group->removeUser($user1);
  790. $expectedGroups = [];
  791. // check result
  792. $groups = $manager->getUserGroups($user1);
  793. $this->assertEquals($expectedGroups, $groups);
  794. }
  795. public function testGetUserIdGroups(): void {
  796. /**
  797. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  798. */
  799. $backend = $this->getTestBackend();
  800. $backend->expects($this->any())
  801. ->method('getUserGroups')
  802. ->with('user1')
  803. ->willReturn(null);
  804. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  805. $manager->addBackend($backend);
  806. $groups = $manager->getUserIdGroups('user1');
  807. $this->assertEquals([], $groups);
  808. }
  809. public function testGroupDisplayName(): void {
  810. /**
  811. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  812. */
  813. $backend = $this->getTestBackend(
  814. GroupInterface::ADD_TO_GROUP |
  815. GroupInterface::REMOVE_FROM_GOUP |
  816. GroupInterface::COUNT_USERS |
  817. GroupInterface::CREATE_GROUP |
  818. GroupInterface::DELETE_GROUP |
  819. GroupInterface::GROUP_DETAILS
  820. );
  821. $backend->expects($this->any())
  822. ->method('getGroupDetails')
  823. ->willReturnMap([
  824. ['group1', ['gid' => 'group1', 'displayName' => 'Group One']],
  825. ['group2', ['gid' => 'group2']],
  826. ]);
  827. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
  828. $manager->addBackend($backend);
  829. // group with display name
  830. $group = $manager->get('group1');
  831. $this->assertNotNull($group);
  832. $this->assertEquals('group1', $group->getGID());
  833. $this->assertEquals('Group One', $group->getDisplayName());
  834. // group without display name
  835. $group = $manager->get('group2');
  836. $this->assertNotNull($group);
  837. $this->assertEquals('group2', $group->getGID());
  838. $this->assertEquals('group2', $group->getDisplayName());
  839. }
  840. }