refactor: replace custom CRM with Monica fork
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s

This commit is contained in:
Kroonk
2026-05-22 16:19:55 +02:00
parent 38cc4c09ca
commit a213a1dba0
2215 changed files with 242567 additions and 6015 deletions

View File

@@ -0,0 +1,87 @@
<?php
namespace Tests\Unit\Services\DavClient\Utils;
use Tests\TestCase;
use Mockery\MockInterface;
use App\Jobs\Dav\PushVCard;
use Tests\Api\DAV\CardEtag;
use Tests\Helpers\DavTester;
use App\Models\User\SyncToken;
use App\Models\Contact\Contact;
use App\Models\Account\AddressBookSubscription;
use App\Services\DavClient\Utils\Model\SyncDto;
use App\Services\DavClient\Utils\Model\ContactDto;
use App\Services\DavClient\Utils\Model\ContactPushDto;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Http\Controllers\DAV\Backend\CardDAV\CardDAVBackend;
use App\Services\DavClient\Utils\AddressBookContactsPushMissed;
class AddressBookContactsPushMissedTest extends TestCase
{
use DatabaseTransactions;
use CardEtag;
/** @test */
public function it_push_contacts_missed()
{
$subscription = AddressBookSubscription::factory()->create();
$token = factory(SyncToken::class)->create([
'account_id' => $subscription->account_id,
'user_id' => $subscription->user_id,
'name' => 'contacts1',
'timestamp' => now()->addDays(-1),
]);
$subscription->localSyncToken = $token->id;
$subscription->save();
$contact = factory(Contact::class)->create([
'account_id' => $subscription->account_id,
'first_name' => 'Test',
'uuid' => 'affacde9-b2fe-4371-9acb-6612aaee6971',
]);
$card = $this->getCard($contact);
$etag = $this->getEtag($contact, true);
$this->mock(CardDAVBackend::class, function (MockInterface $mock) use ($card, $etag, $contact) {
$mock->shouldReceive('init')->andReturn($mock);
$mock->shouldReceive('getUuid')
->once()
->withArgs(function ($uri) {
$this->assertEquals('uuid6', $uri);
return true;
})
->andReturn('uuid3');
$mock->shouldReceive('prepareCard')
->once()
->withArgs(function ($c) use ($contact) {
$this->assertEquals($contact, $c);
return true;
})
->andReturn([
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'carddata' => $card,
'uri' => 'uuid3',
'etag' => $etag,
]);
});
$client = (new DavTester())->fake()->client();
$batchs = (new AddressBookContactsPushMissed())
->execute(new SyncDto($subscription, $client), [], collect([
'uuid6' => new ContactDto('uuid6', $etag),
]), collect([$contact]));
$this->assertCount(1, $batchs);
$batch = $batchs->first();
$this->assertInstanceOf(PushVCard::class, $batch);
$dto = $this->getPrivateValue($batch, 'contact');
$this->assertInstanceOf(ContactPushDto::class, $dto);
$this->assertEquals('uuid3', $dto->uri);
$this->assertEquals(2, $dto->mode);
}
}

View File

