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,35 @@
<?php
namespace Tests\Unit\Jobs;
use Tests\TestCase;
use App\Models\Contact\Contact;
use Illuminate\Support\Facades\Queue;
use App\Jobs\Avatars\GenerateDefaultAvatar;
use App\Jobs\Avatars\GetAvatarsFromInternet;
use App\Jobs\Avatars\CreateAvatarsForExistingContacts;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class CreateAvatarsForExistingContactsTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_creates_jobs_for_avatars_migration()
{
Queue::fake();
$contact = factory(Contact::class)->create([
'avatar_adorable_url' => null,
]);
(new CreateAvatarsForExistingContacts)->handle();
Queue::assertPushed(GenerateDefaultAvatar::class, function ($job) use ($contact) {
return $job->contact->id === $contact->id;
});
Queue::assertPushed(GetAvatarsFromInternet::class, function ($job) use ($contact) {
return $job->contact->id === $contact->id;
});
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace Tests\Unit\Jobs\Dav;
use Tests\TestCase;
use App\Models\User\User;
use App\Jobs\Dav\DeleteVCard;
use Illuminate\Bus\PendingBatch;
use Illuminate\Support\Facades\Bus;
use App\Jobs\Dav\DeleteMultipleVCard;
use Illuminate\Bus\DatabaseBatchRepository;
use App\Models\Account\AddressBookSubscription;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class DeleteMultipleVCardTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_delete_cards()
{
$fake = Bus::fake();
$user = factory(User::class)->create();
$addressBookSubscription = AddressBookSubscription::factory()->create([
'account_id' => $user->account_id,
'user_id' => $user->id,
]);
$pendingBatch = $fake->batch([
$job = new DeleteMultipleVCard($addressBookSubscription, ['https://test/dav/uri']),
]);
$batch = $pendingBatch->dispatch();
$fake->assertBatched(function (PendingBatch $pendingBatch) {
$this->assertCount(1, $pendingBatch->jobs);
$this->assertInstanceOf(DeleteMultipleVCard::class, $pendingBatch->jobs->first());
return true;
});
$batch = app(DatabaseBatchRepository::class)->store($pendingBatch);
$job->withBatchId($batch->id)->handle();
$fake->assertDispatched(function (DeleteVCard $updateVCard) {
$uri = $this->getPrivateValue($updateVCard, 'uri');
$this->assertEquals('https://test/dav/uri', $uri);
return true;
});
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace Tests\Unit\Jobs\Dav;
use Tests\TestCase;
use App\Models\User\User;
use App\Jobs\Dav\DeleteVCard;
use Illuminate\Bus\PendingBatch;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Http;
use Illuminate\Bus\DatabaseBatchRepository;
use App\Models\Account\AddressBookSubscription;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class DeleteVCardTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_delete_card()
{
$fake = Bus::fake();
$user = factory(User::class)->create();
$addressBookSubscription = AddressBookSubscription::factory()->create([
'account_id' => $user->account_id,
'user_id' => $user->id,
]);
Http::fake(function (Request $request) {
$this->assertEquals('https://test/dav/uri', $request->url());
$this->assertEquals('DELETE', $request->method());
return Http::response(null, 204);
});
$pendingBatch = $fake->batch([
$job = new DeleteVCard($addressBookSubscription, 'https://test/dav/uri'),
]);
$batch = $pendingBatch->dispatch();
$fake->assertBatched(function (PendingBatch $pendingBatch) {
$this->assertCount(1, $pendingBatch->jobs);
$this->assertInstanceOf(DeleteVCard::class, $pendingBatch->jobs->first());
return true;
});
$batch = app(DatabaseBatchRepository::class)->store($pendingBatch);
$job->withBatchId($batch->id)->handle();
}
}

View File

@@ -0,0 +1,176 @@
<?php
namespace Tests\Unit\Jobs\Dav;
use Tests\TestCase;
use App\Models\User\User;
use Mockery\MockInterface;
use Tests\Api\DAV\CardEtag;
use App\Jobs\Dav\UpdateVCard;
use App\Models\Contact\Contact;
use Illuminate\Bus\PendingBatch;
use App\Jobs\Dav\GetMultipleVCard;
use Illuminate\Support\Facades\Bus;
use Sabre\CardDAV\Plugin as CardDAVPlugin;
use Illuminate\Bus\DatabaseBatchRepository;
use App\Models\Account\AddressBookSubscription;
use App\Services\DavClient\Utils\Dav\DavClient;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class GetMultipleVCardTest extends TestCase
{
use DatabaseTransactions;
use CardEtag;
/** @test */
public function it_get_cards()
{
$fake = Bus::fake();
$user = factory(User::class)->create();
$addressBookSubscription = AddressBookSubscription::factory()->create([
'account_id' => $user->account_id,
'user_id' => $user->id,
]);
$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(DavClient::class, function (MockInterface $mock) use ($card, $etag) {
$mock->shouldReceive('setBaseUri')->once()->andReturn($mock);
$mock->shouldReceive('setCredentials')->once()->andReturn($mock);
$mock->shouldReceive('addressbookMultiget')
->once()
->withArgs(function ($properties, $contacts) {
$this->assertEquals([
'{DAV:}getetag',
[
'name' => '{'.CardDAVPlugin::NS_CARDDAV.'}address-data',
'value' => null,
'attributes' => [
'content-type' => 'text/vcard',
'version' => '4.0',
],
],
], $properties);
$this->assertEquals(['https://test/dav/uri'], $contacts);
return true;
})
->andReturn([
'https://test/dav/uri' => [
200 => [
'{'.CardDAVPlugin::NS_CARDDAV.'}address-data' => $card,
'{DAV:}getetag' => $etag,
],
],
]);
});
$pendingBatch = $fake->batch([
$job = new GetMultipleVCard($addressBookSubscription, ['https://test/dav/uri']),
]);
$batch = $pendingBatch->dispatch();
$fake->assertBatched(function (PendingBatch $pendingBatch) {
$this->assertCount(1, $pendingBatch->jobs);
$this->assertInstanceOf(GetMultipleVCard::class, $pendingBatch->jobs->first());
return true;
});
$batch = app(DatabaseBatchRepository::class)->store($pendingBatch);
$job->withBatchId($batch->id)->handle();
$fake->assertDispatched(function (UpdateVCard $updateVCard) use ($etag, $card) {
$dto = $this->getPrivateValue($updateVCard, 'contact');
$this->assertEquals('https://test/dav/uri', $dto->uri);
$this->assertEquals($etag, $dto->etag);
$this->assertEquals($card, $dto->card);
return true;
});
}
/** @test */
public function it_get_cards_mock_http()
{
$fake = Bus::fake();
$user = factory(User::class)->create();
$addressBookSubscription = AddressBookSubscription::factory()->create([
'account_id' => $user->account_id,
'user_id' => $user->id,
]);
$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(DavClient::class, function (MockInterface $mock) use ($card, $etag) {
$mock->shouldReceive('setBaseUri')->once()->andReturn($mock);
$mock->shouldReceive('setCredentials')->once()->andReturn($mock);
$mock->shouldReceive('addressbookMultiget')
->once()
->withArgs(function ($properties, $contacts) {
$this->assertEquals([
'{DAV:}getetag',
[
'name' => '{'.CardDAVPlugin::NS_CARDDAV.'}address-data',
'value' => null,
'attributes' => [
'content-type' => 'text/vcard',
'version' => '4.0',
],
],
], $properties);
$this->assertEquals(['https://test/dav/uri'], $contacts);
return true;
})
->andReturn([
'https://test/dav/uri' => [
200 => [
'{'.CardDAVPlugin::NS_CARDDAV.'}address-data' => $card,
'{DAV:}getetag' => $etag,
],
],
]);
});
$pendingBatch = $fake->batch([
$job = new GetMultipleVCard($addressBookSubscription, ['https://test/dav/uri']),
]);
$batch = $pendingBatch->dispatch();
$fake->assertBatched(function (PendingBatch $pendingBatch) {
$this->assertCount(1, $pendingBatch->jobs);
$this->assertInstanceOf(GetMultipleVCard::class, $pendingBatch->jobs->first());
return true;
});
$batch = app(DatabaseBatchRepository::class)->store($pendingBatch);
$job->withBatchId($batch->id)->handle();
$fake->assertDispatched(function (UpdateVCard $updateVCard) use ($etag, $card) {
$dto = $this->getPrivateValue($updateVCard, 'contact');
$this->assertEquals('https://test/dav/uri', $dto->uri);
$this->assertEquals($etag, $dto->etag);
$this->assertEquals($card, $dto->card);
return true;
});
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace Tests\Unit\Jobs\Dav;
use Tests\TestCase;
use App\Models\User\User;
use App\Jobs\Dav\GetVCard;
use Tests\Api\DAV\CardEtag;
use App\Jobs\Dav\UpdateVCard;
use App\Models\Contact\Contact;
use Illuminate\Bus\PendingBatch;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Http;
use Illuminate\Bus\DatabaseBatchRepository;
use App\Models\Account\AddressBookSubscription;
use App\Services\DavClient\Utils\Model\ContactDto;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class GetVCardTest extends TestCase
{
use DatabaseTransactions;
use CardEtag;
/** @test */
public function it_get_card()
{
$fake = Bus::fake();
$user = factory(User::class)->create();
$addressBookSubscription = AddressBookSubscription::factory()->create([
'account_id' => $user->account_id,
'user_id' => $user->id,
]);
$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);
Http::fake([
'https://test/dav/uri' => Http::response($card, 200),
]);
$pendingBatch = $fake->batch([
$job = new GetVCard($addressBookSubscription, new ContactDto('https://test/dav/uri', $etag)),
]);
$batch = $pendingBatch->dispatch();
$fake->assertBatched(function (PendingBatch $pendingBatch) {
$this->assertCount(1, $pendingBatch->jobs);
$this->assertInstanceOf(GetVCard::class, $pendingBatch->jobs->first());
return true;
});
$batch = app(DatabaseBatchRepository::class)->store($pendingBatch);
$job->withBatchId($batch->id)->handle();
$fake->assertDispatched(function (UpdateVCard $updateVCard) use ($etag, $card) {
$dto = $this->getPrivateValue($updateVCard, 'contact');
$this->assertEquals('https://test/dav/uri', $dto->uri);
$this->assertEquals($etag, $dto->etag);
$this->assertEquals($card, $dto->card);
return true;
});
}
}

View File

@@ -0,0 +1,85 @@
<?php
namespace Tests\Unit\Jobs\Dav;
use Tests\TestCase;
use App\Models\User\User;
use App\Jobs\Dav\PushVCard;
use Tests\Api\DAV\CardEtag;
use App\Models\Contact\Contact;
use Illuminate\Bus\PendingBatch;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Http;
use Illuminate\Bus\DatabaseBatchRepository;
use App\Models\Account\AddressBookSubscription;
use App\Services\DavClient\Utils\Model\ContactPushDto;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PushVCardTest extends TestCase
{
use DatabaseTransactions;
use CardEtag;
/**
* @test
* @dataProvider modes
*/
public function it_push_card($mode, $ifmatch)
{
$fake = Bus::fake();
$user = factory(User::class)->create();
$addressBookSubscription = AddressBookSubscription::factory()->create([
'account_id' => $user->account_id,
'user_id' => $user->id,
'uri' => 'https://test/dav',
]);
$contact = factory(Contact::class)->create([
'account_id' => $user->account_id,
'first_name' => 'John',
'last_name' => 'Doe',
'uuid' => 'affacde9-b2fe-4371-9acb-6612aaee6971',
'updated_at' => '2021-09-01',
]);
$card = $this->getCard($contact);
$etag = $this->getEtag($contact, true);
if ($ifmatch == ['etag']) {
$ifmatch = [$etag];
}
Http::fake(function (Request $request, $options) use ($card, $ifmatch) {
$this->assertEquals('https://test/dav/uri', $request->url());
$this->assertEquals('PUT', $request->method());
$this->assertEquals($ifmatch, $request->header('If-Match'));
return Http::response($card, 200);
});
$pendingBatch = $fake->batch([
$job = new PushVCard($addressBookSubscription, new ContactPushDto('https://test/dav/uri', $etag, $card, $contact->id, $mode)),
]);
$batch = $pendingBatch->dispatch();
$fake->assertBatched(function (PendingBatch $pendingBatch) {
$this->assertCount(1, $pendingBatch->jobs);
$this->assertInstanceOf(PushVCard::class, $pendingBatch->jobs->first());
return true;
});
$batch = app(DatabaseBatchRepository::class)->store($pendingBatch);
$job->withBatchId($batch->id)->handle();
}
public function modes(): array
{
return [
[0, []],
[1, ['etag']],
[2, ['*']],
];
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace Tests\Unit\Jobs\Dav;
use Tests\TestCase;
use App\Models\User\User;
use Tests\Api\DAV\CardEtag;
use App\Jobs\Dav\UpdateVCard;
use App\Models\Contact\Contact;
use Illuminate\Bus\PendingBatch;
use App\Models\Account\AddressBook;
use Illuminate\Support\Facades\Bus;
use Illuminate\Bus\DatabaseBatchRepository;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\DavClient\Utils\Model\ContactUpdateDto;
class UpdateVCardTest extends TestCase
{
use DatabaseTransactions;
use CardEtag;
/** @test */
public function it_create_a_contact()
{
$fake = Bus::fake();
$user = factory(User::class)->create();
$addressBook = AddressBook::factory()->create([
'account_id' => $user->account_id,
'user_id' => $user->id,
]);
$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);
$pendingBatch = $fake->batch([
$job = new UpdateVCard($user, $addressBook->name, new ContactUpdateDto('https://test/dav/uricontact1', $etag, $card)),
]);
$batch = $pendingBatch->dispatch();
$fake->assertBatched(function (PendingBatch $pendingBatch) {
$this->assertCount(1, $pendingBatch->jobs);
$this->assertInstanceOf(UpdateVCard::class, $pendingBatch->jobs->first());
return true;
});
$batch = app(DatabaseBatchRepository::class)->store($pendingBatch);
$job->withBatchId($batch->id)->handle();
$this->assertDatabaseHas('contacts', [
'first_name' => 'Test',
'uuid' => 'affacde9-b2fe-4371-9acb-6612aaee6971',
]);
}
}

View File

@@ -0,0 +1,57 @@
<?php
namespace Tests\Unit\Jobs;
use Tests\TestCase;
use Mockery\MockInterface;
use App\Models\Account\Place;
use Illuminate\Bus\PendingBatch;
use App\Jobs\GetWeatherInformation;
use Illuminate\Support\Facades\Bus;
use Illuminate\Bus\DatabaseBatchRepository;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\Instance\Weather\GetWeatherInformation as GetWeatherInformationService;
class GetWeatherInformationTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_run_job_weather_information()
{
$fake = Bus::fake();
$place = factory(Place::class)->create([
'latitude' => '34.112456',
'longitude' => '-118.4270732',
]);
$this->mock(GetWeatherInformationService::class, function (MockInterface $mock) use ($place) {
$mock->shouldReceive('execute')
->once()
->withArgs(function ($data) use ($place) {
$this->assertEquals([
'account_id' => $place->account_id,
'place_id' => $place->id,
], $data);
return true;
});
});
$pendingBatch = $fake->batch([
$job = new GetWeatherInformation($place),
]);
$batch = $pendingBatch->dispatch();
$fake->assertBatched(function (PendingBatch $pendingBatch) {
$this->assertCount(1, $pendingBatch->jobs);
$this->assertInstanceOf(GetWeatherInformation::class, $pendingBatch->jobs->first());
return true;
});
$batch = app(DatabaseBatchRepository::class)->store($pendingBatch);
$job->withBatchId($batch->id)->handle();
}
}

View File

@@ -0,0 +1,274 @@
<?php
namespace Tests\Unit\Jobs\Reminder;
use Carbon\Carbon;
use Tests\TestCase;
use App\Models\User\User;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Contact\Reminder;
use App\Notifications\UserNotified;
use App\Notifications\UserReminded;
use App\Models\Contact\ReminderOutbox;
use Illuminate\Support\Facades\Notification;
use App\Jobs\Reminder\NotifyUserAboutReminder;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class NotifyUserAboutReminderTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_sends_a_reminder_to_a_user()
{
Notification::fake();
Carbon::setTestNow(Carbon::create(2017, 1, 1, 7, 0, 0));
$account = factory(Account::class)->create([
'default_time_reminder_is_sent' => '07:00',
]);
$contact = factory(Contact::class)->create(['account_id' => $account->id]);
$user = factory(User::class)->create(['account_id' => $account->id]);
$reminder = factory(Reminder::class)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
'initial_date' => '2017-01-01',
'title' => 'fake text saying nothing',
'frequency_type' => 'year',
'frequency_number' => 1,
]);
$reminderOutbox = factory(ReminderOutbox::class)->create([
'account_id' => $user->account_id,
'reminder_id' => $reminder->id,
'user_id' => $user->id,
'planned_date' => '2017-01-01',
'nature' => 'reminder',
]);
NotifyUserAboutReminder::dispatch($reminderOutbox);
// Assert the notification has been sent to the user with the right
// reminderoutbox id and the right email content
Notification::assertSentTo(
$user,
UserReminded::class,
function ($notification, $channels) use ($reminderOutbox, $reminder, $user, $contact) {
$mailData = $notification->toMail($user)->toArray();
$this->assertEquals("Reminder for {$contact->name}", $mailData['subject']);
$this->assertEquals("Hi {$user->first_name}", $mailData['greeting']);
$this->assertStringContainsString("You wanted to be reminded of {$reminderOutbox->reminder->title}", $mailData['introLines'][0]);
return $notification->reminder->id === $reminder->id;
}
);
}
/** @test */
public function it_sends_a_notification_to_a_user()
{
Notification::fake();
Carbon::setTestNow(Carbon::create(2017, 1, 1, 7, 0, 0));
$account = factory(Account::class)->create([
'default_time_reminder_is_sent' => '07:00',
]);
$contact = factory(Contact::class)->create(['account_id' => $account->id]);
$user = factory(User::class)->create(['account_id' => $account->id]);
$reminder = factory(Reminder::class)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
'initial_date' => '2017-01-01',
'title' => 'fake text saying nothing',
'frequency_type' => 'year',
'frequency_number' => '1',
]);
$reminderOutbox = factory(ReminderOutbox::class)->create([
'account_id' => $user->account_id,
'reminder_id' => $reminder->id,
'user_id' => $user->id,
'planned_date' => '2017-01-01',
'nature' => 'notification',
]);
NotifyUserAboutReminder::dispatch($reminderOutbox);
// Assert the notification has been sent to the user with the right
// reminderoutbox id and the right email content
Notification::assertSentTo(
$user,
UserNotified::class,
function ($notification, $channels) use ($reminder, $user, $contact) {
$mailData = $notification->toMail($user)->toArray();
$this->assertEquals("Reminder for {$contact->name}", $mailData['subject']);
$this->assertEquals("Hi {$user->first_name}", $mailData['greeting']);
$this->assertStringContainsString('In days (on Jan 01, 2018), the following event will happen:', $mailData['introLines'][0]);
return $notification->reminder->id === $reminder->id;
}
);
}
/** @test */
public function it_doesnt_notify_a_user_if_he_is_on_the_free_plan()
{
Notification::fake();
Carbon::setTestNow(Carbon::create(2017, 1, 1, 7, 0, 0));
config(['monica.requires_subscription' => true]);
$account = factory(Account::class)->create([
'default_time_reminder_is_sent' => '07:00',
'has_access_to_paid_version_for_free' => false,
]);
$contact = factory(Contact::class)->create(['account_id' => $account->id]);
$user = factory(User::class)->create(['account_id' => $account->id]);
$reminder = factory(Reminder::class)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
'initial_date' => '2017-01-01',
'title' => 'fake text saying nothing',
'frequency_type' => 'year',
'frequency_number' => 1,
]);
$reminderOutbox = factory(ReminderOutbox::class)->create([
'account_id' => $user->account_id,
'reminder_id' => $reminder->id,
'user_id' => $user->id,
'planned_date' => '2017-01-01',
]);
NotifyUserAboutReminder::dispatch($reminderOutbox);
Notification::assertNotSentTo(
$user,
UserReminded::class
);
}
/** @test */
public function it_doesnt_notify_a_user_if_contact_deleted()
{
Notification::fake();
Carbon::setTestNow(Carbon::create(2017, 1, 1, 7, 0, 0));
$account = factory(Account::class)->create([
'default_time_reminder_is_sent' => '07:00',
]);
$contact = factory(Contact::class)->create(['account_id' => $account->id]);
$user = factory(User::class)->create(['account_id' => $account->id]);
$reminder = factory(Reminder::class)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
'initial_date' => '2017-01-01',
'title' => 'fake text saying nothing',
'frequency_type' => 'year',
'frequency_number' => '1',
]);
$reminderOutbox = factory(ReminderOutbox::class)->create([
'account_id' => $user->account_id,
'reminder_id' => $reminder->id,
'user_id' => $user->id,
'planned_date' => '2017-01-01',
'nature' => 'notification',
]);
$contact->delete();
NotifyUserAboutReminder::dispatch($reminderOutbox);
Notification::assertNotSentTo(
$user,
UserReminded::class
);
}
/** @test */
public function it_marks_the_one_time_reminder_has_inactive_once_it_is_sent()
{
Notification::fake();
Carbon::setTestNow(Carbon::create(2017, 1, 1, 7, 0, 0));
$account = factory(Account::class)->create([
'default_time_reminder_is_sent' => '07:00',
]);
$contact = factory(Contact::class)->create(['account_id' => $account->id]);
$user = factory(User::class)->create(['account_id' => $account->id]);
$reminder = factory(Reminder::class)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
'initial_date' => '2017-01-01',
'title' => 'fake text saying nothing',
'frequency_type' => 'one_time',
]);
$reminderOutbox = factory(ReminderOutbox::class)->create([
'account_id' => $user->account_id,
'reminder_id' => $reminder->id,
'user_id' => $user->id,
'planned_date' => '2017-01-01',
]);
NotifyUserAboutReminder::dispatch($reminderOutbox);
$this->assertDatabaseMissing('reminder_outbox', [
'account_id' => $user->account_id,
'id' => $reminderOutbox->id,
]);
$this->assertDatabaseHas('reminders', [
'account_id' => $user->account_id,
'id' => $reminder->id,
'inactive' => true,
]);
}
/** @test */
public function it_reschedule_a_recurring_reminder_once_it_is_sent()
{
Notification::fake();
Carbon::setTestNow(Carbon::create(2017, 1, 1, 7, 0, 0));
$account = factory(Account::class)->create([
'default_time_reminder_is_sent' => '07:00',
]);
$contact = factory(Contact::class)->create(['account_id' => $account->id]);
$user = factory(User::class)->create(['account_id' => $account->id]);
$reminder = factory(Reminder::class)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
'initial_date' => '2017-01-01',
'title' => 'fake text saying nothing',
'frequency_type' => 'year',
'frequency_number' => 1,
]);
$reminderOutbox = factory(ReminderOutbox::class)->create([
'account_id' => $user->account_id,
'reminder_id' => $reminder->id,
'user_id' => $user->id,
'planned_date' => '2017-01-01',
]);
NotifyUserAboutReminder::dispatch($reminderOutbox);
$this->assertDatabaseMissing('reminder_outbox', [
'account_id' => $user->account_id,
'id' => $reminderOutbox->id,
]);
$this->assertDatabaseHas('reminders', [
'account_id' => $user->account_id,
'id' => $reminder->id,
'inactive' => false,
]);
$this->assertDatabaseHas('reminder_outbox', [
'account_id' => $user->account_id,
'reminder_id' => $reminder->id,
]);
}
}

