From 48c0c7d4c7e6682bc6cd3c28a9a030619e996a16 Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Mon, 12 May 2025 20:10:29 +0200 Subject: [PATCH] feat: Add `Cms\License::delete()` & `::root()` --- src/Cms/License.php | 38 +++++-- tests/Cms/System/LicenseTest.php | 174 +++++++++---------------------- 2 files changed, 82 insertions(+), 130 deletions(-) diff --git a/src/Cms/License.php b/src/Cms/License.php index 78895232ea..6ce8712cfa 100644 --- a/src/Cms/License.php +++ b/src/Cms/License.php @@ -31,6 +31,8 @@ class License protected const SALT = 'kwAHMLyLPBnHEskzH9pPbJsBxQhKXZnX'; + protected App $kirby; + // cache protected LicenseStatus $status; protected LicenseType $type; @@ -51,6 +53,8 @@ public function __construct( if ($email !== null) { $this->email = $this->normalizeEmail($email); } + + $this->kirby = App::instance(); } /** @@ -101,6 +105,15 @@ public function date( return $this->date !== null ? Str::date(strtotime($this->date), $format, $handler) : null; } + /** + * Deletes the license file if it exists + * @since 5.0.0 + */ + public function delete(): bool + { + return F::remove($this->root()); + } + /** * Returns the activation domain if available */ @@ -180,7 +193,7 @@ public function isLegacy(): bool } // get release date of current major version - $major = Str::before(App::instance()->version(), '.'); + $major = Str::before($this->kirby->version(), '.'); $release = strtotime(static::HISTORY[$major] ?? ''); // if there's no matching version in the history @@ -220,7 +233,7 @@ public function isOnCorrectDomain(): bool } // compare domains - if ($this->normalizeDomain(App::instance()->system()->indexUrl()) !== $this->normalizeDomain($this->domain)) { + if ($this->normalizeDomain($this->kirby->system()->indexUrl()) !== $this->normalizeDomain($this->domain)) { return false; } @@ -237,7 +250,7 @@ public function isSigned(): bool } // get the public key - $pubKey = F::read(App::instance()->root('kirby') . '/kirby.pub'); + $pubKey = F::read($this->kirby->root('kirby') . '/kirby.pub'); // verify the license signature $data = json_encode($this->signatureData()); @@ -329,7 +342,7 @@ public static function polyfill(array $license): array public static function read(): static { try { - $license = Json::read(App::instance()->root('license')); + $license = Json::read(static::root()); } catch (Throwable) { return new static(); } @@ -410,6 +423,15 @@ public function request(string $path, array $data): array // @codeCoverageIgnoreEnd } + /** + * Returns the root path to the license file + * @since 5.0.0 + */ + public static function root(): string + { + return App::instance()->root('license'); + } + /** * Saves the license in the config folder */ @@ -421,11 +443,11 @@ public function save(): bool ); } - // where to store the license file - $file = App::instance()->root('license'); - // save the license information - return Json::write($file, $this->content()); + return Json::write( + file: $this->root(), + data: $this->content() + ); } /** diff --git a/tests/Cms/System/LicenseTest.php b/tests/Cms/System/LicenseTest.php index c83fd3019e..5a2a6af1e1 100644 --- a/tests/Cms/System/LicenseTest.php +++ b/tests/Cms/System/LicenseTest.php @@ -3,12 +3,13 @@ namespace Kirby\Cms; use Kirby\Exception\InvalidArgumentException; +use Kirby\Filesystem\F; use Kirby\Toolkit\Str; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use ReflectionClass; -/** - * @coversDefaultClass \Kirby\Cms\License - */ +#[CoversClass(License::class)] class LicenseTest extends TestCase { public const FIXTURES = __DIR__ . '/fixtures/LicenseTest'; @@ -36,10 +37,7 @@ public static function providerForLicenseUrls(): array ]; } - /** - * @covers ::activation - */ - public function testActivation() + public function testActivation(): void { $license = new License( activation: $date = '2023-12-01' @@ -50,10 +48,7 @@ public function testActivation() $this->assertSame('1/12/2023 00:00', $license->activation('d/M/yyyy HH:mm', 'intl')); } - /** - * @covers ::code - */ - public function testCode() + public function testCode(): void { $license = new License( code: $code = $this->code(LicenseType::Enterprise) @@ -63,11 +58,7 @@ public function testCode() $this->assertSame('K-ENT-1234XXXXXXXXXXXXXXXXXXXXXX', $license->code(true)); } - /** - * @covers ::__construct - * @covers ::content - */ - public function testContent() + public function testContent(): void { $license = new License( code: $code = $this->code(LicenseType::Enterprise) @@ -84,10 +75,7 @@ public function testContent() ], $license->content()); } - /** - * @covers ::date - */ - public function testDate() + public function testDate(): void { $license = new License( date: $date = '2023-12-01' @@ -98,10 +86,17 @@ public function testDate() $this->assertSame('1/12/2023 00:00', $license->date('d/M/yyyy HH:mm', 'intl')); } - /** - * @covers ::domain - */ - public function testDomain() + public function testDelete(): void + { + $license = new License(); + F::write($license->root(), 'test'); + + $this->assertFileExists($license->root()); + $this->assertTrue($license->delete()); + $this->assertFileDoesNotExist($license->root()); + } + + public function testDomain(): void { $license = new License( domain: $domain = 'getkirby.com' @@ -110,10 +105,7 @@ public function testDomain() $this->assertSame($domain, $license->domain()); } - /** - * @covers ::email - */ - public function testEmail() + public function testEmail(): void { $license = new License( email: $email = 'mail@getkirby.com' @@ -122,10 +114,7 @@ public function testEmail() $this->assertSame($email, $license->email()); } - /** - * @covers ::hasValidEmailAddress - */ - public function testHasValidEmailAddress() + public function testHasValidEmailAddress(): void { $license = new License( email: 'mail@getkirby.com' @@ -140,18 +129,12 @@ public function testHasValidEmailAddress() $this->assertFalse($license->hasValidEmailAddress()); } - /** - * @covers ::hub - */ - public function testHub() + public function testHub(): void { $this->assertSame('https://hub.getkirby.com', License::hub()); } - /** - * @covers ::isComplete - */ - public function testIsComplete() + public function testIsComplete(): void { // incomplete $license = new License(); @@ -170,10 +153,7 @@ public function testIsComplete() $this->assertTrue($license->isComplete()); } - /** - * @covers ::isInactive - */ - public function testIsInactive() + public function testIsInactive(): void { MockTime::$time = strtotime('now'); @@ -194,10 +174,7 @@ public function testIsInactive() MockTime::reset(); } - /** - * @covers ::isLegacy - */ - public function testIsLegacy() + public function testIsLegacy(): void { // legacy license type $license = new License( @@ -230,10 +207,7 @@ public function testIsLegacy() $this->assertFalse($license->isLegacy()); } - /** - * @covers ::isOnCorrectDomain - */ - public function testIsOnCorrectDomain() + public function testIsOnCorrectDomain(): void { $this->app = new App([ 'roots' => [ @@ -262,20 +236,13 @@ public function testIsOnCorrectDomain() $this->assertTrue($license->isOnCorrectDomain()); } - /** - * @covers ::label - */ - public function testLabel() + public function testLabel(): void { $license = new License(); $this->assertSame('Unregistered', $license->label()); } - /** - * @covers ::__construct - * @covers ::normalizeEmail - */ - public function testNormalize() + public function testNormalize(): void { $code = $this->code(LicenseType::Enterprise); @@ -288,11 +255,8 @@ public function testNormalize() $this->assertSame('mail@getkirby.com', $license->email()); } - /** - * @covers ::normalizeDomain - * @dataProvider providerForLicenseUrls - */ - public function testNormalizeDomain(string $domain, string $expected) + #[DataProvider('providerForLicenseUrls')] + public function testNormalizeDomain(string $domain, string $expected): void { $reflector = new ReflectionClass(License::class); $normalize = $reflector->getMethod('normalizeDomain'); @@ -303,10 +267,7 @@ public function testNormalizeDomain(string $domain, string $expected) $this->assertSame($expected, $normalize->invoke($license, $domain)); } - /** - * @covers ::order - */ - public function testOrder() + public function testOrder(): void { $license = new License( order: $order = '123456' @@ -315,10 +276,7 @@ public function testOrder() $this->assertSame($order, $license->order()); } - /** - * @covers ::polyfill - */ - public function testPolyfill() + public function testPolyfill(): void { $this->assertSame([ 'activation' => null, @@ -334,10 +292,7 @@ public function testPolyfill() ])); } - /** - * @covers ::read - */ - public function testRead() + public function testRead(): void { // existing license $this->app = new App([ @@ -361,10 +316,7 @@ public function testRead() $this->assertNull($license->code()); } - /** - * @covers ::register - */ - public function testRegisterWithInvalidDomain() + public function testRegisterWithInvalidDomain(): void { $license = new License( code: $this->code(), @@ -377,10 +329,7 @@ public function testRegisterWithInvalidDomain() $license->register(); } - /** - * @covers ::register - */ - public function testRegisterWithInvalidEmail() + public function testRegisterWithInvalidEmail(): void { $license = new License( code: $this->code(), @@ -393,10 +342,7 @@ public function testRegisterWithInvalidEmail() $license->register(); } - /** - * @covers ::register - */ - public function testRegisterWithInvalidLicenseKey() + public function testRegisterWithInvalidLicenseKey(): void { $license = new License(); @@ -406,10 +352,7 @@ public function testRegisterWithInvalidLicenseKey() $license->register(); } - /** - * @covers ::renewal - */ - public function testRenewal() + public function testRenewal(): void { // activated $license = new License( @@ -425,10 +368,15 @@ public function testRenewal() $this->assertNull($license->renewal('Y-m-d')); } - /** - * @covers ::save - */ - public function testSaveWhenNotActivatable() + public function testRoot(): void + { + $this->assertSame( + App::instance()->root('license'), + License::root() + ); + } + + public function testSaveWhenNotActivatable(): void { $license = new License(); @@ -438,10 +386,7 @@ public function testSaveWhenNotActivatable() $license->save(); } - /** - * @covers ::signature - */ - public function testSignature() + public function testSignature(): void { $license = new License( signature: 'secret' @@ -450,19 +395,13 @@ public function testSignature() $this->assertSame('secret', $license->signature()); } - /** - * @covers ::status - */ - public function testStatus() + public function testStatus(): void { $license = new License(); $this->assertSame(LicenseStatus::Missing, $license->status()); } - /** - * @covers ::type - */ - public function testTypeKirby3() + public function testTypeKirby3(): void { $license = new License( code: $this->code(LicenseType::Legacy) @@ -471,10 +410,7 @@ public function testTypeKirby3() $this->assertSame(LicenseType::Legacy, $license->type()); } - /** - * @covers ::type - */ - public function testTypeKirbyBasic() + public function testTypeKirbyBasic(): void { $license = new License( code: $this->code() @@ -483,10 +419,7 @@ public function testTypeKirbyBasic() $this->assertSame(LicenseType::Basic, $license->type()); } - /** - * @covers ::type - */ - public function testTypeKirbyEnterprise() + public function testTypeKirbyEnterprise(): void { $license = new License( code: $this->code(LicenseType::Enterprise) @@ -495,10 +428,7 @@ public function testTypeKirbyEnterprise() $this->assertSame(LicenseType::Enterprise, $license->type()); } - /** - * @covers ::type - */ - public function testTypeUnregistered() + public function testTypeUnregistered(): void { $license = new License();