2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Unit tests for the privacy legacy polyfill.
20 * @package core_privacy
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();
30 use \core_privacy\local\metadata\collection;
31 use \core_privacy\local\request\contextlist;
32 use \core_privacy\local\request\deletion_criteria;
33 use \core_privacy\local\request\approved_contextlist;
36 * Tests for the \core_privacy API's types\user_preference functionality.
37 * Unit tests for the Privacy API's legacy_polyfill.
39 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class core_privacy_legacy_polyfill_test extends advanced_testcase {
44 * Test that the null_provider polyfill works and that the static _get_reason can be
45 * successfully called.
47 public function test_null_provider() {
48 $this->assertEquals('thisisareason', test_legacy_polyfill_null_provider::get_reason());
52 * Test that the metdata\provider polyfill works and that the static _get_metadata can be
53 * successfully called.
55 public function test_metadata_provider() {
56 $collection = new collection('core_privacy');
57 $this->assertSame($collection, test_legacy_polyfill_metadata_provider::get_metadata($collection));
61 * Test that the local\request\user_preference_provider polyfill works and that the static
62 * _export_user_preferences can be successfully called.
64 public function test_user_preference_provider() {
67 $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
68 $mock->expects($this->once())
69 ->method('get_return_value')
70 ->with('_export_user_preferences', [$userid])
73 test_legacy_polyfill_user_preference_provider::$mock = $mock;
74 test_legacy_polyfill_user_preference_provider::export_user_preferences($userid);
78 * Test that the local\request\core_user_preference_provider polyfill works and that the static
79 * _get_contexts_for_userid can be successfully called.
81 public function test_get_contexts_for_userid() {
83 $contextlist = new contextlist('core_privacy');
85 $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
86 $mock->expects($this->once())
87 ->method('get_return_value')
88 ->with('_get_contexts_for_userid', [$userid])
89 ->willReturn($contextlist);
91 test_legacy_polyfill_request_provider::$mock = $mock;
92 $result = test_legacy_polyfill_request_provider::get_contexts_for_userid($userid);
93 $this->assertSame($contextlist, $result);
97 * Test that the local\request\core_user_preference_provider polyfill works and that the static
98 * _export_user_data can be successfully called.
100 public function test_export_user_data() {
101 $contextlist = new approved_contextlist(\core_user::get_user_by_username('admin'), 'core_privacy', [98]);
103 $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
104 $mock->expects($this->once())
105 ->method('get_return_value')
106 ->with('_export_user_data', [$contextlist]);
108 test_legacy_polyfill_request_provider::$mock = $mock;
109 test_legacy_polyfill_request_provider::export_user_data($contextlist);
113 * Test that the local\request\core_user_preference_provider polyfill works and that the static
114 * _delete_for_context can be successfully called.
116 public function test_delete_for_context() {
117 $criteria = new deletion_criteria(\context_system::instance());
119 $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
120 $mock->expects($this->once())
121 ->method('get_return_value')
122 ->with('_delete_for_context', [$criteria]);
124 test_legacy_polyfill_request_provider::$mock = $mock;
125 test_legacy_polyfill_request_provider::delete_for_context($criteria);
129 * Test that the local\request\core_user_preference_provider polyfill works and that the static
130 * _delete_user_data can be successfully called.
132 public function test_delete_user_data() {
133 $contextlist = new approved_contextlist(\core_user::get_user_by_username('admin'), 'core_privacy', [98]);
135 $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
136 $mock->expects($this->once())
137 ->method('get_return_value')
138 ->with('_delete_user_data', [$contextlist]);
140 test_legacy_polyfill_request_provider::$mock = $mock;
141 test_legacy_polyfill_request_provider::delete_user_data($contextlist);
146 * Legacy polyfill test for the null provider.
148 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
149 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
151 class test_legacy_polyfill_null_provider implements \core_privacy\local\metadata\null_provider {
153 use \core_privacy\local\legacy_polyfill;
156 * Test for get_reason
158 protected static function _get_reason() {
159 return 'thisisareason';
164 * Legacy polyfill test for the metadata provider.
166 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
167 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
169 class test_legacy_polyfill_metadata_provider implements \core_privacy\local\metadata\provider {
171 use \core_privacy\local\legacy_polyfill;
174 * Test for get_metadata.
176 * @param collection $collection The initialised collection to add items to.
177 * @return collection A listing of user data stored through this system.
179 protected static function _get_metadata(collection $collection) {
185 * Legacy polyfill test for the metadata provider.
187 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
188 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
190 class test_legacy_polyfill_user_preference_provider implements \core_privacy\local\request\user_preference_provider {
192 use \core_privacy\local\legacy_polyfill;
195 * @var test_legacy_polyfill_request_provider $mock
197 public static $mock = null;
200 * Export all user preferences for the plugin.
202 * @param int $userid The userid of the user whose data is to be exported.
204 protected static function _export_user_preferences($userid) {
205 return static::$mock->get_return_value(__FUNCTION__, func_get_args());
210 * Legacy polyfill test for the request provider.
212 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
213 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
215 class test_legacy_polyfill_request_provider implements \core_privacy\local\request\core_user_data_provider {
217 use \core_privacy\local\legacy_polyfill;
220 * @var test_legacy_polyfill_request_provider $mock
222 public static $mock = null;
225 * Test for get_contexts_for_userid.
227 * @param int $userid The user to search.
228 * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
230 protected static function _get_contexts_for_userid($userid) {
231 return static::$mock->get_return_value(__FUNCTION__, func_get_args());
235 * Test for export_user_data.
237 * @param approved_contextlist $contextlist The approved contexts to export information for.
239 protected static function _export_user_data(approved_contextlist $contextlist) {
240 return static::$mock->get_return_value(__FUNCTION__, func_get_args());
245 * Delete all use data which matches the specified deletion_criteria.
247 * @param deletion_criteria $criteria An object containing specific deletion criteria to delete for.
249 public static function _delete_for_context(deletion_criteria $criteria) {
250 return static::$mock->get_return_value(__FUNCTION__, func_get_args());
254 * Delete all user data for the specified user, in the specified contexts.
256 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
258 public static function _delete_user_data(approved_contextlist $contextlist) {
259 return static::$mock->get_return_value(__FUNCTION__, func_get_args());
263 class test_legacy_polyfill_mock_wrapper {
265 * Get the return value for the specified item.
267 public function get_return_value() {}