Merge pull request 'Remove dead code and validate new polls only once' (#33) from chore/remove-dead-code into main
Reviewed-on: #33
This commit was merged in pull request #33.
This commit is contained in:
commit
a62e13eeff
13 files changed
+78
-102
No files matched your search
@@ -85,7 +85,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`,
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Migrations\BaseMigration;
|
||||
|
||||
/**
|
||||
* polls.show_results_to_participants is unused: results belong on the stage, participants
|
||||
* only get them where they interact with them (see PollStateService). There is no switch per poll.
|
||||
*/
|
||||
class DropShowResultsToParticipants extends BaseMigration
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
$table = $this->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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -22,14 +22,6 @@ class MathCaptcha implements CaptchaInterface
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return 'math';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
|
||||
@@ -10,14 +10,6 @@ use Cake\Http\ServerRequest;
|
||||
*/
|
||||
class NoCaptcha implements CaptchaInterface
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return 'none';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
|
||||
@@ -34,14 +34,6 @@ class RecaptchaCaptcha implements CaptchaInterface
|
||||
$this->client = $client ?? new Client(['timeout' => 5]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return 'recaptcha';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -184,6 +184,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<string, mixed> $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.
|
||||
*
|
||||
@@ -192,11 +208,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];
|
||||
}
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
||||
* @link https://cakephp.org CakePHP(tm) Project
|
||||
* @since 3.0.4
|
||||
* @license https://opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
namespace App\View;
|
||||
|
||||
/**
|
||||
* A view class that is used for AJAX responses.
|
||||
* Currently only switches the default layout and sets the response type -
|
||||
* which just maps to text/html by default.
|
||||
*/
|
||||
class AjaxView extends AppView
|
||||
{
|
||||
/**
|
||||
* The name of the layout file to render the view inside of. The name
|
||||
* specified is the filename of the layout in /templates/Layout without
|
||||
* the .php extension.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected string $layout = 'ajax';
|
||||
|
||||
/**
|
||||
* Initialization hook method.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function initialize(): void
|
||||
{
|
||||
parent::initialize();
|
||||
|
||||
$this->response = $this->response->withType('ajax');
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
||||
* @link https://cakephp.org CakePHP(tm) Project
|
||||
* @since 0.10.0
|
||||
* @license https://opensource.org/licenses/mit-license.php MIT License
|
||||
* @var \App\View\AppView $this
|
||||
*/
|
||||
|
||||
echo $this->fetch('content');
|
||||
@@ -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('/');
|
||||
|
||||
Reference in new issue
Block a user