diff --git a/composer.json b/composer.json index 249dffd..9fd176e 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "license": "MIT", "version": "v3.5.1", "require": { - "saucebase/core": "^3.0" + "saucebase/core": "^3.1" }, "authors": [ { @@ -27,7 +27,7 @@ ] }, "branch-alias": { - "dev-dev": "3.6.x-dev" + "dev-dev": "3.x-dev" } }, "autoload": { diff --git a/database/seeders/ShowcaseThemesDatabaseSeeder.php b/database/seeders/ShowcaseThemesDatabaseSeeder.php index e81f8ae..96f790a 100644 --- a/database/seeders/ShowcaseThemesDatabaseSeeder.php +++ b/database/seeders/ShowcaseThemesDatabaseSeeder.php @@ -4,8 +4,10 @@ use App\Models\User; use Illuminate\Database\Seeder; +use Saucebase\Core\Attributes\DemoAccount; use Spatie\Permission\Models\Role; +#[DemoAccount('themes@saucebase.dev', 'staff: the themes admin')] class ShowcaseThemesDatabaseSeeder extends Seeder { public function run(): void diff --git a/resources/boost/skills/saucebase-themes-development/SKILL.md b/resources/boost/skills/saucebase-themes-development/SKILL.md index 71f0cfb..88472dd 100644 --- a/resources/boost/skills/saucebase-themes-development/SKILL.md +++ b/resources/boost/skills/saucebase-themes-development/SKILL.md @@ -273,4 +273,6 @@ npx playwright test --project="@themes*" # E2E - `tests/Feature/AdminTheme*Test.php`: Filament admin theme - `resources/js/lib/*.test.ts`: colour maths, fields, theme vars, panel payload, Tailwind palette +Feature tests use `Modules\Themes\Tests\Support\IsolatesUserThemes`, which points storage at a temporary directory per test: an app's own `storage/app/themes/*.json` (a template ships a `default.json`) must neither change what a test sees nor be overwritten by it. A new feature test that reads or writes user themes uses it too. + Tests that post to the write routes disable `PreventRequestForgery` (`allowWrites()` in `ThemesApplyTest`), so the CSRF check stays out of what they test. diff --git a/src/Services/ThemeService.php b/src/Services/ThemeService.php index 9592a48..56a6a0d 100644 --- a/src/Services/ThemeService.php +++ b/src/Services/ThemeService.php @@ -148,17 +148,17 @@ public static function discoverThemes(): array */ public static function getTheme(string $theme): string|false { - $bundlePath = static::getBundleThemePath($theme); - if (file_exists($bundlePath)) { - return file_get_contents($bundlePath); - } + // The user's file first, as discoverThemes() does, and only one it accepts: what the + // picker shows is what gets applied. + $user = static::getUserThemePath($theme); - $userPath = static::getUserThemePath($theme); - if (file_exists($userPath)) { - return file_get_contents($userPath); + if (static::parseThemeFile($user) !== null) { + return file_get_contents($user); } - return false; + $bundle = static::getBundleThemePath($theme); + + return file_exists($bundle) ? file_get_contents($bundle) : false; } /** diff --git a/tests/Feature/AdminThemeSettingsPageTest.php b/tests/Feature/AdminThemeSettingsPageTest.php index 9a54e99..72ac01c 100644 --- a/tests/Feature/AdminThemeSettingsPageTest.php +++ b/tests/Feature/AdminThemeSettingsPageTest.php @@ -8,10 +8,12 @@ use Modules\Themes\Admin\AdminTheme; use Modules\Themes\Filament\Pages\AdminThemeSettings as AdminThemeSettingsPage; use Modules\Themes\Settings\AdminThemeSettings; +use Modules\Themes\Tests\Support\IsolatesUserThemes; use Tests\TestCase; class AdminThemeSettingsPageTest extends TestCase { + use IsolatesUserThemes; use RefreshDatabase; private function actingAsAdmin(): void diff --git a/tests/Feature/AdminThemeTest.php b/tests/Feature/AdminThemeTest.php index e15a972..f97ed6b 100644 --- a/tests/Feature/AdminThemeTest.php +++ b/tests/Feature/AdminThemeTest.php @@ -11,6 +11,7 @@ use Illuminate\Testing\TestResponse; use Modules\Themes\Admin\AdminTheme; use Modules\Themes\Settings\AdminThemeSettings; +use Modules\Themes\Tests\Support\IsolatesUserThemes; use Tests\TestCase; /** @@ -19,6 +20,7 @@ */ class AdminThemeTest extends TestCase { + use IsolatesUserThemes; use RefreshDatabase; private function saveSettings(array $values): void diff --git a/tests/Feature/ApplyThemeCommandTest.php b/tests/Feature/ApplyThemeCommandTest.php index 3935e17..4e2d56f 100644 --- a/tests/Feature/ApplyThemeCommandTest.php +++ b/tests/Feature/ApplyThemeCommandTest.php @@ -3,10 +3,13 @@ namespace Modules\Themes\Tests\Feature; use Modules\Themes\Services\ThemeService; +use Modules\Themes\Tests\Support\IsolatesUserThemes; use Tests\TestCase; class ApplyThemeCommandTest extends TestCase { + use IsolatesUserThemes; + private string $themeCssPath; private string $originalCss; @@ -27,6 +30,22 @@ protected function tearDown(): void parent::tearDown(); } + /** A saved preset named like a shipped theme is the one the picker shows, so it is the one applied. */ + public function test_a_saved_preset_wins_over_the_shipped_theme_of_the_same_name(): void + { + ThemeService::saveUserTheme('beetroot', ['name' => 'Beetroot', 'cssVars' => ['theme' => [], 'light' => ['primary' => 'mine'], 'dark' => []]]); + + $this->assertStringContainsString('"mine"', (string) ThemeService::getTheme('beetroot')); + } + + /** A saved file the picker skips (no name) is not what gets applied: the shipped theme is. */ + public function test_a_saved_preset_the_picker_skips_leaves_the_shipped_theme(): void + { + ThemeService::saveUserTheme('beetroot', ['cssVars' => ['theme' => [], 'light' => ['primary' => 'mine'], 'dark' => []]]); + + $this->assertStringNotContainsString('"mine"', (string) ThemeService::getTheme('beetroot')); + } + // ── New shadcn format ───────────────────────────────────────────────────── public function test_new_format_patches_light_vars_into_root_block(): void diff --git a/tests/Feature/ShowcaseStaffTest.php b/tests/Feature/ShowcaseStaffTest.php index 7caf2d6..2662bf1 100644 --- a/tests/Feature/ShowcaseStaffTest.php +++ b/tests/Feature/ShowcaseStaffTest.php @@ -7,6 +7,8 @@ use Illuminate\Support\Facades\Hash; use Modules\Themes\Database\Seeders\DatabaseSeeder; use Modules\Themes\Database\Seeders\ShowcaseThemesDatabaseSeeder; +use Modules\Themes\Tests\Support\IsolatesUserThemes; +use Saucebase\Core\Testing\AssertsDemoAccounts; use Tests\TestCase; /** @@ -15,6 +17,8 @@ */ class ShowcaseStaffTest extends TestCase { + use AssertsDemoAccounts; + use IsolatesUserThemes; use RefreshDatabase; public function test_the_demo_adds_a_staff_account_for_this_module_only(): void @@ -35,4 +39,10 @@ public function test_demo_accounts_use_the_configured_demo_password(): void $this->assertTrue(Hash::check('not-the-old-default', User::where('email', 'themes@saucebase.dev')->value('password'))); } + + /** The accounts the seeder declares (#[DemoAccount]) are the ones it creates. */ + public function test_every_declared_demo_account_is_created(): void + { + $this->assertDemoAccountsSeeded(ShowcaseThemesDatabaseSeeder::class); + } } diff --git a/tests/Feature/ThemesAccessTest.php b/tests/Feature/ThemesAccessTest.php index 6ceadf0..f57639d 100644 --- a/tests/Feature/ThemesAccessTest.php +++ b/tests/Feature/ThemesAccessTest.php @@ -3,11 +3,13 @@ namespace Modules\Themes\Tests\Feature; use Illuminate\Foundation\Testing\RefreshDatabase; +use Modules\Themes\Tests\Support\IsolatesUserThemes; use Saucebase\Core\ProjectConfig; use Tests\TestCase; class ThemesAccessTest extends TestCase { + use IsolatesUserThemes; use RefreshDatabase; private string $storageDir; diff --git a/tests/Feature/ThemesApplyTest.php b/tests/Feature/ThemesApplyTest.php index cada67e..510d359 100644 --- a/tests/Feature/ThemesApplyTest.php +++ b/tests/Feature/ThemesApplyTest.php @@ -5,11 +5,13 @@ use Illuminate\Foundation\Http\Middleware\PreventRequestForgery; use Illuminate\Foundation\Testing\RefreshDatabase; use Modules\Themes\Services\ThemeService; +use Modules\Themes\Tests\Support\IsolatesUserThemes; use Saucebase\Core\ProjectConfig; use Tests\TestCase; class ThemesApplyTest extends TestCase { + use IsolatesUserThemes; use RefreshDatabase; private string $themeCssPath; diff --git a/tests/Feature/ThemesConfigTest.php b/tests/Feature/ThemesConfigTest.php index 1755a4a..c98ff94 100644 --- a/tests/Feature/ThemesConfigTest.php +++ b/tests/Feature/ThemesConfigTest.php @@ -3,11 +3,13 @@ namespace Modules\Themes\Tests\Feature; use Illuminate\Foundation\Testing\RefreshDatabase; +use Modules\Themes\Tests\Support\IsolatesUserThemes; use Saucebase\Core\ProjectConfig; use Tests\TestCase; class ThemesConfigTest extends TestCase { + use IsolatesUserThemes; use RefreshDatabase; private string $storageDir; diff --git a/tests/Feature/ThemesControllerTest.php b/tests/Feature/ThemesControllerTest.php index e015b70..13d105a 100644 --- a/tests/Feature/ThemesControllerTest.php +++ b/tests/Feature/ThemesControllerTest.php @@ -3,10 +3,12 @@ namespace Modules\Themes\Tests\Feature; use Illuminate\Foundation\Testing\RefreshDatabase; +use Modules\Themes\Tests\Support\IsolatesUserThemes; use Tests\TestCase; class ThemesControllerTest extends TestCase { + use IsolatesUserThemes; use RefreshDatabase; private string $storageDir; diff --git a/tests/Feature/ThemesPermissionTest.php b/tests/Feature/ThemesPermissionTest.php index ee3df9a..ef89c07 100644 --- a/tests/Feature/ThemesPermissionTest.php +++ b/tests/Feature/ThemesPermissionTest.php @@ -6,6 +6,7 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use Modules\Themes\Database\Seeders\DatabaseSeeder; use Modules\Themes\Filament\Pages\AdminThemeSettings; +use Modules\Themes\Tests\Support\IsolatesUserThemes; use Spatie\Permission\Models\Permission; use Tests\TestCase; @@ -15,6 +16,7 @@ */ class ThemesPermissionTest extends TestCase { + use IsolatesUserThemes; use RefreshDatabase; protected function setUp(): void diff --git a/tests/Support/IsolatesUserThemes.php b/tests/Support/IsolatesUserThemes.php new file mode 100644 index 0000000..a13d604 --- /dev/null +++ b/tests/Support/IsolatesUserThemes.php @@ -0,0 +1,26 @@ +isolatedStorage = sys_get_temp_dir().'/themes-test-'.bin2hex(random_bytes(6)); + $this->app->useStoragePath($this->isolatedStorage); + } + + protected function tearDownIsolatesUserThemes(): void + { + File::deleteDirectory($this->isolatedStorage); + } +}