1
0

ManagerTest.php 29 KB

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