View File

@@ -0,0 +1,125 @@
<?php
namespace Tests\Unit\Jobs;
use Carbon\Carbon;
use Tests\TestCase;
use App\Models\User\User;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Notifications\StayInTouchEmail;
use App\Jobs\StayInTouch\ScheduleStayInTouch;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Notification as NotificationFacade;
class ScheduleStayInTouchTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_dispatches_an_email()
{
NotificationFacade::fake();
Carbon::setTestNow(Carbon::create(2017, 1, 1, 12, 0, 0));
$account = factory(Account::class)->create([
'default_time_reminder_is_sent' => '07:00',
'has_access_to_paid_version_for_free' => 1,
]);
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
'stay_in_touch_trigger_date' => '2017-01-01 07:00:00',
'stay_in_touch_frequency' => 5,
]);
$user = factory(User::class)->create([
'account_id' => $account->id,
'email' => 'john@doe.com',
'timezone' => 'America/New_York',
]);
ScheduleStayInTouch::dispatch($contact);
NotificationFacade::assertSentTo($user, StayInTouchEmail::class,
function ($notification, $channels) use ($contact) {
return $channels[0] == 'mail'
&& $notification->assertSentFor($contact);
}
);
$notifications = NotificationFacade::sent($user, StayInTouchEmail::class);
$message = $notifications[0]->toMail($user);
$this->assertStringContainsString('You asked to be reminded to stay in touch with John Doe every 5 days.', implode('', $message->introLines));
$this->assertDatabaseHas('contacts', [
'stay_in_touch_trigger_date' => '2017-01-06 07:00:00',
]);
}
/** @test */
public function it_doesnt_dispatches_an_email_if_free_account()
{
NotificationFacade::fake();
Carbon::setTestNow(Carbon::create(2017, 1, 1, 5, 0, 0));
config(['monica.requires_subscription' => true]);
$account = factory(Account::class)->create([
'default_time_reminder_is_sent' => '07:00',
'has_access_to_paid_version_for_free' => 0,
]);
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
'stay_in_touch_trigger_date' => '2017-01-01 07:00:00',
'stay_in_touch_frequency' => 5,
]);
$user = factory(User::class)->create([
'account_id' => $account->id,
'email' => 'john@doe.com',
'timezone' => 'America/New_York',
]);
ScheduleStayInTouch::dispatch($contact);
NotificationFacade::assertNotSentTo($user, StayInTouchEmail::class);
NotificationFacade::assertNothingSent();
$this->assertDatabaseHas('contacts', [
'stay_in_touch_trigger_date' => '2017-01-01 07:00:00',
]);
}
/** @test */
public function it_reschedule_missed_stayintouch()
{
NotificationFacade::fake();
Carbon::setTestNow(Carbon::create(2019, 1, 1, 5, 0, 0));
$account = factory(Account::class)->create([
'default_time_reminder_is_sent' => '07:00',
'has_access_to_paid_version_for_free' => 0,
]);
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
'stay_in_touch_trigger_date' => '2018-01-01 07:00:00',
'stay_in_touch_frequency' => 30,
]);
$user = factory(User::class)->create([
'account_id' => $account->id,
'email' => 'john@doe.com',
'timezone' => 'America/New_York',
]);
ScheduleStayInTouch::dispatch($contact);
NotificationFacade::assertNotSentTo($user, StayInTouchEmail::class);
NotificationFacade::assertNothingSent();
$this->assertDatabaseHas('contacts', [
'stay_in_touch_trigger_date' => '2019-01-26 07:00:00',
]);
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace Tests\Unit\Jobs;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ServiceQueueTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_run_a_service_ok(): void
{
config(['queue.default' => 'sync']);
ServiceQueueTester::dispatch();
$this->assertTrue(ServiceQueueTester::$executed);
$this->assertFalse(ServiceQueueTester::$failed);
}
/** @test */
public function it_run_a_service_sync(): void
{
ServiceQueueTester::dispatchSync();
$this->assertTrue(ServiceQueueTester::$executed);
$this->assertFalse(ServiceQueueTester::$failed);
}
/** @test */
public function it_run_a_service_which_failed(): void
{
$this->expectException(\Exception::class);
try {
ServiceQueueTester::dispatchSync(['throw' => true]);
} finally {
$this->assertTrue(ServiceQueueTester::$executed);
$this->assertTrue(ServiceQueueTester::$failed);
}
}
/** @test */
public function service_is_not_run_if_queue_set(): void
{
config(['queue.default' => 'database']);
ServiceQueueTester::dispatch(['throw' => true]);
$this->assertFalse(ServiceQueueTester::$executed);
$this->assertFalse(ServiceQueueTester::$failed);
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace Tests\Unit\Jobs;
use Throwable;
use App\Services\BaseService;
use App\Services\QueuableService;
use App\Services\DispatchableService;
class ServiceQueueTester extends BaseService implements QueuableService
{
use DispatchableService;
public static bool $executed = false;
public static bool $failed = false;
public bool $object;
/**
* Initialize the service.
*
* @param array $data
*/
public function __construct()
{
self::$executed = false;
self::$failed = false;
}
/**
* Execute the service.
*/
public function handle($data): void
{
self::$executed = true;
if ($data && $data['throw'] === true) {
throw new \Exception();
}
}
/**
* Handle a job failure.
*
* @param \Throwable $exception
*/
public function failed(Throwable $exception): void
{
self::$failed = true;
if (isset($this->obj)) {
// variable can be touch
}
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace Tests\Unit\Jobs;
use Carbon\Carbon;
use Tests\TestCase;
use App\Jobs\SynchronizeAddressBooks;
use App\Models\Account\AddressBookSubscription;
use App\Services\DavClient\SynchronizeAddressBook;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class SynchronizeAddressBooksTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_run_synchronize()
{
Carbon::setTestNow(Carbon::create(2021, 9, 1, 10, 0, 0));
$subscription = AddressBookSubscription::factory()->create();
$this->mock(SynchronizeAddressBook::class, function ($mock) use ($subscription) {
$mock->shouldReceive('execute')
->once()
->with([
'account_id' => $subscription->account_id,
'addressbook_subscription_id' => $subscription->id,
'force' => false,
]);
});
(new SynchronizeAddressBooks($subscription))
->handle();
$subscription->refresh();
$this->assertEquals(Carbon::create(2021, 9, 1, 10, 0, 0), $subscription->last_synchronized_at);
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace Tests\Unit\Jobs;
use Tests\TestCase;
use App\Models\Contact\Contact;
use App\Jobs\Avatars\UpdateGravatar;
use Illuminate\Support\Facades\Queue;
use App\Jobs\Avatars\UpdateAllGravatars;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class UpdateAllGravatarsTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_creates_jobs_for_update_gravatars()
{
Queue::fake();
$contacts = factory(Contact::class, 10)->create([
'avatar_source' => 'gravatar',
]);
(new UpdateAllGravatars)->handle();
foreach ($contacts as $contact) {
Queue::assertPushed(UpdateGravatar::class, function ($job) use ($contact) {
return $job->contact->id === $contact->id;
});
}
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Tests\Unit\Jobs;
use Tests\TestCase;
use App\Models\Contact\Contact;
use App\Models\Contact\ContactField;
use App\Models\Contact\ContactFieldType;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Jobs\Avatars\UpdateGravatar as UpdateGravatarJob;
class UpdateGravatarTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_update_gravatar()
{
$contact = factory(Contact::class)->create();
$contactFieldType = factory(ContactFieldType::class)->create([
'account_id' => $contact->account->id,
]);
factory(ContactField::class)->create([
'contact_id' => $contact->id,
'account_id' => $contact->account->id,
'contact_field_type_id' => $contactFieldType->id,
'data' => 'matt@wordpress.com',
]);
(new UpdateGravatarJob($contact))->handle();
$contact->refresh();
$this->assertNotNull(
$contact->avatar_gravatar_url
);
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace Tests\Unit\Jobs;
use Carbon\Carbon;
use Tests\TestCase;
use App\Models\Contact\Contact;
use App\Jobs\UpdateLastConsultedDate;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class UpdateLastConsultedDateTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_updates_the_last_consulted_at_field_for_the_given_contact()
{
Carbon::setTestNow(Carbon::create(2017, 1, 1, 7, 0, 0));
$contact = factory(Contact::class)->create([
'number_of_views' => 1,
]);
UpdateLastConsultedDate::dispatch($contact);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'last_consulted_at' => '2017-01-01 07:00:00',
'number_of_views' => 2,
]);
}
}