Files
parsaaghayi-backend/tests/Feature/TestimonialTranslationShapeTest.php
T
Parsa Aghayi 99f786265e Let the admin panel manage services, skills and testimonials
These three had read endpoints or nothing at all, so their content could only
be changed by hand in the database. All writes are admin-only and rate
limited like the existing admin routes.

Testimonials are the awkward one: the migration that collapsed the legacy
per-locale tables into JSON columns deliberately skipped them, so they still
live in a child table. Testimonial therefore exposes a translations accessor
to present the same translations[key][locale] shape as Service and Skill,
which is what the generic resource form binds to. Reading the child relation
has to go through getRelation(), since a plain $this->translations inside an
accessor of that name re-enters the accessor and silently yields nulls.

The store requests now mark the NOT NULL columns as required. Leaving them
nullable let a bad payload reach the database and come back as a 500 instead
of a 422, and about_me_id is additionally a cascading foreign key.

Also registers the three resources with the admin read endpoint so the panel
can list them, eager loading the relations their forms need.
2026-10-01 18:19:32 -04:00

205 lines
7.8 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\User;
use App\Models\Website\AboutMe;
use App\Models\Website\Skill;
use App\Models\Website\Testimonial;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase;
/**
* Testimonials persist their translations in a child table, while Service and
* Skill use a JSON column. The admin panel has to see all three in the same
* `translations[key][locale]` shape, otherwise the generic resource form
* renders testimonial fields blank with no error to explain why.
*/
class TestimonialTranslationShapeTest extends TestCase
{
use RefreshDatabase;
/**
* @return array<string, array{0: string}>
*/
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');
}
}