MDL-63495 privacy: Add support for removal of multiple users in a context
[moodle.git] / privacy / tests / userlist_collection.php
blob000ac3d73966c8b21a641639c63165aa5688ee68
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 a the collection of userlists 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\userlist_collection;
31 use \core_privacy\local\request\userlist;
32 use \core_privacy\local\request\approved_userlist;
34 /**
35 * Tests for the \core_privacy API's userlist collection functionality.
37 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class userlist_collection_test extends advanced_testcase {
42 /**
43 * A userlist_collection should support the userlist type.
45 public function test_supports_userlist() {
46 $cut = \context_system::instance();
47 $uut = new userlist_collection($cut);
49 $userlist = new userlist($cut, 'core_privacy');
50 $uut->add_userlist($userlist);
52 $this->assertCount(1, $uut->get_userlists());
55 /**
56 * A userlist_collection should support the approved_userlist type.
58 public function test_supports_approved_userlist() {
59 $cut = \context_system::instance();
60 $uut = new userlist_collection($cut);
62 $userlist = new approved_userlist($cut, 'core_privacy', [1, 2, 3]);
63 $uut->add_userlist($userlist);
65 $this->assertCount(1, $uut->get_userlists());
68 /**
69 * Ensure that get_userlist_for_component returns the correct userlist.
71 public function test_get_userlist_for_component() {
72 $cut = \context_system::instance();
73 $uut = new userlist_collection($cut);
75 $privacy = new userlist($cut, 'core_privacy');
76 $uut->add_userlist($privacy);
78 $test = new userlist($cut, 'core_tests');
79 $uut->add_userlist($test);
81 // Note: This uses assertSame rather than assertEquals.
82 // The former checks the actual object, whilst assertEquals only checks that they look the same.
83 $this->assertSame($privacy, $uut->get_userlist_for_component('core_privacy'));
84 $this->assertSame($test, $uut->get_userlist_for_component('core_tests'));
87 /**
88 * Ensure that get_userlist_for_component does not die horribly when querying a non-existent component.
90 public function test_get_userlist_for_component_not_found() {
91 $cut = \context_system::instance();
92 $uut = new userlist_collection($cut);
94 $this->assertNull($uut->get_userlist_for_component('core_tests'));
97 /**
98 * Ensure that a duplicate userlist in the collection throws an Exception.
100 public function test_duplicate_addition_throws() {
101 $cut = \context_system::instance();
102 $uut = new userlist_collection($cut);
104 $userlist = new userlist($cut, 'core_privacy');
105 $uut->add_userlist($userlist);
107 $this->expectException('moodle_exception');
108 $uut->add_userlist($userlist);
112 * Ensure that the userlist_collection is countable.
114 public function test_countable() {
115 $cut = \context_system::instance();
116 $uut = new userlist_collection($cut);
118 $uut->add_userlist(new userlist($cut, 'core_privacy'));
119 $uut->add_userlist(new userlist($cut, 'core_tests'));
121 $this->assertCount(2, $uut);
125 * Ensure that the userlist_collection iterates over the set of userlists.
127 public function test_iteration() {
128 $cut = \context_system::instance();
129 $uut = new userlist_collection($cut);
131 $testdata = [];
133 $privacy = new userlist($cut, 'core_privacy');
134 $uut->add_userlist($privacy);
135 $testdata['core_privacy'] = $privacy;
137 $test = new userlist($cut, 'core_tests');
138 $uut->add_userlist($test);
139 $testdata['core_tests'] = $test;
141 $another = new userlist($cut, 'privacy_another');
142 $uut->add_userlist($another);
143 $testdata['privacy_another'] = $another;
145 foreach ($uut as $component => $list) {
146 $this->assertEquals($testdata[$component], $list);
149 $this->assertCount(3, $uut);
153 * Test that the context is correctly returned.
155 public function test_get_context() {
156 $cut = \context_system::instance();
157 $uut = new userlist_collection($cut);
159 $this->assertSame($cut, $uut->get_context());