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,92 @@
<?php
namespace Tests\Commands\OneTime;
use Tests\TestCase;
use App\Models\User\User;
use App\Models\Account\Photo;
use App\Models\Contact\Contact;
use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class MoveAvatarsToPhotosDirectoryTest extends TestCase
{
use DatabaseTransactions;
/**
* Returns an array containing a user object along with
* a contact for that user.
*
* @return array
*/
private function fetchUser()
{
$user = factory(User::class)->create();
$contact = factory(Contact::class)->create([
'account_id' => $user->account_id,
]);
return [$user, $contact];
}
/** @test */
public function it_move_avatars_to_photo_directory()
{
[$user, $contact] = $this->fetchUser();
Storage::fake('public');
Storage::disk('public')->put('avatars/avatar.jpg', 'content');
Storage::disk('public')->put('avatars/avatar_110.jpg', 'content');
Storage::disk('public')->put('avatars/avatar_174.jpg', 'content');
$contact->avatar_file_name = 'avatars/avatar.jpg';
$contact->avatar_location = 'public';
$contact->has_avatar = true;
$contact->save();
Storage::disk('public')->assertExists('avatars/avatar.jpg');
$this->artisan('monica:moveavatarstophotosdirectory')->run();
Storage::disk('public')->assertMissing('avatars/avatar.jpg');
Storage::disk('public')->assertMissing('avatars/avatar_110.jpg');
Storage::disk('public')->assertMissing('avatars/avatar_174.jpg');
$contact->refresh();
$photo = Photo::find($contact->avatar_photo_id);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'avatar_source' => 'photo',
]);
$this->assertStringContainsString('photos/', $photo->new_filename);
Storage::disk('public')->assertExists($photo->new_filename);
}
/** @test */
public function it_handles_missing_avatar()
{
[$user, $contact] = $this->fetchUser();
Storage::fake('public');
$contact->avatar_file_name = 'avatars/avatar.jpg';
$contact->avatar_location = 'public';
$contact->has_avatar = true;
$contact->save();
$this->artisan('monica:moveavatarstophotosdirectory')->run();
Storage::disk('public')->assertMissing('avatars/avatar.jpg');
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'avatar_source' => 'default',
'avatar_file_name' => 'avatars/avatar.jpg',
'avatar_location' => 'public',
]);
}
}

View File

