Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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/>.
16
 
17
/**
18
 * Unit tests for the Privacy API's 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
 */
25
 
26
namespace core_privacy;
27
 
28
use core_privacy\local\metadata\collection;
29
use core_privacy\local\request\contextlist;
30
use core_privacy\local\request\approved_contextlist;
31
 
32
defined('MOODLE_INTERNAL') || die();
33
 
34
global $CFG;
35
 
36
/**
37
 * Unit tests for the Privacy API's legacy_polyfill.
38
 *
39
 * @package     core_privacy
40
 * @category    test
41
 * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
42
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43
 * @coversDefaultClass \core_privacy\local\legacy_polyfill
44
 */
45
class legacy_polyfill_test extends \advanced_testcase {
46
    /**
47
     * Test that the null_provider polyfill works and that the static _get_reason can be
48
     * successfully called.
49
     *
50
     * @covers ::get_reason
51
     */
11 efrain 52
    public function test_null_provider(): void {
1 efrain 53
        $this->assertEquals('thisisareason', test_legacy_polyfill_null_provider::get_reason());
54
    }
55
 
56
    /**
57
     * Test that the metdata\provider polyfill works and that the static _get_metadata can be
58
     * successfully called.
59
     *
60
     * @covers ::get_metadata
61
     */
11 efrain 62
    public function test_metadata_provider(): void {
1 efrain 63
        $collection = new collection('core_privacy');
64
        $this->assertSame($collection, test_legacy_polyfill_metadata_provider::get_metadata($collection));
65
    }
66
 
67
    /**
68
     * Test that the local\request\user_preference_provider polyfill works and that the static
69
     * _export_user_preferences can be successfully called.
70
     *
71
     * @covers ::export_user_preferences
72
     */
11 efrain 73
    public function test_user_preference_provider(): void {
1 efrain 74
        $userid = 417;
75
 
76
        $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
77
        $mock->expects($this->once())
78
            ->method('get_return_value')
79
            ->with('_export_user_preferences', [$userid]);
80
 
81
        test_legacy_polyfill_user_preference_provider::$mock = $mock;
82
        test_legacy_polyfill_user_preference_provider::export_user_preferences($userid);
83
    }
84
 
85
    /**
86
     * Test that the local\request\core_user_preference_provider polyfill works and that the static
87
     * _get_contexts_for_userid can be successfully called.
88
     *
89
     * @covers ::get_contexts_for_userid
90
     */
11 efrain 91
    public function test_get_contexts_for_userid(): void {
1 efrain 92
        $userid = 417;
93
        $contextlist = new contextlist('core_privacy');
94
 
95
        $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
96
        $mock->expects($this->once())
97
            ->method('get_return_value')
98
            ->with('_get_contexts_for_userid', [$userid])
99
            ->willReturn($contextlist);
100
 
101
        test_legacy_polyfill_request_provider::$mock = $mock;
102
        $result = test_legacy_polyfill_request_provider::get_contexts_for_userid($userid);
103
        $this->assertSame($contextlist, $result);
104
    }
105
 
106
    /**
107
     * Test that the local\request\core_user_preference_provider polyfill works and that the static
108
     * _export_user_data can be successfully called.
109
     *
110
     * @covers ::export_user_data
111
     */
11 efrain 112
    public function test_export_user_data(): void {
1 efrain 113
        $contextlist = new approved_contextlist(\core_user::get_user_by_username('admin'), 'core_privacy', [98]);
114
 
115
        $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
116
        $mock->expects($this->once())
117
            ->method('get_return_value')
118
            ->with('_export_user_data', [$contextlist]);
119
 
120
        test_legacy_polyfill_request_provider::$mock = $mock;
121
        test_legacy_polyfill_request_provider::export_user_data($contextlist);
122
    }
123
 
124
    /**
125
     * Test that the local\request\core_user_preference_provider polyfill works and that the static
126
     * _delete_data_for_all_users_in_context can be successfully called.
127
     *
128
     * @covers ::delete_data_for_all_users_in_context
129
     */
11 efrain 130
    public function test_delete_data_for_all_users_in_context(): void {
1 efrain 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_all_users_in_context', [\context_system::instance()]);
135
 
136
        test_legacy_polyfill_request_provider::$mock = $mock;
137
        test_legacy_polyfill_request_provider::delete_data_for_all_users_in_context(\context_system::instance());
138
    }
139
 
140
    /**
141
     * Test that the local\request\core_user_preference_provider polyfill works and that the static
142
     * _delete_data_for_user can be successfully called.
143
     *
144
     * @covers ::delete_data_for_user
145
     */
11 efrain 146
    public function test_delete_data_for_user(): void {
1 efrain 147
        $contextlist = new approved_contextlist(\core_user::get_user_by_username('admin'), 'core_privacy', [98]);
148
 
149
        $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class);
150
        $mock->expects($this->once())
151
            ->method('get_return_value')
152
            ->with('_delete_data_for_user', [$contextlist]);
153
 
154
        test_legacy_polyfill_request_provider::$mock = $mock;
155
        test_legacy_polyfill_request_provider::delete_data_for_user($contextlist);
156
    }
