163 lines
4.6 KiB
PHP
163 lines
4.6 KiB
PHP
<?php
|
|
|
|
use App\Models\Forum;
|
|
use App\Models\Post;
|
|
use App\Models\PostThank;
|
|
use App\Models\Thread;
|
|
use App\Models\User;
|
|
use Laravel\Sanctum\Sanctum;
|
|
|
|
function makeThanksThread(): Thread
|
|
{
|
|
$category = Forum::create([
|
|
'name' => 'Category',
|
|
'description' => null,
|
|
'type' => 'category',
|
|
'parent_id' => null,
|
|
'position' => 1,
|
|
]);
|
|
|
|
$forum = Forum::create([
|
|
'name' => 'Forum',
|
|
'description' => null,
|
|
'type' => 'forum',
|
|
'parent_id' => $category->id,
|
|
'position' => 1,
|
|
]);
|
|
|
|
return Thread::create([
|
|
'forum_id' => $forum->id,
|
|
'user_id' => null,
|
|
'title' => 'Thanks Thread',
|
|
'body' => 'Thread Body',
|
|
]);
|
|
}
|
|
|
|
it('lists thanks given by a user', function (): void {
|
|
$thread = makeThanksThread();
|
|
$author = User::factory()->create(['name' => 'Author']);
|
|
$thanker = User::factory()->create(['name' => 'ThanksGiver']);
|
|
|
|
$post = Post::create([
|
|
'thread_id' => $thread->id,
|
|
'user_id' => $author->id,
|
|
'body' => 'Helpful post',
|
|
]);
|
|
|
|
$thank = PostThank::create([
|
|
'post_id' => $post->id,
|
|
'user_id' => $thanker->id,
|
|
]);
|
|
|
|
Sanctum::actingAs($thanker);
|
|
$response = $this->getJson("/api/user/{$thanker->id}/thanks/given");
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonFragment([
|
|
'id' => $thank->id,
|
|
'post_id' => $post->id,
|
|
'thread_id' => $thread->id,
|
|
'thread_title' => 'Thanks Thread',
|
|
'post_author_name' => 'Author',
|
|
]);
|
|
});
|
|
|
|
it('lists thanks received for a user', function (): void {
|
|
$thread = makeThanksThread();
|
|
$author = User::factory()->create(['name' => 'Author']);
|
|
$thanker = User::factory()->create(['name' => 'ThanksGiver']);
|
|
|
|
$post = Post::create([
|
|
'thread_id' => $thread->id,
|
|
'user_id' => $author->id,
|
|
'body' => 'Helpful post',
|
|
]);
|
|
|
|
$thank = PostThank::create([
|
|
'post_id' => $post->id,
|
|
'user_id' => $thanker->id,
|
|
]);
|
|
|
|
Sanctum::actingAs($author);
|
|
$response = $this->getJson("/api/user/{$author->id}/thanks/received");
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonFragment([
|
|
'id' => $thank->id,
|
|
'post_id' => $post->id,
|
|
'thread_id' => $thread->id,
|
|
'thread_title' => 'Thanks Thread',
|
|
'thanker_name' => 'ThanksGiver',
|
|
]);
|
|
});
|
|
|
|
it('requires auth to thank and unthank posts', function (): void {
|
|
$thread = makeThanksThread();
|
|
$post = Post::create([
|
|
'thread_id' => $thread->id,
|
|
'user_id' => null,
|
|
'body' => 'Post',
|
|
]);
|
|
|
|
$this->app['auth']->forgetGuards();
|
|
$response = $this->postJson("/api/posts/{$post->id}/thanks");
|
|
$response->assertStatus(401);
|
|
|
|
$this->app['auth']->forgetGuards();
|
|
$response = $this->deleteJson("/api/posts/{$post->id}/thanks");
|
|
$response->assertStatus(401);
|
|
});
|
|
|
|
it('creates and deletes thanks for a post', function (): void {
|
|
$thread = makeThanksThread();
|
|
$user = User::factory()->create();
|
|
$post = Post::create([
|
|
'thread_id' => $thread->id,
|
|
'user_id' => $user->id,
|
|
'body' => 'Post',
|
|
]);
|
|
|
|
Sanctum::actingAs($user);
|
|
$response = $this->postJson("/api/posts/{$post->id}/thanks");
|
|
$response->assertStatus(201);
|
|
|
|
$response = $this->deleteJson("/api/posts/{$post->id}/thanks");
|
|
$response->assertStatus(204);
|
|
});
|
|
|
|
it('serializes group colors for thanks', function (): void {
|
|
$thread = makeThanksThread();
|
|
$authorRole = \App\Models\Role::create(['name' => 'ROLE_AUTHOR', 'color' => '#ff0000']);
|
|
$thankerRole = \App\Models\Role::create(['name' => 'ROLE_THANKER', 'color' => '#00ff00']);
|
|
|
|
$author = User::factory()->create(['name' => 'Author']);
|
|
$author->roles()->attach($authorRole);
|
|
$author->load('roles');
|
|
$thanker = User::factory()->create(['name' => 'ThanksGiver']);
|
|
$thanker->roles()->attach($thankerRole);
|
|
$thanker->load('roles');
|
|
|
|
$post = Post::create([
|
|
'thread_id' => $thread->id,
|
|
'user_id' => $author->id,
|
|
'body' => 'Helpful post',
|
|
]);
|
|
|
|
PostThank::create([
|
|
'post_id' => $post->id,
|
|
'user_id' => $thanker->id,
|
|
]);
|
|
|
|
Sanctum::actingAs($thanker);
|
|
$response = $this->getJson("/api/user/{$thanker->id}/thanks/given");
|
|
$response->assertOk();
|
|
$payload = $response->getData(true);
|
|
expect($payload[0]['post_author_group_color'])->toBe('#ff0000');
|
|
|
|
Sanctum::actingAs($author);
|
|
$response = $this->getJson("/api/user/{$author->id}/thanks/received");
|
|
$response->assertOk();
|
|
$payload = $response->getData(true);
|
|
expect($payload[0]['thanker_group_color'])->toBe('#00ff00');
|
|
});
|