GroupPluginTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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() {
  101. return [
  102. ['test', false, true, [], [], [], [], true, false],
  103. ['test', false, false, [], [], [], [], true, false],
  104. // group without display name
  105. [
  106. 'test', false, true,
  107. [$this->getGroupMock('test1')],
  108. [],
  109. [],
  110. [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
  111. true,
  112. false,
  113. ],
  114. // group with display name, search by id
  115. [
  116. 'test', false, true,
  117. [$this->getGroupMock('test1', 'Test One')],
  118. [],
  119. [],
  120. [['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
  121. true,
  122. false,
  123. ],
  124. // group with display name, search by display name
  125. [
  126. 'one', false, true,
  127. [$this->getGroupMock('test1', 'Test One')],
  128. [],
  129. [],
  130. [['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
  131. true,
  132. false,
  133. ],
  134. // group with display name, search by display name, exact expected
  135. [
  136. 'Test One', false, true,
  137. [$this->getGroupMock('test1', 'Test One')],
  138. [],
  139. [['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
  140. [],
  141. true,
  142. false,
  143. ],
  144. [
  145. 'test', false, false,
  146. [$this->getGroupMock('test1')],
  147. [],
  148. [],
  149. [],
  150. true,
  151. false,
  152. ],
  153. [
  154. 'test', false, true,
  155. [
  156. $this->getGroupMock('test'),
  157. $this->getGroupMock('test1'),
  158. ],
  159. [],
  160. [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
  161. [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
  162. false,
  163. false,
  164. ],
  165. [
  166. 'test', false, false,
  167. [
  168. $this->getGroupMock('test'),
  169. $this->getGroupMock('test1'),
  170. ],
  171. [],
  172. [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
  173. [],
  174. true,
  175. false,
  176. ],
  177. [
  178. 'test', false, true,
  179. [
  180. $this->getGroupMock('test0'),
  181. $this->getGroupMock('test1'),
  182. ],
  183. [],
  184. [],
  185. [
  186. ['label' => 'test0', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test0']],
  187. ['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
  188. ],
  189. false,
  190. null,
  191. ],
  192. [
  193. 'test', false, false,
  194. [
  195. $this->getGroupMock('test0'),
  196. $this->getGroupMock('test1'),
  197. ],
  198. [],
  199. [],
  200. [],
  201. true,
  202. null,
  203. ],
  204. [
  205. 'test', false, true,
  206. [
  207. $this->getGroupMock('test0'),
  208. $this->getGroupMock('test1'),
  209. ],
  210. [],
  211. [
  212. ['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']],
  213. ],
  214. [
  215. ['label' => 'test0', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test0']],
  216. ['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
  217. ],
  218. false,
  219. $this->getGroupMock('test'),
  220. ],
  221. [
  222. 'test', false, false,
  223. [
  224. $this->getGroupMock('test0'),
  225. $this->getGroupMock('test1'),
  226. ],
  227. [],
  228. [
  229. ['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']],
  230. ],
  231. [],
  232. true,
  233. $this->getGroupMock('test'),
  234. ],
  235. ['test', true, true, [], [], [], [], true, false],
  236. ['test', true, false, [], [], [], [], true, false],
  237. [
  238. 'test', true, true,
  239. [
  240. $this->getGroupMock('test1'),
  241. $this->getGroupMock('test2'),
  242. ],
  243. [$this->getGroupMock('test1')],
  244. [],
  245. [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
  246. false,
  247. false,
  248. ],
  249. [
  250. 'test', true, false,
  251. [
  252. $this->getGroupMock('test1'),
  253. $this->getGroupMock('test2'),
  254. ],
  255. [$this->getGroupMock('test1')],
  256. [],
  257. [],
  258. true,
  259. false,
  260. ],
  261. [
  262. 'test', true, true,
  263. [
  264. $this->getGroupMock('test'),
  265. $this->getGroupMock('test1'),
  266. ],
  267. [$this->getGroupMock('test')],
  268. [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
  269. [],
  270. false,
  271. false,
  272. ],
  273. [
  274. 'test', true, false,
  275. [
  276. $this->getGroupMock('test'),
  277. $this->getGroupMock('test1'),
  278. ],
  279. [$this->getGroupMock('test')],
  280. [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
  281. [],
  282. true,
  283. false,
  284. ],
  285. [
  286. 'test', true, true,
  287. [
  288. $this->getGroupMock('test'),
  289. $this->getGroupMock('test1'),
  290. ],
  291. [$this->getGroupMock('test1')],
  292. [],
  293. [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
  294. false,
  295. false,
  296. ],
  297. [
  298. 'test', true, false,
  299. [
  300. $this->getGroupMock('test'),
  301. $this->getGroupMock('test1'),
  302. ],
  303. [$this->getGroupMock('test1')],
  304. [],
  305. [],
  306. true,
  307. false,
  308. ],
  309. [
  310. 'test', true, true,
  311. [
  312. $this->getGroupMock('test'),
  313. $this->getGroupMock('test1'),
  314. ],
  315. [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
  316. [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
  317. [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
  318. false,
  319. false,
  320. ],
  321. [
  322. 'test', true, false,
  323. [
  324. $this->getGroupMock('test'),
  325. $this->getGroupMock('test1'),
  326. ],
  327. [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
  328. [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
  329. [],
  330. true,
  331. false,
  332. ],
  333. [
  334. 'test', true, true,
  335. [
  336. $this->getGroupMock('test0'),
  337. $this->getGroupMock('test1'),
  338. ],
  339. [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
  340. [],
  341. [
  342. ['label' => 'test0', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test0']],
  343. ['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
  344. ],
  345. false,
  346. null,
  347. ],
  348. [
  349. 'test', true, false,
  350. [
  351. $this->getGroupMock('test0'),
  352. $this->getGroupMock('test1'),
  353. ],
  354. [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
  355. [],
  356. [],
  357. true,
  358. null,
  359. ],
  360. [
  361. 'test', true, true,
  362. [
  363. $this->getGroupMock('test0'),
  364. $this->getGroupMock('test1'),
  365. ],
  366. [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
  367. [
  368. ['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']],
  369. ],
  370. [
  371. ['label' => 'test0', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test0']],
  372. ['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
  373. ],
  374. false,
  375. $this->getGroupMock('test'),
  376. ],
  377. [
  378. 'test', true, false,
  379. [
  380. $this->getGroupMock('test0'),
  381. $this->getGroupMock('test1'),
  382. ],
  383. [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
  384. [
  385. ['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']],
  386. ],
  387. [],
  388. true,
  389. $this->getGroupMock('test'),
  390. ],
  391. [
  392. 'test', false, false,
  393. [
  394. $this->getGroupMock('test', null, true),
  395. $this->getGroupMock('test1'),
  396. ],
  397. [],
  398. [],
  399. [],
  400. true,
  401. false,
  402. ],
  403. ];
  404. }
  405. /**
  406. * @dataProvider dataGetGroups
  407. *
  408. * @param string $searchTerm
  409. * @param bool $shareWithGroupOnly
  410. * @param bool $shareeEnumeration
  411. * @param array $groupResponse
  412. * @param array $userGroupsResponse
  413. * @param array $exactExpected
  414. * @param array $expected
  415. * @param bool $reachedEnd
  416. * @param bool|IGroup $singleGroup
  417. */
  418. public function testSearch(
  419. $searchTerm,
  420. $shareWithGroupOnly,
  421. $shareeEnumeration,
  422. array $groupResponse,
  423. array $userGroupsResponse,
  424. array $exactExpected,
  425. array $expected,
  426. $reachedEnd,
  427. $singleGroup
  428. ) {
  429. $this->config->expects($this->any())
  430. ->method('getAppValue')
  431. ->willReturnCallback(
  432. function ($appName, $key, $default) use ($shareWithGroupOnly, $shareeEnumeration) {
  433. if ($appName === 'core' && $key === 'shareapi_only_share_with_group_members') {
  434. return $shareWithGroupOnly ? 'yes' : 'no';
  435. } elseif ($appName === 'core' && $key === 'shareapi_allow_share_dialog_user_enumeration') {
  436. return $shareeEnumeration ? 'yes' : 'no';
  437. }
  438. return $default;
  439. }
  440. );
  441. $this->instantiatePlugin();
  442. $this->groupManager->expects($this->once())
  443. ->method('search')
  444. ->with($searchTerm, $this->limit, $this->offset)
  445. ->willReturn($groupResponse);
  446. if ($singleGroup !== false) {
  447. $this->groupManager->expects($this->once())
  448. ->method('get')
  449. ->with($searchTerm)
  450. ->willReturn($singleGroup);
  451. }
  452. if ($shareWithGroupOnly) {
  453. $this->session->expects($this->any())
  454. ->method('getUser')
  455. ->willReturn($this->user);
  456. $numGetUserGroupsCalls = empty($groupResponse) ? 0 : 1;
  457. $this->groupManager->expects($this->exactly($numGetUserGroupsCalls))
  458. ->method('getUserGroups')
  459. ->with($this->user)
  460. ->willReturn($userGroupsResponse);
  461. }
  462. $moreResults = $this->plugin->search($searchTerm, $this->limit, $this->offset, $this->searchResult);
  463. $result = $this->searchResult->asArray();
  464. $this->assertEquals($exactExpected, $result['exact']['groups']);
  465. $this->assertEquals($expected, $result['groups']);
  466. $this->assertSame($reachedEnd, $moreResults);
  467. }
  468. }