2015-10-30 18:05:25 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
namespace OCA\DAV\Tests\Unit\CardDAV;
|
|
|
|
|
2015-12-14 13:48:31 +03:00
|
|
|
use InvalidArgumentException;
|
2015-10-30 18:05:25 +03:00
|
|
|
use OCA\DAV\CardDAV\CardDavBackend;
|
2015-11-25 17:24:50 +03:00
|
|
|
use OCA\DAV\Connector\Sabre\Principal;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\ILogger;
|
2015-10-30 18:05:25 +03:00
|
|
|
use Sabre\DAV\PropPatch;
|
2015-11-25 17:24:50 +03:00
|
|
|
use Sabre\VObject\Component\VCard;
|
|
|
|
use Sabre\VObject\Property\Text;
|
2015-10-30 18:05:25 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
2015-11-05 18:46:37 +03:00
|
|
|
/**
|
|
|
|
* Class CardDavBackendTest
|
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
*
|
|
|
|
* @package OCA\DAV\Tests\Unit\CardDAV
|
|
|
|
*/
|
2015-10-30 18:05:25 +03:00
|
|
|
class CardDavBackendTest extends TestCase {
|
|
|
|
|
|
|
|
/** @var CardDavBackend */
|
|
|
|
private $backend;
|
|
|
|
|
2015-11-25 17:24:50 +03:00
|
|
|
/** @var Principal | \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $principal;
|
|
|
|
|
|
|
|
/** @var ILogger | \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
/** @var IDBConnection */
|
|
|
|
private $db;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $dbCardsTable = 'cards';
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $dbCardsPropertiesTable = 'cards_properties';
|
|
|
|
|
2015-10-30 18:05:25 +03:00
|
|
|
const UNIT_TEST_USER = 'carddav-unit-test';
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2015-11-25 17:24:50 +03:00
|
|
|
$this->principal = $this->getMockBuilder('OCA\DAV\Connector\Sabre\Principal')
|
2015-11-05 18:46:37 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->setMethods(['getPrincipalByPath'])
|
|
|
|
->getMock();
|
2015-11-25 17:24:50 +03:00
|
|
|
$this->principal->method('getPrincipalByPath')
|
2015-11-05 18:46:37 +03:00
|
|
|
->willReturn([
|
|
|
|
'uri' => 'principals/best-friend'
|
|
|
|
]);
|
2015-11-25 17:24:50 +03:00
|
|
|
$this->logger = $this->getMock('\OCP\ILogger');
|
|
|
|
|
|
|
|
$this->db = \OC::$server->getDatabaseConnection();
|
|
|
|
|
|
|
|
$this->backend = new CardDavBackend($this->db, $this->principal, $this->logger);
|
|
|
|
|
|
|
|
// start every test with a empty cards_properties and cards table
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->delete('cards_properties')->execute();
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->delete('cards')->execute();
|
2015-11-05 18:46:37 +03:00
|
|
|
|
2015-10-30 18:05:25 +03:00
|
|
|
|
|
|
|
$this->tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
|
|
|
|
|
|
|
if (is_null($this->backend)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
|
|
|
|
foreach ($books as $book) {
|
|
|
|
$this->backend->deleteAddressBook($book['id']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddressBookOperations() {
|
|
|
|
|
|
|
|
// create a new address book
|
|
|
|
$this->backend->createAddressBook(self::UNIT_TEST_USER, 'Example', []);
|
|
|
|
|
|
|
|
$books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
|
|
|
|
$this->assertEquals(1, count($books));
|
2015-11-10 13:37:07 +03:00
|
|
|
$this->assertEquals('Example', $books[0]['{DAV:}displayname']);
|
2015-10-30 18:05:25 +03:00
|
|
|
|
|
|
|
// update it's display name
|
|
|
|
$patch = new PropPatch([
|
|
|
|
'{DAV:}displayname' => 'Unit test',
|
|
|
|
'{urn:ietf:params:xml:ns:carddav}addressbook-description' => 'Addressbook used for unit testing'
|
|
|
|
]);
|
|
|
|
$this->backend->updateAddressBook($books[0]['id'], $patch);
|
|
|
|
$patch->commit();
|
|
|
|
$books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
|
|
|
|
$this->assertEquals(1, count($books));
|
|
|
|
$this->assertEquals('Unit test', $books[0]['{DAV:}displayname']);
|
|
|
|
$this->assertEquals('Addressbook used for unit testing', $books[0]['{urn:ietf:params:xml:ns:carddav}addressbook-description']);
|
|
|
|
|
|
|
|
// delete the address book
|
|
|
|
$this->backend->deleteAddressBook($books[0]['id']);
|
|
|
|
$books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
|
|
|
|
$this->assertEquals(0, count($books));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCardOperations() {
|
2015-11-25 17:24:50 +03:00
|
|
|
|
2015-12-14 13:48:31 +03:00
|
|
|
/** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $backend */
|
2015-11-25 17:24:50 +03:00
|
|
|
$backend = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend')
|
|
|
|
->setConstructorArgs([$this->db, $this->principal, $this->logger])
|
|
|
|
->setMethods(['updateProperties', 'purgeProperties'])->getMock();
|
|
|
|
|
2015-10-30 18:05:25 +03:00
|
|
|
// create a new address book
|
2015-11-25 17:24:50 +03:00
|
|
|
$backend->createAddressBook(self::UNIT_TEST_USER, 'Example', []);
|
|
|
|
$books = $backend->getAddressBooksForUser(self::UNIT_TEST_USER);
|
2015-10-30 18:05:25 +03:00
|
|
|
$this->assertEquals(1, count($books));
|
|
|
|
$bookId = $books[0]['id'];
|
|
|
|
|
|
|
|
$uri = $this->getUniqueID('card');
|
2015-11-25 17:24:50 +03:00
|
|
|
// updateProperties is expected twice, once for createCard and once for updateCard
|
|
|
|
$backend->expects($this->at(0))->method('updateProperties')->with($bookId, $uri, '');
|
|
|
|
$backend->expects($this->at(1))->method('updateProperties')->with($bookId, $uri, '***');
|
|
|
|
// create a card
|
|
|
|
$backend->createCard($bookId, $uri, '');
|
2015-10-30 18:05:25 +03:00
|
|
|
|
|
|
|
// get all the cards
|
2015-11-25 17:24:50 +03:00
|
|
|
$cards = $backend->getCards($bookId);
|
2015-10-30 18:05:25 +03:00
|
|
|
$this->assertEquals(1, count($cards));
|
|
|
|
$this->assertEquals('', $cards[0]['carddata']);
|
|
|
|
|
|
|
|
// get the cards
|
2015-11-25 17:24:50 +03:00
|
|
|
$card = $backend->getCard($bookId, $uri);
|
2015-10-30 18:05:25 +03:00
|
|
|
$this->assertNotNull($card);
|
|
|
|
$this->assertArrayHasKey('id', $card);
|
|
|
|
$this->assertArrayHasKey('uri', $card);
|
|
|
|
$this->assertArrayHasKey('lastmodified', $card);
|
|
|
|
$this->assertArrayHasKey('etag', $card);
|
|
|
|
$this->assertArrayHasKey('size', $card);
|
|
|
|
$this->assertEquals('', $card['carddata']);
|
|
|
|
|
|
|
|
// update the card
|
2015-11-25 17:24:50 +03:00
|
|
|
$backend->updateCard($bookId, $uri, '***');
|
|
|
|
$card = $backend->getCard($bookId, $uri);
|
2015-10-30 18:05:25 +03:00
|
|
|
$this->assertEquals('***', $card['carddata']);
|
|
|
|
|
|
|
|
// delete the card
|
2015-11-25 17:24:50 +03:00
|
|
|
$backend->expects($this->once())->method('purgeProperties')->with($bookId, $card['id']);
|
|
|
|
$backend->deleteCard($bookId, $uri);
|
|
|
|
$cards = $backend->getCards($bookId);
|
2015-10-30 18:05:25 +03:00
|
|
|
$this->assertEquals(0, count($cards));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMultiCard() {
|
2015-11-25 17:24:50 +03:00
|
|
|
|
|
|
|
$this->backend = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend')
|
|
|
|
->setConstructorArgs([$this->db, $this->principal, $this->logger])
|
|
|
|
->setMethods(['updateProperties'])->getMock();
|
|
|
|
|
2015-10-30 18:05:25 +03:00
|
|
|
// create a new address book
|
|
|
|
$this->backend->createAddressBook(self::UNIT_TEST_USER, 'Example', []);
|
|
|
|
$books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
|
|
|
|
$this->assertEquals(1, count($books));
|
|
|
|
$bookId = $books[0]['id'];
|
|
|
|
|
|
|
|
// create a card
|
|
|
|
$uri0 = $this->getUniqueID('card');
|
|
|
|
$this->backend->createCard($bookId, $uri0, '');
|
|
|
|
$uri1 = $this->getUniqueID('card');
|
|
|
|
$this->backend->createCard($bookId, $uri1, '');
|
|
|
|
$uri2 = $this->getUniqueID('card');
|
|
|
|
$this->backend->createCard($bookId, $uri2, '');
|
|
|
|
|
|
|
|
// get all the cards
|
|
|
|
$cards = $this->backend->getCards($bookId);
|
|
|
|
$this->assertEquals(3, count($cards));
|
|
|
|
$this->assertEquals('', $cards[0]['carddata']);
|
|
|
|
$this->assertEquals('', $cards[1]['carddata']);
|
|
|
|
$this->assertEquals('', $cards[2]['carddata']);
|
|
|
|
|
|
|
|
// get the cards
|
|
|
|
$cards = $this->backend->getMultipleCards($bookId, [$uri1, $uri2]);
|
|
|
|
$this->assertEquals(2, count($cards));
|
|
|
|
foreach($cards as $card) {
|
|
|
|
$this->assertArrayHasKey('id', $card);
|
|
|
|
$this->assertArrayHasKey('uri', $card);
|
|
|
|
$this->assertArrayHasKey('lastmodified', $card);
|
|
|
|
$this->assertArrayHasKey('etag', $card);
|
|
|
|
$this->assertArrayHasKey('size', $card);
|
|
|
|
$this->assertEquals('', $card['carddata']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the card
|
|
|
|
$this->backend->deleteCard($bookId, $uri0);
|
|
|
|
$this->backend->deleteCard($bookId, $uri1);
|
|
|
|
$this->backend->deleteCard($bookId, $uri2);
|
|
|
|
$cards = $this->backend->getCards($bookId);
|
|
|
|
$this->assertEquals(0, count($cards));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSyncSupport() {
|
2015-11-25 17:24:50 +03:00
|
|
|
|
|
|
|
$this->backend = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend')
|
|
|
|
->setConstructorArgs([$this->db, $this->principal, $this->logger])
|
|
|
|
->setMethods(['updateProperties'])->getMock();
|
|
|
|
|
2015-10-30 18:05:25 +03:00
|
|
|
// create a new address book
|
|
|
|
$this->backend->createAddressBook(self::UNIT_TEST_USER, 'Example', []);
|
|
|
|
$books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
|
|
|
|
$this->assertEquals(1, count($books));
|
|
|
|
$bookId = $books[0]['id'];
|
|
|
|
|
|
|
|
// fist call without synctoken
|
|
|
|
$changes = $this->backend->getChangesForAddressBook($bookId, '', 1);
|
|
|
|
$syncToken = $changes['syncToken'];
|
|
|
|
|
|
|
|
// add a change
|
|
|
|
$uri0 = $this->getUniqueID('card');
|
|
|
|
$this->backend->createCard($bookId, $uri0, '');
|
|
|
|
|
|
|
|
// look for changes
|
|
|
|
$changes = $this->backend->getChangesForAddressBook($bookId, $syncToken, 1);
|
|
|
|
$this->assertEquals($uri0, $changes['added'][0]);
|
|
|
|
}
|
2015-11-05 18:46:37 +03:00
|
|
|
|
|
|
|
public function testSharing() {
|
|
|
|
$this->backend->createAddressBook(self::UNIT_TEST_USER, 'Example', []);
|
|
|
|
$books = $this->backend->getAddressBooksForUser(self::UNIT_TEST_USER);
|
|
|
|
$this->assertEquals(1, count($books));
|
|
|
|
|
|
|
|
$this->backend->updateShares('Example', [['href' => 'principal:principals/best-friend']], []);
|
|
|
|
|
|
|
|
$shares = $this->backend->getShares('Example');
|
|
|
|
$this->assertEquals(1, count($shares));
|
|
|
|
|
2015-11-18 17:04:53 +03:00
|
|
|
// adding the same sharee again has no effect
|
|
|
|
$this->backend->updateShares('Example', [['href' => 'principal:principals/best-friend']], []);
|
|
|
|
|
|
|
|
$shares = $this->backend->getShares('Example');
|
|
|
|
$this->assertEquals(1, count($shares));
|
|
|
|
|
2015-11-05 18:46:37 +03:00
|
|
|
$books = $this->backend->getAddressBooksForUser('principals/best-friend');
|
|
|
|
$this->assertEquals(1, count($books));
|
|
|
|
|
|
|
|
$this->backend->updateShares('Example', [], [['href' => 'principal:principals/best-friend']]);
|
|
|
|
|
|
|
|
$shares = $this->backend->getShares('Example');
|
|
|
|
$this->assertEquals(0, count($shares));
|
|
|
|
|
|
|
|
$books = $this->backend->getAddressBooksForUser('principals/best-friend');
|
|
|
|
$this->assertEquals(0, count($books));
|
|
|
|
}
|
2015-11-25 17:24:50 +03:00
|
|
|
|
|
|
|
public function testUpdateProperties() {
|
|
|
|
|
|
|
|
$bookId = 42;
|
|
|
|
$cardUri = 'card-uri';
|
|
|
|
$cardId = 2;
|
|
|
|
|
|
|
|
$backend = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend')
|
|
|
|
->setConstructorArgs([$this->db, $this->principal, $this->logger])
|
|
|
|
->setMethods(['getCardId'])->getMock();
|
|
|
|
|
|
|
|
$backend->expects($this->any())->method('getCardId')->willReturn($cardId);
|
|
|
|
|
|
|
|
// add properties for new vCard
|
|
|
|
$vCard = new VCard();
|
|
|
|
$vCard->add(new Text($vCard, 'UID', $cardUri));
|
|
|
|
$vCard->add(new Text($vCard, 'FN', 'John Doe'));
|
|
|
|
$this->invokePrivate($backend, 'updateProperties', [$bookId, $cardUri, $vCard->serialize()]);
|
|
|
|
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$result = $query->select('*')->from('cards_properties')->execute()->fetchAll();
|
|
|
|
|
|
|
|
$this->assertSame(2, count($result));
|
|
|
|
|
|
|
|
$this->assertSame('UID', $result[0]['name']);
|
|
|
|
$this->assertSame($cardUri, $result[0]['value']);
|
|
|
|
$this->assertSame($bookId, (int)$result[0]['addressbookid']);
|
|
|
|
$this->assertSame($cardId, (int)$result[0]['cardid']);
|
|
|
|
|
|
|
|
$this->assertSame('FN', $result[1]['name']);
|
|
|
|
$this->assertSame('John Doe', $result[1]['value']);
|
|
|
|
$this->assertSame($bookId, (int)$result[1]['addressbookid']);
|
|
|
|
$this->assertSame($cardId, (int)$result[1]['cardid']);
|
|
|
|
|
|
|
|
// update properties for existing vCard
|
|
|
|
$vCard = new VCard();
|
|
|
|
$vCard->add(new Text($vCard, 'FN', 'John Doe'));
|
|
|
|
$this->invokePrivate($backend, 'updateProperties', [$bookId, $cardUri, $vCard->serialize()]);
|
|
|
|
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$result = $query->select('*')->from('cards_properties')->execute()->fetchAll();
|
|
|
|
|
|
|
|
$this->assertSame(1, count($result));
|
|
|
|
|
|
|
|
$this->assertSame('FN', $result[0]['name']);
|
|
|
|
$this->assertSame('John Doe', $result[0]['value']);
|
|
|
|
$this->assertSame($bookId, (int)$result[0]['addressbookid']);
|
|
|
|
$this->assertSame($cardId, (int)$result[0]['cardid']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPurgeProperties() {
|
|
|
|
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->insert('cards_properties')
|
|
|
|
->values(
|
|
|
|
[
|
|
|
|
'addressbookid' => $query->createNamedParameter(1),
|
|
|
|
'cardid' => $query->createNamedParameter(1),
|
|
|
|
'name' => $query->createNamedParameter('name1'),
|
|
|
|
'value' => $query->createNamedParameter('value1'),
|
|
|
|
'preferred' => $query->createNamedParameter(0)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->insert('cards_properties')
|
|
|
|
->values(
|
|
|
|
[
|
|
|
|
'addressbookid' => $query->createNamedParameter(1),
|
|
|
|
'cardid' => $query->createNamedParameter(2),
|
|
|
|
'name' => $query->createNamedParameter('name2'),
|
|
|
|
'value' => $query->createNamedParameter('value2'),
|
|
|
|
'preferred' => $query->createNamedParameter(0)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
$this->invokePrivate($this->backend, 'purgeProperties', [1, 1]);
|
|
|
|
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$result = $query->select('*')->from('cards_properties')->execute()->fetchAll();
|
|
|
|
$this->assertSame(1, count($result));
|
|
|
|
$this->assertSame(1 ,(int)$result[0]['addressbookid']);
|
|
|
|
$this->assertSame(2 ,(int)$result[0]['cardid']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetCardId() {
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
|
|
|
|
$query->insert('cards')
|
|
|
|
->values(
|
|
|
|
[
|
|
|
|
'addressbookid' => $query->createNamedParameter(1),
|
|
|
|
'carddata' => $query->createNamedParameter(''),
|
|
|
|
'uri' => $query->createNamedParameter('uri'),
|
|
|
|
'lastmodified' => $query->createNamedParameter(4738743),
|
|
|
|
'etag' => $query->createNamedParameter('etag'),
|
|
|
|
'size' => $query->createNamedParameter(120)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$query->execute();
|
|
|
|
$id = $query->getLastInsertId();
|
|
|
|
|
|
|
|
$this->assertSame($id,
|
|
|
|
$this->invokePrivate($this->backend, 'getCardId', ['uri']));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testGetCardIdFailed() {
|
|
|
|
$this->invokePrivate($this->backend, 'getCardId', ['uri']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestSearch
|
|
|
|
*
|
|
|
|
* @param string $pattern
|
|
|
|
* @param array $expected
|
|
|
|
*/
|
2015-12-14 15:51:12 +03:00
|
|
|
public function testSearch($pattern, $properties, $expected) {
|
2015-12-14 13:48:31 +03:00
|
|
|
/** @var VCard $vCards */
|
2015-11-25 17:24:50 +03:00
|
|
|
$vCards = [];
|
|
|
|
$vCards[0] = new VCard();
|
|
|
|
$vCards[0]->add(new Text($vCards[0], 'UID', 'uid'));
|
|
|
|
$vCards[0]->add(new Text($vCards[0], 'FN', 'John Doe'));
|
2015-12-14 15:51:12 +03:00
|
|
|
$vCards[0]->add(new Text($vCards[0], 'CLOUD', 'john@owncloud.org'));
|
2015-11-25 17:24:50 +03:00
|
|
|
$vCards[1] = new VCard();
|
|
|
|
$vCards[1]->add(new Text($vCards[1], 'UID', 'uid'));
|
|
|
|
$vCards[1]->add(new Text($vCards[1], 'FN', 'John M. Doe'));
|
|
|
|
|
|
|
|
$vCardIds = [];
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
for($i=0; $i<2; $i++) {
|
|
|
|
$query->insert($this->dbCardsTable)
|
|
|
|
->values(
|
|
|
|
[
|
|
|
|
'addressbookid' => $query->createNamedParameter(0),
|
2015-12-14 13:48:31 +03:00
|
|
|
'carddata' => $query->createNamedParameter($vCards[$i]->serialize(), \PDO::PARAM_LOB),
|
2015-11-25 17:24:50 +03:00
|
|
|
'uri' => $query->createNamedParameter('uri' . $i),
|
2015-12-14 13:48:31 +03:00
|
|
|
'lastmodified' => $query->createNamedParameter(time()),
|
2015-11-25 17:24:50 +03:00
|
|
|
'etag' => $query->createNamedParameter('etag' . $i),
|
|
|
|
'size' => $query->createNamedParameter(120),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$query->execute();
|
|
|
|
$vCardIds[] = $query->getLastInsertId();
|
|
|
|
}
|
|
|
|
|
|
|
|
$query->insert($this->dbCardsPropertiesTable)
|
|
|
|
->values(
|
|
|
|
[
|
|
|
|
'addressbookid' => $query->createNamedParameter(0),
|
|
|
|
'cardid' => $query->createNamedParameter($vCardIds[0]),
|
|
|
|
'name' => $query->createNamedParameter('FN'),
|
|
|
|
'value' => $query->createNamedParameter('John Doe'),
|
|
|
|
'preferred' => $query->createNamedParameter(0)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$query->execute();
|
2015-12-14 15:51:12 +03:00
|
|
|
$query->insert($this->dbCardsPropertiesTable)
|
|
|
|
->values(
|
|
|
|
[
|
|
|
|
'addressbookid' => $query->createNamedParameter(0),
|
|
|
|
'cardid' => $query->createNamedParameter($vCardIds[0]),
|
|
|
|
'name' => $query->createNamedParameter('CLOUD'),
|
|
|
|
'value' => $query->createNamedParameter('John@owncloud.org'),
|
|
|
|
'preferred' => $query->createNamedParameter(0)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$query->execute();
|
2015-11-25 17:24:50 +03:00
|
|
|
$query->insert($this->dbCardsPropertiesTable)
|
|
|
|
->values(
|
|
|
|
[
|
|
|
|
'addressbookid' => $query->createNamedParameter(0),
|
|
|
|
'cardid' => $query->createNamedParameter($vCardIds[1]),
|
|
|
|
'name' => $query->createNamedParameter('FN'),
|
|
|
|
'value' => $query->createNamedParameter('John M. Doe'),
|
|
|
|
'preferred' => $query->createNamedParameter(0)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$query->execute();
|
|
|
|
|
2015-12-14 15:51:12 +03:00
|
|
|
$result = $this->backend->search(0, $pattern, $properties);
|
2015-11-25 17:24:50 +03:00
|
|
|
|
|
|
|
// check result
|
|
|
|
$this->assertSame(count($expected), count($result));
|
|
|
|
$found = [];
|
|
|
|
foreach ($result as $r) {
|
|
|
|
foreach ($expected as $exp) {
|
|
|
|
if (strpos($r, $exp) > 0) {
|
|
|
|
$found[$exp] = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertSame(count($expected), count($found));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestSearch() {
|
|
|
|
return [
|
2015-12-14 15:51:12 +03:00
|
|
|
['John', ['FN'], ['John Doe', 'John M. Doe']],
|
|
|
|
['M. Doe', ['FN'], ['John M. Doe']],
|
|
|
|
['Do', ['FN'], ['John Doe', 'John M. Doe']],
|
|
|
|
// check if duplicates are handled correctly
|
|
|
|
['John', ['FN', 'CLOUD'], ['John Doe', 'John M. Doe']],
|
2015-11-25 17:24:50 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetCardUri() {
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->insert($this->dbCardsTable)
|
|
|
|
->values(
|
|
|
|
[
|
|
|
|
'addressbookid' => $query->createNamedParameter(1),
|
|
|
|
'carddata' => $query->createNamedParameter('carddata', \PDO::PARAM_LOB),
|
|
|
|
'uri' => $query->createNamedParameter('uri'),
|
|
|
|
'lastmodified' => $query->createNamedParameter(5489543),
|
|
|
|
'etag' => $query->createNamedParameter('etag'),
|
|
|
|
'size' => $query->createNamedParameter(120),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
$id = $query->getLastInsertId();
|
|
|
|
|
|
|
|
$this->assertSame('uri', $this->backend->getCardUri($id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testGetCardUriFailed() {
|
|
|
|
$this->backend->getCardUri(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetContact() {
|
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
for($i=0; $i<2; $i++) {
|
|
|
|
$query->insert($this->dbCardsTable)
|
|
|
|
->values(
|
|
|
|
[
|
|
|
|
'addressbookid' => $query->createNamedParameter($i),
|
|
|
|
'carddata' => $query->createNamedParameter('carddata' . $i, \PDO::PARAM_LOB),
|
|
|
|
'uri' => $query->createNamedParameter('uri' . $i),
|
|
|
|
'lastmodified' => $query->createNamedParameter(5489543),
|
|
|
|
'etag' => $query->createNamedParameter('etag' . $i),
|
|
|
|
'size' => $query->createNamedParameter(120),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->backend->getContact('uri0');
|
|
|
|
$this->assertSame(7, count($result));
|
|
|
|
$this->assertSame(0, (int)$result['addressbookid']);
|
|
|
|
$this->assertSame('uri0', $result['uri']);
|
|
|
|
$this->assertSame(5489543, (int)$result['lastmodified']);
|
|
|
|
$this->assertSame('etag0', $result['etag']);
|
|
|
|
$this->assertSame(120, (int)$result['size']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetContactFail() {
|
|
|
|
$this->assertEmpty($this->backend->getContact('uri'));
|
|
|
|
}
|
|
|
|
|
2015-10-30 18:05:25 +03:00
|
|
|
}
|