@@ -0,0 +1,102 @@
<?php
namespace Tests\Commands\Other;
use Tests\TestCase;
use App\Models\User\User;
use App\Models\User\SyncToken;
use App\Models\Account\Account;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class CleanCommandTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function clean_command_left_one_token()
{
$account = factory(Account::class)->create();
$user = factory(User::class)->create([
'account_id' => $account->id,
]);
SyncToken::create([
'account_id' => $account->id,
'user_id' => $user->id,
'name' => 'contacts',
'timestamp' => now(),
]);
$this->artisan('monica:clean')->run();
$this->assertDatabaseHas('synctoken', [
'account_id' => $account->id,
'user_id' => $user->id,
'name' => 'contacts',
]);
}
/** @test */
public function clean_command_left_all_token()
{
$account = factory(Account::class)->create();
$user = factory(User::class)->create([
'account_id' => $account->id,
]);
$s1 = SyncToken::create([
'account_id' => $account->id,
'user_id' => $user->id,
'name' => 'contacts',
'timestamp' => now(),
]);
$s2 = SyncToken::create([
'account_id' => $account->id,
'user_id' => $user->id,
'name' => 'contacts',
'timestamp' => now()->addDays(-10),
]);
$command = $this->artisan('monica:clean');
$command->expectsOutput("Delete token {$s2->id} - User {$user->id} - Type contacts - timestamp {$s2->timestamp}");
$command->run();
$this->assertDatabaseHas('synctoken', [
'id' => $s1->id,
]);
$this->assertDatabaseMissing('synctoken', [
'id' => $s2->id,
]);
}
/** @test */
public function clean_command_dryrun()
{
$account = factory(Account::class)->create();
$user = factory(User::class)->create([
'account_id' => $account->id,
]);
$s1 = SyncToken::create([
'account_id' => $account->id,
'user_id' => $user->id,
'name' => 'contacts',
'timestamp' => now(),
]);
$s2 = SyncToken::create([
'account_id' => $account->id,
'user_id' => $user->id,
'name' => 'contacts',
'timestamp' => now()->addDays(-10),
]);
$this->artisan('monica:clean', ['--dry-run' => true])->run();
$this->assertDatabaseHas('synctoken', [
'id' => $s1->id,
]);
$this->assertDatabaseHas('synctoken', [
'id' => $s2->id,
]);
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace Tests\Commands\Other;
use Tests\TestCase;
use App\Models\User\User;
use App\Console\Commands\CreateAccount;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class CreateAccountTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_creates_account()
{
$email = 'user1@example.com';
$this->artisan('account:create', ['--email' => 'user1@example.com', '--password' => 'astrongpassword'])
->run();
$user = User::where('email', '=', $email)->first();
$this->assertNotEmpty($user);
}
/** @test */
public function it_creates_account_with_specified_name()
{
$email = 'user1@example.com';
$firstname = 'firstname';
$lastname = 'lastname';
$this->artisan('account:create', [
'--email' => $email,
'--password' => 'astrongpassword',
'--firstname' => $firstname,
'--lastname' => $lastname,
])->run();
$user = User::where('email', '=', $email)->first();
$this->assertNotEmpty($user);
}
/** @test */
public function it_fails_creation_without_email()
{
$this->artisan('account:create', ['--password' => 'astrongpassword'])
->expectsOutput(CreateAccount::ERROR_MISSING_EMAIL)
->doesntExpectOutput(CreateAccount::ERROR_MISSING_PASSWORD)
->run();
}
/** @test */
public function it_fails_creation_without_password()
{
$email = 'user1@example.com';
$this->artisan('account:create', ['--email' => $email])
->expectsOutput(CreateAccount::ERROR_MISSING_PASSWORD)
->doesntExpectOutput(CreateAccount::ERROR_MISSING_EMAIL)
->run();
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace Tests\Commands\Other;
use Tests\TestCase;
use App\Models\User\User;
use Mockery\MockInterface;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\DavClient\CreateAddressBookSubscription;
class CreateAddressBookSubscriptionTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_add_addressbook()
{
$user = factory(User::class)->create();
$this->mock(CreateAddressBookSubscription::class, function (MockInterface $mock) use ($user) {
$mock->shouldReceive('execute')
->once()
->withArgs(function ($data) use ($user) {
$this->assertEquals([
'account_id' => $user->account_id,
'user_id' => $user->id,
'base_uri' => 'https://test',
'username' => 'login',
'password' => 'password',
], $data);
return true;
});
});
$this->artisan('monica:newaddressbooksubscription', [
'--email' => $user->email,
'--url' => 'https://test',
'--login' => 'login',
'--password' => 'password',
])->run();
}
}

View File

@@ -0,0 +1,87 @@
<?php
namespace Tests\Commands\Other;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ImportCSVTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function csv_import_contacts()
{
Storage::fake('public');
$user = $this->getUser();
$path = base_path('tests/stubs/single_contact_stub.csv');
$totalContacts = Contact::where('account_id', $user->account_id)->count();
$this->artisan('import:csv', [
'user' => $user->email,
'file' => $path,
])
->assertSuccessful()
->run();
$this->assertDatabaseHas('contacts', [
'first_name' => 'Bono',
'last_name' => 'Hewson',
]);
$this->assertDatabaseHas('contact_fields', [
'data' => 'bono@example.com',
]);
// Allows checking if birthday was correctly set
$this->assertDatabaseHas('special_dates', [
'date' => '1960-05-10',
]);
// Asserts that only 3 new contacts were created
$this->assertEquals(
$totalContacts + 1,
Contact::where('account_id', $user->account_id)->count()
);
}
/** @test */
public function csv_import_validates_user()
{
$path = base_path('tests/stubs/single_contact_stub.csv');
$this->artisan('import:csv', [
'user' => 'test@test.com',
'file' => $path,
])
->assertFailed()
->expectsOutput('You need to provide a valid User ID or email address!')
->run();
}
/** @test */
public function csv_import_validates_file()
{
$user = $this->getUser();
$this->artisan('import:csv', [
'user' => $user->email,
'file' => 'xxx',
])
->assertFailed()
->expectsOutput('You need to provide a valid file path.')
->run();
}
private function getUser()
{
$account = Account::createDefault('John', 'Doe', 'johndoe@example.com', 'secret', null, 'en');
return $account->users()->first();
}
}

View File

