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,669 @@
<?php
namespace Tests\Unit\Models;
use App\Models\User\User;
use Tests\FeatureTestCase;
use App\Models\User\Module;
use App\Models\Account\Photo;
use App\Models\Account\Place;
use App\Models\Contact\Gender;
use App\Models\Account\Account;
use App\Models\Account\Company;
use App\Models\Account\Weather;
use App\Models\Contact\Address;
use App\Models\Contact\Contact;
use App\Models\Contact\Message;
use App\Models\Contact\Document;
use App\Models\Contact\LifeEvent;
use App\Models\Instance\AuditLog;
use App\Models\Contact\Occupation;
use Illuminate\Support\Facades\DB;
use App\Models\Account\ActivityType;
use App\Models\Contact\Conversation;
use App\Models\Contact\LifeEventType;
use App\Models\Contact\ReminderOutbox;
use App\Models\Contact\LifeEventCategory;
use App\Models\Account\ActivityTypeCategory;
use App\Models\Relationship\RelationshipType;
use App\Models\Relationship\RelationshipTypeGroup;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class AccountTest extends FeatureTestCase
{
use DatabaseTransactions;
/** @test */
public function it_has_many_genders()
{
$account = factory(Account::class)->create();
$gender = factory(Gender::class)->create([
'account_id' => $account->id,
'name' => 'test',
]);
$gender = factory(Gender::class)->create([
'account_id' => $account->id,
'name' => 'test',
]);
$this->assertTrue($account->genders()->exists());
}
/** @test */
public function it_has_many_relationship_types()
{
$account = factory(Account::class)->create();
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
]);
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($account->relationshipTypes()->exists());
}
/** @test */
public function it_has_many_relationship_type_groups()
{
$contact = factory(Contact::class)->create();
$account = $contact->account;
$relationshipTypeGroup = factory(RelationshipTypeGroup::class)->create([
'account_id' => $account->id,
]);
$relationshipTypeGroup = factory(RelationshipTypeGroup::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($account->relationshipTypeGroups()->exists());
}
/** @test */
public function it_has_many_modules()
{
$contact = factory(Contact::class)->create();
$account = $contact->account;
$module = factory(Module::class)->create([
'account_id' => $account->id,
]);
$module = factory(Module::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($account->modules()->exists());
}
/** @test */
public function it_has_many_activity_types()
{
$account = factory(Account::class)->create();
$activityType = factory(ActivityType::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($account->activityTypes()->exists());
}
/** @test */
public function it_has_many_activity_type_categories()
{
$account = factory(Account::class)->create();
$ActivityTypeCategory = factory(ActivityTypeCategory::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($account->activityTypeCategories()->exists());
}
/** @test */
public function it_has_many_conversations()
{
$account = factory(Account::class)->create([]);
$conversation = factory(Conversation::class, 2)->create([
'account_id' => $account->id,
]);
$this->assertTrue($account->conversations()->exists());
}
/** @test */
public function it_has_many_messages()
{
$account = factory(Account::class)->create([]);
$conversation = factory(Conversation::class)->create([
'account_id' => $account->id,
]);
$message = factory(Message::class, 2)->create([
'account_id' => $account->id,
'conversation_id' => $conversation->id,
]);
$this->assertTrue($account->messages()->exists());
}
/** @test */
public function it_has_many_life_event_categories()
{
$account = factory(Account::class)->create([]);
$lifeEventCategory = factory(LifeEventCategory::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($account->lifeEventCategories()->exists());
}
/** @test */
public function it_has_many_reminder_outboxes()
{
$reminderOutbox = factory(ReminderOutbox::class)->create([]);
$this->assertTrue($reminderOutbox->account->reminderOutboxes()->exists());
}
/** @test */
public function it_has_many_life_event_types()
{
$account = factory(Account::class)->create([]);
$lifeEventType = factory(LifeEventType::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($account->lifeEventTypes()->exists());
}
/** @test */
public function it_has_many_life_events()
{
$account = factory(Account::class)->create([]);
$lifeEvent = factory(LifeEvent::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($account->lifeEvents()->exists());
}
/** @test */
public function it_has_many_documents()
{
$account = factory(Account::class)->create([]);
$document = factory(Document::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($account->documents()->exists());
}
/** @test */
public function it_has_many_photos()
{
$account = factory(Account::class)->create([]);
$photo = factory(Photo::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($account->photos()->exists());
}
/** @test */
public function it_has_many_weathers()
{
$weather = factory(Weather::class)->create([]);
$this->assertTrue($weather->account->weathers()->exists());
}
/** @test */
public function it_has_many_places()
{
$account = factory(Account::class)->create([]);
$places = factory(Place::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($account->places()->exists());
}
/** @test */
public function it_has_many_addresses()
{
$account = factory(Account::class)->create([]);
$addresses = factory(Address::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($account->addresses()->exists());
}
/** @test */
public function it_has_many_companies()
{
$account = factory(Account::class)->create([]);
$companies = factory(Company::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($account->companies()->exists());
}
/** @test */
public function it_has_many_occupations()
{
$account = factory(Account::class)->create([]);
$occupations = factory(Occupation::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($account->occupations()->exists());
}
/** @test */
public function it_has_many_logs()
{
$account = factory(Account::class)->create([]);
factory(AuditLog::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($account->auditLogs()->exists());
}
/** @test */
public function user_is_subscribed_if_user_can_access_to_paid_version_for_free()
{
$account = factory(Account::class)->make([
'has_access_to_paid_version_for_free' => true,
]);
$this->assertTrue(
$account->isSubscribed()
);
}
/** @test */
public function user_is_subscribed_returns_false_if_not_subcribed()
{
$account = factory(Account::class)->make([
'has_access_to_paid_version_for_free' => false,
]);
$this->assertFalse(
$account->isSubscribed()
);
}
/** @test */
public function user_is_subscribed_returns_true_if_monthly_plan_is_set()
{
$account = factory(Account::class)->create();
$plan = factory(\Laravel\Cashier\Subscription::class)->create([
'account_id' => $account->id,
'stripe_price' => 'chandler_5',
'stripe_id' => 'sub_C0R444pbxddhW7',
'name' => 'fakePlan',
]);
config(['monica.paid_plan_monthly_friendly_name' => 'fakePlan']);
$this->assertTrue(
$account->isSubscribed()
);
}
/** @test */
public function user_is_subscribed_returns_true_if_annual_plan_is_set()
{
$account = factory(Account::class)->create();
$plan = factory(\Laravel\Cashier\Subscription::class)->create([
'account_id' => $account->id,
'stripe_price' => 'chandler_annual',
'stripe_id' => 'sub_C0R444pbxddhW7',
'name' => 'annualPlan',
]);
config(['monica.paid_plan_annual_friendly_name' => 'annualPlan']);
$this->assertTrue(
$account->isSubscribed()
);
}
/** @test */
public function user_is_subscribed_returns_false_if_no_plan_is_set()
{
$account = factory(Account::class)->create();
$this->assertFalse(
$account->isSubscribed()
);
}
/** @test */
public function has_invoices_returns_true_if_a_plan_exists()
{
$account = factory(Account::class)->create();
$plan = factory(\Laravel\Cashier\Subscription::class)->create([
'account_id' => $account->id,
'stripe_price' => 'chandler_5',
'stripe_id' => 'sub_C0R444pbxddhW7',
'name' => 'fakePlan',
]);
$this->assertTrue($account->hasInvoices());
}
/** @test */
public function has_invoices_returns_false_if_a_plan_does_not_exist()
{
$account = factory(Account::class)->create();
$this->assertFalse($account->hasInvoices());
}
/** @test */
public function it_gets_the_id_of_the_subscribed_plan()
{
config([
'monica.paid_plan_annual_friendly_name' => 'fakePlan',
'monica.paid_plan_annual_id' => 'chandler_5',
]);
$user = $this->signIn();
$account = $user->account;
$plan = factory(\Laravel\Cashier\Subscription::class)->create([
'account_id' => $account->id,
'stripe_price' => 'chandler_5',
'stripe_id' => 'sub_C0R444pbxddhW7',
'name' => 'fakePlan',
]);
$this->assertEquals(
'chandler_5',
$account->getSubscribedPlanId()
);
}
/** @test */
public function it_gets_the_friendly_name_of_the_subscribed_plan()
{
config([
'monica.paid_plan_annual_friendly_name' => 'fakePlan',
'monica.paid_plan_annual_id' => 'chandler_5',
]);
$user = $this->signIn();
$account = $user->account;
$plan = factory(\Laravel\Cashier\Subscription::class)->create([
'account_id' => $account->id,
'stripe_price' => 'chandler_5',
'stripe_id' => 'sub_C0R444pbxddhW7',
'name' => 'fakePlan',
]);
$this->assertEquals(
'fakePlan',
$account->getSubscribedPlanName()
);
}
/** @test */
public function it_populates_the_account_with_three_default_genders()
{
$account = factory(Account::class)->create();
$account->populateDefaultGendersTable();
$this->assertEquals(
3,
$account->genders->count()
);
}
/** @test */
public function it_populates_the_account_with_the_right_default_genders()
{
$account = factory(Account::class)->create();
$account->populateDefaultGendersTable();
$this->assertDatabaseHas(
'genders',
['name' => 'Man']
);
$this->assertDatabaseHas(
'genders',
['name' => 'Woman']
);
$this->assertDatabaseHas(
'genders',
['name' => 'Rather not say']
);
}
/** @test */
public function it_gets_default_time_reminder_is_sent_attribute()
{
$account = factory(Account::class)->create(['default_time_reminder_is_sent' => '14:00']);
$this->assertEquals(
'14:00',
$account->default_time_reminder_is_sent
);
}
/** @test */
public function it_sets_default_time_reminder_is_sent_attribute()
{
$account = new Account;
$account->default_time_reminder_is_sent = '14:00';
$this->assertEquals(
'14:00',
$account->default_time_reminder_is_sent
);
}
/** @test */
public function it_populates_the_account_with_two_default_reminder_rules()
{
$account = factory(Account::class)->create();
$account->populateDefaultReminderRulesTable();
$this->assertEquals(
2,
$account->reminderRules->count()
);
}
/** @test */
public function it_populates_the_account_with_the_right_default_reminder_rules()
{
$account = factory(Account::class)->create();
$account->populateDefaultReminderRulesTable();
$this->assertDatabaseHas(
'reminder_rules',
['number_of_days_before' => 7]
);
$this->assertDatabaseHas(
'reminder_rules',
['number_of_days_before' => 30]
);
}
/** @test */
public function it_gets_the_relationship_type_object_matching_a_given_name()
{
$account = factory(Account::class)->create();
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => 'partner',
]);
$this->assertInstanceOf(RelationshipType::class, $account->getRelationshipTypeByType('partner'));
}
/** @test */
public function it_gets_the_relationship_type_group_object_matching_a_given_name()
{
$account = factory(Account::class)->create();
$relationshipTypeGroup = factory(RelationshipTypeGroup::class)->create([
'account_id' => $account->id,
'name' => 'love',
]);
$this->assertInstanceOf(RelationshipTypeGroup::class, $account->getRelationshipTypeGroupByType('love'));
}
/** @test */
public function it_populates_default_relationship_type_groups_table_if_tables_havent_been_migrated_yet()
{
$account = factory(Account::class)->create();
// Love type
$id = DB::table('default_relationship_type_groups')->insertGetId([
'name' => 'friend_and_family',
]);
$account->populateRelationshipTypeGroupsTable();
$this->assertDatabaseHas('relationship_type_groups', [
'name' => 'friend_and_family',
]);
}
/** @test */
public function it_skips_default_relationship_type_groups_table_for_types_already_migrated()
{
$account = factory(Account::class)->create();
$id = DB::table('default_relationship_type_groups')->insertGetId([
'name' => 'friend_and_family',
'migrated' => 1,
]);
$account->populateRelationshipTypeGroupsTable(true);
$this->assertDatabaseMissing('relationship_type_groups', [
'name' => 'friend_and_family',
]);
}
/** @test */
public function it_populates_default_relationship_types_table_if_tables_havent_been_migrated_yet()
{
$account = factory(Account::class)->create();
$id = DB::table('default_relationship_type_groups')->insertGetId([
'name' => 'friend_and_family',
]);
DB::table('default_relationship_types')->insert([
'name' => 'fuckfriend',
'relationship_type_group_id' => $id,
]);
$account->populateRelationshipTypeGroupsTable();
$account->populateRelationshipTypesTable();
$this->assertDatabaseHas('relationship_types', [
'name' => 'fuckfriend',
]);
}
/** @test */
public function it_skips_default_relationship_types_table_for_types_already_migrated()
{
$account = factory(Account::class)->create();
$id = DB::table('default_relationship_type_groups')->insertGetId([
'name' => 'friend_and_family',
]);
DB::table('default_relationship_types')->insert([
'name' => 'fuckfriend',
'relationship_type_group_id' => $id,
'migrated' => 1,
]);
$account->populateRelationshipTypeGroupsTable();
$account->populateRelationshipTypesTable(true);
$this->assertDatabaseMissing('relationship_types', [
'name' => 'fuckfriend',
]);
}
/** @test */
public function it_create_default_account()
{
$account = Account::createDefault('John', 'Doe', 'john@doe.com', 'password');
$this->assertDatabaseHas('accounts', [
'id' => $account->id,
]);
$this->assertDatabaseHas('users', [
'account_id' => $account->id,
]);
}
/** @test */
public function it_throw_an_exception_if_user_already_exist()
{
$account = Account::createDefault('John', 'Doe', 'john@doe.com', 'password');
$this->assertDatabaseHas('accounts', [
'id' => $account->id,
]);
$this->assertDatabaseHas('users', [
'account_id' => $account->id,
]);
$this->expectException(\Illuminate\Validation\ValidationException::class);
$account = Account::createDefault('John', 'Doe', 'john@doe.com', 'password');
}
/** @test */
public function it_gets_first_user_locale()
{
$account = factory(Account::class)->create();
$user = factory(User::class)->create([
'account_id' => $account->id,
'locale' => 'fr',
]);
$user = factory(User::class)->create([
'account_id' => $account->id,
'locale' => 'en',
]);
$this->assertEquals(
'fr',
$account->getFirstLocale()
);
}
/** @test */
public function getting_first_locale_returns_null_if_user_doesnt_exist()
{
$account = factory(Account::class)->create();
$this->assertNull($account->getFirstLocale());
}
/** @test */
public function it_populates_default_life_event_tables_upon_creation()
{
$account = factory(Account::class)->create();
$user = factory(User::class)->create([
'account_id' => $account->id,
]);
$account->populateDefaultFields();
$this->assertEquals(
5,
DB::table('life_event_categories')->where('account_id', $account->id)->get()->count()
);
$this->assertEquals(
43,
DB::table('life_event_types')->where('account_id', $account->id)->get()->count()
);
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace Tests\Unit\Models;
use Carbon\Carbon;
use Tests\TestCase;
use App\Models\Account\Activity;
use App\Models\Account\ActivityType;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ActivityTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_returns_the_happened_at()
{
$activity = factory(Activity::class)->make();
$this->assertInstanceOf(
Carbon::class,
$activity->happened_at
);
}
/** @test */
public function it_returns_a_title()
{
$type = factory(ActivityType::class)->create();
$activity = factory(Activity::class)->create([
'activity_type_id' => $type->id,
]);
$this->assertEquals(
$type->translation_key,
$activity->getTitle()
);
}
/** @test */
public function it_gets_info_for_journal_entry()
{
$activity = factory(Activity::class)->create();
$data = [
'type' => 'activity',
'id' => $activity->id,
'activity_type' => (! is_null($activity->type) ? $activity->type->name : null),
'summary' => $activity->summary,
'description' => $activity->description,
'day' => $activity->happened_at->day,
'day_name' => $activity->happened_at->format('D'),
'month' => $activity->happened_at->month,
'month_name' => strtoupper($activity->happened_at->format('M')),
'year' => $activity->happened_at->year,
'attendees' => $activity->getContactsForAPI(),
];
$this->assertEquals(
$data,
$activity->getInfoForJournalEntry()
);
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Account\ActivityType;
use App\Models\Account\ActivityTypeCategory;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ActivityTypeCategoryTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$activityTypeCategory = factory(ActivityTypeCategory::class)->create([]);
$this->assertTrue($activityTypeCategory->account()->exists());
}
/** @test */
public function it_has_many_activity_types()
{
$activityTypeCategory = factory(ActivityTypeCategory::class)->create([]);
$activityType = factory(ActivityType::class, 10)->create([
'activity_type_category_id' => $activityTypeCategory->id,
]);
$this->assertTrue($activityTypeCategory->activityTypes()->exists());
}
/** @test */
public function it_gets_the_name_attribute()
{
$activityTypeCategory = factory(ActivityTypeCategory::class)->create([
'translation_key' => 'awesome_key',
'name' => null,
]);
$this->assertEquals(
'people.activity_type_category_awesome_key',
$activityTypeCategory->name
);
$activityTypeCategory = factory(ActivityTypeCategory::class)->create([
'translation_key' => null,
'name' => 'awesome_name',
]);
$this->assertEquals(
'awesome_name',
$activityTypeCategory->name
);
}
}

View File

@@ -0,0 +1,98 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Account\Activity;
use App\Models\Account\ActivityType;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ActivityTypeTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$activityType = factory(ActivityType::class)->create([]);
$this->assertTrue($activityType->account()->exists());
}
/** @test */
public function it_belongs_to_a_category()
{
$activityType = factory(ActivityType::class)->create([]);
$this->assertTrue($activityType->category()->exists());
}
/** @test */
public function it_has_many_activities()
{
$account = factory(Account::class)->create();
$activityType = factory(ActivityType::class)->create([
'account_id' => $account->id,
]);
$activity = factory(Activity::class, 2)->create([
'account_id' => $account->id,
'activity_type_id' => $activityType->id,
]);
$this->assertTrue($account->activities()->exists());
}
/** @test */
public function it_gets_the_name_attribute()
{
$activityType = factory(ActivityType::class)->create([
'translation_key' => 'awesome_key',
'name' => null,
]);
$this->assertEquals(
'people.activity_type_awesome_key',
$activityType->name
);
$activityType = factory(ActivityType::class)->create([
'translation_key' => null,
'name' => 'awesome_name',
]);
$this->assertEquals(
'awesome_name',
$activityType->name
);
}
/** @test */
public function it_resets_the_associated_activities()
{
$activityType = factory(ActivityType::class)->create([]);
$activity = factory(Activity::class, 10)->create([
'activity_type_id' => $activityType->id,
]);
$this->assertEquals(
10,
$activityType->activities()->count()
);
$this->assertDatabaseHas('activities', [
'activity_type_id' => $activityType->id,
]);
$activityType->resetAssociationWithActivities();
$this->assertDatabaseMissing('activities', [
'activity_type_id' => $activityType->id,
]);
$this->assertEquals(
0,
$activityType->activities()->count()
);
}
}

View File

@@ -0,0 +1,74 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\User\User;
use App\Models\Account\Account;
use App\Models\Account\AddressBook;
use App\Models\Account\AddressBookSubscription;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class AddressBookSubscriptionTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create();
$user = factory(User::class)->create(['account_id' => $account->id]);
$addressBookSubscription = AddressBookSubscription::factory()->create([
'account_id' => $account->id,
'user_id' => $user->id,
]);
$this->assertTrue($addressBookSubscription->account()->exists());
}
/** @test */
public function it_belongs_to_a_user()
{
$user = factory(User::class)->create();
$addressBookSubscription = AddressBookSubscription::factory()->create([
'user_id' => $user->id,
]);
$this->assertTrue($addressBookSubscription->user()->exists());
}
/** @test */
public function it_belongs_to_an_addressbook()
{
$addressBook = AddressBook::factory()->create();
$addressBookSubscription = AddressBookSubscription::factory()->create([
'address_book_id' => $addressBook->id,
]);
$this->assertTrue($addressBookSubscription->addressBook()->exists());
}
/** @test */
public function it_saves_capabilities()
{
$addressBookSubscription = new AddressBookSubscription();
$addressBookSubscription->capabilities = [
'test' => true,
];
$this->assertIsArray($addressBookSubscription->capabilities);
$this->assertEquals([
'test' => true,
], $addressBookSubscription->capabilities);
}
/** @test */
public function it_saves_password()
{
$addressBookSubscription = new AddressBookSubscription();
$addressBookSubscription->password = 'test';
$this->assertEquals('test', $addressBookSubscription->password);
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\User\User;
use App\Models\Account\Account;
use App\Models\Account\AddressBook;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class AddressBookTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create();
$user = factory(User::class)->create(['account_id' => $account->id]);
$addressBook = AddressBook::factory()->create([
'account_id' => $account->id,
'user_id' => $user->id,
]);
$this->assertTrue($addressBook->account()->exists());
}
/** @test */
public function it_belongs_to_a_user()
{
$user = factory(User::class)->create();
$addressBook = AddressBook::factory()->create([
'user_id' => $user->id,
]);
$this->assertTrue($addressBook->user()->exists());
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Contact\Address;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class AddressTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$address = factory(Address::class)->create([]);
$this->assertTrue($address->account()->exists());
}
/** @test */
public function it_belongs_to_a_contact()
{
$address = factory(Address::class)->create([]);
$this->assertTrue($address->contact()->exists());
}
/** @test */
public function it_belongs_to_a_place()
{
$address = factory(Address::class)->create([]);
$this->assertTrue($address->place()->exists());
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Tests\Unit\Models;
use Tests\ApiTestCase;
use App\Models\Contact\Contact;
use App\Models\Instance\AuditLog;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class AuditLogTest extends ApiTestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account(): void
{
$auditLog = factory(AuditLog::class)->create([]);
$this->assertTrue($auditLog->account()->exists());
}
/** @test */
public function it_belongs_to_a_user(): void
{
$auditLog = factory(AuditLog::class)->create([]);
$this->assertTrue($auditLog->author()->exists());
}
/** @test */
public function it_belongs_to_a_contact(): void
{
$contact = factory(Contact::class)->create([]);
$auditLog = factory(AuditLog::class)->create([
'about_contact_id' => $contact->id,
]);
$this->assertTrue($auditLog->contact()->exists());
}
/** @test */
public function it_returns_the_object_attribute(): void
{
$auditLog = factory(AuditLog::class)->create([]);
$this->assertEquals(
1,
$auditLog->object->{'user'}
);
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Account\Company;
use App\Models\Contact\Occupation;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class CompanyTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$company = factory(Company::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($company->account()->exists());
}
/** @test */
public function it_has_many_occupations()
{
$company = factory(Company::class)->create([]);
$occupations = factory(Occupation::class)->create([
'company_id' => $company->id,
]);
$this->assertTrue($company->occupations()->exists());
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Contact\ContactField;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ContactFieldTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_fetches_data_field()
{
$contactField = new ContactField;
$contactField->data = 'this is a test';
$this->assertEquals(
'this is a test',
$contactField->data
);
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace Tests\Unit\Models;
use Tests\FeatureTestCase;
use App\Models\Account\Account;
use App\Models\Contact\Conversation;
use App\Models\Contact\ContactFieldType;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ContactFieldTypeTest extends FeatureTestCase
{
use DatabaseTransactions;
/** @test */
public function it_has_many_conversations()
{
$contactFieldType = factory(ContactFieldType::class)->create([]);
$conversation = factory(Conversation::class, 3)->create([
'account_id' => $contactFieldType->account_id,
'contact_field_type_id' => $contactFieldType->id,
]);
$this->assertTrue($contactFieldType->conversations()->exists());
}
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$contactFieldType = factory(ContactFieldType::class)->create([]);
$conversation = factory(Conversation::class, 3)->create([
'account_id' => $account->id,
'contact_field_type_id' => $contactFieldType->id,
]);
$this->assertTrue($contactFieldType->account()->exists());
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,64 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Contact\Message;
use App\Models\Contact\Conversation;
use App\Models\Contact\ContactFieldType;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ConversationTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$conversation = factory(Conversation::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($conversation->account()->exists());
}
/** @test */
public function it_belongs_to_a_contact()
{
$contact = factory(Contact::class)->create();
$conversation = factory(Conversation::class)->create([
'contact_id' => $contact->id,
]);
$this->assertTrue($conversation->contact()->exists());
}
/** @test */
public function it_belongs_to_a_contact_field_type()
{
$account = factory(Account::class)->create([]);
$contactFieldType = factory(ContactFieldType::class)->create([
'account_id' => $account->id,
]);
$conversation = factory(Conversation::class)->create([
'contact_field_type_id' => $contactFieldType->id,
'account_id' => $account->id,
]);
$this->assertTrue($conversation->contactFieldType()->exists());
}
/** @test */
public function it_has_many_messages()
{
$conversation = factory(Conversation::class)->create();
$message = factory(Message::class)->create([
'conversation_id' => $conversation->id,
]);
$this->assertTrue($conversation->messages()->exists());
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Journal\Day;
use Illuminate\Support\Carbon;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class DayTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function get_info_for_journal_entry_that_doesnt_happen_today()
{
$day = factory(Day::class)->make();
$day->id = 1;
$day->rate = 1;
$day->comment = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor.';
$day->date = '2017-01-01 00:00:00';
$day->created_at = '2017-01-01 00:00:00';
$day->save();
$data = [
'type' => 'day',
'id' => 1,
'rate' => 1,
'comment' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor.',
'day' => 1,
'day_name' => 'Sun',
'month' => 1,
'month_name' => 'JAN',
'year' => 2017,
'happens_today' => false,
'date' => Carbon::parse('2017-01-01 00:00:00'),
];
$this->assertEquals(
$data,
$day->getInfoForJournalEntry()
);
}
/** @test */
public function get_info_for_journal_entry_that_happen_today()
{
$date = now();
$day = factory(Day::class)->make();
$day->id = 1;
$day->rate = 1;
$day->comment = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor.';
$day->date = $date;
$day->created_at = '2017-01-01 00:00:00';
$day->save();
$data = [
'type' => 'day',
'id' => 1,
'rate' => 1,
'comment' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor.',
'day' => $date->day,
'day_name' => $date->format('D'),
'month' => $date->month,
'month_name' => strtoupper($date->format('M')),
'year' => $date->year,
'happens_today' => true,
'date' => $date->addMicroseconds(-1 * $date->microsecond),
];
$this->assertEquals(
$data,
$day->getInfoForJournalEntry()
);
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Contact\Document;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class DocumentTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$document = factory(Document::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($document->account()->exists());
}
/** @test */
public function it_belongs_to_a_contact()
{
$contact = factory(Contact::class)->create();
$document = factory(Document::class)->create([
'contact_id' => $contact->id,
]);
$this->assertTrue($document->contact()->exists());
}
/** @test */
public function it_gets_the_download_link()
{
$document = factory(Document::class)->create();
$this->assertEquals(
config('app.url').'/store/'.$document->new_filename,
$document->getDownloadLink()
);
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Instance\Emotion\Emotion;
use App\Models\Instance\Emotion\PrimaryEmotion;
use App\Models\Instance\Emotion\SecondaryEmotion;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class EmotionTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function emotion_belongs_to_a_primary_emotion()
{
$emotion = factory(Emotion::class)->create([]);
$this->assertTrue($emotion->primary->exists());
$this->assertTrue($emotion->secondary->exists());
}
/** @test */
public function secondary_emotion_belongs_to_a_primary_emotion()
{
$secondaryEmotion = factory(SecondaryEmotion::class)->create([]);
$this->assertTrue($secondaryEmotion->primary->exists());
}
/** @test */
public function a_primary_emotion_has_multiple_emotions()
{
$primaryEmotion = factory(PrimaryEmotion::class)->create([]);
$secondaryEmotion = factory(SecondaryEmotion::class)->create([
'emotion_primary_id' => $primaryEmotion->id,
]);
factory(Emotion::class, 3)->create([
'emotion_primary_id' => $primaryEmotion->id,
'emotion_secondary_id' => $secondaryEmotion->id,
]);
$this->assertTrue($primaryEmotion->secondaries()->exists());
$this->assertTrue($primaryEmotion->emotions()->exists());
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Journal\Entry;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class EntryTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function get_info_for_journal_entry()
{
$entry = factory(Entry::class)->make([
'id' => 1,
'title' => 'This is the title',
'post' => 'this is a post',
'created_at' => '2017-01-01 00:00:00',
]);
$data = [
'type' => 'entry',
'id' => 1,
'title' => 'This is the title',
'post' => 'this is a post',
'day' => 1,
'day_name' => 'Sun',
'month' => 1,
'month_name' => 'JAN',
'year' => 2017,
'date' => '2017-01-01 00:00:00',
'created_at' => 'Jan 01, 2017 00:00',
];
$this->assertEquals(
$data,
$entry->getInfoForJournalEntry()
);
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Contact\Gender;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class GenderTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$gender = factory(Gender::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($gender->account()->exists());
}
/** @test */
public function it_belongs_to_many_contacts()
{
$account = factory(Account::class)->create([]);
$gender = factory(Gender::class)->create([
'account_id' => $account->id,
]);
$contact = factory(Contact::class)->create(['account_id' => $account->id, 'gender_id' => $gender->id]);
$contact = factory(Contact::class)->create(['account_id' => $account->id, 'gender_id' => $gender->id]);
$this->assertTrue($gender->contacts()->exists());
}
/** @test */
public function it_gets_the_gender_name()
{
$gender = new Gender;
$gender->name = 'Woman';
$this->assertEquals(
'Woman',
$gender->name
);
}
/** @test */
public function it_gets_the_default_gender()
{
$account = factory(Account::class)->create();
$gender = Gender::create([
'account_id' => $account->id,
'name' => 'Woman',
]);
$this->assertFalse($gender->isDefault());
$account->default_gender_id = $gender->id;
$account->save();
$gender->refresh();
$this->assertTrue($gender->isDefault());
}
}

View File

@@ -0,0 +1,120 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\User\User;
use App\Models\Contact\Gift;
use App\Models\Contact\Contact;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class GiftTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function has_particular_recipient_returns_false_if_it_s_for_no_specific_recipient()
{
$gift = factory(Gift::class)->make();
$this->assertFalse(
$gift->hasParticularRecipient()
);
}
/** @test */
public function has_particular_recipient_returns_true_if_it_s_for_a_specific_recipient()
{
$gift = factory(Gift::class)->make([
'is_for' => 1,
]);
$this->assertTrue(
$gift->hasParticularRecipient()
);
}
/** @test */
public function it_sets_is_for_attribute()
{
$gift = factory(Gift::class)->make([
'is_for' => 1,
]);
$this->assertEquals(
1,
$gift->is_for
);
}
/** @test */
public function it_gets_the_recipient_name()
{
$contact = factory(Contact::class)->create(['first_name' => 'Regis']);
$gift = factory(Gift::class)->make([
'account_id' => $contact->account_id,
'is_for' => $contact->id,
'contact_id' => $contact->id,
]);
$this->assertEquals(
'Regis',
$gift->recipient_name
);
}
/** @test */
public function it_gets_the_gift_name()
{
$gift = factory(Gift::class)->make([
'name' => 'Maison de folie',
]);
$this->assertEquals(
'Maison de folie',
$gift->name
);
}
/** @test */
public function it_gets_the_gift_url()
{
$gift = factory(Gift::class)->make([
'url' => 'https://facebook.com',
]);
$this->assertEquals(
'https://facebook.com',
$gift->url
);
}
/** @test */
public function it_gets_the_comment()
{
$gift = factory(Gift::class)->make([
'comment' => 'This is just a comment',
]);
$this->assertEquals(
'This is just a comment',
$gift->comment
);
}
/** @test */
public function it_gets_the_value()
{
$user = factory(User::class)->create();
$this->be($user);
$gift = factory(Gift::class)->make([
'account_id' => $user->account_id,
'amount' => 100,
]);
$this->assertEquals(
'100.00',
$gift->amount
);
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\User\User;
use Illuminate\Session\Store;
use App\Http\Requests\Request;
use Illuminate\Session\NullSessionHandler;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class Google2FATest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_tests_a_wrong_key_for_Google2fa()
{
$google2fa = app('pragmarx.google2fa');
$secret = $google2fa->generateSecretKey(32);
$result = $google2fa->verifyGoogle2FA($secret, 'aaaaaa');
$this->assertFalse($result);
}
/** @test */
public function it_tests_a_correct_key_for_Google2fa()
{
$google2fa = app('pragmarx.google2fa');
$secret = $google2fa->generateSecretKey(32);
$one_time_password = $google2fa->getCurrentOtp($secret);
$result = $google2fa->verifyGoogle2FA($secret, $one_time_password);
$this->assertTrue($result);
}
/** @test */
public function it_logs_in_with_Google2Fa()
{
config(['google2fa.enabled' => true]);
$google2fa = app('pragmarx.google2fa')->setStateless(false);
$secret = $google2fa->generateSecretKey(32);
$user = factory(User::class)->create();
$user->google2fa_secret = $secret;
$this->actingAs($user);
$request = $this->app['request'];
// Avoid "Session store not set on request." - Exception!
$request->setLaravelSession(new Store('test', new NullSessionHandler));
$request->getSession()->start();
$authenticator = new \PragmaRX\Google2FALaravel\Support\Authenticator($request);
$this->assertFalse($authenticator->isAuthenticated());
$this->assertTrue($google2fa->isActivated());
$google2fa->login();
$this->assertTrue($authenticator->isAuthenticated());
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Contact\Contact;
use App\Services\Instance\IdHasher;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class IdHasherTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_prepends_the_id_with_the_letter_h()
{
$idHasher = new IdHasher();
$test_id = rand();
$test_hash = $idHasher->encodeId($test_id);
$value = substr($test_hash, 0, 1);
$this->assertEquals('h', $value);
}
/** @test */
public function it_returns_the_id_back()
{
$idHasher = new IdHasher();
$test_id = rand();
$test_hash = $idHasher->encodeId($test_id);
$result_id = $idHasher->decodeId($test_hash);
$this->assertEquals($test_id, $result_id);
}
/** @test */
public function it_gets_an_exception_when_the_id_is_not_valid()
{
$idHasher = new IdHasher();
$test_id = rand();
$this->expectException(\App\Exceptions\WrongIdException::class);
$idHasher->decodeId($test_id);
}
/** @test */
public function it_decodes_the_hash_and_returns_the_right_id()
{
$idHasher = new IdHasher();
$contact = factory(Contact::class)->create();
$value = $idHasher->decodeId($contact->hashID());
$this->assertEquals($contact->id, $value);
}
}

View File

@@ -0,0 +1,347 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\User\User;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Account\ImportJob;
use Sabre\VObject\Component\VCard;
use App\Models\Contact\ContactField;
use App\Models\Account\ImportJobReport;
use Illuminate\Support\Facades\Storage;
use App\Models\Contact\ContactFieldType;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ImportJobTest extends TestCase
{
use DatabaseTransactions;
public $vcfContent = 'BEGIN:VCARD
VERSION:3.0
FN:Bono
N:;Bono;;;
EMAIL;TYPE=INTERNET:bono@example.com
TEL:+1 202-555-0191
ORG:U2
TITLE:Lead vocalist
BDAY:1960-05-10
NOTE:Lorem ipsum dolor sit amet
END:VCARD
BEGIN:VCARD
VERSION:3.0
FN:John Doe
N:Doe;John;;;
EMAIL;TYPE=INTERNET:john.doe@example.com
END:VCARD
BEGIN:VCARD
VERSION:3.0
FN:
N:;;;;
NICKNAME:Johnny
ADR:;;17 Shakespeare Ave.;Southampton;;SO17 2HB;United Kingdom
END:VCARD
';
/** @test */
public function it_belongs_to_a_user()
{
$importJob = factory(ImportJob::class)->create();
$this->assertTrue($importJob->user()->exists());
}
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$user = factory(User::class)->create([
'account_id' => $account->id,
]);
$importJob = factory(ImportJob::class)->create([
'account_id' => $account->id,
'user_id' => $user->id,
]);
$this->assertTrue($importJob->account()->exists());
}
/** @test */
public function it_belongs_to_many_reports()
{
$account = factory(Account::class)->create([]);
$user = factory(User::class)->create([
'account_id' => $account->id,
]);
$importJob = factory(ImportJob::class)->create([
'account_id' => $account->id,
'user_id' => $user->id,
]);
factory(ImportJobReport::class, 100)->create([
'import_job_id' => $importJob->id,
'account_id' => $account->id,
'user_id' => $user->id,
]);
$this->assertTrue($importJob->importJobReports()->exists());
}
/** @test */
public function it_initiates_the_job()
{
$importJob = factory(ImportJob::class)->make([]);
$this->assertNull($importJob->started_at);
$this->invokePrivateMethod($importJob, 'initJob');
$this->assertNotNull($importJob->started_at);
}
/** @test */
public function it_finalizes_the_job()
{
$importJob = factory(ImportJob::class)->make([]);
$this->assertNull($importJob->ended_at);
$this->invokePrivateMethod($importJob, 'endJob');
$this->assertNotNull($importJob->ended_at);
}
/** @test */
public function it_fails_and_throws_an_exception()
{
$importJob = factory(ImportJob::class)->create([]);
$this->invokePrivateMethod($importJob, 'fail', [
'reason',
]);
$this->assertTrue($importJob->failed);
$this->assertEquals(
'reason',
$importJob->failed_reason
);
}
/** @test */
public function it_gets_the_physical_file()
{
Storage::fake('public');
$importJob = factory(ImportJob::class)->create([
'filename' => 'testfile.vcf',
]);
Storage::disk('public')->put(
'testfile.vcf',
'fakeContent'
);
Storage::disk('public')->assertExists($importJob->filename);
$this->assertNull($importJob->physicalFile);
$this->invokePrivateMethod($importJob, 'getPhysicalFile');
$this->assertIsResource($importJob->physicalFile);
}
/** @test */
public function it_throws_an_exception_if_file_doesnt_exist()
{
Storage::fake('public', [
'throw' => true,
]);
$importJob = factory(ImportJob::class)->create([
'filename' => 'testfile.vcf',
]);
$this->invokePrivateMethod($importJob, 'getPhysicalFile');
$this->assertEquals(
trans('settings.import_vcard_file_not_found'),
$importJob->failed_reason
);
}
/** @test */
public function it_deletes_the_file()
{
Storage::fake('public');
$importJob = factory(ImportJob::class)->create([
'filename' => 'testfile.vcf',
]);
Storage::disk('public')->put(
'testfile.vcf',
'fakeContent'
);
$this->invokePrivateMethod($importJob, 'deletePhysicalFile');
Storage::disk('public')->assertMissing($importJob->filename);
}
/** @test */
public function it_calculates_how_many_entries_there_are_and_populate_the_entries_array()
{
Storage::fake('public');
$importJob = $this->createImportJob();
$importJob->filename = 'testfile.vcf';
Storage::disk('public')->put(
'testfile.vcf',
$this->vcfContent
);
$this->invokePrivateMethod($importJob, 'getPhysicalFile');
$this->invokePrivateMethod($importJob, 'getEntries');
$this->invokePrivateMethod($importJob, 'processEntries');
$this->assertJobSuccess($importJob);
$this->assertEquals(
3,
$importJob->contacts_found
);
}
/** @test */
public function it_doesnt_process_an_entry_if_import_is_not_feasible()
{
$importJob = $this->createImportJob();
$vcard = new VCard([
'TEL' => '+1 555 34567 455',
'N' => ['', '', '', '', ''],
]);
$this->invokePrivateMethod($importJob, 'processSingleEntry', [
$vcard->serialize(),
]);
$this->assertJobSuccess($importJob);
$this->assertEquals(
1,
$importJob->contacts_skipped
);
}
/** @test */
public function it_doesnt_process_an_entry_if_contact_already_exists()
{
$importJob = $this->createImportJob();
$contact = factory(Contact::class)->create([
'account_id' => $importJob->account_id,
]);
$contactFieldType = factory(ContactFieldType::class)->create([
'account_id' => $importJob->account_id,
'type' => 'email',
]);
$contactField = factory(ContactField::class)->create([
'account_id' => $importJob->account_id,
'contact_id' => $contact->id,
'contact_field_type_id' => $contactFieldType->id,
'data' => 'john@doe.com',
]);
$vcard = new VCard([
'N' => ['John', 'Doe', '', '', ''],
'EMAIL' => 'john@doe.com',
]);
$this->invokePrivateMethod($importJob, 'processSingleEntry', [$vcard]);
$this->assertJobSuccess($importJob);
$this->assertEquals(
1,
$importJob->contacts_skipped
);
}
/** @test */
public function skipping_entries_increments_counter_and_file_job_report()
{
$importJob = $this->createImportJob();
$this->invokePrivateMethod($importJob, 'skipEntry', [
'John Doe',
]);
$this->assertEquals(
1,
$importJob->contacts_skipped
);
$this->assertDatabaseHas('import_job_reports', [
'account_id' => $importJob->account_id,
'import_job_id' => $importJob->id,
]);
}
/** @test */
public function it_files_an_import_job_report()
{
$importJob = $this->createImportJob();
$vcard = new VCard([
'N' => ['John', 'Doe', '', '', ''],
'EMAIL' => 'john@doe.com',
]);
$this->invokePrivateMethod($importJob, 'fileImportJobReport', [
'Doe John john@doe.com',
$importJob::VCARD_SKIPPED,
]);
$this->assertDatabaseHas('import_job_reports', [
'account_id' => $importJob->account_id,
'user_id' => $importJob->user_id,
'import_job_id' => $importJob->id,
'contact_information' => 'Doe John john@doe.com',
'skipped' => 1,
'skip_reason' => null,
]);
$this->invokePrivateMethod($importJob, 'fileImportJobReport', [
'Doe John john@doe.com',
$importJob::VCARD_IMPORTED,
]);
$this->assertDatabaseHas('import_job_reports', [
'account_id' => $importJob->account_id,
'user_id' => $importJob->user_id,
'import_job_id' => $importJob->id,
'contact_information' => 'Doe John john@doe.com',
'skipped' => 0,
'skip_reason' => null,
]);
$this->invokePrivateMethod($importJob, 'fileImportJobReport', [
'Doe John john@doe.com',
$importJob::VCARD_SKIPPED,
'the reason why',
]);
$this->assertDatabaseHas('import_job_reports', [
'account_id' => $importJob->account_id,
'user_id' => $importJob->user_id,
'import_job_id' => $importJob->id,
'contact_information' => 'Doe John john@doe.com',
'skipped' => 1,
'skip_reason' => 'the reason why',
]);
}
private function createImportJob()
{
$account = factory(Account::class)->create([]);
$user = factory(User::class)->create([
'account_id' => $account->id,
]);
return factory(ImportJob::class)->create([
'account_id' => $account->id,
'user_id' => $user->id,
'failed' => false,
]);
}
private function assertJobSuccess($importJob)
{
$this->assertFalse($importJob->failed, 'Job has failed, reason: '.$importJob->failed_reason);
}
}

View File

@@ -0,0 +1,127 @@
<?php
namespace Tests\Unit\Models;
use Carbon\Carbon;
use Tests\TestCase;
use App\Models\Journal\Entry;
use App\Models\Account\Account;
use App\Models\Account\Activity;
use App\Models\Journal\JournalEntry;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class JournalEntryTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$task = factory(JournalEntry::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($task->account()->exists());
}
/** @test */
public function it_has_polymorphic_relations()
{
$activity = factory(Activity::class)->create();
$journalEntry = JournalEntry::add($activity);
$activity->refresh();
$this->assertNotNull($journalEntry->journalable);
$this->assertEquals($activity->id, $journalEntry->journalable_id);
$this->assertNotNull($activity->journalEntry);
$this->assertEquals($journalEntry->id, $activity->journalEntry->id);
}
/** @test */
public function it_has_polymorphic_relations2()
{
$entry = factory(Entry::class)->create();
$entry->date = '2018-01-01';
$journalEntry = JournalEntry::add($entry);
$entry->refresh();
$this->assertNotNull($journalEntry->journalable);
$this->assertEquals($entry->id, $journalEntry->journalable_id);
$this->assertNotNull($entry->journalEntry);
$this->assertEquals($journalEntry->id, $entry->journalEntry->id);
}
/** @test */
public function get_add_adds_data_of_the_right_type()
{
$activity = factory(Activity::class)->create();
$date = $activity->happened_at;
$journalEntry = JournalEntry::add($activity);
$this->assertDatabaseHas('journal_entries', [
'account_id' => $activity->account_id,
'date' => $date,
'journalable_id' => $activity->id,
'journalable_type' => 'App\Models\Account\Activity',
]);
}
/** @test */
public function get_object_data_returns_an_object()
{
$activity = factory(Activity::class)->create();
$journalEntry = JournalEntry::add($activity);
$data = [
'type' => 'activity',
'id' => $activity->id,
'activity_type' => (! is_null($activity->type) ? $activity->type->name : null),
'summary' => $activity->summary,
'description' => $activity->description,
'day' => $activity->happened_at->day,
'day_name' => $activity->happened_at->format('D'),
'month' => $activity->happened_at->month,
'month_name' => strtoupper($activity->happened_at->format('M')),
'year' => $activity->happened_at->year,
'attendees' => $activity->getContactsForAPI(),
];
$this->assertEquals(
$data,
$journalEntry->getObjectData()
);
}
/** @test */
public function get_edit_journal_entry()
{
Carbon::setTestNow(Carbon::create(2017, 1, 1, 0, 0, 0));
$entry = factory(Entry::class)->create([
'title' => 'This is the title',
'post' => 'this is a post',
]);
$entry->date = '2017-01-01';
$journalEntry = JournalEntry::add($entry);
$this->assertDatabaseHas('journal_entries', [
'account_id' => $entry->account_id,
'date' => '2017-01-01 00:00:00',
'journalable_id' => $entry->id,
'journalable_type' => 'App\Models\Journal\Entry',
]);
$entry->date = '2018-01-01';
$journalEntry->edit($entry);
$this->assertDatabaseHas('journal_entries', [
'account_id' => $entry->account_id,
'date' => '2018-01-01 00:00:00',
'journalable_id' => $entry->id,
'journalable_type' => 'App\Models\Journal\Entry',
]);
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Contact\LifeEvent;
use App\Models\Contact\LifeEventType;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class LIfeEventTypeTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$lifeEventType = factory(LifeEventType::class)->create([]);
$this->assertTrue($lifeEventType->account()->exists());
}
/** @test */
public function it_belongs_to_a_category()
{
$lifeEventType = factory(LifeEventType::class)->create([]);
$this->assertTrue($lifeEventType->lifeEventCategory()->exists());
}
/** @test */
public function it_has_many_life_events()
{
$account = factory(Account::class)->create([]);
$contact = factory(Contact::class)->create(['account_id' => $account->id]);
$lifeEventType = factory(LifeEventType::class)->create([]);
$lifeEvents = factory(LifeEvent::class, 2)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
'life_event_type_id' => $lifeEventType->id,
]);
$this->assertTrue($lifeEventType->lifeEvents()->exists());
}
/** @test */
public function it_gets_the_name_attribute()
{
$lifeEventType = factory(LifeEventType::class)->create([
'name' => 'Fake name',
]);
$this->assertEquals(
'Fake name',
$lifeEventType->name
);
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\LifeEventType;
use App\Models\Contact\LifeEventCategory;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class LifeEventCategoryTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$lifeEventCategory = factory(LifeEventCategory::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($lifeEventCategory->account()->exists());
}
/** @test */
public function it_has_many_life_event_types()
{
$lifeEventCategory = factory(LifeEventCategory::class)->create();
factory(LifeEventType::class)->create([
'life_event_category_id' => $lifeEventCategory->id,
]);
$this->assertTrue($lifeEventCategory->lifeEventTypes()->exists());
}
/** @test */
public function it_gets_name_attribute()
{
$lifeEventCategory = factory(LifeEventCategory::class)->create([
'name' => 'Fake name',
]);
$this->assertEquals(
'Fake name',
$lifeEventCategory->name
);
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Contact\Reminder;
use App\Models\Contact\LifeEvent;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class LifeEventTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$lifeEvent = factory(LifeEvent::class)->create([]);
$this->assertTrue($lifeEvent->account()->exists());
}
/** @test */
public function it_belongs_to_a_contact()
{
$lifeEvent = factory(LifeEvent::class)->create([]);
$this->assertTrue($lifeEvent->contact()->exists());
}
/** @test */
public function it_belongs_to_a_type()
{
$lifeEvent = factory(LifeEvent::class)->create([]);
$this->assertTrue($lifeEvent->lifeEventType()->exists());
}
/** @test */
public function it_has_a_reminder()
{
$lifeEvent = factory(LifeEvent::class)->create([]);
$reminder = factory(Reminder::class)->create([
'account_id' => $lifeEvent->account_id,
]);
$lifeEvent->reminder_id = $reminder->id;
$lifeEvent->save();
$this->assertTrue($lifeEvent->reminder()->exists());
}
/** @test */
public function it_gets_the_name_attribute()
{
$lifeEvent = factory(LifeEvent::class)->create([
'name' => 'Fake name',
]);
$this->assertEquals(
'Fake name',
$lifeEvent->name
);
}
/** @test */
public function it_gets_the_note_attribute()
{
$lifeEvent = factory(LifeEvent::class)->create([
'note' => 'Fake note',
]);
$this->assertEquals(
'Fake note',
$lifeEvent->note
);
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Contact\Message;
use App\Models\Contact\Conversation;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class MessageTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$message = factory(Message::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($message->account()->exists());
}
/** @test */
public function it_belongs_to_a_contact()
{
$contact = factory(Contact::class)->create();
$message = factory(Message::class)->create([
'contact_id' => $contact->id,
]);
$this->assertTrue($message->contact()->exists());
}
/** @test */
public function it_belongs_to_a_conversation()
{
$conversation = factory(Conversation::class)->create();
$message = factory(Message::class)->create([
'conversation_id' => $conversation->id,
]);
$this->assertTrue($message->conversation()->exists());
}
/** @test */
public function it_gets_the_content_attribute()
{
$message = factory(Message::class)->create([
'content' => 'This is a text',
]);
$this->assertEquals(
'This is a text',
$message->content
);
}
}

View File

@@ -0,0 +1,96 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Contact\Note;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class NoteTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$contact = factory(Contact::class)->create(['account_id' => $account->id]);
$note = factory(Note::class)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
]);
$this->assertTrue($note->account()->exists());
}
/** @test */
public function it_belongs_to_a_contact()
{
$contact = factory(Contact::class)->create([]);
$note = factory(Note::class)->create([
'contact_id' => $contact->id,
]);
$this->assertTrue($note->contact()->exists());
}
/** @test */
public function it_filters_by_favorited_notes()
{
$note = factory(Note::class)->create(['is_favorited' => true]);
$note = factory(Note::class)->create(['is_favorited' => true]);
$note = factory(Note::class)->create(['is_favorited' => false]);
$note = factory(Note::class)->create(['is_favorited' => true]);
$this->assertEquals(
3,
Note::favorited()->count()
);
}
public function testGetBodyReturnsNullIfUndefined()
{
$note = new Note;
$this->assertNull($note->getBody());
}
public function testGetBodyReturnsTextIfDefined()
{
$note = new Note;
$note->body = 'This is a text';
$this->assertEquals(
'This is a text',
$note->getBody()
);
}
public function testGetCreatedAtReturnsAFormattedDate()
{
$note = new Note;
$note->created_at = '2017-01-22 17:56:03';
$this->assertEquals(
'Jan 22, 2017',
$note->getCreatedAt()
);
}
public function testGetCreatedAtReturnsAString()
{
$note = new Note;
$note->created_at = '2017-01-22 17:56:03';
$this->assertIsString($note->getCreatedAt());
}
public function testGetContentReturnsAString()
{
$note = factory(Note::class)->make();
$this->assertIsString($note->getContent());
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Account\Company;
use App\Models\Contact\Contact;
use App\Models\Contact\Occupation;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class OccupationTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$occupation = factory(Occupation::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($occupation->account()->exists());
}
/** @test */
public function it_belongs_to_a_contact()
{
$contact = factory(Contact::class)->create([]);
$occupation = factory(Occupation::class)->create([
'contact_id' => $contact->id,
]);
$this->assertTrue($occupation->contact()->exists());
}
/** @test */
public function it_belongs_to_a_company()
{
$company = factory(Company::class)->create([]);
$occupation = factory(Occupation::class)->create([
'company_id' => $company->id,
]);
$this->assertTrue($occupation->company()->exists());
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Contact\PetCategory;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PetCategoryTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_gets_only_common_pets()
{
$petCategory = new PetCategory;
$this->assertEquals(
3,
$petCategory->common()->count()
);
}
/** @test */
public function it_gets_pet_category_name()
{
$petCategory = new PetCategory;
$petCategory->name = 'Rgis';
$this->assertEquals(
'Rgis',
$petCategory->name
);
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Contact\Pet;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Contact\PetCategory;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PetTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$contact = factory(Contact::class)->create(['account_id' => $account->id]);
$pet = factory(Pet::class)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
]);
$this->assertTrue($pet->account()->exists());
}
/** @test */
public function it_belongs_to_a_contact()
{
$contact = factory(Contact::class)->create([]);
$pet = factory(Pet::class)->create([
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
]);
$this->assertTrue($pet->contact()->exists());
}
/** @test */
public function it_belongs_to_a_pet_category()
{
$petCategory = factory(PetCategory::class)->create([]);
$pet = factory(Pet::class)->create([
'pet_category_id' => $petCategory->id,
]);
$this->assertTrue($pet->petCategory()->exists());
}
/** @test */
public function it_sets_name()
{
$pet = new Pet;
$this->assertNull($pet->name);
$pet->name = 'henri';
$this->assertEquals(
'henri',
$pet->name
);
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Account\Photo;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PhotoTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$photo = factory(Photo::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($photo->account()->exists());
}
/** @test */
public function it_belongs_to_many_contacts()
{
$contact = factory(Contact::class)->create();
$photo = factory(Photo::class)->create();
$contact->photos()->sync([$photo->id]);
$photo = factory(Photo::class)->create();
$contact->photos()->sync([$photo->id]);
$this->assertTrue($photo->contacts()->exists());
}
/** @test */
public function it_gets_the_url()
{
$photo = factory(Photo::class)->create();
$this->assertEquals(
config('app.url').'/store/'.$photo->new_filename,
$photo->url()
);
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Account\Place;
use App\Models\Account\Weather;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PlaceTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$place = factory(Place::class)->create([]);
$this->assertTrue($place->account()->exists());
}
/** @test */
public function it_has_many_weathers()
{
$weather = factory(Weather::class)->create([]);
$this->assertTrue($weather->place->weathers()->exists());
}
/** @test */
public function it_returns_the_full_address_as_a_string()
{
$place = factory(Place::class)->create([]);
$this->assertEquals(
'12 beverly hills 90210 United States',
$place->getAddressAsString()
);
}
/** @test */
public function it_returns_country_name()
{
$place = factory(Place::class)->create([]);
$this->assertEquals(
'United States',
$place->getCountryName()
);
}
/** @test */
public function it_returns_a_link_to_google_maps()
{
$place = factory(Place::class)->create([]);
$this->assertEquals(
'https://www.google.com/maps/place/'.urlencode($place->getAddressAsString()),
$place->getGoogleMapAddress()
);
}
/** @test */
public function it_returns_a_google_map_url_with_latitude_longitude()
{
$place = new Place;
$place->latitude = 24.197611;
$place->longitude = 120.780512;
$this->assertEquals(
'http://maps.google.com/maps?q=24.197611,120.780512',
$place->getGoogleMapsAddressWithLatitude()
);
}
}

View File

@@ -0,0 +1,146 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Relationship\Relationship;
use App\Models\Relationship\RelationshipType;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class RelationshipTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$relationship = factory(Relationship::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($relationship->account()->exists());
}
/** @test */
public function it_belongs_to_a_contact()
{
$contact = factory(Contact::class)->create([]);
$relationship = factory(Relationship::class)->create([
'contact_is' => $contact->id,
]);
$this->assertTrue($relationship->contactIs()->exists());
}
/** @test */
public function it_belongs_to_another_contact()
{
$contact = factory(Contact::class)->create([]);
$relationship = factory(Relationship::class)->create([
'of_contact' => $contact->id,
]);
$this->assertTrue($relationship->ofContact()->exists());
}
/** @test */
public function it_belongs_to_a_relationship_type()
{
$account = factory(Account::class)->create([]);
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
]);
$relationship = factory(Relationship::class)->create([
'account_id' => $account->id,
'relationship_type_id' => $relationshipType->id,
]);
$this->assertTrue($relationship->relationshipType()->exists());
}
/** @test */
public function it_belongs_to_a_contact_through_with_contact_field()
{
$contact = factory(Contact::class)->create([]);
$relationship = factory(Relationship::class)->create([
'of_contact' => $contact->id,
]);
$this->assertTrue($relationship->ofContact()->exists());
}
/** @test */
public function it_gets_the_reverse_relationship()
{
$account = factory(Account::class)->create();
$contactA = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$contactB = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$relationshipTypeA = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => 'uncle',
'name_reverse_relationship' => 'nephew',
]);
$relationshipA = factory(Relationship::class)->create([
'account_id' => $account->id,
'relationship_type_id' => $relationshipTypeA->id,
'contact_is' => $contactA->id,
'of_contact' => $contactB->id,
]);
$relationshipTypeB = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => 'nephew',
'name_reverse_relationship' => 'uncle',
]);
$relationshipB = factory(Relationship::class)->create([
'account_id' => $account->id,
'relationship_type_id' => $relationshipTypeB->id,
'contact_is' => $contactB->id,
'of_contact' => $contactA->id,
]);
$reverseRelationship = $relationshipA->reverseRelationship();
$this->assertEquals(
$relationshipB->id,
$reverseRelationship->id
);
$reverseReverseRelationship = $reverseRelationship->reverseRelationship();
$this->assertEquals(
$relationshipA->id,
$reverseReverseRelationship->id
);
}
/** @test */
public function it_not_gets_the_reverse_relationship()
{
$account = factory(Account::class)->create();
$contactA = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$contactB = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$relationshipTypeA = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => 'uncle',
]);
$relationshipA = factory(Relationship::class)->create([
'account_id' => $account->id,
'relationship_type_id' => $relationshipTypeA->id,
'contact_is' => $contactA->id,
'of_contact' => $contactB->id,
]);
$reverseRelationship = $relationshipA->reverseRelationship();
$this->assertNull($reverseRelationship);
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Relationship\RelationshipType;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class RelationshipTypeGroupTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($relationshipType->account()->exists());
}
}

View File

@@ -0,0 +1,182 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Relationship\RelationshipType;
use App\Models\Relationship\RelationshipTypeGroup;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class RelationshipTypeTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($relationshipType->account()->exists());
}
/** @test */
public function it_belongs_to_an_relationship_type_group()
{
$account = factory(Account::class)->create([]);
$relationshipTypeGroup = factory(RelationshipTypeGroup::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($relationshipTypeGroup->account()->exists());
}
/** @test */
public function it_gets_the_masculine_short_name_of_the_relationship_type()
{
$account = factory(Account::class)->create([]);
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => 'uncle',
'name_reverse_relationship' => 'nephew',
]);
$this->assertEquals(
'uncle',
$relationshipType->getLocalizedName()
);
}
/** @test */
public function it_gets_the_feminine_short_name_of_the_relationship_type()
{
$account = factory(Account::class)->create([]);
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => 'uncle',
'name_reverse_relationship' => 'nephew',
]);
$this->assertEquals(
'aunt',
$relationshipType->getLocalizedName(null, false, 'F')
);
}
/** @test */
public function it_gets_the_masculine_name_of_the_relationship_type_with_the_name_of_the_contact()
{
$account = factory(Account::class)->create([]);
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => 'uncle',
'name_reverse_relationship' => 'nephew',
]);
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
'first_name' => 'Mark',
'last_name' => 'Twain',
]);
$this->assertEquals(
'Mark Twains uncle',
$relationshipType->getLocalizedName($contact, false, 'M')
);
}
/** @test */
public function it_gets_the_feminine_name_of_the_relationship_type_with_the_name_of_the_contact()
{
$account = factory(Account::class)->create([]);
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => 'uncle',
'name_reverse_relationship' => 'nephew',
]);
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
'first_name' => 'Mark',
'last_name' => 'Twain',
]);
$this->assertEquals(
'Mark Twains aunt',
$relationshipType->getLocalizedName($contact, false, 'F')
);
}
/** @test */
public function it_gets_both_names_of_the_relationship_type_with_the_name_of_the_contact_and_the_opposite_version()
{
$account = factory(Account::class)->create([]);
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => 'uncle',
'name_reverse_relationship' => 'nephew',
]);
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
'first_name' => 'Mark',
'last_name' => 'Twain',
]);
$this->assertEquals(
'Mark Twains uncle/aunt',
$relationshipType->getLocalizedName($contact, true)
);
}
/** @test */
public function it_gets_only_one_name_of_the_relationship_type_if_name_and_name_reverse_are_similar()
{
$account = factory(Account::class)->create([]);
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => 'partner',
'name_reverse_relationship' => 'partner',
]);
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
'first_name' => 'Mark',
'last_name' => 'Twain',
]);
$this->assertEquals(
'Mark Twains significant other',
$relationshipType->getLocalizedName($contact, true)
);
}
/** @test */
public function it_gets_the_reverse_relationship_type()
{
$account = factory(Account::class)->create([]);
$relationshipTypeA = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => 'uncle',
'name_reverse_relationship' => 'nephew',
]);
$relationshipTypeB = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => 'nephew',
'name_reverse_relationship' => 'uncle',
]);
$reverseRelationshipType = $relationshipTypeA->reverseRelationshipType();
$this->assertEquals(
$relationshipTypeB->id,
$reverseRelationshipType->id
);
$reverseReverseRelationshipType = $reverseRelationshipType->reverseRelationshipType();
$this->assertEquals(
$relationshipTypeA->id,
$reverseReverseRelationshipType->id
);
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Contact\ReminderOutbox;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ReminderOutboxTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$reminderOutbox = factory(ReminderOutbox::class)->create([]);
$this->assertTrue($reminderOutbox->account()->exists());
}
/** @test */
public function it_belongs_to_a_reminder()
{
$reminderOutbox = factory(ReminderOutbox::class)->create([]);
$this->assertTrue($reminderOutbox->reminder()->exists());
}
/** @test */
public function it_belongs_to_a_user()
{
$reminderOutbox = factory(ReminderOutbox::class)->create([]);
$this->assertTrue($reminderOutbox->user()->exists());
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\ReminderRule;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ReminderRuleTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$reminderRule = factory(ReminderRule::class)->create(['account_id' => $account->id]);
$this->assertTrue($reminderRule->account()->exists());
}
/** @test */
public function it_gets_number_of_days_before_attribute()
{
$reminderRule = factory(ReminderRule::class)->create(['number_of_days_before' => '14']);
$this->assertEquals(
14,
$reminderRule->number_of_days_before
);
}
/** @test */
public function it_sets_number_of_days_before_attribute()
{
$reminderRule = new ReminderRule;
$reminderRule->number_of_days_before = '14';
$this->assertEquals(
14,
$reminderRule->number_of_days_before
);
}
}

