Merge pull request #22398 from owncloud/fix-card-properties

Queries on the cards table by uri require the addressbook as well
This commit is contained in:
Thomas Müller 2016-02-16 10:19:51 +01:00
commit 1cb3583a9a
4 changed files with 18 additions and 15 deletions

View File

@ -178,7 +178,7 @@ class AddressBookImpl implements IAddressBook {
protected function createUid() { protected function createUid() {
do { do {
$uid = $this->getUid(); $uid = $this->getUid();
$contact = $this->backend->getContact($uid . '.vcf'); $contact = $this->backend->getContact($this->getKey(), $uid . '.vcf');
} while (!empty($contact)); } while (!empty($contact));
return $uid; return $uid;

View File

@ -548,7 +548,7 @@ class CardDavBackend implements BackendInterface, SyncSupport {
*/ */
function deleteCard($addressBookId, $cardUri) { function deleteCard($addressBookId, $cardUri) {
try { try {
$cardId = $this->getCardId($cardUri); $cardId = $this->getCardId($addressBookId, $cardUri);
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
$cardId = null; $cardId = null;
} }
@ -807,15 +807,16 @@ class CardDavBackend implements BackendInterface, SyncSupport {
/** /**
* return contact with the given URI * return contact with the given URI
* *
* @param int $addressBookId
* @param string $uri * @param string $uri
* @returns array * @returns array
*/ */
public function getContact($uri) { public function getContact($addressBookId, $uri) {
$result = []; $result = [];
$query = $this->db->getQueryBuilder(); $query = $this->db->getQueryBuilder();
$query->select('*')->from($this->dbCardsTable) $query->select('*')->from($this->dbCardsTable)
->where($query->expr()->eq('uri', $query->createParameter('uri'))) ->where($query->expr()->eq('uri', $query->createNamedParameter($uri)))
->setParameter('uri', $uri); ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId)));
$queryResult = $query->execute(); $queryResult = $query->execute();
$contact = $queryResult->fetch(); $contact = $queryResult->fetch();
$queryResult->closeCursor(); $queryResult->closeCursor();
@ -851,7 +852,7 @@ class CardDavBackend implements BackendInterface, SyncSupport {
* @param string $vCardSerialized * @param string $vCardSerialized
*/ */
protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) {
$cardId = $this->getCardId($cardUri); $cardId = $this->getCardId($addressBookId, $cardUri);
$vCard = $this->readCard($vCardSerialized); $vCard = $this->readCard($vCardSerialized);
$this->purgeProperties($addressBookId, $cardId); $this->purgeProperties($addressBookId, $cardId);
@ -913,13 +914,15 @@ class CardDavBackend implements BackendInterface, SyncSupport {
/** /**
* get ID from a given contact * get ID from a given contact
* *
* @param int $addressBookId
* @param string $uri * @param string $uri
* @return int * @return int
*/ */
protected function getCardId($uri) { protected function getCardId($addressBookId, $uri) {
$query = $this->db->getQueryBuilder(); $query = $this->db->getQueryBuilder();
$query->select('id')->from($this->dbCardsTable) $query->select('id')->from($this->dbCardsTable)
->where($query->expr()->eq('uri', $query->createNamedParameter($uri))); ->where($query->expr()->eq('uri', $query->createNamedParameter($uri)))
->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId)));
$result = $query->execute(); $result = $query->execute();
$cardIds = $result->fetch(); $cardIds = $result->fetch();

View File

@ -261,7 +261,7 @@ class AddressBookImplTest extends TestCase {
// simulate that 'uid0' already exists, so the second uid will be returned // simulate that 'uid0' already exists, so the second uid will be returned
$this->backend->expects($this->exactly(2))->method('getContact') $this->backend->expects($this->exactly(2))->method('getContact')
->willReturnCallback( ->willReturnCallback(
function($uid) { function($id, $uid) {
return ($uid === 'uid0.vcf'); return ($uid === 'uid0.vcf');
} }
); );

View File

@ -268,7 +268,7 @@ class CardDavBackendTest extends TestCase {
// create a new address book // create a new address book
$this->backend->expects($this->once()) $this->backend->expects($this->once())
->method('getCardId') ->method('getCardId')
->with($uri) ->with($bookId, $uri)
->willThrowException(new \InvalidArgumentException()); ->willThrowException(new \InvalidArgumentException());
$this->backend->expects($this->exactly(2)) $this->backend->expects($this->exactly(2))
->method('addChange') ->method('addChange')
@ -445,14 +445,14 @@ class CardDavBackendTest extends TestCase {
$id = $query->getLastInsertId(); $id = $query->getLastInsertId();
$this->assertSame($id, $this->assertSame($id,
$this->invokePrivate($this->backend, 'getCardId', ['uri'])); $this->invokePrivate($this->backend, 'getCardId', [1, 'uri']));
} }
/** /**
* @expectedException InvalidArgumentException * @expectedException InvalidArgumentException
*/ */
public function testGetCardIdFailed() { public function testGetCardIdFailed() {
$this->invokePrivate($this->backend, 'getCardId', ['uri']); $this->invokePrivate($this->backend, 'getCardId', [1, 'uri']);
} }
/** /**
@ -596,7 +596,7 @@ class CardDavBackendTest extends TestCase {
$query->execute(); $query->execute();
} }
$result = $this->backend->getContact('uri0'); $result = $this->backend->getContact(0, 'uri0');
$this->assertSame(7, count($result)); $this->assertSame(7, count($result));
$this->assertSame(0, (int)$result['addressbookid']); $this->assertSame(0, (int)$result['addressbookid']);
$this->assertSame('uri0', $result['uri']); $this->assertSame('uri0', $result['uri']);
@ -606,7 +606,7 @@ class CardDavBackendTest extends TestCase {
} }
public function testGetContactFail() { public function testGetContactFail() {
$this->assertEmpty($this->backend->getContact('uri')); $this->assertEmpty($this->backend->getContact(0, 'uri'));
} }
public function testCollectCardProperties() { public function testCollectCardProperties() {