1 |
efrain |
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 sitepolicy manager
|
|
|
19 |
*
|
|
|
20 |
* @package core_privacy
|
|
|
21 |
* @category test
|
|
|
22 |
* @copyright 2018 Marina Glancy
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
defined('MOODLE_INTERNAL') || die();
|
|
|
27 |
|
|
|
28 |
global $CFG;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Unit Tests for sitepolicy manager
|
|
|
32 |
*
|
|
|
33 |
* @package core_privacy
|
|
|
34 |
* @category test
|
|
|
35 |
* @copyright 2018 Marina Glancy
|
|
|
36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
37 |
*/
|
|
|
38 |
class sitepolicy_test extends advanced_testcase {
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Tests for \core_privacy\local\sitepolicy\manager::get_handler_classname() behaviour.
|
|
|
43 |
*/
|
11 |
efrain |
44 |
public function test_get_handler_classname(): void {
|
1 |
efrain |
45 |
global $CFG;
|
|
|
46 |
$this->resetAfterTest(true);
|
|
|
47 |
|
|
|
48 |
$manager = $this->get_mock_manager_with_handler();
|
|
|
49 |
|
|
|
50 |
// If no handler is specified, then we should get the default one.
|
|
|
51 |
$CFG->sitepolicyhandler = '';
|
|
|
52 |
$this->assertEquals($manager->get_handler_classname(), \core_privacy\local\sitepolicy\default_handler::class);
|
|
|
53 |
|
|
|
54 |
// If non-existing handler is specified, we should get the default one too.
|
|
|
55 |
$CFG->sitepolicyhandler = 'non_existing_plugin_which_i_really_hope_will_never_exist';
|
|
|
56 |
$this->assertEquals($manager->get_handler_classname(), \core_privacy\local\sitepolicy\default_handler::class);
|
|
|
57 |
|
|
|
58 |
// If the defined handler is among known handlers, we should get its class name.
|
|
|
59 |
$CFG->sitepolicyhandler = 'testtool_testhandler';
|
|
|
60 |
$this->assertEquals($manager->get_handler_classname(), 'mock_sitepolicy_handler');
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Tests for \core_privacy\local\sitepolicy\manager::is_defined()
|
|
|
65 |
*/
|
11 |
efrain |
66 |
public function test_is_defined(): void {
|
1 |
efrain |
67 |
global $CFG;
|
|
|
68 |
$this->resetAfterTest(true);
|
|
|
69 |
|
|
|
70 |
$manager = new \core_privacy\local\sitepolicy\manager();
|
|
|
71 |
|
|
|
72 |
$this->assertFalse($manager->is_defined(true));
|
|
|
73 |
$this->assertFalse($manager->is_defined(false));
|
|
|
74 |
|
|
|
75 |
$CFG->sitepolicy = 'http://example.com/sitepolicy.html';
|
|
|
76 |
$this->assertFalse($manager->is_defined(true));
|
|
|
77 |
$this->assertTrue($manager->is_defined(false));
|
|
|
78 |
|
|
|
79 |
$CFG->sitepolicyguest = 'http://example.com/sitepolicyguest.html';
|
|
|
80 |
$this->assertTrue($manager->is_defined(true));
|
|
|
81 |
$this->assertTrue($manager->is_defined(false));
|
|
|
82 |
|
|
|
83 |
$CFG->sitepolicy = null;
|
|
|
84 |
$this->assertTrue($manager->is_defined(true));
|
|
|
85 |
$this->assertFalse($manager->is_defined(false));
|
|
|
86 |
|
|
|
87 |
// When non existing plugin is set as $CFG->sitepolicyhandler, assume that $CFG->sitepolicy* are all not set.
|
|
|
88 |
$CFG->sitepolicy = 'http://example.com/sitepolicy.html';
|
|
|
89 |
$CFG->sitepolicyguest = 'http://example.com/sitepolicyguest.html';
|
|
|
90 |
$CFG->sitepolicyhandler = 'non_existing_plugin_which_i_really_hope_will_never_exist';
|
|
|
91 |
$this->assertFalse($manager->is_defined(true));
|
|
|
92 |
$this->assertFalse($manager->is_defined(false));
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Tests for \core_privacy\local\sitepolicy\manager::get_redirect_url()
|
|
|
97 |
*/
|
11 |
efrain |
98 |
public function test_get_redirect_url(): void {
|
1 |
efrain |
99 |
global $CFG;
|
|
|
100 |
$this->resetAfterTest(true);
|
|
|
101 |
|
|
|
102 |
$manager = new \core_privacy\local\sitepolicy\manager();
|
|
|
103 |
|
|
|
104 |
$this->assertEquals(null, $manager->get_redirect_url(true));
|
|
|
105 |
$this->assertEquals(null, $manager->get_redirect_url(false));
|
|
|
106 |
|
|
|
107 |
$CFG->sitepolicy = 'http://example.com/sitepolicy.html';
|
|
|
108 |
$this->assertEquals(null, $manager->get_redirect_url(true));
|
|
|
109 |
$this->assertEquals($CFG->wwwroot.'/user/policy.php', $manager->get_redirect_url(false)->out(false));
|
|
|
110 |
|
|
|
111 |
$CFG->sitepolicyguest = 'http://example.com/sitepolicyguest.html';
|
|
|
112 |
$this->assertEquals($CFG->wwwroot.'/user/policy.php', $manager->get_redirect_url(true)->out(false));
|
|
|
113 |
$this->assertEquals($CFG->wwwroot.'/user/policy.php', $manager->get_redirect_url(false)->out(false));
|
|
|
114 |
|
|
|
115 |
$CFG->sitepolicy = null;
|
|
|
116 |
$this->assertEquals($CFG->wwwroot.'/user/policy.php', $manager->get_redirect_url(true)->out(false));
|
|
|
117 |
$this->assertEquals(null, $manager->get_redirect_url(false));
|
|
|
118 |
|
|
|
119 |
// When non existing plugin is set as $CFG->sitepolicyhandler, assume that $CFG->sitepolicy* are all not set.
|
|
|
120 |
$CFG->sitepolicy = 'http://example.com/sitepolicy.html';
|
|
|
121 |
$CFG->sitepolicyguest = 'http://example.com/sitepolicyguest.html';
|
|
|
122 |
$CFG->sitepolicyhandler = 'non_existing_plugin_which_i_really_hope_will_never_exist';
|
|
|
123 |
$this->assertEquals(null, $manager->get_redirect_url(true));
|
|
|
124 |
$this->assertEquals(null, $manager->get_redirect_url(false));
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Tests for \core_privacy\local\sitepolicy\manager::get_redirect_url()
|
|
|
129 |
*/
|
11 |
efrain |
130 |
public function test_get_embed_url(): void {
|
1 |
efrain |
131 |
global $CFG;
|
|
|
132 |
$this->resetAfterTest(true);
|
|
|
133 |
|
|
|
134 |
$manager = new \core_privacy\local\sitepolicy\manager();
|
|
|
135 |
|
|
|
136 |
$this->assertEquals(null, $manager->get_embed_url(true));
|
|
|
137 |
$this->assertEquals(null, $manager->get_embed_url(false));
|
|
|
138 |
|
|
|
139 |
$CFG->sitepolicy = 'http://example.com/sitepolicy.html';
|
|
|
140 |
$this->assertEquals(null, $manager->get_embed_url(true));
|
|
|
141 |
$this->assertEquals($CFG->sitepolicy, $manager->get_embed_url(false)->out(false));
|
|
|
142 |
|
|
|
143 |
$CFG->sitepolicyguest = 'http://example.com/sitepolicyguest.html';
|
|
|
144 |
$this->assertEquals($CFG->sitepolicyguest, $manager->get_embed_url(true)->out(false));
|
|
|
145 |
$this->assertEquals($CFG->sitepolicy, $manager->get_embed_url(false)->out(false));
|
|
|
146 |
|
|
|
147 |
$CFG->sitepolicy = null;
|
|
|
148 |
$this->assertEquals($CFG->sitepolicyguest, $manager->get_embed_url(true)->out(false));
|
|
|
149 |
$this->assertEquals(null, $manager->get_embed_url(false));
|
|
|
150 |
|
|
|
151 |
// When non existing plugin is set as $CFG->sitepolicyhandler, assume that $CFG->sitepolicy* are all not set.
|
|
|
152 |
$CFG->sitepolicy = 'http://example.com/sitepolicy.html';
|
|
|
153 |
$CFG->sitepolicyguest = 'http://example.com/sitepolicyguest.html';
|
|
|
154 |
$CFG->sitepolicyhandler = 'non_existing_plugin_which_i_really_hope_will_never_exist';
|
|
|
155 |
$this->assertEquals(null, $manager->get_embed_url(true));
|
|
|
156 |
$this->assertEquals(null, $manager->get_embed_url(false));
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
* Tests for \core_privacy\local\sitepolicy\manager::get_redirect_url()
|
|
|
161 |
*/
|
11 |
efrain |
162 |
public function test_accept(): void {
|
1 |
efrain |
163 |
global $CFG, $USER, $DB;
|
|
|
164 |
$this->resetAfterTest(true);
|
|
|
165 |
|
|
|
166 |
$manager = new \core_privacy\local\sitepolicy\manager();
|
|
|
167 |
|
|
|
168 |
// No site policy.
|
|
|
169 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
170 |
$this->setUser($user1);
|
|
|
171 |
$this->assertFalse($manager->accept());
|
|
|
172 |
$this->assertEquals(0, $USER->policyagreed);
|
|
|
173 |
|
|
|
174 |
// With site policy.
|
|
|
175 |
$CFG->sitepolicy = 'http://example.com/sitepolicy.html';
|
|
|
176 |
|
|
|
177 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
178 |
$this->setUser($user2);
|
|
|
179 |
|
|
|
180 |
$this->assertEquals(0, $USER->policyagreed);
|
|
|
181 |
$this->assertEquals(0, $DB->get_field('user', 'policyagreed', ['id' => $USER->id]));
|
|
|
182 |
$this->assertTrue($manager->accept());
|
|
|
183 |
$this->assertEquals(1, $USER->policyagreed);
|
|
|
184 |
$this->assertEquals(1, $DB->get_field('user', 'policyagreed', ['id' => $USER->id]));
|
|
|
185 |
|
|
|
186 |
// When non existing plugin is set as $CFG->sitepolicyhandler, assume that $CFG->sitepolicy* are all not set.
|
|
|
187 |
$CFG->sitepolicy = 'http://example.com/sitepolicy.html';
|
|
|
188 |
$CFG->sitepolicyhandler = 'non_existing_plugin_which_i_really_hope_will_never_exist';
|
|
|
189 |
$user3 = $this->getDataGenerator()->create_user();
|
|
|
190 |
$this->setUser($user3);
|
|
|
191 |
$this->assertEquals(0, $USER->policyagreed);
|
|
|
192 |
$this->assertFalse($manager->accept());
|
|
|
193 |
$this->assertEquals(0, $USER->policyagreed);
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
/**
|
|
|
197 |
* Tests for \core_privacy\local\sitepolicy\manager::get_redirect_url() for guests
|
|
|
198 |
*/
|
11 |
efrain |
199 |
public function test_accept_guests(): void {
|
1 |
efrain |
200 |
global $CFG, $USER, $DB;
|
|
|
201 |
$this->resetAfterTest(true);
|
|
|
202 |
|
|
|
203 |
$manager = new \core_privacy\local\sitepolicy\manager();
|
|
|
204 |
|
|
|
205 |
$this->setGuestUser();
|
|
|
206 |
|
|
|
207 |
// No site policy.
|
|
|
208 |
$this->assertFalse($manager->accept());
|
|
|
209 |
$this->assertEquals(0, $USER->policyagreed);
|
|
|
210 |
|
|
|
211 |
// With site policy.
|
|
|
212 |
$CFG->sitepolicyguest = 'http://example.com/sitepolicy.html';
|
|
|
213 |
|
|
|
214 |
$this->assertEquals(0, $USER->policyagreed);
|
|
|
215 |
$this->assertTrue($manager->accept());
|
|
|
216 |
$this->assertEquals(1, $USER->policyagreed);
|
|
|
217 |
$this->assertEquals(0, $DB->get_field('user', 'policyagreed', ['id' => $USER->id]));
|
|
|
218 |
|
|
|
219 |
// When non existing plugin is set as $CFG->sitepolicyhandler, assume that $CFG->sitepolicy* are all not set.
|
|
|
220 |
$USER->policyagreed = 0; // Reset.
|
|
|
221 |
$CFG->sitepolicyguest = 'http://example.com/sitepolicyguest.html';
|
|
|
222 |
$CFG->sitepolicyhandler = 'non_existing_plugin_which_i_really_hope_will_never_exist';
|
|
|
223 |
$this->assertFalse($manager->accept());
|
|
|
224 |
$this->assertEquals(0, $USER->policyagreed);
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
/**
|
|
|
228 |
* Helper to spoof the results of the internal function get_all_handlers, allowing mock handler to be tested.
|
|
|
229 |
*
|
|
|
230 |
* @return PHPUnit_Framework_MockObject_MockObject
|
|
|
231 |
*/
|
|
|
232 |
protected function get_mock_manager_with_handler() {
|
|
|
233 |
global $CFG;
|
|
|
234 |
require_once($CFG->dirroot.'/privacy/tests/fixtures/mock_sitepolicy_handler.php');
|
|
|
235 |
|
|
|
236 |
$mock = $this->getMockBuilder(\core_privacy\local\sitepolicy\manager::class)
|
|
|
237 |
->onlyMethods(['get_all_handlers'])
|
|
|
238 |
->getMock();
|
|
|
239 |
$mock->expects($this->any())
|
|
|
240 |
->method('get_all_handlers')
|
|
|
241 |
->will($this->returnValue(['testtool_testhandler' => 'mock_sitepolicy_handler']));
|
|
|
242 |
return $mock;
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
/**
|
|
|
246 |
* Tests for \core_privacy\local\sitepolicy\manager::is_defined() with a handler
|
|
|
247 |
*/
|
11 |
efrain |
248 |
public function test_is_defined_with_handler(): void {
|
1 |
efrain |
249 |
global $CFG;
|
|
|
250 |
$this->resetAfterTest(true);
|
|
|
251 |
$CFG->sitepolicyhandler = 'testtool_testhandler';
|
|
|
252 |
$manager = $this->get_mock_manager_with_handler();
|
|
|
253 |
$this->assertTrue($manager->is_defined(true));
|
|
|
254 |
$this->assertTrue($manager->is_defined(false));
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
/**
|
|
|
258 |
* Tests for \core_privacy\local\sitepolicy\manager::get_redirect_url() with a handler
|
|
|
259 |
*/
|
11 |
efrain |
260 |
public function test_get_redirect_url_with_handler(): void {
|
1 |
efrain |
261 |
global $CFG;
|
|
|
262 |
$this->resetAfterTest(true);
|
|
|
263 |
|
|
|
264 |
$CFG->sitepolicyhandler = 'testtool_testhandler';
|
|
|
265 |
$manager = $this->get_mock_manager_with_handler();
|
|
|
266 |
|
|
|
267 |
$this->assertEquals('http://example.com/policy.php', $manager->get_redirect_url(true)->out(false));
|
|
|
268 |
$this->assertEquals('http://example.com/policy.php', $manager->get_redirect_url(false)->out(false));
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
/**
|
|
|
272 |
* Tests for \core_privacy\local\sitepolicy\manager::get_redirect_url() with a handler
|
|
|
273 |
*/
|
11 |
efrain |
274 |
public function test_get_embed_url_with_handler(): void {
|
1 |
efrain |
275 |
global $CFG;
|
|
|
276 |
$this->resetAfterTest(true);
|
|
|
277 |
|
|
|
278 |
$CFG->sitepolicyhandler = 'testtool_testhandler';
|
|
|
279 |
$manager = $this->get_mock_manager_with_handler();
|
|
|
280 |
|
|
|
281 |
$this->assertEquals('http://example.com/view.htm', $manager->get_embed_url(true)->out(false));
|
|
|
282 |
$this->assertEquals('http://example.com/view.htm', $manager->get_embed_url(false)->out(false));
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
/**
|
|
|
286 |
* Tests for \core_privacy\local\sitepolicy\manager::get_redirect_url() with a handler
|
|
|
287 |
*/
|
11 |
efrain |
288 |
public function test_accept_with_handler(): void {
|
1 |
efrain |
289 |
global $CFG, $USER, $DB;
|
|
|
290 |
$this->resetAfterTest(true);
|
|
|
291 |
|
|
|
292 |
$CFG->sitepolicyhandler = 'testtool_testhandler';
|
|
|
293 |
$manager = $this->get_mock_manager_with_handler();
|
|
|
294 |
|
|
|
295 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
296 |
$this->setUser($user2);
|
|
|
297 |
|
|
|
298 |
$this->assertEquals(0, $USER->policyagreed);
|
|
|
299 |
$this->assertEquals(0, $DB->get_field('user', 'policyagreed', ['id' => $USER->id]));
|
|
|
300 |
$this->assertTrue($manager->accept());
|
|
|
301 |
$this->assertEquals(2, $USER->policyagreed);
|
|
|
302 |
$this->assertEquals(2, $DB->get_field('user', 'policyagreed', ['id' => $USER->id]));
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
/**
|
|
|
306 |
* Tests for \core_privacy\local\sitepolicy\manager::get_redirect_url() for guests with a handler
|
|
|
307 |
*/
|
11 |
efrain |
308 |
public function test_accept_guests_with_handler(): void {
|
1 |
efrain |
309 |
global $CFG, $USER, $DB;
|
|
|
310 |
$this->resetAfterTest(true);
|
|
|
311 |
|
|
|
312 |
$CFG->sitepolicyhandler = 'testtool_testhandler';
|
|
|
313 |
$manager = $this->get_mock_manager_with_handler();
|
|
|
314 |
|
|
|
315 |
$this->setGuestUser();
|
|
|
316 |
|
|
|
317 |
$this->assertEquals(0, $USER->policyagreed);
|
|
|
318 |
$this->assertTrue($manager->accept());
|
|
|
319 |
$this->assertEquals(2, $USER->policyagreed);
|
|
|
320 |
$this->assertEquals(0, $DB->get_field('user', 'policyagreed', ['id' => $USER->id]));
|
|
|
321 |
}
|
|
|
322 |
}
|
|
|
323 |
|
|
|
324 |
/**
|
|
|
325 |
* Mock handler for site policies
|
|
|
326 |
*
|
|
|
327 |
* @package core_privacy
|
|
|
328 |
* @copyright 2018 Marina Glancy
|
|
|
329 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
330 |
*/
|
|
|
331 |
class handler extends \core_privacy\local\sitepolicy\handler {
|
|
|
332 |
|
|
|
333 |
/**
|
|
|
334 |
* Checks if the site has site policy defined
|
|
|
335 |
*
|
|
|
336 |
* @param bool $forguests
|
|
|
337 |
* @return bool
|
|
|
338 |
*/
|
|
|
339 |
public static function is_defined($forguests = false) {
|
|
|
340 |
return true;
|
|
|
341 |
}
|
|
|
342 |
|
|
|
343 |
/**
|
|
|
344 |
* Returns URL to redirect user to when user needs to agree to site policy
|
|
|
345 |
*
|
|
|
346 |
* This is a regular interactive page for web users. It should have normal Moodle header/footers, it should
|
|
|
347 |
* allow user to view policies and accept them.
|
|
|
348 |
*
|
|
|
349 |
* @param bool $forguests
|
|
|
350 |
* @return moodle_url|null (returns null if site policy is not defined)
|
|
|
351 |
*/
|
|
|
352 |
public static function get_redirect_url($forguests = false) {
|
|
|
353 |
return 'http://example.com/policy.php';
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
/**
|
|
|
357 |
* Returns URL of the site policy that needs to be displayed to the user (inside iframe or to use in WS such as mobile app)
|
|
|
358 |
*
|
|
|
359 |
* This page should not have any header/footer, it does not also have any buttons/checkboxes. The caller needs to implement
|
|
|
360 |
* the "Accept" button and call {@link self::accept()} on completion.
|
|
|
361 |
*
|
|
|
362 |
* @param bool $forguests
|
|
|
363 |
* @return moodle_url|null
|
|
|
364 |
*/
|
|
|
365 |
public static function get_embed_url($forguests = false) {
|
|
|
366 |
return 'http://example.com/view.htm';
|
|
|
367 |
}
|
|
|
368 |
|
|
|
369 |
/**
|
|
|
370 |
* Accept site policy for the current user
|
|
|
371 |
*
|
|
|
372 |
* @return bool - false if sitepolicy not defined, user is not logged in or user has already agreed to site policy;
|
|
|
373 |
* true - if we have successfully marked the user as agreed to the site policy
|
|
|
374 |
*/
|
|
|
375 |
public static function accept() {
|
|
|
376 |
global $USER, $DB;
|
|
|
377 |
// Accepts policy on behalf of the current user. We set it to 2 here to check that this callback was called.
|
|
|
378 |
$USER->policyagreed = 2;
|
|
|
379 |
if (!isguestuser()) {
|
|
|
380 |
$DB->update_record('user', ['policyagreed' => 2, 'id' => $USER->id]);
|
|
|
381 |
}
|
|
|
382 |
return true;
|
|
|
383 |
}
|
|
|
384 |
}
|