FormHelper adds a hidden _method=PUT for persisted entities, which the POST-only admin:settings route rejected with a MissingRouteException. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
198 lines
7.4 KiB
PHP
198 lines
7.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Test\TestCase\Controller;
|
|
|
|
use App\Model\Entity\Slide;
|
|
use App\Model\Table\PollsTable;
|
|
use App\Test\TestCase\PlenarioTestTrait;
|
|
use Cake\TestSuite\EmailTrait;
|
|
use Cake\TestSuite\IntegrationTestTrait;
|
|
use Cake\TestSuite\TestCase;
|
|
|
|
/**
|
|
* Admin area behind the secret token: dashboard, slides, presenter controls.
|
|
*/
|
|
class AdminControllerTest 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();
|
|
}
|
|
|
|
public function testInvalidTokenIs404(): void
|
|
{
|
|
$this->createPoll();
|
|
$this->get('/admin/' . str_repeat('x', 43));
|
|
$this->assertResponseCode(404);
|
|
}
|
|
|
|
public function testDashboardSendsPrivacyHeaders(): void
|
|
{
|
|
[$poll, $token] = $this->createPoll('Meine <Umfrage>');
|
|
$this->get("/admin/$token");
|
|
|
|
$this->assertResponseOk();
|
|
$this->assertResponseContains('Meine <Umfrage>');
|
|
$this->assertResponseContains($poll->formattedJoinCode());
|
|
$this->assertHeader('Referrer-Policy', 'no-referrer');
|
|
$this->assertHeaderContains('X-Robots-Tag', 'noindex');
|
|
}
|
|
|
|
public function testSettingsFormPostsAndSaves(): void
|
|
{
|
|
[$poll, $token] = $this->createPoll('Alt');
|
|
$this->get("/admin/$token");
|
|
$this->assertResponseNotContains('name="_method" value="PUT"', 'settings form must not override method');
|
|
|
|
$this->post("/admin/$token/settings", ['title' => 'Neu', 'is_active' => '1', 'show_results_to_participants' => '0']);
|
|
$this->assertRedirect(['_name' => 'admin', 'token' => $token]);
|
|
$this->assertSame('Neu', $this->pollsTable()->get($poll->id)->title);
|
|
}
|
|
|
|
public function testTokenOfOtherPollCannotAccessSlides(): void
|
|
{
|
|
[$pollA] = $this->createPoll('A');
|
|
[, $tokenB] = $this->createPoll('B');
|
|
$slide = $this->createSlide($pollA);
|
|
|
|
$this->get("/admin/$tokenB/slides/{$slide->id}/edit");
|
|
$this->assertResponseCode(404);
|
|
}
|
|
|
|
public function testAddChoiceSlide(): void
|
|
{
|
|
[$poll, $token] = $this->createPoll();
|
|
$this->post("/admin/$token/slides/add?type=choice", [
|
|
'type' => 'choice',
|
|
'question' => 'Pizza oder Pasta?',
|
|
'options_text' => "Pizza\n\nPasta\nPizza\n",
|
|
'settings' => ['multiple' => '1', 'evil' => 'x'],
|
|
]);
|
|
|
|
$this->assertRedirect(['_name' => 'admin', 'token' => $token]);
|
|
$slide = $this->slidesTable()->find()->contain(['SlideOptions'])->firstOrFail();
|
|
$this->assertSame($poll->id, $slide->poll_id);
|
|
$this->assertSame(['Pizza', 'Pasta'], array_map(fn($o) => $o->label, $slide->slide_options));
|
|
$this->assertSame(['multiple' => true], $slide->settings);
|
|
$this->assertSame(1, $slide->position);
|
|
}
|
|
|
|
public function testEditKeepsVotesOfUnchangedOptions(): void
|
|
{
|
|
[$poll, $token] = $this->createPoll();
|
|
$slide = $this->createSlide($poll, Slide::TYPE_CHOICE, [], ['A', 'B']);
|
|
$optionA = $slide->slide_options[0];
|
|
|
|
$this->post("/admin/$token/slides/{$slide->id}/edit", [
|
|
'question' => 'Neu',
|
|
'options_text' => "C\nA",
|
|
]);
|
|
|
|
$this->assertRedirect();
|
|
$slide = $this->slidesTable()->get($slide->id, contain: ['SlideOptions']);
|
|
$this->assertSame('Neu', $slide->question);
|
|
$this->assertSame(['C', 'A'], array_map(fn($o) => $o->label, $slide->slide_options));
|
|
$this->assertSame($optionA->id, $slide->slide_options[1]->id);
|
|
}
|
|
|
|
public function testMoveAndDeleteSlides(): void
|
|
{
|
|
[$poll, $token] = $this->createPoll();
|
|
$first = $this->createSlide($poll, Slide::TYPE_OPEN);
|
|
$second = $this->createSlide($poll, Slide::TYPE_SCALE);
|
|
|
|
$this->post("/admin/$token/slides/{$second->id}/move/up");
|
|
$this->assertSame(1, $this->slidesTable()->get($second->id)->position);
|
|
$this->assertSame(2, $this->slidesTable()->get($first->id)->position);
|
|
|
|
$this->post("/admin/$token/slides/{$second->id}/delete");
|
|
$this->assertFalse($this->slidesTable()->exists(['id' => $second->id]));
|
|
$this->assertSame(1, $this->slidesTable()->get($first->id)->position);
|
|
}
|
|
|
|
public function testPresentStartsWithFirstSlideAndControlNavigates(): void
|
|
{
|
|
[$poll, $token] = $this->createPoll();
|
|
$first = $this->createSlide($poll, Slide::TYPE_CHOICE);
|
|
$second = $this->createSlide($poll, Slide::TYPE_WORDCLOUD);
|
|
|
|
$this->get("/admin/$token/present");
|
|
$this->assertResponseOk();
|
|
$this->assertSame($first->id, $this->pollsTable()->get($poll->id)->current_slide_id);
|
|
|
|
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
|
|
$this->post("/admin/$token/control", ['action' => 'next']);
|
|
$this->assertResponseOk();
|
|
$state = json_decode((string)$this->_response->getBody(), true);
|
|
$this->assertSame($second->id, $state['slide']['id']);
|
|
$this->assertSame(1, $state['index']);
|
|
$this->assertArrayNotHasKey('email', $state['poll']);
|
|
|
|
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
|
|
$this->post("/admin/$token/control", ['action' => 'toggle']);
|
|
$this->assertFalse($this->slidesTable()->get($second->id)->is_voting_open);
|
|
|
|
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
|
|
$this->post("/admin/$token/control", ['action' => 'next']);
|
|
$this->assertSame($second->id, $this->pollsTable()->get($poll->id)->current_slide_id, 'stays on last slide');
|
|
}
|
|
|
|
public function testControlRejectsSlideOfOtherPoll(): void
|
|
{
|
|
[, $token] = $this->createPoll('A');
|
|
[$other] = $this->createPoll('B');
|
|
$foreign = $this->createSlide($other);
|
|
|
|
$this->post("/admin/$token/control", ['action' => 'goto', 'slide_id' => $foreign->id]);
|
|
$this->assertResponseCode(400);
|
|
}
|
|
|
|
public function testRotateTokenInvalidatesOldLink(): void
|
|
{
|
|
[$poll, $token] = $this->createPoll();
|
|
$this->post("/admin/$token/rotate-token");
|
|
|
|
$this->assertRedirect();
|
|
$this->assertMailSentTo('owner@example.org');
|
|
$fresh = $this->pollsTable()->get($poll->id);
|
|
$this->assertNotSame(PollsTable::hashToken($token), $fresh->admin_token_hash);
|
|
|
|
$this->get("/admin/$token");
|
|
$this->assertResponseCode(404);
|
|
}
|
|
|
|
public function testDeleteRequiresJoinCode(): void
|
|
{
|
|
[$poll, $token] = $this->createPoll();
|
|
$this->post("/admin/$token/delete", ['confirm_code' => 'WRONG']);
|
|
$this->assertTrue($this->pollsTable()->exists(['id' => $poll->id]));
|
|
|
|
$this->post("/admin/$token/delete", ['confirm_code' => strtolower($poll->join_code)]);
|
|
$this->assertRedirect(['_name' => 'home']);
|
|
$this->assertFalse($this->pollsTable()->exists(['id' => $poll->id]));
|
|
}
|
|
|
|
public function testQrAndExport(): void
|
|
{
|
|
[, $token] = $this->createPoll();
|
|
$this->get("/admin/$token/qr.svg");
|
|
$this->assertResponseOk();
|
|
$this->assertContentType('image/svg+xml');
|
|
|
|
$this->get("/admin/$token/export.csv");
|
|
$this->assertResponseOk();
|
|
$this->assertHeaderContains('Content-Disposition', 'attachment');
|
|
}
|
|
}
|