MDL-61307 core_privacy: Add legacy polyfill for contrib plugins
[moodle.git] / privacy / tests / legacy_polyfill_test.php
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/>.
17 /**
18  * Unit tests for the privacy legacy polyfill.
19  *
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  */
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\deletion_criteria;
33 use \core_privacy\local\request\approved_contextlist;
35 /**
36  * Tests for the \core_privacy API's types\user_preference functionality.
37  * Unit tests for the Privacy API's legacy_polyfill.
38  *
39  * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
40  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41  */
42 class core_privacy_legacy_polyfill_test extends advanced_testcase {
43     /**
44      * Test that the null_provider polyfill works and that the static _get_reason can be
45      * successfully called.
46      */
47     public function test_null_provider() {
48         $this->assertEquals('thisisareason', test_legacy_polyfill_null_provider::get_reason());
49     }
51     /**
52      * Test that the metdata\provider polyfill works and that the static _get_metadata can be
53      * successfully called.
54      */
55     public function test_metadata_provider() {
56         $collection = new collection('core_privacy');
57         $this->assertSame($collection, test_legacy_polyfill_metadata_provider::get_metadata($collection));
58     }
60     /**
61      * Test that the local\request\user_preference_provider polyfill works and that the static
62      * _export_user_preferences can be successfully called.
63      */
64     public function test_user_preference_provider() {
65         $userid = 417;
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])
71             ->willReturn(null);
73         test_legacy_polyfill_user_preference_provider::$mock = $mock;
74         test_legacy_polyfill_user_preference_provider::export_user_preferences($userid);
75     }
77     /**
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.
80      */
81     public function test_get_contexts_for_userid() {
82         $userid = 417;
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);
94     }
96     /**
97      * Test that the local\request\core_user_preference_provider polyfill works and that the static
98      * _export_user_data can be successfully called.
99      */
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);
110     }
112     /**
113      * Test that the local\request\core_user_preference_provider polyfill works and that the static
114      * _delete_for_context can be successfully called.
115      */
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);
126     }
128     /**
129      * Test that the local\request\core_user_preference_provider polyfill works and that the static
130      * _delete_user_data can be successfully called.
131      */
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);
142     }
145 /**
146  * Legacy polyfill test for the null provider.
147  *
148  * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
149  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
150  */
151 class test_legacy_polyfill_null_provider implements \core_privacy\local\metadata\null_provider {
153     use \core_privacy\local\legacy_polyfill;
155     /**
156      * Test for get_reason
157      */
158     protected static function _get_reason() {
159         return 'thisisareason';
160     }
163 /**
164  * Legacy polyfill test for the metadata provider.
165  *
166  * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
167  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
168  */
169 class test_legacy_polyfill_metadata_provider implements \core_privacy\local\metadata\provider {
171     use \core_privacy\local\legacy_polyfill;
173     /**
174      * Test for get_metadata.
175      *
176      * @param   collection     $collection The initialised collection to add items to.
177      * @return  collection     A listing of user data stored through this system.
178      */
179     protected static function _get_metadata(collection $collection) {
180         return $collection;
181     }
184 /**
185  * Legacy polyfill test for the metadata provider.
186  *
187  * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
188  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
189  */
190 class test_legacy_polyfill_user_preference_provider implements \core_privacy\local\request\user_preference_provider {
192     use \core_privacy\local\legacy_polyfill;
194     /**
195      * @var test_legacy_polyfill_request_provider $mock
196      */
197     public static $mock = null;
199     /**
200      * Export all user preferences for the plugin.
201      *
202      * @param   int         $userid The userid of the user whose data is to be exported.
203      */
204     protected static function _export_user_preferences($userid) {
205         return static::$mock->get_return_value(__FUNCTION__, func_get_args());
206     }
209 /**
210  * Legacy polyfill test for the request provider.
211  *
212  * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
213  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
214  */
215 class test_legacy_polyfill_request_provider implements \core_privacy\local\request\core_user_data_provider {
217     use \core_privacy\local\legacy_polyfill;
219     /**
220      * @var test_legacy_polyfill_request_provider $mock
221      */
222     public static $mock = null;
224     /**
225      * Test for get_contexts_for_userid.
226      *
227      * @param   int         $userid     The user to search.
228      * @return  contextlist   $contextlist  The contextlist containing the list of contexts used in this plugin.
229      */
230     protected static function _get_contexts_for_userid($userid) {
231         return static::$mock->get_return_value(__FUNCTION__, func_get_args());
232     }
234     /**
235      * Test for export_user_data.
236      *
237      * @param   approved_contextlist    $contextlist    The approved contexts to export information for.
238      */
239     protected static function _export_user_data(approved_contextlist $contextlist) {
240         return static::$mock->get_return_value(__FUNCTION__, func_get_args());
241     }
244     /**
245      * Delete all use data which matches the specified deletion_criteria.
246      *
247      * @param   deletion_criteria       $criteria   An object containing specific deletion criteria to delete for.
248      */
249     public static function _delete_for_context(deletion_criteria $criteria) {
250         return static::$mock->get_return_value(__FUNCTION__, func_get_args());
251     }
253     /**
254      * Delete all user data for the specified user, in the specified contexts.
255      *
256      * @param   approved_contextlist    $contextlist    The approved contexts and user information to delete information for.
257      */
258     public static function _delete_user_data(approved_contextlist $contextlist) {
259         return static::$mock->get_return_value(__FUNCTION__, func_get_args());
260     }
263 class test_legacy_polyfill_mock_wrapper {
264     /**
265      * Get the return value for the specified item.
266      */
267     public function get_return_value() {}