*/ public static function resourceProvider(): array { return ['services' => ['services'], 'skills' => ['skills'], 'testimonials' => ['testimonials']]; } #[DataProvider('resourceProvider')] public function test_admins_can_read_the_new_resources(string $resource): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $this->getJson("/api/admin/resources/{$resource}")->assertOk()->assertJsonStructure(['data']); } #[DataProvider('resourceProvider')] public function test_anonymous_requests_cannot_read_the_new_resources(string $resource): void { $this->getJson("/api/admin/resources/{$resource}")->assertUnauthorized(); } public function test_the_admin_read_endpoint_returns_nested_locale_objects(): void { $testimonial = Testimonial::create(['image' => '/a.png', 'client_link' => 'https://x.test']); $testimonial->translations()->create([ 'locale' => 'fa', 'client_name' => 'علی', 'client_position' => 'مدیر فنی', 'feedback' => 'نمونه‌کار بسیار خوبی بود.', ]); $testimonial->translations()->create([ 'locale' => 'en', 'client_name' => 'Ali', 'client_position' => 'CTO', 'feedback' => 'Great work overall.', ]); Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $response = $this->getJson('/api/admin/resources/testimonials')->assertOk(); $row = $response->json('data.0'); $this->assertSame( [ 'fa' => 'علی', 'en' => 'Ali', ], $row['translations']['client_name'], 'translations must be nested by key then locale, not a flat row list', ); $this->assertSame('CTO', $row['translations']['client_position']['en']); $this->assertSame('Great work overall.', $row['translations']['feedback']['en']); } public function test_a_missing_locale_is_null_rather_than_absent(): void { $testimonial = Testimonial::create(['image' => '/a.png', 'client_link' => '']); $testimonial->translations()->create([ 'locale' => 'fa', 'client_name' => 'مریم', 'client_position' => 'طراح', 'feedback' => 'دقیق و سریع.', ]); Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $translations = $this->getJson('/api/admin/resources/testimonials')->json('data.0.translations'); // The form binds to translations[key][locale]; a missing key would make // an untouched locale look like a bug in the panel rather than empty data. $this->assertArrayHasKey('en', $translations['client_name']); $this->assertNull($translations['client_name']['en']); } public function test_the_create_response_matches_the_read_shape(): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $response = $this->postJson('/api/website/testimonials', [ 'image' => '/a.png', 'client_link' => 'https://client.test', 'translations' => [ 'client_name' => ['fa' => 'سارا'], 'client_position' => ['fa' => 'مدیر محصول'], 'feedback' => ['fa' => 'همکاری خوبی بود.'], ], ])->assertCreated(); $this->assertSame('سارا', $response->json('translations.client_name.fa')); $this->assertNull($response->json('translations.client_name.en')); } public function test_the_model_accessor_does_not_recurse_into_itself(): void { $testimonial = Testimonial::create(['image' => '/a.png', 'client_link' => '']); $testimonial->translations()->create([ 'locale' => 'fa', 'client_name' => 'رضا', 'client_position' => 'برنامه‌نویس', 'feedback' => 'دقیق.', ]); // Guards the specific regression: reading the relation from inside an // accessor of the same name re-enters the accessor and yields nulls. $this->assertSame('رضا', $testimonial->translations['client_name']['fa']); $this->assertSame(['fa' => 'رضا', 'en' => null], $testimonial->toArray()['translations']['client_name']); } public function test_skill_and_about_me_relations_are_eager_loaded(): void { $aboutMe = AboutMe::create([ 'image' => '/me.png', 'translations' => ['title' => ['fa' => 'من', 'en' => 'Me']], ]); $skill = Skill::create([ 'image' => '/s.png', 'link' => 'https://x.test', 'percentage' => 80, 'about_me_id' => $aboutMe->id, 'translations' => ['title' => ['fa' => 'لاراول', 'en' => 'Laravel']], ]); Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $row = $this->getJson('/api/admin/resources/skills')->assertOk()->json('data.0'); $this->assertSame('Laravel', $row['translations']['title']['en']); $this->assertArrayHasKey('id', $row, 'the skill row itself must serialise'); $this->assertSame(80, (int) $skill->percentage); } /** * about_me_id is a non-nullable foreign key, so a payload without one must * be rejected by validation rather than blow up on the database constraint. */ public function test_a_skill_without_an_owner_is_a_validation_error(): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $this->postJson('/api/website/skills', [ 'image' => '/s.png', 'percentage' => 50, 'translations' => ['title' => ['fa' => 'عنوان']], 'description' => ['fa' => 'توضیح'], ])->assertStatus(422)->assertJsonValidationErrors('about_me_id'); } public function test_a_percentage_out_of_range_is_rejected(): void { $aboutMe = AboutMe::create([ 'image' => '/me.png', 'translations' => ['title' => ['fa' => 'من']], ]); Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $this->postJson('/api/website/skills', [ 'image' => '/s.png', 'percentage' => 140, 'about_me_id' => $aboutMe->id, 'translations' => ['title' => ['fa' => 'عنوان'], 'description' => ['fa' => 'توضیح']], ])->assertStatus(422)->assertJsonValidationErrors('percentage'); } public function test_a_testimonial_without_a_client_link_is_rejected(): void { Sanctum::actingAs(User::factory()->create(['is_admin' => true])); $this->postJson('/api/website/testimonials', [ 'image' => '/a.png', 'translations' => [ 'client_name' => ['fa' => 'سارا'], 'client_position' => ['fa' => 'مدیر'], 'feedback' => ['fa' => 'خوب بود.'], ], ])->assertStatus(422)->assertJsonValidationErrors('client_link'); } }