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

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

View File

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

View File

@@ -4,6 +4,9 @@
* 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';
echo "Database Connection Test\n";

View File

@@ -5,6 +5,10 @@
* 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 "║ Post-ERG Test Suite ║\n";
echo "╚════════════════════════════════════════════╝\n\n";