|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature; |
| 4 | + |
| 5 | +use App\Features\ShowPlugins; |
| 6 | +use App\Models\Plugin; |
| 7 | +use App\Models\PluginPrice; |
| 8 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 9 | +use Laravel\Pennant\Feature; |
| 10 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 11 | +use Tests\TestCase; |
| 12 | + |
| 13 | +class PluginReadmeLicenseLinkTest extends TestCase |
| 14 | +{ |
| 15 | + use RefreshDatabase; |
| 16 | + |
| 17 | + protected function setUp(): void |
| 18 | + { |
| 19 | + parent::setUp(); |
| 20 | + |
| 21 | + Feature::define(ShowPlugins::class, true); |
| 22 | + } |
| 23 | + |
| 24 | + private function createPaidPlugin(string $readmeHtml): Plugin |
| 25 | + { |
| 26 | + $plugin = Plugin::factory()->approved()->paid()->create([ |
| 27 | + 'name' => 'acme/paid-plugin', |
| 28 | + 'repository_url' => 'https://github.com/acme/paid-plugin', |
| 29 | + 'readme_html' => $readmeHtml, |
| 30 | + 'license_html' => '<p>License agreement content</p>', |
| 31 | + ]); |
| 32 | + |
| 33 | + PluginPrice::factory()->regular()->create([ |
| 34 | + 'plugin_id' => $plugin->id, |
| 35 | + 'amount' => 2999, |
| 36 | + ]); |
| 37 | + |
| 38 | + return $plugin; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @return array<string, array{0: string}> |
| 43 | + */ |
| 44 | + public static function licenseFileProvider(): array |
| 45 | + { |
| 46 | + return [ |
| 47 | + 'bare name' => ['LICENSE'], |
| 48 | + 'markdown' => ['LICENSE.md'], |
| 49 | + 'text' => ['LICENSE.txt'], |
| 50 | + 'lowercase' => ['license.md'], |
| 51 | + 'british spelling' => ['LICENCE.md'], |
| 52 | + 'unlicense' => ['UNLICENSE'], |
| 53 | + 'copying' => ['COPYING'], |
| 54 | + 'suffixed' => ['LICENSE-MIT.md'], |
| 55 | + 'dot slash prefixed' => ['./LICENSE.md'], |
| 56 | + 'root prefixed' => ['/LICENSE.md'], |
| 57 | + 'github blob url' => ['https://github.com/acme/paid-plugin/blob/main/LICENSE.md'], |
| 58 | + 'github raw url' => ['https://raw.githubusercontent.com/acme/paid-plugin/main/LICENSE'], |
| 59 | + ]; |
| 60 | + } |
| 61 | + |
| 62 | + #[DataProvider('licenseFileProvider')] |
| 63 | + public function test_license_links_are_rerouted_to_the_license_page(string $href): void |
| 64 | + { |
| 65 | + $plugin = $this->createPaidPlugin('<p>See the <a href="'.$href.'">license</a>.</p>'); |
| 66 | + |
| 67 | + $this->assertStringContainsString( |
| 68 | + '<a href="'.route('plugins.license', $plugin->routeParams()).'">license</a>', |
| 69 | + $plugin->rendered_readme_html |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + public function test_license_links_are_rerouted_when_the_readme_is_rendered(): void |
| 74 | + { |
| 75 | + $plugin = $this->createPaidPlugin('<p>See the <a href="LICENSE.md">license</a>.</p>'); |
| 76 | + |
| 77 | + $this->get(route('plugins.show', $plugin->routeParams())) |
| 78 | + ->assertStatus(200) |
| 79 | + ->assertSee('<a href="'.route('plugins.license', $plugin->routeParams()).'">license</a>', false) |
| 80 | + ->assertDontSee('<a href="LICENSE.md">', false); |
| 81 | + } |
| 82 | + |
| 83 | + public function test_multiple_license_links_are_all_rerouted(): void |
| 84 | + { |
| 85 | + $plugin = $this->createPaidPlugin( |
| 86 | + '<p><a href="LICENSE">MIT</a> and <a class="x" href=\'./LICENCE.txt\'>terms</a></p>' |
| 87 | + ); |
| 88 | + |
| 89 | + $licenseUrl = route('plugins.license', $plugin->routeParams()); |
| 90 | + |
| 91 | + $this->assertSame( |
| 92 | + '<p><a href="'.$licenseUrl.'">MIT</a> and <a class="x" href=\''.$licenseUrl.'\'>terms</a></p>', |
| 93 | + $plugin->rendered_readme_html |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @return array<string, array{0: string}> |
| 99 | + */ |
| 100 | + public static function nonLicenseLinkProvider(): array |
| 101 | + { |
| 102 | + return [ |
| 103 | + 'other document' => ['CONTRIBUTING.md'], |
| 104 | + 'nested file' => ['docs/LICENSE.md'], |
| 105 | + 'anchor' => ['#license'], |
| 106 | + 'unrelated script' => ['https://example.com/license-checker.js'], |
| 107 | + 'another repository' => ['https://github.com/other/repo/blob/main/LICENSE.md'], |
| 108 | + 'repository subdirectory' => ['https://github.com/acme/paid-plugin/blob/main/docs/LICENSE.md'], |
| 109 | + 'repository tree' => ['https://github.com/acme/paid-plugin/tree/main/LICENSE.md'], |
| 110 | + 'mail link' => ['mailto:license@example.com'], |
| 111 | + ]; |
| 112 | + } |
| 113 | + |
| 114 | + #[DataProvider('nonLicenseLinkProvider')] |
| 115 | + public function test_other_links_are_left_alone(string $href): void |
| 116 | + { |
| 117 | + $plugin = $this->createPaidPlugin('<p><a href="'.$href.'">link</a></p>'); |
| 118 | + |
| 119 | + $this->assertSame( |
| 120 | + '<p><a href="'.$href.'">link</a></p>', |
| 121 | + $plugin->rendered_readme_html |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + public function test_license_links_point_at_the_repository_when_there_is_no_license_page(): void |
| 126 | + { |
| 127 | + $plugin = Plugin::factory()->approved()->free()->create([ |
| 128 | + 'name' => 'acme/free-plugin', |
| 129 | + 'repository_url' => 'https://github.com/acme/free-plugin', |
| 130 | + 'readme_html' => '<p><a href="LICENSE.md">license</a></p>', |
| 131 | + ]); |
| 132 | + |
| 133 | + $this->assertSame( |
| 134 | + '<p><a href="https://github.com/acme/free-plugin/blob/main/LICENSE.md">license</a></p>', |
| 135 | + $plugin->rendered_readme_html |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + public function test_license_links_are_untouched_without_a_repository_or_license_page(): void |
| 140 | + { |
| 141 | + $plugin = Plugin::factory()->approved()->free()->create([ |
| 142 | + 'repository_url' => null, |
| 143 | + 'readme_html' => '<p><a href="LICENSE.md">license</a></p>', |
| 144 | + ]); |
| 145 | + |
| 146 | + $this->assertSame('<p><a href="LICENSE.md">license</a></p>', $plugin->rendered_readme_html); |
| 147 | + } |
| 148 | + |
| 149 | + public function test_readme_without_content_is_left_as_is(): void |
| 150 | + { |
| 151 | + $plugin = Plugin::factory()->approved()->free()->create(['readme_html' => null]); |
| 152 | + |
| 153 | + $this->assertNull($plugin->rendered_readme_html); |
| 154 | + } |
| 155 | +} |
0 commit comments