2021-03-31 00:31:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Security\Tests;
|
|
|
|
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\RememberLoginHash;
|
2021-08-24 01:31:06 +02:00
|
|
|
use SilverStripe\SessionManager\Models\LoginSession;
|
2021-03-31 00:31:52 +02:00
|
|
|
|
|
|
|
class RememberLoginHashTest extends SapphireTest
|
|
|
|
{
|
|
|
|
protected static $fixture_file = 'RememberLoginHashTest.yml';
|
|
|
|
|
|
|
|
/** @var RememberLoginHash[] */
|
|
|
|
private $loginHash = [];
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2021-03-31 00:31:52 +02:00
|
|
|
{
|
|
|
|
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
|
2021-10-27 04:39:47 +02:00
|
|
|
* @param mixed $deviceId
|
2021-03-31 00:31:52 +02:00
|
|
|
* @param array $expected
|
|
|
|
* @param array $unexpected
|
|
|
|
* @dataProvider clearScenarios
|
|
|
|
*/
|
2021-10-27 04:39:47 +02:00
|
|
|
public function testClear(bool $logoutAcrossDevices, $deviceId, array $expected, array $unexpected)
|
2021-03-31 00:31:52 +02:00
|
|
|
{
|
2021-08-24 01:31:06 +02:00
|
|
|
// If session-manager module is installed then logout_across_devices is modified so skip
|
|
|
|
if (class_exists(LoginSession::class)) {
|
|
|
|
$this->markTestSkipped();
|
|
|
|
}
|
2021-03-31 00:31:52 +02:00
|
|
|
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"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-04-19 03:13:35 +02:00
|
|
|
|
|
|
|
public function testGetSetLogoutAcrossDevices()
|
|
|
|
{
|
2021-08-24 01:31:06 +02:00
|
|
|
// If session-manager module is installed then logout_across_devices is modified so skip
|
|
|
|
if (class_exists(LoginSession::class)) {
|
|
|
|
$this->markTestSkipped();
|
|
|
|
}
|
2021-04-19 03:13:35 +02:00
|
|
|
// 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());
|
|
|
|
}
|
2021-03-31 00:31:52 +02:00
|
|
|
}
|