@@ -0,0 +1,104 @@
<?php
namespace Tests\Commands\Other;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ImportVCardsTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_validates_user()
{
$path = base_path('tests/stubs/vcard_stub.vcf');
$this->artisan('import:vcard', ['--user' => 'notfound@example.com', '--path' => $path, '--no-interaction' => true])
->assertFailed()
->expectsOutput('No user with that email.')
->run();
}
/** @test */
public function it_validates_file()
{
$user = $this->getUser();
$this->artisan('import:vcard', ['--user' => $user->email, '--path' => 'not_found', '--no-interaction' => true])
->assertFailed()
->expectsOutput('The provided vcard file was not found or is not valid!')
->run();
}
/** @test */
public function it_imports_contacts()
{
Storage::fake('public');
$user = $this->getUser();
$path = base_path('tests/stubs/vcard_stub.vcf');
$totalContacts = Contact::where('account_id', $user->account_id)->count();
$this->artisan('import:vcard', ['--user' => $user->email, '--path' => $path, '--no-interaction' => true])
->assertSuccessful()
->run();
$this->assertDatabaseHas('contacts', [
'first_name' => 'John',
'last_name' => 'Doe',
]);
$this->assertDatabaseHas('contact_fields', [
'data' => 'john.doe@example.com',
]);
// Allows checking if birthday was correctly set
$this->assertDatabaseHas('special_dates', [
'date' => '1960-05-10',
]);
// Allows checking nickname fallback
$this->assertDatabaseHas('contacts', [
'first_name' => 'Johnny',
]);
$this->assertDatabaseHas('contacts', [
'company' => 'U2',
'job' => 'Lead vocalist',
]);
// Allows checking addresses are correctly saved
$this->assertDatabaseHas('places', [
'street' => '17 Shakespeare Ave.',
'postal_code' => 'SO17 2HB',
'city' => 'Southampton',
'country' => 'GB',
]);
$this->assertDatabaseHas('contact_fields', [
'data' => 'bono@example.com',
]);
$this->assertDatabaseHas('contact_fields', [
'data' => '+1 202-555-0191',
]);
// Asserts that only 3 new contacts were created
$this->assertEquals(
$totalContacts + 3,
Contact::where('account_id', $user->account_id)->count()
);
}
private function getUser()
{
$account = Account::createDefault('John', 'Doe', 'johndoe@example.com', 'secret', null, 'en');
return $account->users()->first();
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Tests\Commands\Other;
use Tests\TestCase;
use App\Console\Commands\Helpers\Command;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class UpdateCommandTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function update_command_default()
{
/** @var \Tests\Helpers\CommandCallerFake */
$fake = Command::fake();
$this->artisan('monica:update')->run();
$this->assertCount(9, $fake->buffer);
$this->assertCommandContains($fake->buffer[0], 'Maintenance mode: on', 'php artisan down');
$this->assertCommandContains($fake->buffer[1], 'Resetting application cache', 'php artisan cache:clear');
$this->assertCommandContains($fake->buffer[2], 'Clear config cache', 'php artisan config:clear');
$this->assertCommandContains($fake->buffer[3], 'Clear route cache', 'php artisan route:clear');
$this->assertCommandContains($fake->buffer[4], 'Clear view cache', 'php artisan view:clear');
$this->assertCommandContains($fake->buffer[5], 'Performing migrations', 'php artisan migrate');
$this->assertCommandContains($fake->buffer[6], 'Check for encryption keys', 'php artisan monica:passport');
$this->assertCommandContains($fake->buffer[7], 'Ping for new version', 'php artisan monica:ping');
$this->assertCommandContains($fake->buffer[8], 'Maintenance mode: off', 'php artisan up');
}
/** @test */
public function update_command_composer()
{
/** @var \Tests\Helpers\CommandCallerFake */
$fake = Command::fake();
$this->artisan('monica:update', ['--composer-install' => true])->run();
$this->assertCount(10, $fake->buffer);
$this->assertCommandContains($fake->buffer[0], 'Maintenance mode: on', 'php artisan down');
$this->assertCommandContains($fake->buffer[1], 'Resetting application cache', 'php artisan cache:clear');
$this->assertCommandContains($fake->buffer[2], 'Clear config cache', 'php artisan config:clear');
$this->assertCommandContains($fake->buffer[3], 'Clear route cache', 'php artisan route:clear');
$this->assertCommandContains($fake->buffer[4], 'Clear view cache', 'php artisan view:clear');
$this->assertCommandContains($fake->buffer[5], 'Updating composer dependencies', 'composer install');
$this->assertCommandContains($fake->buffer[6], 'Performing migrations', 'php artisan migrate');
$this->assertCommandContains($fake->buffer[7], 'Check for encryption keys', 'php artisan monica:passport');
$this->assertCommandContains($fake->buffer[8], 'Ping for new version', 'php artisan monica:ping');
$this->assertCommandContains($fake->buffer[9], 'Maintenance mode: off', 'php artisan up');
}
private function assertCommandContains($array, $message, $command)
{
$this->assertStringContainsString($message, $array['message']);
$this->assertStringContainsString($command, $array['command']);
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Tests\Commands\Scheduling;
use Tests\TestCase;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class CalculateStatisticsTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function the_command_runs_well()
{
$runsWell = true;
try {
$this->artisan('monica:calculatestatistics')->run();
} catch (QueryException $e) {
$runsWell = false;
}
$this->assertTrue($runsWell);
}
}

View File

@@ -0,0 +1,119 @@
<?php
namespace Tests\Commands\Scheduling;
use Carbon\Carbon;
use Tests\TestCase;
use App\Models\Instance\Cron;
use App\Console\Scheduling\CronEvent;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class CronEventTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_get_the_right_command()
{
$cron = factory(Cron::class)->create();
$event = CronEvent::command($cron->command);
$this->assertEquals($event->cron()->id, $cron->id);
}
/** @test */
public function now_not_due()
{
$cron = factory(Cron::class)->create();
$event = new CronEvent($cron);
$this->assertFalse($event->isDue());
}
/** @test */
public function next_minute_is_due()
{
Carbon::setTestNow(Carbon::create(2019, 5, 1, 7, 0, 0));
$cron = factory(Cron::class)->create();
$event = new CronEvent($cron);
$this->assertFalse($event->isDue());
Carbon::setTestNow(Carbon::create(2019, 5, 1, 7, 1, 0));
$this->assertTrue($event->isDue());
$this->assertDatabaseHas('crons', [
'command' => $cron->command,
'last_run' => '2019-05-01 07:01:00',
]);
}
/** @test */
public function hourly_cron()
{
Carbon::setTestNow(Carbon::create(2019, 5, 1, 7, 0, 0));
$cron = factory(Cron::class)->create();
$event = new CronEvent($cron);
$event->hourly();
$this->assertFalse($event->isDue());
Carbon::setTestNow(Carbon::create(2019, 5, 1, 8, 22, 0));
$this->assertTrue($event->isDue());
$this->assertDatabaseHas('crons', [
'command' => $cron->command,
'last_run' => '2019-05-01 08:22:00',
]);
Carbon::setTestNow(Carbon::create(2019, 5, 1, 8, 59, 0));
$this->assertFalse($event->isDue());
Carbon::setTestNow(Carbon::create(2019, 5, 1, 9, 01, 0));
$this->assertTrue($event->isDue());
$this->assertDatabaseHas('crons', [
'command' => $cron->command,
'last_run' => '2019-05-01 09:01:00',
]);
}
/** @test */
public function daily_cron()
{
Carbon::setTestNow(Carbon::create(2019, 5, 1, 7, 0, 0));
$cron = factory(Cron::class)->create();
$event = new CronEvent($cron);
$event->daily();
Carbon::setTestNow(Carbon::create(2019, 5, 2, 8, 10, 0));
$this->assertTrue($event->isDue());
$this->assertDatabaseHas('crons', [
'command' => $cron->command,
'last_run' => '2019-05-02 08:10:00',
]);
Carbon::setTestNow(Carbon::create(2019, 5, 2, 10, 0, 0));
$this->assertFalse($event->isDue());
Carbon::setTestNow(Carbon::create(2019, 5, 3, 0, 0, 0));
$this->assertTrue($event->isDue());
$this->assertDatabaseHas('crons', [
'command' => $cron->command,
'last_run' => '2019-05-03 00:00:00',
]);
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Tests\Commands\Scheduling;
use Tests\TestCase;
use App\Jobs\SynchronizeAddressBooks;
use Illuminate\Support\Facades\Queue;
use App\Models\Account\AddressBookSubscription;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class DavClientsUpdateTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_dispatch_subscription_update()
{
Queue::fake();
$subscription = AddressBookSubscription::factory()->create();
$this->artisan('monica:davclients')->run();
Queue::assertPushed(SynchronizeAddressBooks::class, function ($job) use ($subscription) {
return $job->subscription->id === $subscription->id;
});
}
}

View File

@@ -0,0 +1,101 @@
<?php
namespace Tests\Commands\Scheduling;
use Tests\TestCase;
use App\Models\Instance\Instance;
use Illuminate\Support\Facades\Http;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PingVersionServerTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_send_ping()
{
config(['monica.weekly_ping_server_url' => 'https://version.test/ping']);
config(['monica.app_version' => '2.9.0']);
config(['monica.check_version' => true]);
Instance::all()->each(function ($instance) {
$instance->delete();
});
$instance = factory(Instance::class)->create();
$ret = [
'new_version' => true,
'latest_version' => '3.1.0',
'number_of_versions_since_user_version' => 2,
'notes' => 'notes',
];
Http::fake([
'https://version.test/*' => Http::response($ret, 200),
]);
$this->artisan('monica:ping')->run();
$instance->refresh();
$this->assertEquals('3.1.0', $instance->latest_version);
$this->assertEquals('notes', $instance->latest_release_notes);
$this->assertEquals(2, $instance->number_of_versions_since_current_version);
}
/** @test */
public function it_clear_instance()
{
config(['monica.weekly_ping_server_url' => 'https://version.test/ping']);
config(['monica.app_version' => '3.1.0']);
Instance::all()->each(function ($instance) {
$instance->delete();
});
$instance = factory(Instance::class)->create([
'latest_version' => '3.1.0',
]);
$ret = [
'new_version' => false,
'latest_version' => '2.9.0',
'number_of_versions_since_user_version' => 0,
'notes' => '',
];
Http::fake([
'https://version.test/*' => Http::response($ret, 200),
]);
$this->artisan('monica:ping')->run();
$instance->refresh();
$this->assertEquals('3.1.0', $instance->latest_version);
$this->assertNull($instance->latest_release_notes);
$this->assertNull($instance->number_of_versions_since_current_version);
}
/**
* If an instance sets `version_check` env variable to false, the command
* should exit with 0.
*
* @return void
*/
public function test_check_version_set_to_false_disables_the_check()
{
config(['monica.weekly_ping_server_url' => 'https://version.test/ping']);
config(['monica.app_version' => '2.9.0']);
config(['monica.check_version' => false]);
$fake = Http::fake([
'https://version.test/*' => Http::response([], 500),
]);
$this->artisan('monica:ping')
->assertSuccessful()
->run();
$fake->assertNothingSent();
}
}

