Add comprehensive unit tests for Security, UserModel, and Validation utilities
- Implemented SecurityTest to validate token generation, CSRF protection, input sanitization, and rate limiting. - Created UserModelTest to ensure correct database operations for user management, including creation, updating, banning, and fetching active users. - Developed ValidationTest to verify input validation and sanitization for user IDs, nicknames, messages, and API requests. - Introduced Security and Validation utility classes with methods for secure token generation, input sanitization, and comprehensive validation rules.
This commit is contained in:
parent
5692874b10
commit
41cd7a4fd8
32 changed files with 5796 additions and 368 deletions
138
tests/bootstrap.php
Normal file
138
tests/bootstrap.php
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPUnit Test Bootstrap
|
||||
* Sets up test environment and dependencies
|
||||
*/
|
||||
|
||||
// Define test environment
|
||||
define('TESTING', true);
|
||||
define('APP_ENV', 'testing');
|
||||
|
||||
// Include autoloader if it exists, otherwise manually load classes
|
||||
if (file_exists(__DIR__ . '/../includes/autoloader.php')) {
|
||||
require_once __DIR__ . '/../includes/autoloader.php';
|
||||
}
|
||||
|
||||
// Initialize error handling for tests
|
||||
if (class_exists('ErrorHandler')) {
|
||||
ErrorHandler::initialize();
|
||||
}
|
||||
|
||||
// Set up test database configuration
|
||||
$_ENV['APP_ENV'] = 'testing';
|
||||
$_ENV['DB_DATABASE'] = ':memory:'; // Use in-memory SQLite for tests
|
||||
$_ENV['DB_DRIVER'] = 'sqlite';
|
||||
|
||||
// Mock session for testing
|
||||
if (!isset($_SESSION)) {
|
||||
$_SESSION = [];
|
||||
}
|
||||
|
||||
// Mock POST/GET data if needed
|
||||
if (!isset($_POST)) {
|
||||
$_POST = [];
|
||||
}
|
||||
if (!isset($_GET)) {
|
||||
$_GET = [];
|
||||
}
|
||||
if (!isset($_SERVER)) {
|
||||
$_SERVER = [
|
||||
'REQUEST_METHOD' => 'GET',
|
||||
'HTTP_HOST' => 'localhost',
|
||||
'SERVER_NAME' => 'localhost',
|
||||
'SERVER_PORT' => '80',
|
||||
'REQUEST_URI' => '/',
|
||||
'SCRIPT_NAME' => '/index.php',
|
||||
'PHP_SELF' => '/index.php',
|
||||
'REMOTE_ADDR' => '127.0.0.1',
|
||||
'HTTP_USER_AGENT' => 'PHPUnit/Test'
|
||||
];
|
||||
}
|
||||
|
||||
// Initialize PDO for in-memory SQLite testing
|
||||
class TestDatabaseHelper
|
||||
{
|
||||
private static $pdo = null;
|
||||
|
||||
public static function getTestPdo()
|
||||
{
|
||||
if (self::$pdo === null) {
|
||||
self::$pdo = new PDO('sqlite::memory:');
|
||||
self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
self::$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
||||
|
||||
// Enable WAL mode for better testing performance
|
||||
self::$pdo->exec('PRAGMA journal_mode=WAL');
|
||||
self::$pdo->exec('PRAGMA synchronous=NORMAL');
|
||||
}
|
||||
|
||||
return self::$pdo;
|
||||
}
|
||||
|
||||
public static function setupTestSchema()
|
||||
{
|
||||
$pdo = self::getTestPdo();
|
||||
|
||||
// Create tables for testing
|
||||
$sql = file_get_contents(__DIR__ . '/../migrations/001_create_tables.sql');
|
||||
$pdo->exec($sql);
|
||||
|
||||
// Insert test data if needed
|
||||
self::insertTestData($pdo);
|
||||
}
|
||||
|
||||
private static function insertTestData($pdo)
|
||||
{
|
||||
// Insert some test users
|
||||
$pdo->exec("INSERT INTO users (user_id, nickname, ip_address, session_id, last_seen)
|
||||
VALUES ('test_user_1', 'TestUser1', '192.168.1.100', 'session_123', datetime('now'))");
|
||||
|
||||
$pdo->exec("INSERT INTO users (user_id, nickname, ip_address, session_id, last_seen)
|
||||
VALUES ('test_user_2', 'TestUser2', '192.168.1.101', 'session_456', datetime('now'))");
|
||||
|
||||
// Insert test messages
|
||||
$pdo->exec("INSERT INTO chat_messages (user_id, nickname, message, is_admin, ip_address, time_formatted)
|
||||
VALUES ('test_user_1', 'TestUser1', 'Hello from test user 1', 0, '192.168.1.100', '12:00')");
|
||||
|
||||
$pdo->exec("INSERT INTO chat_messages (user_id, nickname, message, is_admin, ip_address, time_formatted)
|
||||
VALUES ('test_user_2', 'TestUser2', 'Hello from test user 2', 0, '192.168.1.101', '12:01')");
|
||||
|
||||
// Insert test active viewers
|
||||
$pdo->exec("INSERT INTO active_viewers (user_id, nickname, ip_address, session_id, is_admin, last_seen)
|
||||
VALUES ('test_user_1', 'TestUser1', '192.168.1.100', 'session_123', 0, datetime('now'))");
|
||||
}
|
||||
|
||||
public static function teardown()
|
||||
{
|
||||
self::$pdo = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up any existing test artifacts
|
||||
function cleanupTestEnvironment()
|
||||
{
|
||||
// Clear test session data
|
||||
$_SESSION = [];
|
||||
|
||||
// Remove any test files
|
||||
$testFiles = [
|
||||
__DIR__ . '/../logs/app.log',
|
||||
__DIR__ . '/../active_viewers.json.backup',
|
||||
__DIR__ . '/../chat_messages.json.backup'
|
||||
];
|
||||
|
||||
foreach ($testFiles as $file) {
|
||||
if (file_exists($file)) {
|
||||
@unlink($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set up test environment
|
||||
cleanupTestEnvironment();
|
||||
|
||||
// Register shutdown function to clean up
|
||||
register_shutdown_function(function() {
|
||||
TestDatabaseHelper::teardown();
|
||||
cleanupTestEnvironment();
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue