Files
Plenario/tests/TestCase/Controller/EmbedTest.php
T
gnilebeinandClaude Opus 5.5 2a53675ce5
CI / Deploy / test (push) Successful in 1m1s
CI / Deploy / deploy (push) Skipped
Send a Content-Security-Policy and security headers on every response
- New SecurityHeadersMiddleware (outermost): CSP with a per-request script
  nonce, framing (SAMEORIGIN, embeds via frame-ancestors), nosniff,
  Referrer- and Permissions-Policy. JSON, files and error pages are covered
  now, not only rendered HTML; headers set by controllers still win.
- The inline theme script carries the nonce; flash messages are dismissed
  via data-dismiss instead of inline onclick handlers, and the flash
  elements share one template.
- reCAPTCHA sources are only allowed when it is enabled.
- The embed CSRF exception and framing use the same isEmbed() check.

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

272 lines
10 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Test\TestCase\Controller;
use App\Model\Entity\Poll;
use App\Model\Entity\Slide;
use App\Test\TestCase\PlenarioTestTrait;
use Cake\I18n\DateTime;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
/**
* Embedded slides (/e/{key}/{id}, e.g. in Canva) and pausing.
*/
class EmbedTest extends TestCase
{
use IntegrationTestTrait;
use PlenarioTestTrait;
/**
* @var list<string>
*/
protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses'];
/**
* @return array{0: \App\Model\Entity\Poll, 1: string, 2: string} poll, admin token, embed key
*/
protected function embeddedPoll(): array
{
[$poll, $token] = $this->createPoll();
$key = $this->pollsTable()->ensureEmbedKey($poll);
return [$poll, $token, $key];
}
/**
* POST without CSRF token, like the beacon / fetch from a third-party iframe.
*
* @return array<string, mixed>
*/
protected function embedPost(string $key, Slide $slide, string $action, array $data = []): array
{
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
$this->post("/e/$key/{$slide->id}/$action", $data);
$this->assertResponseOk();
return json_decode((string)$this->_response->getBody(), true);
}
protected function reload(Poll $poll): Poll
{
return $this->pollsTable()->get($poll->id);
}
public function testEnsureEmbedKeyIsIdempotent(): void
{
[$poll] = $this->createPoll();
$key = $this->pollsTable()->ensureEmbedKey($poll);
$this->assertSame($key, $this->pollsTable()->ensureEmbedKey($this->reload($poll)));
$this->assertNotSame($key, $this->pollsTable()->rotateEmbedKey($poll));
}
public function testUnknownKeyOrForeignSlideIs404(): void
{
[$poll, , $key] = $this->embeddedPoll();
[$other] = $this->createPoll('Andere');
$foreign = $this->createSlide($other);
$this->createSlide($poll);
$this->get('/e/' . str_repeat('x', 43) . '/' . $foreign->id);
$this->assertResponseCode(404);
$this->get("/e/$key/{$foreign->id}");
$this->assertResponseCode(404);
}
public function testViewMayBeFramedAndHidesAdminToken(): void
{
[$poll, $token, $key] = $this->embeddedPoll();
$slide = $this->createSlide($poll, Slide::TYPE_CHOICE, ['question' => 'Wie <geht> es?']);
$this->get("/e/$key/{$slide->id}");
$this->assertResponseOk();
$this->assertResponseContains('Wie &lt;geht&gt; es?');
$this->assertResponseContains('data-embed');
$this->assertResponseNotContains($token);
$this->assertHeaderContains('Content-Security-Policy', 'frame-ancestors *');
$this->assertNull($this->_response->getHeaderLine('X-Frame-Options') ?: null);
$this->assertHeader('Referrer-Policy', 'no-referrer');
$this->assertNull($this->reload($poll)->current_slide_id, 'viewing alone does not go live');
}
public function testActivateWorksWithoutCsrfAndIsIdempotent(): void
{
[$poll, , $key] = $this->embeddedPoll();
$slide = $this->createSlide($poll);
$state = $this->embedPost($key, $slide, 'activate');
$this->embedPost($key, $slide, 'activate');
$this->assertTrue($state['isLive']);
$this->assertSame($slide->id, $state['slide']['id']);
$this->assertArrayNotHasKey('slides', $state);
$poll = $this->reload($poll);
$this->assertSame($slide->id, $poll->current_slide_id);
$this->assertNotNull($poll->embed_seen_at);
}
public function testCsrfIsStillCheckedOutsideEmbedRoutes(): void
{
[$poll, $token] = $this->embeddedPoll();
$slide = $this->createSlide($poll);
$this->post("/admin/$token/control", ['action' => 'goto', 'slide_id' => $slide->id]);
$this->assertResponseCode(403);
$this->post('/p/' . $poll->join_code . '/vote', ['slide_id' => $slide->id]);
$this->assertResponseCode(403);
}
public function testActivateDoesNothingWhenPollIsClosed(): void
{
[$poll, , $key] = $this->embeddedPoll();
$slide = $this->createSlide($poll);
$this->pollsTable()->updateAll(['is_active' => false], ['id' => $poll->id]);
$state = $this->embedPost($key, $slide, 'activate');
$this->assertFalse($state['isLive']);
$this->assertNull($this->reload($poll)->current_slide_id);
}
public function testPauseOnlyWhenOwnSlideIsLive(): void
{
[$poll, , $key] = $this->embeddedPoll();
$first = $this->createSlide($poll);
$second = $this->createSlide($poll, Slide::TYPE_OPEN);
// Canva moves from embed 1 to embed 2: the late pause beacon of 1 must not stop 2.
$this->embedPost($key, $first, 'activate');
$this->embedPost($key, $second, 'activate');
$this->embedPost($key, $first, 'pause');
$this->assertSame($second->id, $this->reload($poll)->current_slide_id);
$this->embedPost($key, $second, 'pause');
$this->embedPost($key, $second, 'pause');
$poll = $this->reload($poll);
$this->assertNull($poll->current_slide_id);
$this->assertSame($second->id, $poll->paused_slide_id);
$this->assertTrue($poll->isPaused());
$this->get('/p/' . $poll->join_code);
$this->assertResponseContains('Kurze Pause');
$this->get('/p/' . $poll->join_code . '/state');
$this->assertTrue(json_decode((string)$this->_response->getBody(), true)['isPaused']);
}
public function testHeartbeatDoesNotTakeBackManuallyChosenSlide(): void
{
[$poll, $token, $key] = $this->embeddedPoll();
$embedded = $this->createSlide($poll);
$other = $this->createSlide($poll, Slide::TYPE_OPEN);
$this->embedPost($key, $embedded, 'activate');
$this->enableCsrfToken();
$this->post("/admin/$token/control", ['action' => 'goto', 'slide_id' => $other->id]);
$this->assertNull($this->reload($poll)->embed_seen_at, 'manual choice has no embed timeout');
$state = $this->embedPost($key, $embedded, 'heartbeat');
$this->assertFalse($state['isLive']);
$this->assertSame($other->id, $this->reload($poll)->current_slide_id);
}
public function testPollPausesWhenHeartbeatsStop(): void
{
[$poll, , $key] = $this->embeddedPoll();
$slide = $this->createSlide($poll);
$this->embedPost($key, $slide, 'activate');
$this->pollsTable()->updateAll(['embed_seen_at' => DateTime::now()->subMinutes(5)], ['id' => $poll->id]);
$this->get('/p/' . $poll->join_code . '/state');
$state = json_decode((string)$this->_response->getBody(), true);
$this->assertNull($state['slideId']);
$this->assertTrue($state['isPaused']);
$poll = $this->reload($poll);
$this->assertNull($poll->current_slide_id);
$this->assertSame($slide->id, $poll->paused_slide_id);
}
public function testFreshHeartbeatKeepsSlideLive(): void
{
[$poll, , $key] = $this->embeddedPoll();
$slide = $this->createSlide($poll);
$this->embedPost($key, $slide, 'activate');
$this->pollsTable()->updateAll(['embed_seen_at' => DateTime::now()->subMinutes(5)], ['id' => $poll->id]);
$this->post("/e/$key/{$slide->id}/heartbeat");
$this->get('/p/' . $poll->join_code . '/state');
$this->assertSame($slide->id, json_decode((string)$this->_response->getBody(), true)['slideId']);
}
public function testControlStartsQuizButRejectsNavigation(): void
{
[$poll, , $key] = $this->embeddedPoll();
$quiz = $this->createSlide($poll, Slide::TYPE_QUIZ, ['is_voting_open' => false], ['Richtig', 'Falsch']);
$this->embedPost($key, $quiz, 'activate');
$state = $this->embedPost($key, $quiz, 'control', ['action' => 'toggle']);
$this->assertSame('running', $state['results']['phase']);
$this->assertStringNotContainsString('correct', (string)$this->_response->getBody());
foreach (['next', 'goto', 'stop', 'delete'] as $action) {
$this->post("/e/$key/{$quiz->id}/control", ['action' => $action]);
$this->assertResponseCode(400, $action);
}
}
public function testQaStageCanBeSwitchedFromEmbed(): void
{
[$poll, , $key] = $this->embeddedPoll();
$qa = $this->createSlide($poll, Slide::TYPE_QA);
$state = $this->embedPost($key, $qa, 'control', ['action' => 'stage', 'stage' => Slide::QA_VOTE]);
$this->assertSame(Slide::QA_VOTE, $state['results']['stage']);
}
public function testAdminPauseAndResume(): void
{
[$poll, $token] = $this->createPoll();
$first = $this->createSlide($poll);
$second = $this->createSlide($poll, Slide::TYPE_OPEN);
$this->goLive($poll, $second);
$this->enableCsrfToken();
$this->post("/admin/$token/control", ['action' => 'stop']);
$this->assertTrue($this->reload($poll)->isPaused());
// Opening the presenter while paused keeps the pause.
$this->get("/admin/$token/present");
$this->assertTrue($this->reload($poll)->isPaused());
// → resumes the slide that was live, not the next one / the first one.
$this->post("/admin/$token/control", ['action' => 'next']);
$poll = $this->reload($poll);
$this->assertSame($second->id, $poll->current_slide_id);
$this->assertNull($poll->paused_slide_id);
$this->assertNotSame($first->id, $poll->current_slide_id);
}
public function testDashboardListsEmbedLinksAndRotationInvalidatesThem(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll);
$this->get("/admin/$token");
$key = (string)$this->reload($poll)->embed_key;
$this->assertNotSame('', $key);
$this->assertResponseContains("/e/$key/{$slide->id}");
$this->enableCsrfToken();
$this->post("/admin/$token/rotate-embed-key");
$this->assertRedirectContains("/admin/$token");
$this->get("/e/$key/{$slide->id}");
$this->assertResponseCode(404);
}
}