@@ -0,0 +1,170 @@
<?php
namespace Tests\Unit\Services\DavClient\Utils;
use Tests\TestCase;
use Mockery\MockInterface;
use App\Jobs\Dav\PushVCard;
use Illuminate\Support\Str;
use Tests\Api\DAV\CardEtag;
use Tests\Helpers\DavTester;
use App\Jobs\Dav\DeleteVCard;
use App\Models\User\SyncToken;
use App\Models\Contact\Contact;
use App\Models\Account\AddressBookSubscription;
use App\Services\DavClient\Utils\Model\SyncDto;
use App\Services\DavClient\Utils\Model\ContactDto;
use App\Services\DavClient\Utils\Model\ContactPushDto;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\DavClient\Utils\AddressBookContactsPush;
use App\Http\Controllers\DAV\Backend\CardDAV\CardDAVBackend;
class AddressBookContactsPushTest extends TestCase
{
use DatabaseTransactions;
use CardEtag;
/** @test */
public function it_push_contacts_added()
{
$subscription = AddressBookSubscription::factory()->create();
$token = factory(SyncToken::class)->create([
'account_id' => $subscription->account_id,
'user_id' => $subscription->user_id,
'name' => 'contacts1',
'timestamp' => now()->addDays(-1),
]);
$subscription->localSyncToken = $token->id;
$subscription->save();
$contact = factory(Contact::class)->create([
'account_id' => $subscription->account_id,
'first_name' => 'Test',
'uuid' => 'affacde9-b2fe-4371-9acb-6612aaee6971',
]);
$card = $this->getCard($contact);
$etag = $this->getEtag($contact, true);
$this->mock(CardDAVBackend::class, function (MockInterface $mock) use ($contact, $card, $etag) {
$mock->shouldReceive('init')->andReturn($mock);
$mock->shouldReceive('getCard')
->withArgs(function ($name, $uri) {
$this->assertEquals($uri, 'uricontact2');
return true;
})
->andReturn([
'contact_id' => $contact->id,
'carddata' => $card,
'etag' => $etag,
'distant_etag' => $etag,
]);
$mock->shouldReceive('getUuid')
->withArgs(function ($uri) {
$this->assertEquals($uri, 'https://test/dav/uricontact1');
return true;
})
->andReturn('uricontact1');
});
$client = (new DavTester())->fake()->client();
$batchs = (new AddressBookContactsPush())
->execute(new SyncDto($subscription, $client), collect([
'https://test/dav/uricontact1' => new ContactDto('https://test/dav/uricontact1', $etag),
]), [
'added' => ['uricontact2'],
]);
$this->assertCount(1, $batchs);
$batch = $batchs->first();
$this->assertInstanceOf(PushVCard::class, $batch);
$dto = $this->getPrivateValue($batch, 'contact');
$this->assertInstanceOf(ContactPushDto::class, $dto);
$this->assertEquals('uricontact2', $dto->uri);
$this->assertEquals(ContactPushDto::MODE_MATCH_NONE, $dto->mode);
}
/** @test */
public function it_push_contacts_modified()
{
$subscription = AddressBookSubscription::factory()->create();
$token = factory(SyncToken::class)->create([
'account_id' => $subscription->account_id,
'user_id' => $subscription->user_id,
'name' => 'contacts1',
'timestamp' => now()->addDays(-1),
]);
$subscription->localSyncToken = $token->id;
$subscription->save();
$contact = factory(Contact::class)->create([
'account_id' => $subscription->account_id,
'first_name' => 'Test',
'uuid' => 'affacde9-b2fe-4371-9acb-6612aaee6971',
]);
$card = $this->getCard($contact);
$etag = $this->getEtag($contact, true);
$this->mock(CardDAVBackend::class, function (MockInterface $mock) use ($contact, $card, $etag) {
$mock->shouldReceive('init')->andReturn($mock);
$mock->shouldReceive('getUuid')
->withArgs(function ($uri) {
$this->assertStringContainsString('uricontact', $uri);
return true;
})
->andReturnUsing(function ($uri) {
return Str::contains($uri, 'uricontact1') ? 'uricontact1' : 'uricontact2';
});
$mock->shouldReceive('getCard')
->withArgs(function ($name, $uri) {
$this->assertEquals($uri, 'uricontact2');
return true;
})
->andReturn([
'contact_id' => $contact->id,
'carddata' => $card,
'etag' => $etag,
'distant_etag' => $etag,
]);
});
$client = (new DavTester())->fake()->client();
$batchs = (new AddressBookContactsPush())
->execute(new SyncDto($subscription, $client), collect([
'https://test/dav/uricontact1' => new ContactDto('https://test/dav/uricontact1', $etag),
]), [
'modified' => ['uricontact2'],
]);
$this->assertCount(1, $batchs);
$batch = $batchs->first();
$this->assertInstanceOf(PushVCard::class, $batch);
$dto = $this->getPrivateValue($batch, 'contact');
$this->assertInstanceOf(ContactPushDto::class, $dto);
$this->assertEquals('uricontact2', $dto->uri);
$this->assertEquals(1, $dto->mode);
}
/** @test */
public function it_delete_contacts_removed()
{
$subscription = AddressBookSubscription::factory()->create();
$client = (new DavTester())->fake()->client();
$batchs = (new AddressBookContactsPush())
->execute(new SyncDto($subscription, $client), collect(), [
'deleted' => ['uricontact2'],
]);
$this->assertCount(1, $batchs);
$batch = $batchs->first();
$this->assertInstanceOf(DeleteVCard::class, $batch);
$uri = $this->getPrivateValue($batch, 'uri');
$this->assertEquals('uricontact2', $uri);
}
}

View File

@@ -0,0 +1,81 @@
<?php
namespace Tests\Unit\Services\DavClient\Utils;
use Tests\TestCase;
use Mockery\MockInterface;
use Tests\Api\DAV\CardEtag;
use Tests\Helpers\DavTester;
use App\Models\User\SyncToken;
use App\Models\Contact\Contact;
use App\Jobs\Dav\GetMultipleVCard;
use App\Models\Account\AddressBookSubscription;
use App\Services\DavClient\Utils\Model\SyncDto;
use App\Services\DavClient\Utils\Model\ContactDto;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Http\Controllers\DAV\Backend\CardDAV\CardDAVBackend;
use App\Services\DavClient\Utils\AddressBookContactsUpdaterMissed;
class AddressBookContactsUpdaterMissedTest extends TestCase
{
use DatabaseTransactions;
use CardEtag;
/** @test */
public function it_sync_changes_missed()
{
$subscription = AddressBookSubscription::factory()->create();
$token = factory(SyncToken::class)->create([
'account_id' => $subscription->account_id,
'user_id' => $subscription->user_id,
'name' => 'contacts1',
'timestamp' => now()->addDays(-1),
]);
$subscription->localSyncToken = $token->id;
$subscription->save();
$contact = new Contact();
$contact->forceFill([
'first_name' => 'Test',
'uuid' => 'affacde9-b2fe-4371-9acb-6612aaee6971',
'updated_at' => now(),
]);
$card = $this->getCard($contact);
$etag = $this->getEtag($contact, true);
$this->mock(CardDAVBackend::class, function (MockInterface $mock) use ($card, $etag) {
$mock->shouldReceive('init')->andReturn($mock);
$mock->shouldReceive('getUuid')
->withArgs(function ($uri) {
$this->assertEquals($uri, 'https://test/dav/uuid2');
return true;
})
->andReturn('uuid2');
$mock->shouldReceive('updateCard')
->withArgs(function ($addressBookId, $cardUri, $cardData) use ($card) {
$this->assertEquals($card, $cardData);
return true;
})
->andReturn($etag);
});
$client = (new DavTester())->fake()->client();
$batchs = (new AddressBookContactsUpdaterMissed())
->execute(new SyncDto($subscription, $client), collect([
[
'uuid' => 'uuid1',
],
]), collect([
'https://test/dav/uuid2' => new ContactDto('https://test/dav/uuid2', $etag),
]));
$this->assertCount(2, $batchs);
$batch = $batchs->first();
$this->assertInstanceOf(GetMultipleVCard::class, $batch);
$hrefs = $this->getPrivateValue($batch, 'hrefs');
$this->assertEquals(['https://test/dav/uuid2'], $hrefs);
}
}

View File

