Merge branch 'MDL-62950-master' of git://github.com/andrewnicols/moodle
[moodle.git] / admin / tool / policy / tests / api_test.php
CommitLineData
3d9571d5
DM
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 * Provides the {@link tool_policy_api_testcase} class.
19 *
20 * @package tool_policy
21 * @category test
22 * @copyright 2018 David Mudrák <david@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26use tool_policy\api;
27use tool_policy\policy_version;
28
29defined('MOODLE_INTERNAL') || die();
30
31global $CFG;
32
33/**
34 * Unit tests for the {@link \tool_policy\api} class.
35 *
36 * @copyright 2018 David Mudrak <david@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class tool_policy_api_testcase extends advanced_testcase {
40
41 /**
42 * Test the common operations with a policy document and its versions.
43 */
44 public function test_policy_document_life_cycle() {
45 $this->resetAfterTest();
46 $this->setAdminUser();
47
48 // Prepare the form data for adding a new policy document.
49 $formdata = api::form_policydoc_data(new policy_version(0));
50 $this->assertObjectHasAttribute('name', $formdata);
51 $this->assertArrayHasKey('text', $formdata->summary_editor);
52 $this->assertArrayHasKey('format', $formdata->content_editor);
53
54 // Save the form.
55 $formdata->name = 'Test terms & conditions';
56 $formdata->type = policy_version::TYPE_OTHER;
57 $policy = api::form_policydoc_add($formdata);
58 $record = $policy->to_record();
59
60 $this->assertNotEmpty($record->id);
61 $this->assertNotEmpty($record->policyid);
62 $this->assertNotEmpty($record->timecreated);
63 $this->assertNotEmpty($record->timemodified);
64 $this->assertNotNull($record->name);
65 $this->assertNotNull($record->summary);
66 $this->assertNotNull($record->summaryformat);
67 $this->assertNotNull($record->content);
68 $this->assertNotNull($record->contentformat);
69
70 // Update the policy document version.
71 $formdata = api::form_policydoc_data($policy);
72 $formdata->revision = '*** Unit test ***';
73 $formdata->summary_editor['text'] = '__Just a summary__';
74 $formdata->summary_editor['format'] = FORMAT_MARKDOWN;
75 $formdata->content_editor['text'] = '### Just a test ###';
76 $formdata->content_editor['format'] = FORMAT_MARKDOWN;
77 $updated = api::form_policydoc_update_overwrite($formdata);
78 $this->assertEquals($policy->get('id'), $updated->get('id'));
79 $this->assertEquals($policy->get('policyid'), $updated->get('policyid'));
80
81 // Save form as a new version.
82 $formdata = api::form_policydoc_data($policy);
83 $formdata->name = 'New terms & conditions';
84 $formdata->revision = '*** Unit test 2 ***';
85 $formdata->summary_editor['text'] = '<strong>Yet another summary</strong>';
86 $formdata->summary_editor['format'] = FORMAT_MOODLE;
87 $formdata->content_editor['text'] = '<h3>Yet another test</h3>';
88 $formdata->content_editor['format'] = FORMAT_HTML;
89 $new = api::form_policydoc_update_new($formdata);
90 $this->assertNotEquals($policy->get('id'), $new->get('id'));
91 $this->assertEquals($policy->get('policyid'), $new->get('policyid'));
92
93 // Add yet another policy document.
94 $formdata = api::form_policydoc_data(new policy_version(0));
95 $formdata->name = 'Privacy terms';
96 $formdata->type = policy_version::TYPE_PRIVACY;
97 $another = api::form_policydoc_add($formdata);
98
99 // Get the list of all policies and their versions.
100 $docs = api::list_policies();
101 $this->assertEquals(2, count($docs));
102
103 // Get just one policy and all its versions.
104 $docs = api::list_policies($another->get('policyid'));
105 $this->assertEquals(1, count($docs));
106
107 // Activate a policy.
108 $this->assertEquals(0, count(api::list_current_versions()));
109 api::make_current($updated->get('id'));
110 $current = api::list_current_versions();
111 $this->assertEquals(1, count($current));
112 $first = reset($current);
113 $this->assertEquals('Test terms &amp; conditions', $first->name);
114
115 // Activate another policy version.
116 api::make_current($new->get('id'));
117 $current = api::list_current_versions();
118 $this->assertEquals(1, count($current));
119 $first = reset($current);
120 $this->assertEquals('New terms &amp; conditions', $first->name);
121
122 // Inactivate the policy.
123 api::inactivate($new->get('policyid'));
124 $this->assertEmpty(api::list_current_versions());
125 $archived = api::get_policy_version($new->get('id'));
126 $this->assertEquals(policy_version::STATUS_ARCHIVED, $archived->status);
127
128 // Create a new draft from an archived version.
129 $draft = api::revert_to_draft($archived->id);
130 $draft = api::get_policy_version($draft->get('id'));
131 $archived = api::get_policy_version($archived->id);
132 $this->assertEmpty(api::list_current_versions());
133 $this->assertNotEquals($draft->id, $archived->id);
134 $this->assertEquals(policy_version::STATUS_DRAFT, $draft->status);
135 $this->assertEquals(policy_version::STATUS_ARCHIVED, $archived->status);
136
137 // An active policy can't be set to draft.
138 api::make_current($draft->id);
139 $this->expectException('coding_exception');
140 $this->expectExceptionMessage('Version not found or is not archived');
141 api::revert_to_draft($draft->id);
142 }
143
144 /**
145 * Test changing the sort order of the policy documents.
146 */
147 public function test_policy_sortorder() {
148 global $DB;
149 $this->resetAfterTest();
150 $this->setAdminUser();
151
152 $formdata = api::form_policydoc_data(new policy_version(0));
153 $formdata->name = 'Policy1';
154 $formdata->summary_editor = ['text' => 'P1 summary', 'format' => FORMAT_HTML, 'itemid' => 0];
155 $formdata->content_editor = ['text' => 'P1 content', 'format' => FORMAT_HTML, 'itemid' => 0];
156 $policy1 = api::form_policydoc_add($formdata);
157 $policy1sortorder = $DB->get_field('tool_policy', 'sortorder', ['id' => $policy1->get('policyid')]);
158
159 $formdata = api::form_policydoc_data(new policy_version(0));
160 $formdata->name = 'Policy2';
161 $formdata->summary_editor = ['text' => 'P2 summary', 'format' => FORMAT_HTML, 'itemid' => 0];
162 $formdata->content_editor = ['text' => 'P2 content', 'format' => FORMAT_HTML, 'itemid' => 0];
163 $policy2 = api::form_policydoc_add($formdata);
164 $policy2sortorder = $DB->get_field('tool_policy', 'sortorder', ['id' => $policy2->get('policyid')]);
165
166 $this->assertTrue($policy1sortorder < $policy2sortorder);
167
168 $formdata = api::form_policydoc_data(new policy_version(0));
169 $formdata->name = 'Policy3';
170 $formdata->summary_editor = ['text' => 'P3 summary', 'format' => FORMAT_HTML, 'itemid' => 0];
171 $formdata->content_editor = ['text' => 'P3 content', 'format' => FORMAT_HTML, 'itemid' => 0];
172 $policy3 = api::form_policydoc_add($formdata);
173 $policy3sortorder = $DB->get_field('tool_policy', 'sortorder', ['id' => $policy3->get('policyid')]);
174
175 $this->assertTrue($policy1sortorder < $policy2sortorder);
176 $this->assertTrue($policy2sortorder < $policy3sortorder);
177
178 api::move_up($policy3->get('policyid'));
179
180 $policy1sortorder = $DB->get_field('tool_policy', 'sortorder', ['id' => $policy1->get('policyid')]);
181 $policy2sortorder = $DB->get_field('tool_policy', 'sortorder', ['id' => $policy2->get('policyid')]);
182 $policy3sortorder = $DB->get_field('tool_policy', 'sortorder', ['id' => $policy3->get('policyid')]);
183
184 $this->assertTrue($policy1sortorder < $policy3sortorder);
185 $this->assertTrue($policy3sortorder < $policy2sortorder);
186
187 api::move_down($policy1->get('policyid'));
188
189 $policy1sortorder = $DB->get_field('tool_policy', 'sortorder', ['id' => $policy1->get('policyid')]);
190 $policy2sortorder = $DB->get_field('tool_policy', 'sortorder', ['id' => $policy2->get('policyid')]);
191 $policy3sortorder = $DB->get_field('tool_policy', 'sortorder', ['id' => $policy3->get('policyid')]);
192
193 $this->assertTrue($policy3sortorder < $policy1sortorder);
194 $this->assertTrue($policy1sortorder < $policy2sortorder);
195
196 $orderedlist = [];
197 foreach (api::list_policies() as $policy) {
198 $orderedlist[] = $policy->id;
199 }
200 $this->assertEquals([$policy3->get('policyid'), $policy1->get('policyid'), $policy2->get('policyid')], $orderedlist);
201 }
202
203 /**
204 * Test that list of policies can be filtered by audience
205 */
206 public function test_list_policies_audience() {
207 $this->resetAfterTest();
208 $this->setAdminUser();
209
210 $policy1 = $this->add_policy(['audience' => policy_version::AUDIENCE_LOGGEDIN]);
211 $policy2 = $this->add_policy(['audience' => policy_version::AUDIENCE_GUESTS]);
212 $policy3 = $this->add_policy();
213
214 api::make_current($policy1->get('id'));
215 api::make_current($policy2->get('id'));
216 api::make_current($policy3->get('id'));
217
218 $list = array_map(function ($version) {
219 return $version->policyid;
220 }, api::list_current_versions());
221 $this->assertEquals([$policy1->get('policyid'), $policy2->get('policyid'), $policy3->get('policyid')],
222 array_values($list));
223 $ids = api::get_current_versions_ids();
224 $this->assertEquals([$policy1->get('policyid') => $policy1->get('id'),
225 $policy2->get('policyid') => $policy2->get('id'),
226 $policy3->get('policyid') => $policy3->get('id')], $ids);
227
228 $list = array_map(function ($version) {
229 return $version->policyid;
230 }, api::list_current_versions(policy_version::AUDIENCE_LOGGEDIN));
231 $this->assertEquals([$policy1->get('policyid'), $policy3->get('policyid')], array_values($list));
232 $ids = api::get_current_versions_ids(policy_version::AUDIENCE_LOGGEDIN);
233 $this->assertEquals([$policy1->get('policyid') => $policy1->get('id'),
234 $policy3->get('policyid') => $policy3->get('id')], $ids);
235
236 $list = array_map(function ($version) {
237 return $version->policyid;
238 }, api::list_current_versions(policy_version::AUDIENCE_GUESTS));
239 $this->assertEquals([$policy2->get('policyid'), $policy3->get('policyid')], array_values($list));
240 $ids = api::get_current_versions_ids(policy_version::AUDIENCE_GUESTS);
241 $this->assertEquals([$policy2->get('policyid') => $policy2->get('id'),
242 $policy3->get('policyid') => $policy3->get('id')], $ids);
243 }
244
245 /**
246 * Helper method that creates a new policy for testing
247 *
248 * @param array $params
249 * @return policy_version
250 */
251 protected function add_policy($params = []) {
252 static $counter = 0;
253 $counter++;
254
255 $defaults = [
256 'name' => 'Policy '.$counter,
257 'summary_editor' => ['text' => "P$counter summary", 'format' => FORMAT_HTML, 'itemid' => 0],
258 'content_editor' => ['text' => "P$counter content", 'format' => FORMAT_HTML, 'itemid' => 0],
259 ];
260
261 $params = (array)$params + $defaults;
262 $formdata = api::form_policydoc_data(new policy_version(0));
263 foreach ($params as $key => $value) {
264 $formdata->$key = $value;
265 }
266 return api::form_policydoc_add($formdata);
267 }
268
269 /**
270 * Helper method that prepare a policy document with some versions.
271 *
272 * @param int $numversions The number of policy versions to create.
273 * @return array Array with all the policy versions created.
274 */
275 protected function create_versions($numversions = 2) {
276 // Prepare a policy document with some versions.
277 $policy = self::add_policy([
278 'name' => 'Test policy',
279 'revision' => 'v1',
280 ]);
281
282 for ($i = 2; $i <= $numversions; $i++) {
283 $formdata = api::form_policydoc_data($policy);
284 $formdata->revision = 'v'.$i;
285 api::form_policydoc_update_new($formdata);
286 }
287
288 $list = api::list_policies($policy->get('policyid'));
289
290 return $list[0]->draftversions;
291 }
292
293 /**
294 * Test behaviour of the {@link api::can_user_view_policy_version()} method.
295 */
296 public function test_can_user_view_policy_version() {
297 global $CFG;
298 $this->resetAfterTest();
299 $this->setAdminUser();
300
301 $child = $this->getDataGenerator()->create_user();
302 $parent = $this->getDataGenerator()->create_user();
303 $this->getDataGenerator()->create_user();
304 $officer = $this->getDataGenerator()->create_user();
305 $manager = $this->getDataGenerator()->create_user();
306
307 $syscontext = context_system::instance();
308 $childcontext = context_user::instance($child->id);
309
310 $roleminorid = create_role('Digital minor', 'digiminor', 'Not old enough to accept site policies themselves');
311 $roleparentid = create_role('Parent', 'parent', 'Can accept policies on behalf of their child');
312 $roleofficerid = create_role('Policy officer', 'policyofficer', 'Can see all acceptances but can\'t edit policy documents');
313 $rolemanagerid = create_role('Policy manager', 'policymanager', 'Can manage policy documents');
314
315 assign_capability('tool/policy:accept', CAP_PROHIBIT, $roleminorid, $syscontext->id);
316 assign_capability('tool/policy:acceptbehalf', CAP_ALLOW, $roleparentid, $syscontext->id);
317 assign_capability('tool/policy:viewacceptances', CAP_ALLOW, $roleofficerid, $syscontext->id);
318 assign_capability('tool/policy:managedocs', CAP_ALLOW, $rolemanagerid, $syscontext->id);
319
320 role_assign($roleminorid, $child->id, $syscontext->id);
321 // Becoming a parent is easy. Being a good one is difficult.
322 role_assign($roleparentid, $parent->id, $childcontext->id);
323 role_assign($roleofficerid, $officer->id, $syscontext->id);
324 role_assign($rolemanagerid, $manager->id, $syscontext->id);
325
326 accesslib_clear_all_caches_for_unit_testing();
327
328 // Prepare a policy document with some versions.
329 list($policy1, $policy2, $policy3) = $this->create_versions(3);
330
331 // Normally users do not have access to policy drafts.
332 $this->assertFalse(api::can_user_view_policy_version($policy1, null, $child->id));
333 $this->assertFalse(api::can_user_view_policy_version($policy2, null, $parent->id));
334 $this->assertFalse(api::can_user_view_policy_version($policy3, null, $CFG->siteguest));
335
336 // Officers and managers have access even to drafts.
337 $this->assertTrue(api::can_user_view_policy_version($policy1, null, $officer->id));
338 $this->assertTrue(api::can_user_view_policy_version($policy3, null, $manager->id));
339
340 // Current versions are public so that users can decide whether to even register on such a site.
341 api::make_current($policy2->id);
342 $policy1 = api::get_policy_version($policy1->id);
343 $policy2 = api::get_policy_version($policy2->id);
344 $policy3 = api::get_policy_version($policy3->id);
345
346 $this->assertFalse(api::can_user_view_policy_version($policy1, null, $child->id));
347 $this->assertTrue(api::can_user_view_policy_version($policy2, null, $child->id));
348 $this->assertTrue(api::can_user_view_policy_version($policy2, null, $CFG->siteguest));
349 $this->assertFalse(api::can_user_view_policy_version($policy3, null, $child->id));
350
351 // Let the parent accept the policy on behalf of her child.
352 $this->setUser($parent);
353 api::accept_policies($policy2->id, $child->id);
354
355 // Release a new version of the policy.
356 api::make_current($policy3->id);
357 $policy1 = api::get_policy_version($policy1->id);
358 $policy2 = api::get_policy_version($policy2->id);
359 $policy3 = api::get_policy_version($policy3->id);
360
361 api::get_user_minors($parent->id);
362 // They should now have access to the archived version (because they agreed) and the current one.
363 $this->assertFalse(api::can_user_view_policy_version($policy1, null, $child->id));
364 $this->assertFalse(api::can_user_view_policy_version($policy1, null, $parent->id));
365 $this->assertTrue(api::can_user_view_policy_version($policy2, null, $child->id));
366 $this->assertTrue(api::can_user_view_policy_version($policy2, null, $parent->id));
367 $this->assertTrue(api::can_user_view_policy_version($policy3, null, $child->id));
368 $this->assertTrue(api::can_user_view_policy_version($policy3, null, $parent->id));
369 }
370
461b1931
SA
371 /**
372 * Test behaviour of the {@link api::can_revoke_policies()} method.
373 */
374 public function test_can_revoke_policies() {
375 global $CFG;
376
377 $this->resetAfterTest();
378 $this->setAdminUser();
379
380 $user = $this->getDataGenerator()->create_user();
381 $child = $this->getDataGenerator()->create_user();
382 $parent = $this->getDataGenerator()->create_user();
383 $officer = $this->getDataGenerator()->create_user();
384 $manager = $this->getDataGenerator()->create_user();
385
386 $syscontext = context_system::instance();
387 $childcontext = context_user::instance($child->id);
388
389 $roleminorid = create_role('Digital minor', 'digiminor', 'Not old enough to accept site policies themselves');
390 $roleparentid = create_role('Parent', 'parent', 'Can accept policies on behalf of their child');
391 $roleofficerid = create_role('Policy officer', 'policyofficer', 'Can see all acceptances but can\'t edit policy documents');
392 $rolemanagerid = create_role('Policy manager', 'policymanager', 'Can manage policy documents');
393
394 assign_capability('tool/policy:accept', CAP_PROHIBIT, $roleminorid, $syscontext->id);
395 assign_capability('tool/policy:acceptbehalf', CAP_ALLOW, $roleparentid, $syscontext->id);
396 assign_capability('tool/policy:acceptbehalf', CAP_ALLOW, $roleofficerid, $syscontext->id);
397 assign_capability('tool/policy:viewacceptances', CAP_ALLOW, $roleofficerid, $syscontext->id);
398 assign_capability('tool/policy:acceptbehalf', CAP_ALLOW, $rolemanagerid, $syscontext->id);
399 assign_capability('tool/policy:managedocs', CAP_ALLOW, $rolemanagerid, $syscontext->id);
400
401 role_assign($roleminorid, $child->id, $syscontext->id);
402 // Becoming a parent is easy. Being a good one is difficult.
403 role_assign($roleparentid, $parent->id, $childcontext->id);
404 role_assign($roleofficerid, $officer->id, $syscontext->id);
405 role_assign($rolemanagerid, $manager->id, $syscontext->id);
406
407 accesslib_clear_all_caches_for_unit_testing();
408
409 // Prepare a policy document with some versions.
410 list($policy1, $policy2, $policy3) = $this->create_versions(3);
411
412 // Normally users do not have access to revoke policies.
413 $this->setUser($user);
414 $this->assertFalse(api::can_revoke_policies($user->id));
415 $this->setUser($child);
416 $this->assertFalse(api::can_revoke_policies($child->id));
417
418 // The parent can revoke the policy on behalf of her child (but not her own policies).
419 $this->setUser($parent);
420 $this->assertFalse(api::can_revoke_policies($parent->id));
421 $this->assertTrue(api::can_revoke_policies($child->id));
422
423 // Officers and managers can revoke everything.
424 $this->setUser($officer);
425 $this->assertTrue(api::can_revoke_policies($officer->id));
426 $this->assertTrue(api::can_revoke_policies($child->id));
427 $this->assertTrue(api::can_revoke_policies($parent->id));
428 $this->assertTrue(api::can_revoke_policies($manager->id));
429
430 $this->setUser($manager);
431 $this->assertTrue(api::can_revoke_policies($manager->id));
432 $this->assertTrue(api::can_revoke_policies($child->id));
433 $this->assertTrue(api::can_revoke_policies($parent->id));
434 $this->assertTrue(api::can_revoke_policies($officer->id));
435 }
436
3d9571d5
DM
437 /**
438 * Test {@link api::fix_revision_values()} behaviour.
439 */
440 public function test_fix_revision_values() {
441 $this->resetAfterTest();
442 $this->setAdminUser();
443
444 $versions = [
445 (object) ['id' => 80, 'timecreated' => mktime(1, 1, 1, 12, 28, 2018), 'revision' => '', 'e' => '28 December 2018'],
446 (object) ['id' => 70, 'timecreated' => mktime(1, 1, 1, 12, 27, 2018), 'revision' => '', 'e' => '27 December 2018 - v2'],
447 (object) ['id' => 60, 'timecreated' => mktime(1, 1, 1, 12, 27, 2018), 'revision' => '', 'e' => '27 December 2018 - v1'],
448 (object) ['id' => 50, 'timecreated' => mktime(0, 0, 0, 12, 26, 2018), 'revision' => '0', 'e' => '0'],
449 (object) ['id' => 40, 'timecreated' => mktime(0, 0, 0, 12, 26, 2018), 'revision' => '1.1', 'e' => '1.1 - v2'],
450 (object) ['id' => 30, 'timecreated' => mktime(0, 0, 0, 12, 26, 2018), 'revision' => '1.1', 'e' => '1.1 - v1'],
451 (object) ['id' => 20, 'timecreated' => mktime(0, 0, 0, 12, 26, 2018), 'revision' => '', 'e' => '26 December 2018'],
452 (object) ['id' => 10, 'timecreated' => mktime(17, 57, 00, 12, 25, 2018), 'revision' => '1.0', 'e' => '1.0'],
453 ];
454
455 api::fix_revision_values($versions);
456
457 foreach ($versions as $version) {
458 $this->assertSame($version->revision, $version->e);
459 }
460 }
461
462 /**
463 * Test that accepting policy updates 'policyagreed'
464 */
465 public function test_accept_policies() {
466 global $DB;
467 $this->resetAfterTest();
468 $this->setAdminUser();
469
470 $policy1 = $this->add_policy()->to_record();
471 api::make_current($policy1->id);
472 $policy2 = $this->add_policy()->to_record();
473 api::make_current($policy2->id);
474
475 // Accept policy on behalf of somebody else.
476 $user1 = $this->getDataGenerator()->create_user();
477 $this->assertEquals(0, $DB->get_field('user', 'policyagreed', ['id' => $user1->id]));
478
479 api::accept_policies([$policy1->id, $policy2->id], $user1->id);
480 $this->assertEquals(1, $DB->get_field('user', 'policyagreed', ['id' => $user1->id]));
481
482 // Now revoke.
483 api::revoke_acceptance($policy1->id, $user1->id);
484 $this->assertEquals(0, $DB->get_field('user', 'policyagreed', ['id' => $user1->id]));
485
486 // Accept policies for oneself.
487 $user2 = $this->getDataGenerator()->create_user();
488 $this->setUser($user2);
489
490 $this->assertEquals(0, $DB->get_field('user', 'policyagreed', ['id' => $user2->id]));
491
492 api::accept_policies([$policy1->id]);
493 $this->assertEquals(0, $DB->get_field('user', 'policyagreed', ['id' => $user2->id]));
494
495 api::accept_policies([$policy2->id]);
496 $this->assertEquals(1, $DB->get_field('user', 'policyagreed', ['id' => $user2->id]));
497 }
498
499 /**
500 * Test that activating a new policy resets everybody's policyagreed flag in the database.
501 */
502 public function test_reset_policyagreed() {
503 global $DB;
504 $this->resetAfterTest();
505 $this->setAdminUser();
506
507 $user1 = $this->getDataGenerator()->create_user();
508
509 // Introducing a new policy.
510 list($policy1v1, $policy1v2) = $this->create_versions(2);
511 api::make_current($policy1v1->id);
512 $this->assertEquals(0, $DB->get_field('user', 'policyagreed', ['id' => $user1->id]));
513 api::accept_policies([$policy1v1->id], $user1->id);
514 $this->assertEquals(1, $DB->get_field('user', 'policyagreed', ['id' => $user1->id]));
515
516 // Introducing another policy.
517 $policy2v1 = $this->add_policy()->to_record();
518 api::make_current($policy2v1->id);
519 $this->assertEquals(0, $DB->get_field('user', 'policyagreed', ['id' => $user1->id]));
520 api::accept_policies([$policy2v1->id], $user1->id);
521 $this->assertEquals(1, $DB->get_field('user', 'policyagreed', ['id' => $user1->id]));
522
523 // Updating an existing policy (major update).
524 api::make_current($policy1v2->id);
525 $this->assertEquals(0, $DB->get_field('user', 'policyagreed', ['id' => $user1->id]));
526 api::accept_policies([$policy1v2->id], $user1->id);
527 $this->assertEquals(1, $DB->get_field('user', 'policyagreed', ['id' => $user1->id]));
528
529 // Do not touch the flag if there is no new version (e.g. a minor update).
530 api::make_current($policy2v1->id);
531 api::make_current($policy1v2->id);
532 $this->assertEquals(1, $DB->get_field('user', 'policyagreed', ['id' => $user1->id]));
533
534 // Do not touch the flag if inactivating a policy.
535 api::inactivate($policy1v2->policyid);
536 $this->assertEquals(1, $DB->get_field('user', 'policyagreed', ['id' => $user1->id]));
537
538 // Do not touch the flag if setting to draft a policy.
539 api::revert_to_draft($policy1v2->id);
540 $this->assertEquals(1, $DB->get_field('user', 'policyagreed', ['id' => $user1->id]));
541 }
542
543 /**
544 * Test behaviour of the {@link api::get_user_minors()} method.
545 */
546 public function test_get_user_minors() {
547 $this->resetAfterTest();
548
549 // A mother having two children, each child having own father.
550 $mother1 = $this->getDataGenerator()->create_user();
551 $father1 = $this->getDataGenerator()->create_user();
552 $father2 = $this->getDataGenerator()->create_user();
553 $child1 = $this->getDataGenerator()->create_user();
554 $child2 = $this->getDataGenerator()->create_user();
555
556 $syscontext = context_system::instance();
557 $child1context = context_user::instance($child1->id);
558 $child2context = context_user::instance($child2->id);
559
560 $roleparentid = create_role('Parent', 'parent', 'Can accept policies on behalf of their child');
561
562 assign_capability('tool/policy:acceptbehalf', CAP_ALLOW, $roleparentid, $syscontext->id);
563
564 role_assign($roleparentid, $mother1->id, $child1context->id);
565 role_assign($roleparentid, $mother1->id, $child2context->id);
566 role_assign($roleparentid, $father1->id, $child1context->id);
567 role_assign($roleparentid, $father2->id, $child2context->id);
568
569 accesslib_clear_all_caches_for_unit_testing();
570
571 $mother1minors = api::get_user_minors($mother1->id);
572 $this->assertEquals(2, count($mother1minors));
573
574 $father1minors = api::get_user_minors($father1->id);
575 $this->assertEquals(1, count($father1minors));
576 $this->assertEquals($child1->id, $father1minors[$child1->id]->id);
577
578 $father2minors = api::get_user_minors($father2->id);
579 $this->assertEquals(1, count($father2minors));
580 $this->assertEquals($child2->id, $father2minors[$child2->id]->id);
581
582 $this->assertEmpty(api::get_user_minors($child1->id));
583 $this->assertEmpty(api::get_user_minors($child2->id));
584
585 $extradata = api::get_user_minors($mother1->id, ['policyagreed', 'deleted']);
586 $this->assertTrue(property_exists($extradata[$child1->id], 'policyagreed'));
587 $this->assertTrue(property_exists($extradata[$child1->id], 'deleted'));
588 $this->assertTrue(property_exists($extradata[$child2->id], 'policyagreed'));
589 $this->assertTrue(property_exists($extradata[$child2->id], 'deleted'));
590 }
591
592 /**
593 * Test behaviour of the {@link api::create_acceptances_user_created()} method.
594 */
595 public function test_create_acceptances_user_created() {
596 global $CFG, $DB;
597 $this->resetAfterTest();
598 $this->setAdminUser();
599
600 $CFG->sitepolicyhandler = 'tool_policy';
601
602 $policy = $this->add_policy()->to_record();
603 api::make_current($policy->id);
604
605 // User has not accepted any policies.
606 $user1 = $this->getDataGenerator()->create_user();
607 \core\event\user_created::create_from_userid($user1->id)->trigger();
608
609 $this->assertEquals(0, $DB->count_records('tool_policy_acceptances',
610 ['userid' => $user1->id, 'policyversionid' => $policy->id]));
611
612 // User has accepted policies.
613 $user2 = $this->getDataGenerator()->create_user();
614 $DB->set_field('user', 'policyagreed', 1, ['id' => $user2->id]);
615 \core\event\user_created::create_from_userid($user2->id)->trigger();
616
617 $this->assertEquals(1, $DB->count_records('tool_policy_acceptances',
618 ['userid' => $user2->id, 'policyversionid' => $policy->id]));
619 }
4e9e2b0a
SA
620
621 /**
622 * Test that user can login if sitepolicyhandler is set but there are no policies.
623 */
624 public function test_login_with_handler_without_policies() {
625 global $CFG;
626
627 $this->resetAfterTest();
628 $user = $this->getDataGenerator()->create_user();
629 $this->setUser($user);
630
631 $CFG->sitepolicyhandler = 'tool_policy';
632
633 require_login(null, false, null, false, true);
634 }
3d9571d5 635}