Files
Plenario/tests/TestCase/Controller/AdminFragmentTest.php
T
gnilebeinandClaude Sonnet 5.5 220227fa86
CI / Deploy / PHP 8.5 (push) Successful in 1m7s
CI / Deploy / PHP 8.4 (push) Successful in 1m7s
CI / Deploy / test (push) Successful in 1s
CI / Deploy / deploy (push) Skipped
Stop FormHelper from emitting inline validity handlers
Required fields got generated oninvalid/oninput attributes, which the CSP
blocks and logs on every page and when parsing dashboard fragments.

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

108 lines
3.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\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
/**
* Dashboard updates without reloading: fragments (header X-Fragment) and the slide form in a dialog.
*/
class AdminFragmentTest extends TestCase
{
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();
}
public function testFragmentContainsRegionsButNoPageFrame(): void
{
[$poll, $token] = $this->createPoll();
$this->createSlide($poll, Slide::TYPE_CHOICE);
$this->configRequest(['headers' => ['X-Fragment' => '1']]);
$this->get("/admin/$token");
$this->assertResponseOk();
$this->assertResponseContains('data-refresh="slides"');
$this->assertResponseContains('data-refresh="head"');
$this->assertResponseContains('data-refresh="flash"');
$this->assertResponseNotContains('<html');
$this->assertResponseNotContains('site-header');
}
public function testFullPageKeepsFrameAndRegions(): void
{
[$poll, $token] = $this->createPoll();
$this->createSlide($poll, Slide::TYPE_CHOICE);
$this->get("/admin/$token");
$this->assertResponseContains('<html');
$this->assertResponseContains('data-refresh="flash"');
$this->assertResponseContains('<dialog');
}
public function testActionsUseNoInlineHandlers(): void
{
[$poll, $token] = $this->createPoll();
$this->createSlide($poll, Slide::TYPE_CHOICE);
$this->get("/admin/$token");
$this->assertResponseNotContains('onclick=');
$this->assertResponseContains('data-confirm=');
// Required fields (settings form) must not get generated oninvalid/oninput handlers, the CSP blocks them.
$this->assertDoesNotMatchRegularExpression('/\son[a-z]+="/', (string)$this->_response->getBody());
}
public function testSlideFormFragmentIsTheDialogContent(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_OPEN);
$this->configRequest(['headers' => ['X-Fragment' => '1']]);
$this->get("/admin/$token/slides/{$slide->id}/edit");
$this->assertResponseOk();
$this->assertResponseContains('data-modal-content');
$this->assertResponseNotContains('site-header');
}
public function testValidationErrorKeepsTheFormInTheFragment(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_OPEN);
$this->configRequest(['headers' => ['X-Fragment' => '1']]);
$this->post("/admin/$token/slides/{$slide->id}/edit", ['question' => '']);
$this->assertResponseOk();
$this->assertResponseContains('data-modal-content');
$this->assertResponseContains('Bitte prüfe deine Eingaben.');
}
public function testSuccessfulSaveRedirectsToTheDashboard(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_OPEN);
$this->configRequest(['headers' => ['X-Fragment' => '1']]);
$this->post("/admin/$token/slides/{$slide->id}/edit", ['question' => 'Neue Frage']);
$this->assertRedirect(['_name' => 'admin', 'token' => $token]);
}
}