MDL-62463 mod_glossary: Fix SQL query
[moodle.git] / privacy / tests / legacy_polyfill_test.php
blob17d0f6feae0b27015880b5040203ba9f446bf84f
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 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
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 use \core_privacy\local\metadata\collection;
31 use \core_privacy\local\request\contextlist;
32 use \core_privacy\local\request\approved_contextlist;
34 /**
35 * Tests for the \core_privacy API's types\user_preference functionality.
36 * Unit tests for the Privacy API's legacy_polyfill.
38 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class core_privacy_legacy_polyfill_test extends advanced_testcase {
42 /**
43 * Test that the null_provider polyfill works and that the static _get_reason can be
44 * successfully called.
46 public function test_null_provider() {
47 $this->assertEquals('thisisareason', test_legacy_polyfill_null_provider::get_reason());
50 /**
51 * Test that the metdata\provider polyfill works and that the static _get_metadata can be
52 * successfully called.
54 public function test_metadata_provider() {
55 $collection = new collection('core_privacy');
56 $this->assertSame($collection, test_legacy_polyfill_metadata_provider::get_metadata($collection));
59 /**
60 * Test that the local\request\user_preference_provider polyfill works and that the static
61 * _export_user_preferences can be successfully called.
63 public function test_user_preference_provider() {
64 $userid = 417;
66 $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
67 $mock->expects($this->once())
68 ->method('get_return_value')
69 ->with('_export_user_preferences', [$userid]);
71 test_legacy_polyfill_user_preference_provider::$mock = $mock;
72 test_legacy_polyfill_user_preference_provider::export_user_preferences($userid);
75 /**
76 * Test that the local\request\core_user_preference_provider polyfill works and that the static
77 * _get_contexts_for_userid can be successfully called.
79 public function test_get_contexts_for_userid() {
80 $userid = 417;
81 $contextlist = new contextlist('core_privacy');
83 $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
84 $mock->expects($this->once())
85 ->method('get_return_value')
86 ->with('_get_contexts_for_userid', [$userid])
87 ->willReturn($contextlist);
89 test_legacy_polyfill_request_provider::$mock = $mock;
90 $result = test_legacy_polyfill_request_provider::get_contexts_for_userid($userid);
91 $this->assertSame($contextlist, $result);
94 /**
95 * Test that the local\request\core_user_preference_provider polyfill works and that the static
96 * _export_user_data can be successfully called.
98 public function test_export_user_data() {
99 $contextlist = new approved_contextlist(\core_user::get_user_by_username('admin'), 'core_privacy', [98]);
101 $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
102 $mock->expects($this->once())
103 ->method('get_return_value')
104 ->with('_export_user_data', [$contextlist]);
106 test_legacy_polyfill_request_provider::$mock = $mock;
107 test_legacy_polyfill_request_provider::export_user_data($contextlist);
111 * Test that the local\request\core_user_preference_provider polyfill works and that the static
112 * _delete_data_for_all_users_in_context can be successfully called.
114 public function test_delete_data_for_all_users_in_context() {
115 $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
116 $mock->expects($this->once())
117 ->method('get_return_value')
118 ->with('_delete_data_for_all_users_in_context', [\context_system::instance()]);
120 test_legacy_polyfill_request_provider::$mock = $mock;
121 test_legacy_polyfill_request_provider::delete_data_for_all_users_in_context(\context_system::instance());
125 * Test that the local\request\core_user_preference_provider polyfill works and that the static
126 * _delete_data_for_user can be successfully called.
128 public function test_delete_data_for_user() {
129 $contextlist = new approved_contextlist(\core_user::get_user_by_username('admin'), 'core_privacy', [98]);
131 $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
132 $mock->expects($this->once())
133 ->method('get_return_value')
134 ->with('_delete_data_for_user', [$contextlist]);
136 test_legacy_polyfill_request_provider::$mock = $mock;
137 test_legacy_polyfill_request_provider::delete_data_for_user($contextlist);
142 * Legacy polyfill test for the null provider.
144 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
145 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
147 class test_legacy_polyfill_null_provider implements \core_privacy\local\metadata\null_provider {
149 use \core_privacy\local\legacy_polyfill;
152 * Test for get_reason
154 protected static function _get_reason() {
155 return 'thisisareason';
160 * Legacy polyfill test for the metadata provider.
162 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
163 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
165 class test_legacy_polyfill_metadata_provider implements \core_privacy\local\metadata\provider {
167 use \core_privacy\local\legacy_polyfill;
170 * Test for get_metadata.
172 * @param collection $collection The initialised collection to add items to.
173 * @return collection A listing of user data stored through this system.
175 protected static function _get_metadata(collection $collection) {
176 return $collection;
181 * Legacy polyfill test for the metadata provider.
183 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
184 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
186 class test_legacy_polyfill_user_preference_provider implements \core_privacy\local\request\user_preference_provider {
188 use \core_privacy\local\legacy_polyfill;
191 * @var test_legacy_polyfill_request_provider $mock
193 public static $mock = null;
196 * Export all user preferences for the plugin.
198 * @param int $userid The userid of the user whose data is to be exported.
200 protected static function _export_user_preferences($userid) {
201 return static::$mock->get_return_value(__FUNCTION__, func_get_args());
206 * Legacy polyfill test for the request provider.
208 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
209 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
211 class test_legacy_polyfill_request_provider implements \core_privacy\local\request\core_user_data_provider {
213 use \core_privacy\local\legacy_polyfill;
216 * @var test_legacy_polyfill_request_provider $mock
218 public static $mock = null;
221 * Test for get_contexts_for_userid.
223 * @param int $userid The user to search.
224 * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
226 protected static function _get_contexts_for_userid($userid) {
227 return static::$mock->get_return_value(__FUNCTION__, func_get_args());
231 * Test for export_user_data.
233 * @param approved_contextlist $contextlist The approved contexts to export information for.
235 protected static function _export_user_data(approved_contextlist $contextlist) {
236 return static::$mock->get_return_value(__FUNCTION__, func_get_args());
241 * Delete all use data which matches the specified deletion criteria.
243 * @param context $context The specific context to delete data for.
245 public static function _delete_data_for_all_users_in_context(\context $context) {
246 return static::$mock->get_return_value(__FUNCTION__, func_get_args());
250 * Delete all user data for the specified user, in the specified contexts.
252 * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
254 public static function _delete_data_for_user(approved_contextlist $contextlist) {
255 return static::$mock->get_return_value(__FUNCTION__, func_get_args());
259 class test_legacy_polyfill_mock_wrapper {
261 * Get the return value for the specified item.
263 public function get_return_value() {}