GroupPluginTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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\GroupPlugin;
  25. use OC\Collaboration\Collaborators\SearchResult;
  26. use OCP\Collaboration\Collaborators\ISearchResult;
  27. use OCP\IConfig;
  28. use OCP\IGroup;
  29. use OCP\IGroupManager;
  30. use OCP\IUser;
  31. use OCP\IUserSession;
  32. use OCP\Share\IShare;
  33. use Test\TestCase;
  34. class GroupPluginTest extends TestCase {
  35. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  36. protected $config;
  37. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  38. protected $groupManager;
  39. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  40. protected $session;
  41. /** @var ISearchResult */
  42. protected $searchResult;
  43. /** @var GroupPlugin */
  44. protected $plugin;
  45. /** @var int */
  46. protected $limit = 2;
  47. /** @var int */
  48. protected $offset = 0;
  49. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
  50. protected $user;
  51. protected function setUp(): void {
  52. parent::setUp();
  53. $this->config = $this->createMock(IConfig::class);
  54. $this->groupManager = $this->createMock(IGroupManager::class);
  55. $this->session = $this->createMock(IUserSession::class);
  56. $this->searchResult = new SearchResult();
  57. $this->user = $this->getUserMock('admin', 'Administrator');
  58. }
  59. public function instantiatePlugin() {
  60. // cannot be done within setUp, because dependent mocks needs to be set
  61. // up with configuration etc. first
  62. $this->plugin = new GroupPlugin(
  63. $this->config,
  64. $this->groupManager,
  65. $this->session
  66. );
  67. }
  68. public function getUserMock($uid, $displayName) {
  69. $user = $this->createMock(IUser::class);
  70. $user->expects($this->any())
  71. ->method('getUID')
  72. ->willReturn($uid);
  73. $user->expects($this->any())
  74. ->method('getDisplayName')
  75. ->willReturn($displayName);
  76. return $user;
  77. }
  78. /**
  79. * @param string $gid
  80. * @param null $displayName
  81. * @param bool $hide
  82. * @return IGroup|\PHPUnit\Framework\MockObject\MockObject
  83. */
  84. protected function getGroupMock($gid, $displayName = null, $hide = false) {
  85. $group = $this->createMock(IGroup::class);
  86. $group->expects($this->any())
  87. ->method('getGID')
  88. ->willReturn($gid);
  89. if (is_null($displayName)) {
  90. // note: this is how the Group class behaves
  91. $displayName = $gid;
  92. }
  93. $group->expects($this->any())
  94. ->method('getDisplayName')
  95. ->willReturn($displayName);
  96. $group->method('hideFromCollaboration')
  97. ->willReturn($hide);
  98. return $group;
  99. }
  100. public function dataGetGroups(): array {
  101. return [
  102. ['test', false, true, false, [], [], [], [], true, false],
  103. ['test', false, false, false, [], [], [], [], true, false],
  104. // group sharing disabled
  105. ['test', false, true, true, [], [], [], [], false, false],
  106. // group without display name
  107. [
  108. 'test', false, true, false,
  109. [$this->getGroupMock('test1')],
  110. [],
  111. [],
  112. [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
  113. true,
  114. false,
  115. ],
  116. // group with display name, search by id
  117. [
  118. 'test', false, true, false,
  119. [$this->getGroupMock('test1', 'Test One')],
  120. [],
  121. [],
  122. [['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
  123. true,
  124. false,
  125. ],
  126. // group with display name, search by display name
  127. [
  128. 'one', false, true, false,
  129. [$this->getGroupMock('test1', 'Test One')],
  130. [],
  131. [],
  132. [['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
  133. true,
  134. false,
  135. ],
  136. // group with display name, search by display name, exact expected
  137. [
  138. 'Test One', false, true, false,
  139. [$this->getGroupMock('test1', 'Test One')],
  140. [],
  141. [['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
  142. [],
  143. true,
  144. false,
  145. ],
  146. [
  147. 'test', false, false, false,
  148. [$this->getGroupMock('test1')],
  149. [],
  150. [],
  151. [],
  152. true,
  153. false,
  154. ],
  155. [
  156. 'test', false, true, false,
  157. [
  158. $this->getGroupMock('test'),
  159. $this->getGroupMock('test1'),
  160. ],
  161. [],
  162. [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
  163. [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
  164. false,
  165. false,
  166. ],
  167. [
  168. 'test', false, false, false,
  169. [
  170. $this->getGroupMock('test'),
  171. $this->getGroupMock('test1'),
  172. ],
  173. [],
  174. [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
  175. [],
  176. true,
  177. false,
  178. ],
  179. [
  180. 'test', false, true, false,
  181. [
  182. $this->getGroupMock('test0'),
  183. $this->getGroupMock('test1'),
  184. ],
  185. [],
  186. [],
  187. [
  188. ['label' => 'test0', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test0']],
  189. ['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
  190. ],
  191. false,
  192. null,
  193. ],
  194. [
  195. 'test', false, false, false,
  196. [
  197. $this->getGroupMock('test0'),
  198. $this->getGroupMock('test1'),
  199. ],
  200. [],
  201. [],
  202. [],
  203. true,
  204. null,
  205. ],
  206. [
  207. 'test', false, true, false,
  208. [
  209. $this->getGroupMock('test0'),
  210. $this->getGroupMock('test1'),
  211. ],
  212. [],
  213. [
  214. ['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']],
  215. ],
  216. [
  217. ['label' => 'test0', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test0']],
  218. ['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
  219. ],
  220. false,
  221. $this->getGroupMock('test'),
  222. ],
  223. [
  224. 'test', false, false, false,
  225. [
  226. $this->getGroupMock('test0'),
  227. $this->getGroupMock('test1'),
  228. ],
  229. [],
  230. [
  231. ['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']],
  232. ],
  233. [],
  234. true,
  235. $this->getGroupMock('test'),
  236. ],
  237. ['test', true, true, false, [], [], [], [], true, false],
  238. ['test', true, false, false, [], [], [], [], true, false],
  239. [
  240. 'test', true, true, false,
  241. [
  242. $this->getGroupMock('test1'),
  243. $this->getGroupMock('test2'),
  244. ],
  245. [$this->getGroupMock('test1')],
  246. [],
  247. [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
  248. false,
  249. false,
  250. ],
  251. [
  252. 'test', true, false, false,
  253. [
  254. $this->getGroupMock('test1'),
  255. $this->getGroupMock('test2'),
  256. ],
  257. [$this->getGroupMock('test1')],
  258. [],
  259. [],
  260. true,
  261. false,
  262. ],
  263. [
  264. 'test', true, true, false,
  265. [
  266. $this->getGroupMock('test'),
  267. $this->getGroupMock('test1'),
  268. ],
  269. [$this->getGroupMock('test')],
  270. [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
  271. [],
  272. false,
  273. false,
  274. ],
  275. [
  276. 'test', true, false, false,
  277. [
  278. $this->getGroupMock('test'),
  279. $this->getGroupMock('test1'),
  280. ],
  281. [$this->getGroupMock('test')],
  282. [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
  283. [],
  284. true,
  285. false,
  286. ],
  287. [
  288. 'test', true, true, false,
  289. [
  290. $this->getGroupMock('test'),
  291. $this->getGroupMock('test1'),
  292. ],
  293. [$this->getGroupMock('test1')],
  294. [],
  295. [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
  296. false,
  297. false,
  298. ],
  299. [
  300. 'test', true, false, false,
  301. [
  302. $this->getGroupMock('test'),
  303. $this->getGroupMock('test1'),
  304. ],
  305. [$this->getGroupMock('test1')],
  306. [],
  307. [],
  308. true,
  309. false,
  310. ],
  311. [
  312. 'test', true, true, false,
  313. [
  314. $this->getGroupMock('test'),
  315. $this->getGroupMock('test1'),
  316. ],
  317. [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
  318. [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
  319. [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
  320. false,
  321. false,
  322. ],
  323. [
  324. 'test', true, false, false,
  325. [
  326. $this->getGroupMock('test'),
  327. $this->getGroupMock('test1'),
  328. ],
  329. [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
  330. [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
  331. [],
  332. true,
  333. false,
  334. ],
  335. [
  336. 'test', true, true, false,
  337. [
  338. $this->getGroupMock('test0'),
  339. $this->getGroupMock('test1'),
  340. ],
  341. [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
  342. [],
  343. [
  344. ['label' => 'test0', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test0']],
  345. ['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
  346. ],
  347. false,
  348. null,
  349. ],
  350. [
  351. 'test', true, false, false,
  352. [
  353. $this->getGroupMock('test0'),
  354. $this->getGroupMock('test1'),
  355. ],
  356. [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
  357. [],
  358. [],
  359. true,
  360. null,
  361. ],
  362. [
  363. 'test', true, true, false,
  364. [
  365. $this->getGroupMock('test0'),
  366. $this->getGroupMock('test1'),
  367. ],
  368. [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
  369. [
  370. ['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']],
  371. ],
  372. [
  373. ['label' => 'test0', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test0']],
  374. ['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
  375. ],
  376. false,
  377. $this->getGroupMock('test'),
  378. ],
  379. [
  380. 'test', true, false, false,
  381. [
  382. $this->getGroupMock('test0'),
  383. $this->getGroupMock('test1'),
  384. ],
  385. [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
  386. [
  387. ['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']],
  388. ],
  389. [],
  390. true,
  391. $this->getGroupMock('test'),
  392. ],
  393. [
  394. 'test', false, false, false,
  395. [
  396. $this->getGroupMock('test', null, true),
  397. $this->getGroupMock('test1'),
  398. ],
  399. [],
  400. [],
  401. [],
  402. true,
  403. false,
  404. ],
  405. ];
  406. }
  407. /**
  408. * @dataProvider dataGetGroups
  409. *
  410. * @param string $searchTerm
  411. * @param bool $shareWithGroupOnly
  412. * @param bool $shareeEnumeration
  413. * @param bool $groupSharingDisabled
  414. * @param array $groupResponse
  415. * @param array $userGroupsResponse
  416. * @param array $exactExpected
  417. * @param array $expected
  418. * @param bool $reachedEnd
  419. * @param bool|IGroup $singleGroup
  420. */
  421. public function testSearch(
  422. string $searchTerm,
  423. bool $shareWithGroupOnly,
  424. bool $shareeEnumeration,
  425. bool $groupSharingDisabled,
  426. array $groupResponse,
  427. array $userGroupsResponse,
  428. array $exactExpected,
  429. array $expected,
  430. bool $reachedEnd,
  431. $singleGroup
  432. ) {
  433. $this->config->expects($this->any())
  434. ->method('getAppValue')
  435. ->willReturnCallback(
  436. function ($appName, $key, $default) use ($shareWithGroupOnly, $shareeEnumeration, $groupSharingDisabled) {
  437. if ($appName !== 'core') {
  438. return $default;
  439. }
  440. switch ($key) {
  441. case 'shareapi_only_share_with_group_members':
  442. return $shareWithGroupOnly ? 'yes' : 'no';
  443. case 'shareapi_allow_share_dialog_user_enumeration':
  444. return $shareeEnumeration ? 'yes' : 'no';
  445. case 'shareapi_allow_group_sharing':
  446. return $groupSharingDisabled ? 'no' : 'yes';
  447. default:
  448. return $default;
  449. }
  450. }
  451. );
  452. $this->instantiatePlugin();
  453. if (!$groupSharingDisabled) {
  454. $this->groupManager->expects($this->once())
  455. ->method('search')
  456. ->with($searchTerm, $this->limit, $this->offset)
  457. ->willReturn($groupResponse);
  458. }
  459. if ($singleGroup !== false) {
  460. $this->groupManager->expects($this->once())
  461. ->method('get')
  462. ->with($searchTerm)
  463. ->willReturn($singleGroup);
  464. }
  465. if ($shareWithGroupOnly) {
  466. $this->session->expects($this->any())
  467. ->method('getUser')
  468. ->willReturn($this->user);
  469. $numGetUserGroupsCalls = empty($groupResponse) ? 0 : 1;
  470. $this->groupManager->expects($this->exactly($numGetUserGroupsCalls))
  471. ->method('getUserGroups')
  472. ->with($this->user)
  473. ->willReturn($userGroupsResponse);
  474. }
  475. $moreResults = $this->plugin->search($searchTerm, $this->limit, $this->offset, $this->searchResult);
  476. $result = $this->searchResult->asArray();
  477. if (!$groupSharingDisabled) {
  478. $this->assertEquals($exactExpected, $result['exact']['groups']);
  479. $this->assertEquals($expected, $result['groups']);
  480. }
  481. $this->assertSame($reachedEnd, $moreResults);
  482. }
  483. }