fix: remove DB_ENV auto-detection; require explicit DB_ENV=test for tests

src/config.php: remove the file-existence fallback that silently redirected
all requests to test.db whenever that file was present on disk. getDatabasePath()
now always returns the production DB unless DB_ENV=test is explicitly set.

tests/run-tests.php: putenv('DB_ENV=test') at the top so the suite always
targets test.db regardless of what is set in the shell environment.

tests/Unit/DatabaseTest.php, tests/Integration/SearchTest.php,
tests/Security/SecurityTest.php: same putenv() guard added to each file so
they work correctly when run standalone (e.g. just test-unit).

justfile: all test and DB-development recipes now prefix DB_ENV=test to their
php/sqlite3 commands, making the intent explicit in the recipe itself.

Fixes: a developer who ran the test suite and kept test.db on disk would
silently hit test data when browsing the local site with no DB_ENV set.
This commit is contained in:
Pontoporeia
2026-03-28 13:43:04 +01:00
parent 7d96a08324
commit 71167b2cdf
9 changed files with 29 additions and 30 deletions

View File

@@ -410,7 +410,7 @@ Goal: rename the tables and column to the canonical M2M pattern (`tags`, `thesis
future API endpoint) a partial failure leaves orphaned rows. Wrap the body in future API endpoint) a partial failure leaves orphaned rows. Wrap the body in
`BEGIN … COMMIT / ROLLBACK` (check `$this->pdo->inTransaction()` to avoid nesting). `BEGIN … COMMIT / ROLLBACK` (check `$this->pdo->inTransaction()` to avoid nesting).
- [ ] **DB config auto-detection is fragile**`src/config.php` switches to `test.db` whenever the - [x] **DB config auto-detection is fragile**`src/config.php` switches to `test.db` whenever the
file exists locally, which means a developer who ran tests and forgot to delete `test.db` will file exists locally, which means a developer who ran tests and forgot to delete `test.db` will
silently hit test data on a local production-mirror. Make the default `prod`; require explicit silently hit test data on a local production-mirror. Make the default `prod`; require explicit
`DB_ENV=test` to use the test database. `DB_ENV=test` to use the test database.

View File

@@ -84,20 +84,20 @@ deploy-db:
[group('test')] [group('test')]
test: test:
@php tests/run-tests.php @DB_ENV=test php tests/run-tests.php
[group('test')] [group('test')]
test-unit: test-unit:
@php tests/Unit/DatabaseTest.php @DB_ENV=test php tests/Unit/DatabaseTest.php
@php tests/Unit/RateLimitTest.php @DB_ENV=test php tests/Unit/RateLimitTest.php
[group('test')] [group('test')]
test-integration: test-integration:
@php tests/Integration/SearchTest.php @DB_ENV=test php tests/Integration/SearchTest.php
[group('test')] [group('test')]
test-security: test-security:
@php tests/Security/SecurityTest.php @DB_ENV=test php tests/Security/SecurityTest.php
[group('test')] [group('test')]
syntax: syntax:

View File

@@ -1 +1 @@
[1774701325] [1774701765]

View File

@@ -15,35 +15,23 @@ define('DB_TEST_PATH', DB_ROOT . '/storage/test.db');
define('DB_PROD_PATH', DB_ROOT . '/storage/posterg.db'); define('DB_PROD_PATH', DB_ROOT . '/storage/posterg.db');
/** /**
* Determine which database to use * Determine which database to use.
* Checks environment variable DB_ENV, defaults to auto-detection
* *
* Set DB_ENV in your environment: * Always defaults to the production database.
* - export DB_ENV=test # Force test database * Set DB_ENV=test explicitly to use the test database.
* - export DB_ENV=prod # Force production database
* *
* Auto-detection logic: * export DB_ENV=test # use storage/test.db
* - If test.db exists, use it (development) * export DB_ENV=prod # use storage/posterg.db (default)
* - Otherwise use posterg.db (production) *
* The old file-existence auto-detection has been removed: a leftover
* test.db on a developer machine no longer silently redirects all
* requests to test data.
*/ */
function getDatabasePath() { function getDatabasePath(): string {
// Allow explicit override via environment variable if (getenv('DB_ENV') === 'test') {
$env = getenv('DB_ENV');
if ($env === 'test') {
return DB_TEST_PATH; return DB_TEST_PATH;
} }
if ($env === 'prod') {
return DB_PROD_PATH;
}
// Auto-detect: prefer test database if it exists
if (file_exists(DB_TEST_PATH)) {
return DB_TEST_PATH;
}
// Default to production database
return DB_PROD_PATH; return DB_PROD_PATH;
} }

Binary file not shown.

View File

@@ -4,6 +4,8 @@
* Tests search queries and results * Tests search queries and results
*/ */
putenv('DB_ENV=test');
require_once __DIR__ . '/../../src/Database.php'; require_once __DIR__ . '/../../src/Database.php';
echo "Search Functionality Test\n"; echo "Search Functionality Test\n";

View File

@@ -4,6 +4,8 @@
* Tests SQL injection protection and input sanitization * Tests SQL injection protection and input sanitization
*/ */
putenv('DB_ENV=test');
require_once __DIR__ . '/../../src/Database.php'; require_once __DIR__ . '/../../src/Database.php';
echo "Security Test Suite\n"; echo "Security Test Suite\n";

View File

@@ -4,6 +4,9 @@
* Tests basic database connectivity and query functionality * Tests basic database connectivity and query functionality
*/ */
// Must be set before Database.php is required so getDatabasePath() picks it up.
putenv('DB_ENV=test');
require_once __DIR__ . '/../../src/Database.php'; require_once __DIR__ . '/../../src/Database.php';
echo "Database Connection Test\n"; echo "Database Connection Test\n";

View File

@@ -5,6 +5,10 @@
* Runs all tests in the tests/ directory * Runs all tests in the tests/ directory
*/ */
// Tests always run against the test database; require an explicit opt-in so
// that a stray test.db on disk never silently redirects a production session.
putenv('DB_ENV=test');
echo "╔════════════════════════════════════════════╗\n"; echo "╔════════════════════════════════════════════╗\n";
echo "║ Post-ERG Test Suite ║\n"; echo "║ Post-ERG Test Suite ║\n";
echo "╚════════════════════════════════════════════╝\n\n"; echo "╚════════════════════════════════════════════╝\n\n";