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