|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Livewire\Customer; |
| 4 | + |
| 5 | +use App\Enums\PluginType; |
| 6 | +use App\Features\AllowPaidPlugins; |
| 7 | +use App\Features\ShowAuthButtons; |
| 8 | +use App\Features\ShowPlugins; |
| 9 | +use App\Livewire\Customer\Plugins\Create; |
| 10 | +use App\Livewire\Customer\Plugins\Show; |
| 11 | +use App\Models\DeveloperAccount; |
| 12 | +use App\Models\Plugin; |
| 13 | +use App\Models\User; |
| 14 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 15 | +use Illuminate\Support\Facades\Http; |
| 16 | +use Laravel\Pennant\Feature; |
| 17 | +use Livewire\Livewire; |
| 18 | +use Tests\TestCase; |
| 19 | + |
| 20 | +class PluginPaidOnboardingTest extends TestCase |
| 21 | +{ |
| 22 | + use RefreshDatabase; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + parent::setUp(); |
| 27 | + |
| 28 | + Feature::define(ShowAuthButtons::class, true); |
| 29 | + Feature::define(ShowPlugins::class, true); |
| 30 | + Feature::define(AllowPaidPlugins::class, true); |
| 31 | + } |
| 32 | + |
| 33 | + private function createGitHubUser(): User |
| 34 | + { |
| 35 | + return User::factory()->create([ |
| 36 | + 'github_id' => '12345', |
| 37 | + 'github_username' => 'testuser', |
| 38 | + 'github_token' => encrypt('fake-token'), |
| 39 | + ]); |
| 40 | + } |
| 41 | + |
| 42 | + private function fakeComposerJson(string $owner, string $repo, string $packageName): void |
| 43 | + { |
| 44 | + $composerJson = base64_encode(json_encode(['name' => $packageName])); |
| 45 | + |
| 46 | + Http::fake([ |
| 47 | + "api.github.com/repos/{$owner}/{$repo}/contents/composer.json*" => Http::response([ |
| 48 | + 'content' => $composerJson, |
| 49 | + ]), |
| 50 | + 'api.github.com/*' => Http::response([], 404), |
| 51 | + ]); |
| 52 | + } |
| 53 | + |
| 54 | + // ======================================== |
| 55 | + // Create: Paid option disabled without onboarding |
| 56 | + // ======================================== |
| 57 | + |
| 58 | + public function test_create_page_shows_paid_option_disabled_without_onboarding(): void |
| 59 | + { |
| 60 | + $user = $this->createGitHubUser(); |
| 61 | + |
| 62 | + Livewire::actingAs($user)->test(Create::class) |
| 63 | + ->assertSee('complete developer onboarding') |
| 64 | + ->assertSeeHtml('disabled'); |
| 65 | + } |
| 66 | + |
| 67 | + public function test_create_page_shows_paid_option_enabled_with_onboarding(): void |
| 68 | + { |
| 69 | + $user = $this->createGitHubUser(); |
| 70 | + DeveloperAccount::factory()->for($user)->create(); |
| 71 | + |
| 72 | + Livewire::actingAs($user)->test(Create::class) |
| 73 | + ->assertDontSee('complete developer onboarding'); |
| 74 | + } |
| 75 | + |
| 76 | + public function test_create_paid_plugin_blocked_without_onboarding(): void |
| 77 | + { |
| 78 | + $user = $this->createGitHubUser(); |
| 79 | + |
| 80 | + $this->fakeComposerJson('testuser', 'my-plugin', 'testuser/my-plugin'); |
| 81 | + |
| 82 | + Livewire::actingAs($user)->test(Create::class) |
| 83 | + ->set('repository', 'testuser/my-plugin') |
| 84 | + ->set('pluginType', 'paid') |
| 85 | + ->call('createPlugin') |
| 86 | + ->assertNoRedirect(); |
| 87 | + |
| 88 | + $this->assertDatabaseMissing('plugins', [ |
| 89 | + 'repository_url' => 'https://github.com/testuser/my-plugin', |
| 90 | + ]); |
| 91 | + } |
| 92 | + |
| 93 | + public function test_create_paid_plugin_allowed_with_onboarding(): void |
| 94 | + { |
| 95 | + $user = $this->createGitHubUser(); |
| 96 | + DeveloperAccount::factory()->for($user)->create(); |
| 97 | + |
| 98 | + $this->fakeComposerJson('testuser', 'paid-plugin', 'testuser/paid-plugin'); |
| 99 | + |
| 100 | + Livewire::actingAs($user)->test(Create::class) |
| 101 | + ->set('repository', 'testuser/paid-plugin') |
| 102 | + ->set('pluginType', 'paid') |
| 103 | + ->call('createPlugin'); |
| 104 | + |
| 105 | + $this->assertDatabaseHas('plugins', [ |
| 106 | + 'repository_url' => 'https://github.com/testuser/paid-plugin', |
| 107 | + 'type' => 'paid', |
| 108 | + 'status' => 'draft', |
| 109 | + ]); |
| 110 | + } |
| 111 | + |
| 112 | + public function test_create_free_plugin_allowed_without_onboarding(): void |
| 113 | + { |
| 114 | + $user = $this->createGitHubUser(); |
| 115 | + |
| 116 | + $this->fakeComposerJson('testuser', 'free-plugin', 'testuser/free-plugin'); |
| 117 | + |
| 118 | + Livewire::actingAs($user)->test(Create::class) |
| 119 | + ->set('repository', 'testuser/free-plugin') |
| 120 | + ->set('pluginType', 'free') |
| 121 | + ->call('createPlugin'); |
| 122 | + |
| 123 | + $this->assertDatabaseHas('plugins', [ |
| 124 | + 'repository_url' => 'https://github.com/testuser/free-plugin', |
| 125 | + 'type' => 'free', |
| 126 | + 'status' => 'draft', |
| 127 | + ]); |
| 128 | + } |
| 129 | + |
| 130 | + // ======================================== |
| 131 | + // Edit Draft: Paid option disabled without onboarding |
| 132 | + // ======================================== |
| 133 | + |
| 134 | + public function test_edit_draft_shows_paid_option_disabled_without_onboarding(): void |
| 135 | + { |
| 136 | + $user = $this->createGitHubUser(); |
| 137 | + $plugin = Plugin::factory()->draft()->for($user)->create([ |
| 138 | + 'name' => 'testuser/onboard-test', |
| 139 | + ]); |
| 140 | + |
| 141 | + [$vendor, $package] = explode('/', $plugin->name); |
| 142 | + |
| 143 | + Livewire::actingAs($user)->test(Show::class, [ |
| 144 | + 'vendor' => $vendor, |
| 145 | + 'package' => $package, |
| 146 | + ]) |
| 147 | + ->assertSee('complete developer onboarding') |
| 148 | + ->assertSeeHtml('disabled'); |
| 149 | + } |
| 150 | + |
| 151 | + public function test_edit_draft_shows_paid_option_enabled_with_onboarding(): void |
| 152 | + { |
| 153 | + $user = $this->createGitHubUser(); |
| 154 | + DeveloperAccount::factory()->for($user)->create(); |
| 155 | + $plugin = Plugin::factory()->draft()->for($user)->create([ |
| 156 | + 'name' => 'testuser/onboard-enabled', |
| 157 | + ]); |
| 158 | + |
| 159 | + [$vendor, $package] = explode('/', $plugin->name); |
| 160 | + |
| 161 | + Livewire::actingAs($user)->test(Show::class, [ |
| 162 | + 'vendor' => $vendor, |
| 163 | + 'package' => $package, |
| 164 | + ]) |
| 165 | + ->assertDontSee('complete developer onboarding'); |
| 166 | + } |
| 167 | + |
| 168 | + public function test_save_draft_as_paid_blocked_without_onboarding(): void |
| 169 | + { |
| 170 | + $user = $this->createGitHubUser(); |
| 171 | + $plugin = Plugin::factory()->draft()->for($user)->create([ |
| 172 | + 'name' => 'testuser/save-paid-test', |
| 173 | + 'support_channel' => 'support@test.io', |
| 174 | + ]); |
| 175 | + |
| 176 | + [$vendor, $package] = explode('/', $plugin->name); |
| 177 | + |
| 178 | + Livewire::actingAs($user)->test(Show::class, [ |
| 179 | + 'vendor' => $vendor, |
| 180 | + 'package' => $package, |
| 181 | + ]) |
| 182 | + ->set('description', 'A test plugin') |
| 183 | + ->set('supportChannel', 'support@test.io') |
| 184 | + ->set('pluginType', 'paid') |
| 185 | + ->set('tier', 'gold') |
| 186 | + ->call('save'); |
| 187 | + |
| 188 | + $plugin->refresh(); |
| 189 | + $this->assertNotEquals(PluginType::Paid, $plugin->type); |
| 190 | + } |
| 191 | + |
| 192 | + public function test_save_draft_as_paid_allowed_with_onboarding(): void |
| 193 | + { |
| 194 | + $user = $this->createGitHubUser(); |
| 195 | + DeveloperAccount::factory()->for($user)->create(); |
| 196 | + $plugin = Plugin::factory()->draft()->for($user)->create([ |
| 197 | + 'name' => 'testuser/save-paid-ok', |
| 198 | + 'support_channel' => 'support@test.io', |
| 199 | + ]); |
| 200 | + |
| 201 | + [$vendor, $package] = explode('/', $plugin->name); |
| 202 | + |
| 203 | + Livewire::actingAs($user)->test(Show::class, [ |
| 204 | + 'vendor' => $vendor, |
| 205 | + 'package' => $package, |
| 206 | + ]) |
| 207 | + ->set('description', 'A test plugin') |
| 208 | + ->set('supportChannel', 'support@test.io') |
| 209 | + ->set('pluginType', 'paid') |
| 210 | + ->set('tier', 'gold') |
| 211 | + ->call('save'); |
| 212 | + |
| 213 | + $plugin->refresh(); |
| 214 | + $this->assertEquals(PluginType::Paid, $plugin->type); |
| 215 | + } |
| 216 | + |
| 217 | + public function test_create_page_shows_onboarding_link_without_onboarding(): void |
| 218 | + { |
| 219 | + $user = $this->createGitHubUser(); |
| 220 | + |
| 221 | + Livewire::actingAs($user)->test(Create::class) |
| 222 | + ->assertSeeHtml(route('customer.developer.onboarding')); |
| 223 | + } |
| 224 | + |
| 225 | + public function test_edit_draft_shows_onboarding_link_without_onboarding(): void |
| 226 | + { |
| 227 | + $user = $this->createGitHubUser(); |
| 228 | + $plugin = Plugin::factory()->draft()->for($user)->create([ |
| 229 | + 'name' => 'testuser/link-test', |
| 230 | + ]); |
| 231 | + |
| 232 | + [$vendor, $package] = explode('/', $plugin->name); |
| 233 | + |
| 234 | + Livewire::actingAs($user)->test(Show::class, [ |
| 235 | + 'vendor' => $vendor, |
| 236 | + 'package' => $package, |
| 237 | + ]) |
| 238 | + ->assertSeeHtml(route('customer.developer.onboarding')); |
| 239 | + } |
| 240 | +} |
0 commit comments