157
}
158
 
159
/**
160
 * Legacy polyfill test for the null provider.
161
 *
162
 * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
163
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
164
 */
165
class test_legacy_polyfill_null_provider implements \core_privacy\local\metadata\null_provider {
166
 
167
    use \core_privacy\local\legacy_polyfill;
168
 
169
    /**
170
     * Test for get_reason
171
     */
172
    protected static function _get_reason() {
173
        return 'thisisareason';
174
    }
175
}
176
 
177
/**
178
 * Legacy polyfill test for the metadata provider.
179
 *
180
 * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
181
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
182
 */
183
class test_legacy_polyfill_metadata_provider implements \core_privacy\local\metadata\provider {
184
 
185
    use \core_privacy\local\legacy_polyfill;
186
 
187
    /**
188
     * Test for get_metadata.
189
     *
190
     * @param   collection     $collection The initialised collection to add items to.
191
     * @return  collection     A listing of user data stored through this system.
192
     */
193
    protected static function _get_metadata(collection $collection) {
194
        return $collection;
195
    }
196
}
197
 
198
/**
199
 * Legacy polyfill test for the metadata provider.
200
 *
201
 * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
202
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
203
 */
204
class test_legacy_polyfill_user_preference_provider implements \core_privacy\local\request\user_preference_provider {
205
 
206
    use \core_privacy\local\legacy_polyfill;
207
 
208
    /**
209
     * @var test_legacy_polyfill_request_provider $mock
210
     */
211
    public static $mock = null;
212
 
213
    /**
214
     * Export all user preferences for the plugin.
215
     *
216
     * @param   int         $userid The userid of the user whose data is to be exported.
217
     */
218
    protected static function _export_user_preferences($userid) {
219
        return static::$mock->get_return_value(__FUNCTION__, func_get_args());
220
    }
221
}
222
 
223
/**
224
 * Legacy polyfill test for the request provider.
225
 *
226
 * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
227
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
228
 */
229
class test_legacy_polyfill_request_provider implements \core_privacy\local\request\core_user_data_provider {
230
 
231
    use \core_privacy\local\legacy_polyfill;
232
 
233
    /**
234
     * @var test_legacy_polyfill_request_provider $mock
235
     */
236
    public static $mock = null;
237
 
238
    /**
239
     * Test for get_contexts_for_userid.
240
     *
241
     * @param   int         $userid     The user to search.
242
     * @return  contextlist   $contextlist  The contextlist containing the list of contexts used in this plugin.
243
     */
244
    protected static function _get_contexts_for_userid($userid) {
245
        return static::$mock->get_return_value(__FUNCTION__, func_get_args());
246
    }
247
 
248
    /**
249
     * Test for export_user_data.
250
     *
251
     * @param   approved_contextlist    $contextlist    The approved contexts to export information for.
252
     */
253
    protected static function _export_user_data(approved_contextlist $contextlist) {
254
        return static::$mock->get_return_value(__FUNCTION__, func_get_args());
255
    }
256
 
257
 
258
    /**
259
     * Delete all use data which matches the specified deletion criteria.
260
     *
261
     * @param   context         $context   The specific context to delete data for.
262
     */
263
    public static function _delete_data_for_all_users_in_context(\context $context) {
264
        return static::$mock->get_return_value(__FUNCTION__, func_get_args());
265
    }
266
 
267
    /**
268
     * Delete all user data for the specified user, in the specified contexts.
269
     *
270
     * @param   approved_contextlist    $contextlist    The approved contexts and user information to delete information for.
271
     */
272
    public static function _delete_data_for_user(approved_contextlist $contextlist) {
273
        return static::$mock->get_return_value(__FUNCTION__, func_get_args());
274
    }
275
}
276
 
277
class test_legacy_polyfill_mock_wrapper {
278
    /**
279
     * Get the return value for the specified item.
280
     */
281
    public function get_return_value() {}
282
}