diff --git a/app/Domains/Contact/ManageLifeEvents/Services/CreateLifeEvent.php b/app/Domains/Contact/ManageLifeEvents/Services/CreateLifeEvent.php index da5df0581..dac8c9d69 100644 --- a/app/Domains/Contact/ManageLifeEvents/Services/CreateLifeEvent.php +++ b/app/Domains/Contact/ManageLifeEvents/Services/CreateLifeEvent.php @@ -39,7 +39,8 @@ class CreateLifeEvent extends BaseService implements ServiceInterface 'currency_id' => 'nullable|integer|exists:currencies,id', 'paid_by_contact_id' => 'nullable|integer|exists:contacts,id', 'duration_in_minutes' => 'nullable|integer', - 'distance_in_km' => 'nullable|integer', + 'distance' => 'nullable|integer', + 'distance_unit' => 'nullable|string|max:255', 'from_place' => 'nullable|string|max:255', 'to_place' => 'nullable|string|max:255', 'place' => 'nullable|string|max:255', @@ -129,7 +130,8 @@ class CreateLifeEvent extends BaseService implements ServiceInterface 'currency_id' => $this->valueOrNull($this->data, 'currency_id'), 'paid_by_contact_id' => $this->valueOrNull($this->data, 'paid_by_contact_id'), 'duration_in_minutes' => $this->valueOrNull($this->data, 'duration_in_minutes'), - 'distance_in_km' => $this->valueOrNull($this->data, 'distance_in_km'), + 'distance' => $this->valueOrNull($this->data, 'distance'), + 'distance_unit' => $this->valueOrNull($this->data, 'distance_unit'), 'from_place' => $this->valueOrNull($this->data, 'from_place'), 'to_place' => $this->valueOrNull($this->data, 'to_place'), 'place' => $this->valueOrNull($this->data, 'place'), diff --git a/app/Domains/Contact/ManageLifeEvents/Services/UpdateLifeEvent.php b/app/Domains/Contact/ManageLifeEvents/Services/UpdateLifeEvent.php index cd7007ef2..65bdcaeea 100644 --- a/app/Domains/Contact/ManageLifeEvents/Services/UpdateLifeEvent.php +++ b/app/Domains/Contact/ManageLifeEvents/Services/UpdateLifeEvent.php @@ -40,7 +40,8 @@ class UpdateLifeEvent extends BaseService implements ServiceInterface 'currency_id' => 'nullable|integer|exists:currencies,id', 'paid_by_contact_id' => 'nullable|integer|exists:contacts,id', 'duration_in_minutes' => 'nullable|integer', - 'distance_in_km' => 'nullable|integer', + 'distance' => 'nullable|integer', + 'distance_unit' => 'nullable|string|max:255', 'from_place' => 'nullable|string|max:255', 'to_place' => 'nullable|string|max:255', 'place' => 'nullable|string|max:255', @@ -114,7 +115,8 @@ class UpdateLifeEvent extends BaseService implements ServiceInterface $this->lifeEvent->currency_id = $this->valueOrNull($this->data, 'currency_id'); $this->lifeEvent->paid_by_contact_id = $this->valueOrNull($this->data, 'paid_by_contact_id'); $this->lifeEvent->duration_in_minutes = $this->valueOrNull($this->data, 'duration_in_minutes'); - $this->lifeEvent->distance_in_km = $this->valueOrNull($this->data, 'distance_in_km'); + $this->lifeEvent->distance = $this->valueOrNull($this->data, 'distance'); + $this->lifeEvent->distance_unit = $this->valueOrNull($this->data, 'distance_unit'); $this->lifeEvent->from_place = $this->valueOrNull($this->data, 'from_place'); $this->lifeEvent->to_place = $this->valueOrNull($this->data, 'to_place'); $this->lifeEvent->place = $this->valueOrNull($this->data, 'place'); diff --git a/app/Domains/Contact/ManageLifeEvents/Web/Controllers/ContactModuleLifeEventController.php b/app/Domains/Contact/ManageLifeEvents/Web/Controllers/ContactModuleLifeEventController.php index b3a773374..5da18d8c3 100644 --- a/app/Domains/Contact/ManageLifeEvents/Web/Controllers/ContactModuleLifeEventController.php +++ b/app/Domains/Contact/ManageLifeEvents/Web/Controllers/ContactModuleLifeEventController.php @@ -40,7 +40,8 @@ class ContactModuleLifeEventController extends Controller 'currency_id' => $request->input('currency_id'), 'paid_by_contact_id' => $request->input('paid_by_contact_id'), 'duration_in_minutes' => $request->input('duration_in_minutes'), - 'distance_in_km' => $request->input('distance_in_km'), + 'distance' => $request->input('distance'), + 'distance_unit' => $request->input('distance_unit'), 'from_place' => $request->input('from_place'), 'to_place' => $request->input('to_place'), 'place' => $request->input('place'), diff --git a/app/Domains/Contact/ManageLifeEvents/Web/Controllers/ContactModuleTimelineEventController.php b/app/Domains/Contact/ManageLifeEvents/Web/Controllers/ContactModuleTimelineEventController.php index 12edda14a..b35dab78d 100644 --- a/app/Domains/Contact/ManageLifeEvents/Web/Controllers/ContactModuleTimelineEventController.php +++ b/app/Domains/Contact/ManageLifeEvents/Web/Controllers/ContactModuleTimelineEventController.php @@ -67,7 +67,8 @@ class ContactModuleTimelineEventController extends Controller 'currency_id' => $request->input('currency_id'), 'paid_by_contact_id' => $request->input('paid_by_contact_id'), 'duration_in_minutes' => $request->input('duration_in_minutes'), - 'distance_in_km' => $request->input('distance_in_km'), + 'distance' => $request->input('distance'), + 'distance_unit' => $request->input('distance_unit'), 'from_place' => $request->input('from_place'), 'to_place' => $request->input('to_place'), 'place' => $request->input('place'), diff --git a/app/Domains/Contact/ManageLifeEvents/Web/ViewHelpers/ModuleLifeEventViewHelper.php b/app/Domains/Contact/ManageLifeEvents/Web/ViewHelpers/ModuleLifeEventViewHelper.php index e72ee469b..c0a11f2c1 100644 --- a/app/Domains/Contact/ManageLifeEvents/Web/ViewHelpers/ModuleLifeEventViewHelper.php +++ b/app/Domains/Contact/ManageLifeEvents/Web/ViewHelpers/ModuleLifeEventViewHelper.php @@ -4,6 +4,7 @@ namespace App\Domains\Contact\ManageLifeEvents\Web\ViewHelpers; use App\Helpers\ContactCardHelper; use App\Helpers\DateHelper; +use App\Helpers\DistanceHelper; use App\Models\Contact; use App\Models\LifeEvent; use App\Models\LifeEventCategory; @@ -113,7 +114,8 @@ class ModuleLifeEventViewHelper 'currency_id' => $lifeEvent->currency_id, 'paid_by_contact_id' => $lifeEvent->paid_by_contact_id, 'duration_in_minutes' => $lifeEvent->duration_in_minutes, - 'distance_in_km' => $lifeEvent->distance_in_km, + 'distance' => $lifeEvent->distance && $lifeEvent->distance_unit ? DistanceHelper::format($user, $lifeEvent->distance, $lifeEvent->distance_unit) : null, + 'distance_unit' => $lifeEvent->distance_unit, 'from_place' => $lifeEvent->from_place, 'to_place' => $lifeEvent->to_place, 'place' => $lifeEvent->place, diff --git a/app/Domains/Settings/ManageUserPreferences/Services/StoreDistanceFormatPreference.php b/app/Domains/Settings/ManageUserPreferences/Services/StoreDistanceFormatPreference.php new file mode 100644 index 000000000..127448bfd --- /dev/null +++ b/app/Domains/Settings/ManageUserPreferences/Services/StoreDistanceFormatPreference.php @@ -0,0 +1,53 @@ + 'required|integer|exists:accounts,id', + 'author_id' => 'required|integer|exists:users,id', + 'distance_format' => 'required|string|max:255', + ]; + } + + /** + * Get the permissions that apply to the user calling the service. + */ + public function permissions(): array + { + return [ + 'author_must_belong_to_account', + ]; + } + + /** + * Store distance format preferences for the given user. + */ + public function execute(array $data): User + { + $this->data = $data; + + $this->validateRules($data); + $this->updateUser(); + + return $this->author; + } + + private function updateUser(): void + { + $this->author->distance_format = $this->data['distance_format']; + $this->author->save(); + } +} diff --git a/app/Domains/Settings/ManageUserPreferences/Web/Controllers/PreferencesDistanceFormatController.php b/app/Domains/Settings/ManageUserPreferences/Web/Controllers/PreferencesDistanceFormatController.php new file mode 100644 index 000000000..da57fbb2f --- /dev/null +++ b/app/Domains/Settings/ManageUserPreferences/Web/Controllers/PreferencesDistanceFormatController.php @@ -0,0 +1,27 @@ + Auth::user()->account_id, + 'author_id' => Auth::id(), + 'distance_format' => $request->input('distanceFormat'), + ]; + + $user = (new StoreDistanceFormatPreference())->execute($data); + + return response()->json([ + 'data' => UserPreferencesIndexViewHelper::dtoDistanceFormat($user), + ], 200); + } +} diff --git a/app/Domains/Settings/ManageUserPreferences/Web/ViewHelpers/UserPreferencesIndexViewHelper.php b/app/Domains/Settings/ManageUserPreferences/Web/ViewHelpers/UserPreferencesIndexViewHelper.php index 811a12b92..ac0eabc9d 100644 --- a/app/Domains/Settings/ManageUserPreferences/Web/ViewHelpers/UserPreferencesIndexViewHelper.php +++ b/app/Domains/Settings/ManageUserPreferences/Web/ViewHelpers/UserPreferencesIndexViewHelper.php @@ -17,6 +17,7 @@ class UserPreferencesIndexViewHelper 'date_format' => self::dtoDateFormat($user), 'timezone' => self::dtoTimezone($user), 'number_format' => self::dtoNumberFormat($user), + 'distance_format' => self::dtoDistanceFormat($user), 'maps' => self::dtoMapsPreferences($user), 'locale' => self::dtoLocale($user), 'url' => [ @@ -131,6 +132,16 @@ class UserPreferencesIndexViewHelper ]; } + public static function dtoDistanceFormat(User $user): array + { + return [ + 'number_format' => $user->distance_format, + 'url' => [ + 'store' => route('settings.preferences.distance.store'), + ], + ]; + } + public static function dtoMapsPreferences(User $user): array { $collection = collect(); diff --git a/app/Helpers/DistanceHelper.php b/app/Helpers/DistanceHelper.php new file mode 100644 index 000000000..c6162cfe5 --- /dev/null +++ b/app/Helpers/DistanceHelper.php @@ -0,0 +1,22 @@ +distance_format === User::DISTANCE_UNIT_KM && $unit === User::DISTANCE_UNIT_MILES) { + $distance = round($distance * 1.609344, 2); + } elseif ($user->distance_format === User::DISTANCE_UNIT_MILES && $unit === User::DISTANCE_UNIT_KM) { + $distance = round($distance / 1.609344, 2); + } + + return trans('app.distance_format_'.$user->distance_format, ['distance' => $distance]); + } +} diff --git a/app/Models/LifeEvent.php b/app/Models/LifeEvent.php index f2d4f6f91..d9b486414 100644 --- a/app/Models/LifeEvent.php +++ b/app/Models/LifeEvent.php @@ -30,7 +30,8 @@ class LifeEvent extends Model 'currency_id', 'paid_by_contact_id', 'duration_in_minutes', - 'distance_in_km', + 'distance', + 'distance_unit', 'from_place', 'to_place', 'place', diff --git a/app/Models/User.php b/app/Models/User.php index f98fd7940..0c857dfe6 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -40,6 +40,13 @@ class User extends Authenticatable implements MustVerifyEmail, HasLocalePreferen public const MAPS_SITE_OPEN_STREET_MAPS = 'open_street_maps'; + /** + * Possible distance unit. + */ + public const DISTANCE_UNIT_MILES = 'mi'; + + public const DISTANCE_UNIT_KM = 'km'; + /** * The attributes that are mass assignable. * @@ -58,6 +65,7 @@ class User extends Authenticatable implements MustVerifyEmail, HasLocalePreferen 'name_order', 'date_format', 'number_format', + 'distance_format', 'timezone', 'default_map_site', 'locale', diff --git a/database/factories/LifeEventFactory.php b/database/factories/LifeEventFactory.php index 7d5b5aab1..43872b719 100644 --- a/database/factories/LifeEventFactory.php +++ b/database/factories/LifeEventFactory.php @@ -32,7 +32,7 @@ class LifeEventFactory extends Factory 'currency_id' => null, 'paid_by_contact_id' => Contact::factory(), 'duration_in_minutes' => $this->faker->randomNumber(), - 'distance_in_km' => $this->faker->randomNumber(), + 'distance' => $this->faker->randomNumber(), 'from_place' => $this->faker->city(), 'to_place' => $this->faker->city(), 'place' => $this->faker->city(), diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index 3babdda87..7b46aa22a 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -25,6 +25,7 @@ return new class() extends Migration $table->string('timezone')->nullable(); $table->string('number_format')->default(User::NUMBER_FORMAT_TYPE_COMMA_THOUSANDS_DOT_DECIMAL); $table->string('default_map_site')->default(User::MAPS_SITE_OPEN_STREET_MAPS); + $table->string('distance_format')->default(User::DISTANCE_UNIT_MILES); $table->string('password')->nullable(); $table->boolean('is_account_administrator')->default(false); $table->boolean('help_shown')->default(true); diff --git a/database/migrations/2022_05_17_155546_create_life_events_table.php b/database/migrations/2022_05_17_155546_create_life_events_table.php index 32bcc10c4..9a2aae4e6 100644 --- a/database/migrations/2022_05_17_155546_create_life_events_table.php +++ b/database/migrations/2022_05_17_155546_create_life_events_table.php @@ -74,7 +74,8 @@ return new class() extends Migration $table->unsignedBigInteger('currency_id')->nullable(); $table->unsignedBigInteger('paid_by_contact_id')->nullable(); $table->integer('duration_in_minutes')->nullable(); - $table->integer('distance_in_km')->nullable(); + $table->integer('distance')->nullable(); + $table->char('distance_unit', 2)->nullable(); $table->string('from_place')->nullable(); $table->string('to_place')->nullable(); $table->string('place')->nullable(); diff --git a/lang/en/app.php b/lang/en/app.php index 76b4b5a87..7e44d0ef8 100644 --- a/lang/en/app.php +++ b/lang/en/app.php @@ -133,4 +133,7 @@ return [ 'min_read' => '{count} min read', 'word_count' => '{count} words', + + 'distance_format_km' => ':distance km', + 'distance_format_mi' => ':distance miles', ]; diff --git a/lang/en/settings.php b/lang/en/settings.php index b40e832ba..0902a0e1c 100644 --- a/lang/en/settings.php +++ b/lang/en/settings.php @@ -39,6 +39,11 @@ return [ 'user_preferences_number_format_title' => 'How should we display numerical values', 'user_preferences_number_format_description' => 'Current way of displaying numbers:', + 'user_preferences_distance_format_title' => 'How should we display distance values', + 'user_preferences_distance_format_description' => 'Current way of displaying distances:', + 'user_preferences_distance_format_km' => 'kilometers (km)', + 'user_preferences_distance_format_miles' => 'miles (mi)', + 'user_preferences_timezone_title' => 'Timezone', 'user_preferences_timezone_description' => 'Regardless of where you are located in the world, have dates displayed in your own timezone.', 'user_preferences_timezone_current' => 'Current timezone:', diff --git a/resources/js/Pages/Settings/Preferences/Index.vue b/resources/js/Pages/Settings/Preferences/Index.vue index 6e5aaf32c..0180cf8cf 100644 --- a/resources/js/Pages/Settings/Preferences/Index.vue +++ b/resources/js/Pages/Settings/Preferences/Index.vue @@ -43,6 +43,8 @@ + + @@ -56,6 +58,7 @@ import Layout from '@/Shared/Layout.vue'; import NameOrder from '@/Pages/Settings/Preferences/Partials/NameOrder.vue'; import DateFormat from '@/Pages/Settings/Preferences/Partials/DateFormat.vue'; import NumberFormat from '@/Pages/Settings/Preferences/Partials/NumberFormat.vue'; +import DistanceFormat from '@/Pages/Settings/Preferences/Partials/DistanceFormat.vue'; import Timezone from '@/Pages/Settings/Preferences/Partials/Timezone.vue'; import Maps from '@/Pages/Settings/Preferences/Partials/Maps.vue'; import Locale from '@/Pages/Settings/Preferences/Partials/Locale.vue'; @@ -68,6 +71,7 @@ export default { DateFormat, Timezone, NumberFormat, + DistanceFormat, Maps, Locale, HelpPreference, diff --git a/resources/js/Pages/Settings/Preferences/Partials/DistanceFormat.vue b/resources/js/Pages/Settings/Preferences/Partials/DistanceFormat.vue new file mode 100644 index 000000000..40dfee8f9 --- /dev/null +++ b/resources/js/Pages/Settings/Preferences/Partials/DistanceFormat.vue @@ -0,0 +1,141 @@ + + + + + diff --git a/resources/js/Pages/Vault/Journal/Show.vue b/resources/js/Pages/Vault/Journal/Show.vue index c9932f5d8..54bf2ece6 100644 --- a/resources/js/Pages/Vault/Journal/Show.vue +++ b/resources/js/Pages/Vault/Journal/Show.vue @@ -130,7 +130,7 @@ defineProps({
-
+
+ +
+ +

Group journal entries together with slices of life.

+
+
View all slices props.openModal, @@ -55,8 +59,10 @@ const resetModal = () => { form.summary = null; form.description = null; + form.distance = 0; addSummaryFieldShown.value = false; addDescriptionFieldShown.value = false; + addDistanceFieldShown.value = false; }; const loadTypes = (category) => { @@ -92,6 +98,15 @@ const showAddDescriptionField = () => { }); }; +const showAddDistanceField = () => { + form.distance = null; + addDistanceFieldShown.value = true; + + nextTick(() => { + distanceField.value.focus(); + }); +}; + const store = () => { loadingState.value = 'loading'; @@ -278,6 +293,57 @@ const store = () => { :textarea-class="'block w-full'" />
+ +
+ + +
    +
  • +
    + + +
    +
  • + +
  • +
    + + +
    +
  • +
+
+
@@ -297,6 +363,15 @@ const store = () => { >+ description
+ + +
+ + distance + +
diff --git a/resources/js/Shared/Modules/LifeEvent.vue b/resources/js/Shared/Modules/LifeEvent.vue index a01204349..af09034ca 100644 --- a/resources/js/Shared/Modules/LifeEvent.vue +++ b/resources/js/Shared/Modules/LifeEvent.vue @@ -261,22 +261,50 @@ const toggleLifeEventVisibility = (lifeEvent) => { {{ lifeEvent.description }}
- +
- - - + +
+ + + - {{ lifeEvent.happened_at }} + {{ lifeEvent.happened_at }} +
+ + +
+ + + + + + + + + + + {{ lifeEvent.distance }} +
diff --git a/routes/web.php b/routes/web.php index f073dc735..de08888ee 100644 --- a/routes/web.php +++ b/routes/web.php @@ -85,6 +85,7 @@ use App\Domains\Settings\ManageTemplates\Web\Controllers\PersonalizeTemplatePage use App\Domains\Settings\ManageTemplates\Web\Controllers\PersonalizeTemplatesController; use App\Domains\Settings\ManageUserPreferences\Web\Controllers\PreferencesController; use App\Domains\Settings\ManageUserPreferences\Web\Controllers\PreferencesDateFormatController; +use App\Domains\Settings\ManageUserPreferences\Web\Controllers\PreferencesDistanceFormatController; use App\Domains\Settings\ManageUserPreferences\Web\Controllers\PreferencesHelpController; use App\Domains\Settings\ManageUserPreferences\Web\Controllers\PreferencesLocaleController; use App\Domains\Settings\ManageUserPreferences\Web\Controllers\PreferencesMapsPreferenceController; @@ -511,6 +512,7 @@ Route::middleware([ Route::post('date', [PreferencesDateFormatController::class, 'store'])->name('date.store'); Route::post('timezone', [PreferencesTimezoneController::class, 'store'])->name('timezone.store'); Route::post('number', [PreferencesNumberFormatController::class, 'store'])->name('number.store'); + Route::post('distance', [PreferencesDistanceFormatController::class, 'store'])->name('distance.store'); Route::post('maps', [PreferencesMapsPreferenceController::class, 'store'])->name('maps.store'); Route::post('locale', [PreferencesLocaleController::class, 'store'])->name('locale.store'); Route::post('help', [PreferencesHelpController::class, 'store'])->name('help.store'); diff --git a/tests/Unit/Domains/Contact/ManageLifeEvents/Services/CreateLifeEventTest.php b/tests/Unit/Domains/Contact/ManageLifeEvents/Services/CreateLifeEventTest.php index 56221c3bd..1aebee13f 100644 --- a/tests/Unit/Domains/Contact/ManageLifeEvents/Services/CreateLifeEventTest.php +++ b/tests/Unit/Domains/Contact/ManageLifeEvents/Services/CreateLifeEventTest.php @@ -158,7 +158,8 @@ class CreateLifeEventTest extends TestCase 'currency_id' => null, 'paid_by_contact_id' => null, 'duration_in_minutes' => null, - 'distance_in_km' => null, + 'distance' => null, + 'distance_unit' => null, 'from_place' => null, 'to_place' => null, 'place' => null, diff --git a/tests/Unit/Domains/Contact/ManageLifeEvents/Services/UpdateLifeEventTest.php b/tests/Unit/Domains/Contact/ManageLifeEvents/Services/UpdateLifeEventTest.php index c828ab3d8..939eebdb3 100644 --- a/tests/Unit/Domains/Contact/ManageLifeEvents/Services/UpdateLifeEventTest.php +++ b/tests/Unit/Domains/Contact/ManageLifeEvents/Services/UpdateLifeEventTest.php @@ -196,7 +196,8 @@ class UpdateLifeEventTest extends TestCase 'currency_id' => null, 'paid_by_contact_id' => null, 'duration_in_minutes' => null, - 'distance_in_km' => null, + 'distance' => null, + 'distance_unit' => null, 'from_place' => null, 'to_place' => null, 'place' => null, diff --git a/tests/Unit/Domains/Contact/ManageLifeEvents/Web/ViewHelpers/ModuleLifeEventViewHelperTest.php b/tests/Unit/Domains/Contact/ManageLifeEvents/Web/ViewHelpers/ModuleLifeEventViewHelperTest.php index a86d56943..0ab95a881 100644 --- a/tests/Unit/Domains/Contact/ManageLifeEvents/Web/ViewHelpers/ModuleLifeEventViewHelperTest.php +++ b/tests/Unit/Domains/Contact/ManageLifeEvents/Web/ViewHelpers/ModuleLifeEventViewHelperTest.php @@ -193,7 +193,7 @@ class ModuleLifeEventViewHelperTest extends TestCase $array = ModuleLifeEventViewHelper::dtoLifeEvent($lifeEvent, $user, $contact); $this->assertEquals( - 18, + 19, count($array) ); @@ -207,7 +207,8 @@ class ModuleLifeEventViewHelperTest extends TestCase $this->assertArrayHasKey('currency_id', $array); $this->assertArrayHasKey('paid_by_contact_id', $array); $this->assertArrayHasKey('duration_in_minutes', $array); - $this->assertArrayHasKey('distance_in_km', $array); + $this->assertArrayHasKey('distance', $array); + $this->assertArrayHasKey('distance_unit', $array); $this->assertArrayHasKey('from_place', $array); $this->assertArrayHasKey('to_place', $array); $this->assertArrayHasKey('place', $array); diff --git a/tests/Unit/Domains/Settings/ManageUserPreferences/Services/StoreDistanceFormatPreferenceTest.php b/tests/Unit/Domains/Settings/ManageUserPreferences/Services/StoreDistanceFormatPreferenceTest.php new file mode 100644 index 000000000..aa81ba1d1 --- /dev/null +++ b/tests/Unit/Domains/Settings/ManageUserPreferences/Services/StoreDistanceFormatPreferenceTest.php @@ -0,0 +1,69 @@ +createUser(); + $this->executeService($ross, $ross->account); + } + + /** @test */ + public function it_fails_if_wrong_parameters_are_given(): void + { + $request = [ + 'title' => 'Ross', + ]; + + $this->expectException(ValidationException::class); + (new StoreDistanceFormatPreference())->execute($request); + } + + /** @test */ + public function it_fails_if_user_doesnt_belong_to_account(): void + { + $this->expectException(ModelNotFoundException::class); + + $ross = $this->createAdministrator(); + $account = $this->createAccount(); + $this->executeService($ross, $account); + } + + private function executeService(User $author, Account $account): void + { + Queue::fake(); + + $request = [ + 'account_id' => $account->id, + 'author_id' => $author->id, + 'distance_format' => 'km', + ]; + + $user = (new StoreDistanceFormatPreference())->execute($request); + + $this->assertDatabaseHas('users', [ + 'id' => $user->id, + 'account_id' => $account->id, + 'distance_format' => 'km', + ]); + + $this->assertInstanceOf( + User::class, + $user + ); + } +} diff --git a/tests/Unit/Domains/Settings/ManageUserPreferences/Web/ViewHelpers/UserPreferencesIndexViewHelperTest.php b/tests/Unit/Domains/Settings/ManageUserPreferences/Web/ViewHelpers/UserPreferencesIndexViewHelperTest.php index 900d280fa..8f0859943 100644 --- a/tests/Unit/Domains/Settings/ManageUserPreferences/Web/ViewHelpers/UserPreferencesIndexViewHelperTest.php +++ b/tests/Unit/Domains/Settings/ManageUserPreferences/Web/ViewHelpers/UserPreferencesIndexViewHelperTest.php @@ -24,7 +24,7 @@ class UserPreferencesIndexViewHelperTest extends TestCase $array = UserPreferencesIndexViewHelper::data($user); $this->assertEquals( - 8, + 9, count($array) ); @@ -34,6 +34,7 @@ class UserPreferencesIndexViewHelperTest extends TestCase $this->assertArrayHasKey('timezone', $array); $this->assertArrayHasKey('url', $array); $this->assertArrayHasKey('number_format', $array); + $this->assertArrayHasKey('distance_format', $array); $this->assertArrayHasKey('maps', $array); $this->assertArrayHasKey('locale', $array); diff --git a/tests/Unit/Helpers/DistanceHelperTest.php b/tests/Unit/Helpers/DistanceHelperTest.php new file mode 100644 index 000000000..f6e665022 --- /dev/null +++ b/tests/Unit/Helpers/DistanceHelperTest.php @@ -0,0 +1,57 @@ +create([ + 'distance_format' => User::DISTANCE_UNIT_KM, + ]); + $this->assertEquals( + '134 km', + DistanceHelper::format($user, $distance, $unit) + ); + + $distance = 134; + $unit = User::DISTANCE_UNIT_MILES; + $user = User::factory()->create([ + 'distance_format' => User::DISTANCE_UNIT_KM, + ]); + $this->assertEquals( + '215.65 km', + DistanceHelper::format($user, $distance, $unit) + ); + + $distance = 134; + $unit = User::DISTANCE_UNIT_MILES; + $user = User::factory()->create([ + 'distance_format' => User::DISTANCE_UNIT_MILES, + ]); + $this->assertEquals( + '134 miles', + DistanceHelper::format($user, $distance, $unit) + ); + + $distance = 134; + $unit = User::DISTANCE_UNIT_KM; + $user = User::factory()->create([ + 'distance_format' => User::DISTANCE_UNIT_MILES, + ]); + $this->assertEquals( + '83.26 miles', + DistanceHelper::format($user, $distance, $unit) + ); + } +}