diff --git a/lang/en/messages.php b/lang/en/messages.php index 0508f562edc..58b709283f6 100644 --- a/lang/en/messages.php +++ b/lang/en/messages.php @@ -180,6 +180,7 @@ 'licensing_error_no_domains' => 'No domains defined', 'licensing_error_no_site_key' => 'No site license key', 'licensing_error_outside_license_range' => 'License valid for versions :start and :end', + 'licensing_error_unknown_edition' => 'The :edition edition set in config/statamic/editions.php doesn\'t exist', 'licensing_error_unknown_site' => 'Unknown site', 'licensing_error_unlicensed' => 'Unlicensed', 'licensing_incorrect_key_format_body' => 'Your site key format is incorrect. Site keys are 16-character alphanumeric strings. Get your current key from your account at statamic.com. Do not use legacy UUID license keys.', diff --git a/src/Addons/Addon.php b/src/Addons/Addon.php index 83d851c1a9f..a17f91befc4 100644 --- a/src/Addons/Addon.php +++ b/src/Addons/Addon.php @@ -488,13 +488,19 @@ public function existsOnMarketplace() public function edition() { - $configured = config('statamic.editions.addons.'.$this->package()); + $configured = $this->configuredEdition(); - if ($configured && ! $this->editions()->contains($configured)) { - throw new \Exception("Invalid edition [$configured] for addon ".$this->package()); - } + return $this->editions()->contains($configured) ? $configured : $this->editions()->first(); + } + + public function configuredEdition() + { + return config('statamic.editions.addons.'.$this->package()); + } - return $configured ?? $this->editions()->first(); + public function hasUnknownEdition() + { + return $this->configuredEdition() && ! $this->editions()->contains($this->configuredEdition()); } public function editions($editions = null) diff --git a/src/Licensing/AddonLicense.php b/src/Licensing/AddonLicense.php index f7733405abe..03d2bfa3d14 100644 --- a/src/Licensing/AddonLicense.php +++ b/src/Licensing/AddonLicense.php @@ -44,8 +44,17 @@ public function existsOnMarketplace() return $this->response['exists']; } + public function valid() + { + return ! $this->addon?->hasUnknownEdition() && parent::valid(); + } + public function invalidReason() { + if ($this->addon?->hasUnknownEdition()) { + return trans('statamic::messages.licensing_error_unknown_edition', ['edition' => $this->addon->configuredEdition()]); + } + switch (Arr::get($this->response, 'reason')) { case 'outside_license_range': [$start, $end] = $this->response['range']; diff --git a/tests/Addons/AddonTest.php b/tests/Addons/AddonTest.php index efe202d63c7..87edbd2fa91 100644 --- a/tests/Addons/AddonTest.php +++ b/tests/Addons/AddonTest.php @@ -102,13 +102,19 @@ public function it_gets_the_editions() } #[Test] - public function it_throws_exception_for_invalid_edition() + public function it_falls_back_to_the_default_edition_when_the_configured_edition_does_not_exist() { - $this->expectExceptionMessage('Invalid edition [rad] for addon foo/bar'); - config(['statamic.editions.addons.foo/bar' => 'rad']); - $this->makeFromPackage(['id' => 'foo/bar', 'editions' => []])->edition(); + $addon = $this->makeFromPackage(['id' => 'foo/bar', 'editions' => ['free', 'pro']]); + + $this->assertEquals('free', $addon->edition()); + $this->assertEquals('rad', $addon->configuredEdition()); + $this->assertTrue($addon->hasUnknownEdition()); + + config(['statamic.editions.addons.foo/bar' => 'pro']); + + $this->assertFalse($addon->hasUnknownEdition()); } #[Test] diff --git a/tests/Feature/Utilities/LicensingTest.php b/tests/Feature/Utilities/LicensingTest.php new file mode 100644 index 00000000000..d6fc762c0af --- /dev/null +++ b/tests/Feature/Utilities/LicensingTest.php @@ -0,0 +1,125 @@ +flush(); + + $this->mock(Manifest::class)->shouldReceive('addons')->andReturn(collect([ + 'acme/seo' => [ + 'id' => 'acme/seo', + 'name' => 'SEO', + 'version' => '1.0.0', + 'editions' => ['free', 'pro'], + 'marketplaceId' => 999, + 'marketplaceUrl' => 'https://statamic.com/addons/acme/seo', + 'isCommercial' => true, + ], + ])); + + Facade::clearResolvedInstance(AddonRepository::class); + + $handler = HandlerStack::create(new MockHandler([ + new Response(200, [], json_encode([ + 'public' => true, + 'site' => ['valid' => true], + 'statamic' => ['valid' => true], + 'packages' => ['acme/seo' => ['valid' => true, 'exists' => true]], + ])), + ])); + + $handler->push(Middleware::history($this->outpostRequests)); + + $this->app->instance(Outpost::class, new Outpost(new Client(['handler' => $handler]))); + } + + #[Test] + #[DataProvider('editionProvider')] + public function it_shows_the_addons_edition($configuredEdition, $expectedEdition) + { + config(['statamic.editions.addons.acme/seo' => $configuredEdition]); + + $this + ->actingAs(User::make()->makeSuper()->save()) + ->get(cp_route('utilities.licensing')) + ->assertOk() + ->assertInertia(fn (AssertableInertia $page) => $page + ->where('addons.0.edition', $expectedEdition) + ->where('addons.0.valid', true) + ->where('addons.0.invalidReason', null) + ->where('_statamic.licensing.valid', true) + ); + + $this->assertEquals(['version' => '1.0.0', 'edition' => $expectedEdition], $this->outpostPayload()['packages']['acme/seo']); + } + + public static function editionProvider() + { + return [ + 'configured edition' => ['pro', 'pro'], + 'no configured edition' => [null, 'free'], + ]; + } + + #[Test] + public function it_shows_an_addon_configured_with_an_edition_it_does_not_have_as_invalid() + { + config(['statamic.editions.addons.acme/seo' => 'premium']); + + $this + ->actingAs(User::make()->makeSuper()->save()) + ->get(cp_route('utilities.licensing')) + ->assertOk() + ->assertInertia(fn (AssertableInertia $page) => $page + ->where('addons.0.edition', 'free') + ->where('addons.0.valid', false) + ->where('addons.0.invalidReason', "The premium edition set in config/statamic/editions.php doesn't exist") + ->where('addToCartUrl', fn ($url) => str_contains($url, 'products=999%3Afree')) + ->where('_statamic.licensing.valid', false) + ); + + $this->assertEquals(['version' => '1.0.0', 'edition' => 'free'], $this->outpostPayload()['packages']['acme/seo']); + } + + #[Test] + public function other_cp_pages_still_load_when_an_addon_is_configured_with_an_edition_it_does_not_have() + { + config(['statamic.editions.addons.acme/seo' => 'premium']); + + $this + ->actingAs(User::make()->makeSuper()->save()) + ->get(cp_route('dashboard')) + ->assertOk(); + } + + private function outpostPayload() + { + return json_decode($this->outpostRequests[0]['request']->getBody(), true); + } +} diff --git a/tests/Licensing/AddonLicenseTest.php b/tests/Licensing/AddonLicenseTest.php index 687b3584267..f8de08c5114 100644 --- a/tests/Licensing/AddonLicenseTest.php +++ b/tests/Licensing/AddonLicenseTest.php @@ -108,4 +108,14 @@ public function edition() { return $this->edition; } + + public function configuredEdition() + { + return $this->edition; + } + + public function hasUnknownEdition() + { + return false; + } }