UserPluginTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Collaboration\Collaborators;
  24. use OC\Collaboration\Collaborators\SearchResult;
  25. use OC\Collaboration\Collaborators\UserPlugin;
  26. use OC\KnownUser\KnownUserService;
  27. use OCP\Collaboration\Collaborators\ISearchResult;
  28. use OCP\IConfig;
  29. use OCP\IGroup;
  30. use OCP\IGroupManager;
  31. use OCP\IUser;
  32. use OCP\IUserManager;
  33. use OCP\IUserSession;
  34. use OCP\Share\IShare;
  35. use OCP\UserStatus\IManager as IUserStatusManager;
  36. use Test\TestCase;
  37. class UserPluginTest extends TestCase {
  38. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  39. protected $config;
  40. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  41. protected $userManager;
  42. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  43. protected $groupManager;
  44. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  45. protected $session;
  46. /** @var KnownUserService|\PHPUnit\Framework\MockObject\MockObject */
  47. protected $knownUserService;
  48. /** @var IUserStatusManager|\PHPUnit\Framework\MockObject\MockObject */
  49. protected $userStatusManager;
  50. /** @var UserPlugin */
  51. protected $plugin;
  52. /** @var ISearchResult */
  53. protected $searchResult;
  54. /** @var int */
  55. protected $limit = 2;
  56. /** @var int */
  57. protected $offset = 0;
  58. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
  59. protected $user;
  60. protected function setUp(): void {
  61. parent::setUp();
  62. $this->config = $this->createMock(IConfig::class);
  63. $this->userManager = $this->createMock(IUserManager::class);
  64. $this->groupManager = $this->createMock(IGroupManager::class);
  65. $this->session = $this->createMock(IUserSession::class);
  66. $this->knownUserService = $this->createMock(KnownUserService::class);
  67. $this->userStatusManager = $this->createMock(IUserStatusManager::class);
  68. $this->searchResult = new SearchResult();
  69. $this->user = $this->getUserMock('admin', 'Administrator');
  70. }
  71. public function instantiatePlugin() {
  72. // cannot be done within setUp, because dependent mocks needs to be set
  73. // up with configuration etc. first
  74. $this->plugin = new UserPlugin(
  75. $this->config,
  76. $this->userManager,
  77. $this->groupManager,
  78. $this->session,
  79. $this->knownUserService,
  80. $this->userStatusManager
  81. );
  82. }
  83. public function mockConfig($shareWithGroupOnly, $shareeEnumeration, $shareeEnumerationLimitToGroup, $shareeEnumerationPhone = false) {
  84. $this->config->expects($this->any())
  85. ->method('getAppValue')
  86. ->willReturnCallback(
  87. function ($appName, $key, $default) use ($shareWithGroupOnly, $shareeEnumeration, $shareeEnumerationLimitToGroup, $shareeEnumerationPhone) {
  88. if ($appName === 'core' && $key === 'shareapi_only_share_with_group_members') {
  89. return $shareWithGroupOnly ? 'yes' : 'no';
  90. } elseif ($appName === 'core' && $key === 'shareapi_allow_share_dialog_user_enumeration') {
  91. return $shareeEnumeration ? 'yes' : 'no';
  92. } elseif ($appName === 'core' && $key === 'shareapi_restrict_user_enumeration_to_group') {
  93. return $shareeEnumerationLimitToGroup ? 'yes' : 'no';
  94. } elseif ($appName === 'core' && $key === 'shareapi_restrict_user_enumeration_to_phone') {
  95. return $shareeEnumerationPhone ? 'yes' : 'no';
  96. }
  97. return $default;
  98. }
  99. );
  100. }
  101. public function getUserMock($uid, $displayName, $enabled = true, $groups = []) {
  102. $user = $this->createMock(IUser::class);
  103. $user->expects($this->any())
  104. ->method('getUID')
  105. ->willReturn($uid);
  106. $user->expects($this->any())
  107. ->method('getDisplayName')
  108. ->willReturn($displayName);
  109. $user->expects($this->any())
  110. ->method('isEnabled')
  111. ->willReturn($enabled);
  112. return $user;
  113. }
  114. public function getGroupMock($gid) {
  115. $group = $this->createMock(IGroup::class);
  116. $group->expects($this->any())
  117. ->method('getGID')
  118. ->willReturn($gid);
  119. return $group;
  120. }
  121. public function dataGetUsers() {
  122. return [
  123. ['test', false, true, [], [], [], [], true, false],
  124. ['test', false, false, [], [], [], [], true, false],
  125. ['test', true, true, [], [], [], [], true, false],
  126. ['test', true, false, [], [], [], [], true, false],
  127. [
  128. 'test', false, true, [], [],
  129. [
  130. ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
  131. ], [], true, $this->getUserMock('test', 'Test'),
  132. ],
  133. [
  134. 'test', false, false, [], [],
  135. [
  136. ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
  137. ], [], true, $this->getUserMock('test', 'Test'),
  138. ],
  139. [
  140. 'test', true, true, [], [],
  141. [], [], true, $this->getUserMock('test', 'Test'),
  142. ],
  143. [
  144. 'test', true, false, [], [],
  145. [], [], true, $this->getUserMock('test', 'Test'),
  146. ],
  147. [
  148. 'test', true, true, ['test-group'], [['test-group', 'test', 2, 0, []]],
  149. [
  150. ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
  151. ], [], true, $this->getUserMock('test', 'Test'),
  152. ],
  153. [
  154. 'test', true, false, ['test-group'], [['test-group', 'test', 2, 0, []]],
  155. [
  156. ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
  157. ], [], true, $this->getUserMock('test', 'Test'),
  158. ],
  159. [
  160. 'test',
  161. false,
  162. true,
  163. [],
  164. [
  165. $this->getUserMock('test1', 'Test One'),
  166. ],
  167. [],
  168. [
  169. ['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test1'],
  170. ],
  171. true,
  172. false,
  173. ],
  174. [
  175. 'test',
  176. false,
  177. false,
  178. [],
  179. [
  180. $this->getUserMock('test1', 'Test One'),
  181. ],
  182. [],
  183. [],
  184. true,
  185. false,
  186. ],
  187. [
  188. 'test',
  189. false,
  190. true,
  191. [],
  192. [
  193. $this->getUserMock('test1', 'Test One'),
  194. $this->getUserMock('test2', 'Test Two'),
  195. ],
  196. [],
  197. [
  198. ['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test1'],
  199. ['label' => 'Test Two', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test2'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test2'],
  200. ],
  201. false,
  202. false,
  203. ],
  204. [
  205. 'test',
  206. false,
  207. false,
  208. [],
  209. [
  210. $this->getUserMock('test1', 'Test One'),
  211. $this->getUserMock('test2', 'Test Two'),
  212. ],
  213. [],
  214. [],
  215. true,
  216. false,
  217. ],
  218. [
  219. 'test',
  220. false,
  221. true,
  222. [],
  223. [
  224. $this->getUserMock('test0', 'Test'),
  225. $this->getUserMock('test1', 'Test One'),
  226. $this->getUserMock('test2', 'Test Two'),
  227. ],
  228. [
  229. ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test0'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test0'],
  230. ],
  231. [
  232. ['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test1'],
  233. ['label' => 'Test Two', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test2'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test2'],
  234. ],
  235. false,
  236. false,
  237. ],
  238. [
  239. 'test',
  240. false,
  241. true,
  242. [],
  243. [
  244. $this->getUserMock('test0', 'Test'),
  245. $this->getUserMock('test1', 'Test One'),
  246. $this->getUserMock('test2', 'Test Two'),
  247. ],
  248. [
  249. ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test0'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test0'],
  250. ],
  251. [
  252. ['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test1'],
  253. ['label' => 'Test Two', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test2'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test2'],
  254. ],
  255. false,
  256. false,
  257. [],
  258. true,
  259. ],
  260. [
  261. 'test',
  262. false,
  263. false,
  264. [],
  265. [
  266. $this->getUserMock('test0', 'Test'),
  267. $this->getUserMock('test1', 'Test One'),
  268. $this->getUserMock('test2', 'Test Two'),
  269. ],
  270. [
  271. ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test0'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test0'],
  272. ],
  273. [],
  274. true,
  275. false,
  276. ],
  277. [
  278. 'test',
  279. true,
  280. true,
  281. ['abc', 'xyz'],
  282. [
  283. ['abc', 'test', 2, 0, ['test1' => 'Test One']],
  284. ['xyz', 'test', 2, 0, []],
  285. ],
  286. [],
  287. [
  288. ['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test1'],
  289. ],
  290. true,
  291. false,
  292. [['test1', $this->getUserMock('test1', 'Test One')]],
  293. ],
  294. [
  295. 'test',
  296. true,
  297. false,
  298. ['abc', 'xyz'],
  299. [
  300. ['abc', 'test', 2, 0, ['test1' => 'Test One']],
  301. ['xyz', 'test', 2, 0, []],
  302. ],
  303. [],
  304. [],
  305. true,
  306. false,
  307. [['test1', $this->getUserMock('test1', 'Test One')]],
  308. ],
  309. [
  310. 'test',
  311. true,
  312. true,
  313. ['abc', 'xyz'],
  314. [
  315. ['abc', 'test', 2, 0, [
  316. 'test1' => 'Test One',
  317. 'test2' => 'Test Two',
  318. ]],
  319. ['xyz', 'test', 2, 0, [
  320. 'test1' => 'Test One',
  321. 'test2' => 'Test Two',
  322. ]],
  323. ],
  324. [],
  325. [
  326. ['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test1'],
  327. ['label' => 'Test Two', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test2'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test2'],
  328. ],
  329. true,
  330. false,
  331. [
  332. ['test1', $this->getUserMock('test1', 'Test One')],
  333. ['test2', $this->getUserMock('test2', 'Test Two')],
  334. ],
  335. ],
  336. [
  337. 'test',
  338. true,
  339. false,
  340. ['abc', 'xyz'],
  341. [
  342. ['abc', 'test', 2, 0, [
  343. 'test1' => 'Test One',
  344. 'test2' => 'Test Two',
  345. ]],
  346. ['xyz', 'test', 2, 0, [
  347. 'test1' => 'Test One',
  348. 'test2' => 'Test Two',
  349. ]],
  350. ],
  351. [],
  352. [],
  353. true,
  354. false,
  355. [
  356. ['test1', $this->getUserMock('test1', 'Test One')],
  357. ['test2', $this->getUserMock('test2', 'Test Two')],
  358. ],
  359. ],
  360. [
  361. 'test',
  362. true,
  363. true,
  364. ['abc', 'xyz'],
  365. [
  366. ['abc', 'test', 2, 0, [
  367. 'test' => 'Test One',
  368. ]],
  369. ['xyz', 'test', 2, 0, [
  370. 'test2' => 'Test Two',
  371. ]],
  372. ],
  373. [
  374. ['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
  375. ],
  376. [
  377. ['label' => 'Test Two', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test2'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test2'],
  378. ],
  379. false,
  380. false,
  381. [
  382. ['test', $this->getUserMock('test', 'Test One')],
  383. ['test2', $this->getUserMock('test2', 'Test Two')],
  384. ],
  385. ],
  386. [
  387. 'test',
  388. true,
  389. false,
  390. ['abc', 'xyz'],
  391. [
  392. ['abc', 'test', 2, 0, [
  393. 'test' => 'Test One',
  394. ]],
  395. ['xyz', 'test', 2, 0, [
  396. 'test2' => 'Test Two',
  397. ]],
  398. ],
  399. [
  400. ['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
  401. ],
  402. [],
  403. true,
  404. false,
  405. [
  406. ['test', $this->getUserMock('test', 'Test One')],
  407. ['test2', $this->getUserMock('test2', 'Test Two')],
  408. ],
  409. ],
  410. ];
  411. }
  412. /**
  413. * @dataProvider dataGetUsers
  414. *
  415. * @param string $searchTerm
  416. * @param bool $shareWithGroupOnly
  417. * @param bool $shareeEnumeration
  418. * @param array $groupResponse
  419. * @param array $userResponse
  420. * @param array $exactExpected
  421. * @param array $expected
  422. * @param bool $reachedEnd
  423. * @param bool|IUser $singleUser
  424. * @param array $users
  425. */
  426. public function testSearch(
  427. $searchTerm,
  428. $shareWithGroupOnly,
  429. $shareeEnumeration,
  430. array $groupResponse,
  431. array $userResponse,
  432. array $exactExpected,
  433. array $expected,
  434. $reachedEnd,
  435. $singleUser,
  436. array $users = [],
  437. $shareeEnumerationPhone = false
  438. ) {
  439. $this->mockConfig($shareWithGroupOnly, $shareeEnumeration, false, $shareeEnumerationPhone);
  440. $this->instantiatePlugin();
  441. $this->session->expects($this->any())
  442. ->method('getUser')
  443. ->willReturn($this->user);
  444. if (!$shareWithGroupOnly) {
  445. if ($shareeEnumerationPhone) {
  446. $this->userManager->expects($this->once())
  447. ->method('searchKnownUsersByDisplayName')
  448. ->with($this->user->getUID(), $searchTerm, $this->limit, $this->offset)
  449. ->willReturn($userResponse);
  450. $this->knownUserService->method('isKnownToUser')
  451. ->willReturnMap([
  452. [$this->user->getUID(), 'test0', true],
  453. [$this->user->getUID(), 'test1', true],
  454. [$this->user->getUID(), 'test2', true],
  455. ]);
  456. } else {
  457. $this->userManager->expects($this->once())
  458. ->method('searchDisplayName')
  459. ->with($searchTerm, $this->limit, $this->offset)
  460. ->willReturn($userResponse);
  461. }
  462. } else {
  463. $this->groupManager->method('getUserGroupIds')
  464. ->with($this->user)
  465. ->willReturn($groupResponse);
  466. if ($singleUser !== false) {
  467. $this->groupManager->method('getUserGroupIds')
  468. ->with($singleUser)
  469. ->willReturn($groupResponse);
  470. }
  471. $this->groupManager->method('displayNamesInGroup')
  472. ->willReturnMap($userResponse);
  473. }
  474. if ($singleUser !== false) {
  475. $users[] = [$searchTerm, $singleUser];
  476. }
  477. if (!empty($users)) {
  478. $this->userManager->expects($this->atLeastOnce())
  479. ->method('get')
  480. ->willReturnMap($users);
  481. }
  482. $moreResults = $this->plugin->search($searchTerm, $this->limit, $this->offset, $this->searchResult);
  483. $result = $this->searchResult->asArray();
  484. $this->assertEquals($exactExpected, $result['exact']['users']);
  485. $this->assertEquals($expected, $result['users']);
  486. $this->assertSame($reachedEnd, $moreResults);
  487. }
  488. public function takeOutCurrentUserProvider() {
  489. $inputUsers = [
  490. 'alice' => 'Alice',
  491. 'bob' => 'Bob',
  492. 'carol' => 'Carol',
  493. ];
  494. return [
  495. [
  496. $inputUsers,
  497. ['alice', 'carol'],
  498. 'bob',
  499. ],
  500. [
  501. $inputUsers,
  502. ['alice', 'bob', 'carol'],
  503. 'dave',
  504. ],
  505. [
  506. $inputUsers,
  507. ['alice', 'bob', 'carol'],
  508. null,
  509. ],
  510. ];
  511. }
  512. /**
  513. * @dataProvider takeOutCurrentUserProvider
  514. * @param array $users
  515. * @param array $expectedUIDs
  516. * @param $currentUserId
  517. */
  518. public function testTakeOutCurrentUser(array $users, array $expectedUIDs, $currentUserId) {
  519. $this->instantiatePlugin();
  520. $this->session->expects($this->once())
  521. ->method('getUser')
  522. ->willReturnCallback(function () use ($currentUserId) {
  523. if ($currentUserId !== null) {
  524. return $this->getUserMock($currentUserId, $currentUserId);
  525. }
  526. return null;
  527. });
  528. $this->plugin->takeOutCurrentUser($users);
  529. $this->assertSame($expectedUIDs, array_keys($users));
  530. }
  531. public function dataSearchEnumeration() {
  532. return [
  533. [
  534. 'test',
  535. ['groupA'],
  536. [
  537. ['uid' => 'test1', 'groups' => ['groupA']],
  538. ['uid' => 'test2', 'groups' => ['groupB']],
  539. ],
  540. ['exact' => [], 'wide' => ['test1']],
  541. ],
  542. [
  543. 'test1',
  544. ['groupA'],
  545. [
  546. ['uid' => 'test1', 'groups' => ['groupA']],
  547. ['uid' => 'test2', 'groups' => ['groupB']],
  548. ],
  549. ['exact' => ['test1'], 'wide' => []],
  550. ],
  551. [
  552. 'test',
  553. ['groupA'],
  554. [
  555. ['uid' => 'test1', 'groups' => ['groupA']],
  556. ['uid' => 'test2', 'groups' => ['groupB', 'groupA']],
  557. ],
  558. ['exact' => [], 'wide' => ['test1', 'test2']],
  559. ],
  560. [
  561. 'test',
  562. ['groupA'],
  563. [
  564. ['uid' => 'test1', 'groups' => ['groupA', 'groupC']],
  565. ['uid' => 'test2', 'groups' => ['groupB', 'groupA']],
  566. ],
  567. ['exact' => [], 'wide' => ['test1', 'test2']],
  568. ],
  569. [
  570. 'test',
  571. ['groupC', 'groupB'],
  572. [
  573. ['uid' => 'test1', 'groups' => ['groupA', 'groupC']],
  574. ['uid' => 'test2', 'groups' => ['groupB', 'groupA']],
  575. ],
  576. ['exact' => [], 'wide' => ['test1', 'test2']],
  577. ],
  578. [
  579. 'test',
  580. [],
  581. [
  582. ['uid' => 'test1', 'groups' => ['groupA']],
  583. ['uid' => 'test2', 'groups' => ['groupB', 'groupA']],
  584. ],
  585. ['exact' => [], 'wide' => []],
  586. ],
  587. [
  588. 'test',
  589. ['groupC', 'groupB'],
  590. [
  591. ['uid' => 'test1', 'groups' => []],
  592. ['uid' => 'test2', 'groups' => []],
  593. ],
  594. ['exact' => [], 'wide' => []],
  595. ],
  596. [
  597. 'test',
  598. ['groupC', 'groupB'],
  599. [
  600. ['uid' => 'test1', 'groups' => []],
  601. ['uid' => 'test2', 'groups' => []],
  602. ],
  603. ['exact' => [], 'wide' => []],
  604. ],
  605. ];
  606. }
  607. /**
  608. * @dataProvider dataSearchEnumeration
  609. */
  610. public function testSearchEnumerationLimit($search, $userGroups, $matchingUsers, $result) {
  611. $this->mockConfig(false, true, true);
  612. $userResults = [];
  613. foreach ($matchingUsers as $user) {
  614. $userResults[$user['uid']] = $user['uid'];
  615. }
  616. $mappedResultExact = array_map(function ($user) {
  617. return ['label' => $user, 'value' => ['shareType' => 0, 'shareWith' => $user], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => $user];
  618. }, $result['exact']);
  619. $mappedResultWide = array_map(function ($user) {
  620. return ['label' => $user, 'value' => ['shareType' => 0, 'shareWith' => $user], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => $user];
  621. }, $result['wide']);
  622. $this->userManager
  623. ->method('get')
  624. ->willReturnCallback(function ($userId) use ($userResults) {
  625. if (isset($userResults[$userId])) {
  626. return $this->getUserMock($userId, $userId);
  627. }
  628. return null;
  629. });
  630. $this->groupManager->method('displayNamesInGroup')
  631. ->willReturn($userResults);
  632. $this->session->expects($this->any())
  633. ->method('getUser')
  634. ->willReturn($this->getUserMock('test', 'foo'));
  635. // current user
  636. $this->groupManager->expects($this->at(0))
  637. ->method('getUserGroupIds')
  638. ->willReturn($userGroups);
  639. $this->groupManager->expects($this->any())
  640. ->method('getUserGroupIds')
  641. ->willReturnCallback(function ($user) use ($matchingUsers) {
  642. $neededObject = array_filter(
  643. $matchingUsers,
  644. function ($e) use ($user) {
  645. return $user->getUID() === $e['uid'];
  646. }
  647. );
  648. if (count($neededObject) > 0) {
  649. return array_shift($neededObject)['groups'];
  650. }
  651. return [];
  652. });
  653. $this->instantiatePlugin();
  654. $this->plugin->search($search, $this->limit, $this->offset, $this->searchResult);
  655. $result = $this->searchResult->asArray();
  656. $this->assertEquals($mappedResultExact, $result['exact']['users']);
  657. $this->assertEquals($mappedResultWide, $result['users']);
  658. }
  659. }