| Línea 23... |
Línea 23... |
| 23 |
* @category test
|
23 |
* @category test
|
| 24 |
* @author Petr Skoda <petr.skoda@totaralms.com>
|
24 |
* @author Petr Skoda <petr.skoda@totaralms.com>
|
| 25 |
* @copyright 2014 Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
|
25 |
* @copyright 2014 Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
|
| 26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
| 27 |
*/
|
27 |
*/
|
| 28 |
class sessionlib_test extends \advanced_testcase {
|
28 |
final class sessionlib_test extends \advanced_testcase {
|
| 29 |
|
- |
|
| 30 |
/**
|
- |
|
| 31 |
* @covers ::cron_setup_user
|
- |
|
| 32 |
*/
|
- |
|
| 33 |
public function test_cron_setup_user(): void {
|
- |
|
| 34 |
// This function uses the $GLOBALS super global. Disable the VariableNameLowerCase sniff for this function.
|
- |
|
| 35 |
// phpcs:disable moodle.NamingConventions.ValidVariableName.VariableNameLowerCase
|
- |
|
| 36 |
|
- |
|
| 37 |
global $PAGE, $USER, $SESSION, $SITE, $CFG;
|
- |
|
| 38 |
$this->resetAfterTest();
|
- |
|
| 39 |
|
- |
|
| 40 |
// NOTE: this function contains some static caches, let's reset first.
|
- |
|
| 41 |
cron_setup_user('reset');
|
- |
|
| 42 |
$this->assertDebuggingCalledCount(1);
|
- |
|
| 43 |
|
- |
|
| 44 |
$admin = get_admin();
|
- |
|
| 45 |
$user1 = $this->getDataGenerator()->create_user();
|
- |
|
| 46 |
$user2 = $this->getDataGenerator()->create_user();
|
- |
|
| 47 |
$course = $this->getDataGenerator()->create_course();
|
- |
|
| 48 |
|
- |
|
| 49 |
cron_setup_user();
|
- |
|
| 50 |
$this->assertDebuggingCalledCount(1);
|
- |
|
| 51 |
$this->assertSame($admin->id, $USER->id);
|
- |
|
| 52 |
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
|
- |
|
| 53 |
$this->assertSame($CFG->timezone, $USER->timezone);
|
- |
|
| 54 |
$this->assertSame('', $USER->lang);
|
- |
|
| 55 |
$this->assertSame('', $USER->theme);
|
- |
|
| 56 |
$SESSION->test1 = true;
|
- |
|
| 57 |
$adminsession = $SESSION;
|
- |
|
| 58 |
$adminuser = $USER;
|
- |
|
| 59 |
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
- |
|
| 60 |
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
- |
|
| 61 |
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
- |
|
| 62 |
$this->assertSame($GLOBALS['USER'], $USER);
|
- |
|
| 63 |
|
- |
|
| 64 |
cron_setup_user(null, $course);
|
- |
|
| 65 |
$this->assertDebuggingCalledCount(1);
|
- |
|
| 66 |
$this->assertSame($admin->id, $USER->id);
|
- |
|
| 67 |
$this->assertSame($PAGE->context, \context_course::instance($course->id));
|
- |
|
| 68 |
$this->assertSame($adminsession, $SESSION);
|
- |
|
| 69 |
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
- |
|
| 70 |
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
- |
|
| 71 |
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
- |
|
| 72 |
$this->assertSame($GLOBALS['USER'], $USER);
|
- |
|
| 73 |
|
- |
|
| 74 |
cron_setup_user($user1);
|
- |
|
| 75 |
$this->assertDebuggingCalledCount(1);
|
- |
|
| 76 |
$this->assertSame($user1->id, $USER->id);
|
- |
|
| 77 |
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
|
- |
|
| 78 |
$this->assertNotSame($adminsession, $SESSION);
|
- |
|
| 79 |
$this->assertObjectNotHasProperty('test1', $SESSION);
|
- |
|
| 80 |
$this->assertEmpty((array)$SESSION);
|
- |
|
| 81 |
$usersession1 = $SESSION;
|
- |
|
| 82 |
$SESSION->test2 = true;
|
- |
|
| 83 |
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
- |
|
| 84 |
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
- |
|
| 85 |
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
- |
|
| 86 |
$this->assertSame($GLOBALS['USER'], $USER);
|
- |
|
| 87 |
|
- |
|
| 88 |
cron_setup_user($user1);
|
- |
|
| 89 |
$this->assertDebuggingCalledCount(1);
|
- |
|
| 90 |
$this->assertSame($user1->id, $USER->id);
|
- |
|
| 91 |
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
|
- |
|
| 92 |
$this->assertNotSame($adminsession, $SESSION);
|
- |
|
| 93 |
$this->assertSame($usersession1, $SESSION);
|
- |
|
| 94 |
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
- |
|
| 95 |
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
- |
|
| 96 |
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
- |
|
| 97 |
$this->assertSame($GLOBALS['USER'], $USER);
|
- |
|
| 98 |
|
- |
|
| 99 |
cron_setup_user($user2);
|
- |
|
| 100 |
$this->assertDebuggingCalledCount(1);
|
- |
|
| 101 |
$this->assertSame($user2->id, $USER->id);
|
- |
|
| 102 |
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
|
- |
|
| 103 |
$this->assertNotSame($adminsession, $SESSION);
|
- |
|
| 104 |
$this->assertNotSame($usersession1, $SESSION);
|
- |
|
| 105 |
$this->assertEmpty((array)$SESSION);
|
- |
|
| 106 |
$usersession2 = $SESSION;
|
- |
|
| 107 |
$usersession2->test3 = true;
|
- |
|
| 108 |
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
- |
|
| 109 |
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
- |
|
| 110 |
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
- |
|
| 111 |
$this->assertSame($GLOBALS['USER'], $USER);
|
- |
|
| 112 |
|
- |
|
| 113 |
cron_setup_user($user2, $course);
|
- |
|
| 114 |
$this->assertDebuggingCalledCount(1);
|
- |
|
| 115 |
$this->assertSame($user2->id, $USER->id);
|
- |
|
| 116 |
$this->assertSame($PAGE->context, \context_course::instance($course->id));
|
- |
|
| 117 |
$this->assertNotSame($adminsession, $SESSION);
|
- |
|
| 118 |
$this->assertNotSame($usersession1, $SESSION);
|
- |
|
| 119 |
$this->assertSame($usersession2, $SESSION);
|
- |
|
| 120 |
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
- |
|
| 121 |
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
- |
|
| 122 |
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
- |
|
| 123 |
$this->assertSame($GLOBALS['USER'], $USER);
|
- |
|
| 124 |
|
- |
|
| 125 |
cron_setup_user($user1);
|
- |
|
| 126 |
$this->assertDebuggingCalledCount(1);
|
- |
|
| 127 |
$this->assertSame($user1->id, $USER->id);
|
- |
|
| 128 |
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
|
- |
|
| 129 |
$this->assertNotSame($adminsession, $SESSION);
|
- |
|
| 130 |
$this->assertNotSame($usersession1, $SESSION);
|
- |
|
| 131 |
$this->assertEmpty((array)$SESSION);
|
- |
|
| 132 |
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
- |
|
| 133 |
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
- |
|
| 134 |
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
- |
|
| 135 |
$this->assertSame($GLOBALS['USER'], $USER);
|
- |
|
| 136 |
|
- |
|
| 137 |
cron_setup_user();
|
- |
|
| 138 |
$this->assertDebuggingCalledCount(1);
|
- |
|
| 139 |
$this->assertSame($admin->id, $USER->id);
|
- |
|
| 140 |
$this->assertSame($PAGE->context, \context_course::instance($SITE->id));
|
- |
|
| 141 |
$this->assertSame($adminsession, $SESSION);
|
- |
|
| 142 |
$this->assertSame($adminuser, $USER);
|
- |
|
| 143 |
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
- |
|
| 144 |
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
- |
|
| 145 |
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
- |
|
| 146 |
$this->assertSame($GLOBALS['USER'], $USER);
|
- |
|
| 147 |
|
- |
|
| 148 |
cron_setup_user('reset');
|
- |
|
| 149 |
$this->assertDebuggingCalledCount(1);
|
- |
|
| 150 |
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
- |
|
| 151 |
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
- |
|
| 152 |
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
- |
|
| 153 |
$this->assertSame($GLOBALS['USER'], $USER);
|
- |
|
| 154 |
|
- |
|
| 155 |
cron_setup_user();
|
- |
|
| 156 |
$this->assertDebuggingCalledCount(1);
|
- |
|
| 157 |
$this->assertNotSame($adminsession, $SESSION);
|
- |
|
| 158 |
$this->assertNotSame($adminuser, $USER);
|
- |
|
| 159 |
$this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
|
- |
|
| 160 |
$this->assertSame($GLOBALS['SESSION'], $SESSION);
|
- |
|
| 161 |
$this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
|
- |
|
| 162 |
$this->assertSame($GLOBALS['USER'], $USER);
|
- |
|
| 163 |
|
- |
|
| 164 |
// phpcs:enable
|
- |
|
| 165 |
}
|
- |
|
| Línea 166... |
Línea 29... |
| 166 |
|
29 |
|
| 167 |
/**
|
30 |
/**
|
| 168 |
* Test provided for secure cookie
|
31 |
* Test provided for secure cookie
|
| 169 |
*
|
32 |
*
|
| 170 |
* @return array of config and secure result
|
33 |
* @return array of config and secure result
|
| 171 |
*/
|
34 |
*/
|
| 172 |
public function moodle_cookie_secure_provider() {
|
35 |
public static function moodle_cookie_secure_provider(): array {
|
| 173 |
return array(
|
36 |
return array(
|
| 174 |
array(
|
37 |
array(
|
| 175 |
// Non ssl, not set.
|
38 |
// Non ssl, not set.
|
| 176 |
'config' => array(
|
39 |
'config' => array(
|