diff --git a/AGENTS.md b/AGENTS.md index 574c571..ddfbd10 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -237,6 +237,9 @@ Kein WebSocket: Presenter (`/admin/{token}/state`) und Teilnehmende (`/p/{code}/ - Einrückung 4 Spaces, LF (siehe `.editorconfig`). - Keine neuen Frontend-Abhängigkeiten / kein Build-Schritt ohne Rücksprache. - Schemaänderungen nur per neuer Migration, nie bestehende Migration ändern (sobald deployed). +- Beim Löschen nie auf `ON DELETE CASCADE` verlassen: SQLite setzt Fremdschlüssel ohne `PRAGMA foreign_keys = ON` + nicht durch (die Test-Suite schaltet sie aber ein, Fehler fallen dort also nicht auf). Abhängige Daten über + `dependent` + `cascadeCallbacks` im ORM löschen (z. B. Polls → Slides → SlideOptions/Responses). - Commits klein und thematisch; Commit-Messages auf Englisch im Imperativ. - **`main` ist geschützt:** nie direkt auf `main` committen oder pushen. Jede Änderung auf einem eigenen Branch (z. B. `feature/…`, `fix/…`, `docs/…`), pushen und per Pull Request in Gitea mergen. diff --git a/config/Migrations/20261108120000_RemoveOrphanedRows.php b/config/Migrations/20261108120000_RemoveOrphanedRows.php new file mode 100644 index 0000000..348358b --- /dev/null +++ b/config/Migrations/20261108120000_RemoveOrphanedRows.php @@ -0,0 +1,33 @@ +execute('DELETE FROM responses WHERE slide_id NOT IN (SELECT id FROM slides)'); + $this->execute('DELETE FROM slide_options WHERE slide_id NOT IN (SELECT id FROM slides)'); + } + + /** + * Deleted rows cannot be restored. + * + * @return void + */ + public function down(): void + { + } +} diff --git a/src/Model/Table/PollsTable.php b/src/Model/Table/PollsTable.php index f0fcc87..d867b3d 100644 --- a/src/Model/Table/PollsTable.php +++ b/src/Model/Table/PollsTable.php @@ -48,7 +48,9 @@ class PollsTable extends Table 'foreignKey' => 'poll_id', 'sort' => ['Slides.position' => 'ASC', 'Slides.id' => 'ASC'], 'dependent' => true, - 'cascadeCallbacks' => false, + // Delete slide by slide so their options and responses go too. Do not rely on + // ON DELETE CASCADE: SQLite does not enforce foreign keys by default. + 'cascadeCallbacks' => true, ]); } diff --git a/tests/TestCase/Controller/AdminControllerTest.php b/tests/TestCase/Controller/AdminControllerTest.php index 5bb126f..3d8d108 100644 --- a/tests/TestCase/Controller/AdminControllerTest.php +++ b/tests/TestCase/Controller/AdminControllerTest.php @@ -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();