MDL-74906 mod_lti: fix form selector in multi-item content item post
[moodle.git] / privacy / tests / legacy_polyfill_test.php
blob4305e4351837efbc67ae96ad5c858f23fd92e2f7
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 Privacy API's legacy_polyfill.
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
24 * @coversDefaultClass \core_privacy\local\legacy_polyfill
27 namespace core_privacy;
29 use core_privacy\local\metadata\collection;
30 use core_privacy\local\request\contextlist;
31 use core_privacy\local\request\approved_contextlist;
33 defined('MOODLE_INTERNAL') || die();
35 global $CFG;
37 /**
38 * Unit tests for the Privacy API's legacy_polyfill.
40 * @package core_privacy
41 * @category test
42 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 * @coversDefaultClass \core_privacy\local\legacy_polyfill
46 class legacy_polyfill_test extends \advanced_testcase {
47 /**
48 * Test that the null_provider polyfill works and that the static _get_reason can be
49 * successfully called.
51 * @covers ::get_reason
53 public function test_null_provider() {
54 $this->assertEquals('thisisareason', test_legacy_polyfill_null_provider::get_reason());
57 /**
58 * Test that the metdata\provider polyfill works and that the static _get_metadata can be
59 * successfully called.
61 * @covers ::get_metadata
63 public function test_metadata_provider() {
64 $collection = new collection('core_privacy');
65 $this->assertSame($collection, test_legacy_polyfill_metadata_provider::get_metadata($collection));
68 /**
69 * Test that the local\request\user_preference_provider polyfill works and that the static
70 * _export_user_preferences can be successfully called.
72 * @covers ::export_user_preferences
74 public function test_user_preference_provider() {
75 $userid = 417;
77 $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
78 $mock->expects($this->once())
79 ->method('get_return_value')
80 ->with('_export_user_preferences', [$userid]);
82 test_legacy_polyfill_user_preference_provider::$mock = $mock;
83 test_legacy_polyfill_user_preference_provider::export_user_preferences($userid);
86 /**
87 * Test that the local\request\core_user_preference_provider polyfill works and that the static
88 * _get_contexts_for_userid can be successfully called.
90 * @covers ::get_contexts_for_userid
92 public function test_get_contexts_for_userid() {
93 $userid = 417;
94 $contextlist = new contextlist('core_privacy');
96 $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
97 $mock->expects($this->once())
98 ->method('get_return_value')
99 ->with('_get_contexts_for_userid', [$userid])
100 ->willReturn($contextlist);
102 test_legacy_polyfill_request_provider::$mock = $mock;
103 $result = test_legacy_polyfill_request_provider::get_contexts_for_userid($userid);
104 $this->assertSame($contextlist, $result);
108 * Test that the local\request\core_user_preference_provider polyfill works and that the static
109 * _export_user_data can be successfully called.
111 * @covers ::export_user_data
113 public function test_export_user_data() {
114 $contextlist = new approved_contextlist(\core_user::get_user_by_username('admin'), 'core_privacy', [98]);
116 $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
117 $mock->expects($this->once())
118 ->method('get_return_value')
119 ->with('_export_user_data', [$contextlist]);
121 test_legacy_polyfill_request_provider::$mock = $mock;
122 test_legacy_polyfill_request_provider::export_user_data($contextlist);
126 * Test that the local\request\core_user_preference_provider polyfill works and that the static
127 * _delete_data_for_all_users_in_context can be successfully called.
129 * @covers ::delete_data_for_all_users_in_context
131 public function test_delete_data_for_all_users_in_context() {
132 $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
133 $mock->expects($this->once())
134 ->method('get_return_value')
135 ->with('_delete_data_for_all_users_in_context', [\context_system::instance()]);
137 test_legacy_polyfill_request_provider::$mock = $mock;
138 test_legacy_polyfill_request_provider::delete_data_for_all_users_in_context(\context_system::instance());
142 * Test that the local\request\core_user_preference_provider polyfill works and that the static
143 * _delete_data_for_user can be successfully called.
145 * @covers ::delete_data_for_user
147 public function test_delete_data_for_user() {
148 $contextlist = new approved_contextlist(\core_user::get_user_by_username('admin'), 'core_privacy', [98]);
150 $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
151 $mock->expects($this->once())
152 ->method('get_return_value')
153 ->with('_delete_data_for_user', [$contextlist]);
155 test_legacy_polyfill_request_provider::$mock = $mock;
156 test_legacy_polyfill_request_provider::delete_data_for_user($contextlist);
161 * Legacy polyfill test for the null provider.
163 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
164 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
166 class test_legacy_polyfill_null_provider implements \core_privacy\local\metadata\null_provider {
168 use \core_privacy\local\legacy_polyfill;
171 * Test for get_reason
173 protected static function _get_reason() {
174 return 'thisisareason';
179 * Legacy polyfill test for the metadata provider.
181 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
182 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
184 class test_legacy_polyfill_metadata_provider implements \core_privacy\local\metadata\provider {
186 use \core_privacy\local\legacy_polyfill;
189 * Test for get_metadata.
191 * @param collection $collection The initialised collection to add items to.
192 * @return collection A listing of user data stored through this system.
194 protected static function _get_metadata(collection $collection) {
195 return $collection;
200 * Legacy polyfill test for the metadata provider.
202 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
203 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
205 class test_legacy_polyfill_user_preference_provider implements \core_privacy\local\request\user_preference_provider {
207 use \core_privacy\local\legacy_polyfill;
210 * @var test_legacy_polyfill_request_provider $mock
212 public static $mock = null;
215 * Export all user preferences for the plugin.
217 * @param int $userid The userid of the user whose data is to be exported.
219 protected static function _export_user_preferences($userid) {
220 return static::$mock->get_return_value(__FUNCTION__, func_get_args());
225 * Legacy polyfill test for the request provider.
227 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
228 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
230 class test_legacy_polyfill_request_provider implements \core_privacy\local\request\core_user_data_provider {
232 use \core_privacy\local\legacy_polyfill;
235 * @var test_legacy_polyfill_request_provider $mock
237 public static $mock = null;
240 * Test for get_contexts_for_userid.
242 * @param int $userid The user to search.
243 * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
245 protected static function _get_contexts_for_userid($userid) {
246 return static::$mock->get_return_value(__FUNCTION__, func_get_args());
250 * Test for export_user_data.
252 * @param approved_contextlist $contextlist The approved contexts to export information for.
254 protected static function _export_user_data(approved_contextlist $contextlist) {
255 return static::$mock->get_return_value(__FUNCTION__, func_get_args());
260 * Delete all use data which matches the specified deletion criteria.
262 * @param context $context The specific context to delete data for.
264 public static function _delete_data_for_all_users_in_context(\context $context) {
265 return static::$mock->get_return_value(__FUNCTION__, func_get_args());
269 * Delete all user data for the specified user, in the specified contexts.
271 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
273 public static function _delete_data_for_user(approved_contextlist $contextlist) {
274 return static::$mock->get_return_value(__FUNCTION__, func_get_args());
278 class test_legacy_polyfill_mock_wrapper {
280 * Get the return value for the specified item.
282 public function get_return_value() {}