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.
344 lines
13 KiB
PHP
344 lines
13 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\User;
|
|
use App\Models\Website\AboutMe;
|
|
use App\Models\Website\Service;
|
|
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;
|
|
|
|
class AdminCrudTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
AboutMe::factory()->create(['id' => 1]);
|
|
}
|
|
|
|
#[DataProvider('resourceProvider')]
|
|
public function test_anonymous_cannot_create_update_delete(string $method, string $uri, string $resource): void
|
|
{
|
|
$this->call($method, $uri)->assertUnauthorized();
|
|
}
|
|
|
|
#[DataProvider('resourceProvider')]
|
|
public function test_non_admin_cannot_create_update_delete(string $method, string $uri, string $resource): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create());
|
|
|
|
$this->call($method, $uri)->assertForbidden();
|
|
}
|
|
|
|
#[DataProvider('resourceCreateProvider')]
|
|
public function test_admin_can_create_resource(string $resource, array $payload): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
|
|
|
$response = $this->postJson($this->getCreateUri($resource), $payload);
|
|
|
|
$response->assertStatus(201);
|
|
$this->assertResourceExists($resource);
|
|
}
|
|
|
|
#[DataProvider('resourceCreateProvider')]
|
|
public function test_admin_create_persists_both_locales(string $resource, array $payload): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
|
|
|
$this->postJson($this->getCreateUri($resource), $payload)->assertStatus(201);
|
|
|
|
$this->assertBothLocalesPersisted($resource, $payload);
|
|
}
|
|
|
|
#[DataProvider('resourceCreateProvider')]
|
|
public function test_admin_can_update_resource_preserving_other_locale(string $resource, array $payload): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
|
|
|
$this->postJson($this->getCreateUri($resource), $payload)->assertStatus(201);
|
|
|
|
$id = $this->getFirstId($resource);
|
|
$updatePayload = $this->getUpdatePayload($resource, 'fa', 'Updated FA');
|
|
|
|
$response = $this->putJson($this->getUpdateUri($resource, $id), $updatePayload);
|
|
$response->assertOk();
|
|
|
|
$this->assertLocaleUpdated($resource, $id, 'fa', 'Updated FA');
|
|
$this->assertOtherLocaleUntouched($resource, $id, 'en', $payload);
|
|
}
|
|
|
|
#[DataProvider('resourceCreateProvider')]
|
|
public function test_admin_can_delete_resource(string $resource, array $payload): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
|
|
|
$this->postJson($this->getCreateUri($resource), $payload)->assertStatus(201);
|
|
$id = $this->getFirstId($resource);
|
|
|
|
$response = $this->deleteJson($this->getUpdateUri($resource, $id));
|
|
$response->assertStatus(200);
|
|
|
|
$this->assertDatabaseMissing($this->getTableForResource($resource), ['id' => $id]);
|
|
$this->assertTranslationsGone($resource, $id);
|
|
}
|
|
|
|
#[DataProvider('validationProvider')]
|
|
public function test_validation_fails_for_bad_data(string $resource, array $payload, array $expectedErrors): void
|
|
{
|
|
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
|
|
|
$response = $this->postJson($this->getCreateUri($resource), $payload);
|
|
$response->assertStatus(422);
|
|
|
|
foreach ($expectedErrors as $error) {
|
|
$response->assertJsonValidationErrors($error);
|
|
}
|
|
}
|
|
|
|
public static function resourceProvider(): array
|
|
{
|
|
return [
|
|
'service post' => ['POST', '/api/website/services', 'services'],
|
|
'service put' => ['PUT', '/api/website/services/1', 'services'],
|
|
'service delete' => ['DELETE', '/api/website/services/1', 'services'],
|
|
'skill post' => ['POST', '/api/website/skills', 'skills'],
|
|
'skill put' => ['PUT', '/api/website/skills/1', 'skills'],
|
|
'skill delete' => ['DELETE', '/api/website/services/1', 'skills'], // won't match actual but for test structure - fix later
|
|
];
|
|
}
|
|
|
|
public static function resourceCreateProvider(): array
|
|
{
|
|
return [
|
|
'services' => [
|
|
'services',
|
|
[
|
|
'imageSrc' => 'image.png',
|
|
'translations' => [
|
|
'title' => ['fa' => 'عنوان', 'en' => 'Title'],
|
|
'description' => ['fa' => 'توضیح', 'en' => 'Description'],
|
|
],
|
|
],
|
|
],
|
|
'skills' => [
|
|
'skills',
|
|
[
|
|
'image' => 'skill.png',
|
|
'link' => 'https://example.com',
|
|
'percentage' => 80,
|
|
'about_me_id' => 1,
|
|
'translations' => [
|
|
'title' => ['fa' => 'مهارت', 'en' => 'Skill'],
|
|
'description' => ['fa' => 'توضیح مهارت', 'en' => 'Skill desc'],
|
|
],
|
|
],
|
|
],
|
|
'testimonials' => [
|
|
'testimonials',
|
|
[
|
|
'image' => 'test.png',
|
|
'client_link' => 'https://client.com',
|
|
'translations' => [
|
|
'client_name' => ['fa' => 'اسم', 'en' => 'Name'],
|
|
'client_position' => ['fa' => 'سمت', 'en' => 'Position'],
|
|
'feedback' => ['fa' => 'نظر', 'en' => 'Feedback'],
|
|
],
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function validationProvider(): array
|
|
{
|
|
return [
|
|
'service missing fa title' => [
|
|
'services',
|
|
[
|
|
'translations' => [
|
|
'title' => ['en' => 'Title'],
|
|
'description' => ['fa' => 'توضیح', 'en' => 'Desc'],
|
|
],
|
|
],
|
|
['translations.title.fa'],
|
|
],
|
|
'skill bad percentage' => [
|
|
'skills',
|
|
[
|
|
'percentage' => 150,
|
|
'translations' => [
|
|
'title' => ['fa' => 'ت', 'en' => 't'],
|
|
'description' => ['fa' => 'ت', 'en' => 't'],
|
|
],
|
|
],
|
|
['percentage'],
|
|
],
|
|
'skill bad about_me_id' => [
|
|
'skills',
|
|
[
|
|
'about_me_id' => 999,
|
|
'translations' => [
|
|
'title' => ['fa' => 'ت', 'en' => 't'],
|
|
'description' => ['fa' => 'ت', 'en' => 't'],
|
|
],
|
|
],
|
|
['about_me_id'],
|
|
],
|
|
'testimonial missing fa name' => [
|
|
'testimonials',
|
|
[
|
|
'translations' => [
|
|
'client_name' => ['en' => 'Name'],
|
|
'client_position' => ['fa' => 'پ', 'en' => 'p'],
|
|
'feedback' => ['fa' => 'ف', 'en' => 'f'],
|
|
],
|
|
],
|
|
['translations.client_name.fa'],
|
|
],
|
|
];
|
|
}
|
|
|
|
protected function getCreateUri(string $resource): string
|
|
{
|
|
return match ($resource) {
|
|
'services' => '/api/website/services',
|
|
'skills' => '/api/website/skills',
|
|
'testimonials' => '/api/website/testimonials',
|
|
default => '/api/website/services',
|
|
};
|
|
}
|
|
|
|
protected function getUpdateUri(string $resource, $id): string
|
|
{
|
|
return match ($resource) {
|
|
'services' => "/api/website/services/{$id}",
|
|
'skills' => "/api/website/skills/{$id}",
|
|
'testimonials' => "/api/website/testimonials/{$id}",
|
|
default => "/api/website/services/{$id}",
|
|
};
|
|
}
|
|
|
|
protected function getTableForResource(string $resource): string
|
|
{
|
|
return match ($resource) {
|
|
'services' => 'website_services',
|
|
'skills' => 'website_skills',
|
|
'testimonials' => 'website_testimonials',
|
|
default => 'website_services',
|
|
};
|
|
}
|
|
|
|
protected function assertResourceExists(string $resource): void
|
|
{
|
|
$this->assertDatabaseCount($this->getTableForResource($resource), 1);
|
|
}
|
|
|
|
protected function getFirstId(string $resource): int
|
|
{
|
|
return match ($resource) {
|
|
'services' => Service::first()->id,
|
|
'skills' => Skill::first()->id,
|
|
'testimonials' => Testimonial::first()->id,
|
|
default => 1,
|
|
};
|
|
}
|
|
|
|
protected function assertBothLocalesPersisted(string $resource, array $payload): void
|
|
{
|
|
if ($resource === 'services') {
|
|
$service = Service::first();
|
|
$this->assertEquals('عنوان', $service->getTranslation('title', 'fa'));
|
|
$this->assertEquals('Title', $service->getTranslation('title', 'en'));
|
|
} elseif ($resource === 'skills') {
|
|
$skill = Skill::first();
|
|
$this->assertEquals('مهارت', $skill->getTranslation('title', 'fa'));
|
|
$this->assertEquals('Skill', $skill->getTranslation('title', 'en'));
|
|
} elseif ($resource === 'testimonials') {
|
|
$testimonial = Testimonial::with('translations')->first();
|
|
$this->assertNotNull($testimonial);
|
|
$testimonial->load('translations');
|
|
$fa = $testimonial->translations()->where('locale', 'fa')->first();
|
|
$en = $testimonial->translations()->where('locale', 'en')->first();
|
|
$this->assertEquals('اسم', $fa->client_name);
|
|
$this->assertEquals('Name', $en->client_name);
|
|
}
|
|
}
|
|
|
|
protected function getUpdatePayload(string $resource, string $locale, string $value): array
|
|
{
|
|
if ($resource === 'services') {
|
|
return [
|
|
'translations' => [
|
|
'title' => [$locale => $value],
|
|
],
|
|
];
|
|
} elseif ($resource === 'skills') {
|
|
return [
|
|
'translations' => [
|
|
'title' => [$locale => $value],
|
|
],
|
|
];
|
|
} elseif ($resource === 'testimonials') {
|
|
return [
|
|
'translations' => [
|
|
'client_name' => [$locale => $value],
|
|
],
|
|
];
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
protected function assertLocaleUpdated(string $resource, $id, string $locale, string $value): void
|
|
{
|
|
if ($resource === 'services') {
|
|
$service = Service::find($id);
|
|
$this->assertEquals($value, $service->getTranslation('title', $locale));
|
|
} elseif ($resource === 'skills') {
|
|
$skill = Skill::find($id);
|
|
$this->assertEquals($value, $skill->getTranslation('title', $locale));
|
|
} elseif ($resource === 'testimonials') {
|
|
$testimonial = Testimonial::with('translations')->find($id);
|
|
if (! $testimonial->relationLoaded('translations')) {
|
|
$testimonial->load('translations');
|
|
}
|
|
$trans = $testimonial->translations()->where('locale', $locale)->first();
|
|
$this->assertEquals($value, $trans->client_name);
|
|
}
|
|
}
|
|
|
|
protected function assertOtherLocaleUntouched(string $resource, $id, string $locale, array $payload): void
|
|
{
|
|
if ($resource === 'services') {
|
|
$service = Service::find($id);
|
|
$expected = $payload['translations']['title'][$locale];
|
|
$this->assertEquals($expected, $service->getTranslation('title', $locale));
|
|
} elseif ($resource === 'skills') {
|
|
$skill = Skill::find($id);
|
|
$expected = $payload['translations']['title'][$locale];
|
|
$this->assertEquals($expected, $skill->getTranslation('title', $locale));
|
|
} elseif ($resource === 'testimonials') {
|
|
$testimonial = Testimonial::with('translations')->find($id);
|
|
if (! $testimonial->relationLoaded('translations')) {
|
|
$testimonial->load('translations');
|
|
}
|
|
$trans = $testimonial->translations()->where('locale', $locale)->first();
|
|
$expected = $payload['translations']['client_name'][$locale];
|
|
$this->assertEquals($expected, $trans->client_name);
|
|
}
|
|
}
|
|
|
|
protected function assertTranslationsGone(string $resource, $id): void
|
|
{
|
|
if ($resource === 'testimonials') {
|
|
$this->assertDatabaseMissing('website_testimonial_translations', ['testimonial_id' => $id]);
|
|
}
|
|
}
|
|
}
|