MDL-62950 behat: Correct path to P&P
[moodle.git] / privacy / tests / sitepolicy_test.php
blob3589829f5d9703d5cc5e9b2894b3e3b3932d1833
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 * Unit Tests for sitepolicy manager
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
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 /**
31 * Unit Tests for sitepolicy manager
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
38 class sitepolicy_test extends advanced_testcase {
41 /**
42 * Tests for \core_privacy\local\sitepolicy\manager::get_handler_classname() behaviour.
44 public function test_get_handler_classname() {
45 global $CFG;
46 $this->resetAfterTest(true);
48 $manager = $this->get_mock_manager_with_handler();
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);
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);
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');
63 /**
64 * Tests for \core_privacy\local\sitepolicy\manager::is_defined()
66 public function test_is_defined() {
67 global $CFG;
68 $this->resetAfterTest(true);
70 $manager = new \core_privacy\local\sitepolicy\manager();
72 $this->assertFalse($manager->is_defined(true));
73 $this->assertFalse($manager->is_defined(false));
75 $CFG->sitepolicy = 'http://example.com/sitepolicy.html';
76 $this->assertFalse($manager->is_defined(true));
77 $this->assertTrue($manager->is_defined(false));
79 $CFG->sitepolicyguest = 'http://example.com/sitepolicyguest.html';
80 $this->assertTrue($manager->is_defined(true));
81 $this->assertTrue($manager->is_defined(false));
83 $CFG->sitepolicy = null;
84 $this->assertTrue($manager->is_defined(true));
85 $this->assertFalse($manager->is_defined(false));
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));
95 /**
96 * Tests for \core_privacy\local\sitepolicy\manager::get_redirect_url()
98 public function test_get_redirect_url() {
99 global $CFG;
100 $this->resetAfterTest(true);
102 $manager = new \core_privacy\local\sitepolicy\manager();
104 $this->assertEquals(null, $manager->get_redirect_url(true));
105 $this->assertEquals(null, $manager->get_redirect_url(false));
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));
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));
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));
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));
128 * Tests for \core_privacy\local\sitepolicy\manager::get_redirect_url()
130 public function test_get_embed_url() {
131 global $CFG;
132 $this->resetAfterTest(true);
134 $manager = new \core_privacy\local\sitepolicy\manager();
136 $this->assertEquals(null, $manager->get_embed_url(true));
137 $this->assertEquals(null, $manager->get_embed_url(false));
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));
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));
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));
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));
160 * Tests for \core_privacy\local\sitepolicy\manager::get_redirect_url()
162 public function test_accept() {
163 global $CFG, $USER, $DB;
164 $this->resetAfterTest(true);
166 $manager = new \core_privacy\local\sitepolicy\manager();
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);
174 // With site policy.
175 $CFG->sitepolicy = 'http://example.com/sitepolicy.html';
177 $user2 = $this->getDataGenerator()->create_user();
178 $this->setUser($user2);
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]));
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);
197 * Tests for \core_privacy\local\sitepolicy\manager::get_redirect_url() for guests
199 public function test_accept_guests() {
200 global $CFG, $USER, $DB;
201 $this->resetAfterTest(true);
203 $manager = new \core_privacy\local\sitepolicy\manager();
205 $this->setGuestUser();
207 // No site policy.
208 $this->assertFalse($manager->accept());
209 $this->assertEquals(0, $USER->policyagreed);
211 // With site policy.
212 $CFG->sitepolicyguest = 'http://example.com/sitepolicy.html';
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]));
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);
228 * Helper to spoof the results of the internal function get_all_handlers, allowing mock handler to be tested.
230 * @return PHPUnit_Framework_MockObject_MockObject
232 protected function get_mock_manager_with_handler() {
233 global $CFG;
234 require_once($CFG->dirroot.'/privacy/tests/fixtures/mock_sitepolicy_handler.php');
236 $mock = $this->getMockBuilder(\core_privacy\local\sitepolicy\manager::class)
237 ->setMethods(['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;
246 * Tests for \core_privacy\local\sitepolicy\manager::is_defined() with a handler
248 public function test_is_defined_with_handler() {
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));
258 * Tests for \core_privacy\local\sitepolicy\manager::get_redirect_url() with a handler
260 public function test_get_redirect_url_with_handler() {
261 global $CFG;
262 $this->resetAfterTest(true);
264 $CFG->sitepolicyhandler = 'testtool_testhandler';
265 $manager = $this->get_mock_manager_with_handler();
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));
272 * Tests for \core_privacy\local\sitepolicy\manager::get_redirect_url() with a handler
274 public function test_get_embed_url_with_handler() {
275 global $CFG;
276 $this->resetAfterTest(true);
278 $CFG->sitepolicyhandler = 'testtool_testhandler';
279 $manager = $this->get_mock_manager_with_handler();
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));
286 * Tests for \core_privacy\local\sitepolicy\manager::get_redirect_url() with a handler
288 public function test_accept_with_handler() {
289 global $CFG, $USER, $DB;
290 $this->resetAfterTest(true);
292 $CFG->sitepolicyhandler = 'testtool_testhandler';
293 $manager = $this->get_mock_manager_with_handler();
295 $user2 = $this->getDataGenerator()->create_user();
296 $this->setUser($user2);
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]));
306 * Tests for \core_privacy\local\sitepolicy\manager::get_redirect_url() for guests with a handler
308 public function test_accept_guests_with_handler() {
309 global $CFG, $USER, $DB;
310 $this->resetAfterTest(true);
312 $CFG->sitepolicyhandler = 'testtool_testhandler';
313 $manager = $this->get_mock_manager_with_handler();
315 $this->setGuestUser();
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]));
325 * Mock handler for site policies
327 * @package core_privacy
328 * @copyright 2018 Marina Glancy
329 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
331 class handler extends \core_privacy\local\sitepolicy\handler {
334 * Checks if the site has site policy defined
336 * @param bool $forguests
337 * @return bool
339 public static function is_defined($forguests = false) {
340 return true;
344 * Returns URL to redirect user to when user needs to agree to site policy
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.
349 * @param bool $forguests
350 * @return moodle_url|null (returns null if site policy is not defined)
352 public static function get_redirect_url($forguests = false) {
353 return 'http://example.com/policy.php';
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)
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.
362 * @param bool $forguests
363 * @return moodle_url|null
365 public static function get_embed_url($forguests = false) {
366 return 'http://example.com/view.htm';
370 * Accept site policy for the current user
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
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]);
382 return true;