ManagerTest.php 29 KB

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