UserPluginTest.php 11 KB

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