|
3 | 3 | use Filament\Facades\Filament; |
4 | 4 | use Tapp\FilamentForum\Models\Forum; |
5 | 5 | use Tapp\FilamentForum\Models\ForumPost; |
6 | | -use Workbench\App\Models\Team; |
7 | | -use Workbench\App\Models\User; |
8 | 6 |
|
9 | 7 | beforeEach(function () { |
| 8 | + // Get models from config |
| 9 | + $this->userModel = config('filament-forum.user.model', 'App\\Models\\User'); |
| 10 | + $this->tenantModel = config('filament-forum.tenancy.model', 'App\\Models\\Team'); |
| 11 | + |
10 | 12 | // Enable tenancy for tests |
11 | 13 | config(['filament-forum.tenancy.enabled' => true]); |
12 | | - config(['filament-forum.tenancy.model' => Team::class]); |
| 14 | + config(['filament-forum.tenancy.model' => $this->tenantModel]); |
13 | 15 | }); |
14 | 16 |
|
15 | 17 | it('can toggle favorite with tenancy enabled', function () { |
16 | | - $team = Team::factory()->create(); |
17 | | - $user = User::factory()->create(); |
| 18 | + $tenantModel = $this->tenantModel; |
| 19 | + $userModel = $this->userModel; |
| 20 | + |
| 21 | + $team = $tenantModel::factory()->create(); |
| 22 | + $user = $userModel::factory()->create(); |
18 | 23 |
|
19 | 24 | // Set current tenant |
20 | 25 | Filament::setTenant($team); |
21 | 26 | actingAs($user); |
22 | 27 |
|
| 28 | + // Get the tenant column name dynamically |
| 29 | + $tenantColumn = Forum::getTenantColumnName(); |
| 30 | + |
23 | 31 | $forum = Forum::factory()->create([ |
24 | | - 'team_id' => $team->id, |
| 32 | + $tenantColumn => $team->id, |
25 | 33 | ]); |
26 | 34 |
|
27 | 35 | $post = ForumPost::factory()->create([ |
28 | 36 | 'forum_id' => $forum->id, |
29 | 37 | 'user_id' => $user->id, |
30 | | - 'team_id' => $team->id, |
| 38 | + $tenantColumn => $team->id, |
31 | 39 | ]); |
32 | 40 |
|
33 | 41 | // Toggle favorite |
34 | 42 | $post->toggleFavorite(); |
35 | 43 |
|
36 | | - // Check that the favorite was created with team_id |
| 44 | + // Check that the favorite was created with tenant_id |
37 | 45 | expect($user->favoriteForumPosts()->count())->toBe(1); |
38 | 46 | expect($user->favoriteForumPosts->first()->id)->toBe($post->id); |
39 | 47 |
|
40 | | - // Verify pivot data includes team_id |
| 48 | + // Verify pivot data includes tenant_id |
41 | 49 | $pivot = $user->favoriteForumPosts()->first()->pivot; |
42 | | - expect($pivot->team_id)->toBe($team->id); |
| 50 | + expect($pivot->{$tenantColumn})->toBe($team->id); |
43 | 51 |
|
44 | 52 | // Check isFavorite returns true |
45 | 53 | expect($post->isFavorite())->toBeTrue(); |
|
51 | 59 | }); |
52 | 60 |
|
53 | 61 | it('scopes favorites to current tenant', function () { |
54 | | - $team1 = Team::factory()->create(); |
55 | | - $team2 = Team::factory()->create(); |
56 | | - $user = User::factory()->create(); |
| 62 | + $tenantModel = $this->tenantModel; |
| 63 | + $userModel = $this->userModel; |
| 64 | + |
| 65 | + $team1 = $tenantModel::factory()->create(); |
| 66 | + $team2 = $tenantModel::factory()->create(); |
| 67 | + $user = $userModel::factory()->create(); |
57 | 68 |
|
58 | 69 | actingAs($user); |
59 | 70 |
|
60 | | - $forum1 = Forum::factory()->create(['team_id' => $team1->id]); |
61 | | - $forum2 = Forum::factory()->create(['team_id' => $team2->id]); |
| 71 | + // Get the tenant column name dynamically |
| 72 | + $tenantColumn = Forum::getTenantColumnName(); |
| 73 | + |
| 74 | + $forum1 = Forum::factory()->create([$tenantColumn => $team1->id]); |
| 75 | + $forum2 = Forum::factory()->create([$tenantColumn => $team2->id]); |
62 | 76 |
|
63 | 77 | $post1 = ForumPost::factory()->create([ |
64 | 78 | 'forum_id' => $forum1->id, |
65 | 79 | 'user_id' => $user->id, |
66 | | - 'team_id' => $team1->id, |
| 80 | + $tenantColumn => $team1->id, |
67 | 81 | ]); |
68 | 82 |
|
69 | 83 | $post2 = ForumPost::factory()->create([ |
70 | 84 | 'forum_id' => $forum2->id, |
71 | 85 | 'user_id' => $user->id, |
72 | | - 'team_id' => $team2->id, |
| 86 | + $tenantColumn => $team2->id, |
73 | 87 | ]); |
74 | 88 |
|
75 | 89 | // Favorite post1 in team1 context |
|
97 | 111 | }); |
98 | 112 |
|
99 | 113 | it('works without tenancy when disabled', function () { |
| 114 | + $userModel = $this->userModel; |
| 115 | + |
100 | 116 | // Disable tenancy |
101 | 117 | config(['filament-forum.tenancy.enabled' => false]); |
102 | 118 |
|
103 | | - $user = User::factory()->create(); |
| 119 | + $user = $userModel::factory()->create(); |
104 | 120 | actingAs($user); |
105 | 121 |
|
106 | 122 | $forum = Forum::factory()->create(); |
|
0 commit comments