ManagerTest.php 29 KB

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