@@ -0,0 +1,208 @@
<?php
namespace Tests\Unit\Services\DavClient\Utils;
use Tests\TestCase;
use App\Jobs\Dav\GetVCard;
use Mockery\MockInterface;
use Tests\Api\DAV\CardEtag;
use Tests\Helpers\DavTester;
use App\Jobs\Dav\DeleteVCard;
use App\Models\User\SyncToken;
use App\Models\Contact\Contact;
use App\Jobs\Dav\GetMultipleVCard;
use App\Jobs\Dav\DeleteMultipleVCard;
use App\Models\Account\AddressBookSubscription;
use App\Services\DavClient\Utils\Model\SyncDto;
use App\Services\DavClient\Utils\Model\ContactDto;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\DavClient\Utils\Model\ContactDeleteDto;
use App\Http\Controllers\DAV\Backend\CardDAV\CardDAVBackend;
use App\Services\DavClient\Utils\AddressBookContactsUpdater;
class AddressBookContactsUpdaterTest extends TestCase
{
use DatabaseTransactions;
use CardEtag;
/** @test */
public function it_sync_changes_multiget()
{
$subscription = AddressBookSubscription::factory()->create();
$token = factory(SyncToken::class)->create([
'account_id' => $subscription->account_id,
'user_id' => $subscription->user_id,
'name' => 'contacts1',
'timestamp' => now()->addDays(-1),
]);
$subscription->localSyncToken = $token->id;
$subscription->save();
$contact = new Contact();
$contact->forceFill([
'first_name' => 'Test',
'uuid' => 'affacde9-b2fe-4371-9acb-6612aaee6971',
'updated_at' => now(),
]);
$card = $this->getCard($contact);
$etag = $this->getEtag($contact, true);
$this->mock(CardDAVBackend::class, function (MockInterface $mock) use ($card, $etag) {
$mock->shouldReceive('updateCard')
->withArgs(function ($addressBookId, $cardUri, $cardData) use ($card) {
$this->assertEquals($card, $cardData);
return true;
})
->andReturn($etag);
});
$client = (new DavTester())->fake()->client();
$batchs = (new AddressBookContactsUpdater())
->execute(new SyncDto($subscription, $client), collect([
'https://test/dav/uuid2' => new ContactDto('https://test/dav/uuid2', $etag),
]));
$this->assertCount(2, $batchs);
$batch = $batchs->first();
$this->assertInstanceOf(GetMultipleVCard::class, $batch);
$hrefs = $this->getPrivateValue($batch, 'hrefs');
$this->assertEquals(['https://test/dav/uuid2'], $hrefs);
}
/** @test */
public function it_sync_deleted_multiget()
{
$subscription = AddressBookSubscription::factory()->create();
$token = factory(SyncToken::class)->create([
'account_id' => $subscription->account_id,
'user_id' => $subscription->user_id,
'name' => 'contacts1',
'timestamp' => now()->addDays(-1),
]);
$subscription->localSyncToken = $token->id;
$subscription->save();
$client = (new DavTester())->fake()->client();
$batchs = (new AddressBookContactsUpdater())
->execute(new SyncDto($subscription, $client), collect([
'https://test/dav/uuid2' => new ContactDeleteDto('https://test/dav/uuid2'),
]));
$this->assertCount(2, $batchs);
$batch = $batchs->first();
$this->assertInstanceOf(GetMultipleVCard::class, $batch);
$hrefs = $this->getPrivateValue($batch, 'hrefs');
$this->assertEquals([], $hrefs);
$batch = $batchs[1];
$this->assertInstanceOf(DeleteMultipleVCard::class, $batch);
$hrefs = $this->getPrivateValue($batch, 'hrefs');
$this->assertEquals(['https://test/dav/uuid2'], $hrefs);
}
/** @test */
public function it_sync_changes_simple()
{
$subscription = AddressBookSubscription::factory()->create([
'capabilities' => [
'addressbookMultiget' => false,
'addressbookQuery' => true,
'syncCollection' => true,
'addressData' => [
'content-type' => 'text/vcard',
'version' => '4.0',
],
],
]);
$token = factory(SyncToken::class)->create([
'account_id' => $subscription->account_id,
'user_id' => $subscription->user_id,
'name' => 'contacts1',
'timestamp' => now()->addDays(-1),
]);
$subscription->localSyncToken = $token->id;
$subscription->save();
$contact = new Contact();
$contact->forceFill([
'first_name' => 'Test',
'uuid' => 'affacde9-b2fe-4371-9acb-6612aaee6971',
'updated_at' => now(),
]);
$card = $this->getCard($contact);
$etag = $this->getEtag($contact, true);
$this->mock(CardDAVBackend::class, function (MockInterface $mock) use ($card, $etag) {
$mock->shouldReceive('updateCard')
->withArgs(function ($addressBookId, $cardUri, $cardData) use ($card) {
$this->assertTrue(is_resource($cardData));
$data = '';
while (! feof($cardData)) {
$data .= fgets($cardData);
}
fclose($cardData);
$this->assertEquals($card, $data);
return true;
})
->andReturn($etag);
});
$client = (new DavTester())->fake()->client();
$batchs = (new AddressBookContactsUpdater())
->execute(new SyncDto($subscription, $client), collect([
'https://test/dav/uuid2' => new ContactDto('https://test/dav/uuid2', $etag),
]));
$this->assertCount(1, $batchs);
$batch = $batchs->first();
$this->assertInstanceOf(GetVCard::class, $batch);
$dto = $this->getPrivateValue($batch, 'contact');
$this->assertInstanceOf(ContactDto::class, $dto);
$this->assertEquals('https://test/dav/uuid2', $dto->uri);
}
/** @test */
public function it_sync_deleted_simple()
{
$subscription = AddressBookSubscription::factory()->create([
'capabilities' => [
'addressbookMultiget' => false,
'addressbookQuery' => true,
'syncCollection' => true,
'addressData' => [
'content-type' => 'text/vcard',
'version' => '4.0',
],
],
]);
$token = factory(SyncToken::class)->create([
'account_id' => $subscription->account_id,
'user_id' => $subscription->user_id,
'name' => 'contacts1',
'timestamp' => now()->addDays(-1),
]);
$subscription->localSyncToken = $token->id;
$subscription->save();
$client = (new DavTester())->fake()->client();
$batchs = (new AddressBookContactsUpdater())
->execute(new SyncDto($subscription, $client), collect([
'https://test/dav/uuid2' => new ContactDeleteDto('https://test/dav/uuid2'),
]));
$this->assertCount(1, $batchs);
$batch = $batchs->first();
$this->assertInstanceOf(DeleteVCard::class, $batch);
$uri = $this->getPrivateValue($batch, 'uri');
$this->assertEquals('https://test/dav/uuid2', $uri);
}
}

