You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContactsStoreTest.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 OCP\Contacts\IManager;
  28. use OCP\IConfig;
  29. use OCP\IGroupManager;
  30. use OCP\IUser;
  31. use OCP\IUserManager;
  32. use PHPUnit_Framework_MockObject_MockObject;
  33. use Test\TestCase;
  34. class ContactsStoreTest extends TestCase {
  35. /** @var ContactsStore */
  36. private $contactsStore;
  37. /** @var IManager|PHPUnit_Framework_MockObject_MockObject */
  38. private $contactsManager;
  39. /** @var IUserManager|PHPUnit_Framework_MockObject_MockObject */
  40. private $userManager;
  41. /** @var IGroupManager|PHPUnit_Framework_MockObject_MockObject */
  42. private $groupManager;
  43. /** @var IConfig|PHPUnit_Framework_MockObject_MockObject */
  44. private $config;
  45. protected function setUp() {
  46. parent::setUp();
  47. $this->contactsManager = $this->createMock(IManager::class);
  48. $this->userManager = $this->createMock(IUserManager::class);
  49. $this->groupManager = $this->createMock(IGroupManager::class);
  50. $this->config = $this->createMock(IConfig::class);
  51. $this->contactsStore = new ContactsStore($this->contactsManager, $this->config, $this->userManager, $this->groupManager);
  52. }
  53. public function testGetContactsWithoutFilter() {
  54. /** @var IUser|PHPUnit_Framework_MockObject_MockObject $user */
  55. $user = $this->createMock(IUser::class);
  56. $this->contactsManager->expects($this->once())
  57. ->method('search')
  58. ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
  59. ->willReturn([
  60. [
  61. 'UID' => 123,
  62. ],
  63. [
  64. 'UID' => 567,
  65. 'FN' => 'Darren Roner',
  66. 'EMAIL' => [
  67. 'darren@roner.au'
  68. ],
  69. ],
  70. ]);
  71. $user->expects($this->once())
  72. ->method('getUID')
  73. ->willReturn('user123');
  74. $entries = $this->contactsStore->getContacts($user, '');
  75. $this->assertCount(2, $entries);
  76. $this->assertEquals([
  77. 'darren@roner.au'
  78. ], $entries[1]->getEMailAddresses());
  79. }
  80. public function testGetContactsHidesOwnEntry() {
  81. /** @var IUser|PHPUnit_Framework_MockObject_MockObject $user */
  82. $user = $this->createMock(IUser::class);
  83. $this->contactsManager->expects($this->once())
  84. ->method('search')
  85. ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
  86. ->willReturn([
  87. [
  88. 'UID' => 'user123',
  89. ],
  90. [
  91. 'UID' => 567,
  92. 'FN' => 'Darren Roner',
  93. 'EMAIL' => [
  94. 'darren@roner.au'
  95. ],
  96. ],
  97. ]);
  98. $user->expects($this->once())
  99. ->method('getUID')
  100. ->willReturn('user123');
  101. $entries = $this->contactsStore->getContacts($user, '');
  102. $this->assertCount(1, $entries);
  103. }
  104. public function testGetContactsWithoutBinaryImage() {
  105. /** @var IUser|PHPUnit_Framework_MockObject_MockObject $user */
  106. $user = $this->createMock(IUser::class);
  107. $this->contactsManager->expects($this->once())
  108. ->method('search')
  109. ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
  110. ->willReturn([
  111. [
  112. 'UID' => 123,
  113. ],
  114. [
  115. 'UID' => 567,
  116. 'FN' => 'Darren Roner',
  117. 'EMAIL' => [
  118. 'darren@roner.au'
  119. ],
  120. 'PHOTO' => base64_encode('photophotophoto'),
  121. ],
  122. ]);
  123. $user->expects($this->once())
  124. ->method('getUID')
  125. ->willReturn('user123');
  126. $entries = $this->contactsStore->getContacts($user, '');
  127. $this->assertCount(2, $entries);
  128. $this->assertNull($entries[1]->getAvatar());
  129. }
  130. public function testGetContactsWithoutAvatarURI() {
  131. /** @var IUser|PHPUnit_Framework_MockObject_MockObject $user */
  132. $user = $this->createMock(IUser::class);
  133. $this->contactsManager->expects($this->once())
  134. ->method('search')
  135. ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
  136. ->willReturn([
  137. [
  138. 'UID' => 123,
  139. ],
  140. [
  141. 'UID' => 567,
  142. 'FN' => 'Darren Roner',
  143. 'EMAIL' => [
  144. 'darren@roner.au'
  145. ],
  146. 'PHOTO' => 'VALUE=uri:https://photo',
  147. ],
  148. ]);
  149. $user->expects($this->once())
  150. ->method('getUID')
  151. ->willReturn('user123');
  152. $entries = $this->contactsStore->getContacts($user, '');
  153. $this->assertCount(2, $entries);
  154. $this->assertEquals('https://photo', $entries[1]->getAvatar());
  155. }
  156. public function testGetContactsWhenUserIsInExcludeGroups() {
  157. $this->config->expects($this->at(0))->method('getAppValue')
  158. ->with($this->equalTo('core'), $this->equalTo('shareapi_allow_share_dialog_user_enumeration'), $this->equalTo('yes'))
  159. ->willReturn('yes');
  160. $this->config->expects($this->at(1))
  161. ->method('getAppValue')
  162. ->with($this->equalTo('core'), $this->equalTo('shareapi_exclude_groups'), $this->equalTo('no'))
  163. ->willReturn('yes');
  164. $this->config->expects($this->at(2))
  165. ->method('getAppValue')
  166. ->with($this->equalTo('core'), $this->equalTo('shareapi_only_share_with_group_members'), $this->equalTo('no'))
  167. ->willReturn('yes');
  168. $this->config->expects($this->at(3))
  169. ->method('getAppValue')
  170. ->with($this->equalTo('core'), $this->equalTo('shareapi_exclude_groups_list'), $this->equalTo(''))
  171. ->willReturn('["group1", "group5", "group6"]');
  172. /** @var IUser|PHPUnit_Framework_MockObject_MockObject $currentUser */
  173. $currentUser = $this->createMock(IUser::class);
  174. $currentUser->expects($this->once())
  175. ->method('getUID')
  176. ->willReturn('user001');
  177. $this->groupManager->expects($this->once())
  178. ->method('getUserGroupIds')
  179. ->with($this->equalTo($currentUser))
  180. ->willReturn(['group1', 'group2', 'group3']);
  181. $this->contactsManager->expects($this->once())
  182. ->method('search')
  183. ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
  184. ->willReturn([
  185. [
  186. 'UID' => 'user123',
  187. 'isLocalSystemBook' => true
  188. ],
  189. [
  190. 'UID' => 'user12345',
  191. 'isLocalSystemBook' => true
  192. ],
  193. ]);
  194. $entries = $this->contactsStore->getContacts($currentUser, '');
  195. $this->assertCount(0, $entries);
  196. }
  197. public function testGetContactsOnlyIfInTheSameGroup() {
  198. $this->config->expects($this->at(0))->method('getAppValue')
  199. ->with($this->equalTo('core'), $this->equalTo('shareapi_allow_share_dialog_user_enumeration'), $this->equalTo('yes'))
  200. ->willReturn('yes');
  201. $this->config->expects($this->at(1)) ->method('getAppValue')
  202. ->with($this->equalTo('core'), $this->equalTo('shareapi_exclude_groups'), $this->equalTo('no'))
  203. ->willReturn('no');
  204. $this->config->expects($this->at(2))
  205. ->method('getAppValue')
  206. ->with($this->equalTo('core'), $this->equalTo('shareapi_only_share_with_group_members'), $this->equalTo('no'))
  207. ->willReturn('yes');
  208. /** @var IUser|PHPUnit_Framework_MockObject_MockObject $currentUser */
  209. $currentUser = $this->createMock(IUser::class);
  210. $currentUser->expects($this->once())
  211. ->method('getUID')
  212. ->willReturn('user001');
  213. $this->groupManager->expects($this->at(0))
  214. ->method('getUserGroupIds')
  215. ->with($this->equalTo($currentUser))
  216. ->willReturn(['group1', 'group2', 'group3']);
  217. $user1 = $this->createMock(IUser::class);
  218. $this->userManager->expects($this->at(0))
  219. ->method('get')
  220. ->with('user1')
  221. ->willReturn($user1);
  222. $this->groupManager->expects($this->at(1))
  223. ->method('getUserGroupIds')
  224. ->with($this->equalTo($user1))
  225. ->willReturn(['group1']);
  226. $user2 = $this->createMock(IUser::class);
  227. $this->userManager->expects($this->at(1))
  228. ->method('get')
  229. ->with('user2')
  230. ->willReturn($user2);
  231. $this->groupManager->expects($this->at(2))
  232. ->method('getUserGroupIds')
  233. ->with($this->equalTo($user2))
  234. ->willReturn(['group2', 'group3']);
  235. $user3 = $this->createMock(IUser::class);
  236. $this->userManager->expects($this->at(2))
  237. ->method('get')
  238. ->with('user3')
  239. ->willReturn($user3);
  240. $this->groupManager->expects($this->at(3))
  241. ->method('getUserGroupIds')
  242. ->with($this->equalTo($user3))
  243. ->willReturn(['group8', 'group9']);
  244. $this->contactsManager->expects($this->once())
  245. ->method('search')
  246. ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
  247. ->willReturn([
  248. [
  249. 'UID' => 'user1',
  250. 'isLocalSystemBook' => true
  251. ],
  252. [
  253. 'UID' => 'user2',
  254. 'isLocalSystemBook' => true
  255. ],
  256. [
  257. 'UID' => 'user3',
  258. 'isLocalSystemBook' => true
  259. ],
  260. [
  261. 'UID' => 'contact',
  262. ],
  263. ]);
  264. $entries = $this->contactsStore->getContacts($currentUser, '');
  265. $this->assertCount(3, $entries);
  266. $this->assertEquals('user1', $entries[0]->getProperty('UID'));
  267. $this->assertEquals('user2', $entries[1]->getProperty('UID'));
  268. $this->assertEquals('contact', $entries[2]->getProperty('UID'));
  269. }
  270. public function testGetContactsWithFilter() {
  271. $this->config->expects($this->at(0))->method('getAppValue')
  272. ->with($this->equalTo('core'), $this->equalTo('shareapi_allow_share_dialog_user_enumeration'), $this->equalTo('yes'))
  273. ->willReturn('no');
  274. /** @var IUser|PHPUnit_Framework_MockObject_MockObject $user */
  275. $user = $this->createMock(IUser::class);
  276. $this->contactsManager->expects($this->any())
  277. ->method('search')
  278. ->willReturn([
  279. [
  280. 'UID' => 'a567',
  281. 'FN' => 'Darren Roner',
  282. 'EMAIL' => [
  283. 'darren@roner.au',
  284. ],
  285. 'isLocalSystemBook' => true,
  286. ],
  287. [
  288. 'UID' => 'john',
  289. 'FN' => 'John Doe',
  290. 'EMAIL' => [
  291. 'john@example.com',
  292. ],
  293. 'isLocalSystemBook' => true,
  294. ],
  295. [
  296. 'FN' => 'Anne D',
  297. 'EMAIL' => [
  298. 'anne@example.com',
  299. ],
  300. 'isLocalSystemBook' => false,
  301. ],
  302. ]);
  303. $user->expects($this->any())
  304. ->method('getUID')
  305. ->willReturn('user123');
  306. // Complete match on UID should match
  307. $entry = $this->contactsStore->getContacts($user, 'a567');
  308. $this->assertSame(2, count($entry));
  309. $this->assertEquals([
  310. 'darren@roner.au'
  311. ], $entry[0]->getEMailAddresses());
  312. // Partial match on UID should not match
  313. $entry = $this->contactsStore->getContacts($user, 'a56');
  314. $this->assertSame(1, count($entry));
  315. $this->assertEquals([
  316. 'anne@example.com'
  317. ], $entry[0]->getEMailAddresses());
  318. // Complete match on email should match
  319. $entry = $this->contactsStore->getContacts($user, 'john@example.com');
  320. $this->assertSame(2, count($entry));
  321. $this->assertEquals([
  322. 'john@example.com'
  323. ], $entry[0]->getEMailAddresses());
  324. $this->assertEquals([
  325. 'anne@example.com'
  326. ], $entry[1]->getEMailAddresses());
  327. // Partial match on email should not match
  328. $entry = $this->contactsStore->getContacts($user, 'john@example.co');
  329. $this->assertSame(1, count($entry));
  330. $this->assertEquals([
  331. 'anne@example.com'
  332. ], $entry[0]->getEMailAddresses());
  333. // Match on FN should not match
  334. $entry = $this->contactsStore->getContacts($user, 'Darren Roner');
  335. $this->assertSame(1, count($entry));
  336. $this->assertEquals([
  337. 'anne@example.com'
  338. ], $entry[0]->getEMailAddresses());
  339. // Don't filter users in local addressbook
  340. $entry = $this->contactsStore->getContacts($user, 'Anne D');
  341. $this->assertSame(1, count($entry));
  342. $this->assertEquals([
  343. 'anne@example.com'
  344. ], $entry[0]->getEMailAddresses());
  345. }
  346. public function testFindOneUser() {
  347. $this->config->expects($this->at(0))->method('getAppValue')
  348. ->with($this->equalTo('core'), $this->equalTo('shareapi_allow_share_dialog_user_enumeration'), $this->equalTo('yes'))
  349. ->willReturn('yes');
  350. /** @var IUser|PHPUnit_Framework_MockObject_MockObject $user */
  351. $user = $this->createMock(IUser::class);
  352. $this->contactsManager->expects($this->once())
  353. ->method('search')
  354. ->with($this->equalTo('a567'), $this->equalTo(['UID']))
  355. ->willReturn([
  356. [
  357. 'UID' => 123,
  358. 'isLocalSystemBook' => false
  359. ],
  360. [
  361. 'UID' => 'a567',
  362. 'FN' => 'Darren Roner',
  363. 'EMAIL' => [
  364. 'darren@roner.au'
  365. ],
  366. 'isLocalSystemBook' => true
  367. ],
  368. ]);
  369. $user->expects($this->any())
  370. ->method('getUID')
  371. ->willReturn('user123');
  372. $entry = $this->contactsStore->findOne($user, 0, 'a567');
  373. $this->assertEquals([
  374. 'darren@roner.au'
  375. ], $entry->getEMailAddresses());
  376. }
  377. public function testFindOneEMail() {
  378. /** @var IUser|PHPUnit_Framework_MockObject_MockObject $user */
  379. $user = $this->createMock(IUser::class);
  380. $this->contactsManager->expects($this->once())
  381. ->method('search')
  382. ->with($this->equalTo('darren@roner.au'), $this->equalTo(['EMAIL']))
  383. ->willReturn([
  384. [
  385. 'UID' => 123,
  386. 'isLocalSystemBook' => false
  387. ],
  388. [
  389. 'UID' => 'a567',
  390. 'FN' => 'Darren Roner',
  391. 'EMAIL' => [
  392. 'darren@roner.au'
  393. ],
  394. 'isLocalSystemBook' => false
  395. ],
  396. ]);
  397. $user->expects($this->any())
  398. ->method('getUID')
  399. ->willReturn('user123');
  400. $entry = $this->contactsStore->findOne($user, 4, 'darren@roner.au');
  401. $this->assertEquals([
  402. 'darren@roner.au'
  403. ], $entry->getEMailAddresses());
  404. }
  405. public function testFindOneNotSupportedType() {
  406. /** @var IUser|PHPUnit_Framework_MockObject_MockObject $user */
  407. $user = $this->createMock(IUser::class);
  408. $entry = $this->contactsStore->findOne($user, 42, 'darren@roner.au');
  409. $this->assertEquals(null, $entry);
  410. }
  411. public function testFindOneNoMatches() {
  412. /** @var IUser|PHPUnit_Framework_MockObject_MockObject $user */
  413. $user = $this->createMock(IUser::class);
  414. $this->contactsManager->expects($this->once())
  415. ->method('search')
  416. ->with($this->equalTo('a567'), $this->equalTo(['UID']))
  417. ->willReturn([
  418. [
  419. 'UID' => 123,
  420. 'isLocalSystemBook' => false
  421. ],
  422. [
  423. 'UID' => 'a567',
  424. 'FN' => 'Darren Roner',
  425. 'EMAIL' => [
  426. 'darren@roner.au123'
  427. ],
  428. 'isLocalSystemBook' => false
  429. ],
  430. ]);
  431. $user->expects($this->once())
  432. ->method('getUID')
  433. ->willReturn('user123');
  434. $entry = $this->contactsStore->findOne($user, 0, 'a567');
  435. $this->assertEquals(null, $entry);
  436. }
  437. }