Delete options and responses together with their poll
CI / Deploy / test (push) Successful in 53s
CI / Deploy / deploy (push) Skipped

Deleting a poll removed its slides with a plain deleteAll and relied on
ON DELETE CASCADE for slide options and responses. SQLite does not enforce
foreign keys by default, so answers of deleted polls stayed in the database.
Cascade through the ORM instead and remove the rows left behind earlier.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Opus 5.5 committed 2026-11-08 21:56:18 +01:00
1 parent 8b0e82d806
commit 0a4c9fad09
4 files changed
+71 -1

No files matched your search

@@ -5,7 +5,9 @@ namespace App\Test\TestCase\Controller;
use App\Model\Entity\Slide;
use App\Model\Table\PollsTable;
use App\Service\VoteService;
use App\Test\TestCase\PlenarioTestTrait;
use Cake\Database\Driver\Sqlite;
use Cake\TestSuite\EmailTrait;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
@@ -284,6 +286,36 @@ class AdminControllerTest extends TestCase
$this->assertFalse($this->pollsTable()->exists(['id' => $poll->id]));
}
public function testDeletePollRemovesOptionsAndResponses(): void
{
[$poll, $token] = $this->createPoll();
$slide = $this->createSlide($poll, Slide::TYPE_CHOICE);
$poll = $this->goLive($poll, $slide);
(new VoteService())->submit($poll, $slide, 'participant', ['option_id' => $slide->slide_options[0]->id]);
$options = $this->fetchTable('SlideOptions');
$responses = $this->fetchTable('Responses');
$this->assertTrue($responses->exists(['slide_id' => $slide->id]));
// A normal SQLite connection does not enforce foreign keys (the test suite turns them on),
// so deleting must not rely on ON DELETE CASCADE.
$connection = $responses->getConnection();
$sqlite = $connection->getDriver() instanceof Sqlite;
if ($sqlite) {
$connection->execute('PRAGMA foreign_keys = OFF');
}
try {
$this->post("/admin/$token/delete", ['confirm_code' => $poll->join_code]);
} finally {
if ($sqlite) {
$connection->execute('PRAGMA foreign_keys = ON');
}
}
$this->assertFalse($this->slidesTable()->exists(['id' => $slide->id]));
$this->assertFalse($options->exists(['slide_id' => $slide->id]), 'Options must not outlive their poll.');
$this->assertFalse($responses->exists(['slide_id' => $slide->id]), 'Responses must not outlive their poll.');
}
public function testQrAndExport(): void
{
[, $token] = $this->createPoll();