CardDavBackendTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Georg Ehrke <georg@owncloud.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\Tests\unit\CardDAV;
  28. use InvalidArgumentException;
  29. use OCA\DAV\CardDAV\AddressBook;
  30. use OCA\DAV\CardDAV\CardDavBackend;
  31. use OCA\DAV\Connector\Sabre\Principal;
  32. use OCP\DB\QueryBuilder\IQueryBuilder;
  33. use OCP\IDBConnection;
  34. use OCP\IL10N;
  35. use Sabre\DAV\PropPatch;
  36. use Sabre\VObject\Component\VCard;
  37. use Sabre\VObject\Property\Text;
  38. use Test\TestCase;
  39. /**
  40. * Class CardDavBackendTest
  41. *
  42. * @group DB
  43. *
  44. * @package OCA\DAV\Tests\unit\CardDAV
  45. */
  46. class CardDavBackendTest extends TestCase {
  47. /** @var CardDavBackend */
  48. private $backend;
  49. /** @var Principal | \PHPUnit_Framework_MockObject_MockObject */
  50. private $principal;
  51. /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  52. private $userManager;
  53. /** @var IDBConnection */
  54. private $db;
  55. /** @var string */
  56. private $dbCardsTable = 'cards';
  57. /** @var string */
  58. private $dbCardsPropertiesTable = 'cards_properties';
  59. const UNIT_TEST_USER = 'principals/users/carddav-unit-test';
  60. const UNIT_TEST_USER1 = 'principals/users/carddav-unit-test1';
  61. const UNIT_TEST_GROUP = 'principals/groups/carddav-unit-test-group';
  62. public function setUp() {
  63. parent::setUp();
  64. $this->userManager = $this->getMockBuilder('OCP\IUserManager')
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $this->principal = $this->getMockBuilder('OCA\DAV\Connector\Sabre\Principal')
  68. ->disableOriginalConstructor()
  69. ->setMethods(['getPrincipalByPath', 'getGroupMembership'])
  70. ->getMock();
  71. $this->principal->method('getPrincipalByPath')
  72. ->willReturn([
  73. 'uri' => 'principals/best-friend'
  74. ]);
  75. $this->principal->method('getGroupMembership')
  76. ->withAnyParameters()
  77. ->willReturn([self::UNIT_TEST_GROUP]);
  78. $this->db = \OC::$server->getDatabaseConnection();
  79. $this->backend = new CardDavBackend($this->db, $this->principal, $this->userManager, null);
  80. // start every test with a empty cards_properties and cards table
  81. $query = $this->db->getQueryBuilder();
  82. $query->delete('cards_properties')->execute();
  83. $query = $this->db->getQueryBuilder();
  84. $query->delete('cards')->execute();
  85. $this->tearDown();
  86. }
  87. public function tearDown() {
  88. parent::tearDown();
  89. if (is_null($this->backend)) {
  90. return;
  91. }
  92. $books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
  93. foreach ($books as $book) {
  94. $this->backend->deleteAddressBook($book['id']);
  95. }
  96. }
  97. public function testAddressBookOperations() {
  98. // create a new address book
  99. $this->backend->createAddressBook(self::UNIT_TEST_USER, 'Example', []);
  100. $this->assertEquals(1, $this->backend->getAddressBooksForUserCount(self::UNIT_TEST_USER));
  101. $books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
  102. $this->assertEquals(1, count($books));
  103. $this->assertEquals('Example', $books[0]['{DAV:}displayname']);
  104. // update it's display name
  105. $patch = new PropPatch([
  106. '{DAV:}displayname' => 'Unit test',
  107. '{urn:ietf:params:xml:ns:carddav}addressbook-description' => 'Addressbook used for unit testing'
  108. ]);
  109. $this->backend->updateAddressBook($books[0]['id'], $patch);
  110. $patch->commit();
  111. $books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
  112. $this->assertEquals(1, count($books));
  113. $this->assertEquals('Unit test', $books[0]['{DAV:}displayname']);
  114. $this->assertEquals('Addressbook used for unit testing', $books[0]['{urn:ietf:params:xml:ns:carddav}addressbook-description']);
  115. // delete the address book
  116. $this->backend->deleteAddressBook($books[0]['id']);
  117. $books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
  118. $this->assertEquals(0, count($books));
  119. }
  120. public function testAddressBookSharing() {
  121. $this->backend->createAddressBook(self::UNIT_TEST_USER, 'Example', []);
  122. $books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
  123. $this->assertEquals(1, count($books));
  124. $l = $this->createMock(IL10N::class);
  125. $addressBook = new AddressBook($this->backend, $books[0], $l);
  126. $this->backend->updateShares($addressBook, [
  127. [
  128. 'href' => 'principal:' . self::UNIT_TEST_USER1,
  129. ],
  130. [
  131. 'href' => 'principal:' . self::UNIT_TEST_GROUP,
  132. ]
  133. ], []);
  134. $books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER1);
  135. $this->assertEquals(1, count($books));
  136. // delete the address book
  137. $this->backend->deleteAddressBook($books[0]['id']);
  138. $books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
  139. $this->assertEquals(0, count($books));
  140. }
  141. public function testCardOperations() {
  142. /** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $backend */
  143. $backend = $this->getMockBuilder(CardDavBackend::class)
  144. ->setConstructorArgs([$this->db, $this->principal, $this->userManager, null])
  145. ->setMethods(['updateProperties', 'purgeProperties'])->getMock();
  146. // create a new address book
  147. $backend->createAddressBook(self::UNIT_TEST_USER, 'Example', []);
  148. $books = $backend->getAddressBooksForUser(self::UNIT_TEST_USER);
  149. $this->assertEquals(1, count($books));
  150. $bookId = $books[0]['id'];
  151. $uri = $this->getUniqueID('card');
  152. // updateProperties is expected twice, once for createCard and once for updateCard
  153. $backend->expects($this->at(0))->method('updateProperties')->with($bookId, $uri, '');
  154. $backend->expects($this->at(1))->method('updateProperties')->with($bookId, $uri, '***');
  155. // create a card
  156. $backend->createCard($bookId, $uri, '');
  157. // get all the cards
  158. $cards = $backend->getCards($bookId);
  159. $this->assertEquals(1, count($cards));
  160. $this->assertEquals('', $cards[0]['carddata']);
  161. // get the cards
  162. $card = $backend->getCard($bookId, $uri);
  163. $this->assertNotNull($card);
  164. $this->assertArrayHasKey('id', $card);
  165. $this->assertArrayHasKey('uri', $card);
  166. $this->assertArrayHasKey('lastmodified', $card);
  167. $this->assertArrayHasKey('etag', $card);
  168. $this->assertArrayHasKey('size', $card);
  169. $this->assertEquals('', $card['carddata']);
  170. // update the card
  171. $backend->updateCard($bookId, $uri, '***');
  172. $card = $backend->getCard($bookId, $uri);
  173. $this->assertEquals('***', $card['carddata']);
  174. // delete the card
  175. $backend->expects($this->once())->method('purgeProperties')->with($bookId, $card['id']);
  176. $backend->deleteCard($bookId, $uri);
  177. $cards = $backend->getCards($bookId);
  178. $this->assertEquals(0, count($cards));
  179. }
  180. public function testMultiCard() {
  181. $this->backend = $this->getMockBuilder(CardDavBackend::class)
  182. ->setConstructorArgs([$this->db, $this->principal, $this->userManager, null])
  183. ->setMethods(['updateProperties'])->getMock();
  184. // create a new address book
  185. $this->backend->createAddressBook(self::UNIT_TEST_USER, 'Example', []);
  186. $books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
  187. $this->assertEquals(1, count($books));
  188. $bookId = $books[0]['id'];
  189. // create a card
  190. $uri0 = $this->getUniqueID('card');
  191. $this->backend->createCard($bookId, $uri0, '');
  192. $uri1 = $this->getUniqueID('card');
  193. $this->backend->createCard($bookId, $uri1, '');
  194. $uri2 = $this->getUniqueID('card');
  195. $this->backend->createCard($bookId, $uri2, '');
  196. // get all the cards
  197. $cards = $this->backend->getCards($bookId);
  198. $this->assertEquals(3, count($cards));
  199. $this->assertEquals('', $cards[0]['carddata']);
  200. $this->assertEquals('', $cards[1]['carddata']);
  201. $this->assertEquals('', $cards[2]['carddata']);
  202. // get the cards
  203. $cards = $this->backend->getMultipleCards($bookId, [$uri1, $uri2]);
  204. $this->assertEquals(2, count($cards));
  205. foreach($cards as $card) {
  206. $this->assertArrayHasKey('id', $card);
  207. $this->assertArrayHasKey('uri', $card);
  208. $this->assertArrayHasKey('lastmodified', $card);
  209. $this->assertArrayHasKey('etag', $card);
  210. $this->assertArrayHasKey('size', $card);
  211. $this->assertEquals('', $card['carddata']);
  212. }
  213. // delete the card
  214. $this->backend->deleteCard($bookId, $uri0);
  215. $this->backend->deleteCard($bookId, $uri1);
  216. $this->backend->deleteCard($bookId, $uri2);
  217. $cards = $this->backend->getCards($bookId);
  218. $this->assertEquals(0, count($cards));
  219. }
  220. public function testDeleteWithoutCard() {
  221. $this->backend = $this->getMockBuilder(CardDavBackend::class)
  222. ->setConstructorArgs([$this->db, $this->principal, $this->userManager, null])
  223. ->setMethods([
  224. 'getCardId',
  225. 'addChange',
  226. 'purgeProperties',
  227. 'updateProperties',
  228. ])
  229. ->getMock();
  230. // create a new address book
  231. $this->backend->createAddressBook(self::UNIT_TEST_USER, 'Example', []);
  232. $books = $this->backend->getUsersOwnAddressBooks(self::UNIT_TEST_USER);
  233. $this->assertEquals(1, count($books));
  234. $bookId = $books[0]['id'];
  235. $uri = $this->getUniqueID('card');
  236. // create a new address book
  237. $this->backend->expects($this->once())
  238. ->method('getCardId')
  239. ->with($bookId, $uri)
  240. ->willThrowException(new \InvalidArgumentException());
  241. $this->backend->expects($this->exactly(2))
  242. ->method('addChange')
  243. ->withConsecutive(
  244. [$bookId, $uri, 1],
  245. [$bookId, $uri, 3]
  246. );
  247. $this->backend->expects($this->never())
  248. ->method('purgeProperties');
  249. // create a card
  250. $this->backend->createCard($bookId, $uri, '');
  251. // delete the card
  252. $this->assertTrue($this->backend->deleteCard($bookId, $uri));
  253. }
  254. public function testSyncSupport() {
  255. $this->backend = $this->getMockBuilder(CardDavBackend::class)
  256. ->setConstructorArgs([$this->db, $this->principal, $this->userManager, null])
  257. ->setMethods(['updateProperties'])->getMock();
  258. // create a new address book
  259. $this->backend->createAddressBook(self::UNIT_TEST_USER, 'Example', []);
  260. $books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
  261. $this->assertEquals(1, count($books));
  262. $bookId = $books[0]['id'];
  263. // fist call without synctoken
  264. $changes = $this->backend->getChangesForAddressBook($bookId, '', 1);
  265. $syncToken = $changes['syncToken'];
  266. // add a change
  267. $uri0 = $this->getUniqueID('card');
  268. $this->backend->createCard($bookId, $uri0, '');
  269. // look for changes
  270. $changes = $this->backend->getChangesForAddressBook($bookId, $syncToken, 1);
  271. $this->assertEquals($uri0, $changes['added'][0]);
  272. }
  273. public function testSharing() {
  274. $this->backend->createAddressBook(self::UNIT_TEST_USER, 'Example', []);
  275. $books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
  276. $this->assertEquals(1, count($books));
  277. $l = $this->createMock(IL10N::class);
  278. $exampleBook = new AddressBook($this->backend, $books[0], $l);
  279. $this->backend->updateShares($exampleBook, [['href' => 'principal:principals/best-friend']], []);
  280. $shares = $this->backend->getShares($exampleBook->getResourceId());
  281. $this->assertEquals(1, count($shares));
  282. // adding the same sharee again has no effect
  283. $this->backend->updateShares($exampleBook, [['href' => 'principal:principals/best-friend']], []);
  284. $shares = $this->backend->getShares($exampleBook->getResourceId());
  285. $this->assertEquals(1, count($shares));
  286. $books = $this->backend->getAddressBooksForUser('principals/best-friend');
  287. $this->assertEquals(1, count($books));
  288. $this->backend->updateShares($exampleBook, [], ['principal:principals/best-friend']);
  289. $shares = $this->backend->getShares($exampleBook->getResourceId());
  290. $this->assertEquals(0, count($shares));
  291. $books = $this->backend->getAddressBooksForUser('principals/best-friend');
  292. $this->assertEquals(0, count($books));
  293. }
  294. public function testUpdateProperties() {
  295. $bookId = 42;
  296. $cardUri = 'card-uri';
  297. $cardId = 2;
  298. $backend = $this->getMockBuilder(CardDavBackend::class)
  299. ->setConstructorArgs([$this->db, $this->principal, $this->userManager, null])
  300. ->setMethods(['getCardId'])->getMock();
  301. $backend->expects($this->any())->method('getCardId')->willReturn($cardId);
  302. // add properties for new vCard
  303. $vCard = new VCard();
  304. $vCard->UID = $cardUri;
  305. $vCard->FN = 'John Doe';
  306. $this->invokePrivate($backend, 'updateProperties', [$bookId, $cardUri, $vCard->serialize()]);
  307. $query = $this->db->getQueryBuilder();
  308. $result = $query->select('*')->from('cards_properties')->execute()->fetchAll();
  309. $this->assertSame(2, count($result));
  310. $this->assertSame('UID', $result[0]['name']);
  311. $this->assertSame($cardUri, $result[0]['value']);
  312. $this->assertSame($bookId, (int)$result[0]['addressbookid']);
  313. $this->assertSame($cardId, (int)$result[0]['cardid']);
  314. $this->assertSame('FN', $result[1]['name']);
  315. $this->assertSame('John Doe', $result[1]['value']);
  316. $this->assertSame($bookId, (int)$result[1]['addressbookid']);
  317. $this->assertSame($cardId, (int)$result[1]['cardid']);
  318. // update properties for existing vCard
  319. $vCard = new VCard();
  320. $vCard->UID = $cardUri;
  321. $this->invokePrivate($backend, 'updateProperties', [$bookId, $cardUri, $vCard->serialize()]);
  322. $query = $this->db->getQueryBuilder();
  323. $result = $query->select('*')->from('cards_properties')->execute()->fetchAll();
  324. $this->assertSame(1, count($result));
  325. $this->assertSame('UID', $result[0]['name']);
  326. $this->assertSame($cardUri, $result[0]['value']);
  327. $this->assertSame($bookId, (int)$result[0]['addressbookid']);
  328. $this->assertSame($cardId, (int)$result[0]['cardid']);
  329. }
  330. public function testPurgeProperties() {
  331. $query = $this->db->getQueryBuilder();
  332. $query->insert('cards_properties')
  333. ->values(
  334. [
  335. 'addressbookid' => $query->createNamedParameter(1),
  336. 'cardid' => $query->createNamedParameter(1),
  337. 'name' => $query->createNamedParameter('name1'),
  338. 'value' => $query->createNamedParameter('value1'),
  339. 'preferred' => $query->createNamedParameter(0)
  340. ]
  341. );
  342. $query->execute();
  343. $query = $this->db->getQueryBuilder();
  344. $query->insert('cards_properties')
  345. ->values(
  346. [
  347. 'addressbookid' => $query->createNamedParameter(1),
  348. 'cardid' => $query->createNamedParameter(2),
  349. 'name' => $query->createNamedParameter('name2'),
  350. 'value' => $query->createNamedParameter('value2'),
  351. 'preferred' => $query->createNamedParameter(0)
  352. ]
  353. );
  354. $query->execute();
  355. $this->invokePrivate($this->backend, 'purgeProperties', [1, 1]);
  356. $query = $this->db->getQueryBuilder();
  357. $result = $query->select('*')->from('cards_properties')->execute()->fetchAll();
  358. $this->assertSame(1, count($result));
  359. $this->assertSame(1 ,(int)$result[0]['addressbookid']);
  360. $this->assertSame(2 ,(int)$result[0]['cardid']);
  361. }
  362. public function testGetCardId() {
  363. $query = $this->db->getQueryBuilder();
  364. $query->insert('cards')
  365. ->values(
  366. [
  367. 'addressbookid' => $query->createNamedParameter(1),
  368. 'carddata' => $query->createNamedParameter(''),
  369. 'uri' => $query->createNamedParameter('uri'),
  370. 'lastmodified' => $query->createNamedParameter(4738743),
  371. 'etag' => $query->createNamedParameter('etag'),
  372. 'size' => $query->createNamedParameter(120)
  373. ]
  374. );
  375. $query->execute();
  376. $id = $query->getLastInsertId();
  377. $this->assertSame($id,
  378. $this->invokePrivate($this->backend, 'getCardId', [1, 'uri']));
  379. }
  380. /**
  381. * @expectedException InvalidArgumentException
  382. */
  383. public function testGetCardIdFailed() {
  384. $this->invokePrivate($this->backend, 'getCardId', [1, 'uri']);
  385. }
  386. /**
  387. * @dataProvider dataTestSearch
  388. *
  389. * @param string $pattern
  390. * @param array $properties
  391. * @param array $expected
  392. */
  393. public function testSearch($pattern, $properties, $expected) {
  394. /** @var VCard $vCards */
  395. $vCards = [];
  396. $vCards[0] = new VCard();
  397. $vCards[0]->add(new Text($vCards[0], 'UID', 'uid'));
  398. $vCards[0]->add(new Text($vCards[0], 'FN', 'John Doe'));
  399. $vCards[0]->add(new Text($vCards[0], 'CLOUD', 'john@owncloud.org'));
  400. $vCards[1] = new VCard();
  401. $vCards[1]->add(new Text($vCards[1], 'UID', 'uid'));
  402. $vCards[1]->add(new Text($vCards[1], 'FN', 'John M. Doe'));
  403. $vCardIds = [];
  404. $query = $this->db->getQueryBuilder();
  405. for($i=0; $i<2; $i++) {
  406. $query->insert($this->dbCardsTable)
  407. ->values(
  408. [
  409. 'addressbookid' => $query->createNamedParameter(0),
  410. 'carddata' => $query->createNamedParameter($vCards[$i]->serialize(), IQueryBuilder::PARAM_LOB),
  411. 'uri' => $query->createNamedParameter('uri' . $i),
  412. 'lastmodified' => $query->createNamedParameter(time()),
  413. 'etag' => $query->createNamedParameter('etag' . $i),
  414. 'size' => $query->createNamedParameter(120),
  415. ]
  416. );
  417. $query->execute();
  418. $vCardIds[] = $query->getLastInsertId();
  419. }
  420. $query->insert($this->dbCardsPropertiesTable)
  421. ->values(
  422. [
  423. 'addressbookid' => $query->createNamedParameter(0),
  424. 'cardid' => $query->createNamedParameter($vCardIds[0]),
  425. 'name' => $query->createNamedParameter('FN'),
  426. 'value' => $query->createNamedParameter('John Doe'),
  427. 'preferred' => $query->createNamedParameter(0)
  428. ]
  429. );
  430. $query->execute();
  431. $query->insert($this->dbCardsPropertiesTable)
  432. ->values(
  433. [
  434. 'addressbookid' => $query->createNamedParameter(0),
  435. 'cardid' => $query->createNamedParameter($vCardIds[0]),
  436. 'name' => $query->createNamedParameter('CLOUD'),
  437. 'value' => $query->createNamedParameter('John@owncloud.org'),
  438. 'preferred' => $query->createNamedParameter(0)
  439. ]
  440. );
  441. $query->execute();
  442. $query->insert($this->dbCardsPropertiesTable)
  443. ->values(
  444. [
  445. 'addressbookid' => $query->createNamedParameter(0),
  446. 'cardid' => $query->createNamedParameter($vCardIds[1]),
  447. 'name' => $query->createNamedParameter('FN'),
  448. 'value' => $query->createNamedParameter('John M. Doe'),
  449. 'preferred' => $query->createNamedParameter(0)
  450. ]
  451. );
  452. $query->execute();
  453. $result = $this->backend->search(0, $pattern, $properties);
  454. // check result
  455. $this->assertSame(count($expected), count($result));
  456. $found = [];
  457. foreach ($result as $r) {
  458. foreach ($expected as $exp) {
  459. if ($r['uri'] === $exp[0] && strpos($r['carddata'], $exp[1]) > 0) {
  460. $found[$exp[1]] = true;
  461. break;
  462. }
  463. }
  464. }
  465. $this->assertSame(count($expected), count($found));
  466. }
  467. public function dataTestSearch() {
  468. return [
  469. ['John', ['FN'], [['uri0', 'John Doe'], ['uri1', 'John M. Doe']]],
  470. ['M. Doe', ['FN'], [['uri1', 'John M. Doe']]],
  471. ['Do', ['FN'], [['uri0', 'John Doe'], ['uri1', 'John M. Doe']]],
  472. 'check if duplicates are handled correctly' => ['John', ['FN', 'CLOUD'], [['uri0', 'John Doe'], ['uri1', 'John M. Doe']]],
  473. 'case insensitive' => ['john', ['FN'], [['uri0', 'John Doe'], ['uri1', 'John M. Doe']]]
  474. ];
  475. }
  476. public function testGetCardUri() {
  477. $query = $this->db->getQueryBuilder();
  478. $query->insert($this->dbCardsTable)
  479. ->values(
  480. [
  481. 'addressbookid' => $query->createNamedParameter(1),
  482. 'carddata' => $query->createNamedParameter('carddata', IQueryBuilder::PARAM_LOB),
  483. 'uri' => $query->createNamedParameter('uri'),
  484. 'lastmodified' => $query->createNamedParameter(5489543),
  485. 'etag' => $query->createNamedParameter('etag'),
  486. 'size' => $query->createNamedParameter(120),
  487. ]
  488. );
  489. $query->execute();
  490. $id = $query->getLastInsertId();
  491. $this->assertSame('uri', $this->backend->getCardUri($id));
  492. }
  493. /**
  494. * @expectedException InvalidArgumentException
  495. */
  496. public function testGetCardUriFailed() {
  497. $this->backend->getCardUri(1);
  498. }
  499. public function testGetContact() {
  500. $query = $this->db->getQueryBuilder();
  501. for($i=0; $i<2; $i++) {
  502. $query->insert($this->dbCardsTable)
  503. ->values(
  504. [
  505. 'addressbookid' => $query->createNamedParameter($i),
  506. 'carddata' => $query->createNamedParameter('carddata' . $i, IQueryBuilder::PARAM_LOB),
  507. 'uri' => $query->createNamedParameter('uri' . $i),
  508. 'lastmodified' => $query->createNamedParameter(5489543),
  509. 'etag' => $query->createNamedParameter('etag' . $i),
  510. 'size' => $query->createNamedParameter(120),
  511. ]
  512. );
  513. $query->execute();
  514. }
  515. $result = $this->backend->getContact(0, 'uri0');
  516. $this->assertSame(7, count($result));
  517. $this->assertSame(0, (int)$result['addressbookid']);
  518. $this->assertSame('uri0', $result['uri']);
  519. $this->assertSame(5489543, (int)$result['lastmodified']);
  520. $this->assertSame('etag0', $result['etag']);
  521. $this->assertSame(120, (int)$result['size']);
  522. // this shouldn't return any result because 'uri1' is in address book 1
  523. // see https://github.com/nextcloud/server/issues/229
  524. $result = $this->backend->getContact(0, 'uri1');
  525. $this->assertEmpty($result);
  526. }
  527. public function testGetContactFail() {
  528. $this->assertEmpty($this->backend->getContact(0, 'uri'));
  529. }
  530. public function testCollectCardProperties() {
  531. $query = $this->db->getQueryBuilder();
  532. $query->insert($this->dbCardsPropertiesTable)
  533. ->values(
  534. [
  535. 'addressbookid' => $query->createNamedParameter(666),
  536. 'cardid' => $query->createNamedParameter(777),
  537. 'name' => $query->createNamedParameter('FN'),
  538. 'value' => $query->createNamedParameter('John Doe'),
  539. 'preferred' => $query->createNamedParameter(0)
  540. ]
  541. )
  542. ->execute();
  543. $result = $this->backend->collectCardProperties(666, 'FN');
  544. $this->assertEquals(['John Doe'], $result);
  545. }
  546. }