168 lines
5.0 KiB
PHP
168 lines
5.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Shared;
|
|
|
|
use App\Shared\Http\SessionManager;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* Tests unitaires pour SessionManager.
|
|
*
|
|
* Vérifie la lecture et l'écriture des données d'authentification
|
|
* dans $_SESSION, ainsi que la destruction de session.
|
|
*
|
|
* Note : session_start() n'est pas appelé dans ces tests — SessionManager
|
|
* manipule directement $_SESSION, ce qui fonctionne en CLI sans session active.
|
|
* session_regenerate_id() et session_destroy() sont gardés par un test
|
|
* session_status() === PHP_SESSION_ACTIVE dans SessionManager, ce qui les rend
|
|
* sans effet en contexte CLI et évite toute notice PHP.
|
|
*/
|
|
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
|
final class SessionManagerTest extends TestCase
|
|
{
|
|
private SessionManager $manager;
|
|
|
|
/**
|
|
* Réinitialise $_SESSION avant chaque test pour garantir l'isolation.
|
|
*/
|
|
protected function setUp(): void
|
|
{
|
|
$_SESSION = [];
|
|
$this->manager = new SessionManager();
|
|
}
|
|
|
|
/**
|
|
* Réinitialise $_SESSION après chaque test.
|
|
*/
|
|
protected function tearDown(): void
|
|
{
|
|
$_SESSION = [];
|
|
}
|
|
|
|
|
|
// ── isAuthenticated ────────────────────────────────────────────
|
|
|
|
/**
|
|
* Sans session active, isAuthenticated() doit retourner false.
|
|
*/
|
|
public function testIsAuthenticatedWithoutSession(): void
|
|
{
|
|
$this->assertFalse($this->manager->isAuthenticated());
|
|
}
|
|
|
|
/**
|
|
* Après setUser(), isAuthenticated() doit retourner true.
|
|
*/
|
|
public function testIsAuthenticatedAfterSetUser(): void
|
|
{
|
|
$this->manager->setUser(1, 'alice', 'user');
|
|
|
|
$this->assertTrue($this->manager->isAuthenticated());
|
|
}
|
|
|
|
|
|
// ── getUserId ──────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Sans session active, getUserId() doit retourner null.
|
|
*/
|
|
public function testGetUserIdWithoutSession(): void
|
|
{
|
|
$this->assertNull($this->manager->getUserId());
|
|
}
|
|
|
|
/**
|
|
* Après setUser(), getUserId() doit retourner l'identifiant correct.
|
|
*/
|
|
public function testGetUserIdAfterSetUser(): void
|
|
{
|
|
$this->manager->setUser(42, 'alice', 'user');
|
|
|
|
$this->assertSame(42, $this->manager->getUserId());
|
|
}
|
|
|
|
|
|
// ── Rôles — isAdmin / isEditor ─────────────────────────────────
|
|
|
|
/**
|
|
* Un utilisateur avec le rôle 'admin' doit être reconnu comme administrateur.
|
|
*/
|
|
public function testIsAdminWithAdminRole(): void
|
|
{
|
|
$this->manager->setUser(1, 'alice', 'admin');
|
|
|
|
$this->assertTrue($this->manager->isAdmin());
|
|
$this->assertFalse($this->manager->isEditor());
|
|
}
|
|
|
|
/**
|
|
* Un utilisateur avec le rôle 'editor' doit être reconnu comme éditeur.
|
|
*/
|
|
public function testIsEditorWithEditorRole(): void
|
|
{
|
|
$this->manager->setUser(1, 'alice', 'editor');
|
|
|
|
$this->assertFalse($this->manager->isAdmin());
|
|
$this->assertTrue($this->manager->isEditor());
|
|
}
|
|
|
|
/**
|
|
* Un utilisateur avec le rôle 'user' ne doit être ni admin ni éditeur.
|
|
*/
|
|
public function testUserRoleIsNeitherAdminNorEditor(): void
|
|
{
|
|
$this->manager->setUser(1, 'alice', 'user');
|
|
|
|
$this->assertFalse($this->manager->isAdmin());
|
|
$this->assertFalse($this->manager->isEditor());
|
|
}
|
|
|
|
/**
|
|
* Sans session active, isAdmin() doit retourner false.
|
|
*/
|
|
public function testIsAdminWithoutSession(): void
|
|
{
|
|
$this->assertFalse($this->manager->isAdmin());
|
|
}
|
|
|
|
/**
|
|
* Sans session active, isEditor() doit retourner false.
|
|
*/
|
|
public function testIsEditorWithoutSession(): void
|
|
{
|
|
$this->assertFalse($this->manager->isEditor());
|
|
}
|
|
|
|
|
|
// ── Données en session ─────────────────────────────────────────
|
|
|
|
/**
|
|
* setUser() doit écrire le username et le rôle dans $_SESSION.
|
|
*/
|
|
public function testSetUserWritesToSession(): void
|
|
{
|
|
$this->manager->setUser(5, 'bob', 'editor');
|
|
|
|
$this->assertSame(5, $_SESSION['user_id']);
|
|
$this->assertSame('bob', $_SESSION['username']);
|
|
$this->assertSame('editor', $_SESSION['role']);
|
|
}
|
|
|
|
|
|
// ── destroy ────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Après destroy(), isAuthenticated() doit retourner false.
|
|
*/
|
|
public function testDestroyClearsSession(): void
|
|
{
|
|
$this->manager->setUser(1, 'alice', 'user');
|
|
$this->manager->destroy();
|
|
|
|
$this->assertFalse($this->manager->isAuthenticated());
|
|
$this->assertNull($this->manager->getUserId());
|
|
$this->assertEmpty($_SESSION);
|
|
}
|
|
}
|