Skip to content

fix: unique check on pages #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions app/Http/Requests/PageRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class PageRequest extends FormRequest
{
Expand All @@ -23,11 +24,16 @@ public function rules(): array
{
return [
'title' => 'required', // Page title is required
'content' => 'json|required', // Page content is required
'content' => 'required|json|', // Page content is required
// 'event_id' => 'required|integer', // Event ID is required and must be an integer
'author_id' => 'uuid|required', // User ID is required and must be a UUID
'author_id' => 'required|uuid|exists:users,id', // User ID is required and must be a UUID
'public' => 'boolean',
'slug' => 'required|alpha_dash:ascii|unique:pages,slug,'.$this->slug.',slug', // Slug is required and must contain only ASCII characters and underscores/dashes
// Slug is required and must contain only ASCII characters and underscores/dashes
'slug' => [
'required',
'alpha_dash:ascii',
Rule::unique('pages', 'slug')->ignore($this->page),
],
];
}
}
7 changes: 6 additions & 1 deletion app/Http/Requests/UserProfileRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ public function authorize(): bool
public function rules(): array
{
return [
'nickname' => 'required|string|max:50|unique:user_profiles,nickname,'.$this->profile->id.',id',
'nickname' => [
'required',
'string',
'max:50',
Rule::unique('user_profiles', 'nickname')->ignore($this->profile),
],
'birthdate' => [
'required',
Rule::date()->format('Y-m-d'),
Expand Down
3 changes: 2 additions & 1 deletion openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,8 @@
"description": "User ID is required and must be a UUID"
},
"slug": {
"type": "string"
"type": "string",
"description": "Slug is required and must contain only ASCII characters and underscores/dashes"
}
},
"required": [
Expand Down
35 changes: 35 additions & 0 deletions tests/Feature/PageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,38 @@
'page_id' => $page->id,
]);
});

test('fail validation on create page', function () {
$user = User::factory()->create();
$page = Page::factory()->create();

// Fail with non unique slug
$this->asUser($user)->postJson('/api/page/', [
'title' => 'test title',
'content' => '{"test":"test2"}',
'author_id' => $user->id,
'slug' => $page->slug,
])->assertStatus(422)->assertInvalid(['slug']);

$this->asUser($user)->postJson('/api/page/', [
'title' => 'test title',
'content' => 'invalid json',
'author_id' => Str::uuid(), // invalid user
'slug' => 'invalid slug',
])->assertStatus(422)->assertInvalid(['content', 'author_id', 'slug']);
});

test('get page versions', function () {
$user = User::factory()->create();
$page = Page::factory()->create();

$response = $this->asUser($user)->getJson("/api/page/{$page->id}/versions");
$response->assertStatus(200)->assertJsonCount(0, 'data');

// Update page to create a new version
$this->asUser($user)->putJson('/api/page/'.$page->id, $page->only(['title', 'content', 'author_id', 'slug']));

// version_number should now be 1
$response = $this->asUser($user)->getJson("/api/page/{$page->id}/versions");
$response->assertStatus(200)->assertJsonCount(1, 'data')->assertJsonPath('data.0.version_number', 1);
});