Files
Plenario/tests/TestCase/Controller/HomeControllerTest.php
T
gnilebeinandClaude Opus 5.5 eb319f8c1e
CI / Deploy / test (push) Successful in 49s
CI / Deploy / deploy (push) Failing after 5s
Add initial Plenario live polling app
CakePHP 5.4 app for live polls: create via email without login, manage
via secret admin link, join via code. Includes slide types (choice,
wordcloud, open, scale, content), presenter mode, CSV export, captcha,
throttling, tests and Gitea CI/deploy workflow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 13:24:29 +01:00

150 lines
5.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Test\TestCase\Controller;
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 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 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();
}
}