UserPluginTest.php 12 KB

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