jilo-web/tests/Feature/Middleware/SessionMiddlewareTest.php

109 lines
2.9 KiB
PHP
Raw Normal View History

2025-02-19 13:31:01 +00:00
<?php
use PHPUnit\Framework\TestCase;
2025-04-13 17:51:52 +00:00
use Tests\Feature\Middleware\Mock\Session;
use Tests\Feature\Middleware\Mock\Feedback;
require_once __DIR__ . '/MockSession.php';
require_once __DIR__ . '/MockFeedback.php';
2025-02-19 13:31:01 +00:00
class SessionMiddlewareTest extends TestCase
{
protected $config;
protected $app_root;
2025-04-12 13:48:53 +00:00
protected const SESSION_TIMEOUT = 7200; // 2 hours in seconds
2025-02-19 13:31:01 +00:00
protected function setUp(): void
{
parent::setUp();
// Mock server variables
$_SERVER['HTTP_USER_AGENT'] = 'PHPUnit Test Browser';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['HTTPS'] = 'on';
// Set up test config
$this->config = [
'folder' => '/app',
'domain' => 'localhost'
];
$this->app_root = 'https://localhost/app';
2025-04-08 10:13:49 +00:00
// Initialize session variables
$_SESSION = [
'user_id' => 1,
'username' => 'testuser',
'CREATED' => time(),
'LAST_ACTIVITY' => time()
];
2025-02-19 13:31:01 +00:00
}
protected function tearDown(): void
{
parent::tearDown();
2025-04-13 17:51:52 +00:00
$_SESSION = [];
2025-02-19 13:31:01 +00:00
}
2025-04-13 17:51:52 +00:00
protected function applyMiddleware()
2025-02-19 13:31:01 +00:00
{
2025-04-13 17:51:52 +00:00
// Check session validity
if (!Session::isValidSession()) {
// Session invalid, clean up
Session::cleanup($this->config);
Feedback::flash("LOGIN", "SESSION_TIMEOUT");
return false;
}
return true;
}
public function testValidSession()
{
$result = $this->applyMiddleware();
2025-02-19 13:31:01 +00:00
$this->assertTrue($result);
$this->assertArrayHasKey('LAST_ACTIVITY', $_SESSION);
$this->assertArrayHasKey('CREATED', $_SESSION);
2025-04-08 10:13:49 +00:00
$this->assertArrayHasKey('user_id', $_SESSION);
$this->assertEquals(1, $_SESSION['user_id']);
2025-02-19 13:31:01 +00:00
}
public function testSessionTimeout()
{
2025-04-12 13:48:53 +00:00
$_SESSION['LAST_ACTIVITY'] = time() - (self::SESSION_TIMEOUT + 60); // 2 hours + 1 minute ago
2025-04-13 17:51:52 +00:00
$result = $this->applyMiddleware();
2025-02-19 13:31:01 +00:00
$this->assertFalse($result);
2025-04-13 17:51:52 +00:00
$this->assertEmpty($_SESSION);
2025-02-19 13:31:01 +00:00
}
public function testRememberMe()
{
2025-04-08 10:13:49 +00:00
$_SESSION['REMEMBER_ME'] = true;
2025-04-12 13:48:53 +00:00
$_SESSION['LAST_ACTIVITY'] = time() - (self::SESSION_TIMEOUT + 60); // More than 2 hours ago
2025-02-19 13:31:01 +00:00
2025-04-13 17:51:52 +00:00
$result = $this->applyMiddleware();
2025-02-19 13:31:01 +00:00
$this->assertTrue($result);
2025-04-08 10:13:49 +00:00
$this->assertArrayHasKey('user_id', $_SESSION);
2025-02-19 13:31:01 +00:00
}
public function testNoUserSession()
{
2025-04-08 10:13:49 +00:00
unset($_SESSION['user_id']);
2025-04-13 17:51:52 +00:00
$result = $this->applyMiddleware();
2025-02-19 13:31:01 +00:00
$this->assertFalse($result);
2025-04-13 17:51:52 +00:00
$this->assertEmpty($_SESSION);
2025-02-19 13:31:01 +00:00
}
2025-04-13 17:51:52 +00:00
public function testInvalidSession()
2025-02-19 13:31:01 +00:00
{
2025-04-12 13:48:53 +00:00
$_SESSION['LAST_ACTIVITY'] = time() - (self::SESSION_TIMEOUT + 60); // 2 hours + 1 minute ago
2025-04-13 17:51:52 +00:00
unset($_SESSION['REMEMBER_ME']);
$result = $this->applyMiddleware();
2025-02-19 13:31:01 +00:00
$this->assertFalse($result);
2025-04-13 17:51:52 +00:00
$this->assertEmpty($_SESSION);
2025-02-19 13:31:01 +00:00
}
}