<?php
-namespace Shaarli\Security;
-require_once 'tests/utils/FakeConfigManager.php';
+namespace Shaarli\Security;
-use PHPUnit\Framework\TestCase;
+use Shaarli\TestCase;
/**
* Test coverage for LoginManager
/** @var string Salt used by hash functions */
protected $salt = '669e24fa9c5a59a613f98e8e38327384504a4af2';
+ /** @var CookieManager */
+ protected $cookieManager;
+
/**
* Prepare or reset test resources
*/
- public function setUp()
+ protected function setUp(): void
{
if (file_exists($this->banFile)) {
unlink($this->banFile);
$this->cookie = [];
$this->session = [];
- $this->sessionManager = new SessionManager($this->session, $this->configManager);
- $this->loginManager = new LoginManager($this->configManager, $this->sessionManager);
+ $this->cookieManager = $this->createMock(CookieManager::class);
+ $this->cookieManager->method('getCookieParameter')->willReturnCallback(function (string $key) {
+ return $this->cookie[$key] ?? null;
+ });
+ $this->sessionManager = new SessionManager($this->session, $this->configManager, 'session_path');
+ $this->loginManager = new LoginManager($this->configManager, $this->sessionManager, $this->cookieManager);
$this->server['REMOTE_ADDR'] = $this->ipAddr;
}
$configManager = new \FakeConfigManager([
'resource.ban_file' => $this->banFile,
]);
- $loginManager = new LoginManager($configManager, null);
- $loginManager->checkLoginState([], '');
+ $loginManager = new LoginManager($configManager, null, $this->cookieManager);
+ $loginManager->checkLoginState('');
$this->assertFalse($loginManager->isLoggedIn());
}
'expires_on' => time() + 100,
];
$this->loginManager->generateStaySignedInToken($this->clientIpAddress);
- $this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = 'nope';
+ $this->cookie[CookieManager::STAY_SIGNED_IN] = 'nope';
- $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress);
+ $this->loginManager->checkLoginState($this->clientIpAddress);
$this->assertTrue($this->loginManager->isLoggedIn());
$this->assertTrue(empty($this->session['username']));
public function testCheckLoginStateStaySignedInWithValidToken()
{
$this->loginManager->generateStaySignedInToken($this->clientIpAddress);
- $this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = $this->loginManager->getStaySignedInToken();
+ $this->cookie[CookieManager::STAY_SIGNED_IN] = $this->loginManager->getStaySignedInToken();
- $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress);
+ $this->loginManager->checkLoginState($this->clientIpAddress);
$this->assertTrue($this->loginManager->isLoggedIn());
$this->assertEquals($this->login, $this->session['username']);
$this->loginManager->generateStaySignedInToken($this->clientIpAddress);
$this->session['expires_on'] = time() - 100;
- $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress);
+ $this->loginManager->checkLoginState($this->clientIpAddress);
$this->assertFalse($this->loginManager->isLoggedIn());
}
{
$this->loginManager->generateStaySignedInToken($this->clientIpAddress);
- $this->loginManager->checkLoginState($this->cookie, '10.7.157.98');
+ $this->loginManager->checkLoginState('10.7.157.98');
$this->assertFalse($this->loginManager->isLoggedIn());
}