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\tests\session;
|
|
|
18 |
|
|
|
19 |
use core\clock;
|
|
|
20 |
use core\di;
|
|
|
21 |
use core\session\database;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Mock handler methods class.
|
|
|
25 |
*
|
|
|
26 |
* @package core
|
|
|
27 |
* @author Darren Cocco <moodle@darren.cocco.id.au>
|
|
|
28 |
* @author Trisha Milan <trishamilan@catalyst-au.net>
|
|
|
29 |
* @copyright 2022 Monash University (http://www.monash.edu)
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
*/
|
|
|
32 |
class mock_handler extends database {
|
|
|
33 |
#[\Override]
|
|
|
34 |
public function init(): bool {
|
|
|
35 |
// Nothing special to do in the mock.
|
|
|
36 |
return true;
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
#[\Override]
|
|
|
40 |
public function session_exists($sid): bool {
|
|
|
41 |
global $DB;
|
|
|
42 |
|
|
|
43 |
return $DB->record_exists('sessions', ['sid' => $sid]);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Insert a new session record to be used in unit tests.
|
|
|
48 |
*
|
|
|
49 |
* @param \stdClass $record
|
|
|
50 |
* @return int Inserted record id.
|
|
|
51 |
*/
|
|
|
52 |
public function add_test_session(\stdClass $record): int {
|
|
|
53 |
global $DB, $USER;
|
|
|
54 |
|
|
|
55 |
$data = new \stdClass();
|
|
|
56 |
$data->state = $record->state ?? 0;
|
|
|
57 |
$data->sid = $record->sid ?? session_id();
|
|
|
58 |
$data->sessdata = $record->sessdata ?? null;
|
|
|
59 |
$data->userid = $record->userid ?? $USER->id;
|
|
|
60 |
$data->timecreated = $record->timecreated ?? di::get(clock::class)->time();
|
|
|
61 |
$data->timemodified = $record->timemodified ?? di::get(clock::class)->time();
|
|
|
62 |
$data->firstip = $record->firstip ?? getremoteaddr();
|
|
|
63 |
$data->lastip = $record->lastip ?? getremoteaddr();
|
|
|
64 |
|
|
|
65 |
return $DB->insert_record('sessions', $data);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
#[\Override]
|
|
|
69 |
public function get_all_sessions(): \Iterator {
|
|
|
70 |
global $DB;
|
|
|
71 |
|
|
|
72 |
$records = $DB->get_records('sessions');
|
|
|
73 |
return new \ArrayIterator($records);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Returns the number of all sessions stored.
|
|
|
78 |
*
|
|
|
79 |
* @return int
|
|
|
80 |
*/
|
|
|
81 |
public function count_sessions(): int {
|
|
|
82 |
global $DB;
|
|
|
83 |
|
|
|
84 |
return $DB->count_records('sessions');
|
|
|
85 |
}
|
|
|
86 |
}
|