Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://statamic.com" class="underline">statamic.com</a>. Do not use legacy UUID license keys.',
Expand Down
16 changes: 11 additions & 5 deletions src/Addons/Addon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions src/Licensing/AddonLicense.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
14 changes: 10 additions & 4 deletions tests/Addons/AddonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
125 changes: 125 additions & 0 deletions tests/Feature/Utilities/LicensingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace Tests\Feature\Utilities;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Facade;
use Inertia\Testing\AssertableInertia;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Addons\AddonRepository;
use Statamic\Addons\Manifest;
use Statamic\Facades\User;
use Statamic\Licensing\Outpost;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;

class LicensingTest extends TestCase
{
use PreventSavingStacheItemsToDisk;

private $outpostRequests = [];

public function setUp(): void
{
parent::setUp();

Cache::store('outpost')->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);
}
}
10 changes: 10 additions & 0 deletions tests/Licensing/AddonLicenseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,14 @@ public function edition()
{
return $this->edition;
}

public function configuredEdition()
{
return $this->edition;
}

public function hasUnknownEdition()
{
return false;
}
}
Loading