Test Helper Reference
t/helper.pl is the test suite’s shared library — the fixture constructor and login utilities every test file uses. This page is the programmer’s-eye reference for the pieces it provides. (For how to run the suite, see the running-tests page; for what gets tested, see the coverage page.)
setup_test_env
The environment constructor, callable from any test file:
- Sets the session secret to a known test value and
MOJO_MODE=test(the test-mode branch of the CMS kicks delayed work synchronously, so tests don’t wait on timers). - Creates a fresh SQLite temp database (File::Temp, UNLINK-only-the-main-file) built statement-by-statement from the real
schema.sql— with the seed INSERT sections for users and settings removed so the suite’s own seeds are authoritative. - Enables foreign-key enforcement (
PRAGMA foreign_keys = ON). - Points
CHINASKI_DBat the temp file,CHINASKI_DOCS_DIRat a per-run nested path (<db>.docs/documents), and creates the app’soutput/directory if missing (it’s gitignored; fresh checkouts on CI would otherwise fail the /health output-dir-writable check in tests that exercise it). - Registers cleanup in an END block: the WAL
-wal/-shmsidecars and the docs tree, which SQLite unlink tricks never catch. - Seeds two users with bcrypt-hashed passwords generated at runtime via Crypt::Passphrase (
admin/admin123,editor/editor123) and a two-language site (en, es). - Finally loads the real application (
require cms.pl) and exposes the harness as$t = Test::Mojo->new(shared package globals, so every test can$t->get_ok(...)right after the call).
login_admin / login_editor
Drive a full sign-in:
- GET /login (200).
- Extract the CSRF token from the rendered form (
csrf_token" ... value="…") via a regex — matching how login CSRF is validated server-side. - POST the credentials with the token, expect the 302 redirect, then assert the
Locationheader matches the admin area (the app may redirect to a saved return_to path when login was preceded by a protected URL, which is why the test asserts only the prefix).
Other exported bits
$tand$dbhare package globals so tests can drive HTTP calls or reach into the DB directly when needed.- Helper functions are exported names via the module list (
setup_test_env,login_admin,login_editor,$t,$dbh).
Conventions
- Files that need the harness call
setup_test_env()right afteruse-ing/require-ing helper.pl; anything before that call has no app and no DB. - Local state mutations should stay inside the test file’s own session; the harness resets nothing between tests within one file (that’s per-file isolation, not per-assertion).
- Build-shell-out tests set
CHINASKI_TEST=1themselves; the helper doesn’t.