baseDirectory = sys_get_temp_dir().DIRECTORY_SEPARATOR.'ptero-theme-document-'.uniqid(); config([ 'extensions.enabled' => true, 'extensions.directory' => $this->baseDirectory.'/extensions', 'extensions.assets_directory' => $this->baseDirectory.'/assets', ]); }); afterEach(function (): void { app(ExtensionRepository::class)->flushDiscovery(); File::deleteDirectory($this->baseDirectory); }); /** * @param array $theme */ function installTheme(string $id, array $theme = []): void { (function () use ($id, $theme): void { $directory = $this->baseDirectory.'/extensions/'.$id; File::ensureDirectoryExists($directory.'/dist'); File::put($directory.'/extension.json', json_encode([ 'id' => $id, 'type' => 'theme', 'name' => ucfirst($id), 'version' => '1.0.0', 'ui' => ['entry' => 'dist/client.js'], 'theme' => ['stylesheet' => 'dist/theme.css', ...$theme], ], JSON_THROW_ON_ERROR)); File::put($directory.'/dist/client.js', 'export default {};'); File::put($directory.'/dist/theme.css', ':root{}'); Extension::query()->create(['identifier' => $id, 'version' => '1.0.0', 'enabled' => true]); $this->app->make(ExtensionRepository::class)->flushDiscovery(); })->call(pterodactylTestCase()); } function aurora(): void { installTheme('aurora', [ 'schemes' => ['dark', 'light'], 'settings' => [ 'accent' => ['type' => 'color', 'label' => 'Accent', 'default' => '#8b5cf6', 'token' => '--accent'], 'radius' => ['type' => 'number', 'label' => 'Radius', 'default' => 0.75, 'unit' => 'rem', 'token' => '--radius'], 'navigation' => ['type' => 'select', 'label' => 'Navigation', 'options' => ['side' => 'Sidebar', 'top' => 'Top bar'], 'default' => 'side', 'attribute' => 'data-aurora-nav'], 'compact' => ['type' => 'toggle', 'label' => 'Compact', 'default' => false, 'attribute' => 'data-density'], 'tagline' => ['type' => 'text', 'label' => 'Tagline', 'token' => '--tagline'], ], ]); app(ThemeRepository::class)->setActive('aurora'); } /** @return array */ function themePayload(TestResponse $response): array { preg_match('/window\.SiteConfiguration = (.+?);\s*<\/script>/s', (string) $response->getContent(), $matches); return json_decode($matches[1] ?? 'null', true, flags: JSON_THROW_ON_ERROR)['theme']; } test('guests get the classic theme with the panel stylesheet and a zoomable viewport', function (): void { $response = $this->get('/auth/login')->assertOk() ->assertSee('', false) ->assertSee('', false) ->assertSee('href="/assets/stylesheet.test.css"', false) ->assertSee('', false) ->assertDontSee('user-scalable', false) ->assertDontSee('modulepreload', false); expect(themePayload($response))->toBe([ 'id' => 'classic', 'chain' => [], 'settings' => [], 'scheme' => 'dark', 'schemes' => ['dark'], 'safeMode' => false, 'preview' => false, ]); // The import map sits in , ahead of the theme entries it serves. $html = (string) $response->getContent(); expect(mb_strpos($html, ''], 'quotes and escapes' => ["\"\\'; } :root { --x: 1"], 'new lines' => ["one\ntwo\r\nthree"], ]); test('safe mode renders classic without extension bundles for the rest of the session', function (): void { aurora(); $safe = $this->get('/auth/login?safe_mode=1')->assertOk()->assertSee('data-theme="classic"', false); expect(themePayload($safe))->toMatchArray(['id' => 'classic', 'safeMode' => true, 'chain' => []]); expect(session(ThemeResolver::SAFE_MODE))->toBeTrue(); $this->get('/auth/login')->assertSee('data-theme="classic"', false)->assertSee('"extensions":[]', false); $this->get('/auth/login?safe_mode=0')->assertSee('data-theme="aurora"', false); expect(session()->has(ThemeResolver::SAFE_MODE))->toBeFalse(); }); test('a root admin previewing a theme sees it alone', function (): void { installTheme('aurora'); installTheme('nebula'); app(ThemeRepository::class)->setActive('aurora'); $admin = User::factory()->create(['root_admin' => true]); $previewing = $this->actingAs($admin)->withSession([ThemeResolver::PREVIEW => 'nebula'])->get('/')->assertOk(); expect(themePayload($previewing))->toMatchArray(['id' => 'nebula', 'preview' => true]); $this->actingAs(User::factory()->create())->withSession([ThemeResolver::PREVIEW => 'nebula'])->get('/') ->assertSee('data-theme="aurora"', false); }); test('the scheme follows the preference cookie when the theme supports it', function (?string $cookie, array $schemes, string $scheme, bool $script): void { installTheme('aurora', ['schemes' => $schemes]); app(ThemeRepository::class)->setActive('aurora'); $request = $cookie === null ? $this : $this->withUnencryptedCookie(ThemeResolver::SCHEME_COOKIE, $cookie); $response = $request->get('/auth/login')->assertOk()->assertSee("class=\"{$scheme}\"", false); expect(themePayload($response)['scheme'])->toBe($scheme); expect(str_contains((string) $response->getContent(), "matchMedia('(prefers-color-scheme: light)')"))->toBe($script); })->with([ 'no preference' => [null, ['dark', 'light'], 'dark', false], 'light' => ['light', ['dark', 'light'], 'light', false], 'system' => ['system', ['dark', 'light'], 'dark', true], 'unsupported scheme' => ['light', ['dark'], 'dark', false], 'unknown value' => ['sepia', ['light', 'dark'], 'light', false], 'system with one scheme' => ['system', ['light'], 'light', false], ]); test('an active theme that cannot be used renders as classic', function (): void { aurora(); Extension::query()->where('identifier', 'aurora')->update(['enabled' => false]); app(ExtensionRepository::class)->flushDiscovery(); $this->get('/auth/login')->assertOk()->assertSee('data-theme="classic"', false)->assertSee('stylesheet.test.css', false); }); test('the extension payload carries the themes of the active chain, whose setup runs while they are active', function (): void { installTheme('aurora'); installTheme('nebula'); app(ThemeRepository::class)->setActive('aurora'); $response = $this->actingAs(User::factory()->create())->get('/')->assertOk(); preg_match('/window\.SiteConfiguration = (.+?);\s*<\/script>/s', (string) $response->getContent(), $matches); $extensions = json_decode($matches[1] ?? 'null', true, flags: JSON_THROW_ON_ERROR)['extensions']; expect(array_column($extensions, 'theme', 'id'))->toBe(['aurora' => true]); });