View File

@@ -0,0 +1,254 @@
<?php
namespace Tests\Unit\Models;
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\Models\Contact\ReminderRule;
use App\Models\Contact\ReminderOutbox;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ReminderTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$reminder = factory(Reminder::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($reminder->account()->exists());
}
/** @test */
public function it_belongs_to_a_contact()
{
$contact = factory(Contact::class)->create([]);
$reminder = factory(Reminder::class)->create([
'contact_id' => $contact->id,
]);
$this->assertTrue($reminder->contact()->exists());
}
/** @test */
public function it_has_many_reminder_outbox()
{
$user = factory(User::class)->create([]);
$reminder = factory(Reminder::class)->create(['account_id' => $user->account_id]);
factory(ReminderOutbox::class, 3)->create([
'account_id' => $user->account_id,
'reminder_id' => $reminder->id,
'user_id' => $user->id,
]);
$this->assertTrue($reminder->reminderOutboxes()->exists());
}
/** @test */
public function it_gets_the_title_attribute()
{
$reminder = factory(Reminder::class)->create([
'title' => 'Fake name',
]);
$this->assertEquals(
'Fake name',
$reminder->title
);
}
/** @test */
public function it_gets_the_description_attribute()
{
$reminder = factory(Reminder::class)->create([
'description' => 'Fake name',
]);
$this->assertEquals(
'Fake name',
$reminder->description
);
}
/** @test */
public function it_calculates_next_expected_date()
{
$timezone = 'UTC';
$reminder = new Reminder;
$reminder->initial_date = '1980-01-01 10:10:10';
$reminder->frequency_number = 1;
Carbon::setTestNow(Carbon::create(1980, 1, 1));
$reminder->frequency_type = 'week';
$this->assertEquals(
'1980-01-08',
$reminder->calculateNextExpectedDate()->toDateString()
);
Carbon::setTestNow(Carbon::create(2017, 1, 1));
// from 1980, incrementing one week will lead to Jan 03, 2017
$reminder->frequency_type = 'week';
$this->assertEquals(
'2017-01-03',
$reminder->calculateNextExpectedDate()->toDateString()
);
$reminder->frequency_type = 'month';
$reminder->initial_date = '1980-01-01 10:10:10';
$this->assertEquals(
'2017-02-01',
$reminder->calculateNextExpectedDate()->toDateString()
);
$reminder->frequency_type = 'year';
$reminder->initial_date = '1980-01-01 10:10:10';
$this->assertEquals(
'2018-01-01',
$reminder->calculateNextExpectedDate()->toDateString()
);
Carbon::setTestNow(Carbon::create(2017, 1, 1));
$reminder->initial_date = '2016-12-25 10:10:10';
$reminder->frequency_type = 'week';
$this->assertEquals(
'2017-01-08',
$reminder->calculateNextExpectedDate()->toDateString()
);
Carbon::setTestNow(Carbon::create(2017, 1, 1));
$reminder->initial_date = '2017-02-02 10:10:10';
$reminder->frequency_type = 'week';
$this->assertEquals(
'2017-02-02',
$reminder->calculateNextExpectedDate()->toDateString()
);
}
/** @test */
public function it_calculates_next_expected_date_in_timezone()
{
config(['app.timezone' => 'Europe/Paris']);
$reminder = new Reminder;
$reminder->initial_date = '1980-05-01';
$reminder->frequency_type = 'year';
$reminder->frequency_number = 1;
Carbon::setTestNow(Carbon::create(2000, 4, 30, 21, 59, 59));
$this->assertEquals(
'2000-05-01',
$reminder->calculateNextExpectedDateOnTimezone()->toDateString()
);
Carbon::setTestNow(Carbon::create(2000, 4, 30, 22, 00, 00));
$this->assertEquals(
'2001-05-01',
$reminder->calculateNextExpectedDateOnTimezone()->toDateString()
);
}
/** @test */
public function it_schedules_a_reminder_for_one_user()
{
Carbon::setTestNow(Carbon::create(2017, 2, 1));
$user = factory(User::class)->create([]);
$reminder = factory(Reminder::class)->create([
'account_id' => $user->account_id,
'initial_date' => '2017-01-01',
'frequency_type' => 'year',
'frequency_number' => 1,
]);
$reminder->schedule($user);
$this->assertDatabaseHas('reminder_outbox', [
'reminder_id' => $reminder->id,
'planned_date' => '2018-01-01',
'nature' => 'reminder',
'user_id' => $user->id,
]);
}
/** @test */
public function scheduling_a_reminder_also_schedules_notifications_for_one_user()
{
Carbon::setTestNow(Carbon::create(2017, 2, 1));
$user = factory(User::class)->create([]);
$reminder = factory(Reminder::class)->create([
'account_id' => $user->account_id,
'initial_date' => '2017-01-01',
'frequency_type' => 'year',
'frequency_number' => 1,
]);
$reminderRule = factory(ReminderRule::class)->create([
'account_id' => $reminder->account_id,
'number_of_days_before' => 30,
'active' => 1,
]);
$reminderRule = factory(ReminderRule::class)->create([
'account_id' => $reminder->account_id,
'number_of_days_before' => 7,
'active' => 1,
]);
$reminder->schedule($user);
$this->assertDatabaseHas('reminder_outbox', [
'reminder_id' => $reminder->id,
'planned_date' => '2017-12-02',
'nature' => 'notification',
'notification_number_days_before' => 30,
]);
$this->assertDatabaseHas('reminder_outbox', [
'reminder_id' => $reminder->id,
'planned_date' => '2017-12-25',
'nature' => 'notification',
'notification_number_days_before' => 7,
'user_id' => $user->id,
]);
$this->assertEquals(
3,
$reminder->reminderOutboxes()->count()
);
}
/** @test */
public function it_doesnt_schedule_a_notification_if_date_is_too_close_to_present_date()
{
Carbon::setTestNow(Carbon::create(2017, 2, 1));
$user = factory(User::class)->create([]);
$reminder = factory(Reminder::class)->create([
'account_id' => $user->account_id,
'initial_date' => '2017-01-01',
'frequency_type' => 'week',
'frequency_number' => 1,
]);
$reminderRule = factory(ReminderRule::class)->create([
'account_id' => $reminder->account_id,
'number_of_days_before' => 7,
'active' => 1,
]);
$reminder->schedule($user);
$this->assertDatabaseMissing('reminder_outbox', [
'reminder_id' => $reminder->id,
'nature' => 'notification',
]);
$this->assertEquals(
1,
$reminder->reminderOutboxes()->count()
);
}
}

