refactor: replace custom CRM with Monica fork
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s
This commit is contained in:
264
tests/Unit/Helpers/AccountHelperTest.php
Normal file
264
tests/Unit/Helpers/AccountHelperTest.php
Normal file
@@ -0,0 +1,264 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Tests\TestCase;
|
||||
use App\Models\User\User;
|
||||
use App\Models\Contact\Call;
|
||||
use App\Helpers\AccountHelper;
|
||||
use App\Models\Contact\Gender;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Account\Activity;
|
||||
use App\Models\Contact\Reminder;
|
||||
use App\Models\Account\Invitation;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class AccountHelperTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function user_has_limitations_if_not_subscribed_or_exempted_of_subscriptions(): void
|
||||
{
|
||||
$account = factory(Account::class)->make([
|
||||
'has_access_to_paid_version_for_free' => true,
|
||||
]);
|
||||
|
||||
$this->assertFalse(AccountHelper::hasLimitations($account));
|
||||
|
||||
// Check that if the ENV variable REQUIRES_SUBSCRIPTION has an effect
|
||||
$account = factory(Account::class)->make([
|
||||
'has_access_to_paid_version_for_free' => false,
|
||||
]);
|
||||
|
||||
config(['monica.requires_subscription' => false]);
|
||||
|
||||
$this->assertFalse(AccountHelper::hasLimitations($account));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function account_has_reached_contact_limit_on_free_plan(): void
|
||||
{
|
||||
$account = factory(Account::class)->create();
|
||||
factory(Contact::class, 2)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
config(['monica.number_of_allowed_contacts_free_account' => 1]);
|
||||
$this->assertTrue(AccountHelper::hasReachedContactLimit($account));
|
||||
$this->assertFalse(AccountHelper::isBelowContactLimit($account));
|
||||
|
||||
factory(Contact::class)->state('partial')->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
config(['monica.number_of_allowed_contacts_free_account' => 3]);
|
||||
$this->assertFalse(AccountHelper::hasReachedContactLimit($account));
|
||||
$this->assertTrue(AccountHelper::isBelowContactLimit($account));
|
||||
|
||||
config(['monica.number_of_allowed_contacts_free_account' => 100]);
|
||||
$this->assertFalse(AccountHelper::hasReachedContactLimit($account));
|
||||
$this->assertTrue(AccountHelper::isBelowContactLimit($account));
|
||||
|
||||
$account = factory(Account::class)->create();
|
||||
factory(Contact::class, 2)->create([
|
||||
'account_id' => $account->id,
|
||||
'is_active' => false,
|
||||
]);
|
||||
factory(Contact::class, 3)->create([
|
||||
'account_id' => $account->id,
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
config(['monica.number_of_allowed_contacts_free_account' => 3]);
|
||||
$this->assertTrue(AccountHelper::hasReachedContactLimit($account));
|
||||
$this->assertTrue(AccountHelper::isBelowContactLimit($account));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function user_can_downgrade_with_only_one_user_and_no_pending_invitations_and_under_contact_limit(): void
|
||||
{
|
||||
config(['monica.number_of_allowed_contacts_free_account' => 1]);
|
||||
$contact = factory(Contact::class)->create();
|
||||
|
||||
factory(User::class)->create([
|
||||
'account_id' => $contact->account_id,
|
||||
]);
|
||||
|
||||
$this->assertTrue(AccountHelper::canDowngrade($contact->account));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function user_cant_downgrade_with_two_users(): void
|
||||
{
|
||||
$contact = factory(Contact::class)->create();
|
||||
|
||||
factory(User::class, 3)->create([
|
||||
'account_id' => $contact->account_id,
|
||||
]);
|
||||
|
||||
$this->assertFalse(AccountHelper::canDowngrade($contact->account));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function user_cant_downgrade_with_pending_invitations(): void
|
||||
{
|
||||
$account = factory(Account::class)->create();
|
||||
|
||||
factory(Invitation::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$this->assertFalse(AccountHelper::canDowngrade($account));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function user_cant_downgrade_with_too_many_contacts(): void
|
||||
{
|
||||
config(['monica.number_of_allowed_contacts_free_account' => 1]);
|
||||
$account = factory(Account::class)->create();
|
||||
|
||||
factory(Contact::class, 2)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$this->assertFalse(AccountHelper::canDowngrade($account));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_gets_the_default_gender_for_the_account(): void
|
||||
{
|
||||
$account = factory(Account::class)->create();
|
||||
|
||||
$this->assertEquals(Gender::UNKNOWN, AccountHelper::getDefaultGender($account));
|
||||
|
||||
$gender = factory(Gender::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
$account->default_gender_id = $gender->id;
|
||||
$account->save();
|
||||
|
||||
$this->assertEquals($gender->type, AccountHelper::getDefaultGender($account));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function get_reminders_for_month_returns_no_reminders(): void
|
||||
{
|
||||
$account = factory(Account::class)->create();
|
||||
$user = factory(User::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
Carbon::setTestNow(Carbon::create(2017, 1, 1));
|
||||
factory(Reminder::class, 3)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
// check if there are reminders for the month of March
|
||||
$this->actingAs($user)->assertCount(0, AccountHelper::getUpcomingRemindersForMonth($account, 3));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function get_reminders_for_month_returns_reminders_for_given_month(): void
|
||||
{
|
||||
$account = factory(Account::class)->create();
|
||||
$user = factory(User::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
Carbon::setTestNow(Carbon::create(2017, 1, 1));
|
||||
|
||||
// add 3 reminders for the month of March
|
||||
for ($i = 0; $i < 3; $i++) {
|
||||
$reminder = factory(Reminder::class)->create([
|
||||
'account_id' => $account->id,
|
||||
'initial_date' => '2017-03-03 00:00:00',
|
||||
]);
|
||||
|
||||
$reminder->schedule($user);
|
||||
}
|
||||
|
||||
$this->actingAs($user)->assertCount(3, AccountHelper::getUpcomingRemindersForMonth($account, 2));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function get_reminders_for_month_returns_reminders_for_current_user_only(): void
|
||||
{
|
||||
$account = factory(Account::class)->create();
|
||||
$user1 = factory(User::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
$user2 = factory(User::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
Carbon::setTestNow(Carbon::create(2017, 1, 1));
|
||||
|
||||
// add 3 reminders for the month of March
|
||||
for ($i = 0; $i < 3; $i++) {
|
||||
$reminder = factory(Reminder::class)->create([
|
||||
'account_id' => $account->id,
|
||||
'initial_date' => '2017-03-03 00:00:00',
|
||||
]);
|
||||
|
||||
$reminder->schedule($user1);
|
||||
$reminder->schedule($user2);
|
||||
}
|
||||
|
||||
$this->actingAs($user1)->assertCount(3, AccountHelper::getUpcomingRemindersForMonth($account, 2));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_retrieves_yearly_activities_statistics(): void
|
||||
{
|
||||
$account = factory(Account::class)->create();
|
||||
factory(Activity::class, 4)->create([
|
||||
'account_id' => $account->id,
|
||||
'happened_at' => '2018-03-02',
|
||||
]);
|
||||
|
||||
factory(Activity::class, 2)->create([
|
||||
'account_id' => $account->id,
|
||||
'happened_at' => '1992-03-02',
|
||||
]);
|
||||
|
||||
$statistics = AccountHelper::getYearlyActivitiesStatistics($account);
|
||||
|
||||
$this->assertEquals(
|
||||
[
|
||||
1992 => 2,
|
||||
2018 => 4,
|
||||
],
|
||||
$statistics->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_retrieves_yearly_call_statistics(): void
|
||||
{
|
||||
$contact = factory(Contact::class)->create();
|
||||
factory(Call::class, 4)->create([
|
||||
'account_id' => $contact->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'called_at' => '2018-03-02',
|
||||
]);
|
||||
|
||||
factory(Call::class, 2)->create([
|
||||
'account_id' => $contact->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'called_at' => '1992-03-02',
|
||||
]);
|
||||
|
||||
$statistics = AccountHelper::getYearlyCallStatistics($contact->account);
|
||||
|
||||
$this->assertEquals(
|
||||
[
|
||||
1992 => 2,
|
||||
2018 => 4,
|
||||
],
|
||||
$statistics->toArray()
|
||||
);
|
||||
}
|
||||
}
|
||||
65
tests/Unit/Helpers/AuditLogHelperTest.php
Normal file
65
tests/Unit/Helpers/AuditLogHelperTest.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\User\User;
|
||||
use App\Helpers\AuditLogHelper;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Instance\AuditLog;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class AuditLogHelperTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_prepares_a_collection_of_audit_logs_for_the_settings_page()
|
||||
{
|
||||
$user = factory(User::class)->create([]);
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $user->account_id,
|
||||
'first_name' => 'roger',
|
||||
'last_name' => 'moore',
|
||||
]);
|
||||
|
||||
factory(AuditLog::class, 2)->create([
|
||||
'account_id' => $user->account_id,
|
||||
'about_contact_id' => $contact->id,
|
||||
'objects' => '{"contact_name":"'.$contact->name.'","contact_id":'.$contact->id.'}',
|
||||
]);
|
||||
|
||||
$logs = $user->account->auditLogs;
|
||||
$collection = AuditLogHelper::getCollectionOfAudits($logs);
|
||||
|
||||
$this->assertEquals(
|
||||
2,
|
||||
$collection->count()
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_prepares_a_collection_of_audit_logs_without_likns_for_the_settings_page()
|
||||
{
|
||||
$user = factory(User::class)->create([]);
|
||||
|
||||
factory(AuditLog::class, 2)->create([
|
||||
'account_id' => $user->account_id,
|
||||
'about_contact_id' => null,
|
||||
'objects' => '{"contact_name":"roger moore","contact_id":123456789}',
|
||||
]);
|
||||
|
||||
$logs = $user->account->auditLogs;
|
||||
$collection = AuditLogHelper::getCollectionOfAudits($logs);
|
||||
|
||||
$this->assertEquals(
|
||||
2,
|
||||
$collection->count()
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'logs.settings_log_account_created_with_name',
|
||||
$collection[0]['description']
|
||||
);
|
||||
}
|
||||
}
|
||||
168
tests/Unit/Helpers/CollectionHelperTest.php
Normal file
168
tests/Unit/Helpers/CollectionHelperTest.php
Normal file
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use Tests\FeatureTestCase;
|
||||
use App\Helpers\CollectionHelper;
|
||||
use Illuminate\Support\Facades\App;
|
||||
|
||||
class CollectionHelperTest extends FeatureTestCase
|
||||
{
|
||||
/** @test */
|
||||
public function sortByCollator_base()
|
||||
{
|
||||
$collection = collect([
|
||||
['name' => 'a'],
|
||||
['name' => 'c'],
|
||||
['name' => 'b'],
|
||||
]);
|
||||
$collection = CollectionHelper::sortByCollator($collection, 'name');
|
||||
|
||||
$this->assertEquals(
|
||||
[
|
||||
['name' => 'a'],
|
||||
['name' => 'b'],
|
||||
['name' => 'c'],
|
||||
],
|
||||
array_values($collection->toArray())
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function sortByCollator_macro()
|
||||
{
|
||||
$collection = collect([
|
||||
['name' => 'a'],
|
||||
['name' => 'c'],
|
||||
['name' => 'b'],
|
||||
]);
|
||||
$collection = $collection->sortByCollator('name');
|
||||
|
||||
$this->assertEquals(
|
||||
[
|
||||
['name' => 'a'],
|
||||
['name' => 'b'],
|
||||
['name' => 'c'],
|
||||
],
|
||||
array_values($collection->toArray())
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function sortByCollator_callback()
|
||||
{
|
||||
$collection = collect([
|
||||
['name' => 'a'],
|
||||
['name' => 'c'],
|
||||
['name' => 'b'],
|
||||
]);
|
||||
$collection = $collection->sortByCollator(function ($item) {
|
||||
return $item['name'];
|
||||
});
|
||||
|
||||
$this->assertEquals(
|
||||
[
|
||||
['name' => 'a'],
|
||||
['name' => 'b'],
|
||||
['name' => 'c'],
|
||||
],
|
||||
array_values($collection->toArray())
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function sortByCollator_default_collation()
|
||||
{
|
||||
App::setLocale('en');
|
||||
|
||||
$collection = collect([
|
||||
['name' => 'cote'],
|
||||
['name' => 'côté'],
|
||||
['name' => 'coté'],
|
||||
['name' => 'côte'],
|
||||
]);
|
||||
$collection = CollectionHelper::sortByCollator($collection, 'name');
|
||||
|
||||
$this->assertEquals(
|
||||
[
|
||||
['name' => 'cote'],
|
||||
['name' => 'coté'],
|
||||
['name' => 'côte'],
|
||||
['name' => 'côté'],
|
||||
],
|
||||
array_values($collection->toArray())
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function sortByCollator_french_collation()
|
||||
{
|
||||
App::setLocale('fr');
|
||||
|
||||
$collection = collect([
|
||||
['name' => 'cote'],
|
||||
['name' => 'côté'],
|
||||
['name' => 'coté'],
|
||||
['name' => 'côte'],
|
||||
]);
|
||||
$collection = CollectionHelper::sortByCollator($collection, 'name');
|
||||
|
||||
$this->assertEquals(
|
||||
[
|
||||
['name' => 'cote'],
|
||||
['name' => 'côte'],
|
||||
['name' => 'coté'],
|
||||
['name' => 'côté'],
|
||||
],
|
||||
array_values($collection->toArray())
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function getCollator_french_collation()
|
||||
{
|
||||
$collator = CollectionHelper::getCollator('fr');
|
||||
|
||||
$this->assertEquals($collator->getAttribute(\Collator::FRENCH_COLLATION), \Collator::ON);
|
||||
$this->assertEquals($collator->getLocale(\Locale::VALID_LOCALE), 'fr');
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function group_by_items_property()
|
||||
{
|
||||
$object1 = (object) ['name' => 'John'];
|
||||
$object2 = (object) ['name' => 'Jack'];
|
||||
$object3 = (object) ['name' => 'John'];
|
||||
|
||||
$collection = collect([
|
||||
$object1,
|
||||
$object2,
|
||||
$object3,
|
||||
]);
|
||||
|
||||
$collection = CollectionHelper::groupByItemsProperty($collection, 'name');
|
||||
|
||||
$this->assertEquals(
|
||||
[
|
||||
'John' => [$object1, $object3],
|
||||
'Jack' => [$object2],
|
||||
],
|
||||
$collection->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_maps_uuid()
|
||||
{
|
||||
$collection = collect();
|
||||
for ($i = 1; $i <= 3; $i++) {
|
||||
$uuid = new \stdClass();
|
||||
$uuid->uuid = $i;
|
||||
$collection->push($uuid);
|
||||
}
|
||||
|
||||
$uuids = $collection->mapUuid();
|
||||
|
||||
$this->assertEquals([1, 2, 3], $uuids);
|
||||
}
|
||||
}
|
||||
44
tests/Unit/Helpers/ComplianceHelperTest.php
Normal file
44
tests/Unit/Helpers/ComplianceHelperTest.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\User\User;
|
||||
use App\Models\Settings\Term;
|
||||
use App\Helpers\ComplianceHelper;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class ComplianceHelperTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_checks_if_the_user_has_signed_the_given_term()
|
||||
{
|
||||
$user = factory(User::class)->create([]);
|
||||
$term = factory(Term::class)->create([]);
|
||||
$this->assertFalse(ComplianceHelper::hasSignedGivenTerm($user, $term));
|
||||
|
||||
$term = factory(Term::class)->create([]);
|
||||
$user->terms()->sync([$term->id => ['account_id' => $user->account_id]]);
|
||||
|
||||
$this->assertTrue(ComplianceHelper::hasSignedGivenTerm($user, $term));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_checks_if_the_user_has_signed_the_latest_term()
|
||||
{
|
||||
$user = factory(User::class)->create([]);
|
||||
$term = factory(Term::class)->create([
|
||||
'created_at' => '1990-02-07 02:26:07',
|
||||
]);
|
||||
$this->assertFalse(ComplianceHelper::isCompliantWithCurrentTerm($user));
|
||||
|
||||
$term = factory(Term::class)->create([
|
||||
'created_at' => '2020-02-07 02:26:07',
|
||||
]);
|
||||
$user->terms()->syncWithoutDetaching([$term->id => ['account_id' => $user->account_id]]);
|
||||
|
||||
$this->assertTrue(ComplianceHelper::isCompliantWithCurrentTerm($user));
|
||||
}
|
||||
}
|
||||
125
tests/Unit/Helpers/CountryHelperTest.php
Normal file
125
tests/Unit/Helpers/CountryHelperTest.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use Tests\FeatureTestCase;
|
||||
use App\Helpers\CountriesHelper;
|
||||
|
||||
class CountryHelperTest extends FeatureTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider countryDefaultCountryFromLocaleProvider
|
||||
*/
|
||||
public function test_country_getDefaultCountryFromLocale($locale, $expect)
|
||||
{
|
||||
$reflection = new \ReflectionClass(CountriesHelper::class);
|
||||
$method = $reflection->getMethod('getDefaultCountryFromLocale');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$country = $method->invokeArgs(null, [$locale]);
|
||||
|
||||
$this->assertEquals(
|
||||
$expect,
|
||||
$country
|
||||
);
|
||||
}
|
||||
|
||||
public function countryDefaultCountryFromLocaleProvider()
|
||||
{
|
||||
return [
|
||||
['en', 'US'],
|
||||
['En', 'US'],
|
||||
['EN', 'US'],
|
||||
['cs', 'CZ'],
|
||||
['he', 'IL'],
|
||||
['zh', 'CN'],
|
||||
['de', 'DE'],
|
||||
['es', 'ES'],
|
||||
['fr', 'FR'],
|
||||
['hr', 'HR'],
|
||||
['it', 'IT'],
|
||||
['nl', 'NL'],
|
||||
['pt', 'PT'],
|
||||
['ru', 'RU'],
|
||||
['tr', 'TR'],
|
||||
['ja', null],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider countryCountryFromLocaleProvider
|
||||
*/
|
||||
public function test_country_getCountryFromLocale($locale, $expect)
|
||||
{
|
||||
$country = CountriesHelper::getCountryFromLocale($locale);
|
||||
|
||||
$this->assertNotNull($country);
|
||||
$this->assertEquals(
|
||||
$expect,
|
||||
$country->getIsoAlpha2()
|
||||
);
|
||||
}
|
||||
|
||||
public function countryCountryFromLocaleProvider()
|
||||
{
|
||||
return [
|
||||
['en', 'US'],
|
||||
['En', 'US'],
|
||||
['EN', 'US'],
|
||||
['en-US', 'US'],
|
||||
['cs', 'CZ'],
|
||||
['he', 'IL'],
|
||||
['zh', 'CN'],
|
||||
['de', 'DE'],
|
||||
['es', 'ES'],
|
||||
['fr', 'FR'],
|
||||
['hr', 'HR'],
|
||||
['id', 'ID'],
|
||||
['it', 'IT'],
|
||||
['nl', 'NL'],
|
||||
['pt', 'PT'],
|
||||
['ru', 'RU'],
|
||||
['tr', 'TR'],
|
||||
['ja', 'JP'],
|
||||
['pt-BR', 'BR'],
|
||||
['fr-BE', 'BE'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider timezoneFromLocaleProvider
|
||||
* @test
|
||||
*/
|
||||
public function it_get_default_timezone($locale, $expect)
|
||||
{
|
||||
$country = CountriesHelper::getCountryFromLocale($locale);
|
||||
$timezone = CountriesHelper::getDefaultTimezone($country);
|
||||
|
||||
$this->assertNotNull($timezone);
|
||||
$this->assertEquals(
|
||||
$expect,
|
||||
$timezone
|
||||
);
|
||||
}
|
||||
|
||||
public function timezoneFromLocaleProvider()
|
||||
{
|
||||
return [
|
||||
['en', 'America/Chicago'],
|
||||
['cs', 'Europe/Prague'],
|
||||
['he', 'Asia/Jerusalem'],
|
||||
['zh', 'Asia/Shanghai'],
|
||||
['de', 'Europe/Berlin'],
|
||||
['es', 'Europe/Madrid'],
|
||||
['fr', 'Europe/Paris'],
|
||||
['hr', 'Europe/Zagreb'],
|
||||
['id', 'Asia/Jakarta'],
|
||||
['it', 'Europe/Rome'],
|
||||
['nl', 'Europe/Amsterdam'],
|
||||
['pt', 'Europe/Lisbon'],
|
||||
['ru', 'Europe/Moscow'],
|
||||
['tr', 'Europe/Istanbul'],
|
||||
['ja', 'Asia/Tokyo'],
|
||||
];
|
||||
}
|
||||
}
|
||||
761
tests/Unit/Helpers/DateHelperTest.php
Normal file
761
tests/Unit/Helpers/DateHelperTest.php
Normal file
@@ -0,0 +1,761 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Tests\FeatureTestCase;
|
||||
use App\Helpers\DateHelper;
|
||||
use App\Helpers\TimezoneHelper;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class DateHelperTest extends FeatureTestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
public function testGetShortDateWithEnglishLocale()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('en');
|
||||
|
||||
$this->assertEquals(
|
||||
'Jan 22, 2017',
|
||||
DateHelper::getShortDate($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetShortDateWithFrenchLocale()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('fr');
|
||||
|
||||
$this->assertEquals(
|
||||
'22 janv. 2017',
|
||||
DateHelper::getShortDate($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetShortDateWithUnknownLocale()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('jp');
|
||||
|
||||
$this->assertEquals(
|
||||
'Jan 22, 2017',
|
||||
DateHelper::getShortDate($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetFullDateWithEnglishLocale()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('en');
|
||||
|
||||
$this->assertEquals(
|
||||
'January 22, 2017',
|
||||
DateHelper::getFullDate($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetFullDateWithFrenchLocale()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('fr');
|
||||
|
||||
$this->assertEquals(
|
||||
'22 janvier 2017',
|
||||
DateHelper::getFullDate($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetFullDateWithUnknownLocale()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('jp');
|
||||
|
||||
$this->assertEquals(
|
||||
'January 22, 2017',
|
||||
DateHelper::getFullDate($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetShortDateWithTimeWithEnglishLocale()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('en');
|
||||
|
||||
$this->assertEquals(
|
||||
'Jan 22, 2017 17:56',
|
||||
DateHelper::getShortDateWithTime($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetShortDateWithTimeWithFrenchLocale()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('fr');
|
||||
|
||||
$this->assertEquals(
|
||||
'22 janv. 2017 17:56',
|
||||
DateHelper::getShortDateWithTime($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetShortDateWithTimeWithUnknownLocale()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('jp');
|
||||
|
||||
$this->assertEquals(
|
||||
'Jan 22, 2017 17:56',
|
||||
DateHelper::getShortDateWithTime($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function test_get_short_date_without_year_returns_a_date()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('en');
|
||||
|
||||
$this->assertEquals(
|
||||
'Jan 22',
|
||||
DateHelper::getShortDateWithoutYear($date)
|
||||
);
|
||||
|
||||
App::setLocale('fr');
|
||||
|
||||
$this->assertEquals(
|
||||
'22 janv.',
|
||||
DateHelper::getShortDateWithoutYear($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_returns_the_default_short_date()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale(null);
|
||||
|
||||
$this->assertEquals(
|
||||
'Jan 22',
|
||||
DateHelper::getShortDateWithoutYear($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function test_add_time_according_to_frequency_type_returns_the_right_value()
|
||||
{
|
||||
$date = '2017-01-22 17:56:03';
|
||||
|
||||
$testDate = DateHelper::parseDateTime($date);
|
||||
$this->assertEquals(
|
||||
'2017-01-29',
|
||||
DateHelper::addTimeAccordingToFrequencyType($testDate, 'week', 1)->toDateString()
|
||||
);
|
||||
|
||||
$testDate = DateHelper::parseDateTime($date);
|
||||
$this->assertEquals(
|
||||
'2017-02-22',
|
||||
DateHelper::addTimeAccordingToFrequencyType($testDate, 'month', 1)->toDateString()
|
||||
);
|
||||
|
||||
$testDate = DateHelper::parseDateTime($date);
|
||||
$this->assertEquals(
|
||||
'2018-01-22',
|
||||
DateHelper::addTimeAccordingToFrequencyType($testDate, 'year', 1)->toDateString()
|
||||
);
|
||||
}
|
||||
|
||||
public function test_parse_dateTime()
|
||||
{
|
||||
$testDate = DateHelper::parseDateTime(null);
|
||||
|
||||
$this->assertNull($testDate);
|
||||
|
||||
$date = '2017-01-22 17:56:03';
|
||||
|
||||
$testDate = DateHelper::parseDateTime($date);
|
||||
|
||||
$this->assertInstanceOf(Carbon::class, $testDate);
|
||||
}
|
||||
|
||||
public function test_parse_dateTime_bad()
|
||||
{
|
||||
$date = 'xF 2017';
|
||||
|
||||
$testDate = DateHelper::parseDateTime($date);
|
||||
|
||||
$this->assertNull($testDate);
|
||||
}
|
||||
|
||||
public function test_parse_parseDate_bad()
|
||||
{
|
||||
$date = 'xF 2017';
|
||||
|
||||
$testDate = DateHelper::parseDate($date);
|
||||
|
||||
$this->assertNull($testDate);
|
||||
}
|
||||
|
||||
public function test_parse_dateTime_format()
|
||||
{
|
||||
$date = '20190120T232144Z';
|
||||
|
||||
$testDate = DateHelper::parseDateTime($date);
|
||||
|
||||
$this->assertEquals(2019, $testDate->year);
|
||||
$this->assertEquals(1, $testDate->month);
|
||||
$this->assertEquals(20, $testDate->day);
|
||||
$this->assertEquals(23, $testDate->hour);
|
||||
$this->assertEquals(21, $testDate->minute);
|
||||
$this->assertEquals(44, $testDate->second);
|
||||
$this->assertEquals('UTC', $testDate->timezone->getName());
|
||||
|
||||
$this->assertEquals(
|
||||
'2019-01-20',
|
||||
$testDate->toDateString()
|
||||
);
|
||||
$this->assertEquals(
|
||||
'2019-01-20T23:21:44Z',
|
||||
DateHelper::getTimestamp($testDate)
|
||||
);
|
||||
}
|
||||
|
||||
public function test_parse_dateTime_utc()
|
||||
{
|
||||
$date = '2017-01-22 17:56:03';
|
||||
|
||||
$testDate = DateHelper::parseDateTime($date);
|
||||
|
||||
$this->assertEquals(2017, $testDate->year);
|
||||
$this->assertEquals(1, $testDate->month);
|
||||
$this->assertEquals(22, $testDate->day);
|
||||
$this->assertEquals(17, $testDate->hour);
|
||||
$this->assertEquals(56, $testDate->minute);
|
||||
$this->assertEquals(03, $testDate->second);
|
||||
$this->assertEquals('UTC', $testDate->timezone->getName());
|
||||
|
||||
$this->assertEquals(
|
||||
'2017-01-22',
|
||||
$testDate->toDateString()
|
||||
);
|
||||
$this->assertEquals(
|
||||
'2017-01-22T17:56:03Z',
|
||||
DateHelper::getTimestamp($testDate)
|
||||
);
|
||||
}
|
||||
|
||||
public function test_parse_dateTime_new_york()
|
||||
{
|
||||
$date = '2017-01-22 17:56:03';
|
||||
$timezone = 'America/New_York';
|
||||
|
||||
$testDate = DateHelper::parseDateTime($date, $timezone);
|
||||
|
||||
$this->assertEquals(2017, $testDate->year);
|
||||
$this->assertEquals(1, $testDate->month);
|
||||
$this->assertEquals(22, $testDate->day);
|
||||
$this->assertEquals(22, $testDate->hour);
|
||||
$this->assertEquals(56, $testDate->minute);
|
||||
$this->assertEquals(03, $testDate->second);
|
||||
$this->assertEquals('UTC', $testDate->timezone->getName());
|
||||
|
||||
$this->assertEquals(
|
||||
'2017-01-22',
|
||||
$testDate->toDateString()
|
||||
);
|
||||
$this->assertEquals(
|
||||
'2017-01-22T22:56:03Z',
|
||||
DateHelper::getTimestamp($testDate)
|
||||
);
|
||||
}
|
||||
|
||||
public function test_parse_dateTime_paris()
|
||||
{
|
||||
$date = '2019-01-01 00:56:03';
|
||||
$timezone = 'Europe/Paris';
|
||||
|
||||
$testDate = DateHelper::parseDateTime($date, $timezone);
|
||||
|
||||
$this->assertEquals(2018, $testDate->year);
|
||||
$this->assertEquals(12, $testDate->month);
|
||||
$this->assertEquals(31, $testDate->day);
|
||||
$this->assertEquals(23, $testDate->hour);
|
||||
$this->assertEquals(56, $testDate->minute);
|
||||
$this->assertEquals(03, $testDate->second);
|
||||
$this->assertEquals('UTC', $testDate->timezone->getName());
|
||||
|
||||
$this->assertEquals(
|
||||
'2018-12-31',
|
||||
$testDate->toDateString()
|
||||
);
|
||||
$this->assertEquals(
|
||||
'2018-12-31T23:56:03Z',
|
||||
DateHelper::getTimestamp($testDate)
|
||||
);
|
||||
}
|
||||
|
||||
public function test_parse_dateTime_carbon()
|
||||
{
|
||||
$date = new Carbon('2019-01-01 00:56:03', 'Europe/Paris');
|
||||
|
||||
$testDate = DateHelper::parseDateTime($date);
|
||||
|
||||
$this->assertEquals(2018, $testDate->year);
|
||||
$this->assertEquals(12, $testDate->month);
|
||||
$this->assertEquals(31, $testDate->day);
|
||||
$this->assertEquals(23, $testDate->hour);
|
||||
$this->assertEquals(56, $testDate->minute);
|
||||
$this->assertEquals(03, $testDate->second);
|
||||
$this->assertEquals('UTC', $testDate->timezone->getName());
|
||||
|
||||
$this->assertEquals(
|
||||
'2018-12-31',
|
||||
$testDate->toDateString()
|
||||
);
|
||||
$this->assertEquals(
|
||||
'2018-12-31T23:56:03Z',
|
||||
DateHelper::getTimestamp($testDate)
|
||||
);
|
||||
}
|
||||
|
||||
public function test_parse_dateTime_dateTimeObject()
|
||||
{
|
||||
$date = new \DateTime('2019-01-01 00:56:03', new \DateTimeZone('Europe/Paris'));
|
||||
|
||||
$testDate = DateHelper::parseDateTime($date);
|
||||
|
||||
$this->assertEquals(2018, $testDate->year);
|
||||
$this->assertEquals(12, $testDate->month);
|
||||
$this->assertEquals(31, $testDate->day);
|
||||
$this->assertEquals(23, $testDate->hour);
|
||||
$this->assertEquals(56, $testDate->minute);
|
||||
$this->assertEquals(03, $testDate->second);
|
||||
$this->assertEquals('UTC', $testDate->timezone->getName());
|
||||
|
||||
$this->assertEquals(
|
||||
'2018-12-31',
|
||||
$testDate->toDateString()
|
||||
);
|
||||
$this->assertEquals(
|
||||
'2018-12-31T23:56:03Z',
|
||||
DateHelper::getTimestamp($testDate)
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetShortMonthWithEnglishLocale()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('en');
|
||||
|
||||
$this->assertEquals(
|
||||
'Jan',
|
||||
DateHelper::getShortMonth($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetShortMonthWithFrenchLocale()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('fr');
|
||||
|
||||
$this->assertEquals(
|
||||
'janv.',
|
||||
DateHelper::getShortMonth($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetShortMonthWithUnknownLocale()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('jp');
|
||||
|
||||
$this->assertEquals(
|
||||
'Jan',
|
||||
DateHelper::getShortMonth($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetFullMonthAndDateWithEnglishLocale()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('en');
|
||||
|
||||
$this->assertEquals(
|
||||
'January 2017',
|
||||
DateHelper::getFullMonthAndDate($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetFullMonthAndDateWithFrenchLocale()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('fr');
|
||||
|
||||
$this->assertEquals(
|
||||
'janvier 2017',
|
||||
DateHelper::getFullMonthAndDate($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetFullMonthAndDateWithUnknownLocale()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('jp');
|
||||
|
||||
$this->assertEquals(
|
||||
'January 2017',
|
||||
DateHelper::getFullMonthAndDate($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetShortDayWithEnglishLocale()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('en');
|
||||
|
||||
$this->assertEquals(
|
||||
'Sun',
|
||||
DateHelper::getShortDay($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetShortDayWithFrenchLocale()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('fr');
|
||||
|
||||
$this->assertEquals(
|
||||
'dim.',
|
||||
DateHelper::getShortDay($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetShortDayWithUnknownLocale()
|
||||
{
|
||||
$date = Carbon::parse('2017-01-22 17:56:03');
|
||||
App::setLocale('jp');
|
||||
|
||||
$this->assertEquals(
|
||||
'Sun',
|
||||
DateHelper::getShortDay($date)
|
||||
);
|
||||
}
|
||||
|
||||
public function test_get_month_and_year()
|
||||
{
|
||||
Carbon::setTestNow(Carbon::create(2017, 1, 1));
|
||||
|
||||
$this->assertEquals(
|
||||
'Jul 2017',
|
||||
DateHelper::getMonthAndYear(6)
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_gets_date_one_month_from_now()
|
||||
{
|
||||
Carbon::setTestNow(Carbon::create(2017, 1, 1));
|
||||
|
||||
$this->assertEquals(
|
||||
'2017-02-01',
|
||||
DateHelper::getNextTheoriticalBillingDate('monthly')->toDateString()
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_gets_date_one_year_from_now()
|
||||
{
|
||||
Carbon::setTestNow(Carbon::create(2017, 1, 1));
|
||||
|
||||
$this->assertEquals(
|
||||
'2018-01-01',
|
||||
DateHelper::getNextTheoriticalBillingDate('yearly')->toDateString()
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_returns_a_list_with_years()
|
||||
{
|
||||
$user = $this->signIn();
|
||||
$user->locale = 'en';
|
||||
$user->save();
|
||||
|
||||
$this->assertCount(
|
||||
3,
|
||||
DateHelper::getListOfYears(2)
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
now()->year,
|
||||
DateHelper::getListOfYears(2)->first()['name']
|
||||
);
|
||||
$this->assertEquals(
|
||||
now()->subYears(2)->year,
|
||||
DateHelper::getListOfYears(2)->last()['name']
|
||||
);
|
||||
$this->assertEquals(
|
||||
now()->subYears(-2)->year,
|
||||
DateHelper::getListOfYears(2, -2)->first()['name']
|
||||
);
|
||||
$this->assertEquals(
|
||||
now()->year,
|
||||
DateHelper::getListOfYears(2, -2)[2]['name']
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_returns_a_list_with_twelve_months()
|
||||
{
|
||||
$user = $this->signIn();
|
||||
$user->locale = 'en';
|
||||
$user->save();
|
||||
|
||||
$this->assertCount(
|
||||
12,
|
||||
DateHelper::getListOfMonths()
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_returns_a_list_of_months_in_english()
|
||||
{
|
||||
$user = $this->signIn();
|
||||
$user->locale = 'en';
|
||||
$user->save();
|
||||
|
||||
$months = DateHelper::getListOfMonths();
|
||||
|
||||
$this->assertEquals(
|
||||
'January',
|
||||
$months[0]['name']
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_returns_a_list_with_thirty_one_days()
|
||||
{
|
||||
$user = $this->signIn();
|
||||
$user->locale = 'en';
|
||||
$user->save();
|
||||
|
||||
$this->assertCount(
|
||||
31,
|
||||
DateHelper::getListOfDays()
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_returns_a_list_with_twenty_four_hours()
|
||||
{
|
||||
$this->assertCount(
|
||||
24,
|
||||
DateHelper::getListOfHours()
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_returns_a_list_of_hours()
|
||||
{
|
||||
$hours = DateHelper::getListOfHours();
|
||||
|
||||
$this->assertEquals(
|
||||
'01.00 AM',
|
||||
$hours[0]['name']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'01:00',
|
||||
$hours[0]['id']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'02.00 PM',
|
||||
$hours[13]['name']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'14:00',
|
||||
$hours[13]['id']
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_returns_a_list_of_hours_French()
|
||||
{
|
||||
App::setLocale('fr');
|
||||
$hours = DateHelper::getListOfHours();
|
||||
|
||||
$this->assertEquals(
|
||||
'01:00',
|
||||
$hours[0]['name']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'01:00',
|
||||
$hours[0]['id']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'14:00',
|
||||
$hours[13]['name']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'14:00',
|
||||
$hours[13]['id']
|
||||
);
|
||||
}
|
||||
|
||||
public function test_old_timezones_exists()
|
||||
{
|
||||
// These are all currently used timezone in monica
|
||||
$oldTimezones = [
|
||||
'US/Eastern',
|
||||
'US/Central',
|
||||
'America/Los_Angeles',
|
||||
'Pacific/Midway',
|
||||
'Pacific/Samoa',
|
||||
'Pacific/Honolulu',
|
||||
'US/Alaska',
|
||||
'America/Tijuana',
|
||||
'US/Arizona',
|
||||
'America/Chihuahua',
|
||||
'America/Chihuahua',
|
||||
'America/Mazatlan',
|
||||
'US/Mountain',
|
||||
'America/Managua',
|
||||
'US/Central',
|
||||
'America/Mexico_City',
|
||||
'America/Mexico_City',
|
||||
'America/Monterrey',
|
||||
'Canada/Saskatchewan',
|
||||
'America/Bogota',
|
||||
'US/Eastern',
|
||||
'US/East-Indiana',
|
||||
'America/Lima',
|
||||
'America/Bogota',
|
||||
'Canada/Atlantic',
|
||||
'America/Caracas',
|
||||
'America/La_Paz',
|
||||
'America/Santiago',
|
||||
'Canada/Newfoundland',
|
||||
'America/Sao_Paulo',
|
||||
'America/Argentina/Buenos_Aires',
|
||||
'America/Noronha',
|
||||
'Atlantic/Azores',
|
||||
'Atlantic/Cape_Verde',
|
||||
'Africa/Casablanca',
|
||||
'Europe/London',
|
||||
'Etc/Greenwich',
|
||||
'Europe/Lisbon',
|
||||
'Europe/London',
|
||||
'Africa/Monrovia',
|
||||
'UTC',
|
||||
'Europe/Amsterdam',
|
||||
'Europe/Belgrade',
|
||||
'Europe/Berlin',
|
||||
'Europe/Bratislava',
|
||||
'Europe/Brussels',
|
||||
'Europe/Budapest',
|
||||
'Europe/Copenhagen',
|
||||
'Europe/Ljubljana',
|
||||
'Europe/Madrid',
|
||||
'Europe/Paris',
|
||||
'Europe/Prague',
|
||||
'Europe/Rome',
|
||||
'Europe/Sarajevo',
|
||||
'Europe/Skopje',
|
||||
'Europe/Stockholm',
|
||||
'Europe/Vienna',
|
||||
'Europe/Warsaw',
|
||||
'Africa/Lagos',
|
||||
'Europe/Zagreb',
|
||||
'Europe/Zurich',
|
||||
'Europe/Athens',
|
||||
'Europe/Bucharest',
|
||||
'Africa/Cairo',
|
||||
'Africa/Harare',
|
||||
'Europe/Helsinki',
|
||||
'Europe/Istanbul',
|
||||
'Asia/Jerusalem',
|
||||
'Europe/Helsinki',
|
||||
'Africa/Johannesburg',
|
||||
'Europe/Riga',
|
||||
'Europe/Sofia',
|
||||
'Europe/Tallinn',
|
||||
'Europe/Vilnius',
|
||||
'Asia/Baghdad',
|
||||
'Asia/Kuwait',
|
||||
'Europe/Minsk',
|
||||
'Africa/Nairobi',
|
||||
'Asia/Riyadh',
|
||||
'Europe/Volgograd',
|
||||
'Asia/Tehran',
|
||||
'Asia/Muscat',
|
||||
'Asia/Baku',
|
||||
'Europe/Moscow',
|
||||
'Asia/Muscat',
|
||||
'Europe/Moscow',
|
||||
'Asia/Tbilisi',
|
||||
'Asia/Yerevan',
|
||||
'Asia/Kabul',
|
||||
'Asia/Karachi',
|
||||
'Asia/Karachi',
|
||||
'Asia/Tashkent',
|
||||
'Asia/Calcutta',
|
||||
'Asia/Kolkata',
|
||||
'Asia/Calcutta',
|
||||
'Asia/Calcutta',
|
||||
'Asia/Calcutta',
|
||||
'Asia/Katmandu',
|
||||
'Asia/Almaty',
|
||||
'Asia/Dhaka',
|
||||
'Asia/Dhaka',
|
||||
'Asia/Yekaterinburg',
|
||||
'Asia/Rangoon',
|
||||
'Asia/Bangkok',
|
||||
'Asia/Bangkok',
|
||||
'Asia/Jakarta',
|
||||
'Asia/Novosibirsk',
|
||||
'Asia/Hong_Kong',
|
||||
'Asia/Chongqing',
|
||||
'Asia/Hong_Kong',
|
||||
'Asia/Krasnoyarsk',
|
||||
'Asia/Kuala_Lumpur',
|
||||
'Australia/Perth',
|
||||
'Asia/Singapore',
|
||||
'Asia/Taipei',
|
||||
'Asia/Ulan_Bator',
|
||||
'Asia/Urumqi',
|
||||
'Asia/Irkutsk',
|
||||
'Asia/Tokyo',
|
||||
'Asia/Tokyo',
|
||||
'Asia/Seoul',
|
||||
'Asia/Tokyo',
|
||||
'Australia/Adelaide',
|
||||
'Australia/Darwin',
|
||||
'Australia/Brisbane',
|
||||
'Australia/Canberra',
|
||||
'Pacific/Guam',
|
||||
'Australia/Hobart',
|
||||
'Australia/Melbourne',
|
||||
'Pacific/Port_Moresby',
|
||||
'Australia/Sydney',
|
||||
'Asia/Yakutsk',
|
||||
'Asia/Vladivostok',
|
||||
'Pacific/Auckland',
|
||||
'Pacific/Fiji',
|
||||
'Pacific/Kwajalein',
|
||||
'Asia/Kamchatka',
|
||||
'Asia/Magadan',
|
||||
'Pacific/Fiji',
|
||||
'Asia/Magadan',
|
||||
'Asia/Magadan',
|
||||
'Pacific/Auckland',
|
||||
'Pacific/Tongatapu',
|
||||
];
|
||||
|
||||
$list = TimezoneHelper::getListOfTimezones();
|
||||
$list = collect($list);
|
||||
|
||||
$missed = '';
|
||||
foreach ($oldTimezones as $timezone) {
|
||||
$timezone = TimezoneHelper::adjustEquivalentTimezone($timezone);
|
||||
if ($list->firstWhere('timezone', $timezone) == null) {
|
||||
$missed .= $timezone.',';
|
||||
}
|
||||
}
|
||||
|
||||
$this->assertTrue(empty($missed), 'Missed timezones : '.$missed);
|
||||
}
|
||||
}
|
||||
54
tests/Unit/Helpers/FormHelperTest.php
Normal file
54
tests/Unit/Helpers/FormHelperTest.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\User\User;
|
||||
use App\Helpers\FormHelper;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class FormHelperTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_gets_name_order_for_a_form()
|
||||
{
|
||||
$user = factory(User::class)->create([]);
|
||||
$user->name_order = 'firstname_lastname';
|
||||
$this->assertEquals(
|
||||
'firstname',
|
||||
FormHelper::getNameOrderForForms($user)
|
||||
);
|
||||
|
||||
$user->name_order = 'firstname_lastname_nickname';
|
||||
$this->assertEquals(
|
||||
'firstname',
|
||||
FormHelper::getNameOrderForForms($user)
|
||||
);
|
||||
|
||||
$user->name_order = 'firstname_nickname_lastname';
|
||||
$this->assertEquals(
|
||||
'firstname',
|
||||
FormHelper::getNameOrderForForms($user)
|
||||
);
|
||||
|
||||
$user->name_order = 'lastname_firstname';
|
||||
$this->assertEquals(
|
||||
'lastname',
|
||||
FormHelper::getNameOrderForForms($user)
|
||||
);
|
||||
|
||||
$user->name_order = 'lastname_firstname_nickname';
|
||||
$this->assertEquals(
|
||||
'lastname',
|
||||
FormHelper::getNameOrderForForms($user)
|
||||
);
|
||||
|
||||
$user->name_order = 'lastname_nickname_firstname';
|
||||
$this->assertEquals(
|
||||
'lastname',
|
||||
FormHelper::getNameOrderForForms($user)
|
||||
);
|
||||
}
|
||||
}
|
||||
54
tests/Unit/Helpers/GenderHelperTest.php
Normal file
54
tests/Unit/Helpers/GenderHelperTest.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use Tests\FeatureTestCase;
|
||||
use App\Helpers\GenderHelper;
|
||||
use App\Models\Contact\Gender;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Contact\Contact;
|
||||
|
||||
class GenderHelperTest extends FeatureTestCase
|
||||
{
|
||||
/** @test */
|
||||
public function it_gets_all_the_gender_inputs()
|
||||
{
|
||||
$this->signIn();
|
||||
|
||||
$genders = GenderHelper::getGendersInput();
|
||||
|
||||
$this->assertCount(4, $genders);
|
||||
$this->assertEquals([
|
||||
'id' => '',
|
||||
'name' => 'No gender',
|
||||
], $genders[0]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_replaces_gender_with_another_gender()
|
||||
{
|
||||
$account = factory(Account::class)->create();
|
||||
$male = factory(Gender::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
$female = factory(Gender::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
factory(Contact::class, 2)->create([
|
||||
'account_id' => $account->id,
|
||||
'gender_id' => $male,
|
||||
]);
|
||||
factory(Contact::class)->create([
|
||||
'account_id' => $account->id,
|
||||
'gender_id' => $female,
|
||||
]);
|
||||
|
||||
GenderHelper::replace($account, $male, $female);
|
||||
|
||||
$this->assertEquals(
|
||||
3,
|
||||
$female->contacts->count()
|
||||
);
|
||||
}
|
||||
}
|
||||
184
tests/Unit/Helpers/InstanceHelperTest.php
Normal file
184
tests/Unit/Helpers/InstanceHelperTest.php
Normal file
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
use function Safe\json_decode;
|
||||
use App\Helpers\InstanceHelper;
|
||||
use App\Models\Account\Account;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class InstanceHelperTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_gets_the_number_of_paid_subscribers()
|
||||
{
|
||||
factory(Account::class)->create(['stripe_id' => 'id292839']);
|
||||
factory(Account::class)->create();
|
||||
factory(Account::class)->create(['stripe_id' => 'id2sdf92839']);
|
||||
|
||||
$this->assertEquals(
|
||||
2,
|
||||
InstanceHelper::getNumberOfPaidSubscribers()
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fetches_the_monthly_plan_information()
|
||||
{
|
||||
config(['monica.paid_plan_monthly_friendly_name' => 'Monthly']);
|
||||
config(['monica.paid_plan_monthly_id' => 'monthly']);
|
||||
config(['monica.paid_plan_monthly_price' => 1000]);
|
||||
|
||||
$this->assertEquals(
|
||||
'monthly',
|
||||
InstanceHelper::getPlanInformationFromConfig('monthly')['type']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'Monthly',
|
||||
InstanceHelper::getPlanInformationFromConfig('monthly')['name']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'monthly',
|
||||
InstanceHelper::getPlanInformationFromConfig('monthly')['id']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
1000,
|
||||
InstanceHelper::getPlanInformationFromConfig('monthly')['price']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'$10.00',
|
||||
InstanceHelper::getPlanInformationFromConfig('monthly')['friendlyPrice']
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fetches_the_annually_plan_information()
|
||||
{
|
||||
config(['monica.paid_plan_annual_friendly_name' => 'Annual']);
|
||||
config(['monica.paid_plan_annual_id' => 'annual']);
|
||||
config(['monica.paid_plan_annual_price' => 1000]);
|
||||
|
||||
$this->assertEquals(
|
||||
'annual',
|
||||
InstanceHelper::getPlanInformationFromConfig('annual')['type']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'Annual',
|
||||
InstanceHelper::getPlanInformationFromConfig('annual')['name']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'annual',
|
||||
InstanceHelper::getPlanInformationFromConfig('annual')['id']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
1000,
|
||||
InstanceHelper::getPlanInformationFromConfig('annual')['price']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'$10.00',
|
||||
InstanceHelper::getPlanInformationFromConfig('annual')['friendlyPrice']
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fetches_subscription_information()
|
||||
{
|
||||
$stripeSubscription = (object) [
|
||||
'plan' => (object) [
|
||||
'currency' => 'USD',
|
||||
'amount' => 500,
|
||||
'interval' => 'month',
|
||||
'id' => 'monthly',
|
||||
],
|
||||
'current_period_end' => 1629976560,
|
||||
];
|
||||
|
||||
$subscription = Mockery::mock('\Laravel\Cashier\Subscription');
|
||||
$subscription->shouldReceive('asStripeSubscription')
|
||||
->andReturn($stripeSubscription);
|
||||
$subscription->shouldReceive('getAttribute')
|
||||
->with('name')
|
||||
->andReturn('Monthly');
|
||||
|
||||
$this->assertEquals(
|
||||
'monthly',
|
||||
InstanceHelper::getPlanInformationFromSubscription($subscription)['type']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'Monthly',
|
||||
InstanceHelper::getPlanInformationFromSubscription($subscription)['name']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'monthly',
|
||||
InstanceHelper::getPlanInformationFromSubscription($subscription)['id']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
500,
|
||||
InstanceHelper::getPlanInformationFromSubscription($subscription)['price']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'$5.00',
|
||||
InstanceHelper::getPlanInformationFromSubscription($subscription)['friendlyPrice']
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_returns_null_when_fetching_an_unknown_plan_information()
|
||||
{
|
||||
$account = new Account;
|
||||
|
||||
$this->assertNull(
|
||||
InstanceHelper::getPlanInformationFromConfig('unknown_plan')
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_gets_latest_changelog_entries()
|
||||
{
|
||||
$json = public_path('changelog.json');
|
||||
$changelogs = json_decode(file_get_contents($json), true)['entries'];
|
||||
$count = count($changelogs);
|
||||
|
||||
$this->assertCount(
|
||||
$count,
|
||||
InstanceHelper::getChangelogEntries()
|
||||
);
|
||||
|
||||
$this->assertCount(
|
||||
3,
|
||||
InstanceHelper::getChangelogEntries(3)
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_checks_if_the_instance_has_at_least_one_account()
|
||||
{
|
||||
DB::table('accounts')->delete();
|
||||
|
||||
$this->assertFalse(
|
||||
InstanceHelper::hasAtLeastOneAccount()
|
||||
);
|
||||
|
||||
factory(Account::class)->create();
|
||||
$this->assertTrue(
|
||||
InstanceHelper::hasAtLeastOneAccount()
|
||||
);
|
||||
}
|
||||
}
|
||||
37
tests/Unit/Helpers/JournalHelperTest.php
Normal file
37
tests/Unit/Helpers/JournalHelperTest.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\User\User;
|
||||
use App\Models\Journal\Day;
|
||||
use App\Helpers\JournalHelper;
|
||||
use App\Models\Account\Account;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class JournalHelperTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function you_can_vote_if_you_havent_voted_yet_today()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create(['account_id' => $account->id]);
|
||||
|
||||
$this->assertFalse(JournalHelper::hasAlreadyRatedToday($user));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function you_cant_vote_if_you_have_already_voted_today()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create(['account_id' => $account->id]);
|
||||
factory(Day::class)->create([
|
||||
'account_id' => $account->id,
|
||||
'date' => now(),
|
||||
]);
|
||||
|
||||
$this->assertTrue(JournalHelper::hasAlreadyRatedToday($user));
|
||||
}
|
||||
}
|
||||
162
tests/Unit/Helpers/LocaleHelperTest.php
Normal file
162
tests/Unit/Helpers/LocaleHelperTest.php
Normal file
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use Tests\FeatureTestCase;
|
||||
use App\Helpers\LocaleHelper;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class LocaleHelperTest extends FeatureTestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function get_locale_returns_english_by_default()
|
||||
{
|
||||
$this->assertEquals(
|
||||
'en',
|
||||
LocaleHelper::getLocale()
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function get_locale_returns_right_locale_if_user_logged()
|
||||
{
|
||||
$user = $this->signIn();
|
||||
$user->locale = 'fr';
|
||||
$user->save();
|
||||
|
||||
$this->assertEquals(
|
||||
'fr',
|
||||
LocaleHelper::getLocale()
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function get_direction_default()
|
||||
{
|
||||
$this->assertEquals(
|
||||
'ltr',
|
||||
LocaleHelper::getDirection()
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function get_direction_french()
|
||||
{
|
||||
App::setLocale('fr');
|
||||
|
||||
$this->assertEquals(
|
||||
'ltr',
|
||||
LocaleHelper::getDirection()
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function get_direction_hebrew()
|
||||
{
|
||||
App::setLocale('he');
|
||||
|
||||
$this->assertEquals(
|
||||
'rtl',
|
||||
LocaleHelper::getDirection()
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function format_telephone_by_iso()
|
||||
{
|
||||
$tel = LocaleHelper::formatTelephoneNumberByISO('202-555-0191', 'gb');
|
||||
|
||||
$this->assertEquals(
|
||||
'+44 20 2555 0191',
|
||||
$tel
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider localeHelperGetLangProvider
|
||||
*/
|
||||
public function test_locale_get_lang($locale, $expect)
|
||||
{
|
||||
$lang = LocaleHelper::getLang($locale);
|
||||
|
||||
$this->assertEquals(
|
||||
$expect,
|
||||
$lang
|
||||
);
|
||||
}
|
||||
|
||||
public function localeHelperGetLangProvider()
|
||||
{
|
||||
return [
|
||||
['en', 'en'],
|
||||
['En', 'en'],
|
||||
['EN', 'en'],
|
||||
['en-US', 'en'],
|
||||
['en-us', 'en'],
|
||||
['en_US', 'en'],
|
||||
['pt-BR', 'pt'],
|
||||
['xx-YY', 'xx'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider localeHelperGetCountryProvider
|
||||
*/
|
||||
public function test_locale_get_country($locale, $expect)
|
||||
{
|
||||
$country = LocaleHelper::getCountry($locale);
|
||||
|
||||
$this->assertEquals(
|
||||
$expect,
|
||||
$country
|
||||
);
|
||||
}
|
||||
|
||||
public function localeHelperGetCountryProvider()
|
||||
{
|
||||
return [
|
||||
['en', 'US'],
|
||||
['en-us', 'US'],
|
||||
['en-US', 'US'],
|
||||
['en_US', 'US'],
|
||||
['pt-BR', 'BR'],
|
||||
['xx-YY', 'YY'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider localeHelperExtractCountryProvider
|
||||
*/
|
||||
public function test_locale_extract_country($locale, $expect)
|
||||
{
|
||||
$country = LocaleHelper::extractCountry($locale);
|
||||
|
||||
$this->assertEquals(
|
||||
$expect,
|
||||
$country
|
||||
);
|
||||
|
||||
App::setLocale($locale);
|
||||
|
||||
$country = LocaleHelper::extractCountry();
|
||||
|
||||
$this->assertEquals(
|
||||
$expect,
|
||||
$country
|
||||
);
|
||||
}
|
||||
|
||||
public function localeHelperExtractCountryProvider()
|
||||
{
|
||||
return [
|
||||
['en', null],
|
||||
['fr', null],
|
||||
['en-US', 'US'],
|
||||
['pt-BR', 'BR'],
|
||||
['xx-YY', 'YY'],
|
||||
];
|
||||
}
|
||||
}
|
||||
114
tests/Unit/Helpers/MoneyHelperTest.php
Normal file
114
tests/Unit/Helpers/MoneyHelperTest.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\User\User;
|
||||
use App\Helpers\MoneyHelper;
|
||||
use App\Models\Settings\Currency;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class MoneyHelperTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_returns_the_amount_with_the_currency_symbol()
|
||||
{
|
||||
$currency = new Currency();
|
||||
$currency->iso = 'EUR';
|
||||
|
||||
$this->assertEquals('€500.00', MoneyHelper::format(50000, $currency));
|
||||
$this->assertEquals('€5,038.29', MoneyHelper::format(503829, $currency));
|
||||
$this->assertEquals('500.00', MoneyHelper::getValue(50000, $currency));
|
||||
$this->assertEquals('5038.29', MoneyHelper::getValue(503829, $currency));
|
||||
$this->assertEquals(500, MoneyHelper::exchangeValue(50000, $currency));
|
||||
$this->assertEquals(5038.29, MoneyHelper::exchangeValue(503829, $currency));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_returns_the_amount_with_the_currency_symbol_in_the_right_locale()
|
||||
{
|
||||
App::setLocale('fr');
|
||||
|
||||
$currency = new Currency();
|
||||
$currency->iso = 'EUR';
|
||||
|
||||
$this->assertEquals('500,00 €', MoneyHelper::format(50000, $currency));
|
||||
$this->assertEquals('5 038,29 €', MoneyHelper::format(503829, $currency));
|
||||
$this->assertEquals('500,00', MoneyHelper::getValue(50000, $currency));
|
||||
$this->assertEquals('5038,29', MoneyHelper::getValue(503829, $currency));
|
||||
$this->assertEquals(500, MoneyHelper::exchangeValue(50000, $currency));
|
||||
$this->assertEquals(5038.29, MoneyHelper::exchangeValue(503829, $currency));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_returns_the_amount_with_the_currency_symbol_with_the_right_punctuation()
|
||||
{
|
||||
$currency = new Currency();
|
||||
$currency->iso = 'JPY'; // minorUnit value is zero "0"
|
||||
|
||||
$this->assertEquals('¥500', MoneyHelper::format(500, $currency));
|
||||
$this->assertEquals('¥5,038', MoneyHelper::format(5038, $currency));
|
||||
$this->assertEquals('500', MoneyHelper::getValue(500, $currency));
|
||||
$this->assertEquals('5038', MoneyHelper::getValue(5038, $currency));
|
||||
$this->assertEquals(500, MoneyHelper::exchangeValue(500, $currency));
|
||||
$this->assertEquals(5038, MoneyHelper::exchangeValue(5038, $currency));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_formats_the_currency_with_the_right_locale()
|
||||
{
|
||||
$currency = Currency::where('iso', 'GBP')->first();
|
||||
$user = factory(User::class)->create([
|
||||
'currency_id' => $currency->id,
|
||||
]);
|
||||
$this->actingAs($user);
|
||||
|
||||
$this->assertEquals('£75.00', MoneyHelper::format(7500, $currency));
|
||||
$this->assertEquals('£2,734.12', MoneyHelper::format(273412, $currency));
|
||||
$this->assertEquals('75.00', MoneyHelper::getValue(7500, $currency));
|
||||
$this->assertEquals('2734.12', MoneyHelper::getValue(273412, $currency));
|
||||
$this->assertEquals(75, MoneyHelper::exchangeValue(7500, $currency));
|
||||
$this->assertEquals(2734.12, MoneyHelper::exchangeValue(273412, $currency));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_returns_the_amount_without_the_currency_symbol_if_not_provided()
|
||||
{
|
||||
$this->assertEquals('500', MoneyHelper::format(500));
|
||||
$this->assertEquals('5,000', MoneyHelper::format(5000));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_returns_zero_if_amount_is_null()
|
||||
{
|
||||
$this->assertEquals('0', MoneyHelper::format(null));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_covers_brazilian_currency()
|
||||
{
|
||||
$currency = Currency::where('iso', 'BRL')->first();
|
||||
|
||||
$user = factory(User::class)->create([
|
||||
'currency_id' => $currency->id,
|
||||
]);
|
||||
$this->actingAs($user);
|
||||
|
||||
$this->assertEquals('R$12,345.67', MoneyHelper::format(1234567, $currency));
|
||||
$this->assertEquals('12345.67', MoneyHelper::getValue(1234567, $currency));
|
||||
$this->assertEquals(12345.67, MoneyHelper::exchangeValue(1234567, $currency));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_parse_an_input_value()
|
||||
{
|
||||
$currency = new Currency();
|
||||
$currency->iso = 'EUR';
|
||||
|
||||
$this->assertEquals(50000, MoneyHelper::parseInput('500.00', $currency));
|
||||
$this->assertEquals(503829, MoneyHelper::parseInput('5038.29', $currency));
|
||||
}
|
||||
}
|
||||
106
tests/Unit/Helpers/RequestHelperTest.php
Normal file
106
tests/Unit/Helpers/RequestHelperTest.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Mockery\MockInterface;
|
||||
use App\Helpers\RequestHelper;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Request;
|
||||
use Stevebauman\Location\Facades\Location;
|
||||
|
||||
class RequestHelperTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function get_cf_ip()
|
||||
{
|
||||
Request::instance()->headers->set('Cf-Connecting-Ip', '1.2.3.4');
|
||||
|
||||
$this->assertEquals(
|
||||
'1.2.3.4',
|
||||
RequestHelper::ip()
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function get_server_ip()
|
||||
{
|
||||
Request::instance()->server->set('REMOTE_ADDR', '1.2.3.4');
|
||||
|
||||
$this->assertEquals(
|
||||
'1.2.3.4',
|
||||
RequestHelper::ip()
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function get_country_from_cf()
|
||||
{
|
||||
Request::instance()->headers->set('Cf-Ipcountry', 'XX');
|
||||
|
||||
$this->assertEquals(
|
||||
'XX',
|
||||
RequestHelper::country('1.2.3.4')
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function get_country_from_ip()
|
||||
{
|
||||
$driver = $this->mock(\Stevebauman\Location\Drivers\Driver::class, function (MockInterface $mock) {
|
||||
$mock->shouldReceive('get')
|
||||
->with('123.45.67.89')
|
||||
->andReturn(tap(new \Stevebauman\Location\Position(), function ($position) {
|
||||
$position->countryCode = 'TEST';
|
||||
}));
|
||||
});
|
||||
Location::setDriver($driver);
|
||||
|
||||
Request::instance()->server->set('REMOTE_ADDR', '123.45.67.89');
|
||||
|
||||
$this->assertEquals(
|
||||
'TEST',
|
||||
RequestHelper::country(null)
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function get_infos_from_ip()
|
||||
{
|
||||
config(['location.ipdata.token' => 'test']);
|
||||
|
||||
$body = file_get_contents(base_path('tests/Fixtures/Helpers/ipdata.json'));
|
||||
Http::fake([
|
||||
'https://api.ipdata.co/*' => Http::response($body, 200),
|
||||
]);
|
||||
|
||||
$this->assertEquals(
|
||||
[
|
||||
'country' => 'FR',
|
||||
'currency' => 'EUR',
|
||||
'timezone' => 'Europe/Paris',
|
||||
],
|
||||
RequestHelper::infos('test')
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function get_infos_from_ip_fail()
|
||||
{
|
||||
config(['location.ipdata.token' => 'test']);
|
||||
|
||||
Http::fake([
|
||||
'https://api.ipdata.co/*' => Http::response(null, 500),
|
||||
]);
|
||||
Request::instance()->headers->set('Cf-Ipcountry', 'XX');
|
||||
|
||||
$this->assertEquals(
|
||||
[
|
||||
'country' => 'XX',
|
||||
'currency' => null,
|
||||
'timezone' => null,
|
||||
],
|
||||
RequestHelper::infos('test')
|
||||
);
|
||||
}
|
||||
}
|
||||
81
tests/Unit/Helpers/SearchHelperTest.php
Normal file
81
tests/Unit/Helpers/SearchHelperTest.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use Tests\FeatureTestCase;
|
||||
use App\Models\Contact\Note;
|
||||
use App\Helpers\SearchHelper;
|
||||
use App\Models\Contact\Contact;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class SearchHelperTest extends FeatureTestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function searching_for_contacts_returns_a_collection_with_pagination()
|
||||
{
|
||||
$user = $this->signin();
|
||||
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $user->account_id,
|
||||
]);
|
||||
$searchResults = SearchHelper::searchContacts($contact->first_name, 'created_at')
|
||||
->paginate(1);
|
||||
|
||||
$this->assertNotNull($searchResults);
|
||||
$this->assertInstanceOf('Illuminate\Pagination\LengthAwarePaginator', $searchResults);
|
||||
$this->assertCount(1, $searchResults);
|
||||
}
|
||||
|
||||
/** disabled for now */
|
||||
public function searching_with_notes()
|
||||
{
|
||||
$user = $this->signin();
|
||||
|
||||
$note = factory(Note::class)->create([
|
||||
'account_id' => $user->account_id,
|
||||
'body' => 'we met on github and talked about monica',
|
||||
]);
|
||||
|
||||
$searchResults = SearchHelper::searchContacts('monica', 'created_at')
|
||||
->paginate(1);
|
||||
|
||||
$this->assertNotNull($searchResults);
|
||||
$this->assertInstanceOf('Illuminate\Pagination\LengthAwarePaginator', $searchResults);
|
||||
$this->assertCount(1, $searchResults);
|
||||
}
|
||||
|
||||
/** disabled for now */
|
||||
public function searching_with_introduction_information()
|
||||
{
|
||||
$user = $this->signin();
|
||||
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $user->account_id,
|
||||
'first_met_additional_info' => 'github',
|
||||
]);
|
||||
$searchResults = SearchHelper::searchContacts($contact->first_met_additional_info, 'created_at')
|
||||
->paginate(1);
|
||||
|
||||
$this->assertNotNull($searchResults);
|
||||
$this->assertInstanceOf('Illuminate\Pagination\LengthAwarePaginator', $searchResults);
|
||||
$this->assertCount(1, $searchResults);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function searching_with_wrong_search_field()
|
||||
{
|
||||
$user = $this->signin();
|
||||
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $user->account_id,
|
||||
]);
|
||||
$searchResults = SearchHelper::searchContacts('wrongsearchfield:1', 'created_at')
|
||||
->paginate(1);
|
||||
|
||||
$this->assertNotNull($searchResults);
|
||||
$this->assertInstanceOf('Illuminate\Pagination\LengthAwarePaginator', $searchResults);
|
||||
$this->assertCount(0, $searchResults);
|
||||
}
|
||||
}
|
||||
68
tests/Unit/Helpers/StorageHelperTest.php
Normal file
68
tests/Unit/Helpers/StorageHelperTest.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Photo;
|
||||
use App\Helpers\StorageHelper;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Contact\Document;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class StorageHelperTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_calculates_the_account_storage_size(): void
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
|
||||
factory(Document::class)->create([
|
||||
'filesize' => 1000000,
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
factory(Photo::class)->create([
|
||||
'filesize' => 1000000,
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$this->assertEquals(
|
||||
2000000,
|
||||
StorageHelper::getAccountStorageSize($account)
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_tests_account_storage_limit(): void
|
||||
{
|
||||
config(['monica.requires_subscription' => true]);
|
||||
$account = factory(Account::class)->create([]);
|
||||
|
||||
factory(Document::class)->create([
|
||||
'filesize' => 1000000,
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
config(['monica.max_storage_size' => 0.1]);
|
||||
$this->assertTrue(StorageHelper::hasReachedAccountStorageLimit($account));
|
||||
|
||||
config(['monica.max_storage_size' => 1]);
|
||||
$this->assertFalse(StorageHelper::hasReachedAccountStorageLimit($account));
|
||||
|
||||
factory(Photo::class)->create([
|
||||
'filesize' => 1000000,
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
config(['monica.max_storage_size' => 2]);
|
||||
$this->assertFalse(StorageHelper::hasReachedAccountStorageLimit($account));
|
||||
|
||||
config(['monica.max_storage_size' => 1]);
|
||||
$this->assertTrue(StorageHelper::hasReachedAccountStorageLimit($account));
|
||||
|
||||
config(['monica.requires_subscription' => false]);
|
||||
$this->assertFalse(StorageHelper::hasReachedAccountStorageLimit($account));
|
||||
}
|
||||
}
|
||||
26
tests/Unit/Helpers/VCardHelperTest.php
Normal file
26
tests/Unit/Helpers/VCardHelperTest.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use Tests\FeatureTestCase;
|
||||
use App\Helpers\VCardHelper;
|
||||
use Sabre\VObject\Component\VCard;
|
||||
|
||||
class VCardHelperTest extends FeatureTestCase
|
||||
{
|
||||
/** @test */
|
||||
public function it_get_country_by_sabre_vcard()
|
||||
{
|
||||
$vcard = new VCard([
|
||||
'TEL' => '202-555-0191',
|
||||
'ADR' => ['', '', '17 Shakespeare Ave.', 'Southampton', '', 'SO17 2HB', 'United Kingdom'],
|
||||
]);
|
||||
|
||||
$iso = VCardHelper::getCountryISOFromSabreVCard($vcard);
|
||||
|
||||
$this->assertEquals(
|
||||
'GB',
|
||||
$iso
|
||||
);
|
||||
}
|
||||
}
|
||||
48
tests/Unit/Helpers/WeatherHelperTest.php
Normal file
48
tests/Unit/Helpers/WeatherHelperTest.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Helpers;
|
||||
|
||||
use Tests\FeatureTestCase;
|
||||
use App\Helpers\WeatherHelper;
|
||||
use App\Jobs\GetGPSCoordinate;
|
||||
use App\Models\Contact\Address;
|
||||
use App\Models\Contact\Contact;
|
||||
use Illuminate\Bus\PendingBatch;
|
||||
use App\Jobs\GetWeatherInformation;
|
||||
use Illuminate\Support\Facades\Bus;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class WeatherHelperTest extends FeatureTestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_returns_null_if_address_is_not_set()
|
||||
{
|
||||
$contact = factory(Contact::class)->create([]);
|
||||
$this->assertNull(WeatherHelper::getWeatherForAddress($contact->addresses()->first()));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_dispatch_batch_with_get_coordinates()
|
||||
{
|
||||
config(['monica.enable_geolocation' => true]);
|
||||
config(['monica.location_iq_api_key' => 'test']);
|
||||
config(['monica.enable_weather' => true]);
|
||||
config(['monica.weatherapi_key' => 'test']);
|
||||
|
||||
$fake = Bus::fake();
|
||||
|
||||
$address = factory(Address::class)->create();
|
||||
|
||||
WeatherHelper::getWeatherForAddress($address);
|
||||
|
||||
$fake->assertBatched(function (PendingBatch $pendingBatch) {
|
||||
$this->assertCount(2, $pendingBatch->jobs);
|
||||
$this->assertInstanceOf(GetGPSCoordinate::class, $pendingBatch->jobs[0]);
|
||||
$this->assertInstanceOf(GetWeatherInformation::class, $pendingBatch->jobs[1]);
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user