---
title: "Test Helper Reference"
url: "https://docs.chinaski.net/en/about/contributing/testing/test-helper/"
type: "page"
language: "en"
description: "Programmer's reference for t/helper.pl, the test suite's shared library: setup_test_env, the temp database fixture, and the login utilities."
updated: "2026-09-29"
generated: "Markdown rendition of the HTML page at url, built by Chinaski"
---

# 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_DB` at the temp file, `CHINASKI_DOCS_DIR` at a per-run nested path (`<db>.docs/documents`), and creates the app’s `output/` 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`/`-shm` sidecars 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:

1. GET /login (200).
2. Extract the CSRF token from the rendered form (`csrf_token" ... value="…"`) via a regex: matching how login CSRF is validated server-side.
3. POST the credentials with the token, expect the 302 redirect, then assert the `Location` header 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

- `$t` and `$dbh` are 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 after `use`-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=1` themselves; the helper doesn’t.
