diff --git a/AGENTS.md b/AGENTS.md index 574c571..f43d4c8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -82,7 +82,8 @@ tests/ PHPUnit; PlenarioTestTrait erzeugt Testdaten, Fixtures ### Datenmodell - `polls`: `title`, `email`, `admin_token_hash` (sha256), `admin_token_encrypted` (Security::encrypt), - `join_code` (6 Zeichen, ohne 0/O/1/I/L), `current_slide_id`, `is_active`, `show_results_to_participants` (ungenutzt, Altlast), + `join_code` (6 Zeichen, ohne 0/O/1/I/L), `current_slide_id`, `is_active`, + `last_admin_access` (höchstens alle 10 min aktualisiert, Grundlage für ein künftiges Aufräumen alter Umfragen), `embed_key` (Einbett-Schlüssel), `embed_seen_at` (letztes Lebenszeichen der eingebetteten Live-Folie), `paused_slide_id` (Folie vor der Pause) - `slides`: `type` (`choice|wordcloud|open|scale|content|quiz|youtube|qa|order`), `question`, `description`, `position`, diff --git a/config/Migrations/20261108130000_DropShowResultsToParticipants.php b/config/Migrations/20261108130000_DropShowResultsToParticipants.php new file mode 100644 index 0000000..db146df --- /dev/null +++ b/config/Migrations/20261108130000_DropShowResultsToParticipants.php @@ -0,0 +1,37 @@ +table('polls'); + if ($table->hasColumn('show_results_to_participants')) { + $table->removeColumn('show_results_to_participants')->update(); + } + } + + /** + * @return void + */ + public function down(): void + { + $table = $this->table('polls'); + if (!$table->hasColumn('show_results_to_participants')) { + $table->addColumn('show_results_to_participants', 'boolean', [ + 'null' => false, + 'default' => true, + 'after' => 'is_active', + ])->update(); + } + } +} diff --git a/src/Captcha/CaptchaInterface.php b/src/Captcha/CaptchaInterface.php index 51d74fd..7ff028f 100644 --- a/src/Captcha/CaptchaInterface.php +++ b/src/Captcha/CaptchaInterface.php @@ -13,11 +13,6 @@ use Cake\Http\ServerRequest; */ interface CaptchaInterface { - /** - * @return string 'none' | 'math' | 'recaptcha' - */ - public function getType(): string; - /** * Prepares a challenge and returns the data the view element needs. * diff --git a/src/Captcha/MathCaptcha.php b/src/Captcha/MathCaptcha.php index 2b7099e..610b528 100644 --- a/src/Captcha/MathCaptcha.php +++ b/src/Captcha/MathCaptcha.php @@ -22,14 +22,6 @@ class MathCaptcha implements CaptchaInterface { } - /** - * @inheritDoc - */ - public function getType(): string - { - return 'math'; - } - /** * @return string */ diff --git a/src/Captcha/NoCaptcha.php b/src/Captcha/NoCaptcha.php index 5b9d774..c8e3dd6 100644 --- a/src/Captcha/NoCaptcha.php +++ b/src/Captcha/NoCaptcha.php @@ -10,14 +10,6 @@ use Cake\Http\ServerRequest; */ class NoCaptcha implements CaptchaInterface { - /** - * @inheritDoc - */ - public function getType(): string - { - return 'none'; - } - /** * @inheritDoc */ diff --git a/src/Captcha/RecaptchaCaptcha.php b/src/Captcha/RecaptchaCaptcha.php index 06f2e2e..9b5366c 100644 --- a/src/Captcha/RecaptchaCaptcha.php +++ b/src/Captcha/RecaptchaCaptcha.php @@ -34,14 +34,6 @@ class RecaptchaCaptcha implements CaptchaInterface $this->client = $client ?? new Client(['timeout' => 5]); } - /** - * @inheritDoc - */ - public function getType(): string - { - return 'recaptcha'; - } - /** * @inheritDoc */ diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 45241ec..585fa25 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -72,8 +72,7 @@ class HomeController extends AppController $this->request->allowMethod(['post']); $captcha = CaptchaFactory::create('create'); - $poll = $this->Polls->newEmptyEntity(); - $poll = $this->Polls->patchEntity($poll, (array)$this->request->getData(), ['fields' => ['title', 'email']]); + $poll = $this->Polls->newPoll((array)$this->request->getData()); if ($this->isHoneypotFilled()) { $this->Flash->error(__('Die Umfrage konnte nicht angelegt werden.')); @@ -82,7 +81,7 @@ class HomeController extends AppController } elseif (!$poll->hasErrors() && !$this->throttle('create', 'Plenario.throttle.createPerHour')) { $this->Flash->error(__('Zu viele Umfragen in kurzer Zeit. Bitte versuche es später noch einmal.')); } elseif (!$poll->hasErrors()) { - [$poll, $token] = $this->Polls->createPoll((array)$this->request->getData()); + [$poll, $token] = $this->Polls->insertPoll($poll); if ($token !== null) { $adminUrl = Router::url(['_name' => 'admin', 'token' => $token], true); $joinUrl = Router::url(['_name' => 'participate', 'code' => $poll->join_code], true); diff --git a/src/Controller/SlidesController.php b/src/Controller/SlidesController.php index dc51a66..809cba2 100644 --- a/src/Controller/SlidesController.php +++ b/src/Controller/SlidesController.php @@ -41,7 +41,6 @@ class SlidesController extends AdminAppController $slide = $this->Slides->newEntity(['type' => $type, 'settings' => Slide::DEFAULT_SETTINGS[$type]], [ 'validate' => false, ]); - $slide->setNew(true); $slide->poll_id = $this->poll->id; // A quiz is started by the presenter (that also starts its timer). $slide->is_voting_open = $type !== Slide::TYPE_QUIZ; diff --git a/src/Model/Table/PollsTable.php b/src/Model/Table/PollsTable.php index f0fcc87..c31391b 100644 --- a/src/Model/Table/PollsTable.php +++ b/src/Model/Table/PollsTable.php @@ -182,6 +182,22 @@ class PollsTable extends Table return (string)preg_replace('/[^A-Z0-9]/', '', strtoupper($code)); } + /** + * Builds and validates a new, not yet saved poll from form input (title + email, normalised). + * + * @param array $data Request data + * @return \App\Model\Entity\Poll Check getErrors() + */ + public function newPoll(array $data): Poll + { + $field = fn(string $key): string => is_scalar($data[$key] ?? null) ? trim((string)$data[$key]) : ''; + + return $this->newEntity([ + 'title' => $field('title'), + 'email' => strtolower($field('email')), + ], ['fields' => ['title', 'email']]); + } + /** * Creates and persists a new poll. * @@ -190,11 +206,17 @@ class PollsTable extends Table */ public function createPoll(array $data): array { - $poll = $this->newEntity([ - 'title' => trim((string)($data['title'] ?? '')), - 'email' => strtolower(trim((string)($data['email'] ?? ''))), - ], ['fields' => ['title', 'email']]); + return $this->insertPoll($this->newPoll($data)); + } + /** + * Assigns token and join code and saves a poll built by newPoll(). + * + * @param \App\Model\Entity\Poll $poll New poll + * @return array{0: \App\Model\Entity\Poll, 1: string|null} The entity (check getErrors()) and the plain token on success + */ + public function insertPoll(Poll $poll): array + { if ($poll->hasErrors()) { return [$poll, null]; } diff --git a/src/View/AjaxView.php b/src/View/AjaxView.php deleted file mode 100644 index c37f318..0000000 --- a/src/View/AjaxView.php +++ /dev/null @@ -1,46 +0,0 @@ -response = $this->response->withType('ajax'); - } -} diff --git a/templates/cell/.gitkeep b/templates/cell/.gitkeep deleted file mode 100644 index 8b13789..0000000 --- a/templates/cell/.gitkeep +++ /dev/null @@ -1 +0,0 @@ - diff --git a/templates/layout/ajax.php b/templates/layout/ajax.php deleted file mode 100644 index cd51169..0000000 --- a/templates/layout/ajax.php +++ /dev/null @@ -1,17 +0,0 @@ -fetch('content'); diff --git a/tests/TestCase/Controller/HomeControllerTest.php b/tests/TestCase/Controller/HomeControllerTest.php index 5acfafb..5e75d37 100644 --- a/tests/TestCase/Controller/HomeControllerTest.php +++ b/tests/TestCase/Controller/HomeControllerTest.php @@ -70,6 +70,17 @@ class HomeControllerTest extends TestCase $this->assertResponseNotContains('/admin/', 'Admin link must not be shown when disabled'); } + public function testCreateAcceptsEmailWithSurroundingSpaces(): void + { + $this->session(['Captcha.math.create' => 7]); + $this->post('/create', ['title' => ' Retro ', 'email' => ' Owner@Example.org ', 'captcha_answer' => '7']); + + $this->assertRedirect(['_name' => 'created']); + $poll = $this->pollsTable()->find()->firstOrFail(); + $this->assertSame('Retro', $poll->title); + $this->assertSame('owner@example.org', $poll->email); + } + public function testIndexRendersHiddenHoneypot(): void { $this->get('/');