View File

@@ -0,0 +1,78 @@
<?php
namespace Tests\Commands\Scheduling;
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 Illuminate\Support\Facades\Bus;
use App\Models\Contact\ReminderOutbox;
use App\Jobs\Reminder\NotifyUserAboutReminder;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class SendRemindersTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_schedules_a_reminder_email_job()
{
Bus::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',
]);
factory(ReminderOutbox::class)->create([
'account_id' => $account->id,
'reminder_id' => $reminder->id,
'user_id' => $user->id,
'planned_date' => '2017-01-01',
]);
$this->artisan('send:reminders')->run();
Bus::assertDispatched(NotifyUserAboutReminder::class);
}
/** @test */
public function it_doesnt_schedule_a_notification_if_it_is_not_the_right_time()
{
Bus::fake();
Carbon::setTestNow(Carbon::create(2017, 1, 1, 7, 0, 0));
$account = factory(Account::class)->create([
'default_time_reminder_is_sent' => '08: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',
]);
$reminderOutbox = factory(ReminderOutbox::class)->create([
'account_id' => $account->id,
'reminder_id' => $reminder->id,
'user_id' => $user->id,
'planned_date' => '2017-01-01',
]);
$this->artisan('send:reminders')->run();
Bus::assertNotDispatched(NotifyUserAboutReminder::class);
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace Tests\Commands\Scheduling;
use Carbon\Carbon;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use Illuminate\Support\Facades\Bus;
use App\Jobs\StayInTouch\ScheduleStayInTouch;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class SendStayInTouchTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_schedules_a_stay_in_touch_job()
{
Bus::fake();
Carbon::setTestNow(Carbon::create(2017, 1, 1, 7, 0, 0));
$account = factory(Account::class)->create([]);
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
'stay_in_touch_trigger_date' => '2017-01-01 07:00:00',
'stay_in_touch_frequency' => 30,
]);
$this->artisan('send:stay_in_touch')->run();
Bus::assertDispatched(ScheduleStayInTouch::class);
}
/** @test */
public function it_doesnt_schedule_stay_in_touch_jobs_if_no_date_is_found()
{
Bus::fake();
Carbon::setTestNow(Carbon::create(2017, 1, 1, 7, 0, 0));
$account = factory(Account::class)->create([]);
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
'stay_in_touch_trigger_date' => '2017-03-01 07:00:00',
'stay_in_touch_frequency' => 30,
]);
$this->artisan('send:stay_in_touch')->run();
Bus::assertNotDispatched(ScheduleStayInTouch::class);
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace Tests\Commands\Tests;
use Tests\TestCase;
use App\Console\Commands\Helpers\Command;
use Laravel\Passport\PersonalAccessClient;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PassportCommandTest extends TestCase
{
use DatabaseTransactions;
public function setUp(): void
{
parent::setUp();
if (! file_exists(base_path('storage/oauth-private.key')) || ! file_exists(base_path('storage/oauth-public.key'))) {
$this->markTestSkipped('Run "php artisan key:generate" before executing these tests.');
}
foreach (PersonalAccessClient::all() as $client) {
$client->delete();
}
}
/** @test */
public function passport_command_create()
{
/** @var \Tests\Helpers\CommandCallerFake */
$fake = Command::fake();
$this->artisan('monica:passport')->run();
$this->assertCount(1, $fake->buffer, $fake->buffer->implode(','));
$this->assertCommandContains($fake->buffer[0], '✓ Creating personal access client', 'php artisan passport:client');
}
/** @test */
public function passport_command_already_created()
{
/** @var \Tests\Helpers\CommandCallerFake */
$fake = Command::fake();
PersonalAccessClient::create();
$this->artisan('monica:passport')->run();
$this->assertCount(0, $fake->buffer, $fake->buffer->implode(','));
}
/** @test */
public function passport_command_env_config()
{
/** @var \Tests\Helpers\CommandCallerFake */
$fake = Command::fake();
config(['passport.private_key' => '-', 'passport.public_key' => '-']);
$this->artisan('monica:passport')->run();
$this->assertCount(1, $fake->buffer, $fake->buffer->implode(','));
$this->assertCommandContains($fake->buffer[0], '✓ Creating personal access client', 'php artisan passport:client');
}
private function assertCommandContains($array, $message, $command)
{
$this->assertStringContainsString($message, $array['message']);
$this->assertStringContainsString($command, $array['command']);
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace Tests\Commands\Tests;
use Tests\TestCase;
use Illuminate\Support\Facades\Mail;
class SendTestEmailTest extends TestCase
{
/** @test */
public function error_for_bad_email()
{
$exampleEmail = 'no.at.symbol';
$this->artisan('monica:test-email', ['--email' => $exampleEmail])
->expectsOutput("Invalid email address: \"$exampleEmail\".")
->assertFailed()
->run();
}
/** @test */
public function command_prompts_for_email()
{
$exampleEmail = 'no.at.symbol';
$this->artisan('monica:test-email')
->expectsQuestion('What email address should I send the test email to?', $exampleEmail)
->expectsOutput("Invalid email address: \"$exampleEmail\".")
->assertFailed()
->run();
}
/**
* @test
*/
public function command_attempts_to_send_email()
{
$exampleEmail = 'test@example.org';
Mail::shouldReceive('raw')
->once()
->withArgs(function ($message, $closure) use ($exampleEmail) {
$this->assertEquals(
"Hi $exampleEmail, you requested a test email from Monica.",
$message
);
return true;
});
$this->artisan('monica:test-email', ['--email' => $exampleEmail])
->assertSuccessful()
->run();
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Tests\Commands\Tests;
use Tests\TestCase;
use App\Models\User\User;
use App\Models\Account\Account;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class SetupFrontEndTestUserTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_create_a_test_user()
{
$accountCount = Account::count();
$userCount = User::count();
$this->artisan('setup:frontendtestuser')->run();
$this->assertEquals($accountCount + 1, Account::count());
$this->assertEquals($userCount + 1, User::count());
}
}