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 ;
@@ -104,7 +105,7 @@ public function testEmptySync(): void {
104105 'system ' ,
105106 'system ' ,
106107 '1234567890 ' ,
107- null ,
108+ ' 1 ' ,
108109 '1 ' ,
109110 'principals/system/system ' ,
110111 []
@@ -175,7 +176,7 @@ public function testSyncWithNewElement(): void {
175176 'system ' ,
176177 'system ' ,
177178 '1234567890 ' ,
178- null ,
179+ ' 1 ' ,
179180 '1 ' ,
180181 'principals/system/system ' ,
181182 []
@@ -246,7 +247,7 @@ public function testSyncWithUpdatedElement(): void {
246247 'system ' ,
247248 'system ' ,
248249 '1234567890 ' ,
249- null ,
250+ ' 1 ' ,
250251 '1 ' ,
251252 'principals/system/system ' ,
252253 []
@@ -287,7 +288,7 @@ public function testSyncWithDeletedElement(): void {
287288 'system ' ,
288289 'system ' ,
289290 '1234567890 ' ,
290- null ,
291+ ' 1 ' ,
291292 '1 ' ,
292293 'principals/system/system ' ,
293294 []
@@ -296,6 +297,97 @@ public function testSyncWithDeletedElement(): void {
296297 $ this ->assertEquals ('http://sabre.io/ns/sync/4 ' , $ token );
297298 }
298299
300+ public function testFullSyncWithOrphanElement (): void {
301+ $ pendingCards = [];
302+ $ this ->backend ->expects ($ this ->exactly (0 ))
303+ ->method ('createCard ' );
304+ $ this ->backend ->expects ($ this ->exactly (1 ))
305+ ->method ('updateCard ' )
306+ ->willReturnCallback (function ($ id , $ uri ) use (&$ pendingCards ) {
307+ unset($ pendingCards [$ uri ]);
308+ });
309+ $ this ->backend ->expects ($ this ->exactly (1 ))
310+ ->method ('markCardsAsPending ' )
311+ ->willReturnCallback (function ($ id ) use (&$ pendingCards ) {
312+ $ cards = array_values ($ this ->backend ->getCards ($ id ));
313+ $ uris = array_map (fn ($ card ) => $ card ['uri ' ], $ cards );
314+ $ pendingCards = array_combine ($ uris , $ cards );
315+ });
316+ $ this ->backend ->expects ($ this ->exactly (1 ))
317+ ->method ('getPendingCards ' )
318+ ->willReturnCallback (function ($ id ) use (&$ pendingCards ) {
319+ return array_values ($ pendingCards );
320+ });
321+ $ this ->backend ->expects ($ this ->exactly (1 ))
322+ ->method ('deleteCard ' );
323+
324+ $ body = '<?xml version="1.0"?>
325+ <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">
326+ <d:response>
327+ <d:href>/remote.php/dav/addressbooks/system/system/system/Database:alice.vcf</d:href>
328+ <d:propstat>
329+ <d:prop>
330+ <d:getcontenttype>text/vcard; charset=utf-8</d:getcontenttype>
331+ <d:getetag>"2df155fa5c2a24cd7f750353fc63f037"</d:getetag>
332+ </d:prop>
333+ <d:status>HTTP/1.1 200 OK</d:status>
334+ </d:propstat>
335+ </d:response>
336+ <d:sync-token>http://sabre.io/ns/sync/3</d:sync-token>
337+ </d:multistatus> ' ;
338+
339+ $ reportResponse = new Response (new PsrResponse (
340+ 207 ,
341+ ['Content-Type ' => 'application/xml; charset=utf-8 ' , 'Content-Length ' => strlen ($ body )],
342+ $ body
343+ ));
344+
345+ $ this ->client
346+ ->method ('request ' )
347+ ->willReturn ($ reportResponse );
348+
349+ $ vCard = 'BEGIN:VCARD
350+ VERSION:3.0
351+ PRODID:-//Sabre//Sabre VObject 4.5.4//EN
352+ UID:alice
353+ FN;X-NC-SCOPE=v2-federated:alice
354+ N;X-NC-SCOPE=v2-federated:alice;;;;
355+ X-SOCIALPROFILE;TYPE=NEXTCLOUD;X-NC-SCOPE=v2-published:https://server2.internal/index.php/u/alice
356+ CLOUD:alice@server2.internal
357+ END:VCARD ' ;
358+
359+ $ getResponse = new Response (new PsrResponse (
360+ 200 ,
361+ ['Content-Type ' => 'text/vcard; charset=utf-8 ' , 'Content-Length ' => strlen ($ vCard )],
362+ $ vCard ,
363+ ));
364+
365+ $ this ->client
366+ ->method ('get ' )
367+ ->willReturn ($ getResponse );
368+
369+ $ this ->backend ->method ('getCards ' )
370+ ->willReturn ([
371+ ['uri ' => 'Database:alice.vcf ' ],
372+ ['uri ' => 'Database:bob.vcf ' ],
373+ ]);
374+
375+ $ this ->service ->markCardsAsPending (1 );
376+ $ token = $ this ->service ->syncRemoteAddressBook (
377+ '' ,
378+ 'system ' ,
379+ 'system ' ,
380+ '1234567890 ' ,
381+ null ,
382+ '1 ' ,
383+ 'principals/system/system ' ,
384+ []
385+ )[0 ];
386+ $ this ->service ->deletePendingCards (1 );
387+
388+ $ this ->assertEquals ('http://sabre.io/ns/sync/3 ' , $ token );
389+ }
390+
299391 public function testEnsureSystemAddressBookExists (): void {
300392 /** @var CardDavBackend&MockObject $backend */
301393 $ backend = $ this ->createMock (CardDavBackend::class);
@@ -496,7 +588,7 @@ public function testUseAbsoluteUriReport(string $host, string $expected): void {
496588 'system ' ,
497589 'remote.php/dav/addressbooks/system/system/system ' ,
498590 '1234567890 ' ,
499- null ,
591+ ' 1 ' ,
500592 '1 ' ,
501593 'principals/system/system ' ,
502594 []
0 commit comments