ContactsSearchProviderTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Tests\unit;
  8. use OCA\DAV\CardDAV\CardDavBackend;
  9. use OCA\DAV\Search\ContactsSearchProvider;
  10. use OCP\App\IAppManager;
  11. use OCP\IL10N;
  12. use OCP\IURLGenerator;
  13. use OCP\IUser;
  14. use OCP\Search\ISearchQuery;
  15. use OCP\Search\SearchResult;
  16. use OCP\Search\SearchResultEntry;
  17. use Sabre\VObject\Reader;
  18. use Test\TestCase;
  19. class ContactsSearchProviderTest extends TestCase {
  20. /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
  21. private $appManager;
  22. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  23. private $l10n;
  24. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  25. private $urlGenerator;
  26. /** @var CardDavBackend|\PHPUnit\Framework\MockObject\MockObject */
  27. private $backend;
  28. /** @var ContactsSearchProvider */
  29. private $provider;
  30. private $vcardTest0 = 'BEGIN:VCARD' . PHP_EOL .
  31. 'VERSION:3.0' . PHP_EOL .
  32. 'PRODID:-//Sabre//Sabre VObject 4.1.2//EN' . PHP_EOL .
  33. 'UID:Test' . PHP_EOL .
  34. 'FN:FN of Test' . PHP_EOL .
  35. 'N:Test;;;;' . PHP_EOL .
  36. 'EMAIL:forrestgump@example.com' . PHP_EOL .
  37. 'END:VCARD';
  38. private $vcardTest1 = 'BEGIN:VCARD' . PHP_EOL .
  39. 'VERSION:3.0' . PHP_EOL .
  40. 'PRODID:-//Sabre//Sabre VObject 4.1.2//EN' . PHP_EOL .
  41. 'PHOTO;ENCODING=b;TYPE=image/jpeg:' . PHP_EOL .
  42. 'UID:Test2' . PHP_EOL .
  43. 'FN:FN of Test2' . PHP_EOL .
  44. 'N:Test2;;;;' . PHP_EOL .
  45. 'END:VCARD';
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->appManager = $this->createMock(IAppManager::class);
  49. $this->l10n = $this->createMock(IL10N::class);
  50. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  51. $this->backend = $this->createMock(CardDavBackend::class);
  52. $this->provider = new ContactsSearchProvider(
  53. $this->appManager,
  54. $this->l10n,
  55. $this->urlGenerator,
  56. $this->backend
  57. );
  58. }
  59. public function testGetId(): void {
  60. $this->assertEquals('contacts', $this->provider->getId());
  61. }
  62. public function testGetName(): void {
  63. $this->l10n->expects($this->exactly(1))
  64. ->method('t')
  65. ->with('Contacts')
  66. ->willReturnArgument(0);
  67. $this->assertEquals('Contacts', $this->provider->getName());
  68. }
  69. public function testSearchAppDisabled(): void {
  70. $user = $this->createMock(IUser::class);
  71. $query = $this->createMock(ISearchQuery::class);
  72. $this->appManager->expects($this->once())
  73. ->method('isEnabledForUser')
  74. ->with('contacts', $user)
  75. ->willReturn(false);
  76. $this->l10n->expects($this->exactly(1))
  77. ->method('t')
  78. ->with('Contacts')
  79. ->willReturnArgument(0);
  80. $this->backend->expects($this->never())
  81. ->method('getAddressBooksForUser');
  82. $this->backend->expects($this->never())
  83. ->method('searchPrincipalUri');
  84. $actual = $this->provider->search($user, $query);
  85. $data = $actual->jsonSerialize();
  86. $this->assertInstanceOf(SearchResult::class, $actual);
  87. $this->assertEquals('Contacts', $data['name']);
  88. $this->assertEmpty($data['entries']);
  89. $this->assertFalse($data['isPaginated']);
  90. $this->assertNull($data['cursor']);
  91. }
  92. public function testSearch(): void {
  93. $user = $this->createMock(IUser::class);
  94. $user->method('getUID')->willReturn('john.doe');
  95. $query = $this->createMock(ISearchQuery::class);
  96. $query->method('getTerm')->willReturn('search term');
  97. $query->method('getLimit')->willReturn(5);
  98. $query->method('getCursor')->willReturn(20);
  99. $this->appManager->expects($this->once())
  100. ->method('isEnabledForUser')
  101. ->with('contacts', $user)
  102. ->willReturn(true);
  103. $this->l10n->expects($this->exactly(1))
  104. ->method('t')
  105. ->with('Contacts')
  106. ->willReturnArgument(0);
  107. $this->backend->expects($this->once())
  108. ->method('getAddressBooksForUser')
  109. ->with('principals/users/john.doe')
  110. ->willReturn([
  111. [
  112. 'id' => 99,
  113. 'principaluri' => 'principals/users/john.doe',
  114. 'uri' => 'addressbook-uri-99',
  115. ], [
  116. 'id' => 123,
  117. 'principaluri' => 'principals/users/john.doe',
  118. 'uri' => 'addressbook-uri-123',
  119. ]
  120. ]);
  121. $this->backend->expects($this->once())
  122. ->method('searchPrincipalUri')
  123. ->with('principals/users/john.doe', '',
  124. [
  125. 'N',
  126. 'FN',
  127. 'NICKNAME',
  128. 'EMAIL',
  129. 'TEL',
  130. 'ADR',
  131. 'TITLE',
  132. 'ORG',
  133. 'NOTE',
  134. ],
  135. ['limit' => 5, 'offset' => 20, 'since' => null, 'until' => null, 'person' => null, 'company' => null])
  136. ->willReturn([
  137. [
  138. 'addressbookid' => 99,
  139. 'uri' => 'vcard0.vcf',
  140. 'carddata' => $this->vcardTest0,
  141. ],
  142. [
  143. 'addressbookid' => 123,
  144. 'uri' => 'vcard1.vcf',
  145. 'carddata' => $this->vcardTest1,
  146. ],
  147. ]);
  148. $provider = $this->getMockBuilder(ContactsSearchProvider::class)
  149. ->setConstructorArgs([
  150. $this->appManager,
  151. $this->l10n,
  152. $this->urlGenerator,
  153. $this->backend,
  154. ])
  155. ->setMethods([
  156. 'getDavUrlForContact',
  157. 'getDeepLinkToContactsApp',
  158. 'generateSubline',
  159. ])
  160. ->getMock();
  161. $provider->expects($this->once())
  162. ->method('getDavUrlForContact')
  163. ->with('principals/users/john.doe', 'addressbook-uri-123', 'vcard1.vcf')
  164. ->willReturn('absolute-thumbnail-url');
  165. $provider->expects($this->exactly(2))
  166. ->method('generateSubline')
  167. ->willReturn('subline');
  168. $provider->expects($this->exactly(2))
  169. ->method('getDeepLinkToContactsApp')
  170. ->withConsecutive(
  171. ['addressbook-uri-99', 'Test'],
  172. ['addressbook-uri-123', 'Test2']
  173. )
  174. ->willReturn('deep-link-to-contacts');
  175. $actual = $provider->search($user, $query);
  176. $data = $actual->jsonSerialize();
  177. $this->assertInstanceOf(SearchResult::class, $actual);
  178. $this->assertEquals('Contacts', $data['name']);
  179. $this->assertCount(2, $data['entries']);
  180. $this->assertTrue($data['isPaginated']);
  181. $this->assertEquals(22, $data['cursor']);
  182. $result0 = $data['entries'][0];
  183. $result0Data = $result0->jsonSerialize();
  184. $result1 = $data['entries'][1];
  185. $result1Data = $result1->jsonSerialize();
  186. $this->assertInstanceOf(SearchResultEntry::class, $result0);
  187. $this->assertEquals('', $result0Data['thumbnailUrl']);
  188. $this->assertEquals('FN of Test', $result0Data['title']);
  189. $this->assertEquals('subline', $result0Data['subline']);
  190. $this->assertEquals('deep-link-to-contacts', $result0Data['resourceUrl']);
  191. $this->assertEquals('icon-contacts-dark', $result0Data['icon']);
  192. $this->assertTrue($result0Data['rounded']);
  193. $this->assertInstanceOf(SearchResultEntry::class, $result1);
  194. $this->assertEquals('absolute-thumbnail-url?photo', $result1Data['thumbnailUrl']);
  195. $this->assertEquals('FN of Test2', $result1Data['title']);
  196. $this->assertEquals('subline', $result1Data['subline']);
  197. $this->assertEquals('deep-link-to-contacts', $result1Data['resourceUrl']);
  198. $this->assertEquals('icon-contacts-dark', $result1Data['icon']);
  199. $this->assertTrue($result1Data['rounded']);
  200. }
  201. public function testGetDavUrlForContact(): void {
  202. $this->urlGenerator->expects($this->once())
  203. ->method('linkTo')
  204. ->with('', 'remote.php')
  205. ->willReturn('link-to-remote.php');
  206. $this->urlGenerator->expects($this->once())
  207. ->method('getAbsoluteURL')
  208. ->with('link-to-remote.php/dav/addressbooks/users/john.doe/foo/bar.vcf')
  209. ->willReturn('absolute-url-link-to-remote.php/dav/addressbooks/users/john.doe/foo/bar.vcf');
  210. $actual = self::invokePrivate($this->provider, 'getDavUrlForContact', ['principals/users/john.doe', 'foo', 'bar.vcf']);
  211. $this->assertEquals('absolute-url-link-to-remote.php/dav/addressbooks/users/john.doe/foo/bar.vcf', $actual);
  212. }
  213. public function testGetDeepLinkToContactsApp(): void {
  214. $this->urlGenerator->expects($this->once())
  215. ->method('linkToRoute')
  216. ->with('contacts.contacts.direct', ['contact' => 'uid123~uri-john.doe'])
  217. ->willReturn('link-to-route-contacts.contacts.direct/direct/uid123~uri-john.doe');
  218. $this->urlGenerator->expects($this->once())
  219. ->method('getAbsoluteURL')
  220. ->with('link-to-route-contacts.contacts.direct/direct/uid123~uri-john.doe')
  221. ->willReturn('absolute-url-link-to-route-contacts.contacts.direct/direct/uid123~uri-john.doe');
  222. $actual = self::invokePrivate($this->provider, 'getDeepLinkToContactsApp', ['uri-john.doe', 'uid123']);
  223. $this->assertEquals('absolute-url-link-to-route-contacts.contacts.direct/direct/uid123~uri-john.doe', $actual);
  224. }
  225. public function testGenerateSubline(): void {
  226. $vCard0 = Reader::read($this->vcardTest0);
  227. $vCard1 = Reader::read($this->vcardTest1);
  228. $actual1 = self::invokePrivate($this->provider, 'generateSubline', [$vCard0]);
  229. $actual2 = self::invokePrivate($this->provider, 'generateSubline', [$vCard1]);
  230. $this->assertEquals('forrestgump@example.com', $actual1);
  231. $this->assertEquals('', $actual2);
  232. }
  233. }