-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
165 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -289,7 +289,7 @@ public function getSynctoken(string $synctoken = '"test"') | |
'</d:multistatus>')); | ||
} | ||
|
||
public function getSyncCollection(string $synctoken = 'token', string $etag = '"etag"') | ||
public function getSyncCollection(string $synctoken = '"token"', string $etag = '"etag"') | ||
{ | ||
return $this->addResponse('https://test/dav/addressbooks/[email protected]/contacts/', Http::response($this->multistatusHeader(). | ||
'<d:response>'. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,50 +18,16 @@ | |
use Illuminate\Support\Facades\Bus; | ||
use Illuminate\Support\Facades\Http; | ||
use Mockery\MockInterface; | ||
use PHPUnit\Framework\Attributes\RunClassInSeparateProcess; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Tests\Helpers\DavTester; | ||
use Tests\TestCase; | ||
use Tests\Unit\Domains\Contact\DAV\CardEtag; | ||
|
||
#[RunClassInSeparateProcess] | ||
class AddressBookSynchronizerTest extends TestCase | ||
{ | ||
use DatabaseTransactions; | ||
use CardEtag; | ||
|
||
#[Test] | ||
public function it_sync_no_changes() | ||
{ | ||
Bus::fake(); | ||
|
||
$this->mock(PrepareJobsContactUpdater::class, function (MockInterface $mock) { | ||
$mock->shouldReceive('withSubscription')->once()->andReturn($mock); | ||
$mock->shouldReceive('execute') | ||
->once() | ||
->andReturn(collect()); | ||
}); | ||
$this->partialMock(PrepareJobsContactPush::class, function (MockInterface $mock) { | ||
$mock->shouldReceive('withSubscription')->once()->andReturn($mock); | ||
$mock->shouldReceive('execute') | ||
->once() | ||
->andReturn(collect()); | ||
}); | ||
|
||
$subscription = $this->getSubscription(); | ||
|
||
$tester = (new DavTester($subscription->uri)) | ||
->getSynctoken('"test21"') | ||
->getSyncCollection('test20') | ||
->fake(); | ||
|
||
(new AddressBookSynchronizer) | ||
->withSubscription($subscription) | ||
->execute(); | ||
|
||
$tester->assert(); | ||
} | ||
|
||
#[Test] | ||
public function it_sync_empty_changes() | ||
{ | ||
|
@@ -93,87 +59,6 @@ public function it_sync_empty_changes() | |
$tester->assert(); | ||
} | ||
|
||
#[Test] | ||
public function it_sync_changes_added_local_contact() | ||
{ | ||
Bus::fake(); | ||
|
||
$subscription = $this->getSubscription(); | ||
|
||
Contact::factory()->create([ | ||
'vault_id' => $subscription->vault_id, | ||
'id' => 'd403af1c-8492-4e9b-9833-cf18c795dfa9', | ||
]); | ||
|
||
$tester = (new DavTester($subscription->uri)) | ||
->getSynctoken('"token"') | ||
->getSyncCollection('token', '"test2"') | ||
->fake(); | ||
|
||
$this->mock(PrepareJobsContactUpdater::class, function (MockInterface $mock) { | ||
$mock->shouldReceive('withSubscription')->once()->andReturn($mock); | ||
$mock->shouldReceive('execute') | ||
->once() | ||
->withArgs(function ($contacts) { | ||
$this->assertEquals('https://test/dav/addressbooks/[email protected]/contacts/uuid', $contacts->first()->uri); | ||
$this->assertEquals('"test2"', $contacts->first()->etag); | ||
|
||
return true; | ||
}) | ||
->andReturn(collect()); | ||
}); | ||
$this->partialMock(PrepareJobsContactPush::class, function (MockInterface $mock) { | ||
$mock->shouldReceive('withSubscription')->once()->andReturn($mock); | ||
$mock->shouldReceive('execute') | ||
->once() | ||
->withArgs(function ($localChanges, $changes) { | ||
$this->assertEquals('"test2"', $changes->first()->etag); | ||
|
||
return true; | ||
}) | ||
->andReturn(collect()); | ||
}); | ||
|
||
(new AddressBookSynchronizer) | ||
->withSubscription($subscription) | ||
->execute(); | ||
|
||
$tester->assert(); | ||
} | ||
|
||
#[Test] | ||
public function it_sync_changes_added_local_contact_batched() | ||
{ | ||
Bus::fake(); | ||
|
||
$subscription = $this->getSubscription(); | ||
|
||
Contact::factory()->create([ | ||
'vault_id' => $subscription->vault_id, | ||
'id' => 'd403af1c-8492-4e9b-9833-cf18c795dfa9', | ||
]); | ||
|
||
$tester = (new DavTester($subscription->uri)) | ||
->getSynctoken('"token"') | ||
->getSyncCollection('token', '"test2"') | ||
->fake(); | ||
|
||
(new AddressBookSynchronizer) | ||
->withSubscription($subscription) | ||
->execute(); | ||
|
||
$tester->assert(); | ||
|
||
Bus::assertBatched(function (PendingBatch $batch) { | ||
$this->assertCount(1, $batch->jobs); | ||
$job = $batch->jobs[0]; | ||
$this->assertInstanceOf(GetMultipleVCard::class, $job); | ||
$this->assertEquals(['https://test/dav/addressbooks/[email protected]/contacts/uuid'], $this->getPrivateValue($job, 'hrefs')); | ||
|
||
return true; | ||
}); | ||
} | ||
|
||
#[Test] | ||
public function it_sync_changes_deleted_contact_batched() | ||
{ | ||
|
164 changes: 164 additions & 0 deletions
164
tests/Unit/Domains/Contact/DavClient/Services/Utils/AddressBookSynchronizerTest2.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Domains\Contact\DavClient\Services\Utils; | ||
|
||
use App\Domains\Contact\DavClient\Jobs\GetMultipleVCard; | ||
use App\Domains\Contact\DavClient\Services\Utils\AddressBookSynchronizer; | ||
use App\Domains\Contact\DavClient\Services\Utils\PrepareJobsContactPush; | ||
use App\Domains\Contact\DavClient\Services\Utils\PrepareJobsContactUpdater; | ||
use App\Models\AddressBookSubscription; | ||
use App\Models\Contact; | ||
use App\Models\SyncToken; | ||
use App\Models\Vault; | ||
use Illuminate\Bus\PendingBatch; | ||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
use Illuminate\Support\Facades\Bus; | ||
use Mockery\MockInterface; | ||
use PHPUnit\Framework\Attributes\RunClassInSeparateProcess; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Tests\Helpers\DavTester; | ||
use Tests\TestCase; | ||
use Tests\Unit\Domains\Contact\DAV\CardEtag; | ||
|
||
#[RunClassInSeparateProcess] | ||
class AddressBookSynchronizerTest2 extends TestCase | ||
{ | ||
use DatabaseTransactions; | ||
use CardEtag; | ||
|
||
#[Test] | ||
public function it_sync_no_changes() | ||
{ | ||
Bus::fake(); | ||
|
||
$this->mock(PrepareJobsContactUpdater::class, function (MockInterface $mock) { | ||
$mock->shouldReceive('withSubscription')->once()->andReturn($mock); | ||
$mock->shouldReceive('execute') | ||
->once() | ||
->andReturn(collect()); | ||
}); | ||
$this->partialMock(PrepareJobsContactPush::class, function (MockInterface $mock) { | ||
$mock->shouldReceive('withSubscription')->once()->andReturn($mock); | ||
$mock->shouldReceive('execute') | ||
->once() | ||
->andReturn(collect()); | ||
}); | ||
|
||
$subscription = $this->getSubscription(); | ||
|
||
$tester = (new DavTester($subscription->uri)) | ||
->getSynctoken('"test21"') | ||
->getSyncCollection('"test20"') | ||
->fake(); | ||
|
||
(new AddressBookSynchronizer) | ||
->withSubscription($subscription) | ||
->execute(); | ||
|
||
$tester->assert(); | ||
} | ||
|
||
#[Test] | ||
public function it_sync_changes_added_local_contact() | ||
{ | ||
Bus::fake(); | ||
|
||
$subscription = $this->getSubscription(); | ||
|
||
Contact::factory()->create([ | ||
'vault_id' => $subscription->vault_id, | ||
'id' => 'd403af1c-8492-4e9b-9833-cf18c795dfa9', | ||
]); | ||
|
||
$tester = (new DavTester($subscription->uri)) | ||
->getSynctoken('"token"') | ||
->getSyncCollection('"token"', 'test2') | ||
->fake(); | ||
|
||
$this->mock(PrepareJobsContactUpdater::class, function (MockInterface $mock) { | ||
$mock->shouldReceive('withSubscription')->once()->andReturn($mock); | ||
$mock->shouldReceive('execute') | ||
->once() | ||
->withArgs(function ($contacts) { | ||
$this->assertEquals('https://test/dav/addressbooks/[email protected]/contacts/uuid', $contacts->first()->uri); | ||
$this->assertEquals('test2', $contacts->first()->etag); | ||
|
||
return true; | ||
}) | ||
->andReturn(collect()); | ||
}); | ||
$this->partialMock(PrepareJobsContactPush::class, function (MockInterface $mock) { | ||
$mock->shouldReceive('withSubscription')->once()->andReturn($mock); | ||
$mock->shouldReceive('execute') | ||
->once() | ||
->withArgs(function ($localChanges, $changes) { | ||
$this->assertEquals('test2', $changes->first()->etag); | ||
|
||
return true; | ||
}) | ||
->andReturn(collect()); | ||
}); | ||
|
||
(new AddressBookSynchronizer) | ||
->withSubscription($subscription) | ||
->execute(); | ||
|
||
$tester->assert(); | ||
} | ||
|
||
#[Test] | ||
public function it_sync_changes_added_local_contact_batched() | ||
{ | ||
Bus::fake(); | ||
|
||
$subscription = $this->getSubscription(); | ||
|
||
Contact::factory()->create([ | ||
'vault_id' => $subscription->vault_id, | ||
'id' => 'd403af1c-8492-4e9b-9833-cf18c795dfa9', | ||
]); | ||
|
||
$tester = (new DavTester($subscription->uri)) | ||
->getSynctoken('"token"') | ||
->getSyncCollection('"token"', 'test2') | ||
->fake(); | ||
|
||
(new AddressBookSynchronizer) | ||
->withSubscription($subscription) | ||
->execute(); | ||
|
||
$tester->assert(); | ||
|
||
Bus::assertBatched(function (PendingBatch $batch) { | ||
$this->assertCount(1, $batch->jobs); | ||
$job = $batch->jobs[0]; | ||
$this->assertInstanceOf(GetMultipleVCard::class, $job); | ||
$this->assertEquals(['https://test/dav/addressbooks/[email protected]/contacts/uuid'], $this->getPrivateValue($job, 'hrefs')); | ||
|
||
return true; | ||
}); | ||
} | ||
|
||
private function getSubscription(): AddressBookSubscription | ||
{ | ||
$subscription = AddressBookSubscription::factory()->create([ | ||
'uri' => 'https://test/dav/addressbooks/[email protected]/contacts/', | ||
]); | ||
$this->setPermissionInVault($subscription->user, Vault::PERMISSION_VIEW, $subscription->vault); | ||
$token = SyncToken::factory()->create([ | ||
'account_id' => $subscription->user->account_id, | ||
'user_id' => $subscription->user_id, | ||
'name' => 'contacts1', | ||
'timestamp' => now()->addDays(-1), | ||
]); | ||
$subscription->sync_token_id = $token->id; | ||
$subscription->save(); | ||
|
||
$this->assertDatabaseHas('addressbook_subscriptions', [ | ||
'id' => $subscription->id, | ||
'sync_token_id' => $token->id, | ||
]); | ||
|
||
return $subscription; | ||
} | ||
} |