From ebfab0ef205084449ab3e5cc984820b1d4bd2b2e Mon Sep 17 00:00:00 2001 From: Patrick Niebeling Date: Thu, 19 Nov 2026 09:16:54 +0100 Subject: [PATCH] Add polls seed command for demo and load test polls Every run creates a new poll with one slide per interactive type and shows the first (choice) slide live with voting open. Co-Authored-By: Claude Sonnet 5.5 --- AGENTS.md | 4 +- src/Command/PollsSeedCommand.php | 133 +++++++++++++++++++ tests/TestCase/Command/PollsCommandsTest.php | 18 +++ 3 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 src/Command/PollsSeedCommand.php diff --git a/AGENTS.md b/AGENTS.md index ae36aef..cf44474 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 list\|show\|delete\|remind\|cleanup\|seed` (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). @@ -70,7 +70,7 @@ src/ QaService, SlideControlService, ResetService, ReportService Model/ Polls → Slides → SlideOptions, Slides → Responses Middleware/ HostHeader, SecurityHeaders (CSP mit Nonce, Framing, Referrer-Policy) - Command/ images_cleanup, polls list|show|delete|remind|cleanup + Command/ images_cleanup, polls list|show|delete|remind|cleanup|seed Mailer/ PollMailer templates/ Deutsche UI; Layouts: default, present, error, email, fragment, pdf webroot/ css/plenario.css (eigenes CSS, Tokens), js/plenario.js (Vanilla JS) diff --git a/src/Command/PollsSeedCommand.php b/src/Command/PollsSeedCommand.php new file mode 100644 index 0000000..d998148 --- /dev/null +++ b/src/Command/PollsSeedCommand.php @@ -0,0 +1,133 @@ +setDescription(__('Legt eine neue Demo-Umfrage mit Beispielfolien an (z. B. für den Lasttest). Jeder Aufruf erzeugt eine neue Umfrage.')) + ->addOption('title', [ + 'help' => __('Titel der Umfrage.'), + 'default' => 'Lasttest', + ]) + ->addOption('email', [ + 'help' => __('E-Mail-Adresse des Besitzers (es wird nichts verschickt).'), + 'default' => 'lasttest@example.org', + ]) + ->addOption('json', [ + 'help' => __('Ausgabe als JSON statt als Text (für Skripte), inkl. Admin-Link.'), + 'boolean' => true, + ]); + } + + /** + * @param \Cake\Console\Arguments $args Arguments + * @param \Cake\Console\ConsoleIo $io Console + * @return int + */ + public function execute(Arguments $args, ConsoleIo $io): int + { + $polls = $this->polls(); + $slides = $this->fetchTable('Slides'); + + $result = $polls->getConnection()->transactional(function () use ($polls, $slides, $args): array { + [$poll, $token] = $polls->createPoll([ + 'title' => (string)$args->getOption('title'), + 'email' => (string)$args->getOption('email'), + ]); + if ($token === null) { + return [$poll, null, 0]; + } + + $first = null; + $count = 0; + foreach ($this->definitions() as [$type, $question, $options, $correct]) { + $slide = $slides->newEntity([ + 'type' => $type, + 'question' => $question, + 'settings' => Slide::DEFAULT_SETTINGS[$type] ?? [], + ]); + $slide->poll_id = $poll->id; + $slides->saveOrFail($slide); + if ($options !== []) { + $slides->syncOptions($slide, $options, $correct); + } + $first ??= $slide; + $count++; + } + + $poll->showSlide($first->id); + $polls->saveOrFail($poll); + + return [$poll, $token, $count]; + }); + + [$poll, $token, $count] = $result; + if ($token === null) { + $io->err(__('Umfrage konnte nicht angelegt werden: {0}', (string)json_encode($poll->getErrors()))); + + return static::CODE_ERROR; + } + + $adminUrl = Router::url(['_name' => 'admin', 'token' => $token], true); + if ($args->getOption('json')) { + $this->printJson($io, $this->pollData($poll) + ['slides' => $count, 'adminUrl' => $adminUrl]); + + return static::CODE_SUCCESS; + } + + $io->out([ + __('Umfrage #{0} angelegt mit {1} Folien (erste Folie live, Abstimmung offen).', $poll->id, $count), + __('Beitrittscode: {0}', $poll->join_code), + __('Teilnahme-Link: {0}', $this->joinLink($poll)), + __('Admin-Link: {0}', $adminUrl), + __('Lasttest: jmeter -n -t tests/load/plenario.jmx -Jcode={0} …', $poll->join_code), + ]); + + return static::CODE_SUCCESS; + } + + /** + * @return list, 3: list}> Type, question, options, correct options + */ + protected function definitions(): array + { + return [ + [Slide::TYPE_CHOICE, 'Wie gefällt dir PlenarIO?', ['Super', 'Gut', 'Geht so', 'Gar nicht'], []], + [Slide::TYPE_WORDCLOUD, 'Was fällt dir zu Live-Umfragen ein?', [], []], + [Slide::TYPE_OPEN, 'Was wünschst du dir für heute?', [], []], + [Slide::TYPE_SCALE, 'Wie zufrieden bist du?', [], []], + [Slide::TYPE_QUIZ, 'Wie viele Kontinente gibt es?', ['7', '5', '6', '8'], ['7']], + [Slide::TYPE_QA, 'Stell deine Frage', [], []], + [Slide::TYPE_ORDER, 'Bringe die Planeten in die richtige Reihenfolge', ['Merkur', 'Venus', 'Erde', 'Mars'], []], + ]; + } +} diff --git a/tests/TestCase/Command/PollsCommandsTest.php b/tests/TestCase/Command/PollsCommandsTest.php index 4c6f90a..e7b4192 100644 --- a/tests/TestCase/Command/PollsCommandsTest.php +++ b/tests/TestCase/Command/PollsCommandsTest.php @@ -51,6 +51,24 @@ class PollsCommandsTest extends TestCase $this->assertTrue($this->pollsTable()->get($poll->id)->isExpired()); } + public function testSeedCreatesNewPollWithLiveFirstSlideEveryTime(): void + { + $this->exec('polls seed --json'); + $this->assertExitSuccess(); + $first = json_decode(implode('', $this->_out->messages()), true); + $this->exec('polls seed --json'); + $second = json_decode(implode('', $this->_out->messages()), true); + + $this->assertNotSame($first['id'], $second['id']); + $this->assertNotSame($first['joinCode'], $second['joinCode']); + $this->assertSame(7, $second['slides']); + + $poll = $this->pollsTable()->get($second['id']); + $slide = $this->slidesTable()->get($poll->current_slide_id); + $this->assertSame('choice', $slide->type); + $this->assertTrue($slide->isVotingOpen()); + } + public function testSettingsAreRespected(): void { [$poll] = $this->createPoll();