ManagerTest.php 29 KB

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