Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core\session;
18
 
19
use core\tests\session\mock_handler;
20
 
21
/**
22
 * Unit tests for classes/session/file.php.
23
 *
24
 * @package   core
25
 * @copyright Meirza <meirza.arson@gmail.com>
26
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 *
28
 * @runTestsInSeparateProcesses
29
 * @covers \core\session\file
30
 */
31
final class file_test extends \advanced_testcase {
32
    /** @var file|null $filesession */
33
    private ?file $filesession = null;
34
 
35
    /** @var mock_handler $mockhandler Dedicated testing handler. */
36
    private mock_handler $mockhandler;
37
 
38
    #[\Override]
39
    public function setUp(): void {
40
        global $CFG;
41
 
42
        parent::setUp();
43
        $this->resetAfterTest();
44
 
45
        $this->mockhandler = new mock_handler();
46
 
47
        $this->filesession = new file();
48
        $this->filesession->init();
49
    }
50
 
51
    /**
52
     * Test destroy a specific session and delete this session record for this session id.
53
     */
54
    public function test_destroy(): void {
55
        $sid = md5('sesstest');
56
        $this->add_session($sid);
57
 
58
        $this->assertTrue($this->filesession->session_exists($sid));
59
        $this->assertTrue(manager::session_exists($sid));
60
 
61
        $this->filesession->destroy($sid);
62
 
63
        $this->assertFalse($this->filesession->session_exists($sid));
64
        $this->assertFalse(manager::session_exists($sid));
65
    }
66
 
67
    /**
68
     * Test destroy all sessions, and delete all the session data.
69
     */
70
    public function test_destroy_all(): void {
71
        global $DB;
72
 
73
        $sid1 = md5('sesstest1');
74
        $this->add_session($sid1);
75
        $sid2 = md5('sesstest2');
76
        $this->add_session($sid2);
77
 
78
        $this->assertTrue($this->filesession->session_exists($sid1));
79
        $this->assertTrue($this->filesession->session_exists($sid2));
80
        $this->assertEquals(2, $DB->count_records('sessions'));
81
 
82
        $this->filesession->destroy_all();
83
 
84
        $this->assertFalse($this->filesession->session_exists($sid1));
85
        $this->assertFalse($this->filesession->session_exists($sid2));
86
        $this->assertEquals(0, $DB->count_records('sessions'));
87
    }
88
 
89
    /**
90
     * Adds a session with the given session ID.
91
     *
92
     * @param string $sid The session ID to add.
93
     */
94
    private function add_session(string $sid): void {
95
        global $CFG;
96
 
97
        touch("{$CFG->dataroot}/sessions/sess_{$sid}");
98
 
99
        $record = new \stdClass();
100
        $record->sid = $sid;
101
        $this->mockhandler->add_test_session($record);
102
    }
103
}