jilo-web/tests/Unit/Classes/UserRegisterTest.php

168 lines
6.3 KiB
PHP
Raw Normal View History

2025-04-17 07:59:40 +00:00
<?php
2026-01-20 21:38:49 +00:00
require_once dirname(__DIR__, 3) . '/app/core/App.php';
2025-04-17 07:59:40 +00:00
require_once dirname(__DIR__, 3) . '/app/classes/database.php';
require_once dirname(__DIR__, 3) . '/app/classes/user.php';
require_once dirname(__DIR__, 3) . '/plugins/register/models/register.php';
require_once dirname(__DIR__, 3) . '/app/classes/ratelimiter.php';
use PHPUnit\Framework\TestCase;
2026-01-20 21:38:49 +00:00
use App\App;
2025-04-17 07:59:40 +00:00
class UserRegisterTest extends TestCase
{
private $db;
private $register;
private $user;
protected function setUp(): void
{
parent::setUp();
2025-04-25 14:15:56 +00:00
// Prepare DB for Github CI
$host = defined('CI_DB_HOST') ? CI_DB_HOST : '127.0.0.1';
$password = defined('CI_DB_PASSWORD') ? CI_DB_PASSWORD : '';
2025-04-17 07:59:40 +00:00
$this->db = new Database([
2025-04-25 14:15:56 +00:00
'type' => 'mariadb',
'host' => $host,
'port' => '3306',
2025-04-25 15:30:24 +00:00
'dbname' => 'jilo_test',
'user' => 'test_jilo',
2025-04-25 14:15:56 +00:00
'password' => $password
2025-04-17 07:59:40 +00:00
]);
2026-01-20 21:38:49 +00:00
// Set up App::db() for Register class to use
App::set('db', $this->db->getConnection());
2026-01-21 17:56:14 +00:00
// Use centralized schema setup
setupTestDatabaseSchema($this->db->getConnection());
2025-04-17 07:59:40 +00:00
2026-01-21 17:56:14 +00:00
// Clean up any test users from previous runs
$this->db->getConnection()->exec("DELETE FROM user_2fa WHERE user_id >= 1000");
$this->db->getConnection()->exec("DELETE FROM security_rate_auth WHERE username LIKE 'testuser%'");
$this->db->getConnection()->exec("DELETE FROM user_meta WHERE user_id >= 1000");
$this->db->getConnection()->exec("DELETE FROM user WHERE id >= 1000");
2025-04-17 07:59:40 +00:00
2026-01-20 21:38:49 +00:00
$this->register = new Register();
2025-04-17 07:59:40 +00:00
$this->user = new User($this->db);
}
2025-04-25 14:15:56 +00:00
protected function tearDown(): void
{
2026-01-20 21:38:49 +00:00
// Clean up App state
App::reset('db');
2026-01-21 17:56:14 +00:00
// Clean up test data
$this->db->getConnection()->exec("DELETE FROM user_2fa WHERE user_id >= 1000");
$this->db->getConnection()->exec("DELETE FROM security_rate_auth WHERE username LIKE 'testuser%'");
$this->db->getConnection()->exec("DELETE FROM user_meta WHERE user_id >= 1000");
$this->db->getConnection()->exec("DELETE FROM user WHERE id >= 1000");
2025-04-25 14:15:56 +00:00
parent::tearDown();
}
2025-04-17 07:59:40 +00:00
public function testRegister()
{
2026-01-21 17:56:14 +00:00
// Register a new user with unique username
$username = 'testuser_reg_' . time() . '_' . rand(1000, 9999);
2025-04-25 14:15:56 +00:00
$password = 'password123';
$result = $this->register->register($username, $password);
2025-04-17 07:59:40 +00:00
$this->assertTrue($result);
// Verify user was created
2025-04-25 14:15:56 +00:00
$stmt = $this->db->getConnection()->prepare("SELECT * FROM user WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
2025-04-17 07:59:40 +00:00
2025-04-25 14:15:56 +00:00
$this->assertNotNull($user);
$this->assertEquals($username, $user['username']);
$this->assertTrue(password_verify($password, $user['password']));
2025-04-17 07:59:40 +00:00
2025-04-25 14:15:56 +00:00
// Verify metadata was created
$stmt = $this->db->getConnection()->prepare("SELECT * FROM user_meta WHERE user_id = ?");
2025-04-17 07:59:40 +00:00
$stmt->execute([$user['id']]);
2025-04-25 14:15:56 +00:00
$meta = $stmt->fetch(PDO::FETCH_ASSOC);
2025-04-17 07:59:40 +00:00
$this->assertNotNull($meta);
2025-04-25 14:15:56 +00:00
$this->assertEquals($user['id'], $meta['user_id']);
2025-04-17 07:59:40 +00:00
}
public function testLogin()
{
2026-01-21 17:56:14 +00:00
// First register a user with unique username
$username = 'testuser_login_' . time() . '_' . rand(1000, 9999);
2025-04-17 07:59:40 +00:00
$password = 'password123';
2025-04-25 14:15:56 +00:00
$this->register->register($username, $password);
2025-04-17 07:59:40 +00:00
// Mock $_SERVER['REMOTE_ADDR'] for rate limiter
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
// Test successful login
try {
2025-04-25 14:15:56 +00:00
$result = $this->user->login($username, $password);
2025-04-17 07:59:40 +00:00
$this->assertIsArray($result);
$this->assertEquals('success', $result['status']);
$this->assertArrayHasKey('user_id', $result);
$this->assertArrayHasKey('username', $result);
$this->assertArrayHasKey('user_id', $_SESSION);
2025-04-25 14:15:56 +00:00
$this->assertArrayHasKey('username', $_SESSION);
2025-04-17 07:59:40 +00:00
$this->assertArrayHasKey('CREATED', $_SESSION);
$this->assertArrayHasKey('LAST_ACTIVITY', $_SESSION);
} catch (Exception $e) {
2025-04-25 14:15:56 +00:00
$this->fail('Login should not throw for valid credentials: ' . $e->getMessage());
2025-04-17 07:59:40 +00:00
}
// Test failed login
2025-04-25 14:15:56 +00:00
$result = $this->user->login($username, 'wrongpassword');
$this->assertIsArray($result);
$this->assertEquals('failed', $result['status']);
$this->assertArrayHasKey('message', $result);
$this->assertStringContainsString('Invalid credentials', $result['message']);
2025-04-17 07:59:40 +00:00
}
public function testGetUserDetails()
{
2026-01-21 17:56:14 +00:00
// First register a user with unique username
$username = 'testuser_details_' . time() . '_' . rand(1000, 9999);
2025-04-25 14:15:56 +00:00
$password = 'password123';
$result = $this->register->register($username, $password);
$this->assertTrue($result);
2025-04-17 07:59:40 +00:00
2025-04-25 14:15:56 +00:00
// Get user ID from database
$stmt = $this->db->getConnection()->prepare("SELECT id FROM user WHERE username = ?");
$stmt->execute([$username]);
$userId = $stmt->fetchColumn();
$this->assertNotFalse($userId);
// Insert user metadata
$stmt = $this->db->getConnection()->prepare("
UPDATE user_meta
SET name = ?, email = ?
WHERE user_id = ?
");
$stmt->execute(['Test User', 'test@example.com', $userId]);
2025-04-17 07:59:40 +00:00
2025-04-25 14:15:56 +00:00
// Get user details
2025-04-17 07:59:40 +00:00
$userDetails = $this->user->getUserDetails($userId);
2025-04-25 14:15:56 +00:00
2025-04-17 07:59:40 +00:00
$this->assertIsArray($userDetails);
2025-04-25 14:15:56 +00:00
$this->assertNotEmpty($userDetails);
$this->assertArrayHasKey(0, $userDetails, 'User details should be returned as an array');
// Get first row since we're querying by primary key
$userDetails = $userDetails[0];
$this->assertArrayHasKey('username', $userDetails, 'User details should include username');
$this->assertArrayHasKey('name', $userDetails, 'User details should include name');
$this->assertArrayHasKey('email', $userDetails, 'User details should include email');
// Verify values
$this->assertEquals($username, $userDetails['username'], 'Username should match');
$this->assertEquals('Test User', $userDetails['name'], 'Name should match');
$this->assertEquals('test@example.com', $userDetails['email'], 'Email should match');
2025-04-17 07:59:40 +00:00
}
}