diff --git a/composer.json b/composer.json index 584c9c7..8b8d5d1 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "license": "MIT", "version": "v3.4.4", "require": { - "saucebase/core": "^2.8" + "saucebase/core": "^2.12.0" }, "authors": [ { diff --git a/resources/boost/skills/saucebase-announcements-development/SKILL.md b/resources/boost/skills/saucebase-announcements-development/SKILL.md index 22921e0..5f5a2fc 100644 --- a/resources/boost/skills/saucebase-announcements-development/SKILL.md +++ b/resources/boost/skills/saucebase-announcements-development/SKILL.md @@ -45,7 +45,7 @@ No frontend pages — admin-only via Filament. Only shares: `id`, `text`, `is_dismissable`, `show_on_frontend`, `show_on_dashboard`. ### Dismissal -`DismissAnnouncementController` sets a 1-year cookie (`saucebase_announcement_dismissed`) with the announcement ID. The banner component posts to `announcements.dismiss` on click; the provider checks the cookie on subsequent requests. +`DismissAnnouncementController` answers 404 for an announcement that is not `is_dismissable` (the banner shows no button for it; sc-1134), and otherwise sets a 1-year cookie (`saucebase_announcement_dismissed`) with the announcement ID. The banner component posts to `announcements.dismiss` on click; the provider checks the cookie on subsequent requests. Cookie name is configurable via `config('announcements.cookie_name')`. @@ -88,6 +88,7 @@ The schedule tests write their window around the real clock. Never use `laravel. ## Gotchas +- `text` is rich-editor HTML the banner renders with `v-html`. The model casts `text` with core's `SanitizedHtml` (`Str::sanitizeHtml()` on read, stored as written), so every read (the shared prop included) is safe markup. Keep it if the prop changes (sc-1131) - No frontend routes or pages — this module is admin-only; the banner mounts through the `app::page.before` global component slot - The banner is removed by the server, not by the click: it disappears when the response comes back without the `announcement` prop. Hiding it locally first raced the request that writes the cookie, so a reload straight after dismissing brought it back - Cookie is set for 1 year (60 × 24 × 365 minutes); users (or the app) can clear/overwrite it to re-show the same announcement, but if you want to re-show to users who dismissed it without touching cookies, publish a new announcement (new ID) diff --git a/src/Http/Controllers/DismissAnnouncementController.php b/src/Http/Controllers/DismissAnnouncementController.php index 02eb2d3..401e8a0 100644 --- a/src/Http/Controllers/DismissAnnouncementController.php +++ b/src/Http/Controllers/DismissAnnouncementController.php @@ -9,6 +9,9 @@ class DismissAnnouncementController { public function __invoke(Announcement $announcement): RedirectResponse { + // 404, as for a missing one: the banner offers no button for these. + abort_unless($announcement->is_dismissable, 404); + return back()->withCookie( cookie( config('announcements.cookie_name'), diff --git a/src/Models/Announcement.php b/src/Models/Announcement.php index 1834c46..f53a476 100644 --- a/src/Models/Announcement.php +++ b/src/Models/Announcement.php @@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Saucebase\Core\Casts\SanitizedHtml; /** * @property int $id @@ -46,6 +47,8 @@ protected function casts(): array 'show_on_dashboard' => 'boolean', 'starts_at' => 'datetime', 'ends_at' => 'datetime', + // The banner renders it as markup on every page. + 'text' => SanitizedHtml::class, ]; } diff --git a/tests/Feature/AnnouncementResourceTest.php b/tests/Feature/AnnouncementResourceTest.php index 72bb483..6db912f 100644 --- a/tests/Feature/AnnouncementResourceTest.php +++ b/tests/Feature/AnnouncementResourceTest.php @@ -143,11 +143,21 @@ public function test_dismissed_cookie_prevents_prop_from_being_shared(): void public function test_dismiss_route_sets_announcement_dismissed_cookie(): void { - $announcement = Announcement::factory()->active()->create(); + $announcement = Announcement::factory()->active()->dismissable()->create(); $cookieName = config('announcements.cookie_name'); $response = $this->post(route('announcements.dismiss', $announcement)); $response->assertCookie($cookieName, (string) $announcement->id); } + + /** The banner offers no button for these; a hand-made request must not hide one either. */ + public function test_an_announcement_that_cannot_be_dismissed_is_not(): void + { + $announcement = Announcement::factory()->active()->create(['is_dismissable' => false]); + + $this->post(route('announcements.dismiss', $announcement)) + ->assertNotFound() + ->assertCookieMissing(config('announcements.cookie_name')); + } } diff --git a/tests/Unit/AnnouncementTest.php b/tests/Unit/AnnouncementTest.php index 5c8fcf8..eb98163 100644 --- a/tests/Unit/AnnouncementTest.php +++ b/tests/Unit/AnnouncementTest.php @@ -10,6 +10,20 @@ class AnnouncementTest extends TestCase { use RefreshDatabase; + /** The banner renders the text as HTML on every page, so markup that runs is stripped. */ + public function test_text_is_sanitised_for_rendering(): void + { + $announcement = Announcement::factory()->create([ + 'text' => '

Sale

', + ]); + + $text = $announcement->fresh()->text; + + $this->assertStringContainsString('Sale', $text); + $this->assertStringNotContainsString('onerror', $text); + $this->assertStringNotContainsString('first();