View File

@@ -0,0 +1,110 @@
<?php
namespace Tests\Unit\Services\DavClient\Utils;
use Tests\TestCase;
use Tests\Helpers\DavTester;
use App\Services\DavClient\Utils\AddressBookGetter;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\DavClient\Utils\Dav\DavClientException;
use App\Services\DavClient\Utils\Dav\DavServerNotCompliantException;
class AddressBookGetterTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_get_address_book_data()
{
$tester = (new DavTester())
->addressBookBaseUri()
->capabilities()
->displayName()
->fake();
$client = $tester->client();
$result = (new AddressBookGetter())
->execute($client);
$tester->assert();
$this->assertEquals([
'uri' => 'https://test/dav/addressbooks/user@test.com/contacts/',
'capabilities' => [
'addressbookMultiget' => true,
'addressbookQuery' => true,
'syncCollection' => true,
'addressData' => [
'content-type' => 'text/vcard',
'version' => '4.0',
],
],
'name' => 'Test',
], $result);
}
/** @test */
public function it_fails_on_server_not_compliant()
{
$tester = (new DavTester())
->userPrincipalEmpty()
->serviceUrl()
->optionsFail()
->fake();
$client = $tester->client();
$this->expectException(DavServerNotCompliantException::class);
(new AddressBookGetter())
->execute($client);
}
/** @test */
public function it_fails_if_no_userprincipal()
{
$tester = (new DavTester())
->userPrincipalEmpty()
->serviceUrl()
->optionsOk()
->userPrincipalEmpty()
->fake();
$client = $tester->client();
$this->expectException(DavServerNotCompliantException::class);
(new AddressBookGetter())
->execute($client);
}
/** @test */
public function it_fails_if_no_addressbook()
{
$tester = (new DavTester())
->userPrincipalEmpty()
->serviceUrl()
->optionsOk()
->userPrincipal()
->addressbookEmpty()
->fake();
$client = $tester->client();
$this->expectException(DavServerNotCompliantException::class);
(new AddressBookGetter())
->execute($client);
}
/** @test */
public function it_fails_if_no_addressbook_url()
{
$tester = (new DavTester())
->userPrincipalEmpty()
->serviceUrl()
->optionsOk()
->userPrincipal()
->addressbookHome()
->resourceTypeHomeOnly()
->optionsOk()
->fake();
$client = $tester->client();
$this->expectException(DavClientException::class);
(new AddressBookGetter())
->execute($client);
}
}

View File

