mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
manager_test.php
303 lines (265 loc) · 13.3 KB
/
manager_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Privacy manager unit tests.
*
* @package core_privacy
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/privacy/tests/fixtures/mock_null_provider.php');
require_once($CFG->dirroot . '/privacy/tests/fixtures/mock_provider.php');
require_once($CFG->dirroot . '/privacy/tests/fixtures/mock_plugin_subplugin_provider.php');
require_once($CFG->dirroot . '/privacy/tests/fixtures/mock_mod_with_user_data_provider.php');
use \core_privacy\local\request\writer;
/**
* Privacy manager unit tests.
*
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class privacy_manager_testcase extends advanced_testcase {
/**
* Test tearDown.
*/
public function tearDown() {
\core_privacy\local\request\writer::reset();
}
/**
* Helper to spoof the results of the internal function get_components_list, allowing mock components to be tested.
*
* @param array $componentnames and array of component names to include as valid core components.
* @return PHPUnit_Framework_MockObject_MockObject
*/
protected function get_mock_manager_with_core_components($componentnames) {
$mock = $this->getMockBuilder(\core_privacy\manager::class)
->setMethods(['get_component_list'])
->getMock();
$mock->expects($this->any())
->method('get_component_list')
->will($this->returnValue($componentnames));
return $mock;
}
/**
* Test collection of metadata for components implementing a metadata provider.
*/
public function test_get_metadata_for_components() {
// Get a mock manager, in which the core components list is mocked to include all mock plugins.
// testcomponent is a core provider, testcomponent2 is a null provider, testcomponent3 is subplugin provider (non core).
$mockman = $this->get_mock_manager_with_core_components(['mod_testcomponent', 'mod_testcomponent2', 'mod_testcomponent3']);
// Core providers and shared providers both implement the metadata provider.
$collectionarray = $mockman->get_metadata_for_components();
$this->assertArrayHasKey('mod_testcomponent', $collectionarray);
$collection = $collectionarray['mod_testcomponent'];
$this->assertInstanceOf(\core_privacy\local\metadata\collection::class, $collection);
$this->assertArrayHasKey('mod_testcomponent3', $collectionarray);
$collection = $collectionarray['mod_testcomponent3'];
$this->assertInstanceOf(\core_privacy\local\metadata\collection::class, $collection);
// Component which implements just the local\metadata\null_provider. Metadata is not provided.
$this->assertArrayNotHasKey('mod_testcomponent2', $collectionarray);
}
/**
* Test that get_contexts_for_userid() only returns contextlist collections for core providers.
*/
public function test_get_contexts_for_userid() {
// Get a mock manager, in which the core components list is mocked to include all mock plugins.
// testcomponent is a core provider, testcomponent2 is a null provider, testcomponent3 is subplugin provider (non core).
$mockman = $this->get_mock_manager_with_core_components(['mod_testcomponent', 'mod_testcomponent2', 'mod_testcomponent3']);
// Get the contextlist_collection.
$contextlistcollection = $mockman->get_contexts_for_userid(10);
$this->assertInstanceOf(\core_privacy\local\request\contextlist_collection::class, $contextlistcollection);
ob_flush();
// Verify we have a contextlist for the component in the collection.
$this->assertInstanceOf(\core_privacy\local\request\contextlist::class,
$contextlistcollection->get_contextlist_for_component('mod_testcomponent'));
// Verify we don't have a contextlist for the shared provider in the collection.
$this->assertNull($contextlistcollection->get_contextlist_for_component('mod_testcomponent3'));
// Verify we don't have a contextlist for the component which does not store user data.
$this->assertEmpty($contextlistcollection->get_contextlist_for_component('mod_testcomponent2'));
}
/**
* Test verifying the output of component_is_compliant.
*/
public function test_component_is_compliant() {
// Get a mock manager, in which the core components list is mocked to include all mock plugins.
// testcomponent is a core provider, testcomponent2 is a null provider, testcomponent3 is subplugin provider (non core).
$mockman = $this->get_mock_manager_with_core_components(['mod_testcomponent', 'mod_testcomponent2', 'mod_testcomponent3']);
// A core_provider plugin implementing all required interfaces (local\metadata\provider, local\request\plugin_provider).
$this->assertTrue($mockman->component_is_compliant('mod_testcomponent'));
// A component implementing just the \core_privacy\local\metadata\null_provider is compliant.
$this->assertTrue($mockman->component_is_compliant('mod_testcomponent2'));
// A shared provider plugin implementing all required interfaces (local\metadata\provider, local\request\plugin\subplugin_provider)
// is compliant.
$this->assertTrue($mockman->component_is_compliant('mod_testcomponent3'));
// A component implementing none of the providers.
$this->assertFalse($mockman->component_is_compliant('tool_thisisnotarealtool123'));
}
/**
* Provider for component_is_compliant tests.
*
* @return array
*/
public function component_is_compliant_provider() {
return [
'An empty subsystem' => [
'core_countries',
true,
],
'A real subsystem' => [
'core_privacy',
true,
],
];
}
/**
* Test verifying the output of component_is_compliant with specified
* components.
*
* @dataProvider component_is_compliant_provider
* @param string $component
* @param boolean $expected
*/
public function test_component_is_compliant_examples($component, $expected) {
$manager = new \core_privacy\manager();
$this->assertEquals($expected, $manager->component_is_compliant($component));
}
/**
* Test verifying only approved contextlists can be used with the export_user_data method.
*/
public function test_export_user_data() {
// Get a mock manager, in which the core components list is mocked to include all mock plugins.
// testcomponent is a core provider, testcomponent2 is a null provider, testcomponent3 is subplugin provider (non core).
$mockman = $this->get_mock_manager_with_core_components(['mod_testcomponent', 'mod_testcomponent2', 'mod_testcomponent3', 'mod_testcomponent4']);
// Get the non-approved contextlists.
$contextlistcollection = $mockman->get_contexts_for_userid(10);
// Create an approved contextlist.
$approvedcontextlistcollection = new \core_privacy\local\request\contextlist_collection(10);
foreach ($contextlistcollection->get_contextlists() as $contextlist) {
$approvedcontextlist = new \core_privacy\local\request\approved_contextlist(new stdClass(), $contextlist->get_component(),
$contextlist->get_contextids());
$approvedcontextlistcollection->add_contextlist($approvedcontextlist);
}
// Verify the mocked return from the writer, meaning the manager method exited normally.
$this->assertEquals('mock_path', $mockman->export_user_data($approvedcontextlistcollection));
// Verify that a user preference was exported for 'mod_testcomponent4'.
$prefs = writer::with_context(\context_system::instance())->get_user_preferences('mod_testcomponent4');
$this->assertNotEmpty($prefs);
$this->assertNotEmpty($prefs->mykey);
$this->assertEquals('myvalue', $prefs->mykey->value);
$this->assertEquals('mydescription', $prefs->mykey->description);
// Verify an exception is thrown if trying to pass in a collection of non-approved_contextlist items.
$this->expectException(moodle_exception::class);
$mockman->export_user_data($contextlistcollection);
}
/**
* Test verifying only approved contextlists can be used with the delete_data_for_user method.
*/
public function test_delete_data_for_user() {
$this->resetAfterTest();
// Get a mock manager, in which the core components list is mocked to include all mock plugins.
// testcomponent is a core provider, testcomponent2 is a null provider, testcomponent3 is subplugin provider (non core).
$mockman = $this->get_mock_manager_with_core_components(['mod_testcomponent', 'mod_testcomponent2', 'mod_testcomponent3']);
// Get the non-approved contextlists.
$user = \core_user::get_user_by_username('admin');
$contextlistcollection = $mockman->get_contexts_for_userid($user->id);
// Create an approved contextlist.
$approvedcontextlistcollection = new \core_privacy\local\request\contextlist_collection($user->id);
foreach ($contextlistcollection->get_contextlists() as $contextlist) {
$approvedcontextlist = new \core_privacy\local\request\approved_contextlist($user, $contextlist->get_component(),
$contextlist->get_contextids());
$approvedcontextlistcollection->add_contextlist($approvedcontextlist);
}
// Verify null, as the method has no return type and exits normally. Mainly checking we don't see any exception.
$this->assertNull($mockman->delete_data_for_user($approvedcontextlistcollection));
// Verify an exception is thrown if trying to pass in a collection of non-approved_contextlist items.
$this->expectException(moodle_exception::class);
$mockman->delete_data_for_user($contextlistcollection);
}
/**
* Ensure that all installed plugins can provide metadata.
*
* This really just checks that all providers can be safely autoloaded.
*/
public function test_installed_plugins() {
$manager = new \core_privacy\manager();
$metadata = $manager->get_metadata_for_components();
$this->assertNotEmpty($metadata);
}
/**
* Test that the reason for the null provider is returned.
*/
public function test_get_null_provider_reason() {
$manager = new \core_privacy\manager();
// Null providers return the reason string.
$this->assertEquals('testcomponent2 null provider reason', $manager->get_null_provider_reason('mod_testcomponent2'));
// Throw an exception if the wrong type of provider is given.
$this->expectException(\coding_exception::class);
$string = $manager->get_null_provider_reason('mod_testcomponent');
}
/**
* Test that manager::plugintype_class_callback() can be executed.
*/
public function test_plugintype_class_callback() {
\core_privacy\manager::plugintype_class_callback('doesnotexist', 'unusable', 'foo', ['bar']);
}
/**
* Test that manager::component_class_callback() can be executed.
*/
public function test_component_class_callback() {
\core_privacy\manager::component_class_callback('foo_bar', 'unusable', 'foo', ['bar']);
}
/**
* Test the manager::is_empty_subsystem function.
*
* @dataProvider is_empty_subsystem_provider
* @param string $component
* @param bool $expected
*/
public function test_is_empty_subsystem($component, $expected) {
$this->assertEquals($expected, \core_privacy\manager::is_empty_subsystem($component));
}
/**
* Test cases for the is_empty_subsystem function.
*
* @return array
*/
public function is_empty_subsystem_provider() {
return [
'A subsystem which has no directory' => [
'core_langconfig',
true,
],
'A subsystem with a directory' => [
'core_portfolio',
false,
],
'A plugin' => [
'mod_forum',
false,
],
'A plugintype' => [
'mod',
false,
],
'An unprefixed subsystem with no directory' => [
'langconfig',
false,
],
];
}
}