ManagerTest.php 29 KB

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