silverstripe-framework/tests/php/Security/RememberLoginHashTest.php

99 lines
3.5 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Security\Tests;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Security\Member;
use SilverStripe\Security\RememberLoginHash;
use SilverStripe\SessionManager\Models\LoginSession;
class RememberLoginHashTest extends SapphireTest
{
protected static $fixture_file = 'RememberLoginHashTest.yml';
/** @var RememberLoginHash[] */
private $loginHash = [];
protected function setUp(): void
{
parent::setUp();
/** @var Member $main */
$main = $this->objFromFixture(Member::class, 'main');
/** @var Member $secondary */
$secondary = $this->objFromFixture(Member::class, 'secondary');
$this->loginHash = [
'current' => RememberLoginHash::generate($main),
'other' => RememberLoginHash::generate($main),
'secondary' => RememberLoginHash::generate($secondary),
];
}
public function clearScenarios()
{
return [
'logout across devices' => [true, 'current', ['secondary'], ['current', 'other']],
'logout across devices on non-persistent session' => [true, false, ['secondary'], ['current', 'other']],
'logout single device' => [false, 'current', ['secondary', 'other'], ['current']],
'logout single device on non-persistent session' => [false, false, ['secondary', 'current', 'other'], []],
];
}
/**
* @param bool $logoutAcrossDevices
* @param mixed $deviceId
* @param array $expected
* @param array $unexpected
* @dataProvider clearScenarios
*/
public function testClear(bool $logoutAcrossDevices, $deviceId, array $expected, array $unexpected)
{
// If session-manager module is installed then logout_across_devices is modified so skip
if (class_exists(LoginSession::class)) {
$this->markTestSkipped();
}
RememberLoginHash::config()->set('logout_across_devices', $logoutAcrossDevices);
RememberLoginHash::clear(
$this->objFromFixture(Member::class, 'main'),
$deviceId ? $this->loginHash[$deviceId]->DeviceID : null
);
foreach ($expected as $key) {
$ID = $this->loginHash[$key]->ID;
$this->assertNotEmpty(
RememberLoginHash::get()->byID($ID),
"$key $ID RememberLoginHash is found"
);
}
foreach ($unexpected as $key) {
$ID = $this->loginHash[$key]->ID;
$this->assertEmpty(
RememberLoginHash::get()->byID($ID),
"$key RememberLoginHash has been removed"
);
}
}
public function testGetSetLogoutAcrossDevices()
{
// If session-manager module is installed then logout_across_devices is modified so skip
if (class_exists(LoginSession::class)) {
$this->markTestSkipped();
}
// set config directly
RememberLoginHash::config()->set('logout_across_devices', true);
$this->assertTrue(RememberLoginHash::getLogoutAcrossDevices());
RememberLoginHash::config()->set('logout_across_devices', false);
$this->assertFalse(RememberLoginHash::getLogoutAcrossDevices());
// override using public API
RememberLoginHash::setLogoutAcrossDevices(true);
$this->assertTrue(RememberLoginHash::getLogoutAcrossDevices());
RememberLoginHash::setLogoutAcrossDevices(false);
$this->assertFalse(RememberLoginHash::getLogoutAcrossDevices());
}
}