|
| 1 | +<?php |
| 2 | + |
| 3 | +use Filament\Facades\Filament; |
| 4 | +use Tapp\FilamentForum\Models\Forum; |
| 5 | +use Tapp\FilamentForum\Models\ForumPost; |
| 6 | +use Tapp\FilamentForum\Models\ForumPostReaction; |
| 7 | + |
| 8 | +beforeEach(function () { |
| 9 | + $this->userModel = config('filament-forum.user.model'); |
| 10 | + $this->tenantModel = config('filament-forum.tenancy.model'); |
| 11 | +}); |
| 12 | + |
| 13 | +it('can toggle reaction on forum post', function () { |
| 14 | + $tenantModel = $this->tenantModel; |
| 15 | + $userModel = $this->userModel; |
| 16 | + |
| 17 | + $team = $tenantModel::factory()->create(); |
| 18 | + $user = $userModel::factory()->create(); |
| 19 | + |
| 20 | + actingAs($user); |
| 21 | + Filament::setTenant($team); |
| 22 | + |
| 23 | + $tenantColumn = Forum::getTenantColumnName(); |
| 24 | + $forum = Forum::factory()->create([$tenantColumn => $team->id]); |
| 25 | + $post = ForumPost::factory()->create([ |
| 26 | + 'forum_id' => $forum->id, |
| 27 | + 'user_id' => $user->id, |
| 28 | + $tenantColumn => $team->id, |
| 29 | + ]); |
| 30 | + |
| 31 | + $post->toggleReaction('👍', $user); |
| 32 | + |
| 33 | + expect($post->getReactionCounts())->toBe(['👍' => 1]); |
| 34 | + expect($post->getUserReactions($user)->toArray())->toBe(['👍']); |
| 35 | + expect(ForumPostReaction::where('forum_post_id', $post->id)->count())->toBe(1); |
| 36 | + |
| 37 | + $post->toggleReaction('👍', $user); |
| 38 | + |
| 39 | + expect($post->getReactionCounts())->toBe([]); |
| 40 | + expect($post->getUserReactions($user)->toArray())->toBe([]); |
| 41 | + expect(ForumPostReaction::where('forum_post_id', $post->id)->count())->toBe(0); |
| 42 | +}); |
| 43 | + |
| 44 | +it('rejects invalid reaction types', function () { |
| 45 | + $tenantModel = $this->tenantModel; |
| 46 | + $userModel = $this->userModel; |
| 47 | + |
| 48 | + $team = $tenantModel::factory()->create(); |
| 49 | + $user = $userModel::factory()->create(); |
| 50 | + |
| 51 | + actingAs($user); |
| 52 | + Filament::setTenant($team); |
| 53 | + |
| 54 | + $tenantColumn = Forum::getTenantColumnName(); |
| 55 | + $forum = Forum::factory()->create([$tenantColumn => $team->id]); |
| 56 | + $post = ForumPost::factory()->create([ |
| 57 | + 'forum_id' => $forum->id, |
| 58 | + 'user_id' => $user->id, |
| 59 | + $tenantColumn => $team->id, |
| 60 | + ]); |
| 61 | + |
| 62 | + $post->toggleReaction('invalid-reaction-type', $user); |
| 63 | + |
| 64 | + expect(ForumPostReaction::where('forum_post_id', $post->id)->count())->toBe(0); |
| 65 | +}); |
| 66 | + |
| 67 | +it('scopes reactions to current tenant', function () { |
| 68 | + $tenantModel = $this->tenantModel; |
| 69 | + $userModel = $this->userModel; |
| 70 | + |
| 71 | + $team1 = $tenantModel::factory()->create(); |
| 72 | + $team2 = $tenantModel::factory()->create(); |
| 73 | + $user = $userModel::factory()->create(); |
| 74 | + |
| 75 | + actingAs($user); |
| 76 | + |
| 77 | + $tenantColumn = Forum::getTenantColumnName(); |
| 78 | + |
| 79 | + $forum1 = Forum::factory()->create([$tenantColumn => $team1->id]); |
| 80 | + $forum2 = Forum::factory()->create([$tenantColumn => $team2->id]); |
| 81 | + |
| 82 | + $post1 = ForumPost::factory()->create([ |
| 83 | + 'forum_id' => $forum1->id, |
| 84 | + 'user_id' => $user->id, |
| 85 | + $tenantColumn => $team1->id, |
| 86 | + ]); |
| 87 | + |
| 88 | + $post2 = ForumPost::factory()->create([ |
| 89 | + 'forum_id' => $forum2->id, |
| 90 | + 'user_id' => $user->id, |
| 91 | + $tenantColumn => $team2->id, |
| 92 | + ]); |
| 93 | + |
| 94 | + Filament::setTenant($team1); |
| 95 | + $post1->toggleReaction('❤️', $user); |
| 96 | + |
| 97 | + Filament::setTenant($team2); |
| 98 | + $post2->toggleReaction('😂', $user); |
| 99 | + |
| 100 | + expect(ForumPostReaction::count())->toBe(2); |
| 101 | + |
| 102 | + $reaction1 = ForumPostReaction::where('forum_post_id', $post1->id)->first(); |
| 103 | + $reaction2 = ForumPostReaction::where('forum_post_id', $post2->id)->first(); |
| 104 | + |
| 105 | + expect($reaction1->{$tenantColumn})->toBe($team1->id); |
| 106 | + expect($reaction2->{$tenantColumn})->toBe($team2->id); |
| 107 | +}); |
0 commit comments