ContactsStoreTest.php 30 KB

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