Add the admin content API the CMS needs
Two gaps blocked building the admin panel:
About-me writes could only set the image. Title and description live in the
JSON translations column and were unreachable from the API, so the homepage
bio could never be edited through the app. Accept title_fa/title_en/
description_fa/description_en on both About Me form requests and write them
per locale through setTranslation().
Editing also needs raw rows. The public project and about-me endpoints only
return display-oriented payloads - one translated title, reshaped image
arrays, no second locale - which is not enough to populate a form. Add
GET /api/admin/resources/{resource} behind the same auth:sanctum + admin
middleware as the write routes, returning stored rows including every locale,
plus a public GET /api/website/project-categories so project forms have a
category picker.
Covered by AdminResourceReadTest: anonymous and non-admin callers are
rejected for every resource, unknown resource names 404, and project rows
expose both locales.
This commit is contained in:
1 parent
8b834189a4
commit
3f386c7be8
7 files changed
+221
-4
No files matched your search
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Website\Project;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* The admin panel needs raw rows (both locales included) to edit records,
|
||||
* because the public API only exposes display-oriented payloads.
|
||||
*/
|
||||
class AdminResourceReadTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @return array<string, array{0: string}>
|
||||
*/
|
||||
public static function resourceProvider(): array
|
||||
{
|
||||
return [
|
||||
'posts' => ['posts'],
|
||||
'categories' => ['categories'],
|
||||
'tags' => ['tags'],
|
||||
'projects' => ['projects'],
|
||||
'project categories' => ['project-categories'],
|
||||
'about me' => ['about-me'],
|
||||
];
|
||||
}
|
||||
|
||||
#[DataProvider('resourceProvider')]
|
||||
public function test_anonymous_requests_cannot_read_admin_resources(string $resource): void
|
||||
{
|
||||
$this->getJson("/api/admin/resources/{$resource}")->assertUnauthorized();
|
||||
}
|
||||
|
||||
#[DataProvider('resourceProvider')]
|
||||
public function test_non_admins_cannot_read_admin_resources(string $resource): void
|
||||
{
|
||||
Sanctum::actingAs(User::factory()->create());
|
||||
|
||||
$this->getJson("/api/admin/resources/{$resource}")->assertForbidden();
|
||||
}
|
||||
|
||||
#[DataProvider('resourceProvider')]
|
||||
public function test_admins_can_read_admin_resources(string $resource): void
|
||||
{
|
||||
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
||||
|
||||
$this->getJson("/api/admin/resources/{$resource}")
|
||||
->assertOk()
|
||||
->assertJsonStructure(['data']);
|
||||
}
|
||||
|
||||
public function test_unknown_resources_are_rejected(): void
|
||||
{
|
||||
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
||||
|
||||
$this->getJson('/api/admin/resources/users')->assertNotFound();
|
||||
$this->getJson('/api/admin/resources/personal_access_tokens')->assertNotFound();
|
||||
}
|
||||
|
||||
public function test_project_rows_expose_every_locale(): void
|
||||
{
|
||||
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
|
||||
|
||||
$project = Project::factory()->create([
|
||||
'image1' => 'a.png',
|
||||
'image2' => 'b.png',
|
||||
'translations' => [
|
||||
'title' => ['fa' => 'عنوان', 'en' => 'Title'],
|
||||
'description' => ['fa' => 'توضیح', 'en' => 'Description'],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->getJson('/api/admin/resources/projects')
|
||||
->assertOk()
|
||||
->assertJsonPath("data.0.id", $project->id)
|
||||
->assertJsonPath('data.0.image1', 'a.png')
|
||||
->assertJsonPath('data.0.translations.title.fa', 'عنوان')
|
||||
->assertJsonPath('data.0.translations.title.en', 'Title');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user