@@ -0,0 +1,370 @@
<?php
namespace Tests\Unit\Services\DavClient\Utils;
use Tests\TestCase;
use Mockery\MockInterface;
use Tests\Api\DAV\CardEtag;
use Tests\Helpers\DavTester;
use App\Models\User\SyncToken;
use App\Models\Contact\Contact;
use Illuminate\Bus\PendingBatch;
use App\Jobs\Dav\GetMultipleVCard;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Http;
use App\Jobs\Dav\DeleteMultipleVCard;
use App\Models\Account\AddressBookSubscription;
use App\Services\DavClient\Utils\Model\SyncDto;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\DavClient\Utils\AddressBookSynchronizer;
use App\Services\DavClient\Utils\AddressBookContactsUpdater;
use App\Services\DavClient\Utils\AddressBookContactsPushMissed;
use App\Services\DavClient\Utils\AddressBookContactsUpdaterMissed;
class AddressBookSynchronizerTest extends TestCase
{
use DatabaseTransactions;
use CardEtag;
/** @test */
public function it_sync_empty_changes()
{
Bus::fake();
$this->mock(AddressBookContactsUpdater::class, function (MockInterface $mock) {
$mock->shouldReceive('execute')
->once()
->andReturn(collect());
});
$subscription = $this->getSubscription();
$tester = (new DavTester('https://test/dav/addressbooks/user@test.com/contacts/'))
->getSynctoken($subscription->syncToken)
->fake();
$client = $tester->client();
(new AddressBookSynchronizer())
->execute(new SyncDto($subscription, $client));
$tester->assert();
}
/** @test */
public function it_sync_no_changes()
{
Bus::fake();
$this->mock(AddressBookContactsUpdater::class, function (MockInterface $mock) {
$mock->shouldReceive('execute')
->once()
->andReturn(collect());
});
$subscription = $this->getSubscription();
$tester = (new DavTester('https://test/dav/addressbooks/user@test.com/contacts/'));
$tester->getSynctoken('"test21"')
->getSyncCollection('test20')
->fake();
$client = $tester->client();
(new AddressBookSynchronizer())
->execute(new SyncDto($subscription, $client));
$tester->assert();
}
/** @test */
public function it_sync_changes_added_local_contact()
{
Bus::fake();
$subscription = $this->getSubscription();
$contact = factory(Contact::class)->create([
'account_id' => $subscription->account_id,
'address_book_id' => $subscription->address_book_id,
'uuid' => 'd403af1c-8492-4e9b-9833-cf18c795dfa9',
]);
$tester = (new DavTester('https://test/dav/addressbooks/user@test.com/contacts/'));
$tester->getSynctoken('"token"')
->getSyncCollection('token', '"test2"')
->fake();
$client = $tester->client();
$sync = new SyncDto($subscription, $client);
$this->mock(AddressBookContactsUpdater::class, function (MockInterface $mock) use ($sync) {
$mock->shouldReceive('execute')
->once()
->withArgs(function ($localSync, $contacts) use ($sync) {
$this->assertEquals($sync, $localSync);
$this->assertEquals('https://test/dav/addressbooks/user@test.com/contacts/uuid', $contacts->first()->uri);
$this->assertEquals('"test2"', $contacts->first()->etag);
return true;
})
->andReturn(collect());
});
(new AddressBookSynchronizer())
->execute($sync);
$tester->assert();
}
/** @test */
public function it_sync_changes_added_local_contact_batched()
{
Bus::fake();
$subscription = $this->getSubscription();
factory(Contact::class)->create([
'account_id' => $subscription->account_id,
'address_book_id' => $subscription->address_book_id,
'uuid' => 'd403af1c-8492-4e9b-9833-cf18c795dfa9',
]);
$tester = (new DavTester('https://test/dav/addressbooks/user@test.com/contacts/'));
$tester->getSynctoken('"token"')
->getSyncCollection('token', '"test2"')
->fake();
$client = $tester->client();
$sync = new SyncDto($subscription, $client);
(new AddressBookSynchronizer())
->execute($sync);
$tester->assert();
Bus::assertBatched(function (PendingBatch $batch) {
$this->assertCount(2, $batch->jobs);
$job = $batch->jobs[0];
$this->assertInstanceOf(GetMultipleVCard::class, $job);
$this->assertEquals(['https://test/dav/addressbooks/user@test.com/contacts/uuid'], $this->getPrivateValue($job, 'hrefs'));
return true;
});
}
/** @test */
public function it_sync_changes_deleted_contact_batched()
{
Bus::fake();
$subscription = $this->getSubscription();
$tester = (new DavTester('https://test/dav/addressbooks/user@test.com/contacts/'));
$tester->getSynctoken('"token"')
->addResponse('https://test/dav/addressbooks/user@test.com/contacts/', Http::response(DavTester::multistatusHeader().
'<d:response>'.
'<d:status>HTTP/1.1 404 Not Found</d:status>'.
'<d:href>https://test/dav/addressbooks/user@test.com/contacts/uuid</d:href>'.
'<d:propstat>'.
'<d:prop/>'.
'<d:status>HTTP/1.1 418 I\'m a teapot</d:status>'.
'</d:propstat>'.
'</d:response>'.
'<d:sync-token>token</d:sync-token>'.
'</d:multistatus>'), null, 'REPORT')
->fake();
$client = $tester->client();
$sync = new SyncDto($subscription, $client);
(new AddressBookSynchronizer())
->execute($sync);
$tester->assert();
Bus::assertBatched(function (PendingBatch $batch) {
$this->assertCount(2, $batch->jobs);
$job = $batch->jobs[1];
$this->assertInstanceOf(DeleteMultipleVCard::class, $job);
$this->assertEquals(['https://test/dav/addressbooks/user@test.com/contacts/uuid'], $this->getPrivateValue($job, 'hrefs'));
return true;
});
}
/** @test */
public function it_forcesync_changes_added_local_contact()
{
Bus::fake();
$subscription = $this->getSubscription();
$contact = factory(Contact::class)->create([
'account_id' => $subscription->account_id,
'address_book_id' => $subscription->address_book_id,
'uuid' => 'd403af1c-8492-4e9b-9833-cf18c795dfa9',
]);
$etag = $this->getEtag($contact, true);
$tester = (new DavTester('https://test/dav/addressbooks/user@test.com/contacts/'))
->fake();
$tester->addResponse('https://test/dav/addressbooks/user@test.com/contacts/', Http::response(DavTester::multistatusHeader().
'<d:response>'.
'<d:href>https://test/dav/uuid1</d:href>'.
'<d:propstat>'.
'<d:prop>'.
"<d:getetag>$etag</d:getetag>".
'</d:prop>'.
'<d:status>HTTP/1.1 200 OK</d:status>'.
'</d:propstat>'.
'</d:response>'.
'</d:multistatus>'), '<?xml version="1.0" encoding="UTF-8"?>'."\n".
'<card:addressbook-query xmlns:card="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">'.
'<d:prop>'.
'<d:getetag/>'.
'</d:prop>'.
"</card:addressbook-query>\n", 'REPORT');
$client = $tester->client();
$sync = new SyncDto($subscription, $client);
$this->mock(AddressBookContactsUpdaterMissed::class, function (MockInterface $mock) use ($sync, $contact, $etag) {
$mock->shouldReceive('execute')
->once()
->withArgs(function ($localSync, $localContacts, $distContacts) use ($sync, $contact, $etag) {
$this->assertEquals($sync, $localSync);
$this->assertEquals($contact->id, $localContacts->first()->id);
$this->assertEquals('https://test/dav/uuid1', $distContacts->first()->uri);
$this->assertEquals($etag, $distContacts->first()->etag);
return true;
})
->andReturn(collect());
});
$this->mock(AddressBookContactsPushMissed::class, function (MockInterface $mock) {
$mock->shouldReceive('execute')
->once()
->andReturn(collect());
});
(new AddressBookSynchronizer())
->execute($sync, true);
$tester->assert();
}
/** @test */
public function it_forcesync_changes_added_local_contact_batched()
{
Bus::fake();
$subscription = $this->getSubscription();
$contact = factory(Contact::class)->create([
'account_id' => $subscription->account_id,
'address_book_id' => $subscription->address_book_id,
'uuid' => 'd403af1c-8492-4e9b-9833-cf18c795dfa9',
]);
$etag = $this->getEtag($contact, true);
$tester = (new DavTester('https://test/dav/addressbooks/user@test.com/contacts/'))
->fake();
$tester->addResponse('https://test/dav/addressbooks/user@test.com/contacts/', Http::response(DavTester::multistatusHeader().
'<d:response>'.
'<d:href>https://test/dav/uuid1</d:href>'.
'<d:propstat>'.
'<d:prop>'.
"<d:getetag>$etag</d:getetag>".
'</d:prop>'.
'<d:status>HTTP/1.1 200 OK</d:status>'.
'</d:propstat>'.
'</d:response>'.
'</d:multistatus>'), '<?xml version="1.0" encoding="UTF-8"?>'."\n".
'<card:addressbook-query xmlns:card="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">'.
'<d:prop>'.
'<d:getetag/>'.
'</d:prop>'.
"</card:addressbook-query>\n", 'REPORT');
$client = $tester->client();
$sync = new SyncDto($subscription, $client);
(new AddressBookSynchronizer())
->execute($sync, true);
$tester->assert();
Bus::assertBatched(function (PendingBatch $batch) {
$this->assertCount(2, $batch->jobs);
$job = $batch->jobs[0];
$this->assertInstanceOf(GetMultipleVCard::class, $job);
$this->assertEquals(['https://test/dav/uuid1'], $this->getPrivateValue($job, 'hrefs'));
return true;
});
}
/** @test */
public function it_forcesync_changes_deleted_contact_batched()
{
Bus::fake();
$subscription = $this->getSubscription();
$tester = (new DavTester('https://test/dav/addressbooks/user@test.com/contacts/'))
->fake();
$tester->addResponse('https://test/dav/addressbooks/user@test.com/contacts/', Http::response(DavTester::multistatusHeader().
'<d:response>'.
'<d:status>HTTP/1.1 404 Not Found</d:status>'.
'<d:href>https://test/dav/uuid1</d:href>'.
'<d:propstat>'.
'<d:prop/>'.
'<d:status>HTTP/1.1 418 I\'m a teapot</d:status>'.
'</d:propstat>'.
'</d:response>'.
'</d:multistatus>'), '<?xml version="1.0" encoding="UTF-8"?>'."\n".
'<card:addressbook-query xmlns:card="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">'.
'<d:prop>'.
'<d:getetag/>'.
'</d:prop>'.
"</card:addressbook-query>\n", 'REPORT');
$client = $tester->client();
$sync = new SyncDto($subscription, $client);
(new AddressBookSynchronizer())
->execute($sync, true);
$tester->assert();
Bus::assertBatched(function (PendingBatch $batch) {
$this->assertCount(2, $batch->jobs);
$job = $batch->jobs[1];
$this->assertInstanceOf(DeleteMultipleVCard::class, $job);
$this->assertEquals(['https://test/dav/uuid1'], $this->getPrivateValue($job, 'hrefs'));
return true;
});
}
private function getSubscription()
{
$subscription = AddressBookSubscription::factory()->create([
'uri' => 'https://test/dav/addressbooks/user@test.com/contacts/',
]);
$token = factory(SyncToken::class)->create([
'account_id' => $subscription->account_id,
'user_id' => $subscription->user_id,
'name' => 'contacts1',
'timestamp' => now()->addDays(-1),
]);
$subscription->localSyncToken = $token->id;
$subscription->save();
return $subscription;
}
}

