Use app configured session lifetime for garbage collection

This commit is contained in:
Igor Scheller 2023-04-08 00:34:43 +02:00
parent 6da0c8a835
commit 9f35ebc482
2 changed files with 7 additions and 1 deletions

View File

@ -71,6 +71,7 @@ class DatabaseHandler extends AbstractHandler
*/ */
public function gc(int $max_lifetime): int|false public function gc(int $max_lifetime): int|false
{ {
$max_lifetime = config('session.lifetime') * 24 * 60 * 60;
$timestamp = $this->getCurrentTimestamp(-$max_lifetime); $timestamp = $this->getCurrentTimestamp(-$max_lifetime);
return $this->getQuery() return $this->getQuery()

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Engelsystem\Test\Unit\Http\SessionHandlers; namespace Engelsystem\Test\Unit\Http\SessionHandlers;
use Engelsystem\Config\Config;
use Engelsystem\Http\SessionHandlers\DatabaseHandler; use Engelsystem\Http\SessionHandlers\DatabaseHandler;
use Engelsystem\Test\Unit\HasDatabase; use Engelsystem\Test\Unit\HasDatabase;
use Engelsystem\Test\Unit\TestCase; use Engelsystem\Test\Unit\TestCase;
@ -73,12 +74,16 @@ class DatabaseHandlerTest extends TestCase
*/ */
public function testGc(): void public function testGc(): void
{ {
$this->app->instance('config', new Config());
$this->database->insert("INSERT INTO sessions VALUES ('foo', 'Lorem Ipsum', '2000-01-01 01:00')"); $this->database->insert("INSERT INTO sessions VALUES ('foo', 'Lorem Ipsum', '2000-01-01 01:00')");
$this->database->insert("INSERT INTO sessions VALUES ('bar', 'Dolor Sit', '3000-01-01 01:00')"); $this->database->insert("INSERT INTO sessions VALUES ('bar', 'Dolor Sit', '3000-01-01 01:00')");
config(['session.lifetime' => 1]); // 1 day
$handler = new DatabaseHandler($this->database); $handler = new DatabaseHandler($this->database);
$this->assertEquals(1, $handler->gc(60 * 60)); // Max lifetime gets overwritten by settings
$this->assertEquals(1, $handler->gc(0));
$return = $this->database->select('SELECT * FROM sessions'); $return = $this->database->select('SELECT * FROM sessions');
$this->assertCount(1, $return); $this->assertCount(1, $return);