mirror of
https://github.com/MarijnDoeve/TijdVoorDeTest.git
synced 2026-07-10 01:20:14 +02:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5e7028c972 | |||
| 0ee15e3cbb |
@@ -4,7 +4,11 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
|
||||
|
||||
## Project Overview
|
||||
|
||||
**Tijd voor de test** is a PHP/Symfony 8.1 application for managing quizzes in the style of **Wie is de Mol?** (WIDM) — a Dutch TV show where contestants try to identify a saboteur ("de Mol") among them. At the end of each episode, participants take a quiz about the Mol's identity and actions; the candidate with the least correct answers is eliminated. This app replicates that quiz format with:
|
||||
**Tijd voor de test** is a PHP/Symfony 8.1 application for managing quizzes in the style of **Wie is de Mol?** (WIDM) —
|
||||
a Dutch TV show where contestants try to identify a saboteur ("de Mol") among them. At the end of each episode,
|
||||
participants take a quiz about the Mol's identity and actions; the candidate with the least correct answers is
|
||||
eliminated. This app replicates that quiz format with:
|
||||
|
||||
- Test creation with variable question counts
|
||||
- Season management with active test controls
|
||||
- Candidate answer tracking with automatic timing
|
||||
@@ -12,6 +16,7 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
|
||||
- Backoffice management for quiz administration and statistics
|
||||
|
||||
Tech Stack:
|
||||
|
||||
- **Framework**: Symfony 8.1
|
||||
- **PHP**: 8.5+
|
||||
- **Database**: PostgreSQL 16
|
||||
@@ -76,7 +81,7 @@ All code quality checks run in CI/CD (.github/workflows/ci.yml) and should pass
|
||||
|
||||
```
|
||||
src/
|
||||
Controller/ # HTTP request handlers (attribute-routed)
|
||||
Controller/ # HTTP request handlers (attribute-routed)
|
||||
Backoffice/ # Admin panel controllers
|
||||
Entity/ # Doctrine ORM entities
|
||||
Repository/ # Database queries
|
||||
@@ -122,59 +127,151 @@ tests/
|
||||
- **Elimination**: Records red/green screens and forced results with joker adjustments.
|
||||
- **User**: Administrative accounts for managing the system.
|
||||
|
||||
## Domain Context: "De Test" (Wie is de Mol)
|
||||
|
||||
**Wie is de Mol?** (WIDM) is a Dutch reality competition: a group of contestants ("kandidaten") travels together while
|
||||
one of them, "de Mol", secretly sabotages assignments. Each episode ends with the fixed line: *"Tijd voor de test.
|
||||
Twintig vragen over de identiteit en het doen en laten van de Mol. Degene die het minst weet, ligt uit het spel. Behalve
|
||||
de Mol. Die hoeft nooit naar huis."* ("Time for the test. Twenty questions about the identity and the actions of the
|
||||
Mol. Whoever knows the least is out of the game. Except the Mol — they never have to go home.") The contestant with the
|
||||
worst score is eliminated ("afvallen"); the Mol is immune regardless of score, since they already know the answers. This
|
||||
app is a generic engine for running that quiz format for private/fan seasons, not just modeling the TV show
|
||||
incidentally — the entity model below exists specifically to reproduce WIDM's test mechanics.
|
||||
|
||||
### What a test's 20 questions actually are
|
||||
|
||||
Per the intro line, questions fall into two factual categories — never opinion ("who would you vote off") — plus a
|
||||
third recurring format used on the show:
|
||||
|
||||
1. **Identity of the Mol**: guessing which contestant is the Mol.
|
||||
2. **The Mol's actions**: what the Mol did or where the Mol was during a specific assignment/moment.
|
||||
3. **Candidate self-answered questions**: earlier, every contestant privately answered a question about themselves
|
||||
(an interview-style question); the test then asks other contestants to guess what a *specific* candidate answered
|
||||
about themselves. This tests how well contestants know each other, not just Mol-tracking.
|
||||
|
||||
### Why answers can be bound to candidates
|
||||
|
||||
All three categories above can have contestants themselves as the answer options rather than free text: "who is the
|
||||
Mol" and "who did X" both need contestant names as options, and "what did candidate Y answer" needs Y's own submitted
|
||||
answer among the options. In the domain model this is `Answer::$candidates` (a `ManyToMany` to `Candidate`, on both
|
||||
sides): an answer option can *be* another contestant, not just text.
|
||||
|
||||
Because the relationship is many-to-many on the answer side too, a single answer option can cover **more than one
|
||||
candidate at once** — e.g. "Anna en Bram" as one option for "who missed the assignment together", or an option
|
||||
representing everyone who gave a particular self-answer in category 3 above. So a candidate-bound answer isn't always
|
||||
one candidate, it can be a group; treat `Answer::$candidates` as "the set of contestants this option represents", not
|
||||
as a single foreign key.
|
||||
|
||||
Combined with `GivenAnswer::$candidate` (who answered), every given answer on a candidate-bound question is a directed
|
||||
relationship from the answering candidate to *every* candidate covered by the chosen option — a one-to-many edge when
|
||||
the option is a group, not just candidate A pointed at candidate B. This is the mechanic behind any "who's suspected of
|
||||
what" or sociogram-style statistic — it only applies to candidate-bound questions, plain trivia questions have no such
|
||||
relationship. `Quiz::getQuestionErrors()` already relies on this distinction to validate that every active candidate is
|
||||
covered exactly once per candidate-bound question (a candidate appearing across multiple group-options on the same
|
||||
question counts as covered more than once).
|
||||
|
||||
### Elimination mechanics
|
||||
|
||||
- **Red/green screens**: at the end of a test, contestants are shown red or green screens one at a time to build tension
|
||||
before the elimination is revealed. `Elimination::$data` stores the colour shown per candidate (
|
||||
`SCREEN_RED/SCREEN_GREEN` via `getScreenColour()`), independent of the actual quiz score.
|
||||
- **Jokers / corrections**: contestants can hold a "joker" (an advantage, e.g. an extra correct answer) that adjusts
|
||||
their effective score without changing what they actually answered. This is `QuizCandidate::$corrections` — a float
|
||||
added to the raw score, kept separate from `GivenAnswer` so the audit trail of what was actually answered stays
|
||||
untouched.
|
||||
- **Dropouts**: `Quiz::$dropouts` controls how many contestants can be eliminated in a single test (normally 1, but some
|
||||
episodes eliminate more).
|
||||
- **Finalization/locking**: `Quiz::$isFinalized` and `$isLocked` gate when a quiz's questions/answers can still be
|
||||
edited — a quiz becomes immutable once a candidate has started it or an admin explicitly finalizes it. Treat this as
|
||||
the natural point where computed results (scores, statistics) can be cached indefinitely, since nothing that feeds
|
||||
them can change afterward.
|
||||
|
||||
### Terminology map (Dutch UI ↔ domain code)
|
||||
|
||||
| UI/domain term (Dutch) | Code |
|
||||
|------------------------------|-------------------------------|
|
||||
| Test | `Quiz` |
|
||||
| Vraag | `Question` |
|
||||
| Antwoord | `Answer` |
|
||||
| Kandidaat | `Candidate` |
|
||||
| Ingevuld antwoord | `GivenAnswer` |
|
||||
| Afvallen / rood-groen scherm | `Elimination` |
|
||||
| Joker / correctie | `QuizCandidate::$corrections` |
|
||||
|
||||
## Architecture Notes
|
||||
|
||||
### Routing
|
||||
|
||||
- Routes are **attribute-based** (PHP 8 attributes in controller methods)
|
||||
- Configured in `config/routes/attributes.yaml` for automatic discovery
|
||||
- Main entry point: `config/routes.yaml`
|
||||
|
||||
### Service Container & Dependency Injection
|
||||
|
||||
- Services in `src/` are automatically registered via PSR-4 namespace `Tvdt\`
|
||||
- Exclusions: Entity, DependencyInjection, Kernel classes
|
||||
- Autowiring and autoconfiguration enabled by default
|
||||
- Service definitions in `config/services.yaml`
|
||||
|
||||
### Database & Migrations
|
||||
|
||||
- PostgreSQL-based with Doctrine ORM
|
||||
- Migrations in `migrations/` at project root, namespace `DoctrineMigrations` (intentionally not autoloaded); generate with `bin/console make:migration`
|
||||
- Migrations in `migrations/` at project root, namespace `DoctrineMigrations` (intentionally not autoloaded); generate
|
||||
with `bin/console make:migration`
|
||||
- Test fixtures in `src/DataFixtures/` (loaded with `--group=test`)
|
||||
- Test database configured separately via `.env.test`
|
||||
|
||||
### Testing Infrastructure
|
||||
|
||||
- **PHPUnit 13** with DAMA Doctrine Test Bundle for transaction rollback
|
||||
- Bootstrap: `tests/bootstrap.php` loads env vars and autoloader; `tests/symfony-container.php` boots the test kernel/container (used by Rector)
|
||||
- Bootstrap: `tests/bootstrap.php` loads env vars and autoloader; `tests/symfony-container.php` boots the test
|
||||
kernel/container (used by Rector)
|
||||
- Symfony test utilities (BrowserKit, CSS selectors) available
|
||||
- Coverage excluded from: `src/DataFixtures/`
|
||||
- Test environment: `APP_ENV=test` (set in phpunit.dist.xml)
|
||||
|
||||
### Testing Conventions (TDD)
|
||||
- **Write the failing test first.** When fixing any PHP-reachable bug, write a PHPUnit test that reproduces the failure before touching the production code. Fix the code until the test passes.
|
||||
|
||||
- **Write the failing test first.** When fixing any PHP-reachable bug, write a PHPUnit test that reproduces the failure
|
||||
before touching the production code. Fix the code until the test passes.
|
||||
- Only skip a test if the bug is purely in JavaScript/frontend where PHPUnit cannot reach it.
|
||||
- Don't write tests for trivial presentational markup (e.g. asserting a tooltip/popover attribute or a CSS class exists in a template). Tests cover behavior: routing, forms, persistence, authorization.
|
||||
- Follow the pattern in `tests/Controller/Backoffice/` for controller/integration tests: log in, GET for CSRF token, POST form data, assert redirect, clear entity manager, assert DB state.
|
||||
- Don't write tests for trivial presentational markup (e.g. asserting a tooltip/popover attribute or a CSS class exists
|
||||
in a template). Tests cover behavior: routing, forms, persistence, authorization.
|
||||
- Follow the pattern in `tests/Controller/Backoffice/` for controller/integration tests: log in, GET for CSRF token,
|
||||
POST form data, assert redirect, clear entity manager, assert DB state.
|
||||
- **Prefer `TestCase` over `WebTestCase`/`KernelTestCase`.** Reach for the full kernel/DB boot only when the test
|
||||
genuinely needs routing, persistence, or the container — pure logic (services, listeners, helpers) should be tested
|
||||
with plain PHPUnit `TestCase` and mocked dependencies; it's faster and more isolated.
|
||||
- **Boy Scout Rule**: when you're already touching a file for an unrelated change, fix small nearby issues in the same
|
||||
commit (e.g. a test that unnecessarily extends `WebTestCase`, a stale comment) rather than leaving them for later —
|
||||
but don't let this balloon into an unrelated refactor.
|
||||
|
||||
### Code Style & Standards
|
||||
|
||||
- **PHP-CS-Fixer**: Symfony ruleset + risky rules enabled
|
||||
- Strict types declaration required
|
||||
- Trailing commas in multiline structures
|
||||
- No else-only blocks
|
||||
- **Rector**: Aggressive modernization with all attribute sets + prepared sets (dead code, code quality, Doctrine, Symfony, PHPUnit)
|
||||
- Strict types declaration required
|
||||
- Trailing commas in multiline structures
|
||||
- No else-only blocks
|
||||
- **Rector**: Aggressive modernization with all attribute sets + prepared sets (dead code, code quality, Doctrine,
|
||||
Symfony, PHPUnit)
|
||||
- **PHPStan**: Level 8 with extensions for Doctrine and Symfony
|
||||
- **Twig-CS-Fixer**: Template style enforcement
|
||||
- **Safe functions**: Use `thecodingmachine/safe` wrappers for standard PHP functions that return `false` on failure — they throw exceptions instead
|
||||
- **Safe functions**: Use `thecodingmachine/safe` wrappers for standard PHP functions that return `false` on failure —
|
||||
they throw exceptions instead
|
||||
|
||||
### Environment Configuration
|
||||
|
||||
- `.env` - Local development defaults (uncommitted in .env.local)
|
||||
- `.env.dev` - Development overrides
|
||||
- `.env.test` - Test environment configuration
|
||||
- Production uses `composer dump-env prod` for compiled configuration
|
||||
- Key variables:
|
||||
- `APP_ENV` - Environment (dev/test/prod)
|
||||
- `DATABASE_URL` - PostgreSQL connection string
|
||||
- `MAILER_SENDER` - From address for emails
|
||||
- `APP_ENV` - Environment (dev/test/prod)
|
||||
- `DATABASE_URL` - PostgreSQL connection string
|
||||
- `MAILER_SENDER` - From address for emails
|
||||
|
||||
### Frontend Build
|
||||
|
||||
- Asset mapper (no Node.js/Webpack) for JS/CSS bundling; JS modules declared in `importmap.php`
|
||||
- **Stimulus** controllers in `assets/controllers/`, **Turbo** for SPA-like navigation
|
||||
- Sass sources in `assets/styles/`, compiled via `bin/console sass:build`
|
||||
@@ -187,27 +284,28 @@ GitHub Actions workflow (`.github/workflows/ci.yml`):
|
||||
|
||||
1. **Linting**: Dockerfile (hadolint), Twig templates
|
||||
2. **Code Quality**:
|
||||
- PHP-CS-Fixer style check
|
||||
- Twig-CS-Fixer style check
|
||||
- PHPStan static analysis
|
||||
- Rector dry-run
|
||||
- PHP-CS-Fixer style check
|
||||
- Twig-CS-Fixer style check
|
||||
- PHPStan static analysis
|
||||
- Rector dry-run
|
||||
3. **Integration Tests**:
|
||||
- Docker image build and start services
|
||||
- Database creation and migration
|
||||
- Fixture loading
|
||||
- Full PHPUnit test suite with JUnit XML output
|
||||
- Doctrine schema validation
|
||||
- Docker image build and start services
|
||||
- Database creation and migration
|
||||
- Fixture loading
|
||||
- Full PHPUnit test suite with JUnit XML output
|
||||
- Doctrine schema validation
|
||||
4. **Build & Deploy** (on tags or main, disabled currently):
|
||||
- Docker image push to GitHub Container Registry
|
||||
- Sentry release creation
|
||||
- Portainer webhook trigger for production deployment
|
||||
- Docker image push to GitHub Container Registry
|
||||
- Sentry release creation
|
||||
- Portainer webhook trigger for production deployment
|
||||
|
||||
Runs on all pushes to main and pull requests. Concurrency cancels old runs on new commits.
|
||||
|
||||
## Important Files & Conventions
|
||||
|
||||
- **Kernel**: `src/Kernel.php` - Symfony kernel class
|
||||
- **AbstractController**: Base class for all controllers — defines route parameter regexes (`SEASON_CODE_REGEX`, `CANDIDATE_HASH_REGEX`) and flash helpers
|
||||
- **AbstractController**: Base class for all controllers — defines route parameter regexes (`SEASON_CODE_REGEX`,
|
||||
`CANDIDATE_HASH_REGEX`) and flash helpers
|
||||
- **Flash Messages**: Use `FlashType` enum instead of string literals
|
||||
- **QuizSpreadsheetService**: Handles importing quizzes from XLSX files
|
||||
- **Rector container**: `tests/symfony-container.php` — boots a test kernel so Rector can resolve Symfony service types
|
||||
@@ -226,6 +324,7 @@ Runs on all pushes to main and pull requests. Concurrency cancels old runs on ne
|
||||
## Composer Scripts
|
||||
|
||||
Auto-executed scripts on install/update:
|
||||
|
||||
- `cache:clear` - Symfony cache clear
|
||||
- `assets:install` - Copy public assets
|
||||
- `importmap:install` - JS import map setup
|
||||
@@ -244,4 +343,5 @@ When writing Dutch help content in `templates/backoffice/help/nl/`:
|
||||
- The backoffice elimination logic is in `Controller/Backoffice/PrepareEliminationController.php`
|
||||
- Quiz timing logic starts on candidate start click and stops on final answer selection
|
||||
- Background music feature noted but not yet implemented (requirements only)
|
||||
- Statistics functionality is marked TBD in README
|
||||
- Statistics module (per-quiz statistics page, candidate accusation matrix, caching) is planned per GitHub issue #199 —
|
||||
see "Domain Context" above for why candidate-bound answers matter to it
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import {Controller} from '@hotwired/stimulus';
|
||||
|
||||
const STORAGE_KEY = 'tvdt-fullscreen';
|
||||
|
||||
export default class extends Controller {
|
||||
connect() {
|
||||
this.onFullscreenChange = this.onFullscreenChange.bind(this);
|
||||
document.addEventListener('fullscreenchange', this.onFullscreenChange);
|
||||
this.syncState();
|
||||
|
||||
if (sessionStorage.getItem(STORAGE_KEY) === '1' && !document.fullscreenElement) {
|
||||
this.request();
|
||||
}
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
document.removeEventListener('fullscreenchange', this.onFullscreenChange);
|
||||
}
|
||||
|
||||
toggle() {
|
||||
if (document.fullscreenElement) {
|
||||
document.exitFullscreen();
|
||||
} else {
|
||||
this.request();
|
||||
}
|
||||
}
|
||||
|
||||
request() {
|
||||
document.documentElement.requestFullscreen().catch(() => {});
|
||||
}
|
||||
|
||||
onFullscreenChange() {
|
||||
sessionStorage.setItem(STORAGE_KEY, document.fullscreenElement ? '1' : '0');
|
||||
this.syncState();
|
||||
}
|
||||
|
||||
syncState() {
|
||||
document.documentElement.classList.toggle('is-fullscreen', Boolean(document.fullscreenElement));
|
||||
}
|
||||
}
|
||||
@@ -94,6 +94,32 @@ input.btn-check:checked + label.answer-btn {
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.fullscreen-btn {
|
||||
position: fixed;
|
||||
bottom: 0.75rem;
|
||||
left: 0.75rem;
|
||||
z-index: 1040;
|
||||
width: 2.25rem;
|
||||
height: 2.25rem;
|
||||
padding: 0;
|
||||
background: transparent;
|
||||
border: none;
|
||||
color: rgba(255, 255, 255, 0.35);
|
||||
font-size: 1.25rem;
|
||||
line-height: 1;
|
||||
cursor: pointer;
|
||||
|
||||
&:hover,
|
||||
&:focus {
|
||||
color: rgba(255, 255, 255, 0.8);
|
||||
outline: none;
|
||||
}
|
||||
}
|
||||
|
||||
html.is-fullscreen .fullscreen-btn {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.elimination-screen {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
"symfony/security-bundle": "8.1.*",
|
||||
"symfony/security-csrf": "8.1.*",
|
||||
"symfony/serializer": "8.1.*",
|
||||
"symfony/string": "8.1.*",
|
||||
"symfony/translation": "8.1.*",
|
||||
"symfony/twig-bundle": "8.1.*",
|
||||
"symfony/uid": "8.1.*",
|
||||
|
||||
Generated
+1
-1
@@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "010a4456ebc1a8ebaf73c6db051d3d09",
|
||||
"content-hash": "ccae654dd9c952e8920d9cb9c0f35ff5",
|
||||
"packages": [
|
||||
{
|
||||
"name": "composer/pcre",
|
||||
|
||||
@@ -14,10 +14,13 @@ use Symfony\Component\HttpKernel\Attribute\AsController;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Routing\Requirement\Requirement;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Tvdt\Controller\AbstractController;
|
||||
use Tvdt\Entity\Quiz;
|
||||
use Tvdt\Entity\Season;
|
||||
use Tvdt\Enum\FlashType;
|
||||
use Tvdt\Form\CreateSeasonFormType;
|
||||
use Tvdt\Helpers\FilenameSanitizer;
|
||||
use Tvdt\Repository\SeasonRepository;
|
||||
use Tvdt\Security\Voter\SeasonVoter;
|
||||
use Tvdt\Service\QuizSpreadsheetService;
|
||||
@@ -31,6 +34,7 @@ final class BackofficeController extends AbstractController
|
||||
private readonly Security $security,
|
||||
private readonly QuizSpreadsheetService $excel,
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly TranslatorInterface $translator,
|
||||
) {}
|
||||
|
||||
#[Route('/backoffice/', name: 'tvdt_backoffice_index')]
|
||||
@@ -83,11 +87,17 @@ final class BackofficeController extends AbstractController
|
||||
requirements: ['quiz' => Requirement::UUID],
|
||||
methods: ['GET'],
|
||||
)]
|
||||
public function exportQuiz(Quiz $quiz): StreamedResponse
|
||||
public function exportQuiz(Quiz $quiz): Response
|
||||
{
|
||||
if (!$this->authenticatedUser->isVerified) {
|
||||
$this->addFlash(FlashType::Warning, $this->translator->trans('Please confirm your email address before exporting a quiz.'));
|
||||
|
||||
return $this->redirectToRoute('tvdt_backoffice_season', ['seasonCode' => $quiz->season->seasonCode]);
|
||||
}
|
||||
|
||||
$response = new StreamedResponse($this->excel->quizToXlsx($quiz));
|
||||
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
||||
$response->headers->set('Content-Disposition', HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, $quiz->name.'.xlsx'));
|
||||
$response->headers->set('Content-Disposition', HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, FilenameSanitizer::sanitize($quiz->name).'.xlsx'));
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
@@ -6,9 +6,12 @@ namespace Tvdt\Controller\Backoffice;
|
||||
|
||||
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Safe\DateTimeImmutable;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\Form\FormError;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\HeaderUtils;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
@@ -21,8 +24,10 @@ use Tvdt\Entity\User;
|
||||
use Tvdt\Enum\FlashType;
|
||||
use Tvdt\Form\ChangeEmailFormType;
|
||||
use Tvdt\Form\ChangeUserPasswordFormType;
|
||||
use Tvdt\Helpers\FilenameSanitizer;
|
||||
use Tvdt\Repository\UserRepository;
|
||||
use Tvdt\Security\EmailVerifier;
|
||||
use Tvdt\Service\DataExportService;
|
||||
|
||||
final class SettingsController extends AbstractController
|
||||
{
|
||||
@@ -33,6 +38,7 @@ final class SettingsController extends AbstractController
|
||||
private readonly EmailVerifier $emailVerifier,
|
||||
private readonly Security $security,
|
||||
private readonly TranslatorInterface $translator,
|
||||
private readonly DataExportService $dataExportService,
|
||||
) {}
|
||||
|
||||
#[Route('/backoffice/settings', name: 'tvdt_backoffice_settings', methods: ['GET'])]
|
||||
@@ -148,6 +154,34 @@ final class SettingsController extends AbstractController
|
||||
return $this->redirectToRoute('tvdt_backoffice_settings');
|
||||
}
|
||||
|
||||
#[Route('/backoffice/settings/download-data', name: 'tvdt_backoffice_settings_download_data', methods: ['GET'])]
|
||||
public function downloadData(): Response
|
||||
{
|
||||
if (!$this->authenticatedUser->isVerified) {
|
||||
$this->addFlash(FlashType::Warning, $this->translator->trans('Please confirm your email address before downloading your data.'));
|
||||
|
||||
return $this->redirectToRoute('tvdt_backoffice_settings');
|
||||
}
|
||||
|
||||
$zipPath = $this->dataExportService->exportForUser($this->authenticatedUser);
|
||||
|
||||
$filename = \sprintf(
|
||||
'tijd-voor-de-test-data-%s-%s.zip',
|
||||
FilenameSanitizer::sanitize($this->authenticatedUser->email),
|
||||
new DateTimeImmutable()->format('Y-m-d_H-i-s'),
|
||||
);
|
||||
|
||||
$response = new BinaryFileResponse($zipPath);
|
||||
$response->deleteFileAfterSend(true);
|
||||
$response->headers->set('Content-Type', 'application/zip');
|
||||
$response->headers->set(
|
||||
'Content-Disposition',
|
||||
HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, $filename),
|
||||
);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
#[IsCsrfTokenValid('delete_account')]
|
||||
#[Route('/backoffice/settings/delete', name: 'tvdt_backoffice_settings_delete', methods: ['POST'])]
|
||||
public function deleteAccount(Request $request): Response
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tvdt\Helpers;
|
||||
|
||||
use Symfony\Component\String\Slugger\AsciiSlugger;
|
||||
|
||||
class FilenameSanitizer
|
||||
{
|
||||
/** Slugs user-supplied text (e.g. a season/quiz name) into a string safe to use as a zip entry path segment or a downloaded filename. */
|
||||
public static function sanitize(string $value): string
|
||||
{
|
||||
$slug = new AsciiSlugger()->slug($value)->toString();
|
||||
|
||||
return '' === $slug ? 'unnamed' : $slug;
|
||||
}
|
||||
}
|
||||
@@ -5,9 +5,15 @@ declare(strict_types=1);
|
||||
namespace Tvdt\Repository;
|
||||
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
|
||||
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
|
||||
use Tvdt\Entity\BankQuestion;
|
||||
use Tvdt\Entity\Elimination;
|
||||
use Tvdt\Entity\GivenAnswer;
|
||||
use Tvdt\Entity\QuizCandidate;
|
||||
use Tvdt\Entity\Season;
|
||||
use Tvdt\Entity\User;
|
||||
|
||||
/** @extends ServiceEntityRepository<User> */
|
||||
@@ -44,8 +50,11 @@ class UserRepository extends ServiceEntityRepository implements PasswordUpgrader
|
||||
$em->wrapInTransaction(function () use ($em, $user): void {
|
||||
$this->invalidateResetPasswordRequests($user);
|
||||
|
||||
$bankQuestionIds = [];
|
||||
foreach ($user->seasons->toArray() as $season) {
|
||||
if (1 === $season->owners->count()) {
|
||||
$this->purgeSoftDeletableData($em, $season);
|
||||
array_push($bankQuestionIds, ...$this->bankQuestionIds($season));
|
||||
$em->remove($season);
|
||||
|
||||
continue;
|
||||
@@ -56,9 +65,63 @@ class UserRepository extends ServiceEntityRepository implements PasswordUpgrader
|
||||
|
||||
$em->remove($user);
|
||||
$em->flush();
|
||||
|
||||
// Gedmo\Loggable writes its own "removed" log entry as part of the flush above, so the
|
||||
// audit-log purge must happen after — purging first would just leave that final row behind.
|
||||
$this->purgeBankQuestionAuditLog($em, $bankQuestionIds);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* QuizCandidate, GivenAnswer, and Elimination are Gedmo\SoftDeleteable, so cascading their
|
||||
* removal through the season/quiz/candidate relations only sets deletedAt — it never removes
|
||||
* the row. That leaves personal data behind indefinitely and, since Candidate/Answer are hard
|
||||
* deleted via orphanRemoval, it also breaks their foreign keys and rolls back the whole
|
||||
* deletion. Bulk DQL deletes bypass the Gedmo listener and physically remove these rows first.
|
||||
*/
|
||||
private function purgeSoftDeletableData(EntityManagerInterface $em, Season $season): void
|
||||
{
|
||||
foreach ([QuizCandidate::class, GivenAnswer::class, Elimination::class] as $class) {
|
||||
$em->createQuery(<<<DQL
|
||||
delete from {$class} e
|
||||
where e.quiz in (select q from Tvdt\Entity\Quiz q where q.season = :season)
|
||||
DQL)
|
||||
->setParameter('season', $season)
|
||||
->execute();
|
||||
}
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
private function bankQuestionIds(Season $season): array
|
||||
{
|
||||
return array_values(array_map(
|
||||
static fn (BankQuestion $bankQuestion): string => $bankQuestion->id->toString(),
|
||||
$season->bankQuestions->toArray(),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gedmo\Loggable audit rows (ext_log_entries) aren't foreign-keyed to the entity they log —
|
||||
* object_id is a plain string — so removing a BankQuestion never cleans up its history, and
|
||||
* the editor's username/email would otherwise remain in those rows forever.
|
||||
*
|
||||
* @param list<string> $bankQuestionIds
|
||||
*/
|
||||
private function purgeBankQuestionAuditLog(EntityManagerInterface $em, array $bankQuestionIds): void
|
||||
{
|
||||
if ([] === $bankQuestionIds) {
|
||||
return;
|
||||
}
|
||||
|
||||
$em->createQuery(<<<'DQL'
|
||||
delete from Tvdt\Entity\LogEntry l
|
||||
where l.objectClass = :class and l.objectId in (:ids)
|
||||
DQL)
|
||||
->setParameter('class', BankQuestion::class)
|
||||
->setParameter('ids', $bankQuestionIds)
|
||||
->execute();
|
||||
}
|
||||
|
||||
public function makeAdmin(string $email): void
|
||||
{
|
||||
$user = $this->findOneBy(['email' => $email]);
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tvdt\Security;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Security\Http\Event\LogoutEvent;
|
||||
|
||||
final readonly class LogoutRedirectListener implements EventSubscriberInterface
|
||||
{
|
||||
private const array BLOCKED_TARGET_PREFIXES = ['/backoffice', '/elimination'];
|
||||
|
||||
public function __construct(private UrlGeneratorInterface $urlGenerator) {}
|
||||
|
||||
public function onLogout(LogoutEvent $event): void
|
||||
{
|
||||
$target = $event->getRequest()->query->get('target');
|
||||
|
||||
if (\is_string($target) && $this->isAllowedTarget($target)) {
|
||||
$event->setResponse(new RedirectResponse($target));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$event->setResponse(new RedirectResponse($this->urlGenerator->generate('tvdt_quiz_select_season')));
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
// Must run before Symfony's DefaultLogoutListener (priority 64), which only
|
||||
// sets a response if none is set yet.
|
||||
return [
|
||||
LogoutEvent::class => ['onLogout', 128],
|
||||
];
|
||||
}
|
||||
|
||||
private function isAllowedTarget(string $target): bool
|
||||
{
|
||||
if (!str_starts_with($target, '/') || str_starts_with($target, '//')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !array_any(self::BLOCKED_TARGET_PREFIXES, static fn (string $prefix): bool => str_starts_with($target, $prefix));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,444 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tvdt\Service;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
use PhpOffice\PhpSpreadsheet\Writer;
|
||||
use Safe\Exceptions\FilesystemException;
|
||||
use Tvdt\Dto\Result;
|
||||
use Tvdt\Entity\BankQuestionUsage;
|
||||
use Tvdt\Entity\Candidate;
|
||||
use Tvdt\Entity\Question;
|
||||
use Tvdt\Entity\QuestionLabel;
|
||||
use Tvdt\Entity\Quiz;
|
||||
use Tvdt\Entity\Season;
|
||||
use Tvdt\Entity\User;
|
||||
use Tvdt\Helpers\FilenameSanitizer;
|
||||
use Tvdt\Repository\QuizRepository;
|
||||
|
||||
use function Safe\tempnam;
|
||||
use function Safe\unlink;
|
||||
|
||||
/** Builds a GDPR data-portability export (a zip of xlsx files) for everything owned by a single user. */
|
||||
class DataExportService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly QuizSpreadsheetService $quizSpreadsheetService,
|
||||
private readonly QuizRepository $quizRepository,
|
||||
) {}
|
||||
|
||||
/** @throws FilesystemException @return string path to a temp zip file; caller is responsible for removing it */
|
||||
public function exportForUser(User $user): string
|
||||
{
|
||||
$filter = $this->entityManager->getFilters();
|
||||
$filter->disable('softdeleteable');
|
||||
|
||||
try {
|
||||
return $this->buildZip($user);
|
||||
} finally {
|
||||
$filter->enable('softdeleteable');
|
||||
}
|
||||
}
|
||||
|
||||
private function buildZip(User $user): string
|
||||
{
|
||||
$zipPath = tempnam(sys_get_temp_dir(), 'tvdt_export_');
|
||||
$tempXlsxFiles = [];
|
||||
|
||||
$zip = new \ZipArchive();
|
||||
if (true !== $zip->open($zipPath, \ZipArchive::OVERWRITE)) {
|
||||
unlink($zipPath);
|
||||
|
||||
throw new \RuntimeException('Could not create the export zip archive.');
|
||||
}
|
||||
|
||||
try {
|
||||
$profilePath = $this->writeToTempFile($this->buildProfileWorkbook($user));
|
||||
$tempXlsxFiles[] = $profilePath;
|
||||
$zip->addFile($profilePath, 'profile.xlsx');
|
||||
|
||||
foreach ($user->seasons as $season) {
|
||||
$folder = FilenameSanitizer::sanitize($season->seasonCode.'-'.$season->name).'/';
|
||||
|
||||
foreach ($season->quizzes as $quiz) {
|
||||
$quizPath = $this->writeToTempFile($this->buildQuizWorkbook($quiz));
|
||||
$tempXlsxFiles[] = $quizPath;
|
||||
$zip->addFile($quizPath, $folder.FilenameSanitizer::sanitize($quiz->name).'.xlsx');
|
||||
}
|
||||
|
||||
$candidatesPath = $this->writeToTempFile($this->buildCandidatesWorkbook($season));
|
||||
$tempXlsxFiles[] = $candidatesPath;
|
||||
$zip->addFile($candidatesPath, $folder.'candidates.xlsx');
|
||||
|
||||
$questionBankPath = $this->writeToTempFile($this->buildQuestionBankWorkbook($season));
|
||||
$tempXlsxFiles[] = $questionBankPath;
|
||||
$zip->addFile($questionBankPath, $folder.'question-bank.xlsx');
|
||||
}
|
||||
|
||||
if (!$zip->close()) {
|
||||
throw new \RuntimeException('Could not finalize the export zip archive.');
|
||||
}
|
||||
} catch (\Throwable $throwable) {
|
||||
unlink($zipPath);
|
||||
|
||||
throw $throwable;
|
||||
} finally {
|
||||
foreach ($tempXlsxFiles as $tempXlsxFile) {
|
||||
unlink($tempXlsxFile);
|
||||
}
|
||||
}
|
||||
|
||||
return $zipPath;
|
||||
}
|
||||
|
||||
private function buildProfileWorkbook(User $user): Spreadsheet
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
|
||||
$account = $spreadsheet->getActiveSheet();
|
||||
$account->setTitle('Account');
|
||||
$account->getStyle('A:A')->getFont()->setBold(true);
|
||||
$account->fromArray([
|
||||
['Email', $user->email],
|
||||
['Roles', implode(', ', $user->getRoles())],
|
||||
['Email verified', $user->isVerified ? 'Yes' : 'No'],
|
||||
['Account ID', $user->id->toString()],
|
||||
], null, 'A1');
|
||||
$account->getColumnDimension('A')->setAutoSize(true);
|
||||
$account->getColumnDimension('B')->setAutoSize(true);
|
||||
|
||||
$seasons = $spreadsheet->createSheet();
|
||||
$seasons->setTitle('Seasons');
|
||||
$seasons->fromArray(['Season', 'Season code', 'Quizzes', 'Candidates', 'Shared with other owners'], null, 'A1');
|
||||
$seasons->getStyle('1:1')->getFont()->setBold(true);
|
||||
|
||||
$row = 2;
|
||||
foreach ($user->seasons as $season) {
|
||||
$seasons->fromArray([
|
||||
$season->name,
|
||||
$season->seasonCode,
|
||||
$season->quizzes->count(),
|
||||
$season->candidates->count(),
|
||||
$season->owners->count() > 1 ? 'Yes' : 'No',
|
||||
], null, 'A'.$row);
|
||||
++$row;
|
||||
}
|
||||
|
||||
foreach (['A', 'B', 'C', 'D', 'E'] as $column) {
|
||||
$seasons->getColumnDimension($column)->setAutoSize(true);
|
||||
}
|
||||
|
||||
$spreadsheet->setActiveSheetIndex(0);
|
||||
|
||||
return $spreadsheet;
|
||||
}
|
||||
|
||||
private function buildQuizWorkbook(Quiz $quiz): Spreadsheet
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
|
||||
$info = $spreadsheet->getActiveSheet();
|
||||
$info->setTitle('Quiz info');
|
||||
$this->fillQuizInfoSheet($info, $quiz);
|
||||
|
||||
$questions = $spreadsheet->createSheet();
|
||||
$questions->setTitle('Questions');
|
||||
|
||||
$this->quizSpreadsheetService->fillQuestionsSheet($questions, $quiz);
|
||||
|
||||
$rawAnswers = $spreadsheet->createSheet();
|
||||
$rawAnswers->setTitle('Raw answers');
|
||||
$this->fillRawAnswersSheet($rawAnswers, $quiz);
|
||||
|
||||
$results = $spreadsheet->createSheet();
|
||||
$results->setTitle('Results');
|
||||
$this->fillResultsSheet($results, $quiz);
|
||||
|
||||
$eliminations = $spreadsheet->createSheet();
|
||||
$eliminations->setTitle('Eliminations');
|
||||
$this->fillEliminationsSheet($eliminations, $quiz);
|
||||
|
||||
$spreadsheet->setActiveSheetIndex(0);
|
||||
|
||||
return $spreadsheet;
|
||||
}
|
||||
|
||||
private function fillQuizInfoSheet(Worksheet $sheet, Quiz $quiz): void
|
||||
{
|
||||
$disabledQuestions = $quiz->questions
|
||||
->filter(static fn (Question $question): bool => !$question->enabled)
|
||||
->map(static fn (Question $question): string => $question->question)
|
||||
->toArray();
|
||||
|
||||
$sheet->getStyle('A:A')->getFont()->setBold(true);
|
||||
$sheet->fromArray([
|
||||
['Quiz name', $quiz->name],
|
||||
['Number of dropouts', $quiz->dropouts],
|
||||
['Finalized', $quiz->isFinalized ? 'Yes' : 'No'],
|
||||
['Finalized at', $quiz->finalizedAt?->format(\DateTimeInterface::ATOM) ?? ''],
|
||||
['Disabled questions', implode(', ', $disabledQuestions)],
|
||||
], null, 'A1');
|
||||
$sheet->getColumnDimension('A')->setAutoSize(true);
|
||||
$sheet->getColumnDimension('B')->setAutoSize(true);
|
||||
}
|
||||
|
||||
private function fillResultsSheet(Worksheet $sheet, Quiz $quiz): void
|
||||
{
|
||||
$sheet->fromArray(['Candidate', 'Correct answers', 'Corrections', 'Penalty (s)', 'Score', 'Time', 'Started', 'Active', 'Deleted'], null, 'A1');
|
||||
$sheet->getStyle('1:1')->getFont()->setBold(true);
|
||||
|
||||
/** @var array<string, Result> $scoresByCandidateId */
|
||||
$scoresByCandidateId = [];
|
||||
foreach ($this->quizRepository->getScores($quiz) as $result) {
|
||||
$scoresByCandidateId[$result->id->toString()] = $result;
|
||||
}
|
||||
|
||||
$row = 2;
|
||||
foreach ($quiz->candidateData as $quizCandidate) {
|
||||
$candidate = $quizCandidate->candidate;
|
||||
$result = $scoresByCandidateId[$candidate->id->toString()] ?? null;
|
||||
|
||||
$sheet->fromArray([
|
||||
$candidate->name,
|
||||
$result?->correct,
|
||||
$result?->corrections,
|
||||
$result?->penaltySeconds,
|
||||
$result?->score,
|
||||
$result instanceof Result ? $result->time->format('%i:%S') : null,
|
||||
$quizCandidate->started?->format(\DateTimeInterface::ATOM),
|
||||
$quizCandidate->active ? 'Yes' : 'No',
|
||||
$quizCandidate->getDeletedAt()?->format(\DateTimeInterface::ATOM) ?? '',
|
||||
], null, 'A'.$row);
|
||||
++$row;
|
||||
}
|
||||
|
||||
foreach (range('A', 'I') as $column) {
|
||||
$sheet->getColumnDimension($column)->setAutoSize(true);
|
||||
}
|
||||
}
|
||||
|
||||
/** Raw crosstab: one row per candidate, one column per question, cell = the answer text they gave. */
|
||||
private function fillRawAnswersSheet(Worksheet $sheet, Quiz $quiz): void
|
||||
{
|
||||
/** @var list<Question> $questions */
|
||||
$questions = $quiz->questions->toArray();
|
||||
|
||||
$header = ['Candidate'];
|
||||
foreach ($questions as $question) {
|
||||
$header[] = $question->question;
|
||||
}
|
||||
|
||||
$sheet->fromArray($header, null, 'A1');
|
||||
$sheet->getStyle('1:1')->getFont()->setBold(true);
|
||||
$sheet->getStyle('1:1')->getAlignment()->setWrapText(true);
|
||||
|
||||
/** @var array<string, array<string, string>> $answersByCandidateAndQuestion */
|
||||
$answersByCandidateAndQuestion = [];
|
||||
foreach ($questions as $question) {
|
||||
foreach ($question->answers as $answer) {
|
||||
foreach ($answer->givenAnswers as $givenAnswer) {
|
||||
$candidateId = $givenAnswer->candidate->id->toString();
|
||||
$answersByCandidateAndQuestion[$candidateId][$question->id->toString()] = $answer->text;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$row = 2;
|
||||
foreach ($quiz->candidateData as $quizCandidate) {
|
||||
$candidate = $quizCandidate->candidate;
|
||||
|
||||
$line = [$candidate->name];
|
||||
foreach ($questions as $question) {
|
||||
$line[] = $answersByCandidateAndQuestion[$candidate->id->toString()][$question->id->toString()] ?? '';
|
||||
}
|
||||
|
||||
$sheet->fromArray($line, null, 'A'.$row);
|
||||
++$row;
|
||||
}
|
||||
|
||||
$lastColumnIndex = 1 + \count($questions);
|
||||
foreach (range('A', Coordinate::stringFromColumnIndex($lastColumnIndex)) as $column) {
|
||||
$sheet->getColumnDimension($column)->setWidth(30);
|
||||
$sheet->getStyle($column.':'.$column)->getAlignment()->setWrapText(true);
|
||||
}
|
||||
}
|
||||
|
||||
private function fillEliminationsSheet(Worksheet $sheet, Quiz $quiz): void
|
||||
{
|
||||
/** @var list<Candidate> $candidates */
|
||||
$candidates = $quiz->season->candidates->toArray();
|
||||
|
||||
$header = ['Prepared at', 'Deleted'];
|
||||
foreach ($candidates as $candidate) {
|
||||
$header[] = $candidate->name;
|
||||
}
|
||||
|
||||
$sheet->fromArray($header, null, 'A1');
|
||||
$sheet->getStyle('1:1')->getFont()->setBold(true);
|
||||
|
||||
$row = 2;
|
||||
foreach ($quiz->eliminations as $elimination) {
|
||||
$line = [
|
||||
$elimination->getCreatedAt()?->format(\DateTimeInterface::ATOM) ?? '',
|
||||
$elimination->getDeletedAt()?->format(\DateTimeInterface::ATOM) ?? '',
|
||||
];
|
||||
|
||||
foreach ($candidates as $candidate) {
|
||||
$line[] = $elimination->getScreenColour($candidate->name) ?? '';
|
||||
}
|
||||
|
||||
$sheet->fromArray($line, null, 'A'.$row);
|
||||
++$row;
|
||||
}
|
||||
|
||||
foreach (range('A', Coordinate::stringFromColumnIndex(2 + \count($candidates))) as $column) {
|
||||
$sheet->getColumnDimension($column)->setAutoSize(true);
|
||||
}
|
||||
}
|
||||
|
||||
private function buildCandidatesWorkbook(Season $season): Spreadsheet
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
|
||||
$candidatesSheet = $spreadsheet->getActiveSheet();
|
||||
$candidatesSheet->setTitle('Candidates');
|
||||
$candidatesSheet->fromArray(['Name'], null, 'A1');
|
||||
$candidatesSheet->getStyle('1:1')->getFont()->setBold(true);
|
||||
|
||||
$row = 2;
|
||||
foreach ($season->candidates as $candidate) {
|
||||
$candidatesSheet->fromArray([$candidate->name], null, 'A'.$row);
|
||||
++$row;
|
||||
}
|
||||
|
||||
$candidatesSheet->getColumnDimension('A')->setAutoSize(true);
|
||||
|
||||
$infoSheet = $spreadsheet->createSheet();
|
||||
$infoSheet->setTitle('Season info');
|
||||
$infoSheet->getStyle('A:A')->getFont()->setBold(true);
|
||||
$infoSheet->fromArray([
|
||||
['Season name', $season->name],
|
||||
['Season code', $season->seasonCode],
|
||||
['Number of quizzes', $season->quizzes->count()],
|
||||
['Number of candidates', $season->candidates->count()],
|
||||
['Active quiz', $season->activeQuiz instanceof Quiz ? $season->activeQuiz->name : ''],
|
||||
['Show numbers', $season->settings?->showNumbers ? 'Yes' : 'No'],
|
||||
['Confirm answers', $season->settings?->confirmAnswers ? 'Yes' : 'No'],
|
||||
['Shared with other owners', $season->owners->count() > 1 ? 'Yes' : 'No'],
|
||||
], null, 'A1');
|
||||
$infoSheet->getColumnDimension('A')->setAutoSize(true);
|
||||
$infoSheet->getColumnDimension('B')->setAutoSize(true);
|
||||
|
||||
$spreadsheet->setActiveSheetIndex(0);
|
||||
|
||||
return $spreadsheet;
|
||||
}
|
||||
|
||||
private function buildQuestionBankWorkbook(Season $season): Spreadsheet
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
|
||||
$questions = $spreadsheet->getActiveSheet();
|
||||
$questions->setTitle('Questions');
|
||||
$this->fillBankQuestionsSheet($questions, $season);
|
||||
|
||||
$labels = $spreadsheet->createSheet();
|
||||
$labels->setTitle('Labels');
|
||||
$this->fillQuestionLabelsSheet($labels, $season);
|
||||
|
||||
$spreadsheet->setActiveSheetIndex(0);
|
||||
|
||||
return $spreadsheet;
|
||||
}
|
||||
|
||||
private function fillBankQuestionsSheet(Worksheet $sheet, Season $season): void
|
||||
{
|
||||
$metaColumns = ['Question', 'Reusable', 'Complete for quiz', 'Labels', 'Used in quizzes'];
|
||||
$sheet->fromArray($metaColumns, null, 'A1');
|
||||
$sheet->getStyle('1:1')->getFont()->setBold(true);
|
||||
|
||||
$answerStartColumnIndex = \count($metaColumns);
|
||||
$maxAnswers = 0;
|
||||
$row = 2;
|
||||
|
||||
foreach ($season->bankQuestions as $bankQuestion) {
|
||||
$labels = implode(', ', array_map(
|
||||
static fn (QuestionLabel $label): string => $label->name,
|
||||
$bankQuestion->labels->toArray(),
|
||||
));
|
||||
$usedInQuizzes = implode(', ', array_map(
|
||||
static fn (BankQuestionUsage $usage): string => $usage->quiz->name,
|
||||
$bankQuestion->usages->toArray(),
|
||||
));
|
||||
|
||||
$sheet->fromArray([
|
||||
$bankQuestion->question,
|
||||
$bankQuestion->reusable ? 'Yes' : 'No',
|
||||
$bankQuestion->isCompleteForQuiz ? 'Yes' : 'No',
|
||||
$labels,
|
||||
$usedInQuizzes,
|
||||
], null, 'A'.$row);
|
||||
|
||||
$col = 0;
|
||||
foreach ($bankQuestion->answers as $answer) {
|
||||
$sheet->setCellValue(Coordinate::stringFromColumnIndex($answerStartColumnIndex + 1 + 2 * $col).$row, $answer->text);
|
||||
$sheet->setCellValue(Coordinate::stringFromColumnIndex($answerStartColumnIndex + 2 + 2 * $col).$row, $answer->isRightAnswer);
|
||||
++$col;
|
||||
}
|
||||
|
||||
$maxAnswers = max($maxAnswers, $col);
|
||||
++$row;
|
||||
}
|
||||
|
||||
for ($i = 0; $i < $maxAnswers; ++$i) {
|
||||
$answerCol = Coordinate::stringFromColumnIndex($answerStartColumnIndex + 1 + 2 * $i);
|
||||
$correctCol = Coordinate::stringFromColumnIndex($answerStartColumnIndex + 2 + 2 * $i);
|
||||
|
||||
$sheet->setCellValue($answerCol.'1', 'Answer '.($i + 1));
|
||||
$sheet->setCellValue($correctCol.'1', 'Correct');
|
||||
}
|
||||
|
||||
$lastColumnIndex = $answerStartColumnIndex + max(1, 2 * $maxAnswers);
|
||||
foreach (range('A', Coordinate::stringFromColumnIndex($lastColumnIndex)) as $column) {
|
||||
$sheet->getColumnDimension($column)->setAutoSize(true);
|
||||
}
|
||||
}
|
||||
|
||||
private function fillQuestionLabelsSheet(Worksheet $sheet, Season $season): void
|
||||
{
|
||||
$sheet->fromArray(['Name', 'Colour', 'Slug'], null, 'A1');
|
||||
$sheet->getStyle('1:1')->getFont()->setBold(true);
|
||||
|
||||
$row = 2;
|
||||
foreach ($season->questionLabels as $label) {
|
||||
$sheet->fromArray([$label->name, $label->colour->name, $label->slug], null, 'A'.$row);
|
||||
++$row;
|
||||
}
|
||||
|
||||
foreach (['A', 'B', 'C'] as $column) {
|
||||
$sheet->getColumnDimension($column)->setAutoSize(true);
|
||||
}
|
||||
}
|
||||
|
||||
/** @throws FilesystemException */
|
||||
private function writeToTempFile(Spreadsheet $spreadsheet): string
|
||||
{
|
||||
$path = tempnam(sys_get_temp_dir(), 'tvdt_export_sheet_');
|
||||
|
||||
try {
|
||||
new Writer\Xlsx($spreadsheet)->save($path);
|
||||
} catch (\Throwable $throwable) {
|
||||
unlink($path);
|
||||
|
||||
throw $throwable;
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ namespace Tvdt\Service;
|
||||
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
||||
use PhpOffice\PhpSpreadsheet\Reader;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
use PhpOffice\PhpSpreadsheet\Writer;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
use Tvdt\Entity\Answer;
|
||||
@@ -117,8 +118,13 @@ class QuizSpreadsheetService
|
||||
public function quizToXlsx(Quiz $quiz): \Closure
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
$this->fillQuestionsSheet($spreadsheet->getActiveSheet(), $quiz);
|
||||
|
||||
return $this->toXlsx($spreadsheet);
|
||||
}
|
||||
|
||||
public function fillQuestionsSheet(Worksheet $sheet, Quiz $quiz): void
|
||||
{
|
||||
// Write data rows first so we know the maximum answer count.
|
||||
$maxAnswers = 0;
|
||||
$row = 2;
|
||||
@@ -153,11 +159,9 @@ class QuizSpreadsheetService
|
||||
$sheet->setCellValue($correctCol.'1', 'Correct');
|
||||
$sheet->getColumnDimension($correctCol)->setAutoSize(true);
|
||||
}
|
||||
|
||||
return $this->toXlsx($spreadsheet);
|
||||
}
|
||||
|
||||
private function toXlsx(Spreadsheet $spreadsheet): \Closure
|
||||
public function toXlsx(Spreadsheet $spreadsheet): \Closure
|
||||
{
|
||||
$writer = new Writer\Xlsx($spreadsheet);
|
||||
|
||||
|
||||
@@ -55,14 +55,13 @@
|
||||
{{ form(emailForm, {action: path('tvdt_backoffice_settings_email')}) }}
|
||||
</section>
|
||||
|
||||
<section class="mb-5" data-controller="bo--popover">
|
||||
<section class="mb-5">
|
||||
<h4>{{ 'Your data'|trans }}</h4>
|
||||
<span class="d-inline-block" tabindex="0"
|
||||
data-bs-toggle="popover"
|
||||
data-bs-trigger="hover focus"
|
||||
data-bs-content="{{ 'Soon™'|trans }}">
|
||||
<button type="button" class="btn btn-secondary pe-none" disabled>{{ 'Download data'|trans }}</button>
|
||||
</span>
|
||||
<p>{{ 'Download an archive of everything stored under your account: your profile, the seasons you own, their quizzes, results and candidates.'|trans }}</p>
|
||||
{% if not app.user.isVerified %}
|
||||
<p class="text-warning">{{ 'Confirm your email address to enable this feature.'|trans }}</p>
|
||||
{% endif %}
|
||||
<a class="btn btn-primary" href="{{ path('tvdt_backoffice_settings_download_data') }}">{{ 'Download data'|trans }}</a>
|
||||
</section>
|
||||
|
||||
<section class="mb-5">
|
||||
|
||||
@@ -1,3 +1,13 @@
|
||||
{% extends 'base.html.twig' %}
|
||||
{% block importmap %}{{ importmap('quiz') }}{% endblock %}
|
||||
{% block nav %}{{ include('quiz/nav.html.twig') }}{% endblock %}
|
||||
{% block main %}
|
||||
<div data-controller="fullscreen">
|
||||
<button type="button"
|
||||
class="fullscreen-btn"
|
||||
data-action="fullscreen#toggle"
|
||||
aria-label="{{ 'Fullscreen'|trans }}"
|
||||
title="{{ 'Fullscreen'|trans }}">⛶</button>
|
||||
{{ parent() }}
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<a href="{{ path('tvdt_backoffice_index') }}" class="btn btn-outline-secondary btn-sm">
|
||||
{{ 'Backoffice'|trans }}
|
||||
</a>
|
||||
<a href="{{ path('tvdt_login_logout') }}" class="btn btn-outline-secondary btn-sm">
|
||||
<a href="{{ path('tvdt_login_logout', {target: app.request.pathInfo}) }}" class="btn btn-outline-secondary btn-sm">
|
||||
{{ 'Logout'|trans }}
|
||||
</a>
|
||||
{% else %}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tvdt\Tests\Controller\Backoffice;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Tvdt\Controller\Backoffice\BackofficeController;
|
||||
use Tvdt\Entity\Quiz;
|
||||
use Tvdt\Entity\User;
|
||||
|
||||
#[CoversClass(BackofficeController::class)]
|
||||
final class BackofficeControllerTest extends WebTestCase
|
||||
{
|
||||
public function testExportQuizFilenameIsSanitized(): void
|
||||
{
|
||||
$client = self::createClient();
|
||||
$entityManager = self::getContainer()->get(EntityManagerInterface::class);
|
||||
|
||||
$user = $entityManager->getRepository(User::class)->findOneBy(['email' => 'user2@example.org']);
|
||||
$this->assertInstanceOf(User::class, $user);
|
||||
$user->isVerified = true;
|
||||
$entityManager->flush();
|
||||
$client->loginUser($user);
|
||||
|
||||
$quiz = $entityManager->getRepository(Quiz::class)->findOneBy(['name' => 'Quiz 1']);
|
||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||
|
||||
$client->request(Request::METHOD_GET, \sprintf('/backoffice/quiz/%s/export', $quiz->id));
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
$disposition = (string) $client->getResponse()->headers->get('Content-Disposition');
|
||||
$this->assertStringContainsString('filename=Quiz-1.xlsx', $disposition);
|
||||
$this->assertStringNotContainsString('Quiz 1.xlsx', $disposition);
|
||||
}
|
||||
|
||||
public function testExportQuizRequiresVerifiedEmail(): void
|
||||
{
|
||||
$client = self::createClient();
|
||||
$entityManager = self::getContainer()->get(EntityManagerInterface::class);
|
||||
|
||||
$user = $entityManager->getRepository(User::class)->findOneBy(['email' => 'user2@example.org']);
|
||||
$this->assertInstanceOf(User::class, $user);
|
||||
$this->assertFalse($user->isVerified);
|
||||
$client->loginUser($user);
|
||||
|
||||
$quiz = $entityManager->getRepository(Quiz::class)->findOneBy(['name' => 'Quiz 1']);
|
||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||
|
||||
$client->request(Request::METHOD_GET, \sprintf('/backoffice/quiz/%s/export', $quiz->id));
|
||||
|
||||
self::assertResponseRedirects(\sprintf('/backoffice/season/%s', $quiz->season->seasonCode));
|
||||
}
|
||||
}
|
||||
@@ -350,4 +350,47 @@ final class SettingsControllerTest extends WebTestCase
|
||||
$this->assertNotEmpty($ownerEmails);
|
||||
}
|
||||
}
|
||||
|
||||
public function testDownloadDataRequiresAuthentication(): void
|
||||
{
|
||||
$this->client->restart();
|
||||
$this->client->request(Request::METHOD_GET, '/backoffice/settings/download-data');
|
||||
|
||||
self::assertResponseRedirects();
|
||||
}
|
||||
|
||||
public function testDownloadDataReturnsAZipWithATimestampedAccountFilename(): void
|
||||
{
|
||||
$this->markUserVerified('test@example.org');
|
||||
|
||||
$this->client->request(Request::METHOD_GET, '/backoffice/settings/download-data');
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
self::assertResponseHeaderSame('Content-Type', 'application/zip');
|
||||
|
||||
$disposition = (string) $this->client->getResponse()->headers->get('Content-Disposition');
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/filename=tijd-voor-de-test-data-test-example-org-\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}\.zip/',
|
||||
$disposition,
|
||||
);
|
||||
}
|
||||
|
||||
public function testDownloadDataRequiresVerifiedEmail(): void
|
||||
{
|
||||
$user = $this->getUserByEmail('test@example.org');
|
||||
$this->assertInstanceOf(User::class, $user);
|
||||
$this->assertFalse($user->isVerified);
|
||||
|
||||
$this->client->request(Request::METHOD_GET, '/backoffice/settings/download-data');
|
||||
|
||||
self::assertResponseRedirects('/backoffice/settings');
|
||||
}
|
||||
|
||||
private function markUserVerified(string $email): void
|
||||
{
|
||||
$user = $this->getUserByEmail($email);
|
||||
$this->assertInstanceOf(User::class, $user);
|
||||
$user->isVerified = true;
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tvdt\Tests\Helpers;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Tvdt\Helpers\FilenameSanitizer;
|
||||
|
||||
final class FilenameSanitizerTest extends TestCase
|
||||
{
|
||||
public function testReplacesSpacesWithDashes(): void
|
||||
{
|
||||
$this->assertSame('Krtek-Weekend', FilenameSanitizer::sanitize('Krtek Weekend'));
|
||||
}
|
||||
|
||||
public function testStripsPathSeparatorsAndTraversal(): void
|
||||
{
|
||||
$this->assertSame('etc-passwd', FilenameSanitizer::sanitize('../../etc/passwd'));
|
||||
$this->assertSame('a-b', FilenameSanitizer::sanitize('a/b'));
|
||||
$this->assertSame('a-b', FilenameSanitizer::sanitize('a\\b'));
|
||||
}
|
||||
|
||||
public function testStripsControlCharactersAndSpecialSymbols(): void
|
||||
{
|
||||
$this->assertSame('Quiz-1-script', FilenameSanitizer::sanitize("Quiz #1 <script>\0"));
|
||||
}
|
||||
|
||||
public function testTransliteratesUnicodeToAscii(): void
|
||||
{
|
||||
$this->assertSame('Weird-Name', FilenameSanitizer::sanitize('Wéird Ñame'));
|
||||
}
|
||||
|
||||
public function testTransliteratesAtSignInEmail(): void
|
||||
{
|
||||
$this->assertSame('test-example-org', FilenameSanitizer::sanitize('test@example.org'));
|
||||
}
|
||||
|
||||
public function testReturnsUnnamedForEmptyOrFullyStrippedInput(): void
|
||||
{
|
||||
$this->assertSame('unnamed', FilenameSanitizer::sanitize(''));
|
||||
$this->assertSame('unnamed', FilenameSanitizer::sanitize('///'));
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,12 @@ namespace Tvdt\Tests\Repository;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
use Tvdt\DataFixtures\TestFixtures;
|
||||
use Tvdt\Entity\BankQuestion;
|
||||
use Tvdt\Entity\Elimination;
|
||||
use Tvdt\Entity\GivenAnswer;
|
||||
use Tvdt\Entity\Question;
|
||||
use Tvdt\Entity\Quiz;
|
||||
use Tvdt\Entity\QuizCandidate;
|
||||
use Tvdt\Repository\UserRepository;
|
||||
|
||||
use function PHPUnit\Framework\assertEmpty;
|
||||
@@ -42,4 +48,89 @@ final class UserRepositoryTest extends DatabaseTestCase
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->userRepository->makeAdmin('invalid@example.org');
|
||||
}
|
||||
|
||||
/**
|
||||
* GDPR right-to-erasure: deleting the sole owner of a season must physically remove every
|
||||
* row tied to it, not merely soft-delete it. QuizCandidate, GivenAnswer, and Elimination are
|
||||
* all Gedmo\SoftDeleteable, so a naive $em->remove($season) cascade leaves them (or the
|
||||
* transaction itself) behind. Assertions bypass the softdeleteable filter and read raw SQL,
|
||||
* since a soft-deleted row would otherwise still be invisible to a filtered ORM query.
|
||||
*/
|
||||
public function testDeleteUserHardDeletesQuizCandidateGivenAnswerAndElimination(): void
|
||||
{
|
||||
$user = $this->getUserByEmail('sole-owner@example.org');
|
||||
$season = $this->getSeasonByCode('doomd');
|
||||
|
||||
$quiz = $this->entityManager->getRepository(Quiz::class)->findOneBy(['name' => 'Doomed Quiz', 'season' => $season]);
|
||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||
$candidate = $this->getCandidateBySeasonAndName($season, 'Vera');
|
||||
|
||||
/** @var Question $question */
|
||||
$question = $quiz->questions->first();
|
||||
$rightAnswer = $question->answers->first();
|
||||
$this->assertNotFalse($rightAnswer);
|
||||
|
||||
$this->quizCandidateRepository->createIfNotExist($quiz, $candidate);
|
||||
$quizCandidate = $this->quizCandidateRepository->findOneBy(['quiz' => $quiz, 'candidate' => $candidate]);
|
||||
$this->assertInstanceOf(QuizCandidate::class, $quizCandidate);
|
||||
|
||||
$givenAnswer = new GivenAnswer($candidate, $quiz, $rightAnswer);
|
||||
$this->entityManager->persist($givenAnswer);
|
||||
|
||||
$elimination = new Elimination($quiz);
|
||||
$elimination->data = ['Vera' => Elimination::SCREEN_GREEN];
|
||||
|
||||
$this->entityManager->persist($elimination);
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
$quizCandidateId = $quizCandidate->id->toString();
|
||||
$givenAnswerId = $givenAnswer->id->toString();
|
||||
$eliminationId = $elimination->id->toString();
|
||||
|
||||
$this->userRepository->deleteUser($user);
|
||||
$this->entityManager->clear();
|
||||
|
||||
$connection = $this->entityManager->getConnection();
|
||||
$this->assertSame(0, (int) $connection->fetchOne('select count(*) from quiz_candidate where id = ?', [$quizCandidateId]));
|
||||
$this->assertSame(0, (int) $connection->fetchOne('select count(*) from given_answer where id = ?', [$givenAnswerId]));
|
||||
$this->assertSame(0, (int) $connection->fetchOne('select count(*) from elimination where id = ?', [$eliminationId]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gedmo\Loggable writes an audit row (including the editor's username/email) to
|
||||
* ext_log_entries for every change to a Versioned field. Those rows aren't linked via a
|
||||
* foreign key (object_id is a plain string), so deleting the season/BankQuestion never
|
||||
* cleans them up on its own — the deleted account's email would otherwise live on forever.
|
||||
*/
|
||||
public function testDeleteUserPurgesBankQuestionAuditLogEntries(): void
|
||||
{
|
||||
$user = $this->getUserByEmail('sole-owner@example.org');
|
||||
$season = $this->getSeasonByCode('doomd');
|
||||
|
||||
$bankQuestion = new BankQuestion();
|
||||
$bankQuestion->question = 'Wie is de Krtek eigenlijk?';
|
||||
$bankQuestion->season = $season;
|
||||
|
||||
$this->entityManager->persist($bankQuestion);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$bankQuestionId = $bankQuestion->id->toString();
|
||||
$connection = $this->entityManager->getConnection();
|
||||
|
||||
$logCountBefore = (int) $connection->fetchOne(
|
||||
'select count(*) from ext_log_entries where object_class = ? and object_id = ?',
|
||||
[BankQuestion::class, $bankQuestionId],
|
||||
);
|
||||
$this->assertGreaterThan(0, $logCountBefore);
|
||||
|
||||
$this->userRepository->deleteUser($user);
|
||||
$this->entityManager->clear();
|
||||
|
||||
$logCountAfter = (int) $connection->fetchOne(
|
||||
'select count(*) from ext_log_entries where object_class = ? and object_id = ?',
|
||||
[BankQuestion::class, $bankQuestionId],
|
||||
);
|
||||
$this->assertSame(0, $logCountAfter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tvdt\Tests\Security;
|
||||
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Security\Http\Event\LogoutEvent;
|
||||
use Tvdt\Security\LogoutRedirectListener;
|
||||
|
||||
#[CoversClass(LogoutRedirectListener::class)]
|
||||
final class LogoutRedirectListenerTest extends TestCase
|
||||
{
|
||||
public function testLogoutRedirectsBackToTheGivenTarget(): void
|
||||
{
|
||||
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
|
||||
$urlGenerator->expects($this->never())->method('generate');
|
||||
$listener = new LogoutRedirectListener($urlGenerator);
|
||||
|
||||
$event = new LogoutEvent(Request::create('/logout?target=/krtek'), null);
|
||||
$listener->onLogout($event);
|
||||
|
||||
$this->assertSame('/krtek', $event->getResponse()?->headers->get('Location'));
|
||||
}
|
||||
|
||||
public function testLogoutWithoutTargetRedirectsToSeasonSelect(): void
|
||||
{
|
||||
$listener = new LogoutRedirectListener($this->seasonSelectUrlGenerator());
|
||||
|
||||
$event = new LogoutEvent(Request::create('/logout'), null);
|
||||
$listener->onLogout($event);
|
||||
|
||||
$this->assertSame('/', $event->getResponse()?->headers->get('Location'));
|
||||
}
|
||||
|
||||
#[DataProvider('blockedTargetProvider')]
|
||||
public function testLogoutIgnoresBlockedOrUnsafeTargets(string $target): void
|
||||
{
|
||||
$listener = new LogoutRedirectListener($this->seasonSelectUrlGenerator());
|
||||
|
||||
$event = new LogoutEvent(Request::create('/logout?target='.urlencode($target)), null);
|
||||
$listener->onLogout($event);
|
||||
|
||||
$this->assertSame('/', $event->getResponse()?->headers->get('Location'));
|
||||
}
|
||||
|
||||
/** @return iterable<string, array{string}> */
|
||||
public static function blockedTargetProvider(): iterable
|
||||
{
|
||||
yield 'backoffice page' => ['/backoffice/season/krtek'];
|
||||
yield 'elimination page' => ['/elimination/00000000-0000-0000-0000-000000000000'];
|
||||
yield 'protocol-relative url' => ['//evil.example.org'];
|
||||
yield 'absolute url' => ['https://evil.example.org'];
|
||||
}
|
||||
|
||||
private function seasonSelectUrlGenerator(): UrlGeneratorInterface&MockObject
|
||||
{
|
||||
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
|
||||
$urlGenerator->expects($this->once())
|
||||
->method('generate')
|
||||
->with('tvdt_quiz_select_season')
|
||||
->willReturn('/');
|
||||
|
||||
return $urlGenerator;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,286 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tvdt\Tests\Service;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Reader;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use Tvdt\Entity\Answer;
|
||||
use Tvdt\Entity\GivenAnswer;
|
||||
use Tvdt\Entity\Question;
|
||||
use Tvdt\Entity\Quiz;
|
||||
use Tvdt\Entity\QuizCandidate;
|
||||
use Tvdt\Entity\User;
|
||||
use Tvdt\Service\DataExportService;
|
||||
use Tvdt\Tests\Repository\DatabaseTestCase;
|
||||
|
||||
use function Safe\file_put_contents;
|
||||
use function Safe\tempnam;
|
||||
use function Safe\unlink;
|
||||
|
||||
#[CoversClass(DataExportService::class)]
|
||||
final class DataExportServiceTest extends DatabaseTestCase
|
||||
{
|
||||
private DataExportService $subject;
|
||||
|
||||
/** @var list<string> */
|
||||
private array $tempFiles = [];
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->subject = self::getContainer()->get(DataExportService::class);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
foreach ($this->tempFiles as $path) {
|
||||
if (file_exists($path)) {
|
||||
unlink($path);
|
||||
}
|
||||
}
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testExportForUserWithNoSeasonsContainsOnlyProfile(): void
|
||||
{
|
||||
$zip = $this->openZip($this->getUserByEmail('test@example.org'));
|
||||
|
||||
$this->assertSame(1, $zip->numFiles);
|
||||
$this->assertNotFalse($zip->locateName('profile.xlsx'));
|
||||
$zip->close();
|
||||
}
|
||||
|
||||
public function testExportForUserIncludesOwnedSeasonsQuizzesAndCandidates(): void
|
||||
{
|
||||
$zip = $this->openZip($this->getUserByEmail('user2@example.org'));
|
||||
|
||||
$names = $this->entryNames($zip);
|
||||
|
||||
$this->assertContains('profile.xlsx', $names);
|
||||
$this->assertContains('krtek-Krtek-Weekend/Quiz-1.xlsx', $names);
|
||||
$this->assertContains('krtek-Krtek-Weekend/Quiz-2.xlsx', $names);
|
||||
$this->assertContains('krtek-Krtek-Weekend/candidates.xlsx', $names);
|
||||
$this->assertContains('krtek-Krtek-Weekend/question-bank.xlsx', $names);
|
||||
$this->assertContains('bbbbb-Another-Season/candidates.xlsx', $names);
|
||||
$this->assertContains('bbbbb-Another-Season/question-bank.xlsx', $names);
|
||||
|
||||
// Another Season has no quizzes, so no quiz xlsx should be present for it.
|
||||
foreach ($names as $name) {
|
||||
$this->assertStringStartsNotWith('bbbbb-Another-Season/Quiz', $name);
|
||||
}
|
||||
|
||||
$quizContent = $zip->getFromName('krtek-Krtek-Weekend/Quiz-1.xlsx');
|
||||
$this->assertIsString($quizContent);
|
||||
$this->assertSame(['Quiz info', 'Questions', 'Raw answers', 'Results', 'Eliminations'], $this->sheetNames($quizContent));
|
||||
|
||||
$candidatesContent = $zip->getFromName('krtek-Krtek-Weekend/candidates.xlsx');
|
||||
$this->assertIsString($candidatesContent);
|
||||
$this->assertSame(['Candidates', 'Season info'], $this->sheetNames($candidatesContent));
|
||||
|
||||
$questionBankContent = $zip->getFromName('krtek-Krtek-Weekend/question-bank.xlsx');
|
||||
$this->assertIsString($questionBankContent);
|
||||
$this->assertSame(['Questions', 'Labels'], $this->sheetNames($questionBankContent));
|
||||
|
||||
$zip->close();
|
||||
}
|
||||
|
||||
public function testQuestionBankSheetIncludesBankQuestionsAndUsage(): void
|
||||
{
|
||||
$zip = $this->openZip($this->getUserByEmail('user2@example.org'));
|
||||
|
||||
$questionBankContent = $zip->getFromName('krtek-Krtek-Weekend/question-bank.xlsx');
|
||||
$this->assertIsString($questionBankContent);
|
||||
$zip->close();
|
||||
|
||||
$rows = $this->loadSheet($questionBankContent, 'Questions')->toArray();
|
||||
$header = $rows[0];
|
||||
$dataRows = \array_slice($rows, 1);
|
||||
|
||||
$questionIndex = array_search('Question', $header, true);
|
||||
$reusableIndex = array_search('Reusable', $header, true);
|
||||
$labelsIndex = array_search('Labels', $header, true);
|
||||
$usedInQuizzesIndex = array_search('Used in quizzes', $header, true);
|
||||
|
||||
$reusableRow = current(array_filter($dataRows, static fn (array $row): bool => 'Wie is de Krtek?' === $row[$questionIndex]));
|
||||
$this->assertIsArray($reusableRow);
|
||||
$this->assertSame('Yes', $reusableRow[$reusableIndex]);
|
||||
$this->assertSame('Finale', $reusableRow[$labelsIndex]);
|
||||
|
||||
$usedRow = current(array_filter($dataRows, static fn (array $row): bool => 'Waar sliep de Krtek?' === $row[$questionIndex]));
|
||||
$this->assertIsArray($usedRow);
|
||||
$this->assertSame('Quiz 2', $usedRow[$usedInQuizzesIndex]);
|
||||
|
||||
$labelRows = $this->loadSheet($questionBankContent, 'Labels')->toArray();
|
||||
$labelNames = array_column(\array_slice($labelRows, 1), 0);
|
||||
$this->assertContains('Locatie', $labelNames);
|
||||
$this->assertContains('Finale', $labelNames);
|
||||
}
|
||||
|
||||
public function testProfileSheetDoesNotContainPasswordHash(): void
|
||||
{
|
||||
$user = $this->getUserByEmail('user2@example.org');
|
||||
$zip = $this->openZip($user);
|
||||
|
||||
$profileContent = $zip->getFromName('profile.xlsx');
|
||||
$this->assertIsString($profileContent);
|
||||
$zip->close();
|
||||
|
||||
$rows = $this->loadSheet($profileContent, 'Account')->toArray();
|
||||
$flattened = implode(' ', array_merge(...$rows));
|
||||
|
||||
$this->assertStringNotContainsString($user->password, $flattened);
|
||||
}
|
||||
|
||||
public function testResultsSheetIncludesSoftDeletedQuizCandidates(): void
|
||||
{
|
||||
$season = $this->getSeasonByCode('krtek');
|
||||
$quiz = $this->entityManager->getRepository(Quiz::class)->findOneBy(['name' => 'Quiz 1', 'season' => $season]);
|
||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||
$candidate = $this->getCandidateBySeasonAndName($season, 'Claudia');
|
||||
|
||||
$quizCandidate = new QuizCandidate($quiz, $candidate);
|
||||
$this->entityManager->persist($quizCandidate);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->entityManager->remove($quizCandidate);
|
||||
$this->entityManager->flush();
|
||||
$this->entityManager->clear();
|
||||
|
||||
$zip = $this->openZip($this->getUserByEmail('user2@example.org'));
|
||||
$quizContent = $zip->getFromName('krtek-Krtek-Weekend/Quiz-1.xlsx');
|
||||
$this->assertIsString($quizContent);
|
||||
$zip->close();
|
||||
|
||||
$rows = $this->loadSheet($quizContent, 'Results')->toArray();
|
||||
$deletedColumnIndex = array_search('Deleted', $rows[0], true);
|
||||
$this->assertIsInt($deletedColumnIndex);
|
||||
$hasDeletedRow = array_any(\array_slice($rows, 1), static fn (array $row): bool => null !== $row[$deletedColumnIndex] && '' !== $row[$deletedColumnIndex]);
|
||||
|
||||
$this->assertTrue($hasDeletedRow, 'Expected the soft-deleted QuizCandidate to still appear with a Deleted timestamp');
|
||||
}
|
||||
|
||||
public function testRawAnswersSheetShowsCandidatesByQuestionsGrid(): void
|
||||
{
|
||||
$season = $this->getSeasonByCode('krtek');
|
||||
$quiz = $this->entityManager->getRepository(Quiz::class)->findOneBy(['name' => 'Quiz 1', 'season' => $season]);
|
||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||
$candidate = $this->getCandidateBySeasonAndName($season, 'Claudia');
|
||||
|
||||
/** @var Question $firstQuestion */
|
||||
$firstQuestion = $quiz->questions->first();
|
||||
$chosenAnswer = $firstQuestion->answers->filter(static fn (Answer $answer): bool => 'Man' === $answer->text)->first();
|
||||
$this->assertInstanceOf(Answer::class, $chosenAnswer);
|
||||
|
||||
$this->quizCandidateRepository->createIfNotExist($quiz, $candidate);
|
||||
|
||||
$givenAnswer = new GivenAnswer($candidate, $quiz, $chosenAnswer);
|
||||
$this->entityManager->persist($givenAnswer);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$zip = $this->openZip($this->getUserByEmail('user2@example.org'));
|
||||
$quizContent = $zip->getFromName('krtek-Krtek-Weekend/Quiz-1.xlsx');
|
||||
$this->assertIsString($quizContent);
|
||||
$zip->close();
|
||||
|
||||
$rows = $this->loadSheet($quizContent, 'Raw answers')->toArray();
|
||||
$header = $rows[0];
|
||||
$this->assertSame('Candidate', $header[0]);
|
||||
|
||||
$questionColumnIndex = array_search($firstQuestion->question, $header, true);
|
||||
$this->assertIsInt($questionColumnIndex);
|
||||
|
||||
$claudiaRow = current(array_filter(
|
||||
\array_slice($rows, 1),
|
||||
static fn (array $row): bool => 'Claudia' === $row[0],
|
||||
));
|
||||
$this->assertIsArray($claudiaRow);
|
||||
$this->assertSame('Man', $claudiaRow[$questionColumnIndex]);
|
||||
}
|
||||
|
||||
public function testQuizInfoSheetShowsDropoutsFinalizationAndDisabledQuestions(): void
|
||||
{
|
||||
$season = $this->getSeasonByCode('krtek');
|
||||
$quiz = $this->entityManager->getRepository(Quiz::class)->findOneBy(['name' => 'Quiz 1', 'season' => $season]);
|
||||
$this->assertInstanceOf(Quiz::class, $quiz);
|
||||
$this->assertTrue($quiz->isFinalized);
|
||||
|
||||
/** @var Question $disabledQuestion */
|
||||
$disabledQuestion = $quiz->questions->first();
|
||||
$disabledQuestion->enabled = false;
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
$zip = $this->openZip($this->getUserByEmail('user2@example.org'));
|
||||
$quizContent = $zip->getFromName('krtek-Krtek-Weekend/Quiz-1.xlsx');
|
||||
$this->assertIsString($quizContent);
|
||||
$zip->close();
|
||||
|
||||
$rows = $this->loadSheet($quizContent, 'Quiz info')->toArray();
|
||||
$values = [];
|
||||
foreach ($rows as $row) {
|
||||
$values[$row[0]] = $row[1];
|
||||
}
|
||||
|
||||
$this->assertSame('Quiz 1', $values['Quiz name']);
|
||||
$this->assertSame($quiz->dropouts, (int) $values['Number of dropouts']);
|
||||
$this->assertSame('Yes', $values['Finalized']);
|
||||
$this->assertNotEmpty($values['Finalized at']);
|
||||
$this->assertStringContainsString($disabledQuestion->question, (string) $values['Disabled questions']);
|
||||
}
|
||||
|
||||
private function openZip(User $user): \ZipArchive
|
||||
{
|
||||
$zipPath = $this->subject->exportForUser($user);
|
||||
$this->tempFiles[] = $zipPath;
|
||||
|
||||
$zip = new \ZipArchive();
|
||||
$this->assertTrue($zip->open($zipPath));
|
||||
|
||||
return $zip;
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
private function entryNames(\ZipArchive $zip): array
|
||||
{
|
||||
$names = [];
|
||||
for ($i = 0; $i < $zip->numFiles; ++$i) {
|
||||
$name = $zip->getNameIndex($i);
|
||||
$this->assertIsString($name);
|
||||
$names[] = $name;
|
||||
}
|
||||
|
||||
return $names;
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
private function sheetNames(string $xlsxContent): array
|
||||
{
|
||||
$path = $this->createTempPath();
|
||||
file_put_contents($path, $xlsxContent);
|
||||
|
||||
return array_values(new Reader\Xlsx()->load($path)->getSheetNames());
|
||||
}
|
||||
|
||||
private function loadSheet(string $xlsxContent, string $sheetName): Worksheet
|
||||
{
|
||||
$path = $this->createTempPath();
|
||||
file_put_contents($path, $xlsxContent);
|
||||
|
||||
$sheet = new Reader\Xlsx()->load($path)->getSheetByName($sheetName);
|
||||
$this->assertInstanceOf(Worksheet::class, $sheet);
|
||||
|
||||
return $sheet;
|
||||
}
|
||||
|
||||
private function createTempPath(): string
|
||||
{
|
||||
$path = tempnam(sys_get_temp_dir(), 'tvdt_export_test_');
|
||||
$this->tempFiles[] = $path;
|
||||
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
@@ -205,6 +205,14 @@
|
||||
<source>Confirm Answers</source>
|
||||
<target>Bevestig antwoorden</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="T1Z6nI1" resname="Confirm your email address to enable this feature.">
|
||||
<source>Confirm your email address to enable this feature.</source>
|
||||
<target>Bevestig je e-mailadres om deze functie te kunnen gebruiken.</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="QZfvKMx" resname="Confirm your email address to enable this.">
|
||||
<source>Confirm your email address to enable this.</source>
|
||||
<target>Bevestig je e-mailadres om dit te gebruiken.</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="PiAVEe9" resname="Confirmed">
|
||||
<source>Confirmed</source>
|
||||
<target>Bevestigd</target>
|
||||
@@ -293,6 +301,10 @@
|
||||
<source>Download Template</source>
|
||||
<target>Download sjabloon</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="43g0Dc8" resname="Download an archive of everything stored under your account: your profile, the seasons you own, their quizzes, results and candidates.">
|
||||
<source>Download an archive of everything stored under your account: your profile, the seasons you own, their quizzes, results and candidates.</source>
|
||||
<target>Download een archief met alles wat onder je account is opgeslagen: je profiel, de seizoenen die je bezit, met de bijbehorende testen, resultaten en kandidaten.</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="58e2QWG" resname="Download data">
|
||||
<source>Download data</source>
|
||||
<target>Gegevens downloaden</target>
|
||||
@@ -357,6 +369,10 @@
|
||||
<source>Forgot your password?</source>
|
||||
<target>Wachtwoord vergeten?</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="3cWWP_q" resname="Fullscreen">
|
||||
<source>Fullscreen</source>
|
||||
<target>Volledig scherm</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="MebBrmp" resname="Gray">
|
||||
<source>Gray</source>
|
||||
<target>Grijs</target>
|
||||
@@ -557,6 +573,18 @@
|
||||
<source>Please Confirm your Email</source>
|
||||
<target>Bevestig je e-mailadres alsjeblieft</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="7osID7L" resname="Please confirm your email address before downloading your data.">
|
||||
<source>Please confirm your email address before downloading your data.</source>
|
||||
<target>Bevestig eerst je e-mailadres voordat je je gegevens downloadt.</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="XS4opCD" resname="Please confirm your email address before exporting a quiz.">
|
||||
<source>Please confirm your email address before exporting a quiz.</source>
|
||||
<target>Bevestig je e-mailadres voordat je een quiz exporteert.</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="y4TgBCP" resname="Please confirm your email address before exporting data.">
|
||||
<source>Please confirm your email address before exporting data.</source>
|
||||
<target>Bevestig eerst je e-mailadres voordat je gegevens exporteert.</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="mq1QYAv" resname="Please select an answer">
|
||||
<source>Please select an answer</source>
|
||||
<target>Selecteer een antwoorden alsjeblieft</target>
|
||||
|
||||
Reference in New Issue
Block a user