config = [ 'folder' => '/app', 'domain' => 'localhost' ]; $this->app_root = 'https://localhost/app'; // Initialize session variables $_SESSION = [ 'user_id' => 1, 'username' => 'testuser', 'CREATED' => time(), 'LAST_ACTIVITY' => time() ]; } protected function tearDown(): void { parent::tearDown(); $_SESSION = []; } protected function applyMiddleware() { // 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(); $this->assertTrue($result); $this->assertArrayHasKey('LAST_ACTIVITY', $_SESSION); $this->assertArrayHasKey('CREATED', $_SESSION); $this->assertArrayHasKey('user_id', $_SESSION); $this->assertEquals(1, $_SESSION['user_id']); } public function testSessionTimeout() { $_SESSION['LAST_ACTIVITY'] = time() - (self::SESSION_TIMEOUT + 60); // 2 hours + 1 minute ago $result = $this->applyMiddleware(); $this->assertFalse($result); $this->assertEmpty($_SESSION); } public function testRememberMe() { $_SESSION['REMEMBER_ME'] = true; $_SESSION['LAST_ACTIVITY'] = time() - (self::SESSION_TIMEOUT + 60); // More than 2 hours ago $result = $this->applyMiddleware(); $this->assertTrue($result); $this->assertArrayHasKey('user_id', $_SESSION); } public function testNoUserSession() { unset($_SESSION['user_id']); $result = $this->applyMiddleware(); $this->assertFalse($result); $this->assertEmpty($_SESSION); } public function testInvalidSession() { $_SESSION['LAST_ACTIVITY'] = time() - (self::SESSION_TIMEOUT + 60); // 2 hours + 1 minute ago unset($_SESSION['REMEMBER_ME']); $result = $this->applyMiddleware(); $this->assertFalse($result); $this->assertEmpty($_SESSION); } }