|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Filament; |
| 4 | + |
| 5 | +use App\Filament\Resources\PluginBundleResource\RelationManagers\LicensesRelationManager; |
| 6 | +use App\Models\Plugin; |
| 7 | +use App\Models\PluginBundle; |
| 8 | +use App\Models\PluginLicense; |
| 9 | +use App\Models\User; |
| 10 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 11 | +use Livewire\Livewire; |
| 12 | +use Tests\TestCase; |
| 13 | + |
| 14 | +class BundlePurchasesRelationManagerTest extends TestCase |
| 15 | +{ |
| 16 | + use RefreshDatabase; |
| 17 | + |
| 18 | + private User $admin; |
| 19 | + |
| 20 | + private PluginBundle $bundle; |
| 21 | + |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + parent::setUp(); |
| 25 | + |
| 26 | + $this->admin = User::factory()->create(['email' => 'admin@test.com']); |
| 27 | + config(['filament.users' => ['admin@test.com']]); |
| 28 | + |
| 29 | + $this->bundle = PluginBundle::factory()->active()->create(); |
| 30 | + } |
| 31 | + |
| 32 | + public function test_it_groups_bundle_licenses_into_a_single_sale_row(): void |
| 33 | + { |
| 34 | + $buyer = User::factory()->create(); |
| 35 | + $purchasedAt = now(); |
| 36 | + |
| 37 | + $plugins = Plugin::factory()->count(3)->approved()->create(); |
| 38 | + $this->bundle->plugins()->attach($plugins->pluck('id')); |
| 39 | + |
| 40 | + foreach ($plugins as $plugin) { |
| 41 | + PluginLicense::factory()->create([ |
| 42 | + 'user_id' => $buyer->id, |
| 43 | + 'plugin_id' => $plugin->id, |
| 44 | + 'plugin_bundle_id' => $this->bundle->id, |
| 45 | + 'price_paid' => 1000, |
| 46 | + 'purchased_at' => $purchasedAt, |
| 47 | + ]); |
| 48 | + } |
| 49 | + |
| 50 | + Livewire::actingAs($this->admin) |
| 51 | + ->test(LicensesRelationManager::class, [ |
| 52 | + 'ownerRecord' => $this->bundle, |
| 53 | + 'pageClass' => \App\Filament\Resources\PluginBundleResource\Pages\ViewPluginBundle::class, |
| 54 | + ]) |
| 55 | + ->assertCanSeeTableRecords( |
| 56 | + PluginLicense::where('plugin_bundle_id', $this->bundle->id) |
| 57 | + ->whereIn('id', function ($sub) { |
| 58 | + $sub->selectRaw('MIN(id)') |
| 59 | + ->from('plugin_licenses as pl') |
| 60 | + ->where('pl.plugin_bundle_id', $this->bundle->id) |
| 61 | + ->groupBy('pl.user_id', 'pl.purchased_at'); |
| 62 | + }) |
| 63 | + ->get() |
| 64 | + ) |
| 65 | + ->assertCountTableRecords(1); |
| 66 | + } |
| 67 | + |
| 68 | + public function test_it_shows_separate_rows_for_different_sales(): void |
| 69 | + { |
| 70 | + $buyer1 = User::factory()->create(); |
| 71 | + $buyer2 = User::factory()->create(); |
| 72 | + $purchasedAt = now(); |
| 73 | + |
| 74 | + $plugins = Plugin::factory()->count(2)->approved()->create(); |
| 75 | + $this->bundle->plugins()->attach($plugins->pluck('id')); |
| 76 | + |
| 77 | + foreach ($plugins as $plugin) { |
| 78 | + PluginLicense::factory()->create([ |
| 79 | + 'user_id' => $buyer1->id, |
| 80 | + 'plugin_id' => $plugin->id, |
| 81 | + 'plugin_bundle_id' => $this->bundle->id, |
| 82 | + 'price_paid' => 1500, |
| 83 | + 'purchased_at' => $purchasedAt, |
| 84 | + ]); |
| 85 | + } |
| 86 | + |
| 87 | + foreach ($plugins as $plugin) { |
| 88 | + PluginLicense::factory()->create([ |
| 89 | + 'user_id' => $buyer2->id, |
| 90 | + 'plugin_id' => $plugin->id, |
| 91 | + 'plugin_bundle_id' => $this->bundle->id, |
| 92 | + 'price_paid' => 1500, |
| 93 | + 'purchased_at' => $purchasedAt, |
| 94 | + ]); |
| 95 | + } |
| 96 | + |
| 97 | + Livewire::actingAs($this->admin) |
| 98 | + ->test(LicensesRelationManager::class, [ |
| 99 | + 'ownerRecord' => $this->bundle, |
| 100 | + 'pageClass' => \App\Filament\Resources\PluginBundleResource\Pages\ViewPluginBundle::class, |
| 101 | + ]) |
| 102 | + ->assertCountTableRecords(2); |
| 103 | + } |
| 104 | + |
| 105 | + public function test_it_shows_grandfathered_status(): void |
| 106 | + { |
| 107 | + $buyer = User::factory()->create(); |
| 108 | + $plugin = Plugin::factory()->approved()->create(); |
| 109 | + $this->bundle->plugins()->attach($plugin); |
| 110 | + |
| 111 | + PluginLicense::factory()->grandfathered()->create([ |
| 112 | + 'user_id' => $buyer->id, |
| 113 | + 'plugin_id' => $plugin->id, |
| 114 | + 'plugin_bundle_id' => $this->bundle->id, |
| 115 | + 'price_paid' => 0, |
| 116 | + 'purchased_at' => now(), |
| 117 | + ]); |
| 118 | + |
| 119 | + Livewire::actingAs($this->admin) |
| 120 | + ->test(LicensesRelationManager::class, [ |
| 121 | + 'ownerRecord' => $this->bundle, |
| 122 | + 'pageClass' => \App\Filament\Resources\PluginBundleResource\Pages\ViewPluginBundle::class, |
| 123 | + ]) |
| 124 | + ->assertCountTableRecords(1); |
| 125 | + } |
| 126 | +} |
0 commit comments