MDL-63497 mod_lesson: Add support for removal of context users
[moodle.git] / privacy / tests / approved_userlist_test.php
blobedb79fd1864efd87f9aea67fad4341509d5511e5
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 the approved userlist Class
20 * @package core_privacy
21 * @category test
22 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 use \core_privacy\local\request\approved_userlist;
31 use \core_privacy\local\request\userlist;
33 /**
34 * Tests for the \core_privacy API's approved userlist functionality.
36 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class approved_userlist_test extends advanced_testcase {
40 /**
41 * The approved userlist should not be modifiable once set.
43 public function test_default_values_set() {
44 $this->resetAfterTest();
46 $u1 = $this->getDataGenerator()->create_user();
47 $u2 = $this->getDataGenerator()->create_user();
48 $u3 = $this->getDataGenerator()->create_user();
49 $u4 = $this->getDataGenerator()->create_user();
51 $context = \context_system::instance();
52 $component = 'core_privacy';
54 $uut = new approved_userlist($context, $component, [$u1->id, $u2->id]);
56 $this->assertEquals($context, $uut->get_context());
57 $this->assertEquals($component, $uut->get_component());
59 $expected = [
60 $u1->id,
61 $u2->id,
63 sort($expected);
65 $result = $uut->get_userids();
66 sort($result);
68 $this->assertEquals($expected, $result);
71 public function test_create_from_userlist() {
72 $this->resetAfterTest();
74 $u1 = $this->getDataGenerator()->create_user();
75 $u2 = $this->getDataGenerator()->create_user();
76 $u3 = $this->getDataGenerator()->create_user();
77 $u4 = $this->getDataGenerator()->create_user();
79 $context = \context_system::instance();
80 $component = 'core_privacy';
82 $sourcelist = new userlist($context, $component);
83 $sourcelist->add_users([$u1->id, $u3->id]);
85 $expected = [
86 $u1->id,
87 $u3->id,
89 sort($expected);
91 $approvedlist = approved_userlist::create_from_userlist($sourcelist);
93 $this->assertEquals($component, $approvedlist->get_component());
94 $this->assertEquals($context, $approvedlist->get_context());
96 $result = $approvedlist->get_userids();
97 sort($result);
98 $this->assertEquals($expected, $result);