ContactsSearchProviderTest.php 9.1 KB

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