View File

@@ -0,0 +1,196 @@
<?php
namespace Tests\Unit\Models;
use Carbon\Carbon;
use Tests\FeatureTestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Instance\SpecialDate;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class SpecialDateTest extends FeatureTestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$specialDate = factory(SpecialDate::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($specialDate->account()->exists());
}
/** @test */
public function it_belongs_to_a_contact()
{
$account = factory(Account::class)->create([]);
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$specialDate = factory(SpecialDate::class)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
]);
$this->assertTrue($specialDate->contact()->exists());
}
/** @test */
public function get_age_returns_null_if_no_date_is_set()
{
$specialDate = new SpecialDate;
$this->assertNull($specialDate->getAge());
}
/** @test */
public function get_age_returns_null_if_year_is_unknown()
{
$specialDate = factory(SpecialDate::class)->make();
$specialDate->is_year_unknown = 1;
$specialDate->save();
$this->assertNull($specialDate->getAge());
}
/** @test */
public function get_age_returns_age()
{
Carbon::setTestNow(Carbon::create(2020, 2, 17, 17, 0, 0));
$specialDate = factory(SpecialDate::class)->make();
$specialDate->is_year_unknown = 0;
$specialDate->date = now()->subYears(5);
$specialDate->save();
$this->assertEquals(
5,
$specialDate->getAge()
);
}
/** @test */
public function create_from_age_sets_the_right_date()
{
$specialDate = factory(SpecialDate::class)->make();
$specialDate->createFromAge(100);
$this->assertTrue(
$specialDate->is_age_based
);
$this->assertEquals(
1,
$specialDate->date->day
);
$this->assertEquals(
1,
$specialDate->date->month
);
}
/** @test */
public function create_from_date_creates_an_approximate_date()
{
$specialDate = factory(SpecialDate::class)->make();
$specialDate->createFromDate(0, 10, 10);
$this->assertTrue(
$specialDate->is_year_unknown
);
$this->assertEquals(
10,
$specialDate->date->day
);
$this->assertEquals(
10,
$specialDate->date->month
);
$this->assertEquals(
now()->year,
$specialDate->date->year
);
}
/** @test */
public function create_from_date_creates_an_exact_date()
{
$specialDate = factory(SpecialDate::class)->make();
$specialDate->createFromDate(2019, 10, 10);
$this->assertFalse(
$specialDate->is_year_unknown
);
$this->assertEquals(
10,
$specialDate->date->day
);
$this->assertEquals(
10,
$specialDate->date->month
);
$this->assertEquals(
2019,
$specialDate->date->year
);
}
/** @test */
public function set_contact_sets_the_contact_information()
{
$specialDate = factory(SpecialDate::class)->make();
$contact = factory(Contact::class)->create();
$specialDate->setToContact($contact);
$this->assertEquals(
$contact->account_id,
$specialDate->account_id
);
$this->assertEquals(
$contact->id,
$specialDate->contact_id
);
}
/** @test */
public function to_short_string_returns_date_with_year()
{
$specialDate = new SpecialDate;
$specialDate->is_year_unknown = false;
$specialDate->date = Carbon::create(2001, 5, 21);
$this->assertEquals(
'May 21, 2001',
$specialDate->toShortString()
);
}
/** @test */
public function to_short_string_returns_date_without_year()
{
$specialDate = new SpecialDate;
$specialDate->is_year_unknown = true;
$specialDate->date = Carbon::create(2001, 5, 21);
$this->assertEquals(
'May 21',
$specialDate->toShortString()
);
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Contact\Tag;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class TagTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$contact = factory(Contact::class)->create(['account_id' => $account->id]);
$tag = factory(Tag::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($tag->account()->exists());
}
/** @test */
public function it_belongs_to_many_contacts()
{
$account = factory(Account::class)->create([]);
$contact = factory(Contact::class)->create(['account_id' => $account->id]);
$tag = factory(Tag::class)->create(['account_id' => $account->id]);
$contact->tags()->sync([$tag->id => ['account_id' => $account->id]]);
$contact = factory(Contact::class)->create(['account_id' => $account->id]);
$tag = factory(Tag::class)->create(['account_id' => $account->id]);
$contact->tags()->sync([$tag->id => ['account_id' => $account->id]]);
$this->assertTrue($tag->contacts()->exists());
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Contact\Task;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class TaskTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$contact = factory(Contact::class)->create(['account_id' => $account->id]);
$task = factory(Task::class)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
]);
$this->assertTrue($task->account()->exists());
}
/** @test */
public function it_belongs_to_a_contact()
{
$account = factory(Account::class)->create([]);
$contact = factory(Contact::class)->create(['account_id' => $account->id]);
$task = factory(Task::class)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
]);
$this->assertTrue($task->contact()->exists());
}
/** @test */
public function it_filters_by_completed_items()
{
$task = factory(Task::class)->create(['completed' => true]);
$task = factory(Task::class)->create(['completed' => true]);
$task = factory(Task::class)->create(['completed' => false]);
$task = factory(Task::class)->create(['completed' => true]);
$this->assertEquals(
3,
Task::completed()->count()
);
}
/** @test */
public function it_filters_by_incomplete_items()
{
$task = factory(Task::class)->create(['completed' => false]);
$task = factory(Task::class)->create(['completed' => true]);
$task = factory(Task::class)->create(['completed' => true]);
$task = factory(Task::class)->create(['completed' => true]);
$this->assertEquals(
1,
Task::inProgress()->count()
);
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\User\User;
use App\Models\Settings\Term;
use App\Models\Account\Account;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class TermTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_many_users()
{
$account = factory(Account::class)->create([]);
$user = factory(User::class)->create(['account_id' => $account->id]);
$term = factory(Term::class)->create([]);
$term->users()->sync([$user->id => ['account_id' => $account->id]]);
$user = factory(User::class)->create(['account_id' => $account->id]);
$term = factory(Term::class)->create([]);
$term->users()->sync([$user->id => ['account_id' => $account->id]]);
$this->assertTrue($term->users()->exists());
}
}

