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/memcached.php.
|
|
|
23 |
*
|
|
|
24 |
* NOTE: in order to execute this test you need to set up
|
|
|
25 |
* Memcached server and add configuration a constant
|
|
|
26 |
* to config.php or phpunit.xml configuration file:
|
|
|
27 |
*
|
|
|
28 |
* define('TEST_SESSION_MEMCACHED_SERVER', 'localhost:11211');
|
|
|
29 |
* define('TEST_SESSION_MEMCACHED_PREFIX', 'memc.sess.key.');
|
|
|
30 |
*
|
|
|
31 |
* The 'TEST_SESSION_MEMCACHED_PREFIX' is optional and if not set the default value will be used.
|
|
|
32 |
*
|
|
|
33 |
* @package core
|
|
|
34 |
* @copyright Meirza <meirza.arson@gmail.com>
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
*
|
|
|
37 |
* @runTestsInSeparateProcesses
|
|
|
38 |
* @covers core\session\memcached
|
|
|
39 |
*/
|
|
|
40 |
final class memcached_test extends \advanced_testcase {
|
|
|
41 |
/** @var memcached|null $memcachedession An instance of the memcached session or null if not initialized. */
|
|
|
42 |
private ?memcached $memcachedession = null;
|
|
|
43 |
|
|
|
44 |
/** @var \Memcached $memcached An instance of the Memcached class used for handling session storage. */
|
|
|
45 |
private \Memcached $memcached;
|
|
|
46 |
|
|
|
47 |
/** @var mock_handler $mockhandler Dedicated testing handler. */
|
|
|
48 |
private mock_handler $mockhandler;
|
|
|
49 |
|
|
|
50 |
/** @var string $keyprefix The prefix used for keys in the Memcached session storage. */
|
|
|
51 |
private string $keyprefix = 'memc.sess.key.';
|
|
|
52 |
|
|
|
53 |
#[\Override]
|
|
|
54 |
public function setUp(): void {
|
|
|
55 |
global $CFG;
|
|
|
56 |
parent::setUp();
|
|
|
57 |
$this->resetAfterTest();
|
|
|
58 |
|
|
|
59 |
if (!extension_loaded('memcached')) {
|
|
|
60 |
$this->markTestSkipped('memcached extension is not loaded');
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
$version = phpversion('memcached');
|
|
|
64 |
if (!$version || version_compare($version, '2.0') < 0) {
|
|
|
65 |
$this->markTestSkipped('memcached extension version must be at least 2.0');
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
if (!defined('TEST_SESSION_MEMCACHED_SERVER')) {
|
|
|
69 |
$this->markTestSkipped('Session test server not set. define: TEST_SESSION_MEMCACHED_SERVER');
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
if (defined('TEST_SESSION_MEMCACHED_PREFIX')) {
|
|
|
73 |
$this->keyprefix = TEST_SESSION_MEMCACHED_PREFIX;
|
|
|
74 |
$CFG->session_memcached_prefix = TEST_SESSION_MEMCACHED_PREFIX;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
$this->mockhandler = new mock_handler();
|
|
|
78 |
|
|
|
79 |
$CFG->session_memcached_save_path = TEST_SESSION_MEMCACHED_SERVER;
|
|
|
80 |
$this->memcachedession = new memcached();
|
|
|
81 |
$this->memcachedession->init();
|
|
|
82 |
|
|
|
83 |
[$host, $port] = explode(':', TEST_SESSION_MEMCACHED_SERVER);
|
|
|
84 |
$this->memcached = new \Memcached();
|
|
|
85 |
$this->memcached->addServer($host, $port);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
#[\Override]
|
|
|
89 |
public function tearDown(): void {
|
|
|
90 |
$this->memcached->quit();
|
|
|
91 |
parent::tearDown();
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Test the destruction of a session.
|
|
|
96 |
*/
|
|
|
97 |
public function test_destroy(): void {
|
|
|
98 |
$sid = $this->add_session('sesstest');
|
|
|
99 |
|
|
|
100 |
$this->assertTrue($this->memcachedession->session_exists($sid));
|
|
|
101 |
$this->assertTrue(manager::session_exists($sid));
|
|
|
102 |
|
|
|
103 |
$this->memcachedession->destroy($sid);
|
|
|
104 |
|
|
|
105 |
$this->assertFalse($this->memcachedession->session_exists($sid));
|
|
|
106 |
$this->assertFalse(manager::session_exists($sid));
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Test the destruction of all sessions.
|
|
|
111 |
*/
|
|
|
112 |
public function test_destroy_all(): void {
|
|
|
113 |
global $DB;
|
|
|
114 |
|
|
|
115 |
$sid1 = $this->add_session('sesstest1');
|
|
|
116 |
$sid2 = $this->add_session('sesstest2');
|
|
|
117 |
|
|
|
118 |
$this->assertTrue($this->memcachedession->session_exists($sid1));
|
|
|
119 |
$this->assertTrue($this->memcachedession->session_exists($sid2));
|
|
|
120 |
$this->assertEquals(2, $DB->count_records('sessions'));
|
|
|
121 |
|
|
|
122 |
$this->memcachedession->destroy_all();
|
|
|
123 |
|
|
|
124 |
$this->assertFalse($this->memcachedession->session_exists($sid1));
|
|
|
125 |
$this->assertFalse($this->memcachedession->session_exists($sid2));
|
|
|
126 |
$this->assertEquals(0, $DB->count_records('sessions'));
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Adds a session with the given session ID.
|
|
|
131 |
*
|
|
|
132 |
* @param string $sid The session ID to add.
|
|
|
133 |
* @return string The result of adding the session.
|
|
|
134 |
*/
|
|
|
135 |
private function add_session(string $sid): string {
|
|
|
136 |
$sid = md5($sid);
|
|
|
137 |
$this->memcached->set($this->keyprefix . $sid, 'abc');
|
|
|
138 |
|
|
|
139 |
$record = new \stdClass();
|
|
|
140 |
$record->sid = $sid;
|
|
|
141 |
$this->mockhandler->add_test_session($record);
|
|
|
142 |
|
|
|
143 |
return $sid;
|
|
|
144 |
}
|
|
|
145 |
}
|