Files
gnilebeinandClaude Opus 5.5 121097f7a7
CI / Deploy / test (push) Successful in 52s
CI / Deploy / deploy (push) Skipped
Remove dead code and validate new polls only once
- Drop the unused polls.show_results_to_participants column (idempotent migration).
- Remove CaptchaInterface::getType(), the unused AjaxView with its layout,
  the empty cell template folder and a redundant setNew() call.
- PollsTable::newPoll() normalises and validates once; the create form used
  to validate the raw input first, so an e-mail address with surrounding
  spaces was rejected although it would have been trimmed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 22:12:35 +01:00

209 lines
7.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Test\TestCase\Controller;
use App\Model\Entity\Slide;
use App\Test\TestCase\PlenarioTestTrait;
use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\TestSuite\EmailTrait;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
/**
* Poll creation, captcha handling, join and recovery.
*/
class HomeControllerTest extends TestCase
{
use EmailTrait;
use IntegrationTestTrait;
use PlenarioTestTrait;
/**
* @var list<string>
*/
protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses'];
protected function setUp(): void
{
parent::setUp();
$this->enableCsrfToken();
Configure::write('Plenario.captcha.type', 'math');
Configure::write('Plenario.showAdminLinkAfterCreate', false);
Cache::clear('throttle');
}
public function testIndexRendersBothForms(): void
{
$this->get('/');
$this->assertResponseOk();
$this->assertResponseContains('Neue Umfrage erstellen');
$this->assertResponseContains('Sicherheitsfrage: Was ist');
$this->assertResponseContains('Teilnahme-Code');
}
public function testIndexListsAllSlideTypes(): void
{
$this->get('/');
foreach (Slide::typeLabels() as $type => $label) {
$this->assertResponseContains('type-card--' . $type, $type);
$this->assertResponseContains(h($label), $type);
}
}
public function testCreateWithValidMathCaptchaSendsAdminMail(): 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('owner@example.org', $poll->email);
$this->assertMatchesRegularExpression('/^[A-Z2-9]{6}$/', $poll->join_code);
$this->assertMailSentTo('owner@example.org');
$this->assertMailContains('/admin/');
$this->session(['Plenario.created' => $_SESSION['Plenario']['created'] ?? null]);
$this->get('/created');
$this->assertResponseContains('Fast geschafft');
$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('/');
$this->assertResponseContains('class="hp-field" aria-hidden="true"');
$this->assertResponseContains('name="website"');
$this->assertResponseContains('tabindex="-1"');
}
public function testCreateRejectsFilledHoneypot(): void
{
$this->session(['Captcha.math.create' => 7]);
$this->post('/create', [
'title' => 'Retro',
'email' => 'owner@example.org',
'captcha_answer' => '7',
'website' => 'https://spam.example',
]);
$this->assertResponseOk();
$this->assertResponseContains('Die Umfrage konnte nicht angelegt werden.');
$this->assertSame(0, $this->pollsTable()->find()->count());
$this->assertNoMailSent();
}
public function testCreateAcceptsEmptyHoneypot(): void
{
$this->session(['Captcha.math.create' => 7]);
$this->post('/create', [
'title' => 'Retro',
'email' => 'owner@example.org',
'captcha_answer' => '7',
'website' => '',
]);
$this->assertRedirect(['_name' => 'created']);
$this->assertSame(1, $this->pollsTable()->find()->count());
}
public function testCreateRejectsWrongCaptcha(): void
{
$this->session(['Captcha.math.create' => 7]);
$this->post('/create', ['title' => 'Retro', 'email' => 'owner@example.org', 'captcha_answer' => '8']);
$this->assertResponseOk();
$this->assertResponseContains('Sicherheitsabfrage war nicht korrekt');
$this->assertSame(0, $this->pollsTable()->find()->count());
$this->assertNoMailSent();
}
public function testCreateRejectsMissingCaptchaSession(): void
{
$this->post('/create', ['title' => 'Retro', 'email' => 'owner@example.org', 'captcha_answer' => '0']);
$this->assertSame(0, $this->pollsTable()->find()->count());
}
public function testCreateWithoutCaptchaAndInvalidEmail(): void
{
Configure::write('Plenario.captcha.type', 'none');
$this->post('/create', ['title' => 'Retro', 'email' => 'kein-mail']);
$this->assertResponseOk();
$this->assertResponseContains('gültige E-Mail-Adresse');
$this->assertSame(0, $this->pollsTable()->find()->count());
}
public function testCreateShowsAdminLinkInDevMode(): void
{
Configure::write('Plenario.captcha.type', 'none');
Configure::write('Plenario.showAdminLinkAfterCreate', true);
$this->post('/create', ['title' => 'Retro', 'email' => 'owner@example.org']);
$this->session(['Plenario.created' => $_SESSION['Plenario']['created'] ?? null]);
$this->get('/created');
$this->assertResponseContains('/admin/');
}
public function testCreateIsThrottled(): void
{
Configure::write('Plenario.captcha.type', 'none');
Configure::write('Plenario.throttle.createPerHour', 2);
for ($i = 0; $i < 3; $i++) {
$this->post('/create', ['title' => 'Retro ' . $i, 'email' => 'owner@example.org']);
}
$this->assertResponseContains('Zu viele Umfragen');
$this->assertSame(2, $this->pollsTable()->find()->count());
}
public function testJoinNormalisesCode(): void
{
[$poll] = $this->createPoll();
$code = strtolower(substr($poll->join_code, 0, 3) . ' - ' . substr($poll->join_code, 3));
$this->get('/join?code=' . urlencode($code));
$this->assertRedirect(['_name' => 'participate', 'code' => $poll->join_code]);
}
public function testJoinUnknownCode(): void
{
$this->get('/join?code=ZZZZZZ');
$this->assertResponseOk();
$this->assertResponseContains('keine Umfrage gefunden');
}
public function testRecoverResendsExistingLinks(): void
{
Configure::write('Plenario.captcha.type', 'none');
[, $token] = $this->createPoll('Eins', 'owner@example.org');
$this->createPoll('Fremd', 'other@example.org');
$this->post('/recover', ['email' => 'OWNER@example.org']);
$this->assertRedirect(['_name' => 'recover']);
$this->assertMailCount(1);
$this->assertMailSentTo('owner@example.org');
$this->assertMailContains('/admin/' . $token);
$this->assertMailContainsText('Eins');
}
public function testRecoverUnknownAddressSendsNothingButSameMessage(): void
{
Configure::write('Plenario.captcha.type', 'none');
$this->post('/recover', ['email' => 'nobody@example.org']);
$this->assertRedirect(['_name' => 'recover']);
$this->assertFlashMessage('Falls zu nobody@example.org Umfragen existieren, haben wir dir die Admin-Links per E-Mail geschickt.');
$this->assertNoMailSent();
}
}