MDL-30811 output: Add support for session notifications
[moodle.git] / lib / tests / sessionlib_test.php
CommitLineData
2e00d01d
PS
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 sessionlib.php file.
19 *
20 * @package core
21 * @category phpunit
22 * @author Petr Skoda <petr.skoda@totaralms.com>
23 * @copyright 2014 Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Unit tests for sessionlib.php file.
31 *
32 * @package core
33 * @category phpunit
34 * @author Petr Skoda <petr.skoda@totaralms.com>
35 * @copyright 2014 Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class core_sessionlib_testcase extends advanced_testcase {
39 public function test_cron_setup_user() {
40 global $PAGE, $USER, $SESSION, $SITE, $CFG;
41 $this->resetAfterTest();
42
43 // NOTE: this function contains some static caches, let's reset first.
44 cron_setup_user('reset');
45
46 $admin = get_admin();
47 $user1 = $this->getDataGenerator()->create_user();
48 $user2 = $this->getDataGenerator()->create_user();
49 $course = $this->getDataGenerator()->create_course();
50
51 cron_setup_user();
52 $this->assertSame($admin->id, $USER->id);
53 $this->assertSame($PAGE->context, context_course::instance($SITE->id));
54 $this->assertSame($CFG->timezone, $USER->timezone);
55 $this->assertSame('', $USER->lang);
56 $this->assertSame('', $USER->theme);
57 $SESSION->test1 = true;
58 $adminsession = $SESSION;
59 $adminuser = $USER;
60 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
61 $this->assertSame($GLOBALS['SESSION'], $SESSION);
62 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
63 $this->assertSame($GLOBALS['USER'], $USER);
64
65 cron_setup_user(null, $course);
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->assertSame($user1->id, $USER->id);
76 $this->assertSame($PAGE->context, context_course::instance($SITE->id));
77 $this->assertNotSame($adminsession, $SESSION);
78 $this->assertObjectNotHasAttribute('test1', $SESSION);
0346323c 79 $this->assertCount(1, (array)$SESSION);
2e00d01d
PS
80 $usersession1 = $SESSION;
81 $SESSION->test2 = true;
82 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
83 $this->assertSame($GLOBALS['SESSION'], $SESSION);
84 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
85 $this->assertSame($GLOBALS['USER'], $USER);
86
87 cron_setup_user($user1);
88 $this->assertSame($user1->id, $USER->id);
89 $this->assertSame($PAGE->context, context_course::instance($SITE->id));
90 $this->assertNotSame($adminsession, $SESSION);
91 $this->assertSame($usersession1, $SESSION);
92 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
93 $this->assertSame($GLOBALS['SESSION'], $SESSION);
94 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
95 $this->assertSame($GLOBALS['USER'], $USER);
96
97 cron_setup_user($user2);
98 $this->assertSame($user2->id, $USER->id);
99 $this->assertSame($PAGE->context, context_course::instance($SITE->id));
100 $this->assertNotSame($adminsession, $SESSION);
101 $this->assertNotSame($usersession1, $SESSION);
0346323c 102 $this->assertCount(1, (array)$SESSION);
2e00d01d
PS
103 $usersession2 = $SESSION;
104 $usersession2->test3 = true;
105 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
106 $this->assertSame($GLOBALS['SESSION'], $SESSION);
107 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
108 $this->assertSame($GLOBALS['USER'], $USER);
109
110 cron_setup_user($user2, $course);
111 $this->assertSame($user2->id, $USER->id);
112 $this->assertSame($PAGE->context, context_course::instance($course->id));
113 $this->assertNotSame($adminsession, $SESSION);
114 $this->assertNotSame($usersession1, $SESSION);
115 $this->assertSame($usersession2, $SESSION);
116 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
117 $this->assertSame($GLOBALS['SESSION'], $SESSION);
118 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
119 $this->assertSame($GLOBALS['USER'], $USER);
120
121 cron_setup_user($user1);
122 $this->assertSame($user1->id, $USER->id);
123 $this->assertSame($PAGE->context, context_course::instance($SITE->id));
124 $this->assertNotSame($adminsession, $SESSION);
125 $this->assertNotSame($usersession1, $SESSION);
0346323c 126 $this->assertCount(1, (array)$SESSION);
2e00d01d
PS
127 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
128 $this->assertSame($GLOBALS['SESSION'], $SESSION);
129 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
130 $this->assertSame($GLOBALS['USER'], $USER);
131
132 cron_setup_user();
133 $this->assertSame($admin->id, $USER->id);
134 $this->assertSame($PAGE->context, context_course::instance($SITE->id));
135 $this->assertSame($adminsession, $SESSION);
136 $this->assertSame($adminuser, $USER);
137 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
138 $this->assertSame($GLOBALS['SESSION'], $SESSION);
139 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
140 $this->assertSame($GLOBALS['USER'], $USER);
141
142 cron_setup_user('reset');
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();
149 $this->assertNotSame($adminsession, $SESSION);
150 $this->assertNotSame($adminuser, $USER);
151 $this->assertSame($GLOBALS['SESSION'], $_SESSION['SESSION']);
152 $this->assertSame($GLOBALS['SESSION'], $SESSION);
153 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
154 $this->assertSame($GLOBALS['USER'], $USER);
155 }
156
157 public function test_sesskey() {
158 global $USER;
159 $this->resetAfterTest();
160
161 $user = $this->getDataGenerator()->create_user();
162
163 \core\session\manager::init_empty_session();
164 $this->assertObjectNotHasAttribute('sesskey', $USER);
165
166 $sesskey = sesskey();
167 $this->assertNotEmpty($sesskey);
168 $this->assertSame($sesskey, $USER->sesskey);
169 $this->assertSame($GLOBALS['USER'], $_SESSION['USER']);
170 $this->assertSame($GLOBALS['USER'], $USER);
171
172 $this->assertSame($sesskey, sesskey());
173
174 // Test incomplete session init - the sesskeys should return random values.
175 $_SESSION = array();
176 unset($GLOBALS['USER']);
177 unset($GLOBALS['SESSION']);
178
179 $this->assertFalse(sesskey());
180 $this->assertArrayNotHasKey('USER', $GLOBALS);
181 $this->assertFalse(sesskey());
182 }
183
184 public function test_confirm_sesskey() {
185 $this->resetAfterTest();
186
187 $sesskey = sesskey();
188
189 try {
190 confirm_sesskey();
191 $this->fail('Exception expected when sesskey not present');
192 } catch (moodle_exception $e) {
193 $this->assertSame('missingparam', $e->errorcode);
194 }
195
196 $this->assertTrue(confirm_sesskey($sesskey));
197 $this->assertFalse(confirm_sesskey('blahblah'));
198
199 $_GET['sesskey'] = $sesskey;
200 $this->assertTrue(confirm_sesskey());
201
202 $_GET['sesskey'] = 'blah';
203 $this->assertFalse(confirm_sesskey());
204 }
205
206 public function test_require_sesskey() {
207 $this->resetAfterTest();
208
209 $sesskey = sesskey();
210
211 try {
212 require_sesskey();
213 $this->fail('Exception expected when sesskey not present');
214 } catch (moodle_exception $e) {
215 $this->assertSame('missingparam', $e->errorcode);
216 }
217
218 $_GET['sesskey'] = $sesskey;
219 require_sesskey();
220
221 $_GET['sesskey'] = 'blah';
222 try {
223 require_sesskey();
224 $this->fail('Exception expected when sesskey not incorrect');
225 } catch (moodle_exception $e) {
226 $this->assertSame('invalidsesskey', $e->errorcode);
227 }
228 }
229}