diff --git a/composer.json b/composer.json index a4ce4e9..a2d8655 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "license": "MIT", "version": "v3.5.2", "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/ShowcaseRoadmapDatabaseSeeder.php b/database/seeders/ShowcaseRoadmapDatabaseSeeder.php index 6dea187..4364a72 100644 --- a/database/seeders/ShowcaseRoadmapDatabaseSeeder.php +++ b/database/seeders/ShowcaseRoadmapDatabaseSeeder.php @@ -7,8 +7,10 @@ use Modules\Roadmap\Enums\RoadmapStatus; use Modules\Roadmap\Enums\RoadmapType; use Modules\Roadmap\Models\RoadmapItem; +use Saucebase\Core\Attributes\DemoAccount; use Spatie\Permission\Models\Role; +#[DemoAccount('roadmap@saucebase.dev', 'staff: the roadmap admin')] class ShowcaseRoadmapDatabaseSeeder extends Seeder { public function run(): void diff --git a/resources/boost/skills/saucebase-roadmap-development/SKILL.md b/resources/boost/skills/saucebase-roadmap-development/SKILL.md index 8aa5e25..b052bf6 100644 --- a/resources/boost/skills/saucebase-roadmap-development/SKILL.md +++ b/resources/boost/skills/saucebase-roadmap-development/SKILL.md @@ -65,6 +65,11 @@ Moderation hides rather than deletes: `hidden_at` plus a private `hidden_reason` `Actions\MergeItem` moves votes and comments to the target, drops votes from anyone who had voted on both (the unique index would reject them), then sets `merged_into_id` and closes the source. `comments()->reorder()` matters: the relation is ordered, and an ordered `UPDATE` is not portable. +- **The source's address redirects** (301, as a merge cannot be undone) to the target: `RoadmapController::show()`, before the visibility check, so shared links and "My feedback" still reach the conversation. The target's own rules apply there. +- **Only into a public item:** voters follow their votes and the mail names the target. `MergeItem` throws otherwise, and the admin picker lists public items only. +- **Once only:** the source is locked and read again inside the transaction; a double click or a second admin finds it merged and changes and sends nothing. +- **Its subscribers hear where it went:** `RoadmapItemMergedNotification` to the source's voters and author, read before the votes move, sent after commit. A merge sends no `StatusChanged` mail: closing the source is not a rejection (the `updated` hook skips a status change that comes with `merged_into_id`). + ### Notifications Go To Everyone Waiting `StatusChanged` dispatches after commit, so a rolled-back merge mails nobody, and the mail itself is queued. A status change notifies `subscribers()`: the submitter plus everyone who voted. Voting is how you subscribe. diff --git a/resources/js/react/pages/Show.tsx b/resources/js/react/pages/Show.tsx index d05c6df..6cec1b7 100644 --- a/resources/js/react/pages/Show.tsx +++ b/resources/js/react/pages/Show.tsx @@ -43,7 +43,7 @@ export default function Show({ canonical={item.url} type="article" > -
+
-
+
isPublic()) { + throw new InvalidArgumentException('An item can only be merged into a public one.'); + } + + $subscribers = DB::transaction(function () use ($duplicate, $target) { + // Locked and read again: a double click, or two admins at once, merges it once. + if (RoadmapItem::lockForUpdate()->find($duplicate->getKey())?->merged_into_id !== null) { + return null; + } + + // Read before the votes move: afterwards the duplicate has no voters. + $subscribers = $duplicate->subscribers(); + $duplicate->votes()->whereIn('user_id', $target->votes()->pluck('user_id'))->delete(); $duplicate->votes()->update(['roadmap_item_id' => $target->getKey()]); // The relation is ordered, and an ordered UPDATE is not portable. @@ -30,6 +46,12 @@ public function handle(RoadmapItem $duplicate, RoadmapItem $target): void 'merged_into_id' => $target->getKey(), 'status' => RoadmapStatus::Closed, ]); + + return $subscribers; }); + + if ($subscribers !== null) { + Notification::send($subscribers, (new RoadmapItemMergedNotification($duplicate, $target))->afterCommit()); + } } } diff --git a/src/Filament/Resources/Roadmap/Tables/RoadmapItemsTable.php b/src/Filament/Resources/Roadmap/Tables/RoadmapItemsTable.php index eefda72..5e99508 100644 --- a/src/Filament/Resources/Roadmap/Tables/RoadmapItemsTable.php +++ b/src/Filament/Resources/Roadmap/Tables/RoadmapItemsTable.php @@ -90,6 +90,8 @@ private static function mergeAction(): Action ->getSearchResultsUsing(fn (RoadmapItem $record, string $search) => RoadmapItem::query() ->whereKeyNot($record->getKey()) ->whereNull('merged_into_id') + // Only what visitors can see: MergeItem refuses anything else. + ->whereIn('status', RoadmapStatus::publicStatuses()) ->where('title', 'like', "%{$search}%") ->orderBy('title') ->limit(50) diff --git a/src/Http/Controllers/RoadmapController.php b/src/Http/Controllers/RoadmapController.php index fbfb9fc..787f62e 100644 --- a/src/Http/Controllers/RoadmapController.php +++ b/src/Http/Controllers/RoadmapController.php @@ -67,8 +67,14 @@ public function index(Request $request, RoadmapSettings $settings): Response * package would otherwise treat any ordinary link as "open me over the * previous page", and a shared link has to stay a real page. */ - public function show(RoadmapItem $item, RoadmapSettings $settings): Response|Modal + public function show(RoadmapItem $item, RoadmapSettings $settings): Response|Modal|RedirectResponse { + // Merged away: its votes and comments live on the target now, whose own rules apply. + // Permanent, as a merge cannot be undone. + if ($item->mergedInto !== null) { + return redirect($item->mergedInto->url(), 301); + } + // 404 and not 403: a refusal would confirm that a hidden item exists. abort_unless(Gate::allows('view', $item), 404); diff --git a/src/Models/RoadmapItem.php b/src/Models/RoadmapItem.php index 6d00f93..8bf137e 100644 --- a/src/Models/RoadmapItem.php +++ b/src/Models/RoadmapItem.php @@ -54,7 +54,8 @@ protected static function booted(): void }); static::updated(function (RoadmapItem $item): void { - if ($item->wasChanged('status')) { + // A merge closes the duplicate, but it isn't a rejection: MergeItem sends its own mail. + if ($item->wasChanged('status') && ! $item->wasChanged('merged_into_id')) { StatusChanged::dispatch($item); } }); diff --git a/src/Notifications/RoadmapItemMergedNotification.php b/src/Notifications/RoadmapItemMergedNotification.php new file mode 100644 index 0000000..66ded70 --- /dev/null +++ b/src/Notifications/RoadmapItemMergedNotification.php @@ -0,0 +1,54 @@ + + */ + public function via(object $notifiable): array + { + return ['mail']; + } + + public function toMail(object $notifiable): MailMessage + { + return (new MailMessage) + ->subject(__('A roadmap item you follow was merged')) + ->greeting(__('Hello :name,', ['name' => $notifiable->name])) + ->line(__('**":duplicate"** was merged into **":target"**, which asks for the same thing. Your vote and the comments now count there.', [ + 'duplicate' => Markdown::escape($this->duplicate->title), + 'target' => Markdown::escape($this->target->title), + ])) + // A fixed label: Laravel repeats it, unescaped, in the mail's fallback line. + ->action(__('View the item'), $this->target->url()) + ->line(__('Thank you for your feedback!')); + } + + /** + * @return array + */ + public function toArray(object $notifiable): array + { + return [ + 'roadmap_item_id' => $this->duplicate->id, + 'merged_into_id' => $this->target->id, + ]; + } +} diff --git a/tests/Feature/RoadmapMergeTest.php b/tests/Feature/RoadmapMergeTest.php index dca0a55..7c4451a 100644 --- a/tests/Feature/RoadmapMergeTest.php +++ b/tests/Feature/RoadmapMergeTest.php @@ -4,9 +4,12 @@ use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Facades\Notification; use Modules\Roadmap\Actions\MergeItem; use Modules\Roadmap\Enums\RoadmapStatus; use Modules\Roadmap\Models\RoadmapItem; +use Modules\Roadmap\Notifications\RoadmapItemMergedNotification; +use Modules\Roadmap\Notifications\RoadmapItemStatusChangedNotification; use Tests\TestCase; class RoadmapMergeTest extends TestCase @@ -61,4 +64,87 @@ public function test_a_user_who_voted_on_both_items_only_counts_once(): void $this->assertSame(1, $target->votes()->count()); } + + /** Voters follow their votes there, so the target must be one they can see. */ + public function test_an_item_is_only_merged_into_a_public_one(): void + { + $duplicate = RoadmapItem::factory()->planned()->create(); + $hidden = RoadmapItem::factory()->create(['status' => RoadmapStatus::UnderReview]); + + $this->expectException(\InvalidArgumentException::class); + + try { + app(MergeItem::class)->handle($duplicate, $hidden); + } finally { + $this->assertNull($duplicate->fresh()->merged_into_id); + } + } + + /** A link to the duplicate, shared or bookmarked, still reaches the conversation. */ + public function test_the_duplicates_address_leads_to_the_item_it_was_merged_into(): void + { + $duplicate = RoadmapItem::factory()->planned()->create(); + $target = RoadmapItem::factory()->planned()->create(); + + app(MergeItem::class)->handle($duplicate, $target); + + $this->get($duplicate->url())->assertStatus(301)->assertRedirect($target->url()); + } + + /** Their vote moved: voters and the author hear where, once, and nothing about "Closed". */ + /** A double click, or two admins at once: the second merge finds it merged and tells no one again. */ + public function test_a_second_merge_of_the_same_item_changes_nothing(): void + { + Notification::fake(); + $author = User::factory()->create(); + $duplicate = RoadmapItem::factory()->planned()->create(['user_id' => $author->id]); + $target = RoadmapItem::factory()->planned()->create(); + $stale = RoadmapItem::find($duplicate->id); + + app(MergeItem::class)->handle($duplicate, $target); + app(MergeItem::class)->handle($stale, $target); + + Notification::assertSentToTimes($author, RoadmapItemMergedNotification::class, 1); + } + + public function test_voters_and_the_author_are_told_where_it_went(): void + { + Notification::fake(); + $author = User::factory()->create(); + $voter = User::factory()->create(); + $duplicate = RoadmapItem::factory()->planned()->create(['user_id' => $author->id]); + $target = RoadmapItem::factory()->planned()->create(); + $duplicate->votes()->create(['user_id' => $voter->id]); + + app(MergeItem::class)->handle($duplicate, $target); + + foreach ([$author, $voter] as $user) { + Notification::assertSentTo($user, RoadmapItemMergedNotification::class, fn ($n) => $n->target->is($target)); + } + Notification::assertNotSentTo([$author, $voter], RoadmapItemStatusChangedNotification::class); + } + + public function test_titles_cannot_smuggle_a_link_into_the_merged_email(): void + { + $user = User::factory()->create(); + $duplicate = RoadmapItem::factory()->planned()->create(['title' => 'Evil [one](https://evil.test)']); + $target = RoadmapItem::factory()->planned()->create(['title' => 'Evil [two](https://evil.test)']); + + $mail = (new RoadmapItemMergedNotification($duplicate, $target))->toMail($user); + + $this->assertStringNotContainsString('href="https://evil.test"', (string) $mail->render()); + } + + public function test_the_merged_email_names_both_items_and_links_to_the_target(): void + { + $user = User::factory()->create(['name' => 'Ada']); + $duplicate = RoadmapItem::factory()->planned()->create(['title' => 'Dark mode']); + $target = RoadmapItem::factory()->planned()->create(['title' => 'Themes']); + + $mail = (new RoadmapItemMergedNotification($duplicate, $target))->toMail($user); + + $this->assertStringContainsString('Dark mode', implode(' ', $mail->introLines)); + $this->assertStringContainsString('Themes', implode(' ', $mail->introLines)); + $this->assertSame($target->url(), $mail->actionUrl); + } } diff --git a/tests/Feature/ShowcaseStaffTest.php b/tests/Feature/ShowcaseStaffTest.php index e61c105..0a51541 100644 --- a/tests/Feature/ShowcaseStaffTest.php +++ b/tests/Feature/ShowcaseStaffTest.php @@ -7,6 +7,7 @@ use Illuminate\Support\Facades\Hash; use Modules\Roadmap\Database\Seeders\DatabaseSeeder; use Modules\Roadmap\Database\Seeders\ShowcaseRoadmapDatabaseSeeder; +use Saucebase\Core\Testing\AssertsDemoAccounts; use Tests\TestCase; /** @@ -15,6 +16,7 @@ */ class ShowcaseStaffTest extends TestCase { + use AssertsDemoAccounts; use RefreshDatabase; public function test_the_demo_adds_a_staff_account_for_this_module_only(): void @@ -35,4 +37,10 @@ public function test_demo_accounts_use_the_configured_demo_password(): void $this->assertTrue(Hash::check('not-the-old-default', User::where('email', 'roadmap@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(ShowcaseRoadmapDatabaseSeeder::class); + } } diff --git a/tests/e2e/tests/roadmap.spec.ts b/tests/e2e/tests/roadmap.spec.ts index fa5b1bb..bdeba5e 100644 --- a/tests/e2e/tests/roadmap.spec.ts +++ b/tests/e2e/tests/roadmap.spec.ts @@ -308,9 +308,10 @@ test.describe('Roadmap', () => { await page.getByTestId('filter-mine').click(); const mine = page.getByTestId('roadmap-mine'); await expect(mine).toContainText('Export to CSV'); - await expect( - mine.locator('[data-testid^="item-status-"]'), - ).toHaveAttribute('data-state', 'under_review'); + const status = mine.locator('[data-testid^="item-status-"]'); + await expect(status).toBeVisible(); + await expect(status).toHaveAttribute('data-state', 'under_review'); + await expect(status).toHaveText(/\S/); await page.getByTestId('filter-mine-back').click(); await expect(page.getByTestId('roadmap-board')).toBeVisible();