CardDavBackendTest.php 21 KB

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