ContactsStoreTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. <?php
  2. /**
  3. * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
  4. * @copyright 2017 Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author 2017 Lukas Reschke <lukas@statuscode.ch>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace Tests\Contacts\ContactsMenu;
  26. use OC\Contacts\ContactsMenu\ContactsStore;
  27. use OC\KnownUser\KnownUserService;
  28. use OCP\Contacts\IManager;
  29. use OCP\IConfig;
  30. use OCP\IGroupManager;
  31. use OCP\IUser;
  32. use OCP\IUserManager;
  33. use PHPUnit\Framework\MockObject\MockObject;
  34. use Test\TestCase;
  35. class ContactsStoreTest extends TestCase {
  36. /** @var ContactsStore */
  37. private $contactsStore;
  38. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  39. private $contactsManager;
  40. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  41. private $userManager;
  42. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  43. private $groupManager;
  44. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  45. private $config;
  46. /** @var KnownUserService|MockObject */
  47. private $knownUserService;
  48. protected function setUp(): void {
  49. parent::setUp();
  50. $this->contactsManager = $this->createMock(IManager::class);
  51. $this->userManager = $this->createMock(IUserManager::class);
  52. $this->groupManager = $this->createMock(IGroupManager::class);
  53. $this->config = $this->createMock(IConfig::class);
  54. $this->knownUserService = $this->createMock(KnownUserService::class);
  55. $this->contactsStore = new ContactsStore(
  56. $this->contactsManager,
  57. $this->config,
  58. $this->userManager,
  59. $this->groupManager,
  60. $this->knownUserService
  61. );
  62. }
  63. public function testGetContactsWithoutFilter() {
  64. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  65. $user = $this->createMock(IUser::class);
  66. $this->contactsManager->expects($this->once())
  67. ->method('search')
  68. ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
  69. ->willReturn([
  70. [
  71. 'UID' => 123,
  72. ],
  73. [
  74. 'UID' => 567,
  75. 'FN' => 'Darren Roner',
  76. 'EMAIL' => [
  77. 'darren@roner.au'
  78. ],
  79. ],
  80. ]);
  81. $user->expects($this->once())
  82. ->method('getUID')
  83. ->willReturn('user123');
  84. $entries = $this->contactsStore->getContacts($user, '');
  85. $this->assertCount(2, $entries);
  86. $this->assertEquals([
  87. 'darren@roner.au'
  88. ], $entries[1]->getEMailAddresses());
  89. }
  90. public function testGetContactsHidesOwnEntry() {
  91. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  92. $user = $this->createMock(IUser::class);
  93. $this->contactsManager->expects($this->once())
  94. ->method('search')
  95. ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
  96. ->willReturn([
  97. [
  98. 'UID' => 'user123',
  99. ],
  100. [
  101. 'UID' => 567,
  102. 'FN' => 'Darren Roner',
  103. 'EMAIL' => [
  104. 'darren@roner.au'
  105. ],
  106. ],
  107. ]);
  108. $user->expects($this->once())
  109. ->method('getUID')
  110. ->willReturn('user123');
  111. $entries = $this->contactsStore->getContacts($user, '');
  112. $this->assertCount(1, $entries);
  113. }
  114. public function testGetContactsWithoutBinaryImage() {
  115. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  116. $user = $this->createMock(IUser::class);
  117. $this->contactsManager->expects($this->once())
  118. ->method('search')
  119. ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
  120. ->willReturn([
  121. [
  122. 'UID' => 123,
  123. ],
  124. [
  125. 'UID' => 567,
  126. 'FN' => 'Darren Roner',
  127. 'EMAIL' => [
  128. 'darren@roner.au'
  129. ],
  130. 'PHOTO' => base64_encode('photophotophoto'),
  131. ],
  132. ]);
  133. $user->expects($this->once())
  134. ->method('getUID')
  135. ->willReturn('user123');
  136. $entries = $this->contactsStore->getContacts($user, '');
  137. $this->assertCount(2, $entries);
  138. $this->assertNull($entries[1]->getAvatar());
  139. }
  140. public function testGetContactsWithoutAvatarURI() {
  141. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  142. $user = $this->createMock(IUser::class);
  143. $this->contactsManager->expects($this->once())
  144. ->method('search')
  145. ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
  146. ->willReturn([
  147. [
  148. 'UID' => 123,
  149. ],
  150. [
  151. 'UID' => 567,
  152. 'FN' => 'Darren Roner',
  153. 'EMAIL' => [
  154. 'darren@roner.au'
  155. ],
  156. 'PHOTO' => 'VALUE=uri:https://photo',
  157. ],
  158. ]);
  159. $user->expects($this->once())
  160. ->method('getUID')
  161. ->willReturn('user123');
  162. $entries = $this->contactsStore->getContacts($user, '');
  163. $this->assertCount(2, $entries);
  164. $this->assertEquals('https://photo', $entries[1]->getAvatar());
  165. }
  166. public function testGetContactsWhenUserIsInExcludeGroups() {
  167. $this->config
  168. ->method('getAppValue')
  169. ->willReturnMap([
  170. ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
  171. ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'no'],
  172. ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'no'],
  173. ['core', 'shareapi_exclude_groups', 'no', 'yes'],
  174. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  175. ['core', 'shareapi_exclude_groups_list', '', '["group1", "group5", "group6"]'],
  176. ]);
  177. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $currentUser */
  178. $currentUser = $this->createMock(IUser::class);
  179. $currentUser->expects($this->once())
  180. ->method('getUID')
  181. ->willReturn('user001');
  182. $this->groupManager->expects($this->once())
  183. ->method('getUserGroupIds')
  184. ->with($this->equalTo($currentUser))
  185. ->willReturn(['group1', 'group2', 'group3']);
  186. $this->contactsManager->expects($this->once())
  187. ->method('search')
  188. ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
  189. ->willReturn([
  190. [
  191. 'UID' => 'user123',
  192. 'isLocalSystemBook' => true
  193. ],
  194. [
  195. 'UID' => 'user12345',
  196. 'isLocalSystemBook' => true
  197. ],
  198. ]);
  199. $entries = $this->contactsStore->getContacts($currentUser, '');
  200. $this->assertCount(0, $entries);
  201. }
  202. public function testGetContactsOnlyShareIfInTheSameGroup() {
  203. $this->config
  204. ->method('getAppValue')
  205. ->willReturnMap([
  206. ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
  207. ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'no'],
  208. ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'no'],
  209. ['core', 'shareapi_exclude_groups', 'no', 'no'],
  210. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  211. ]);
  212. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $currentUser */
  213. $currentUser = $this->createMock(IUser::class);
  214. $currentUser->expects($this->once())
  215. ->method('getUID')
  216. ->willReturn('user001');
  217. $this->groupManager->expects($this->at(0))
  218. ->method('getUserGroupIds')
  219. ->with($this->equalTo($currentUser))
  220. ->willReturn(['group1', 'group2', 'group3']);
  221. $user1 = $this->createMock(IUser::class);
  222. $this->userManager->expects($this->at(0))
  223. ->method('get')
  224. ->with('user1')
  225. ->willReturn($user1);
  226. $this->groupManager->expects($this->at(1))
  227. ->method('getUserGroupIds')
  228. ->with($this->equalTo($user1))
  229. ->willReturn(['group1']);
  230. $user2 = $this->createMock(IUser::class);
  231. $this->userManager->expects($this->at(1))
  232. ->method('get')
  233. ->with('user2')
  234. ->willReturn($user2);
  235. $this->groupManager->expects($this->at(2))
  236. ->method('getUserGroupIds')
  237. ->with($this->equalTo($user2))
  238. ->willReturn(['group2', 'group3']);
  239. $user3 = $this->createMock(IUser::class);
  240. $this->userManager->expects($this->at(2))
  241. ->method('get')
  242. ->with('user3')
  243. ->willReturn($user3);
  244. $this->groupManager->expects($this->at(3))
  245. ->method('getUserGroupIds')
  246. ->with($this->equalTo($user3))
  247. ->willReturn(['group8', 'group9']);
  248. $this->contactsManager->expects($this->once())
  249. ->method('search')
  250. ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
  251. ->willReturn([
  252. [
  253. 'UID' => 'user1',
  254. 'isLocalSystemBook' => true
  255. ],
  256. [
  257. 'UID' => 'user2',
  258. 'isLocalSystemBook' => true
  259. ],
  260. [
  261. 'UID' => 'user3',
  262. 'isLocalSystemBook' => true
  263. ],
  264. [
  265. 'UID' => 'contact',
  266. ],
  267. ]);
  268. $entries = $this->contactsStore->getContacts($currentUser, '');
  269. $this->assertCount(3, $entries);
  270. $this->assertEquals('user1', $entries[0]->getProperty('UID'));
  271. $this->assertEquals('user2', $entries[1]->getProperty('UID'));
  272. $this->assertEquals('contact', $entries[2]->getProperty('UID'));
  273. }
  274. public function testGetContactsOnlyEnumerateIfInTheSameGroup() {
  275. $this->config
  276. ->method('getAppValue')
  277. ->willReturnMap([
  278. ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
  279. ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'yes'],
  280. ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'no'],
  281. ['core', 'shareapi_exclude_groups', 'no', 'no'],
  282. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  283. ]);
  284. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $currentUser */
  285. $currentUser = $this->createMock(IUser::class);
  286. $currentUser->expects($this->once())
  287. ->method('getUID')
  288. ->willReturn('user001');
  289. $this->groupManager->expects($this->at(0))
  290. ->method('getUserGroupIds')
  291. ->with($this->equalTo($currentUser))
  292. ->willReturn(['group1', 'group2', 'group3']);
  293. $user1 = $this->createMock(IUser::class);
  294. $this->userManager->expects($this->at(0))
  295. ->method('get')
  296. ->with('user1')
  297. ->willReturn($user1);
  298. $this->groupManager->expects($this->at(1))
  299. ->method('getUserGroupIds')
  300. ->with($this->equalTo($user1))
  301. ->willReturn(['group1']);
  302. $user2 = $this->createMock(IUser::class);
  303. $this->userManager->expects($this->at(1))
  304. ->method('get')
  305. ->with('user2')
  306. ->willReturn($user2);
  307. $this->groupManager->expects($this->at(2))
  308. ->method('getUserGroupIds')
  309. ->with($this->equalTo($user2))
  310. ->willReturn(['group2', 'group3']);
  311. $user3 = $this->createMock(IUser::class);
  312. $this->userManager->expects($this->at(2))
  313. ->method('get')
  314. ->with('user3')
  315. ->willReturn($user3);
  316. $this->groupManager->expects($this->at(3))
  317. ->method('getUserGroupIds')
  318. ->with($this->equalTo($user3))
  319. ->willReturn(['group8', 'group9']);
  320. $this->contactsManager->expects($this->once())
  321. ->method('search')
  322. ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
  323. ->willReturn([
  324. [
  325. 'UID' => 'user1',
  326. 'isLocalSystemBook' => true
  327. ],
  328. [
  329. 'UID' => 'user2',
  330. 'isLocalSystemBook' => true
  331. ],
  332. [
  333. 'UID' => 'user3',
  334. 'isLocalSystemBook' => true
  335. ],
  336. [
  337. 'UID' => 'contact',
  338. ],
  339. ]);
  340. $entries = $this->contactsStore->getContacts($currentUser, '');
  341. $this->assertCount(3, $entries);
  342. $this->assertEquals('user1', $entries[0]->getProperty('UID'));
  343. $this->assertEquals('user2', $entries[1]->getProperty('UID'));
  344. $this->assertEquals('contact', $entries[2]->getProperty('UID'));
  345. }
  346. public function testGetContactsOnlyEnumerateIfPhoneBookMatch() {
  347. $this->config
  348. ->method('getAppValue')
  349. ->willReturnMap([
  350. ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
  351. ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'no'],
  352. ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'yes'],
  353. ['core', 'shareapi_exclude_groups', 'no', 'no'],
  354. ['core', 'shareapi_only_share_with_group_members', 'no', 'no'],
  355. ]);
  356. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $currentUser */
  357. $currentUser = $this->createMock(IUser::class);
  358. $currentUser->expects($this->once())
  359. ->method('getUID')
  360. ->willReturn('user001');
  361. $this->groupManager->expects($this->at(0))
  362. ->method('getUserGroupIds')
  363. ->with($this->equalTo($currentUser))
  364. ->willReturn(['group1', 'group2', 'group3']);
  365. $this->knownUserService->method('isKnownToUser')
  366. ->willReturnMap([
  367. ['user001', 'user1', true],
  368. ['user001', 'user2', true],
  369. ['user001', 'user3', false],
  370. ]);
  371. $this->contactsManager->expects($this->once())
  372. ->method('search')
  373. ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
  374. ->willReturn([
  375. [
  376. 'UID' => 'user1',
  377. 'isLocalSystemBook' => true
  378. ],
  379. [
  380. 'UID' => 'user2',
  381. 'isLocalSystemBook' => true
  382. ],
  383. [
  384. 'UID' => 'user3',
  385. 'isLocalSystemBook' => true
  386. ],
  387. [
  388. 'UID' => 'contact',
  389. ],
  390. ]);
  391. $entries = $this->contactsStore->getContacts($currentUser, '');
  392. $this->assertCount(3, $entries);
  393. $this->assertEquals('user1', $entries[0]->getProperty('UID'));
  394. $this->assertEquals('user2', $entries[1]->getProperty('UID'));
  395. $this->assertEquals('contact', $entries[2]->getProperty('UID'));
  396. }
  397. public function testGetContactsOnlyEnumerateIfPhoneBookMatchWithOwnGroupsOnly() {
  398. $this->config
  399. ->method('getAppValue')
  400. ->willReturnMap([
  401. ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
  402. ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'no'],
  403. ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'yes'],
  404. ['core', 'shareapi_exclude_groups', 'no', 'no'],
  405. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  406. ]);
  407. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $currentUser */
  408. $currentUser = $this->createMock(IUser::class);
  409. $currentUser->expects($this->once())
  410. ->method('getUID')
  411. ->willReturn('user001');
  412. $this->groupManager->expects($this->at(0))
  413. ->method('getUserGroupIds')
  414. ->with($this->equalTo($currentUser))
  415. ->willReturn(['group1', 'group2', 'group3']);
  416. $user1 = $this->createMock(IUser::class);
  417. $this->userManager->expects($this->at(0))
  418. ->method('get')
  419. ->with('user1')
  420. ->willReturn($user1);
  421. $this->groupManager->expects($this->at(1))
  422. ->method('getUserGroupIds')
  423. ->with($this->equalTo($user1))
  424. ->willReturn(['group1']);
  425. $user2 = $this->createMock(IUser::class);
  426. $this->userManager->expects($this->at(1))
  427. ->method('get')
  428. ->with('user2')
  429. ->willReturn($user2);
  430. $this->groupManager->expects($this->at(2))
  431. ->method('getUserGroupIds')
  432. ->with($this->equalTo($user2))
  433. ->willReturn(['group2', 'group3']);
  434. $user3 = $this->createMock(IUser::class);
  435. $this->userManager->expects($this->at(2))
  436. ->method('get')
  437. ->with('user3')
  438. ->willReturn($user3);
  439. $this->groupManager->expects($this->at(3))
  440. ->method('getUserGroupIds')
  441. ->with($this->equalTo($user3))
  442. ->willReturn(['group8', 'group9']);
  443. $this->knownUserService->method('isKnownToUser')
  444. ->willReturnMap([
  445. ['user001', 'user1', true],
  446. ['user001', 'user2', true],
  447. ['user001', 'user3', true],
  448. ]);
  449. $this->contactsManager->expects($this->once())
  450. ->method('search')
  451. ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
  452. ->willReturn([
  453. [
  454. 'UID' => 'user1',
  455. 'isLocalSystemBook' => true
  456. ],
  457. [
  458. 'UID' => 'user2',
  459. 'isLocalSystemBook' => true
  460. ],
  461. [
  462. 'UID' => 'user3',
  463. 'isLocalSystemBook' => true
  464. ],
  465. [
  466. 'UID' => 'contact',
  467. ],
  468. ]);
  469. $entries = $this->contactsStore->getContacts($currentUser, '');
  470. $this->assertCount(3, $entries);
  471. $this->assertEquals('user1', $entries[0]->getProperty('UID'));
  472. $this->assertEquals('user2', $entries[1]->getProperty('UID'));
  473. $this->assertEquals('contact', $entries[2]->getProperty('UID'));
  474. }
  475. public function testGetContactsOnlyEnumerateIfPhoneBookOrSameGroup() {
  476. $this->config
  477. ->method('getAppValue')
  478. ->willReturnMap([
  479. ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
  480. ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'yes'],
  481. ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'yes'],
  482. ['core', 'shareapi_exclude_groups', 'no', 'no'],
  483. ['core', 'shareapi_only_share_with_group_members', 'no', 'no'],
  484. ]);
  485. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $currentUser */
  486. $currentUser = $this->createMock(IUser::class);
  487. $currentUser->expects($this->once())
  488. ->method('getUID')
  489. ->willReturn('user001');
  490. $this->groupManager->expects($this->at(0))
  491. ->method('getUserGroupIds')
  492. ->with($this->equalTo($currentUser))
  493. ->willReturn(['group1', 'group2', 'group3']);
  494. $user1 = $this->createMock(IUser::class);
  495. $this->userManager->expects($this->at(0))
  496. ->method('get')
  497. ->with('user1')
  498. ->willReturn($user1);
  499. $this->groupManager->expects($this->at(1))
  500. ->method('getUserGroupIds')
  501. ->with($this->equalTo($user1))
  502. ->willReturn(['group1']);
  503. $this->knownUserService->method('isKnownToUser')
  504. ->willReturnMap([
  505. ['user001', 'user1', false],
  506. ['user001', 'user2', true],
  507. ['user001', 'user3', true],
  508. ]);
  509. $this->contactsManager->expects($this->once())
  510. ->method('search')
  511. ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
  512. ->willReturn([
  513. [
  514. 'UID' => 'user1',
  515. 'isLocalSystemBook' => true
  516. ],
  517. [
  518. 'UID' => 'user2',
  519. 'isLocalSystemBook' => true
  520. ],
  521. [
  522. 'UID' => 'user3',
  523. 'isLocalSystemBook' => true
  524. ],
  525. [
  526. 'UID' => 'contact',
  527. ],
  528. ]);
  529. $entries = $this->contactsStore->getContacts($currentUser, '');
  530. $this->assertCount(4, $entries);
  531. $this->assertEquals('user1', $entries[0]->getProperty('UID'));
  532. $this->assertEquals('user2', $entries[1]->getProperty('UID'));
  533. $this->assertEquals('user3', $entries[2]->getProperty('UID'));
  534. $this->assertEquals('contact', $entries[3]->getProperty('UID'));
  535. }
  536. public function testGetContactsOnlyEnumerateIfPhoneBookOrSameGroupInOwnGroupsOnly() {
  537. $this->config
  538. ->method('getAppValue')
  539. ->willReturnMap([
  540. ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
  541. ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'yes'],
  542. ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'yes'],
  543. ['core', 'shareapi_exclude_groups', 'no', 'no'],
  544. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  545. ]);
  546. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $currentUser */
  547. $currentUser = $this->createMock(IUser::class);
  548. $currentUser->expects($this->once())
  549. ->method('getUID')
  550. ->willReturn('user001');
  551. $this->groupManager->expects($this->at(0))
  552. ->method('getUserGroupIds')
  553. ->with($this->equalTo($currentUser))
  554. ->willReturn(['group1', 'group2', 'group3']);
  555. $user1 = $this->createMock(IUser::class);
  556. $this->userManager->expects($this->at(0))
  557. ->method('get')
  558. ->with('user1')
  559. ->willReturn($user1);
  560. $this->groupManager->expects($this->at(1))
  561. ->method('getUserGroupIds')
  562. ->with($this->equalTo($user1))
  563. ->willReturn(['group1']);
  564. $user2 = $this->createMock(IUser::class);
  565. $this->userManager->expects($this->at(1))
  566. ->method('get')
  567. ->with('user2')
  568. ->willReturn($user2);
  569. $this->groupManager->expects($this->at(2))
  570. ->method('getUserGroupIds')
  571. ->with($this->equalTo($user2))
  572. ->willReturn(['group2', 'group3']);
  573. $user3 = $this->createMock(IUser::class);
  574. $this->userManager->expects($this->at(2))
  575. ->method('get')
  576. ->with('user3')
  577. ->willReturn($user3);
  578. $this->groupManager->expects($this->at(3))
  579. ->method('getUserGroupIds')
  580. ->with($this->equalTo($user3))
  581. ->willReturn(['group8', 'group9']);
  582. $this->knownUserService->method('isKnownToUser')
  583. ->willReturnMap([
  584. ['user001', 'user1', false],
  585. ['user001', 'user2', true],
  586. ['user001', 'user3', true],
  587. ]);
  588. $this->contactsManager->expects($this->once())
  589. ->method('search')
  590. ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
  591. ->willReturn([
  592. [
  593. 'UID' => 'user1',
  594. 'isLocalSystemBook' => true
  595. ],
  596. [
  597. 'UID' => 'user2',
  598. 'isLocalSystemBook' => true
  599. ],
  600. [
  601. 'UID' => 'user3',
  602. 'isLocalSystemBook' => true
  603. ],
  604. [
  605. 'UID' => 'contact',
  606. ],
  607. ]);
  608. $entries = $this->contactsStore->getContacts($currentUser, '');
  609. $this->assertCount(3, $entries);
  610. $this->assertEquals('user1', $entries[0]->getProperty('UID'));
  611. $this->assertEquals('user2', $entries[1]->getProperty('UID'));
  612. $this->assertEquals('contact', $entries[2]->getProperty('UID'));
  613. }
  614. public function testGetContactsWithFilter() {
  615. $this->config
  616. ->method('getAppValue')
  617. ->willReturnMap([
  618. ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'no'],
  619. ['core', 'shareapi_restrict_user_enumeration_full_match', 'yes', 'yes'],
  620. ]);
  621. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  622. $user = $this->createMock(IUser::class);
  623. $this->contactsManager->expects($this->any())
  624. ->method('search')
  625. ->willReturn([
  626. [
  627. 'UID' => 'a567',
  628. 'FN' => 'Darren Roner',
  629. 'EMAIL' => [
  630. 'darren@roner.au',
  631. ],
  632. 'isLocalSystemBook' => true,
  633. ],
  634. [
  635. 'UID' => 'john',
  636. 'FN' => 'John Doe',
  637. 'EMAIL' => [
  638. 'john@example.com',
  639. ],
  640. 'isLocalSystemBook' => true,
  641. ],
  642. [
  643. 'FN' => 'Anne D',
  644. 'EMAIL' => [
  645. 'anne@example.com',
  646. ],
  647. 'isLocalSystemBook' => false,
  648. ],
  649. ]);
  650. $user->expects($this->any())
  651. ->method('getUID')
  652. ->willReturn('user123');
  653. // Complete match on UID should match
  654. $entry = $this->contactsStore->getContacts($user, 'a567');
  655. $this->assertSame(2, count($entry));
  656. $this->assertEquals([
  657. 'darren@roner.au'
  658. ], $entry[0]->getEMailAddresses());
  659. // Partial match on UID should not match
  660. $entry = $this->contactsStore->getContacts($user, 'a56');
  661. $this->assertSame(1, count($entry));
  662. $this->assertEquals([
  663. 'anne@example.com'
  664. ], $entry[0]->getEMailAddresses());
  665. // Complete match on email should match
  666. $entry = $this->contactsStore->getContacts($user, 'john@example.com');
  667. $this->assertSame(2, count($entry));
  668. $this->assertEquals([
  669. 'john@example.com'
  670. ], $entry[0]->getEMailAddresses());
  671. $this->assertEquals([
  672. 'anne@example.com'
  673. ], $entry[1]->getEMailAddresses());
  674. // Partial match on email should not match
  675. $entry = $this->contactsStore->getContacts($user, 'john@example.co');
  676. $this->assertSame(1, count($entry));
  677. $this->assertEquals([
  678. 'anne@example.com'
  679. ], $entry[0]->getEMailAddresses());
  680. // Match on FN should not match
  681. $entry = $this->contactsStore->getContacts($user, 'Darren Roner');
  682. $this->assertSame(1, count($entry));
  683. $this->assertEquals([
  684. 'anne@example.com'
  685. ], $entry[0]->getEMailAddresses());
  686. // Don't filter users in local addressbook
  687. $entry = $this->contactsStore->getContacts($user, 'Anne D');
  688. $this->assertSame(1, count($entry));
  689. $this->assertEquals([
  690. 'anne@example.com'
  691. ], $entry[0]->getEMailAddresses());
  692. }
  693. public function testGetContactsWithFilterWithoutFullMatch() {
  694. $this->config
  695. ->method('getAppValue')
  696. ->willReturnMap([
  697. ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'no'],
  698. ['core', 'shareapi_restrict_user_enumeration_full_match', 'yes', 'no'],
  699. ]);
  700. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  701. $user = $this->createMock(IUser::class);
  702. $this->contactsManager->expects($this->any())
  703. ->method('search')
  704. ->willReturn([
  705. [
  706. 'UID' => 'a567',
  707. 'FN' => 'Darren Roner',
  708. 'EMAIL' => [
  709. 'darren@roner.au',
  710. ],
  711. 'isLocalSystemBook' => true,
  712. ],
  713. [
  714. 'UID' => 'john',
  715. 'FN' => 'John Doe',
  716. 'EMAIL' => [
  717. 'john@example.com',
  718. ],
  719. 'isLocalSystemBook' => true,
  720. ],
  721. [
  722. 'FN' => 'Anne D',
  723. 'EMAIL' => [
  724. 'anne@example.com',
  725. ],
  726. 'isLocalSystemBook' => false,
  727. ],
  728. ]);
  729. $user->expects($this->any())
  730. ->method('getUID')
  731. ->willReturn('user123');
  732. // Complete match on UID should not match
  733. $entry = $this->contactsStore->getContacts($user, 'a567');
  734. $this->assertSame(1, count($entry));
  735. $this->assertEquals([
  736. 'anne@example.com'
  737. ], $entry[0]->getEMailAddresses());
  738. // Partial match on UID should not match
  739. $entry = $this->contactsStore->getContacts($user, 'a56');
  740. $this->assertSame(1, count($entry));
  741. $this->assertEquals([
  742. 'anne@example.com'
  743. ], $entry[0]->getEMailAddresses());
  744. // Complete match on email should not match
  745. $entry = $this->contactsStore->getContacts($user, 'john@example.com');
  746. $this->assertSame(1, count($entry));
  747. $this->assertEquals([
  748. 'anne@example.com'
  749. ], $entry[0]->getEMailAddresses());
  750. // Partial match on email should not match
  751. $entry = $this->contactsStore->getContacts($user, 'john@example.co');
  752. $this->assertSame(1, count($entry));
  753. $this->assertEquals([
  754. 'anne@example.com'
  755. ], $entry[0]->getEMailAddresses());
  756. // Match on FN should not match
  757. $entry = $this->contactsStore->getContacts($user, 'Darren Roner');
  758. $this->assertSame(1, count($entry));
  759. $this->assertEquals([
  760. 'anne@example.com'
  761. ], $entry[0]->getEMailAddresses());
  762. // Don't filter users in local addressbook
  763. $entry = $this->contactsStore->getContacts($user, 'Anne D');
  764. $this->assertSame(1, count($entry));
  765. $this->assertEquals([
  766. 'anne@example.com'
  767. ], $entry[0]->getEMailAddresses());
  768. }
  769. public function testFindOneUser() {
  770. $this->config->expects($this->at(0))->method('getAppValue')
  771. ->with($this->equalTo('core'), $this->equalTo('shareapi_allow_share_dialog_user_enumeration'), $this->equalTo('yes'))
  772. ->willReturn('yes');
  773. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  774. $user = $this->createMock(IUser::class);
  775. $this->contactsManager->expects($this->once())
  776. ->method('search')
  777. ->with($this->equalTo('a567'), $this->equalTo(['UID']))
  778. ->willReturn([
  779. [
  780. 'UID' => 123,
  781. 'isLocalSystemBook' => false
  782. ],
  783. [
  784. 'UID' => 'a567',
  785. 'FN' => 'Darren Roner',
  786. 'EMAIL' => [
  787. 'darren@roner.au'
  788. ],
  789. 'isLocalSystemBook' => true
  790. ],
  791. ]);
  792. $user->expects($this->any())
  793. ->method('getUID')
  794. ->willReturn('user123');
  795. $entry = $this->contactsStore->findOne($user, 0, 'a567');
  796. $this->assertEquals([
  797. 'darren@roner.au'
  798. ], $entry->getEMailAddresses());
  799. }
  800. public function testFindOneEMail() {
  801. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  802. $user = $this->createMock(IUser::class);
  803. $this->contactsManager->expects($this->once())
  804. ->method('search')
  805. ->with($this->equalTo('darren@roner.au'), $this->equalTo(['EMAIL']))
  806. ->willReturn([
  807. [
  808. 'UID' => 123,
  809. 'isLocalSystemBook' => false
  810. ],
  811. [
  812. 'UID' => 'a567',
  813. 'FN' => 'Darren Roner',
  814. 'EMAIL' => [
  815. 'darren@roner.au'
  816. ],
  817. 'isLocalSystemBook' => false
  818. ],
  819. ]);
  820. $user->expects($this->any())
  821. ->method('getUID')
  822. ->willReturn('user123');
  823. $entry = $this->contactsStore->findOne($user, 4, 'darren@roner.au');
  824. $this->assertEquals([
  825. 'darren@roner.au'
  826. ], $entry->getEMailAddresses());
  827. }
  828. public function testFindOneNotSupportedType() {
  829. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  830. $user = $this->createMock(IUser::class);
  831. $entry = $this->contactsStore->findOne($user, 42, 'darren@roner.au');
  832. $this->assertEquals(null, $entry);
  833. }
  834. public function testFindOneNoMatches() {
  835. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  836. $user = $this->createMock(IUser::class);
  837. $this->contactsManager->expects($this->once())
  838. ->method('search')
  839. ->with($this->equalTo('a567'), $this->equalTo(['UID']))
  840. ->willReturn([
  841. [
  842. 'UID' => 123,
  843. 'isLocalSystemBook' => false
  844. ],
  845. [
  846. 'UID' => 'a567',
  847. 'FN' => 'Darren Roner',
  848. 'EMAIL' => [
  849. 'darren@roner.au123'
  850. ],
  851. 'isLocalSystemBook' => false
  852. ],
  853. ]);
  854. $user->expects($this->once())
  855. ->method('getUID')
  856. ->willReturn('user123');
  857. $entry = $this->contactsStore->findOne($user, 0, 'a567');
  858. $this->assertEquals(null, $entry);
  859. }
  860. }