diff --git a/AGENTS.md b/AGENTS.md index 5262fef..7bb5527 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -34,7 +34,7 @@ E-Mail anlegen, kein Login, Verwaltung über einen geheimen Admin-Link, Teilnahm | Coding Standard | `vendor/bin/phpcs` / Autofix `vendor/bin/phpcbf` | | Neue Migration | `bin/cake bake migration NameDerMigration` | | Verwaiste Folienbilder löschen | `bin/cake images_cleanup [--dry-run]` | -| Umfragen verwalten (Admin-CLI) | `bin/cake polls list\|show\|delete\|remind\|cleanup` (siehe [docs/operations.md](docs/operations.md)) | +| Umfragen verwalten (Admin-CLI) | `bin/cake polls` (Menü) bzw. `polls list\|show\|delete\|remind\|cleanup` (siehe [docs/operations.md](docs/operations.md)) | **Vor jedem Commit:** `vendor/bin/phpunit` und `vendor/bin/phpcs` müssen grün sein (Warnungen zur Zeilenlänge bei langen deutschen Texten sind ok). diff --git a/docs/architecture.md b/docs/architecture.md index 2c9f723..ad86e30 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -24,7 +24,7 @@ src/ EmbedController /e/{key}/{id}: eine Folie zum Einbetten (z. B. Canva), activate/heartbeat/pause/control ErrorController Fehlerseiten (Templates unter templates/Error, Layout `error`) Command/ImagesCleanupCommand `images_cleanup`: Bilddateien ohne Folie entfernen (idempotent) - Command/Polls*Command `polls list|show|delete|remind|cleanup` (Basis `PollsCommand`: Suche per id/Code, Links, Löschen) + Command/Polls*Command `polls` (Menü), `polls list|show|delete|remind|cleanup` (Basis `PollsCommand`: Suche per id/Code, Links, Löschen) Mailer/ PollMailer (adminLink, recovery; `sendSafely()` loggt Transportfehler statt zu werfen), Transport/FileTransport (lokal) Middleware/ HostHeaderMiddleware: prüft in Produktion den Host gegen APP_FULL_BASE_URL; SecurityHeadersMiddleware (äußerste Schicht): CSP mit Nonce, Framing, nosniff, Referrer-/Permissions-Policy diff --git a/docs/deployment.md b/docs/deployment.md index 3716d7b..2fa4812 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -118,8 +118,13 @@ Bei Updates: Code aktualisieren, `composer install --no-dev`, Migrationen und `c ### Umfragen verwalten und aufräumen (CLI) +Am einfachsten geht es menügeführt: `bin/cake polls` ohne Unterbefehl zeigt ein Menü (Umfragen anzeigen und suchen, +Umfrage öffnen mit Details, Admin-Link und Löschen, Erinnern, Aufräumen). Destruktive Aktionen zeigen vorher eine Vorschau +und fragen nach, jede Eingabe hat eine sichere Vorgabe (Zurück/Nein). Für Skripte und Cron gibt es die einzelnen Befehle: + | Befehl | Zweck | |---|---| +| `bin/cake polls` | Menü (interaktiv) | | `bin/cake polls list [--expired] [--email=text] [--sort=created\|activity\|title]` | Umfragen mit E-Mail, Ablaufdatum und Erinnerung | | `bin/cake polls show [--link]` | 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) | diff --git a/docs/operations.md b/docs/operations.md index baff5a7..2e8de48 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -13,7 +13,9 @@ Aufräumen und Erinnerungen, CI/CD, Release-Pakete, Container. Aufräumen per Cron: `bin/cake polls cleanup` (erst `remind`, dann idempotent löschen über `PollsTable::delete()` samt Folien/Bildern, danach `images_cleanup`; `--dry-run`; Cron-Einrichtung für Server und Docker steht in [deployment.md](deployment.md) unter „Cronjob einrichten“, im Container läuft kein cron). `last_admin_access` fließt nicht mehr ein. `polls show --link` - ist die einzige Stelle, die den Admin-Link ausgibt (nie in `list`). Tests: `PollsCommandsTest.php` (leert vorher alle Umfragen). + ist die einzige Stelle, die den Admin-Link ausgibt (nie in `list`). Das Menü `bin/cake polls` (`PollsMenuCommand`) ruft nur diese Befehle per `executeCommand()` auf; jede Abfrage hat eine + Vorgabe (`0`/leer/`n`), damit geschlossene Eingabe es beendet statt zu schleifen; Löschen/Aufräumen erst nach Vorschau (`--dry-run`) und Rückfrage. + Tests: `PollsCommandsTest.php` (leert vorher alle Umfragen). ## Deployment diff --git a/src/Command/PollsMenuCommand.php b/src/Command/PollsMenuCommand.php new file mode 100644 index 0000000..b6db5d4 --- /dev/null +++ b/src/Command/PollsMenuCommand.php @@ -0,0 +1,165 @@ +setDescription(__('Menügeführte Verwaltung der Umfragen. Für Skripte und Cron: polls list|show|delete|remind|cleanup.')); + } + + /** + * @param \Cake\Console\Arguments $args Arguments + * @param \Cake\Console\ConsoleIo $io Console + * @return int + */ + public function execute(Arguments $args, ConsoleIo $io): int + { + while (true) { + $io->out(''); + $io->out('' . __('PlenarIO – Umfragen verwalten') . ''); + $io->out([ + ' 1) ' . __('Alle Umfragen anzeigen'), + ' 2) ' . __('Abgelaufene Umfragen anzeigen'), + ' 3) ' . __('Nach E-Mail-Adresse suchen'), + ' 4) ' . __('Umfrage öffnen (Details, Admin-Link, Löschen)'), + ' 5) ' . __('Erinnerungen vor dem Ablauf versenden'), + ' 6) ' . __('Aufräumen (Erinnern, abgelaufene Umfragen löschen)'), + ' 0) ' . __('Beenden'), + ]); + + switch ($io->askChoice(__('Was möchtest du tun?'), ['1', '2', '3', '4', '5', '6', '0'], '0')) { + case '1': + $this->executeCommand(PollsListCommand::class, [], $io); + break; + case '2': + $this->executeCommand(PollsListCommand::class, ['--expired'], $io); + break; + case '3': + $email = $io->ask(__('Teil der E-Mail-Adresse (leer = zurück)'), ''); + if ($email !== '') { + $this->executeCommand(PollsListCommand::class, ['--email=' . $email], $io); + } + break; + case '4': + $this->openPoll($io); + break; + case '5': + $this->confirmed($io, PollsRemindCommand::class, $this->polls()->find('reminderDue')->count()); + break; + case '6': + $this->confirmed($io, PollsCleanupCommand::class, $this->polls()->find('expired')->count() + + $this->polls()->find('reminderDue')->count()); + break; + default: + return static::CODE_SUCCESS; + } + } + } + + /** + * Shows what a command would do (--dry-run), asks and only then runs it for real. + * + * @param \Cake\Console\ConsoleIo $io Console + * @param class-string<\Cake\Command\Command> $command Command with a --dry-run option + * @param int $affected Number of polls the command would touch + * @return void + */ + protected function confirmed(ConsoleIo $io, string $command, int $affected): void + { + if ($affected === 0) { + $io->out(__('Nichts zu tun.')); + + return; + } + + $this->executeCommand($command, ['--dry-run'], $io); + if ($io->askChoice(__('Jetzt ausführen?'), ['y', 'n'], 'n') === 'y') { + $this->executeCommand($command, [], $io); + } else { + $io->out(__('Abgebrochen.')); + } + } + + /** + * Asks for one poll and offers the actions on it until the user goes back. + * + * @param \Cake\Console\ConsoleIo $io Console + * @return void + */ + protected function openPoll(ConsoleIo $io): void + { + $this->executeCommand(PollsListCommand::class, [], $io); + $reference = $io->ask(__('ID oder Code der Umfrage (leer = zurück)'), ''); + if ($reference === '') { + return; + } + $poll = $this->findPoll($reference); + if ($poll === null) { + $io->err(__('Umfrage „{0}“ nicht gefunden.', $reference)); + + return; + } + + $this->executeCommand(PollsShowCommand::class, [(string)$poll->id], $io); + while (true) { + $io->out([ + '', + ' 1) ' . __('Details anzeigen'), + ' 2) ' . __('Admin-Link anzeigen (geheim!)'), + ' 3) ' . __('Umfrage endgültig löschen'), + ' 0) ' . __('Zurück'), + ]); + $choice = $io->askChoice(__('#{0} {1}: Was möchtest du tun?', $poll->id, $this->shorten($poll)), ['1', '2', '3', '0'], '0'); + if ($choice === '1') { + $this->executeCommand(PollsShowCommand::class, [(string)$poll->id], $io); + } elseif ($choice === '2') { + $this->executeCommand(PollsShowCommand::class, [(string)$poll->id, '--link'], $io); + } elseif ($choice === '3') { + // The delete command asks for confirmation itself. + $this->executeCommand(PollsDeleteCommand::class, [(string)$poll->id], $io); + if ($this->polls()->find()->where(['Polls.id' => $poll->id])->count() === 0) { + return; + } + } else { + return; + } + } + } + + /** + * @param \App\Model\Entity\Poll $poll Poll + * @return string Title, shortened for prompts + */ + protected function shorten(Poll $poll): string + { + return mb_strimwidth($poll->title, 0, 40, '…'); + } +} diff --git a/tests/TestCase/Command/PollsCommandsTest.php b/tests/TestCase/Command/PollsCommandsTest.php index 18a82b1..ced826d 100644 --- a/tests/TestCase/Command/PollsCommandsTest.php +++ b/tests/TestCase/Command/PollsCommandsTest.php @@ -118,6 +118,53 @@ class PollsCommandsTest extends TestCase $this->assertExitError(); } + public function testMenuQuits(): void + { + $this->exec('polls', ['0']); + $this->assertExitSuccess(); + $this->assertOutputContains('Umfragen verwalten'); + } + + public function testMenuListsAndOpensPollWithLink(): void + { + [$poll, $token] = $this->createPoll('Menü-Umfrage', 'menu@example.org'); + + $this->exec('polls', ['1', '4', (string)$poll->id, '2', '0', '0']); + $this->assertExitSuccess(); + $this->assertOutputContains('menu@example.org'); + $this->assertOutputContains('https://plenario.test/admin/' . $token); + + $this->exec('polls', ['4', 'ZZZZZZ', '0']); + $this->assertExitSuccess(); + $this->assertErrorContains('nicht gefunden'); + } + + public function testMenuDeleteNeedsConfirmation(): void + { + [$poll] = $this->createPoll(); + + $this->exec('polls', ['4', (string)$poll->id, '3', 'n', '0', '0']); + $this->assertSame(1, $this->pollsTable()->find()->where(['id' => $poll->id])->count()); + + $this->exec('polls', ['4', (string)$poll->id, '3', 'y', '0']); + $this->assertSame(0, $this->pollsTable()->find()->where(['id' => $poll->id])->count()); + } + + public function testMenuCleanupAsksBeforeDeleting(): void + { + [$old] = $this->createPoll(); + $this->age($old->id, 100); + + $this->exec('polls', ['6', 'n', '0']); + $this->assertSame(1, $this->pollsTable()->find()->count()); + + $this->exec('polls', ['6', 'y', '0']); + $this->assertSame(0, $this->pollsTable()->find()->count()); + + $this->exec('polls', ['6', '0']); + $this->assertOutputContains('Nichts zu tun.'); + } + public function testReminderGoesOutOnceInTheLastDaysBeforeExpiry(): void { [$young, $youngToken] = $this->createPoll('Jung', 'jung@example.org');