ManagerTest.php 29 KB

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