diff --git a/app/Http/Controllers/Api/V1/PreferenceController.php b/app/Http/Controllers/Api/V1/PreferenceController.php index fded0ddd..52d5f646 100755 --- a/app/Http/Controllers/Api/V1/PreferenceController.php +++ b/app/Http/Controllers/Api/V1/PreferenceController.php @@ -30,7 +30,7 @@ public function index() Log::info('Preferences retrieved', ['user_id' => Auth::id(), 'preferences' => $preferences]); return response()->json([ 'status_code' => 200, - 'message' => 'Languages fetched successfully', + 'message' => 'Preferences fetched successfully', 'preferences' => $preferences ], 200); } diff --git a/app/Models/Language.php b/app/Models/Language.php index de9d6f5f..0c60f775 100644 --- a/app/Models/Language.php +++ b/app/Models/Language.php @@ -15,6 +15,8 @@ class Language extends Model // Set the key type to string protected $keyType = 'string'; + protected $fillable = ['language', 'code', 'description']; + // Disable auto-incrementing IDs public $incrementing = false; diff --git a/tests/Feature/LanguageControllerTest.php b/tests/Feature/LanguageControllerTest.php new file mode 100644 index 00000000..1f4cfb70 --- /dev/null +++ b/tests/Feature/LanguageControllerTest.php @@ -0,0 +1,249 @@ +create(); + $this->actingAs($user, 'api'); + + $response = $this->postJson('/api/v1/languages', [ + 'language' => 'Italian', + 'code' => 'it', + 'description' => 'Italian Language', + ]); + + $response->assertStatus(201) + ->assertJson([ + 'status_code' => 201, + 'message' => 'Language Created Successfully', + 'data' => [ + 'language' => 'Italian', + 'code' => 'it', + 'description' => 'Italian Language', + ], + ]); + + $this->assertDatabaseHas('languages', [ + 'language' => 'Italian', + 'code' => 'it', + 'description' => 'Italian Language', + ]); + } + + /** + * Test the creation of a language with validation errors. + * + * @return void + */ + public function testCreateLanguageValidationErrors() + { + $user = User::factory()->create(); + $this->actingAs($user, 'api'); + + $response = $this->postJson('/api/v1/languages', []); + + $response->assertStatus(400) + ->assertJson([ + 'status_code' => 400, + 'message' => 'Bad Request', + 'errors' => [ + 'language' => ['The language field is required.'], + 'code' => ['The code field is required.'], + ], + ]); + } + + /** + * Test updating an existing language. + * + * @return void + */ + public function testUpdateLanguage() + { + $user = User::factory()->create(); + $this->actingAs($user, 'api'); + + $language = Language::create([ + 'language' => 'French', + 'code' => 'fr', + 'description' => 'French Language', + ]); + + $response = $this->putJson("/api/v1/languages/{$language->id}", [ + 'language' => 'French(Updated)', + 'code' => 'fr-updated', + 'description' => 'Updated French Language', + ]); + + $response->assertStatus(200) + ->assertJson([ + 'status_code' => 200, + 'message' => 'Language Updated Successfully', + 'data' => [ + 'language' => 'French(Updated)', + 'code' => 'fr-updated', + 'description' => 'Updated French Language', + ], + ]); + + $this->assertDatabaseHas('languages', [ + 'id' => $language->id, + 'language' => 'French(Updated)', + 'code' => 'fr-updated', + 'description' => 'Updated French Language', + ]); + } + + /** + * Test updating a language that does not exist. + * + * @return void + */ + public function testUpdateNonExistentLanguage() + { + $user = User::factory()->create(); + $this->actingAs($user, 'api'); + + $nonExistentId = (string) Str::uuid(); + + $response = $this->putJson("/api/v1/languages/{$nonExistentId}", [ + 'language' => 'NonExistent', + 'code' => 'nx', + 'description' => 'Non-Existent Language', + ]); + + $response->assertStatus(404) + ->assertJson([ + 'status_code' => 404, + 'message' => 'Language not found', + ]); + } + + /** + * Test fetching the list of languages. + * + * @return void + */ + public function testFetchLanguages() +{ + $user = User::factory()->create(); + $this->actingAs($user, 'api'); + + // Manually create languages + $languages = [ + Language::create([ + 'id' => (string) Str::uuid(), + 'language' => 'English', + 'code' => 'en', + 'description' => 'English Language', + ]), + Language::create([ + 'id' => (string) Str::uuid(), + 'language' => 'Spanish', + 'code' => 'es', + 'description' => 'Spanish Language', + ]), + Language::create([ + 'id' => (string) Str::uuid(), + 'language' => 'French', + 'code' => 'fr', + 'description' => 'French Language', + ]), + ]; + + $response = $this->getJson('/api/v1/languages'); + + $response->assertStatus(200) + ->assertJson([ + 'status_code' => 200, + 'message' => 'Languages fetched successfully', + 'data' => array_map(function ($language) { + return [ + 'id' => $language->id, + 'language' => $language->language, + 'code' => $language->code, + ]; + }, $languages), + ]); +} + + + /** + * Test unauthorized access to create language. + * + * @return void + */ + public function testCreateLanguageUnauthorized() + { + $response = $this->postJson('/api/v1/languages', [ + 'language' => 'Italian', + 'code' => 'it', + 'description' => 'Italian Language', + ]); + + $response->assertStatus(401) + ->assertJson([ + 'status_code' => 401, + 'message' => 'Unauthorized', + ]); + } + + /** + * Test unauthorized access to update language. + * + * @return void + */ + public function testUpdateLanguageUnauthorized() + { + $language = Language::create([ + 'language' => 'French', + 'code' => 'fr', + 'description' => 'French Language', + ]); + + $response = $this->putJson("/api/v1/languages/{$language->id}", [ + 'language' => 'French(Updated)', + 'code' => 'fr-updated', + 'description' => 'Updated French Language', + ]); + + $response->assertStatus(401) + ->assertJson([ + 'status_code' => 401, + 'message' => 'Unauthorized', + ]); + } + + /** + * Test unauthorized access to fetch languages. + * + * @return void + */ + public function testFetchLanguagesUnauthorized() + { + $response = $this->getJson('/api/v1/languages'); + + $response->assertStatus(401) + ->assertJson([ + 'status_code' => 401, + 'message' => 'Unauthorized', + ]); + } +} \ No newline at end of file