-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add user language preference endpoint (#86) #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| // SPDX-FileCopyrightText: 2025 SecPal Contributors | ||
| // SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
|
||
| namespace App\Http\Requests; | ||
|
|
||
| use Illuminate\Foundation\Http\FormRequest; | ||
| use Illuminate\Validation\Rule; | ||
|
|
||
| class UpdateUserLanguageRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Determine if the user is authorized to make this request. | ||
| */ | ||
| public function authorize(): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Get the validation rules that apply to the request. | ||
| * | ||
| * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return [ | ||
| 'locale' => [ | ||
| 'present', | ||
| 'nullable', | ||
| Rule::in(['en', 'de']), | ||
| ], | ||
| ]; | ||
| } | ||
kevalyq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Get the custom error messages for validation rules. | ||
| * | ||
| * @return array<string, string> | ||
| */ | ||
| public function messages(): array | ||
| { | ||
| return [ | ||
| 'locale.in' => 'Language must be either English (en) or German (de).', | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
database/migrations/2025_11_16_192506_add_preferred_locale_to_users_table.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| // SPDX-FileCopyrightText: 2025 SecPal Contributors | ||
| // SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| /** | ||
| * Run the migrations. | ||
| */ | ||
| public function up(): void | ||
| { | ||
| Schema::table('users', function (Blueprint $table) { | ||
| $table->string('preferred_locale', 5)->nullable()->after('remember_token'); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Reverse the migrations. | ||
| */ | ||
| public function down(): void | ||
| { | ||
| Schema::table('users', function (Blueprint $table) { | ||
| $table->dropColumn('preferred_locale'); | ||
| }); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
tests/Feature/Controllers/Api/V1/UserLanguagePreferenceTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| // SPDX-FileCopyrightText: 2025 SecPal Contributors | ||
| // SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
|
||
| use App\Models\User; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
|
|
||
| uses(RefreshDatabase::class); | ||
|
|
||
| describe('User Language Preference API', function () { | ||
| test('user can update their language preference to German', function () { | ||
| /** @var User $user */ | ||
| $user = User::factory()->create(['preferred_locale' => null]); | ||
|
|
||
| $response = $this->actingAs($user) | ||
| ->patchJson('/v1/me/language', [ | ||
| 'locale' => 'de', | ||
| ]); | ||
|
|
||
| $response->assertOk() | ||
| ->assertJson([ | ||
| 'data' => [ | ||
| 'preferred_locale' => 'de', | ||
| ], | ||
| ]); | ||
|
|
||
| expect($user->fresh()->preferred_locale)->toBe('de'); | ||
| }); | ||
|
|
||
| test('user can update their language preference to English', function () { | ||
| /** @var User $user */ | ||
| $user = User::factory()->create(['preferred_locale' => 'de']); | ||
|
|
||
| $response = $this->actingAs($user) | ||
| ->patchJson('/v1/me/language', [ | ||
| 'locale' => 'en', | ||
| ]); | ||
|
|
||
| $response->assertOk(); | ||
| expect($user->fresh()->preferred_locale)->toBe('en'); | ||
| }); | ||
|
|
||
| test('user can reset language preference to null', function () { | ||
| /** @var User $user */ | ||
| $user = User::factory()->create(['preferred_locale' => 'de']); | ||
|
|
||
| $response = $this->actingAs($user) | ||
| ->patchJson('/v1/me/language', [ | ||
| 'locale' => null, | ||
| ]); | ||
|
|
||
| $response->assertOk(); | ||
| expect($user->fresh()->preferred_locale)->toBeNull(); | ||
| }); | ||
|
|
||
| test('validation rejects invalid locale codes', function () { | ||
| /** @var User $user */ | ||
| $user = User::factory()->create(); | ||
|
|
||
| $response = $this->actingAs($user) | ||
| ->patchJson('/v1/me/language', [ | ||
| 'locale' => 'fr', // French not supported | ||
| ]); | ||
|
|
||
| $response->assertStatus(422) | ||
| ->assertJsonValidationErrors(['locale']); | ||
| }); | ||
|
|
||
| test('empty string is treated as null and accepted', function () { | ||
| /** @var User $user */ | ||
| $user = User::factory()->create(['preferred_locale' => 'de']); | ||
|
|
||
| $response = $this->actingAs($user) | ||
| ->patchJson('/v1/me/language', [ | ||
| 'locale' => '', | ||
| ]); | ||
|
|
||
| $response->assertOk(); | ||
| expect($user->fresh()->preferred_locale)->toBeNull(); | ||
| }); | ||
|
|
||
| test('validation requires locale field', function () { | ||
| /** @var User $user */ | ||
| $user = User::factory()->create(); | ||
|
|
||
| $response = $this->actingAs($user) | ||
| ->patchJson('/v1/me/language', []); | ||
|
|
||
| $response->assertStatus(422) | ||
| ->assertJsonValidationErrors(['locale']); | ||
| }); | ||
|
|
||
| test('unauthenticated users cannot update language preference', function () { | ||
| $response = $this->patchJson('/v1/me/language', [ | ||
| 'locale' => 'de', | ||
| ]); | ||
|
|
||
| $response->assertStatus(401); | ||
| }); | ||
|
|
||
| test('endpoint only affects authenticated user', function () { | ||
| /** @var User $user1 */ | ||
| $user1 = User::factory()->create(['preferred_locale' => null]); | ||
| /** @var User $user2 */ | ||
| $user2 = User::factory()->create(['preferred_locale' => null]); | ||
|
|
||
| $response = $this->actingAs($user1) | ||
| ->patchJson('/v1/me/language', [ | ||
| 'locale' => 'de', | ||
| ]); | ||
|
|
||
| $response->assertOk(); | ||
| expect($user1->fresh()->preferred_locale)->toBe('de'); | ||
| expect($user2->fresh()->preferred_locale)->toBeNull(); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.