MDL-59599 navigation: Respect previous activity-level navigation
[moodle.git] / lib / tests / session_redis_test.php
blob8b32e759fed165efed6f8a0df231910acfd78ad1
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.
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/>.
17 /**
18 * Redis session tests.
20 * NOTE: in order to execute this test you need to set up
21 * Redis server and add configuration a constant
22 * to config.php or phpunit.xml configuration file:
24 * define('TEST_SESSION_REDIS_HOST', '127.0.0.1');
26 * @package core
27 * @author Russell Smith <mr-russ@smith2001.net>
28 * @copyright 2016 Russell Smith
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 defined('MOODLE_INTERNAL') || die();
34 /**
35 * Unit tests for classes/session/redis.php.
37 * @package core
38 * @author Russell Smith <mr-russ@smith2001.net>
39 * @copyright 2016 Russell Smith
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class core_session_redis_testcase extends advanced_testcase {
44 /** @var $keyprefix This key prefix used when testing Redis */
45 protected $keyprefix = null;
46 /** @var $redis The current testing redis connection */
47 protected $redis = null;
49 public function setUp() {
50 global $CFG;
52 if (!extension_loaded('redis')) {
53 $this->markTestSkipped('Redis extension not loaded.');
55 if (!defined('TEST_SESSION_REDIS_HOST')) {
56 $this->markTestSkipped('Session test server not set. define: TEST_SESSION_REDIS_HOST');
58 $version = phpversion('Redis');
59 if (!$version) {
60 $this->markTestSkipped('Redis extension version missing');
61 } else if (version_compare($version, '2.0') <= 0) {
62 $this->markTestSkipped('Redis extension version must be at least 2.0: now running "' . $version . '"');
65 $this->resetAfterTest();
67 $this->keyprefix = 'phpunit'.rand(1, 100000);
69 $CFG->session_redis_host = TEST_SESSION_REDIS_HOST;
70 $CFG->session_redis_prefix = $this->keyprefix;
72 // Set a very short lock timeout to ensure tests run quickly. We are running single threaded,
73 // so unless we lock and expect it to be there, we will always see a lock.
74 $CFG->session_redis_acquire_lock_timeout = 1;
75 $CFG->session_redis_lock_expire = 70;
77 $this->redis = new Redis();
78 $this->redis->connect(TEST_SESSION_REDIS_HOST);
81 public function tearDown() {
82 if (!extension_loaded('redis') || !defined('TEST_SESSION_REDIS_HOST')) {
83 return;
86 $list = $this->redis->keys($this->keyprefix.'*');
87 foreach ($list as $keyname) {
88 $this->redis->del($keyname);
90 $this->redis->close();
93 public function test_normal_session_start_stop_works() {
94 $sess = new \core\session\redis();
95 $sess->init();
96 $this->assertTrue($sess->handler_open('Not used', 'Not used'));
97 $this->assertSame('', $sess->handler_read('sess1'));
98 $this->assertTrue($sess->handler_write('sess1', 'DATA'));
99 $this->assertTrue($sess->handler_close());
101 // Read the session again to ensure locking did what it should.
102 $this->assertTrue($sess->handler_open('Not used', 'Not used'));
103 $this->assertSame('DATA', $sess->handler_read('sess1'));
104 $this->assertTrue($sess->handler_write('sess1', 'DATA-new'));
105 $this->assertTrue($sess->handler_close());
106 $this->assertSessionNoLocks();
109 public function test_session_blocks_with_existing_session() {
110 $sess = new \core\session\redis();
111 $sess->init();
112 $this->assertTrue($sess->handler_open('Not used', 'Not used'));
113 $this->assertSame('', $sess->handler_read('sess1'));
114 $this->assertTrue($sess->handler_write('sess1', 'DATA'));
115 $this->assertTrue($sess->handler_close());
117 // Sessions are not locked until they have been saved once.
118 $this->assertTrue($sess->handler_open('Not used', 'Not used'));
119 $this->assertSame('DATA', $sess->handler_read('sess1'));
121 $sessblocked = new \core\session\redis();
122 $sessblocked->init();
123 $this->assertTrue($sessblocked->handler_open('Not used', 'Not used'));
125 // Trap the error log and send it to stdOut so we can expect output at the right times.
126 $errorlog = tempnam(sys_get_temp_dir(), "rediserrorlog");
127 $this->iniSet('error_log', $errorlog);
128 try {
129 $sessblocked->handler_read('sess1');
130 $this->fail('Session lock must fail to be obtained.');
131 } catch (\core\session\exception $e) {
132 $this->assertContains("Unable to obtain session lock", $e->getMessage());
133 $this->assertContains('Cannot obtain session lock for sid: sess1', file_get_contents($errorlog));
136 $this->assertTrue($sessblocked->handler_close());
137 $this->assertTrue($sess->handler_write('sess1', 'DATA-new'));
138 $this->assertTrue($sess->handler_close());
139 $this->assertSessionNoLocks();
142 public function test_session_is_destroyed_when_it_does_not_exist() {
143 $sess = new \core\session\redis();
144 $sess->init();
145 $this->assertTrue($sess->handler_open('Not used', 'Not used'));
146 $this->assertTrue($sess->handler_destroy('sess-destroy'));
147 $this->assertSessionNoLocks();
150 public function test_session_is_destroyed_when_we_have_it_open() {
151 $sess = new \core\session\redis();
152 $sess->init();
153 $this->assertTrue($sess->handler_open('Not used', 'Not used'));
154 $this->assertSame('', $sess->handler_read('sess-destroy'));
155 $this->assertTrue($sess->handler_destroy('sess-destroy'));
156 $this->assertTrue($sess->handler_close());
157 $this->assertSessionNoLocks();
160 public function test_multiple_sessions_do_not_interfere_with_each_other() {
161 $sess1 = new \core\session\redis();
162 $sess1->init();
163 $sess2 = new \core\session\redis();
164 $sess2->init();
166 // Initialize session 1.
167 $this->assertTrue($sess1->handler_open('Not used', 'Not used'));
168 $this->assertSame('', $sess1->handler_read('sess1'));
169 $this->assertTrue($sess1->handler_write('sess1', 'DATA'));
170 $this->assertTrue($sess1->handler_close());
172 // Initialize session 2.
173 $this->assertTrue($sess2->handler_open('Not used', 'Not used'));
174 $this->assertSame('', $sess2->handler_read('sess2'));
175 $this->assertTrue($sess2->handler_write('sess2', 'DATA2'));
176 $this->assertTrue($sess2->handler_close());
178 // Open and read session 1 and 2.
179 $this->assertTrue($sess1->handler_open('Not used', 'Not used'));
180 $this->assertSame('DATA', $sess1->handler_read('sess1'));
181 $this->assertTrue($sess2->handler_open('Not used', 'Not used'));
182 $this->assertSame('DATA2', $sess2->handler_read('sess2'));
184 // Write both sessions.
185 $this->assertTrue($sess1->handler_write('sess1', 'DATAX'));
186 $this->assertTrue($sess2->handler_write('sess2', 'DATA2X'));
188 // Read both sessions.
189 $this->assertTrue($sess1->handler_open('Not used', 'Not used'));
190 $this->assertTrue($sess2->handler_open('Not used', 'Not used'));
191 $this->assertEquals('DATAX', $sess1->handler_read('sess1'));
192 $this->assertEquals('DATA2X', $sess2->handler_read('sess2'));
194 // Close both sessions
195 $this->assertTrue($sess1->handler_close());
196 $this->assertTrue($sess2->handler_close());
198 // Read the session again to ensure locking did what it should.
199 $this->assertSessionNoLocks();
202 public function test_multiple_sessions_work_with_a_single_instance() {
203 $sess = new \core\session\redis();
204 $sess->init();
206 // Initialize session 1.
207 $this->assertTrue($sess->handler_open('Not used', 'Not used'));
208 $this->assertSame('', $sess->handler_read('sess1'));
209 $this->assertTrue($sess->handler_write('sess1', 'DATA'));
210 $this->assertSame('', $sess->handler_read('sess2'));
211 $this->assertTrue($sess->handler_write('sess2', 'DATA2'));
212 $this->assertSame('DATA', $sess->handler_read('sess1'));
213 $this->assertSame('DATA2', $sess->handler_read('sess2'));
214 $this->assertTrue($sess->handler_destroy('sess2'));
216 $this->assertTrue($sess->handler_close());
217 $this->assertSessionNoLocks();
219 $this->assertTrue($sess->handler_close());
222 public function test_session_exists_returns_valid_values() {
223 $sess = new \core\session\redis();
224 $sess->init();
226 $this->assertTrue($sess->handler_open('Not used', 'Not used'));
227 $this->assertSame('', $sess->handler_read('sess1'));
229 $this->assertFalse($sess->session_exists('sess1'), 'Session must not exist yet, it has not been saved');
230 $this->assertTrue($sess->handler_write('sess1', 'DATA'));
231 $this->assertTrue($sess->session_exists('sess1'), 'Session must exist now.');
232 $this->assertTrue($sess->handler_destroy('sess1'));
233 $this->assertFalse($sess->session_exists('sess1'), 'Session should be destroyed.');
236 public function test_kill_sessions_removes_the_session_from_redis() {
237 global $DB;
239 $sess = new \core\session\redis();
240 $sess->init();
242 $this->assertTrue($sess->handler_open('Not used', 'Not used'));
243 $this->assertTrue($sess->handler_write('sess1', 'DATA'));
244 $this->assertTrue($sess->handler_write('sess2', 'DATA'));
245 $this->assertTrue($sess->handler_write('sess3', 'DATA'));
247 $sessiondata = new \stdClass();
248 $sessiondata->userid = 2;
249 $sessiondata->timecreated = time();
250 $sessiondata->timemodified = time();
252 $sessiondata->sid = 'sess1';
253 $DB->insert_record('sessions', $sessiondata);
254 $sessiondata->sid = 'sess2';
255 $DB->insert_record('sessions', $sessiondata);
256 $sessiondata->sid = 'sess3';
257 $DB->insert_record('sessions', $sessiondata);
259 $this->assertNotEquals('', $sess->handler_read('sess1'));
260 $sess->kill_session('sess1');
261 $this->assertEquals('', $sess->handler_read('sess1'));
263 $this->assertEmpty($this->redis->keys($this->keyprefix.'sess1.lock'));
265 $sess->kill_all_sessions();
267 $this->assertEquals(3, $DB->count_records('sessions'), 'Moodle handles session database, plugin must not change it.');
268 $this->assertSessionNoLocks();
269 $this->assertEmpty($this->redis->keys($this->keyprefix.'*'), 'There should be no session data left.');
273 * Assert that we don't have any session locks in Redis.
275 protected function assertSessionNoLocks() {
276 $this->assertEmpty($this->redis->keys($this->keyprefix.'*.lock'));