11 |
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 |
namespace core_privacy;
|
|
|
18 |
|
|
|
19 |
use advanced_testcase;
|
|
|
20 |
use \core_privacy\local\request\userlist_collection;
|
|
|
21 |
use \core_privacy\local\request\userlist;
|
|
|
22 |
use \core_privacy\local\request\approved_userlist;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Tests for the \core_privacy API's userlist collection functionality.
|
|
|
26 |
*
|
|
|
27 |
* @package core_privacy
|
|
|
28 |
* @category test
|
|
|
29 |
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
* @coversDefaultClass \core_privacy\local\request\userlist_collection
|
|
|
32 |
*/
|
|
|
33 |
final class userlist_collection_test extends advanced_testcase {
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* A userlist_collection should support the userlist type.
|
|
|
37 |
*
|
|
|
38 |
* @covers ::add_userlist
|
|
|
39 |
*/
|
|
|
40 |
public function test_supports_userlist() {
|
|
|
41 |
$cut = \context_system::instance();
|
|
|
42 |
$uut = new userlist_collection($cut);
|
|
|
43 |
|
|
|
44 |
$userlist = new userlist($cut, 'core_privacy');
|
|
|
45 |
$uut->add_userlist($userlist);
|
|
|
46 |
|
|
|
47 |
$this->assertCount(1, $uut->get_userlists());
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* A userlist_collection should support the approved_userlist type.
|
|
|
52 |
*
|
|
|
53 |
* @covers ::add_userlist
|
|
|
54 |
*/
|
|
|
55 |
public function test_supports_approved_userlist() {
|
|
|
56 |
$cut = \context_system::instance();
|
|
|
57 |
$uut = new userlist_collection($cut);
|
|
|
58 |
|
|
|
59 |
$userlist = new approved_userlist($cut, 'core_privacy', [1, 2, 3]);
|
|
|
60 |
$uut->add_userlist($userlist);
|
|
|
61 |
|
|
|
62 |
$this->assertCount(1, $uut->get_userlists());
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Ensure that get_userlist_for_component returns the correct userlist.
|
|
|
67 |
*
|
|
|
68 |
* @covers ::get_userlist_for_component
|
|
|
69 |
*/
|
|
|
70 |
public function test_get_userlist_for_component() {
|
|
|
71 |
$cut = \context_system::instance();
|
|
|
72 |
$uut = new userlist_collection($cut);
|
|
|
73 |
|
|
|
74 |
$privacy = new userlist($cut, 'core_privacy');
|
|
|
75 |
$uut->add_userlist($privacy);
|
|
|
76 |
|
|
|
77 |
$test = new userlist($cut, 'core_tests');
|
|
|
78 |
$uut->add_userlist($test);
|
|
|
79 |
|
|
|
80 |
// Note: This uses assertSame rather than assertEquals.
|
|
|
81 |
// The former checks the actual object, whilst assertEquals only checks that they look the same.
|
|
|
82 |
$this->assertSame($privacy, $uut->get_userlist_for_component('core_privacy'));
|
|
|
83 |
$this->assertSame($test, $uut->get_userlist_for_component('core_tests'));
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Ensure that get_userlist_for_component does not die horribly when querying a non-existent component.
|
|
|
88 |
*
|
|
|
89 |
* @covers ::get_userlist_for_component
|
|
|
90 |
*/
|
|
|
91 |
public function test_get_userlist_for_component_not_found() {
|
|
|
92 |
$cut = \context_system::instance();
|
|
|
93 |
$uut = new userlist_collection($cut);
|
|
|
94 |
|
|
|
95 |
$this->assertNull($uut->get_userlist_for_component('core_tests'));
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Ensure that a duplicate userlist in the collection throws an Exception.
|
|
|
100 |
*
|
|
|
101 |
* @covers ::add_userlist
|
|
|
102 |
*/
|
|
|
103 |
public function test_duplicate_addition_throws() {
|
|
|
104 |
$cut = \context_system::instance();
|
|
|
105 |
$uut = new userlist_collection($cut);
|
|
|
106 |
|
|
|
107 |
$userlist = new userlist($cut, 'core_privacy');
|
|
|
108 |
$uut->add_userlist($userlist);
|
|
|
109 |
|
|
|
110 |
$this->expectException('moodle_exception');
|
|
|
111 |
$uut->add_userlist($userlist);
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* Ensure that the userlist_collection is countable.
|
|
|
116 |
*
|
|
|
117 |
* @covers ::count
|
|
|
118 |
*/
|
|
|
119 |
public function test_countable() {
|
|
|
120 |
$cut = \context_system::instance();
|
|
|
121 |
$uut = new userlist_collection($cut);
|
|
|
122 |
|
|
|
123 |
$uut->add_userlist(new userlist($cut, 'core_privacy'));
|
|
|
124 |
$uut->add_userlist(new userlist($cut, 'core_tests'));
|
|
|
125 |
|
|
|
126 |
$this->assertCount(2, $uut);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Ensure that the userlist_collection iterates over the set of userlists.
|
|
|
131 |
*
|
|
|
132 |
* @covers ::current
|
|
|
133 |
* @covers ::key
|
|
|
134 |
* @covers ::next
|
|
|
135 |
* @covers ::rewind
|
|
|
136 |
* @covers ::valid
|
|
|
137 |
*/
|
|
|
138 |
public function test_iteration() {
|
|
|
139 |
$cut = \context_system::instance();
|
|
|
140 |
$uut = new userlist_collection($cut);
|
|
|
141 |
|
|
|
142 |
$testdata = [];
|
|
|
143 |
|
|
|
144 |
$privacy = new userlist($cut, 'core_privacy');
|
|
|
145 |
$uut->add_userlist($privacy);
|
|
|
146 |
$testdata['core_privacy'] = $privacy;
|
|
|
147 |
|
|
|
148 |
$test = new userlist($cut, 'core_tests');
|
|
|
149 |
$uut->add_userlist($test);
|
|
|
150 |
$testdata['core_tests'] = $test;
|
|
|
151 |
|
|
|
152 |
$another = new userlist($cut, 'privacy_another');
|
|
|
153 |
$uut->add_userlist($another);
|
|
|
154 |
$testdata['privacy_another'] = $another;
|
|
|
155 |
|
|
|
156 |
foreach ($uut as $component => $list) {
|
|
|
157 |
$this->assertEquals($testdata[$component], $list);
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
$this->assertCount(3, $uut);
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
/**
|
|
|
164 |
* Test that the context is correctly returned.
|
|
|
165 |
*
|
|
|
166 |
* @covers ::get_context
|
|
|
167 |
*/
|
|
|
168 |
public function test_get_context() {
|
|
|
169 |
$cut = \context_system::instance();
|
|
|
170 |
$uut = new userlist_collection($cut);
|
|
|
171 |
|
|
|
172 |
$this->assertSame($cut, $uut->get_context());
|
|
|
173 |
}
|
|
|
174 |
}
|