*/ protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses']; protected const PID = '0123456789abcdef0123456789abcdef'; protected function setUp(): void { parent::setUp(); $this->enableCsrfToken(); Cache::clear('throttle'); } public function testUnknownCodeIs404(): void { $this->get('/p/ZZZZZZ'); $this->assertResponseCode(404); } public function testWaitingScreenAndCookie(): void { [$poll] = $this->createPoll(); $this->get('/p/' . $poll->join_code); $this->assertResponseOk(); $this->assertResponseContains('Gleich geht'); $this->assertCookieIsSet('plenario_pid'); } public function testVoteOnceOnly(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_CHOICE); $this->goLive($poll, $slide); $optionId = $slide->slide_options[0]->id; $this->cookie('plenario_pid', self::PID); $this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'option_id' => $optionId]); $this->assertRedirect(['_name' => 'participate', 'code' => $poll->join_code]); $this->assertFlashMessage('Danke für deine Antwort!'); $this->cookie('plenario_pid', self::PID); $this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'option_id' => $optionId]); $this->assertFlashMessage('Du hast bereits abgestimmt.'); $this->assertSame(1, TableRegistry::getTableLocator()->get('Responses')->find()->count()); } public function testVoteForInactiveSlideIsRejected(): void { [$poll] = $this->createPoll(); $live = $this->createSlide($poll, Slide::TYPE_OPEN); $other = $this->createSlide($poll, Slide::TYPE_OPEN); $this->goLive($poll, $live); $this->post("/p/{$poll->join_code}/vote", ['slide_id' => $other->id, 'text' => 'Hallo']); $this->assertFlashMessage('Diese Frage ist nicht mehr aktiv.'); $this->assertSame(0, TableRegistry::getTableLocator()->get('Responses')->find()->count()); } public function testStateNeverContainsResults(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_SCALE); $this->goLive($poll, $slide); $this->cookie('plenario_pid', self::PID); $this->get("/p/{$poll->join_code}/state"); $state = json_decode((string)$this->_response->getBody(), true); $this->assertSame($slide->id, $state['slideId']); $this->assertFalse($state['hasResponded']); $this->assertNull($state['results']); $this->cookie('plenario_pid', self::PID); $this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'value' => 4]); $this->cookie('plenario_pid', self::PID); $this->get("/p/{$poll->join_code}/state"); $state = json_decode((string)$this->_response->getBody(), true); $this->assertTrue($state['hasResponded']); // Results are for the stage only, not even after answering or closing. $this->assertNull($state['results']); $this->slidesTable()->updateAll(['is_voting_open' => false], ['id' => $slide->id]); $this->cookie('plenario_pid', self::PID); $this->get("/p/{$poll->join_code}/state"); $this->assertNull(json_decode((string)$this->_response->getBody(), true)['results']); } public function testWordcloudAjaxVoteReturnsStateWithUpvoteCloud(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD, ['settings' => ['allow_upvotes' => true]]); $this->goLive($poll, $slide); $this->cookie('plenario_pid', self::PID); $this->configRequest(['headers' => ['X-Requested-With' => 'XMLHttpRequest', 'Accept' => 'application/json']]); $this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'word' => 'Kaffee']); $this->assertResponseOk(); $body = json_decode((string)$this->_response->getBody(), true); $this->assertTrue($body['ok']); $this->assertSame(['kaffee'], $body['state']['words']['mine']); // The cloud reaches the device because participants upvote in it. $this->assertSame('Kaffee', $body['state']['results']['words'][0]['text']); $this->cookie('plenario_pid', self::PID); $this->configRequest(['headers' => ['X-Requested-With' => 'XMLHttpRequest', 'Accept' => 'application/json']]); $this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'word' => 'kaffee']); $body = json_decode((string)$this->_response->getBody(), true); $this->assertFalse($body['ok']); $this->assertSame('Diesen Begriff hast du schon genannt.', $body['message']); } public function testWordcloudWithoutUpvotesHidesCloudBeforeAnswer(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD, ['settings' => ['allow_upvotes' => false]]); $this->goLive($poll, $slide); $this->cookie('plenario_pid', self::PID); $this->get('/p/' . $poll->join_code); $this->assertResponseOk(); $this->assertResponseContains('data-word-input'); $this->assertResponseNotContains('data-upvote-hint'); $this->cookie('plenario_pid', self::PID); $this->get("/p/{$poll->join_code}/state"); $state = json_decode((string)$this->_response->getBody(), true); $this->assertNull($state['results']); $this->assertFalse($state['words']['allowUpvotes']); } public function testClosedPoll(): void { [$poll] = $this->createPoll(); $poll->is_active = false; $this->pollsTable()->saveOrFail($poll); $this->get('/p/' . $poll->join_code); $this->assertResponseContains('Diese Umfrage ist geschlossen'); } public function testVotesAreThrottledPerIp(): void { Configure::write('Plenario.throttle.votesPerMinute', 2); [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD); $this->goLive($poll, $slide); foreach (['Kaffee', 'Tee', 'Wasser'] as $i => $word) { $this->cookie('plenario_pid', self::PID); $this->configRequest(['headers' => ['X-Requested-With' => 'XMLHttpRequest', 'Accept' => 'application/json']]); $this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'word' => $word]); $body = json_decode((string)$this->_response->getBody(), true); $this->assertSame($i < 2, $body['ok'], $word); } $this->assertResponseCode(429); $this->assertStringContainsString('Zu viele Antworten', $body['message']); $this->assertArrayHasKey('state', $body); $this->assertSame(2, TableRegistry::getTableLocator()->get('Responses')->find()->where(['slide_id' => $slide->id])->count()); } public function testOpenAnswerCanBeEditedWhileVotingIsOpen(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_OPEN); $this->goLive($poll, $slide); $responses = TableRegistry::getTableLocator()->get('Responses'); $this->cookie('plenario_pid', self::PID); $this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'text' => 'Tipfehler']); $this->cookie('plenario_pid', self::PID); $this->get('/p/' . $poll->join_code); $this->assertResponseContains('Antwort bearbeiten'); $this->assertResponseContains('Tipfehler'); $this->cookie('plenario_pid', self::PID); $this->post("/p/{$poll->join_code}/edit", ['slide_id' => $slide->id, 'text' => ' Tippfehler ']); $this->assertRedirect(['_name' => 'participate', 'code' => $poll->join_code]); $this->assertFlashMessage('Deine Antwort wurde geändert.'); $this->assertSame(1, $responses->find()->count()); $this->assertSame('Tippfehler', $responses->find()->firstOrFail()->text_value); // An open answer cannot be emptied. $this->cookie('plenario_pid', self::PID); $this->post("/p/{$poll->join_code}/edit", ['slide_id' => $slide->id, 'text' => ' ']); $this->assertFlashMessage('Bitte gib eine Antwort ein.'); $this->assertSame('Tippfehler', $responses->find()->firstOrFail()->text_value); // Without an own answer there is nothing to edit (and nothing is created). $this->cookie('plenario_pid', str_repeat('f', 32)); $this->post("/p/{$poll->join_code}/edit", ['slide_id' => $slide->id, 'text' => 'Neu']); $this->assertFlashMessage('Du hast hier noch nicht geantwortet.'); $this->assertSame(1, $responses->find()->count()); // Closed: no form, no change. TableRegistry::getTableLocator()->get('Slides')->updateAll(['is_voting_open' => false], ['id' => $slide->id]); $this->cookie('plenario_pid', self::PID); $this->get('/p/' . $poll->join_code); $this->assertResponseNotContains('Antwort bearbeiten'); $this->cookie('plenario_pid', self::PID); $this->post("/p/{$poll->join_code}/edit", ['slide_id' => $slide->id, 'text' => 'Zu spät']); $this->assertFlashMessage('Die Abstimmung zu dieser Frage ist geschlossen.'); $this->assertSame('Tippfehler', $responses->find()->firstOrFail()->text_value); } public function testChoiceAnswersCannotBeEdited(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_CHOICE); $this->goLive($poll, $slide); $this->cookie('plenario_pid', self::PID); $this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'option_id' => $slide->slide_options[0]->id]); $this->cookie('plenario_pid', self::PID); $this->post("/p/{$poll->join_code}/edit", ['slide_id' => $slide->id, 'text' => 'Hack']); $this->assertFlashMessage('Diese Antwort kann nicht mehr geändert werden.'); $this->assertFalse(TableRegistry::getTableLocator()->get('Responses')->exists(['text_value' => 'Hack'])); } public function testScaleCommentOnlyWithoutRating(): void { [$poll] = $this->createPoll(); $slide = $this->createSlide($poll, Slide::TYPE_SCALE, ['settings' => ['comments' => true]]); $this->goLive($poll, $slide); $this->cookie('plenario_pid', self::PID); $this->get('/p/' . $poll->join_code); $this->assertResponseNotContains('auch nur einen Kommentar'); $slide = $this->createSlide($poll, Slide::TYPE_SCALE, ['settings' => ['comments' => true, 'comment_only' => true]]); $this->goLive($poll, $slide); $this->cookie('plenario_pid', self::PID); $this->get('/p/' . $poll->join_code); $this->assertResponseContains('auch nur einen Kommentar'); $this->assertResponseNotContains('required>'); $this->cookie('plenario_pid', self::PID); $this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'comment' => 'Keine Meinung dazu']); $this->assertFlashMessage('Danke für deine Antwort!'); $this->cookie('plenario_pid', self::PID); $this->get('/p/' . $poll->join_code); $this->assertResponseContains('Keine Meinung dazu'); } }