View File

@@ -0,0 +1,453 @@
<?php
namespace Tests\Unit\Models;
use Carbon\Carbon;
use Tests\TestCase;
use App\Models\User\User;
use App\Models\Settings\Term;
use App\Models\Account\Account;
use App\Models\Contact\Reminder;
use App\Models\Settings\Currency;
use App\Services\User\CreateUser;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Notification;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class UserTest extends TestCase
{
use DatabaseTransactions;
private function createUser($account_id, $first_name, $last_name, $email, $password)
{
return app(CreateUser::class)->execute([
'account_id' => $account_id,
'first_name' => $first_name,
'last_name' => $last_name,
'email' => $email,
'password' => $password,
]);
}
/** @test */
public function it_belongs_to_account()
{
$account = factory(Account::class)->create([]);
$user = factory(User::class)->create(['account_id' => $account->id]);
$this->assertTrue($user->account()->exists());
}
/** @test */
public function it_belongs_to_many_terms()
{
$account = factory(Account::class)->create([]);
$user = factory(User::class)->create(['account_id' => $account->id]);
$term = factory(Term::class)->create();
$user->terms()->sync([$term->id => ['account_id' => $account->id]]);
$user = factory(User::class)->create(['account_id' => $account->id]);
$term = factory(Term::class)->create();
$user->terms()->sync([$term->id => ['account_id' => $account->id]]);
$this->assertTrue($user->terms()->exists());
}
/** @test */
public function name_accessor_returns_name_in_the_user_preferred_way()
{
$user = new User;
$user->first_name = 'John';
$user->last_name = 'Doe';
$user->name_order = 'firstname_lastname';
$this->assertEquals(
$user->name,
'John Doe'
);
$user->name_order = 'lastname_firstname';
$this->assertEquals(
$user->name,
'Doe John'
);
}
/** @test */
public function it_gets_2fa_secret_attribute()
{
$user = new User;
$this->assertNull($user->getGoogle2faSecretAttribute(null));
$string = 'pass1234';
$this->assertEquals(
$string,
$user->getGoogle2faSecretAttribute(encrypt($string))
);
}
/** @test */
public function it_gets_fluid_layout()
{
$user = new User;
$user->fluid_container = true;
$this->assertEquals(
'container-fluid',
$user->getFluidLayout()
);
$user->fluid_container = false;
$this->assertEquals(
'container',
$user->getFluidLayout()
);
}
/** @test */
public function it_gets_the_locale()
{
$user = new User;
$user->locale = 'en';
$this->assertEquals(
'en',
$user->locale
);
}
/** @test */
public function user_should_not_be_reminded_because_dates_are_different()
{
Carbon::setTestNow(Carbon::create(2017, 1, 1));
$account = factory(Account::class)->create();
$user = factory(User::class)->create(['account_id' => $account->id]);
$reminder = factory(Reminder::class)->create([
'account_id' => $account->id,
'initial_date' => '2018-02-01',
]);
$this->assertFalse($user->isTheRightTimeToBeReminded($reminder->initial_date));
}
/** @test */
public function user_should_not_be_reminded_because_hours_are_different()
{
Carbon::setTestNow(Carbon::create(2017, 1, 1, 7, 0, 0));
$account = factory(Account::class)->create(['default_time_reminder_is_sent' => '08:00']);
$user = factory(User::class)->create(['account_id' => $account->id]);
$reminder = factory(Reminder::class)->create([
'account_id' => $account->id,
'initial_date' => '2017-01-01',
]);
$this->assertFalse($user->isTheRightTimeToBeReminded($reminder->initial_date));
}
/** @test */
public function user_should_not_be_reminded_because_timezone_is_different()
{
Carbon::setTestNow(Carbon::create(2017, 1, 1, 7, 0, 0));
$account = factory(Account::class)->create(['default_time_reminder_is_sent' => '07:00']);
$user = factory(User::class)->create([
'account_id' => $account->id,
'timezone' => 'Europe/Paris',
]);
$reminder = factory(Reminder::class)->create([
'account_id' => $account->id,
'initial_date' => '2017-01-01',
]);
$this->assertFalse($user->isTheRightTimeToBeReminded($reminder->initial_date));
}
/** @test */
public function user_should_be_reminded()
{
Carbon::setTestNow(Carbon::create(2017, 1, 1, 7, 32, 12));
$account = factory(Account::class)->create(['default_time_reminder_is_sent' => '07:00']);
$user = factory(User::class)->create(['account_id' => $account->id]);
$reminder = factory(Reminder::class)->create([
'account_id' => $account->id,
'initial_date' => '2017-01-01',
]);
$this->assertTrue($user->isTheRightTimeToBeReminded($reminder->initial_date));
}
/** @test */
public function it_creates_default_user_en()
{
App::setLocale('en');
$account = factory(Account::class)->create([]);
$user = $this->createUser($account->id, 'John', 'Doe', 'john@doe.com', 'password');
$currency = Currency::where('iso', 'USD')->first();
$this->assertDatabaseHas('users', [
'id' => $user->id,
'account_id' => $account->id,
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'john@doe.com',
'locale' => 'en',
'timezone' => 'America/Chicago',
'currency_id' => $currency->id,
'temperature_scale' => 'fahrenheit',
]);
}
/** @test */
public function it_creates_default_user_fr()
{
App::setLocale('fr');
$account = factory(Account::class)->create([]);
$user = $this->createUser($account->id, 'John', 'Doe', 'john@doe.com', 'password');
$currency = Currency::where('iso', 'EUR')->first();
$this->assertDatabaseHas('users', [
'id' => $user->id,
'account_id' => $account->id,
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'john@doe.com',
'locale' => 'fr',
'timezone' => 'Europe/Paris',
'currency_id' => $currency->id,
'temperature_scale' => 'celsius',
]);
}
/** @test */
public function it_creates_default_user_cs()
{
App::setLocale('cs');
$account = factory(Account::class)->create([]);
$user = $this->createUser($account->id, 'John', 'Doe', 'john@doe.com', 'password');
$currency = Currency::where('iso', 'CZK')->first();
$this->assertDatabaseHas('users', [
'id' => $user->id,
'account_id' => $account->id,
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'john@doe.com',
'locale' => 'cs',
'timezone' => 'Europe/Prague',
'currency_id' => $currency->id,
'temperature_scale' => 'celsius',
]);
}
/** @test */
public function it_creates_default_user_de()
{
App::setLocale('de');
$account = factory(Account::class)->create([]);
$user = $this->createUser($account->id, 'John', 'Doe', 'john@doe.com', 'password');
$currency = Currency::where('iso', 'EUR')->first();
$this->assertDatabaseHas('users', [
'id' => $user->id,
'account_id' => $account->id,
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'john@doe.com',
'locale' => 'de',
'timezone' => 'Europe/Berlin',
'currency_id' => $currency->id,
'temperature_scale' => 'celsius',
]);
}
/** @test */
public function it_creates_default_user_es()
{
App::setLocale('es');
$account = factory(Account::class)->create([]);
$user = $this->createUser($account->id, 'John', 'Doe', 'john@doe.com', 'password');
$currency = Currency::where('iso', 'EUR')->first();
$this->assertDatabaseHas('users', [
'id' => $user->id,
'account_id' => $account->id,
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'john@doe.com',
'locale' => 'es',
'timezone' => 'Europe/Madrid',
'currency_id' => $currency->id,
'temperature_scale' => 'celsius',
]);
}
/** @test */
public function it_creates_default_user_he()
{
App::setLocale('he');
$account = factory(Account::class)->create([]);
$user = $this->createUser($account->id, 'John', 'Doe', 'john@doe.com', 'password');
$currency = Currency::where('iso', 'ILS')->first();
$this->assertDatabaseHas('users', [
'id' => $user->id,
'account_id' => $account->id,
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'john@doe.com',
'locale' => 'he',
'timezone' => 'Asia/Jerusalem',
'currency_id' => $currency->id,
'temperature_scale' => 'celsius',
]);
}
/** @test */
public function it_creates_default_user_it()
{
App::setLocale('it');
$account = factory(Account::class)->create([]);
$user = $this->createUser($account->id, 'John', 'Doe', 'john@doe.com', 'password');
$currency = Currency::where('iso', 'EUR')->first();
$this->assertDatabaseHas('users', [
'id' => $user->id,
'account_id' => $account->id,
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'john@doe.com',
'locale' => 'it',
'timezone' => 'Europe/Rome',
'currency_id' => $currency->id,
'temperature_scale' => 'celsius',
]);
}
/** @test */
public function it_creates_default_user_nl()
{
App::setLocale('nl');
$account = factory(Account::class)->create([]);
$user = $this->createUser($account->id, 'John', 'Doe', 'john@doe.com', 'password');
$currency = Currency::where('iso', 'EUR')->first();
$this->assertDatabaseHas('users', [
'id' => $user->id,
'account_id' => $account->id,
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'john@doe.com',
'locale' => 'nl',
'timezone' => 'Europe/Amsterdam',
'currency_id' => $currency->id,
'temperature_scale' => 'celsius',
]);
}
/** @test */
public function it_creates_default_user_pt()
{
App::setLocale('pt');
$account = factory(Account::class)->create([]);
$user = $this->createUser($account->id, 'John', 'Doe', 'john@doe.com', 'password');
$currency = Currency::where('iso', 'EUR')->first();
$this->assertDatabaseHas('users', [
'id' => $user->id,
'account_id' => $account->id,
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'john@doe.com',
'locale' => 'pt',
'timezone' => 'Europe/Lisbon',
'currency_id' => $currency->id,
'temperature_scale' => 'celsius',
]);
}
/** @test */
public function it_creates_default_user_ru()
{
App::setLocale('ru');
$account = factory(Account::class)->create([]);
$user = $this->createUser($account->id, 'John', 'Doe', 'john@doe.com', 'password');
$currency = Currency::where('iso', 'RUB')->first();
$this->assertDatabaseHas('users', [
'id' => $user->id,
'account_id' => $account->id,
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'john@doe.com',
'locale' => 'ru',
'timezone' => 'Europe/Moscow',
'currency_id' => $currency->id,
'temperature_scale' => 'celsius',
]);
}
/** @test */
public function it_creates_default_user_zh()
{
App::setLocale('zh');
$account = factory(Account::class)->create([]);
$user = $this->createUser($account->id, 'John', 'Doe', 'john@doe.com', 'password');
$currency = Currency::where('iso', 'CNY')->first();
$this->assertDatabaseHas('users', [
'id' => $user->id,
'account_id' => $account->id,
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'john@doe.com',
'locale' => 'zh',
'timezone' => 'Asia/Shanghai',
'currency_id' => $currency->id,
'temperature_scale' => 'celsius',
]);
}
/** @test */
public function it_sends_a_verification_email()
{
config(['monica.signup_double_optin' => true]);
Notification::fake();
// Creating a fake account
factory(Account::class)->create();
$user = factory(User::class)->create([]);
$user->sendEmailVerificationNotification();
Notification::assertSentTo(
[$user], VerifyEmail::class
);
}
/** @test */
public function it_doesnt_send_a_verification_email_if_the_double_optin_is_disabled_at_the_instance_level()
{
config(['monica.signup_double_optin' => false]);
Notification::fake();
$user = factory(User::class)->create([]);
$user->sendEmailVerificationNotification();
Notification::assertNothingSent();
}
}

