Add extensive controller and model tests
This commit is contained in:
111
tests/Feature/ForumControllerTest.php
Normal file
111
tests/Feature/ForumControllerTest.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Forum;
|
||||
use App\Models\User;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
it('can filter forums by parent exists', function (): void {
|
||||
$category = Forum::create([
|
||||
'name' => 'Category 1',
|
||||
'description' => null,
|
||||
'type' => 'category',
|
||||
'parent_id' => null,
|
||||
'position' => 1,
|
||||
]);
|
||||
|
||||
$forum = Forum::create([
|
||||
'name' => 'Forum A',
|
||||
'description' => null,
|
||||
'type' => 'forum',
|
||||
'parent_id' => $category->id,
|
||||
'position' => 1,
|
||||
]);
|
||||
|
||||
$response = $this->getJson('/api/forums?parent[exists]=false');
|
||||
$response->assertOk();
|
||||
$response->assertJsonCount(1);
|
||||
$response->assertJsonFragment(['id' => $category->id]);
|
||||
|
||||
$response = $this->getJson('/api/forums?parent[exists]=true');
|
||||
$response->assertOk();
|
||||
$response->assertJsonCount(1);
|
||||
$response->assertJsonFragment(['id' => $forum->id]);
|
||||
});
|
||||
|
||||
it('rejects forum without category parent', function (): void {
|
||||
Sanctum::actingAs(User::factory()->create());
|
||||
|
||||
$response = $this->postJson('/api/forums', [
|
||||
'name' => 'Bad Forum',
|
||||
'type' => 'forum',
|
||||
'parent' => null,
|
||||
]);
|
||||
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonFragment(['message' => 'Forums must belong to a category.']);
|
||||
});
|
||||
|
||||
it('rejects non-category parent', function (): void {
|
||||
Sanctum::actingAs(User::factory()->create());
|
||||
|
||||
$category = Forum::create([
|
||||
'name' => 'Category',
|
||||
'description' => null,
|
||||
'type' => 'category',
|
||||
'parent_id' => null,
|
||||
'position' => 1,
|
||||
]);
|
||||
|
||||
$parent = Forum::create([
|
||||
'name' => 'Not Category',
|
||||
'description' => null,
|
||||
'type' => 'forum',
|
||||
'parent_id' => $category->id,
|
||||
'position' => 1,
|
||||
]);
|
||||
|
||||
$response = $this->postJson('/api/forums', [
|
||||
'name' => 'Child Forum',
|
||||
'type' => 'forum',
|
||||
'parent' => "/api/forums/{$parent->id}",
|
||||
]);
|
||||
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonFragment(['message' => 'Parent must be a category.']);
|
||||
});
|
||||
|
||||
it('reorders positions within parent scope', function (): void {
|
||||
Sanctum::actingAs(User::factory()->create());
|
||||
|
||||
$first = Forum::create([
|
||||
'name' => 'Cat A',
|
||||
'description' => null,
|
||||
'type' => 'category',
|
||||
'parent_id' => null,
|
||||
'position' => 1,
|
||||
]);
|
||||
|
||||
$second = Forum::create([
|
||||
'name' => 'Cat B',
|
||||
'description' => null,
|
||||
'type' => 'category',
|
||||
'parent_id' => null,
|
||||
'position' => 2,
|
||||
]);
|
||||
|
||||
$response = $this->postJson('/api/forums/reorder', [
|
||||
'parentId' => null,
|
||||
'orderedIds' => [$second->id, $first->id],
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
|
||||
$this->assertDatabaseHas('forums', [
|
||||
'id' => $second->id,
|
||||
'position' => 1,
|
||||
]);
|
||||
$this->assertDatabaseHas('forums', [
|
||||
'id' => $first->id,
|
||||
'position' => 2,
|
||||
]);
|
||||
});
|
||||
Reference in New Issue
Block a user