View File

@@ -0,0 +1,470 @@
<?php
namespace Tests\Unit\Services\DavClient\Utils\Dav;
use Tests\TestCase;
use Tests\Helpers\DavTester;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Client\RequestException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\DavClient\Utils\Dav\DavClientException;
class DavClientTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_get_options()
{
$tester = (new DavTester())
->addResponse('https://test', Http::response(), null, 'OPTIONS')
->addResponse('https://test', Http::response(null, 200, ['Dav' => 'test']), null, 'OPTIONS')
->addResponse('https://test', Http::response(null, 200, ['Dav' => ' test ']), null, 'OPTIONS')
->fake();
$client = $tester->client();
$result = $client->options();
$this->assertEquals([], $result);
$result = $client->options();
$this->assertEquals(['test'], $result);
$result = $client->options();
$this->assertEquals(['test'], $result);
$tester->assert();
}
/** @test */
public function it_get_serviceurl()
{
$tester = (new DavTester())
->serviceUrl()
->fake();
$client = $tester->client();
$result = $client->getServiceUrl();
$tester->assert();
$this->assertEquals('https://test/dav/', $result);
}
/** @test */
public function it_get_non_standard_serviceurl()
{
$tester = (new DavTester())
->addResponse('https://test/.well-known/carddav', Http::response(), null, 'GET')
->addResponse('https://test/.well-known/carddav', Http::response(), null, 'GET')
->nonStandardServiceUrl()
->fake();
$client = $tester->client();
$result = $client->getServiceUrl();
$tester->assert();
$this->assertEquals('https://test/dav/', $result);
}
/** @test */
public function it_get_non_standard_serviceurl2()
{
$tester = (new DavTester())
->addResponse('https://test/.well-known/carddav', Http::response(null, 404), null, 'GET')
->addResponse('https://test/.well-known/carddav', Http::response(null, 404), null, 'GET')
->nonStandardServiceUrl()
->fake();
$client = $tester->client();
$result = $client->getServiceUrl();
$tester->assert();
$this->assertEquals('https://test/dav/', $result);
}
/** @test */
public function it_fail_non_standard()
{
$tester = (new DavTester())
->addResponse('https://test/.well-known/carddav', Http::response(null, 500), null, 'GET')
->fake();
$client = $tester->client();
$this->expectException(RequestException::class);
$client->getServiceUrl();
}
/** @test */
public function it_get_base_uri()
{
$tester = (new DavTester())
->fake();
$client = $tester->client();
$result = $client->path();
$this->assertEquals('https://test', $result);
$result = $client->path('xxx');
$this->assertEquals('https://test/xxx', $result);
}
/** @test */
public function it_set_base_uri()
{
$tester = (new DavTester())
->fake();
$client = $tester->client();
$result = $client->setBaseUri('https://new')
->path();
$this->assertEquals('https://new', $result);
}
/** @test */
public function it_call_propfind()
{
$tester = (new DavTester())
->addResponse('https://test', Http::response(DavTester::multistatusHeader().
'<d:response>'.
'<d:href>href</d:href>'.
'<d:propstat>'.
'<d:prop>'.
'<d:test>value</d:test>'.
'</d:prop>'.
'<d:status>HTTP/1.1 200 OK</d:status>'.
'</d:propstat>'.
'</d:response>'.
'</d:multistatus>'), '<?xml version="1.0" encoding="UTF-8"?>'."\n".
'<d:propfind xmlns:d="DAV:">'.
'<d:prop>'.
'<d:test/>'.
'</d:prop>'.
"</d:propfind>\n", 'PROPFIND')
->fake();
$client = $tester->client();
$result = $client->propFind(['{DAV:}test']);
$tester->assert();
$this->assertEquals([
'{DAV:}test' => 'value',
], $result);
}
/** @test */
public function it_get_property()
{
$tester = (new DavTester())
->addResponse('https://test/test', Http::response(DavTester::multistatusHeader().
'<d:response>'.
'<d:href>href</d:href>'.
'<d:propstat>'.
'<d:prop>'.
'<d:test>value</d:test>'.
'</d:prop>'.
'<d:status>HTTP/1.1 200 OK</d:status>'.
'</d:propstat>'.
'</d:response>'.
'</d:multistatus>'), '<?xml version="1.0" encoding="UTF-8"?>'."\n".
'<d:propfind xmlns:d="DAV:">'.
'<d:prop>'.
'<d:test/>'.
'</d:prop>'.
"</d:propfind>\n", 'PROPFIND')
->fake();
$client = $tester->client();
$result = $client->getProperty('{DAV:}test', 'https://test/test');
$tester->assert();
$this->assertEquals('value', $result);
}
/** @test */
public function it_get_supported_report()
{
$tester = (new DavTester('https://test/dav'))
->addResponse('https://test/dav', Http::response(DavTester::multistatusHeader().
'<d:response>'.
'<d:href>/dav</d:href>'.
'<d:propstat>'.
'<d:prop>'.
'<d:supported-report-set>'.
'<d:supported-report>'.
'<d:report><d:test1/></d:report>'.
'</d:supported-report>'.
'<d:supported-report>'.
'<d:report><d:test2/></d:report>'.
'</d:supported-report>'.
'</d:supported-report-set>'.
'</d:prop>'.
'<d:status>HTTP/1.1 200 OK</d:status>'.
'</d:propstat>'.
'</d:response>'.
'</d:multistatus>'), '<?xml version="1.0" encoding="UTF-8"?>'."\n".
'<d:propfind xmlns:d="DAV:">'.
'<d:prop>'.
'<d:supported-report-set/>'.
'</d:prop>'.
"</d:propfind>\n", 'PROPFIND')
->fake();
$client = $tester->client();
$result = $client->getSupportedReportSet();
$tester->assert();
$this->assertEquals(['{DAV:}test1', '{DAV:}test2'], $result);
}
/** @test */
public function it_sync_collection()
{
$tester = (new DavTester())
->addResponse('https://test', Http::response(DavTester::multistatusHeader().
'<d:response>'.
'<d:href>href</d:href>'.
'<d:propstat>'.
'<d:prop>'.
'<d:getetag>"00001-abcd1"</d:getetag>'.
'<d:test>value</d:test>'.
'</d:prop>'.
'<d:status>HTTP/1.1 200 OK</d:status>'.
'</d:propstat>'.
'</d:response>'.
'<d:sync-token>"00001-abcd1"</d:sync-token>'.
'</d:multistatus>'), '<?xml version="1.0" encoding="UTF-8"?>'."\n".
'<d:sync-collection xmlns:d="DAV:">'.
'<d:sync-token/>'.
'<d:sync-level>1</d:sync-level>'.
'<d:prop>'.
'<d:test/>'.
'</d:prop>'.
"</d:sync-collection>\n", 'REPORT')
->fake();
$client = $tester->client();
$result = $client->syncCollection(['{DAV:}test'], '');
$tester->assert();
$this->assertEquals([
'href' => [
'properties' => [
200 => [
'{DAV:}getetag' => '"00001-abcd1"',
'{DAV:}test' => 'value',
],
],
'status' => '200',
],
'synctoken' => '"00001-abcd1"',
], $result);
}
/** @test */
public function it_sync_collection_with_synctoken()
{
$tester = (new DavTester())
->addResponse('https://test', Http::response(DavTester::multistatusHeader().
'<d:response>'.
'<d:href>href</d:href>'.
'<d:propstat>'.
'<d:prop>'.
'<d:getetag>"00001-abcd1"</d:getetag>'.
'<d:test>value</d:test>'.
'</d:prop>'.
'<d:status>HTTP/1.1 200 OK</d:status>'.
'</d:propstat>'.
'</d:response>'.
'<d:sync-token>"00001-abcd1"</d:sync-token>'.
'</d:multistatus>'), '<?xml version="1.0" encoding="UTF-8"?>'."\n".
'<d:sync-collection xmlns:d="DAV:">'.
'<d:sync-token>"00000-abcd0"</d:sync-token>'.
'<d:sync-level>1</d:sync-level>'.
'<d:prop>'.
'<d:test/>'.
'</d:prop>'.
"</d:sync-collection>\n", 'REPORT')
->fake();
$client = $tester->client();
$result = $client->syncCollection(['{DAV:}test'], '"00000-abcd0"');
$tester->assert();
$this->assertEquals([
'href' => [
'properties' => [
200 => [
'{DAV:}getetag' => '"00001-abcd1"',
'{DAV:}test' => 'value',
],
],
'status' => '200',
],
'synctoken' => '"00001-abcd1"',
], $result);
}
/** @test */
public function it_run_addressbook_multiget_report()
{
$tester = (new DavTester())
->addResponse('https://test', Http::response(DavTester::multistatusHeader().
'<d:response>'.
'<d:href>href</d:href>'.
'<d:propstat>'.
'<d:prop>'.
'<d:getetag>"00001-abcd1"</d:getetag>'.
'<d:test>value</d:test>'.
'</d:prop>'.
'<d:status>HTTP/1.1 200 OK</d:status>'.
'</d:propstat>'.
'</d:response>'.
'</d:multistatus>'), '<?xml version="1.0" encoding="UTF-8"?>'."\n".
'<card:addressbook-multiget xmlns:card="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">'.
'<d:prop>'.
'<d:test/>'.
'</d:prop>'.
'<d:href>https://test/contacts/1</d:href>'.
"</card:addressbook-multiget>\n", 'REPORT')
->fake();
$client = $tester->client();
$result = $client->addressbookMultiget(['{DAV:}test'], ['https://test/contacts/1']);
$this->assertEquals([
'href' => [
'properties' => [
200 => [
'{DAV:}getetag' => '"00001-abcd1"',
'{DAV:}test' => 'value',
],
],
'status' => '200',
],
], $result);
$tester->assert();
}
/** @test */
public function it_run_addressbook_query_report()
{
$tester = (new DavTester())
->addResponse('https://test', Http::response(DavTester::multistatusHeader().
'<d:response>'.
'<d:href>href</d:href>'.
'<d:propstat>'.
'<d:prop>'.
'<d:getetag>"00001-abcd1"</d:getetag>'.
'<d:test>value</d:test>'.
'</d:prop>'.
'<d:status>HTTP/1.1 200 OK</d:status>'.
'</d:propstat>'.
'</d:response>'.
'</d:multistatus>'), '<?xml version="1.0" encoding="UTF-8"?>'."\n".
'<card:addressbook-query xmlns:card="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">'.
'<d:prop>'.
'<d:test/>'.
'</d:prop>'.
"</card:addressbook-query>\n", 'REPORT')
->fake();
$client = $tester->client();
$result = $client->addressbookQuery(['{DAV:}test']);
$tester->assert();
$this->assertEquals([
'href' => [
'properties' => [
200 => [
'{DAV:}getetag' => '"00001-abcd1"',
'{DAV:}test' => 'value',
],
],
'status' => '200',
],
], $result);
}
/** @test */
public function it_run_proppatch()
{
$tester = (new DavTester())
->addResponse('https://test', Http::response(DavTester::multistatusHeader().
'<d:response>'.
'<d:href>href</d:href>'.
'<d:propstat>'.
'<d:prop>'.
'<d:test>value</d:test>'.
'</d:prop>'.
'<d:status>HTTP/1.1 200 OK</d:status>'.
'</d:propstat>'.
'</d:response>'.
'</d:multistatus>', 207), '<?xml version="1.0"?>'."\n".
'<d:propertyupdate xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">'."\n".
' <d:set>'."\n".
' <d:prop>'."\n".
' <d:test>value</d:test>'."\n".
' </d:prop>'."\n".
' </d:set>'."\n".
"</d:propertyupdate>\n", 'PROPPATCH')
->fake();
$client = $tester->client();
$result = $client->propPatch(['{DAV:}test' => 'value']);
$tester->assert();
$this->assertTrue($result);
}
/** @test */
public function it_run_proppatch_error()
{
$tester = (new DavTester())
->addResponse('https://test', Http::response(DavTester::multistatusHeader().
'<d:response>'.
'<d:href>href</d:href>'.
'<d:propstat>'.
'<d:prop>'.
'<d:test>x</d:test>'.
'</d:prop>'.
'<d:status>HTTP/1.1 405 OK</d:status>'.
'</d:propstat>'.
'<d:propstat>'.
'<d:prop>'.
'<d:excerpt>x</d:excerpt>'.
'</d:prop>'.
'<d:status>HTTP/1.1 500 OK</d:status>'.
'</d:propstat>'.
'</d:response>'.
'</d:multistatus>', 207), '<?xml version="1.0"?>'."\n".
'<d:propertyupdate xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">'."\n".
' <d:set>'."\n".
' <d:prop>'."\n".
' <d:test>value</d:test>'."\n".
' <d:excerpt>value</d:excerpt>'."\n".
' </d:prop>'."\n".
' </d:set>'."\n".
"</d:propertyupdate>\n", 'PROPPATCH')
->fake();
$client = $tester->client();
$this->expectException(DavClientException::class);
$this->expectExceptionMessage('PROPPATCH failed. The following properties errored: {DAV:}test (405), {DAV:}excerpt (500)');
$client->propPatch([
'{DAV:}test' => 'value',
'{DAV:}excerpt' => 'value',
]);
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Tests\Unit\Services\DavClient\Utils\Model;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\DavClient\Utils\Model\ContactUpdateDto;
class ContactUpdateDtoTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_create_dto_string()
{
$dto = new ContactUpdateDto('uri', 'etag', 'card');
$this->assertEquals('uri', $dto->uri);
$this->assertEquals('etag', $dto->etag);
$this->assertEquals('card', $dto->card);
}
/** @test */
public function it_create_dto_resource()
{
$resource = fopen(__DIR__.'/stub.vcf', 'r');
$dto = new ContactUpdateDto('uri', 'etag', $resource);
$this->assertEquals('uri', $dto->uri);
$this->assertEquals('etag', $dto->etag);
$this->assertEquals('card', $dto->card);
}
}

View File

@@ -0,0 +1 @@
card