View File

@@ -0,0 +1,96 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Account\Weather;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class WeatherTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_belongs_to_an_account()
{
$account = factory(Account::class)->create([]);
$weather = factory(Weather::class)->create([
'account_id' => $account->id,
]);
$this->assertTrue($weather->account()->exists());
}
/** @test */
public function it_belongs_to_a_place()
{
$weather = factory(Weather::class)->create([]);
$this->assertTrue($weather->place()->exists());
}
/** @test */
public function it_gets_current_temperature()
{
$weather = factory(Weather::class)->create();
$this->assertEquals(
13,
$weather->temperature()
);
}
/** @test */
public function it_gets_current_temperature_in_celsius()
{
$weather = factory(Weather::class)->create();
$this->assertEquals(
13,
$weather->temperature('celsius')
);
}
/** @test */
public function it_gets_current_temperature_in_fahrenheit()
{
$weather = factory(Weather::class)->create();
$this->assertEquals(
55.4,
$weather->temperature('fahrenheit')
);
}
/** @test */
public function it_gets_current_summary()
{
$weather = factory(Weather::class)->create();
$this->assertEquals(
'Partly cloudy',
$weather->summary
);
}
/** @test */
public function it_gets_current_code()
{
$weather = factory(Weather::class)->create();
$this->assertEquals(
'partly-cloudy-night',
$weather->summary_code
);
}
/** @test */
public function it_gets_weather_emoji()
{
$weather = factory(Weather::class)->create();
$this->assertEquals(
'🎑',
$weather->emoji
);
}
}