GroupPluginTest.php 12 KB

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