ManagerTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. ->willReturn(['123', 'abc']);
  348. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
  349. $manager->addBackend($backend);
  350. /** @var \OC\User\User|\PHPUnit\Framework\MockObject\MockObject $user */
  351. $user = $this->createMock(IUser::class);
  352. $groups = $manager->getUserGroupIds($user);
  353. $this->assertCount(2, $groups);
  354. foreach ($groups as $group) {
  355. $this->assertIsString($group);
  356. }
  357. }
  358. public function testGetUserGroupsWithDeletedGroup() {
  359. /**
  360. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  361. */
  362. $backend = $this->createMock(Database::class);
  363. $backend->expects($this->once())
  364. ->method('getUserGroups')
  365. ->with('user1')
  366. ->willReturn(['group1']);
  367. $backend->expects($this->any())
  368. ->method('groupExists')
  369. ->with('group1')
  370. ->willReturn(false);
  371. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
  372. $manager->addBackend($backend);
  373. /** @var \OC\User\User|\PHPUnit\Framework\MockObject\MockObject $user */
  374. $user = $this->createMock(IUser::class);
  375. $user->expects($this->atLeastOnce())
  376. ->method('getUID')
  377. ->willReturn('user1');
  378. $groups = $manager->getUserGroups($user);
  379. $this->assertEmpty($groups);
  380. }
  381. public function testInGroup() {
  382. /**
  383. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  384. */
  385. $backend = $this->getTestBackend();
  386. $backend->expects($this->once())
  387. ->method('getUserGroups')
  388. ->with('user1')
  389. ->willReturn(['group1', 'admin', 'group2']);
  390. $backend->expects($this->any())
  391. ->method('groupExists')
  392. ->willReturn(true);
  393. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
  394. $manager->addBackend($backend);
  395. $this->assertTrue($manager->isInGroup('user1', 'group1'));
  396. }
  397. public function testIsAdmin() {
  398. /**
  399. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  400. */
  401. $backend = $this->getTestBackend();
  402. $backend->expects($this->once())
  403. ->method('getUserGroups')
  404. ->with('user1')
  405. ->willReturn(['group1', 'admin', 'group2']);
  406. $backend->expects($this->any())
  407. ->method('groupExists')
  408. ->willReturn(true);
  409. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
  410. $manager->addBackend($backend);
  411. $this->assertTrue($manager->isAdmin('user1'));
  412. }
  413. public function testNotAdmin() {
  414. /**
  415. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  416. */
  417. $backend = $this->getTestBackend();
  418. $backend->expects($this->once())
  419. ->method('getUserGroups')
  420. ->with('user1')
  421. ->willReturn(['group1', 'group2']);
  422. $backend->expects($this->any())
  423. ->method('groupExists')
  424. ->willReturn(true);
  425. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
  426. $manager->addBackend($backend);
  427. $this->assertFalse($manager->isAdmin('user1'));
  428. }
  429. public function testGetUserGroupsMultipleBackends() {
  430. /**
  431. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend1
  432. */
  433. $backend1 = $this->getTestBackend();
  434. $backend1->expects($this->once())
  435. ->method('getUserGroups')
  436. ->with('user1')
  437. ->willReturn(['group1']);
  438. $backend1->expects($this->any())
  439. ->method('groupExists')
  440. ->willReturn(true);
  441. /**
  442. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend2
  443. */
  444. $backend2 = $this->getTestBackend();
  445. $backend2->expects($this->once())
  446. ->method('getUserGroups')
  447. ->with('user1')
  448. ->willReturn(['group1', 'group2']);
  449. $backend1->expects($this->any())
  450. ->method('groupExists')
  451. ->willReturn(true);
  452. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
  453. $manager->addBackend($backend1);
  454. $manager->addBackend($backend2);
  455. $groups = $manager->getUserGroups($this->getTestUser('user1'));
  456. $this->assertCount(2, $groups);
  457. $group1 = reset($groups);
  458. $group2 = next($groups);
  459. $this->assertEquals('group1', $group1->getGID());
  460. $this->assertEquals('group2', $group2->getGID());
  461. }
  462. public function testDisplayNamesInGroupWithOneUserBackend() {
  463. /**
  464. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  465. */
  466. $backend = $this->getTestBackend();
  467. $backend->expects($this->exactly(1))
  468. ->method('groupExists')
  469. ->with('testgroup')
  470. ->willReturn(true);
  471. $backend->expects($this->any())
  472. ->method('inGroup')
  473. ->willReturnCallback(function ($uid, $gid) {
  474. switch ($uid) {
  475. case 'user1': return false;
  476. case 'user2': return true;
  477. case 'user3': return false;
  478. case 'user33': return true;
  479. default:
  480. return null;
  481. }
  482. });
  483. $this->userManager->expects($this->any())
  484. ->method('searchDisplayName')
  485. ->with('user3')
  486. ->willReturnCallback(function ($search, $limit, $offset) {
  487. switch ($offset) {
  488. case 0: return ['user3' => $this->getTestUser('user3'),
  489. 'user33' => $this->getTestUser('user33')];
  490. case 2: return [];
  491. }
  492. return null;
  493. });
  494. $this->userManager->expects($this->any())
  495. ->method('get')
  496. ->willReturnCallback(function ($uid) {
  497. switch ($uid) {
  498. case 'user1': return $this->getTestUser('user1');
  499. case 'user2': return $this->getTestUser('user2');
  500. case 'user3': return $this->getTestUser('user3');
  501. case 'user33': return $this->getTestUser('user33');
  502. default:
  503. return null;
  504. }
  505. });
  506. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
  507. $manager->addBackend($backend);
  508. $users = $manager->displayNamesInGroup('testgroup', 'user3');
  509. $this->assertCount(1, $users);
  510. $this->assertFalse(isset($users['user1']));
  511. $this->assertFalse(isset($users['user2']));
  512. $this->assertFalse(isset($users['user3']));
  513. $this->assertTrue(isset($users['user33']));
  514. }
  515. public function testDisplayNamesInGroupWithOneUserBackendWithLimitSpecified() {
  516. /**
  517. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  518. */
  519. $backend = $this->getTestBackend();
  520. $backend->expects($this->exactly(1))
  521. ->method('groupExists')
  522. ->with('testgroup')
  523. ->willReturn(true);
  524. $backend->expects($this->any())
  525. ->method('inGroup')
  526. ->willReturnCallback(function ($uid, $gid) {
  527. switch ($uid) {
  528. case 'user1': return false;
  529. case 'user2': return true;
  530. case 'user3': return false;
  531. case 'user33': return true;
  532. case 'user333': return true;
  533. default:
  534. return null;
  535. }
  536. });
  537. $this->userManager->expects($this->any())
  538. ->method('searchDisplayName')
  539. ->with('user3')
  540. ->willReturnCallback(function ($search, $limit, $offset) {
  541. switch ($offset) {
  542. case 0: return ['user3' => $this->getTestUser('user3'),
  543. 'user33' => $this->getTestUser('user33')];
  544. case 2: return ['user333' => $this->getTestUser('user333')];
  545. }
  546. return null;
  547. });
  548. $this->userManager->expects($this->any())
  549. ->method('get')
  550. ->willReturnCallback(function ($uid) {
  551. switch ($uid) {
  552. case 'user1': return $this->getTestUser('user1');
  553. case 'user2': return $this->getTestUser('user2');
  554. case 'user3': return $this->getTestUser('user3');
  555. case 'user33': return $this->getTestUser('user33');
  556. case 'user333': return $this->getTestUser('user333');
  557. default:
  558. return null;
  559. }
  560. });
  561. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
  562. $manager->addBackend($backend);
  563. $users = $manager->displayNamesInGroup('testgroup', 'user3', 1);
  564. $this->assertCount(1, $users);
  565. $this->assertFalse(isset($users['user1']));
  566. $this->assertFalse(isset($users['user2']));
  567. $this->assertFalse(isset($users['user3']));
  568. $this->assertTrue(isset($users['user33']));
  569. $this->assertFalse(isset($users['user333']));
  570. }
  571. public function testDisplayNamesInGroupWithOneUserBackendWithLimitAndOffsetSpecified() {
  572. /**
  573. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  574. */
  575. $backend = $this->getTestBackend();
  576. $backend->expects($this->exactly(1))
  577. ->method('groupExists')
  578. ->with('testgroup')
  579. ->willReturn(true);
  580. $backend->expects($this->any())
  581. ->method('inGroup')
  582. ->willReturnCallback(function ($uid) {
  583. switch ($uid) {
  584. case 'user1': return false;
  585. case 'user2': return true;
  586. case 'user3': return false;
  587. case 'user33': return true;
  588. case 'user333': return true;
  589. default:
  590. return null;
  591. }
  592. });
  593. $this->userManager->expects($this->any())
  594. ->method('searchDisplayName')
  595. ->with('user3')
  596. ->willReturnCallback(function ($search, $limit, $offset) {
  597. switch ($offset) {
  598. case 0:
  599. return [
  600. 'user3' => $this->getTestUser('user3'),
  601. 'user33' => $this->getTestUser('user33'),
  602. 'user333' => $this->getTestUser('user333')
  603. ];
  604. }
  605. return null;
  606. });
  607. $this->userManager->expects($this->any())
  608. ->method('get')
  609. ->willReturnCallback(function ($uid) {
  610. switch ($uid) {
  611. case 'user1': return $this->getTestUser('user1');
  612. case 'user2': return $this->getTestUser('user2');
  613. case 'user3': return $this->getTestUser('user3');
  614. case 'user33': return $this->getTestUser('user33');
  615. case 'user333': return $this->getTestUser('user333');
  616. default:
  617. return null;
  618. }
  619. });
  620. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
  621. $manager->addBackend($backend);
  622. $users = $manager->displayNamesInGroup('testgroup', 'user3', 1, 1);
  623. $this->assertCount(1, $users);
  624. $this->assertFalse(isset($users['user1']));
  625. $this->assertFalse(isset($users['user2']));
  626. $this->assertFalse(isset($users['user3']));
  627. $this->assertFalse(isset($users['user33']));
  628. $this->assertTrue(isset($users['user333']));
  629. }
  630. public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmpty() {
  631. /**
  632. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  633. */
  634. $backend = $this->getTestBackend();
  635. $backend->expects($this->exactly(1))
  636. ->method('groupExists')
  637. ->with('testgroup')
  638. ->willReturn(true);
  639. $backend->expects($this->once())
  640. ->method('usersInGroup')
  641. ->with('testgroup', '', -1, 0)
  642. ->willReturn(['user2', 'user33']);
  643. $this->userManager->expects($this->any())
  644. ->method('get')
  645. ->willReturnCallback(function ($uid) {
  646. switch ($uid) {
  647. case 'user1': return $this->getTestUser('user1');
  648. case 'user2': return $this->getTestUser('user2');
  649. case 'user3': return $this->getTestUser('user3');
  650. case 'user33': return $this->getTestUser('user33');
  651. default:
  652. return null;
  653. }
  654. });
  655. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
  656. $manager->addBackend($backend);
  657. $users = $manager->displayNamesInGroup('testgroup', '');
  658. $this->assertCount(2, $users);
  659. $this->assertFalse(isset($users['user1']));
  660. $this->assertTrue(isset($users['user2']));
  661. $this->assertFalse(isset($users['user3']));
  662. $this->assertTrue(isset($users['user33']));
  663. }
  664. public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitSpecified() {
  665. /**
  666. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  667. */
  668. $backend = $this->getTestBackend();
  669. $backend->expects($this->exactly(1))
  670. ->method('groupExists')
  671. ->with('testgroup')
  672. ->willReturn(true);
  673. $backend->expects($this->once())
  674. ->method('usersInGroup')
  675. ->with('testgroup', '', 1, 0)
  676. ->willReturn(['user2']);
  677. $this->userManager->expects($this->any())
  678. ->method('get')
  679. ->willReturnCallback(function ($uid) {
  680. switch ($uid) {
  681. case 'user1': return $this->getTestUser('user1');
  682. case 'user2': return $this->getTestUser('user2');
  683. case 'user3': return $this->getTestUser('user3');
  684. case 'user33': return $this->getTestUser('user33');
  685. default:
  686. return null;
  687. }
  688. });
  689. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
  690. $manager->addBackend($backend);
  691. $users = $manager->displayNamesInGroup('testgroup', '', 1);
  692. $this->assertCount(1, $users);
  693. $this->assertFalse(isset($users['user1']));
  694. $this->assertTrue(isset($users['user2']));
  695. $this->assertFalse(isset($users['user3']));
  696. $this->assertFalse(isset($users['user33']));
  697. }
  698. public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitAndOffsetSpecified() {
  699. /**
  700. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  701. */
  702. $backend = $this->getTestBackend();
  703. $backend->expects($this->exactly(1))
  704. ->method('groupExists')
  705. ->with('testgroup')
  706. ->willReturn(true);
  707. $backend->expects($this->once())
  708. ->method('usersInGroup')
  709. ->with('testgroup', '', 1, 1)
  710. ->willReturn(['user33']);
  711. $this->userManager->expects($this->any())
  712. ->method('get')
  713. ->willReturnCallback(function ($uid) {
  714. switch ($uid) {
  715. case 'user1': return $this->getTestUser('user1');
  716. case 'user2': return $this->getTestUser('user2');
  717. case 'user3': return $this->getTestUser('user3');
  718. case 'user33': return $this->getTestUser('user33');
  719. default:
  720. return null;
  721. }
  722. });
  723. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
  724. $manager->addBackend($backend);
  725. $users = $manager->displayNamesInGroup('testgroup', '', 1, 1);
  726. $this->assertCount(1, $users);
  727. $this->assertFalse(isset($users['user1']));
  728. $this->assertFalse(isset($users['user2']));
  729. $this->assertFalse(isset($users['user3']));
  730. $this->assertTrue(isset($users['user33']));
  731. }
  732. public function testGetUserGroupsWithAddUser() {
  733. /**
  734. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  735. */
  736. $backend = $this->getTestBackend();
  737. $expectedGroups = [];
  738. $backend->expects($this->any())
  739. ->method('getUserGroups')
  740. ->with('user1')
  741. ->willReturnCallback(function () use (&$expectedGroups) {
  742. return $expectedGroups;
  743. });
  744. $backend->expects($this->any())
  745. ->method('groupExists')
  746. ->with('group1')
  747. ->willReturn(true);
  748. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
  749. $manager->addBackend($backend);
  750. // prime cache
  751. $user1 = $this->getTestUser('user1');
  752. $groups = $manager->getUserGroups($user1);
  753. $this->assertEquals([], $groups);
  754. // add user
  755. $group = $manager->get('group1');
  756. $group->addUser($user1);
  757. $expectedGroups[] = 'group1';
  758. // check result
  759. $groups = $manager->getUserGroups($user1);
  760. $this->assertCount(1, $groups);
  761. $group1 = reset($groups);
  762. $this->assertEquals('group1', $group1->getGID());
  763. }
  764. public function testGetUserGroupsWithRemoveUser() {
  765. /**
  766. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  767. */
  768. $backend = $this->getTestBackend();
  769. $expectedGroups = ['group1'];
  770. $backend->expects($this->any())
  771. ->method('getUserGroups')
  772. ->with('user1')
  773. ->willReturnCallback(function () use (&$expectedGroups) {
  774. return $expectedGroups;
  775. });
  776. $backend->expects($this->any())
  777. ->method('groupExists')
  778. ->with('group1')
  779. ->willReturn(true);
  780. $backend->expects($this->once())
  781. ->method('inGroup')
  782. ->willReturn(true);
  783. $backend->expects($this->once())
  784. ->method('removeFromGroup')
  785. ->willReturn(true);
  786. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
  787. $manager->addBackend($backend);
  788. // prime cache
  789. $user1 = $this->getTestUser('user1');
  790. $groups = $manager->getUserGroups($user1);
  791. $this->assertCount(1, $groups);
  792. $group1 = reset($groups);
  793. $this->assertEquals('group1', $group1->getGID());
  794. // remove user
  795. $group = $manager->get('group1');
  796. $group->removeUser($user1);
  797. $expectedGroups = [];
  798. // check result
  799. $groups = $manager->getUserGroups($user1);
  800. $this->assertEquals($expectedGroups, $groups);
  801. }
  802. public function testGetUserIdGroups() {
  803. /**
  804. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  805. */
  806. $backend = $this->getTestBackend();
  807. $backend->expects($this->any())
  808. ->method('getUserGroups')
  809. ->with('user1')
  810. ->willReturn(null);
  811. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
  812. $manager->addBackend($backend);
  813. $groups = $manager->getUserIdGroups('user1');
  814. $this->assertEquals([], $groups);
  815. }
  816. public function testGroupDisplayName() {
  817. /**
  818. * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
  819. */
  820. $backend = $this->getTestBackend(
  821. GroupInterface::ADD_TO_GROUP |
  822. GroupInterface::REMOVE_FROM_GOUP |
  823. GroupInterface::COUNT_USERS |
  824. GroupInterface::CREATE_GROUP |
  825. GroupInterface::DELETE_GROUP |
  826. GroupInterface::GROUP_DETAILS
  827. );
  828. $backend->expects($this->any())
  829. ->method('getGroupDetails')
  830. ->willReturnMap([
  831. ['group1', ['gid' => 'group1', 'displayName' => 'Group One']],
  832. ['group2', ['gid' => 'group2']],
  833. ]);
  834. $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
  835. $manager->addBackend($backend);
  836. // group with display name
  837. $group = $manager->get('group1');
  838. $this->assertNotNull($group);
  839. $this->assertEquals('group1', $group->getGID());
  840. $this->assertEquals('Group One', $group->getDisplayName());
  841. // group without display name
  842. $group = $manager->get('group2');
  843. $this->assertNotNull($group);
  844. $this->assertEquals('group2', $group->getGID());
  845. $this->assertEquals('group2', $group->getDisplayName());
  846. }
  847. }