[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 /** 18 * Unit tests for session manager class. 19 * 20 * @package core 21 * @category phpunit 22 * @copyright 2013 Petr Skoda {@link http://skodak.org} 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 /** 29 * Unit tests for session manager class. 30 * 31 * @package core 32 * @category phpunit 33 * @copyright 2013 Petr Skoda {@link http://skodak.org} 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class core_session_manager_testcase extends advanced_testcase { 37 public function test_start() { 38 $this->resetAfterTest(); 39 // Session must be started only once... 40 \core\session\manager::start(); 41 $this->assertDebuggingCalled('Session was already started!', DEBUG_DEVELOPER); 42 } 43 44 public function test_init_empty_session() { 45 global $SESSION, $USER; 46 $this->resetAfterTest(); 47 48 $user = $this->getDataGenerator()->create_user(); 49 50 $SESSION->test = true; 51 $this->assertTrue($GLOBALS['SESSION']->test); 52 $this->assertTrue($_SESSION['SESSION']->test); 53 54 \core\session\manager::set_user($user); 55 $this->assertSame($user, $USER); 56 $this->assertSame($user, $GLOBALS['USER']); 57 $this->assertSame($user, $_SESSION['USER']); 58 59 \core\session\manager::init_empty_session(); 60 61 $this->assertInstanceOf('stdClass', $SESSION); 62 $this->assertEmpty((array)$SESSION); 63 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']); 64 $this->assertSame($GLOBALS['SESSION'], $SESSION); 65 66 $this->assertInstanceOf('stdClass', $USER); 67 $this->assertEquals(array('id' => 0, 'mnethostid' => 1), (array)$USER, '', 0, 10, true); 68 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 69 $this->assertSame($GLOBALS['USER'], $USER); 70 71 // Now test how references work. 72 73 $GLOBALS['SESSION'] = new \stdClass(); 74 $GLOBALS['SESSION']->test = true; 75 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']); 76 $this->assertSame($GLOBALS['SESSION'], $SESSION); 77 78 $SESSION = new \stdClass(); 79 $SESSION->test2 = true; 80 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']); 81 $this->assertSame($GLOBALS['SESSION'], $SESSION); 82 83 $_SESSION['SESSION'] = new stdClass(); 84 $_SESSION['SESSION']->test3 = true; 85 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']); 86 $this->assertSame($GLOBALS['SESSION'], $SESSION); 87 88 $GLOBALS['USER'] = new \stdClass(); 89 $GLOBALS['USER']->test = true; 90 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 91 $this->assertSame($GLOBALS['USER'], $USER); 92 93 $USER = new \stdClass(); 94 $USER->test2 = true; 95 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 96 $this->assertSame($GLOBALS['USER'], $USER); 97 98 $_SESSION['USER'] = new stdClass(); 99 $_SESSION['USER']->test3 = true; 100 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 101 $this->assertSame($GLOBALS['USER'], $USER); 102 } 103 104 public function test_set_user() { 105 global $USER; 106 $this->resetAfterTest(); 107 108 $this->assertEquals(0, $USER->id); 109 110 $user = $this->getDataGenerator()->create_user(); 111 $this->assertObjectHasAttribute('description', $user); 112 $this->assertObjectHasAttribute('password', $user); 113 114 \core\session\manager::set_user($user); 115 116 $this->assertEquals($user->id, $USER->id); 117 $this->assertObjectNotHasAttribute('description', $user); 118 $this->assertObjectNotHasAttribute('password', $user); 119 $this->assertObjectHasAttribute('sesskey', $user); 120 $this->assertSame($user, $GLOBALS['USER']); 121 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 122 $this->assertSame($GLOBALS['USER'], $USER); 123 } 124 125 public function test_login_user() { 126 global $USER; 127 $this->resetAfterTest(); 128 129 $this->assertEquals(0, $USER->id); 130 131 $user = $this->getDataGenerator()->create_user(); 132 133 @\core\session\manager::login_user($user); // Ignore header error messages. 134 $this->assertEquals($user->id, $USER->id); 135 136 $this->assertObjectNotHasAttribute('description', $user); 137 $this->assertObjectNotHasAttribute('password', $user); 138 $this->assertSame($user, $GLOBALS['USER']); 139 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 140 $this->assertSame($GLOBALS['USER'], $USER); 141 } 142 143 public function test_terminate_current() { 144 global $USER, $SESSION; 145 $this->resetAfterTest(); 146 147 $this->setAdminUser(); 148 \core\session\manager::terminate_current(); 149 $this->assertEquals(0, $USER->id); 150 151 $this->assertInstanceOf('stdClass', $SESSION); 152 $this->assertEmpty((array)$SESSION); 153 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']); 154 $this->assertSame($GLOBALS['SESSION'], $SESSION); 155 156 $this->assertInstanceOf('stdClass', $USER); 157 $this->assertEquals(array('id' => 0, 'mnethostid' => 1), (array)$USER, '', 0, 10, true); 158 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 159 $this->assertSame($GLOBALS['USER'], $USER); 160 } 161 162 public function test_write_close() { 163 global $USER; 164 $this->resetAfterTest(); 165 166 // Just make sure no errors and $USER->id is kept 167 $this->setAdminUser(); 168 $userid = $USER->id; 169 \core\session\manager::write_close(); 170 $this->assertSame($userid, $USER->id); 171 172 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 173 $this->assertSame($GLOBALS['USER'], $USER); 174 } 175 176 public function test_session_exists() { 177 global $CFG, $DB; 178 $this->resetAfterTest(); 179 180 $this->assertFalse(\core\session\manager::session_exists('abc')); 181 182 $user = $this->getDataGenerator()->create_user(); 183 $guest = guest_user(); 184 185 // The file handler is used by default, so let's fake the data somehow. 186 $sid = md5('hokus'); 187 mkdir("$CFG->dataroot/sessions/", $CFG->directorypermissions, true); 188 touch("$CFG->dataroot/sessions/sess_$sid"); 189 190 $this->assertFalse(\core\session\manager::session_exists($sid)); 191 192 $record = new stdClass(); 193 $record->userid = 0; 194 $record->sid = $sid; 195 $record->timecreated = time(); 196 $record->timemodified = $record->timecreated; 197 $record->id = $DB->insert_record('sessions', $record); 198 199 $this->assertTrue(\core\session\manager::session_exists($sid)); 200 201 $record->timecreated = time() - $CFG->sessiontimeout - 100; 202 $record->timemodified = $record->timecreated + 10; 203 $DB->update_record('sessions', $record); 204 205 $this->assertTrue(\core\session\manager::session_exists($sid)); 206 207 $record->userid = $guest->id; 208 $DB->update_record('sessions', $record); 209 210 $this->assertTrue(\core\session\manager::session_exists($sid)); 211 212 $record->userid = $user->id; 213 $DB->update_record('sessions', $record); 214 215 $this->assertFalse(\core\session\manager::session_exists($sid)); 216 217 $CFG->sessiontimeout = $CFG->sessiontimeout + 3000; 218 219 $this->assertTrue(\core\session\manager::session_exists($sid)); 220 } 221 222 public function test_touch_session() { 223 global $DB; 224 $this->resetAfterTest(); 225 226 $sid = md5('hokus'); 227 $record = new \stdClass(); 228 $record->state = 0; 229 $record->sid = $sid; 230 $record->sessdata = null; 231 $record->userid = 2; 232 $record->timecreated = time() - 60*60; 233 $record->timemodified = time() - 30; 234 $record->firstip = $record->lastip = '10.0.0.1'; 235 $record->id = $DB->insert_record('sessions', $record); 236 237 $now = time(); 238 \core\session\manager::touch_session($sid); 239 $updated = $DB->get_field('sessions', 'timemodified', array('id'=>$record->id)); 240 241 $this->assertGreaterThanOrEqual($now, $updated); 242 $this->assertLessThanOrEqual(time(), $updated); 243 } 244 245 public function test_kill_session() { 246 global $DB, $USER; 247 $this->resetAfterTest(); 248 249 $this->setAdminUser(); 250 $userid = $USER->id; 251 252 $sid = md5('hokus'); 253 $record = new \stdClass(); 254 $record->state = 0; 255 $record->sid = $sid; 256 $record->sessdata = null; 257 $record->userid = $userid; 258 $record->timecreated = time() - 60*60; 259 $record->timemodified = time() - 30; 260 $record->firstip = $record->lastip = '10.0.0.1'; 261 $DB->insert_record('sessions', $record); 262 263 $record->userid = 0; 264 $record->sid = md5('pokus'); 265 $DB->insert_record('sessions', $record); 266 267 $this->assertEquals(2, $DB->count_records('sessions')); 268 269 \core\session\manager::kill_session($sid); 270 271 $this->assertEquals(1, $DB->count_records('sessions')); 272 $this->assertFalse($DB->record_exists('sessions', array('sid'=>$sid))); 273 274 $this->assertSame($userid, $USER->id); 275 } 276 277 public function test_kill_user_sessions() { 278 global $DB, $USER; 279 $this->resetAfterTest(); 280 281 $this->setAdminUser(); 282 $userid = $USER->id; 283 284 $sid = md5('hokus'); 285 $record = new \stdClass(); 286 $record->state = 0; 287 $record->sid = $sid; 288 $record->sessdata = null; 289 $record->userid = $userid; 290 $record->timecreated = time() - 60*60; 291 $record->timemodified = time() - 30; 292 $record->firstip = $record->lastip = '10.0.0.1'; 293 $DB->insert_record('sessions', $record); 294 295 $record->sid = md5('hokus2'); 296 $DB->insert_record('sessions', $record); 297 298 $record->userid = 0; 299 $record->sid = md5('pokus'); 300 $DB->insert_record('sessions', $record); 301 302 $this->assertEquals(3, $DB->count_records('sessions')); 303 304 \core\session\manager::kill_user_sessions($userid); 305 306 $this->assertEquals(1, $DB->count_records('sessions')); 307 $this->assertFalse($DB->record_exists('sessions', array('userid'=>$userid))); 308 } 309 310 public function test_kill_all_sessions() { 311 global $DB, $USER; 312 $this->resetAfterTest(); 313 314 $this->setAdminUser(); 315 $userid = $USER->id; 316 317 $sid = md5('hokus'); 318 $record = new \stdClass(); 319 $record->state = 0; 320 $record->sid = $sid; 321 $record->sessdata = null; 322 $record->userid = $userid; 323 $record->timecreated = time() - 60*60; 324 $record->timemodified = time() - 30; 325 $record->firstip = $record->lastip = '10.0.0.1'; 326 $DB->insert_record('sessions', $record); 327 328 $record->sid = md5('hokus2'); 329 $DB->insert_record('sessions', $record); 330 331 $record->userid = 0; 332 $record->sid = md5('pokus'); 333 $DB->insert_record('sessions', $record); 334 335 $this->assertEquals(3, $DB->count_records('sessions')); 336 337 \core\session\manager::kill_all_sessions(); 338 339 $this->assertEquals(0, $DB->count_records('sessions')); 340 $this->assertSame(0, $USER->id); 341 } 342 343 public function test_gc() { 344 global $CFG, $DB, $USER; 345 $this->resetAfterTest(); 346 347 $this->setAdminUser(); 348 $adminid = $USER->id; 349 $this->setGuestUser(); 350 $guestid = $USER->id; 351 $this->setUser(0); 352 353 $CFG->sessiontimeout = 60*10; 354 355 $record = new \stdClass(); 356 $record->state = 0; 357 $record->sid = md5('hokus1'); 358 $record->sessdata = null; 359 $record->userid = $adminid; 360 $record->timecreated = time() - 60*60; 361 $record->timemodified = time() - 30; 362 $record->firstip = $record->lastip = '10.0.0.1'; 363 $r1 = $DB->insert_record('sessions', $record); 364 365 $record->sid = md5('hokus2'); 366 $record->userid = $adminid; 367 $record->timecreated = time() - 60*60; 368 $record->timemodified = time() - 60*20; 369 $r2 = $DB->insert_record('sessions', $record); 370 371 $record->sid = md5('hokus3'); 372 $record->userid = $guestid; 373 $record->timecreated = time() - 60*60*60; 374 $record->timemodified = time() - 60*20; 375 $r3 = $DB->insert_record('sessions', $record); 376 377 $record->sid = md5('hokus4'); 378 $record->userid = $guestid; 379 $record->timecreated = time() - 60*60*60; 380 $record->timemodified = time() - 60*10*5 - 60; 381 $r4 = $DB->insert_record('sessions', $record); 382 383 $record->sid = md5('hokus5'); 384 $record->userid = 0; 385 $record->timecreated = time() - 60*5; 386 $record->timemodified = time() - 60*5; 387 $r5 = $DB->insert_record('sessions', $record); 388 389 $record->sid = md5('hokus6'); 390 $record->userid = 0; 391 $record->timecreated = time() - 60*60; 392 $record->timemodified = time() - 60*10 -10; 393 $r6 = $DB->insert_record('sessions', $record); 394 395 $record->sid = md5('hokus7'); 396 $record->userid = 0; 397 $record->timecreated = time() - 60*60; 398 $record->timemodified = time() - 60*9; 399 $r7 = $DB->insert_record('sessions', $record); 400 401 \core\session\manager::gc(); 402 403 $this->assertTrue($DB->record_exists('sessions', array('id'=>$r1))); 404 $this->assertFalse($DB->record_exists('sessions', array('id'=>$r2))); 405 $this->assertTrue($DB->record_exists('sessions', array('id'=>$r3))); 406 $this->assertFalse($DB->record_exists('sessions', array('id'=>$r4))); 407 $this->assertFalse($DB->record_exists('sessions', array('id'=>$r5))); 408 $this->assertFalse($DB->record_exists('sessions', array('id'=>$r6))); 409 $this->assertTrue($DB->record_exists('sessions', array('id'=>$r7))); 410 } 411 412 /** 413 * Test loginas. 414 * @copyright 2103 Rajesh Taneja <[email protected]> 415 */ 416 public function test_loginas() { 417 global $USER, $SESSION; 418 $this->resetAfterTest(); 419 420 // Set current user as Admin user and save it for later use. 421 $this->setAdminUser(); 422 $adminuser = $USER; 423 $adminsession = $SESSION; 424 $user = $this->getDataGenerator()->create_user(); 425 $_SESSION['extra'] = true; 426 427 // Try admin loginas this user in system context. 428 $this->assertObjectNotHasAttribute('realuser', $USER); 429 \core\session\manager::loginas($user->id, context_system::instance()); 430 431 $this->assertSame($user->id, $USER->id); 432 $this->assertSame(context_system::instance(), $USER->loginascontext); 433 $this->assertSame($adminuser->id, $USER->realuser); 434 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']); 435 $this->assertSame($GLOBALS['USER'], $USER); 436 $this->assertNotSame($adminuser, $_SESSION['REALUSER']); 437 $this->assertEquals($adminuser, $_SESSION['REALUSER']); 438 439 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']); 440 $this->assertSame($GLOBALS['SESSION'], $SESSION); 441 $this->assertNotSame($adminsession, $_SESSION['REALSESSION']); 442 $this->assertEquals($adminsession, $_SESSION['REALSESSION']); 443 444 $this->assertArrayNotHasKey('extra', $_SESSION); 445 446 // Set user as current user and login as admin user in course context. 447 \core\session\manager::init_empty_session(); 448 $this->setUser($user); 449 $this->assertNotEquals($adminuser->id, $USER->id); 450 $course = $this->getDataGenerator()->create_course(); 451 $coursecontext = context_course::instance($course->id); 452 453 // Catch event triggered. 454 $sink = $this->redirectEvents(); 455 \core\session\manager::loginas($adminuser->id, $coursecontext); 456 $events = $sink->get_events(); 457 $sink->close(); 458 $event = array_pop($events); 459 460 $this->assertSame($adminuser->id, $USER->id); 461 $this->assertSame($coursecontext, $USER->loginascontext); 462 $this->assertSame($user->id, $USER->realuser); 463 464 // Test event captured has proper information. 465 $this->assertInstanceOf('\core\event\user_loggedinas', $event); 466 $this->assertSame($user->id, $event->objectid); 467 $this->assertSame($adminuser->id, $event->relateduserid); 468 $this->assertSame($course->id, $event->courseid); 469 $this->assertEquals($coursecontext, $event->get_context()); 470 $oldfullname = fullname($user, true); 471 $newfullname = fullname($adminuser, true); 472 $expectedlogdata = array($course->id, "course", "loginas", "../user/view.php?id=$course->id&user=$user->id", "$oldfullname -> $newfullname"); 473 $this->assertEventLegacyLogData($expectedlogdata, $event); 474 } 475 476 public function test_is_loggedinas() { 477 $this->resetAfterTest(); 478 479 $user1 = $this->getDataGenerator()->create_user(); 480 $user2 = $this->getDataGenerator()->create_user(); 481 482 $this->assertFalse(\core\session\manager::is_loggedinas()); 483 484 $this->setUser($user1); 485 \core\session\manager::loginas($user2->id, context_system::instance()); 486 487 $this->assertTrue(\core\session\manager::is_loggedinas()); 488 } 489 490 public function test_get_realuser() { 491 $this->resetAfterTest(); 492 493 $user1 = $this->getDataGenerator()->create_user(); 494 $user2 = $this->getDataGenerator()->create_user(); 495 496 $this->setUser($user1); 497 $normal = \core\session\manager::get_realuser(); 498 $this->assertSame($GLOBALS['USER'], $normal); 499 500 \core\session\manager::loginas($user2->id, context_system::instance()); 501 502 $real = \core\session\manager::get_realuser(); 503 504 unset($real->password); 505 unset($real->description); 506 unset($real->sesskey); 507 unset($user1->password); 508 unset($user1->description); 509 unset($user1->sesskey); 510 511 $this->assertEquals($real, $user1); 512 $this->assertSame($_SESSION['REALUSER'], $real); 513 } 514 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |