55 * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
66 * SPDX-License-Identifier: AGPL-3.0-only
77 */
8+
89namespace OCA \DAV \Tests \unit \CardDAV ;
910
1011use GuzzleHttp \Exception \ClientException ;
@@ -100,7 +101,7 @@ public function testEmptySync(): void {
100101 'system ' ,
101102 'system ' ,
102103 '1234567890 ' ,
103- null ,
104+ ' 1 ' ,
104105 '1 ' ,
105106 'principals/system/system ' ,
106107 []
@@ -171,7 +172,7 @@ public function testSyncWithNewElement(): void {
171172 'system ' ,
172173 'system ' ,
173174 '1234567890 ' ,
174- null ,
175+ ' 1 ' ,
175176 '1 ' ,
176177 'principals/system/system ' ,
177178 []
@@ -242,7 +243,7 @@ public function testSyncWithUpdatedElement(): void {
242243 'system ' ,
243244 'system ' ,
244245 '1234567890 ' ,
245- null ,
246+ ' 1 ' ,
246247 '1 ' ,
247248 'principals/system/system ' ,
248249 []
@@ -283,7 +284,7 @@ public function testSyncWithDeletedElement(): void {
283284 'system ' ,
284285 'system ' ,
285286 '1234567890 ' ,
286- null ,
287+ ' 1 ' ,
287288 '1 ' ,
288289 'principals/system/system ' ,
289290 []
@@ -292,6 +293,97 @@ public function testSyncWithDeletedElement(): void {
292293 $ this ->assertEquals ('http://sabre.io/ns/sync/4 ' , $ token );
293294 }
294295
296+ public function testFullSyncWithOrphanElement (): void {
297+ $ pendingCards = [];
298+ $ this ->backend ->expects ($ this ->exactly (0 ))
299+ ->method ('createCard ' );
300+ $ this ->backend ->expects ($ this ->exactly (1 ))
301+ ->method ('updateCard ' )
302+ ->willReturnCallback (function ($ id , $ uri ) use (&$ pendingCards ) {
303+ unset($ pendingCards [$ uri ]);
304+ });
305+ $ this ->backend ->expects ($ this ->exactly (1 ))
306+ ->method ('markCardsAsPending ' )
307+ ->willReturnCallback (function ($ id ) use (&$ pendingCards ) {
308+ $ cards = array_values ($ this ->backend ->getCards ($ id ));
309+ $ uris = array_map (fn ($ card ) => $ card ['uri ' ], $ cards );
310+ $ pendingCards = array_combine ($ uris , $ cards );
311+ });
312+ $ this ->backend ->expects ($ this ->exactly (1 ))
313+ ->method ('getPendingCards ' )
314+ ->willReturnCallback (function ($ id ) use (&$ pendingCards ) {
315+ return array_values ($ pendingCards );
316+ });
317+ $ this ->backend ->expects ($ this ->exactly (1 ))
318+ ->method ('deleteCard ' );
319+
320+ $ body = '<?xml version="1.0"?>
321+ <d:multistatus xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns" xmlns:card="urn:ietf:params:xml:ns:carddav" xmlns:oc="http://owncloud.org/ns">
322+ <d:response>
323+ <d:href>/remote.php/dav/addressbooks/system/system/system/Database:alice.vcf</d:href>
324+ <d:propstat>
325+ <d:prop>
326+ <d:getcontenttype>text/vcard; charset=utf-8</d:getcontenttype>
327+ <d:getetag>"2df155fa5c2a24cd7f750353fc63f037"</d:getetag>
328+ </d:prop>
329+ <d:status>HTTP/1.1 200 OK</d:status>
330+ </d:propstat>
331+ </d:response>
332+ <d:sync-token>http://sabre.io/ns/sync/3</d:sync-token>
333+ </d:multistatus> ' ;
334+
335+ $ reportResponse = new Response (new PsrResponse (
336+ 207 ,
337+ ['Content-Type ' => 'application/xml; charset=utf-8 ' , 'Content-Length ' => strlen ($ body )],
338+ $ body
339+ ));
340+
341+ $ this ->client
342+ ->method ('request ' )
343+ ->willReturn ($ reportResponse );
344+
345+ $ vCard = 'BEGIN:VCARD
346+ VERSION:3.0
347+ PRODID:-//Sabre//Sabre VObject 4.5.4//EN
348+ UID:alice
349+ FN;X-NC-SCOPE=v2-federated:alice
350+ N;X-NC-SCOPE=v2-federated:alice;;;;
351+ X-SOCIALPROFILE;TYPE=NEXTCLOUD;X-NC-SCOPE=v2-published:https://server2.internal/index.php/u/alice
352+ CLOUD:alice@server2.internal
353+ END:VCARD ' ;
354+
355+ $ getResponse = new Response (new PsrResponse (
356+ 200 ,
357+ ['Content-Type ' => 'text/vcard; charset=utf-8 ' , 'Content-Length ' => strlen ($ vCard )],
358+ $ vCard ,
359+ ));
360+
361+ $ this ->client
362+ ->method ('get ' )
363+ ->willReturn ($ getResponse );
364+
365+ $ this ->backend ->method ('getCards ' )
366+ ->willReturn ([
367+ ['uri ' => 'Database:alice.vcf ' ],
368+ ['uri ' => 'Database:bob.vcf ' ],
369+ ]);
370+
371+ $ this ->service ->markCardsAsPending (1 );
372+ $ token = $ this ->service ->syncRemoteAddressBook (
373+ '' ,
374+ 'system ' ,
375+ 'system ' ,
376+ '1234567890 ' ,
377+ null ,
378+ '1 ' ,
379+ 'principals/system/system ' ,
380+ []
381+ )[0 ];
382+ $ this ->service ->deletePendingCards (1 );
383+
384+ $ this ->assertEquals ('http://sabre.io/ns/sync/3 ' , $ token );
385+ }
386+
295387 public function testEnsureSystemAddressBookExists (): void {
296388 /** @var CardDavBackend | \PHPUnit\Framework\MockObject\MockObject $backend */
297389 $ backend = $ this ->getMockBuilder (CardDavBackend::class)->disableOriginalConstructor ()->getMock ();
@@ -468,7 +560,7 @@ public function testUseAbsoluteUriReport(string $host, string $expected): void {
468560 'system ' ,
469561 'remote.php/dav/addressbooks/system/system/system ' ,
470562 '1234567890 ' ,
471- null ,
563+ ' 1 ' ,
472564 '1 ' ,
473565 'principals/system/system ' ,
474566 []
0 commit comments