From f21aead039dd0c75129727f67af5d921f15b1ba5 Mon Sep 17 00:00:00 2001 From: Patrick Niebeling Date: Mon, 16 Nov 2026 14:10:08 +0100 Subject: [PATCH] Let polls remind target one poll for testing `polls remind ` sends the reminder to that poll right away, ignoring the reminder window, an earlier reminder and POLL_REMINDER_DAYS. A forced reminder does not set the reminded marker. Co-Authored-By: Claude Sonnet 5.5 --- docs/deployment.md | 2 +- docs/operations.md | 2 + src/Command/PollsRemindCommand.php | 44 ++++++++++++++++---- tests/TestCase/Command/PollsCommandsTest.php | 42 +++++++++++++++++++ 4 files changed, 80 insertions(+), 10 deletions(-) diff --git a/docs/deployment.md b/docs/deployment.md index d5dae7d..ecc0b8e 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -125,7 +125,7 @@ Zum Verwalten per Hand und für Skripte/Cron: | `bin/cake polls list [--expired] [--email=text] [--sort=created\|activity\|title] [--json]` | Umfragen mit E-Mail, Ablaufdatum und Erinnerung | | `bin/cake polls show [--link] [--json]` | Details; mit `--link` auch der geheime Admin-Link (nur auf Wunsch) | | `bin/cake polls delete [--yes]` | Eine Umfrage samt Folien, Antworten und Bildern löschen (fragt nach) | -| `bin/cake polls remind [--dry-run]` | Erinnerungsmails für bald ablaufende Umfragen (je Umfrage einmal) | +| `bin/cake polls remind [] [--dry-run]` | Erinnerungsmails für bald ablaufende Umfragen (je Umfrage einmal). Mit ID oder Code geht die Mail sofort und erzwungen nur an diese Umfrage, zum Testen: unabhängig vom Erinnerungsfenster und von `POLL_REMINDER_DAYS`, beliebig oft, ohne die Umfrage als erinnert zu markieren (die reguläre Erinnerung kommt trotzdem noch) | | `bin/cake polls cleanup [--dry-run]` | Der Cronjob: erst erinnern, dann abgelaufene Umfragen löschen, danach verwaiste Bilddateien | Eine Umfrage läuft `POLL_RETENTION_DAYS` Tage (Standard 90) nach dem Anlegen ab und wird dann gelöscht, unabhängig davon, diff --git a/docs/operations.md b/docs/operations.md index 2f1bb88..a274bbe 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -16,6 +16,8 @@ Aufräumen und Erinnerungen, CI/CD, Release-Pakete, Container. ist die einzige Stelle, die den Admin-Link ausgibt (nie in `list`). `polls list|show --json` gibt nur JSON aus (`PollsCommand::pollData()`/`printJson()`, Ausgabemodus RAW, sonst würden `<…>` in Titeln als Markup verschluckt; nie das Token, `adminUrl` nur mit `--link`). + `polls remind ` erzwingt die Erinnerung für genau eine Umfrage (Testzweck): ignoriert Fenster, Marker und `POLL_REMINDER_DAYS`, + setzt `expiry_reminded_at` nicht, braucht aber ein Ablaufdatum (`POLL_RETENTION_DAYS` > 0). Tests: `PollsCommandsTest.php` (leert vorher alle Umfragen). ## Deployment diff --git a/src/Command/PollsRemindCommand.php b/src/Command/PollsRemindCommand.php index 526943c..3abfdaf 100644 --- a/src/Command/PollsRemindCommand.php +++ b/src/Command/PollsRemindCommand.php @@ -15,7 +15,11 @@ use Cake\I18n\DateTime; * Idempotent: a poll is marked after a successful send; a failed send is retried at the next run. * `polls cleanup` runs this first, a separate cron entry is not needed. * - * Usage: bin/cake polls remind [--dry-run] + * With an id or code the reminder goes to that one poll right now (for testing the mail): regardless of the + * reminder window, of an earlier reminder and of POLL_REMINDER_DAYS. A forced reminder does not set the + * "reminded" marker, so the regular reminder still goes out when it is due. + * + * Usage: bin/cake polls remind [] [--dry-run] */ class PollsRemindCommand extends PollsCommand { @@ -35,6 +39,10 @@ class PollsRemindCommand extends PollsCommand { return $parser ->setDescription(__('Erinnert Besitzer per E-Mail, bevor ihre Umfrage automatisch gelöscht wird.')) + ->addArgument('poll', [ + 'help' => __('Optional: ID oder Beitrittscode. Verschickt die Erinnerung sofort und erzwungen an diese Umfrage (zum Testen), ohne sie als erinnert zu markieren.'), + 'required' => false, + ]) ->addOption('dry-run', [ 'help' => __('Nur anzeigen, nichts verschicken.'), 'boolean' => true, @@ -48,16 +56,32 @@ class PollsRemindCommand extends PollsCommand */ public function execute(Arguments $args, ConsoleIo $io): int { - if (Poll::reminderLeadDays() === 0) { + $dryRun = (bool)$args->getOption('dry-run'); + $reference = $args->getArgument('poll'); + $forced = $reference !== null; + + if ($forced) { + $poll = $this->findPoll((string)$reference); + if ($poll === null) { + $io->err(__('Umfrage „{0}“ nicht gefunden.', $reference)); + + return static::CODE_ERROR; + } + if ($poll->expiresAt() === null) { + $io->err(__('Umfragen laufen nie ab (POLL_RETENTION_DAYS=0), die Mail braucht ein Ablaufdatum.')); + + return static::CODE_ERROR; + } + $due = [$poll]; + } elseif (Poll::reminderLeadDays() === 0) { $io->out(__('Erinnerungen sind aus (POLL_REMINDER_DAYS=0 oder Umfragen laufen nie ab).')); return static::CODE_SUCCESS; + } else { + /** @var list<\App\Model\Entity\Poll> $due */ + $due = $this->polls()->find('reminderDue')->orderBy(['Polls.id' => 'ASC'])->all()->toList(); } - $dryRun = (bool)$args->getOption('dry-run'); - /** @var list<\App\Model\Entity\Poll> $due */ - $due = $this->polls()->find('reminderDue')->orderBy(['Polls.id' => 'ASC'])->all()->toList(); - $sent = 0; $failed = 0; foreach ($due as $poll) { @@ -78,9 +102,11 @@ class PollsRemindCommand extends PollsCommand $failed++; continue; } - // Only the marker column; the poll may have been changed meanwhile. - $this->polls()->updateAll(['expiry_reminded_at' => DateTime::now()], ['id' => $poll->id]); - $io->out(__('Erinnert: {0}', $label)); + if (!$forced) { + // Only the marker column; the poll may have been changed meanwhile. + $this->polls()->updateAll(['expiry_reminded_at' => DateTime::now()], ['id' => $poll->id]); + } + $io->out(($forced ? __('Erinnert (erzwungen, nicht markiert): {0}', $label) : __('Erinnert: {0}', $label))); $sent++; } diff --git a/tests/TestCase/Command/PollsCommandsTest.php b/tests/TestCase/Command/PollsCommandsTest.php index 41d440f..4c6f90a 100644 --- a/tests/TestCase/Command/PollsCommandsTest.php +++ b/tests/TestCase/Command/PollsCommandsTest.php @@ -7,6 +7,7 @@ use App\Test\TestCase\PlenarioTestTrait; use Cake\Console\TestSuite\ConsoleIntegrationTestTrait; use Cake\Core\Configure; use Cake\I18n\DateTime; +use Cake\TestSuite\EmailTrait; use Cake\TestSuite\TestCase; /** @@ -15,6 +16,7 @@ use Cake\TestSuite\TestCase; class PollsCommandsTest extends TestCase { use ConsoleIntegrationTestTrait; + use EmailTrait; use PlenarioTestTrait; protected function setUp(): void @@ -193,6 +195,46 @@ class PollsCommandsTest extends TestCase $this->assertNotEmpty($youngToken . $dueToken); } + public function testRemindCanBeForcedForOnePoll(): void + { + [$young] = $this->createPoll('Frisch', 'frisch@example.org'); + [$other] = $this->createPoll('Anderer', 'anderer@example.org'); + + // Not in the reminder window at all, yet it is sent on request, to this poll only. + $this->exec('polls remind ' . $young->join_code); + $this->assertExitSuccess(); + $this->assertOutputContains('erzwungen'); + $this->assertMailCount(1); + $this->assertMailSentTo('frisch@example.org'); + $this->assertNull($this->pollsTable()->get($young->id)->expiry_reminded_at, 'a forced reminder does not mark the poll'); + + // Sendable again (also after a regular reminder), still not marked. + $this->pollsTable()->updateAll(['expiry_reminded_at' => DateTime::now()], ['id' => $young->id]); + $this->exec('polls remind ' . $young->id); + $this->assertExitSuccess(); + $this->assertMailCount(2); + + // Dry run sends nothing. + $this->exec('polls remind ' . $other->id . ' --dry-run'); + $this->assertOutputContains('Würde erinnern'); + $this->assertMailCount(2); + + // Works with reminders switched off, fails without an expiry date and for unknown polls. + Configure::write('Plenario.retention.reminderDays', 0); + $this->exec('polls remind ' . $other->id); + $this->assertExitSuccess(); + $this->assertMailCount(3); + + $this->exec('polls remind 99999'); + $this->assertExitError(); + $this->assertErrorContains('nicht gefunden'); + + Configure::write('Plenario.retention.days', 0); + $this->exec('polls remind ' . $other->id); + $this->assertExitError(); + $this->assertMailCount(3); + } + public function testCleanupRemindsThenDeletesOnlyExpiredPollsAndIsIdempotent(): void { [$old] = $this->createPoll('Alt'); -- 2.54.1