From: Joas Schilling Date: Thu, 21 Jan 2016 11:19:28 +0000 (+0100) Subject: No card ID when deleting is acceptable X-Git-Tag: v9.0beta1~225^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=df27a939ce76c06a1a1f1ecf0525c62553ac4f0a;p=nextcloud-server.git No card ID when deleting is acceptable --- diff --git a/apps/dav/lib/carddav/carddavbackend.php b/apps/dav/lib/carddav/carddavbackend.php index 7b8c43958b6..97067abb7c3 100644 --- a/apps/dav/lib/carddav/carddavbackend.php +++ b/apps/dav/lib/carddav/carddavbackend.php @@ -536,7 +536,11 @@ class CardDavBackend implements BackendInterface, SyncSupport { * @return bool */ function deleteCard($addressBookId, $cardUri) { - $cardId = $this->getCardId($cardUri); + try { + $cardId = $this->getCardId($cardUri); + } catch (\InvalidArgumentException $e) { + $cardId = null; + } $query = $this->db->getQueryBuilder(); $ret = $query->delete('cards') ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) @@ -546,7 +550,9 @@ class CardDavBackend implements BackendInterface, SyncSupport { $this->addChange($addressBookId, $cardUri, 3); if ($ret === 1) { - $this->purgeProperties($addressBookId, $cardId); + if ($cardId !== null) { + $this->purgeProperties($addressBookId, $cardId); + } return true; } diff --git a/apps/dav/tests/unit/carddav/carddavbackendtest.php b/apps/dav/tests/unit/carddav/carddavbackendtest.php index 3291314ec40..1a0d32c186c 100644 --- a/apps/dav/tests/unit/carddav/carddavbackendtest.php +++ b/apps/dav/tests/unit/carddav/carddavbackendtest.php @@ -218,6 +218,47 @@ class CardDavBackendTest extends TestCase { $this->assertEquals(0, count($cards)); } + public function testDeleteWithoutCard() { + + $this->backend = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend') + ->setConstructorArgs([$this->db, $this->principal]) + ->setMethods([ + 'getCardId', + 'addChange', + 'purgeProperties', + 'updateProperties', + ]) + ->getMock(); + + // 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']; + $uri = $this->getUniqueID('card'); + + // create a new address book + $this->backend->expects($this->once()) + ->method('getCardId') + ->with($uri) + ->willThrowException(new \InvalidArgumentException()); + $this->backend->expects($this->exactly(2)) + ->method('addChange') + ->withConsecutive( + [$bookId, $uri, 1], + [$bookId, $uri, 3] + ); + $this->backend->expects($this->never()) + ->method('purgeProperties'); + + // create a card + $this->backend->createCard($bookId, $uri, ''); + + // delete the card + $this->assertTrue($this->backend->deleteCard($bookId, $uri)); + } + public function testSyncSupport() { $this->backend = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend')