]> source.dussan.org Git - nextcloud-server.git/commitdiff
No card ID when deleting is acceptable
authorJoas Schilling <nickvergessen@owncloud.com>
Thu, 21 Jan 2016 11:19:28 +0000 (12:19 +0100)
committerJoas Schilling <nickvergessen@owncloud.com>
Thu, 21 Jan 2016 11:19:28 +0000 (12:19 +0100)
apps/dav/lib/carddav/carddavbackend.php
apps/dav/tests/unit/carddav/carddavbackendtest.php

index 7b8c43958b6b65e0d7b632fe36b2215f598bb1ea..97067abb7c3e80272d9337dddd6e6c9917aa870d 100644 (file)
@@ -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;
                }
 
index 3291314ec407c1af4191497cf2d02724bcf6188a..1a0d32c186c3a438efd16aee586175a033686daf 100644 (file)
@@ -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')