|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature; |
| 4 | + |
| 5 | +use App\Livewire\Customer\Plugins\Create; |
| 6 | +use App\Models\DeveloperAccount; |
| 7 | +use App\Models\Plugin; |
| 8 | +use App\Models\User; |
| 9 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 10 | +use Illuminate\Support\Facades\Http; |
| 11 | +use Illuminate\Support\Facades\Notification; |
| 12 | +use Livewire\Livewire; |
| 13 | +use Tests\TestCase; |
| 14 | + |
| 15 | +class PluginSubmissionNotesTest extends TestCase |
| 16 | +{ |
| 17 | + use RefreshDatabase; |
| 18 | + |
| 19 | + /** |
| 20 | + * @param array<string, mixed> $extraComposerData |
| 21 | + */ |
| 22 | + private function fakeGitHubForPlugin(string $repoSlug, array $extraComposerData = []): void |
| 23 | + { |
| 24 | + $base = "https://api.github.com/repos/{$repoSlug}"; |
| 25 | + $composerJson = json_encode(array_merge([ |
| 26 | + 'name' => $repoSlug, |
| 27 | + 'description' => "A test plugin: {$repoSlug}", |
| 28 | + 'require' => [ |
| 29 | + 'php' => '^8.1', |
| 30 | + 'nativephp/mobile' => '^3.0.0', |
| 31 | + ], |
| 32 | + ], $extraComposerData)); |
| 33 | + |
| 34 | + Http::fake([ |
| 35 | + "{$base}/contents/README.md" => Http::response([ |
| 36 | + 'content' => base64_encode("# {$repoSlug}"), |
| 37 | + 'encoding' => 'base64', |
| 38 | + ]), |
| 39 | + "{$base}/contents/composer.json" => Http::response([ |
| 40 | + 'content' => base64_encode($composerJson), |
| 41 | + 'encoding' => 'base64', |
| 42 | + ]), |
| 43 | + "{$base}/contents/nativephp.json" => Http::response([], 404), |
| 44 | + "{$base}/contents/LICENSE*" => Http::response([], 404), |
| 45 | + "{$base}/releases/latest" => Http::response([], 404), |
| 46 | + "{$base}/tags*" => Http::response([]), |
| 47 | + "https://raw.githubusercontent.com/{$repoSlug}/*" => Http::response('', 404), |
| 48 | + $base => Http::response(['default_branch' => 'main']), |
| 49 | + "{$base}/git/trees/main*" => Http::response([ |
| 50 | + 'tree' => [ |
| 51 | + ['path' => 'src/ServiceProvider.php', 'type' => 'blob'], |
| 52 | + ], |
| 53 | + ]), |
| 54 | + "{$base}/readme" => Http::response([ |
| 55 | + 'content' => base64_encode("# {$repoSlug}"), |
| 56 | + 'encoding' => 'base64', |
| 57 | + ]), |
| 58 | + ]); |
| 59 | + } |
| 60 | + |
| 61 | + private function createUserWithGitHub(): User |
| 62 | + { |
| 63 | + $user = User::factory()->create([ |
| 64 | + 'github_id' => '12345', |
| 65 | + ]); |
| 66 | + DeveloperAccount::factory()->withAcceptedTerms()->create([ |
| 67 | + 'user_id' => $user->id, |
| 68 | + ]); |
| 69 | + |
| 70 | + return $user; |
| 71 | + } |
| 72 | + |
| 73 | + /** @test */ |
| 74 | + public function submitting_a_plugin_saves_notes(): void |
| 75 | + { |
| 76 | + Notification::fake(); |
| 77 | + $user = $this->createUserWithGitHub(); |
| 78 | + $repoSlug = 'acme/notes-plugin'; |
| 79 | + $this->fakeGitHubForPlugin($repoSlug); |
| 80 | + |
| 81 | + Livewire::actingAs($user) |
| 82 | + ->test(Create::class) |
| 83 | + ->set('repository', $repoSlug) |
| 84 | + ->set('pluginType', 'free') |
| 85 | + ->set('notes', 'Please review this quickly, we have a launch deadline.') |
| 86 | + ->call('submitPlugin') |
| 87 | + ->assertRedirect(); |
| 88 | + |
| 89 | + $plugin = $user->plugins()->where('repository_url', "https://github.com/{$repoSlug}")->first(); |
| 90 | + |
| 91 | + $this->assertNotNull($plugin); |
| 92 | + $this->assertEquals('Please review this quickly, we have a launch deadline.', $plugin->notes); |
| 93 | + } |
| 94 | + |
| 95 | + /** @test */ |
| 96 | + public function submitting_a_plugin_without_notes_stores_null(): void |
| 97 | + { |
| 98 | + Notification::fake(); |
| 99 | + $user = $this->createUserWithGitHub(); |
| 100 | + $repoSlug = 'acme/no-notes-plugin'; |
| 101 | + $this->fakeGitHubForPlugin($repoSlug); |
| 102 | + |
| 103 | + Livewire::actingAs($user) |
| 104 | + ->test(Create::class) |
| 105 | + ->set('repository', $repoSlug) |
| 106 | + ->set('pluginType', 'free') |
| 107 | + ->call('submitPlugin') |
| 108 | + ->assertRedirect(); |
| 109 | + |
| 110 | + $plugin = $user->plugins()->where('repository_url', "https://github.com/{$repoSlug}")->first(); |
| 111 | + |
| 112 | + $this->assertNotNull($plugin); |
| 113 | + $this->assertNull($plugin->notes); |
| 114 | + } |
| 115 | + |
| 116 | + /** @test */ |
| 117 | + public function submitting_a_plugin_saves_support_channel_email(): void |
| 118 | + { |
| 119 | + Notification::fake(); |
| 120 | + $user = $this->createUserWithGitHub(); |
| 121 | + $repoSlug = 'acme/support-email-plugin'; |
| 122 | + $this->fakeGitHubForPlugin($repoSlug); |
| 123 | + |
| 124 | + Livewire::actingAs($user) |
| 125 | + ->test(Create::class) |
| 126 | + ->set('repository', $repoSlug) |
| 127 | + ->set('pluginType', 'free') |
| 128 | + ->set('supportChannel', 'help@example.com') |
| 129 | + ->call('submitPlugin') |
| 130 | + ->assertRedirect(); |
| 131 | + |
| 132 | + $plugin = $user->plugins()->where('repository_url', "https://github.com/{$repoSlug}")->first(); |
| 133 | + |
| 134 | + $this->assertNotNull($plugin); |
| 135 | + $this->assertEquals('help@example.com', $plugin->support_channel); |
| 136 | + } |
| 137 | + |
| 138 | + /** @test */ |
| 139 | + public function submitting_a_plugin_saves_support_channel_url(): void |
| 140 | + { |
| 141 | + Notification::fake(); |
| 142 | + $user = $this->createUserWithGitHub(); |
| 143 | + $repoSlug = 'acme/support-url-plugin'; |
| 144 | + $this->fakeGitHubForPlugin($repoSlug); |
| 145 | + |
| 146 | + Livewire::actingAs($user) |
| 147 | + ->test(Create::class) |
| 148 | + ->set('repository', $repoSlug) |
| 149 | + ->set('pluginType', 'free') |
| 150 | + ->set('supportChannel', 'https://example.com/support') |
| 151 | + ->call('submitPlugin') |
| 152 | + ->assertRedirect(); |
| 153 | + |
| 154 | + $plugin = $user->plugins()->where('repository_url', "https://github.com/{$repoSlug}")->first(); |
| 155 | + |
| 156 | + $this->assertNotNull($plugin); |
| 157 | + $this->assertEquals('https://example.com/support', $plugin->support_channel); |
| 158 | + } |
| 159 | + |
| 160 | + /** @test */ |
| 161 | + public function submitting_a_plugin_without_support_channel_stores_null(): void |
| 162 | + { |
| 163 | + Notification::fake(); |
| 164 | + $user = $this->createUserWithGitHub(); |
| 165 | + $repoSlug = 'acme/no-support-plugin'; |
| 166 | + $this->fakeGitHubForPlugin($repoSlug); |
| 167 | + |
| 168 | + Livewire::actingAs($user) |
| 169 | + ->test(Create::class) |
| 170 | + ->set('repository', $repoSlug) |
| 171 | + ->set('pluginType', 'free') |
| 172 | + ->call('submitPlugin') |
| 173 | + ->assertRedirect(); |
| 174 | + |
| 175 | + $plugin = $user->plugins()->where('repository_url', "https://github.com/{$repoSlug}")->first(); |
| 176 | + |
| 177 | + $this->assertNotNull($plugin); |
| 178 | + $this->assertNull($plugin->support_channel); |
| 179 | + } |
| 180 | + |
| 181 | + /** @test */ |
| 182 | + public function notes_and_support_channel_fields_are_hidden_until_repository_selected(): void |
| 183 | + { |
| 184 | + $user = User::factory()->create([ |
| 185 | + 'github_id' => '12345', |
| 186 | + ]); |
| 187 | + |
| 188 | + Livewire::actingAs($user) |
| 189 | + ->test(Create::class) |
| 190 | + ->assertDontSee('Support Channel') |
| 191 | + ->assertDontSee('Any notes for the review team') |
| 192 | + ->set('repository', 'acme/my-plugin') |
| 193 | + ->assertSee('Support Channel') |
| 194 | + ->assertSee('Notes'); |
| 195 | + } |
| 196 | + |
| 197 | + /** @test */ |
| 198 | + public function plugin_factory_with_notes_state_works(): void |
| 199 | + { |
| 200 | + $plugin = Plugin::factory()->withNotes('Custom note')->create(); |
| 201 | + |
| 202 | + $this->assertEquals('Custom note', $plugin->notes); |
| 203 | + } |
| 204 | + |
| 205 | + /** @test */ |
| 206 | + public function plugin_factory_with_support_channel_state_works(): void |
| 207 | + { |
| 208 | + $plugin = Plugin::factory()->withSupportChannel('support@myplugin.dev')->create(); |
| 209 | + |
| 210 | + $this->assertEquals('support@myplugin.dev', $plugin->support_channel); |
| 211 | + } |
| 212 | +} |
0 commit comments