. namespace core\session; use core\tests\session\mock_handler; /** * Unit tests for classes/session/file.php. * * @package core * @copyright Meirza * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * * @runTestsInSeparateProcesses * @covers \core\session\file */ final class file_test extends \advanced_testcase { /** @var file|null $filesession */ private ?file $filesession = null; /** @var mock_handler $mockhandler Dedicated testing handler. */ private mock_handler $mockhandler; #[\Override] public function setUp(): void { global $CFG; parent::setUp(); $this->resetAfterTest(); $this->mockhandler = new mock_handler(); $this->filesession = new file(); $this->filesession->init(); } /** * Test destroy a specific session and delete this session record for this session id. */ public function test_destroy(): void { $sid = md5('sesstest'); $this->add_session($sid); $this->assertTrue($this->filesession->session_exists($sid)); $this->assertTrue(manager::session_exists($sid)); $this->filesession->destroy($sid); $this->assertFalse($this->filesession->session_exists($sid)); $this->assertFalse(manager::session_exists($sid)); } /** * Test destroy all sessions, and delete all the session data. */ public function test_destroy_all(): void { global $DB; $sid1 = md5('sesstest1'); $this->add_session($sid1); $sid2 = md5('sesstest2'); $this->add_session($sid2); $this->assertTrue($this->filesession->session_exists($sid1)); $this->assertTrue($this->filesession->session_exists($sid2)); $this->assertEquals(2, $DB->count_records('sessions')); $this->filesession->destroy_all(); $this->assertFalse($this->filesession->session_exists($sid1)); $this->assertFalse($this->filesession->session_exists($sid2)); $this->assertEquals(0, $DB->count_records('sessions')); } /** * Adds a session with the given session ID. * * @param string $sid The session ID to add. */ private function add_session(string $sid): void { global $CFG; touch("{$CFG->dataroot}/sessions/sess_{$sid}"); $record = new \stdClass(); $record->sid = $sid; $this->mockhandler->add_test_session($record); } }