Proyectos de Subversion Moodle

Rev

Rev 11 | | 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
 * Privacy tests for enrol_flatfile.
19
 *
20
 * @package    enrol_flatfile
21
 * @category   test
22
 * @copyright  2018 Jake Dallimore <jrhdallimore@gmail.com>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
namespace enrol_flatfile\privacy;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
use core_privacy\local\metadata\collection;
30
use core_privacy\tests\provider_testcase;
31
use core_privacy\local\request\approved_contextlist;
32
use core_privacy\local\request\writer;
33
use enrol_flatfile\privacy\provider;
34
 
35
/**
36
 * Privacy tests for enrol_flatfile.
37
 *
38
 * @copyright  2018 Jake Dallimore <jrhdallimore@gmail.com>
39
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
1441 ariadna 41
final class provider_test extends provider_testcase {
1 efrain 42
 
43
    /** @var \stdClass $user1 a test user.*/
44
    protected $user1;
45
 
46
    /** @var \stdClass $user2 a test user.*/
47
    protected $user2;
48
 
49
    /** @var \stdClass $user3 a test user.*/
50
    protected $user3;
51
 
52
    /** @var \stdClass $user4 a test user.*/
53
    protected $user4;
54
 
55
    /** @var \context $coursecontext1 a course context.*/
56
    protected $coursecontext1;
57
 
58
    /** @var \context $coursecontext2 a course context.*/
59
    protected $coursecontext2;
60
 
61
    /** @var \context $coursecontext3 a course context.*/
62
    protected $coursecontext3;
63
 
64
    /**
65
     * Called before every test.
66
     */
67
    public function setUp(): void {
1441 ariadna 68
        parent::setUp();
1 efrain 69
        $this->resetAfterTest(true);
70
    }
71
 
72
    /**
73
     * Verify that get_metadata returns the database table mapping.
74
     */
11 efrain 75
    public function test_get_metadata(): void {
1 efrain 76
        $collection = new collection('enrol_flatfile');
77
        $collection = provider::get_metadata($collection);
78
        $collectiondata = $collection->get_collection();
79
        $this->assertNotEmpty($collectiondata);
80
        $this->assertInstanceOf(\core_privacy\local\metadata\types\database_table::class, $collectiondata[0]);
81
    }
82
 
83
    /**
84
     * Verify that the relevant course contexts are returned for users with pending enrolment records.
85
     */
11 efrain 86
    public function test_get_contexts_for_user(): void {
1 efrain 87
        global $DB;
88
        // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
89
        $this->create_future_enrolments();
90
 
91
        $this->assertEquals(5, $DB->count_records('enrol_flatfile'));
92
 
93
        // We expect to see 2 entries for user1, in course1 and course3.
94
        $contextlist = provider::get_contexts_for_userid($this->user1->id);
95
        $this->assertEquals(2, $contextlist->count());
96
        $contextids = $contextlist->get_contextids();
97
        $this->assertContainsEquals($this->coursecontext1->id, $contextids);
98
        $this->assertContainsEquals($this->coursecontext3->id, $contextids);
99
 
100
        // And 1 for user2 on course2.
101
        $contextlist = provider::get_contexts_for_userid($this->user2->id);
102
        $this->assertEquals(1, $contextlist->count());
103
        $contextids = $contextlist->get_contextids();
104
        $this->assertContainsEquals($this->coursecontext2->id, $contextids);
105
    }
106
 
107
    /**
108
     * Verify the export includes any future enrolment records for the user.
109
     */
11 efrain 110
    public function test_export_user_data(): void {
1 efrain 111
        // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
112
        $this->create_future_enrolments();
113
 
114
        // Get contexts containing user data.
115
        $contextlist = provider::get_contexts_for_userid($this->user1->id);
116
        $this->assertEquals(2, $contextlist->count());
117
 
118
        $approvedcontextlist = new approved_contextlist(
119
            $this->user1,
120
            'enrol_flatfile',
121
            $contextlist->get_contextids()
122
        );
123
 
124
        // Export for the approved contexts.
125
        provider::export_user_data($approvedcontextlist);
126
 
127
        // Verify we see one future course enrolment in course1, and one in course3.
128
        $subcontext = \core_enrol\privacy\provider::get_subcontext([get_string('pluginname', 'enrol_flatfile')]);
129
 
130
        $writer = writer::with_context($this->coursecontext1);
131
        $this->assertNotEmpty($writer->get_data($subcontext));
132
 
133
        $writer = writer::with_context($this->coursecontext3);
134
        $this->assertNotEmpty($writer->get_data($subcontext));
135
 
136
        // Verify we have nothing in course 2 for this user.
137
        $writer = writer::with_context($this->coursecontext2);
138
        $this->assertEmpty($writer->get_data($subcontext));
139
    }
140
 
141
    /**
142
     * Verify export will limit any future enrolment records to only those contextids provided.
143
     */
11 efrain 144
    public function test_export_user_data_restricted_context_subset(): void {
1 efrain 145
        // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
146
        $this->create_future_enrolments();
147
 
148
        // Now, limit the export scope to just course1's context and verify only that data is seen in any export.
149
        $subsetapprovedcontextlist = new approved_contextlist(
150
            $this->user1,
151
            'enrol_flatfile',
152
            [$this->coursecontext1->id]
153
        );
154
 
155
        // Export for the approved contexts.
156
        provider::export_user_data($subsetapprovedcontextlist);
157
 
158
        // Verify we see one future course enrolment in course1 only.
159
        $subcontext = \core_enrol\privacy\provider::get_subcontext([get_string('pluginname', 'enrol_flatfile')]);
160
 
161
        $writer = writer::with_context($this->coursecontext1);
162
        $this->assertNotEmpty($writer->get_data($subcontext));
163
 
164
        // And nothing in the course3 context.
165
        $writer = writer::with_context($this->coursecontext3);
166
        $this->assertEmpty($writer->get_data($subcontext));
167
    }
168
 
169
    /**
170
     * Verify that records can be deleted by context.
171
     */
11 efrain 172
    public function test_delete_data_for_all_users_in_context(): void {
1 efrain 173
        global $DB;
174
        // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
175
        $this->create_future_enrolments();
176
 
177
        // Verify we have 3 future enrolments for course 1.
178
        $this->assertEquals(3, $DB->count_records('enrol_flatfile', ['courseid' => $this->coursecontext1->instanceid]));
179
 
180
        // Now, run delete by context and confirm that all records are removed.
181
        provider::delete_data_for_all_users_in_context($this->coursecontext1);
182
        $this->assertEquals(0, $DB->count_records('enrol_flatfile', ['courseid' => $this->coursecontext1->instanceid]));
183
    }
184
 
11 efrain 185
    public function test_delete_data_for_user(): void {
1 efrain 186
        global $DB;
187
        // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
188
        $this->create_future_enrolments();
189
 
190
        // Verify we have 2 future enrolments for course 1 and course 3.
191
        $contextlist = provider::get_contexts_for_userid($this->user1->id);
192
        $this->assertEquals(2, $contextlist->count());
193
        $contextids = $contextlist->get_contextids();
194
        $this->assertContainsEquals($this->coursecontext1->id, $contextids);
195
        $this->assertContainsEquals($this->coursecontext3->id, $contextids);
196
 
197
        $approvedcontextlist = new approved_contextlist(
198
            $this->user1,
199
            'enrol_flatfile',
200
            $contextids
201
        );
202
 
203
        // Now, run delete for user and confirm that both records are removed.
204
        provider::delete_data_for_user($approvedcontextlist);
205
        $contextlist = provider::get_contexts_for_userid($this->user1->id);
206
        $this->assertEquals(0, $contextlist->count());
207
        $this->assertEquals(0, $DB->count_records('enrol_flatfile', ['userid' => $this->user1->id]));
208
    }
209
 
210
    /**
211
     * Test for provider::get_users_in_context().
212
     */
11 efrain 213
    public function test_get_users_in_context(): void {
1 efrain 214
        global $DB;
215
        // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
216
        $this->create_future_enrolments();
217
 
218
        $this->assertEquals(5, $DB->count_records('enrol_flatfile'));
219
 
220
        // We expect to see 3 entries for course1, and that's user1, user3 and user4.
221
        $userlist = new \core_privacy\local\request\userlist($this->coursecontext1, 'enrol_flatfile');
222
        provider::get_users_in_context($userlist);
223
        $this->assertEqualsCanonicalizing(
224
                [$this->user1->id, $this->user3->id, $this->user4->id],
225
                $userlist->get_userids());
226
 
227
        // And 1 for course2 which is for user2.
228
        $userlist = new \core_privacy\local\request\userlist($this->coursecontext2, 'enrol_flatfile');
229
        provider::get_users_in_context($userlist);
230
        $this->assertEquals([$this->user2->id], $userlist->get_userids());
231
 
232
        // And 1 for course3 which is for user1 again.
233
        $userlist = new \core_privacy\local\request\userlist($this->coursecontext3, 'enrol_flatfile');
234
        provider::get_users_in_context($userlist);
235
        $this->assertEquals([$this->user1->id], $userlist->get_userids());
236
    }
237
 
238
    /**
239
     * Test for provider::delete_data_for_users().
240
     */
11 efrain 241
    public function test_delete_data_for_users(): void {
1 efrain 242
        global $DB;
243
 
244
        // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
245
        $this->create_future_enrolments();
246
 
247
        // Verify we have 3 future enrolment for user 1, user 3 and user 4.
248
        $userlist = new \core_privacy\local\request\userlist($this->coursecontext1, 'enrol_flatfile');
249
        provider::get_users_in_context($userlist);
250
        $this->assertEqualsCanonicalizing(
251
                [$this->user1->id, $this->user3->id, $this->user4->id],
252
                $userlist->get_userids());
253
 
254
        $approveduserlist = new \core_privacy\local\request\approved_userlist($this->coursecontext1, 'enrol_flatfile',
255
                [$this->user1->id, $this->user3->id]);
256
 
257
        // Now, run delete for user and confirm that the record is removed.
258
        provider::delete_data_for_users($approveduserlist);
259
        $userlist = new \core_privacy\local\request\userlist($this->coursecontext1, 'enrol_flatfile');
260
        provider::get_users_in_context($userlist);
261
        $this->assertEquals([$this->user4->id], $userlist->get_userids());
262
        $this->assertEquals(
263
                [$this->user4->id],
264
                $DB->get_fieldset_select('enrol_flatfile', 'userid', 'courseid = ?', [$this->coursecontext1->instanceid])
265
        );
266
    }
267
 
268
    /**
269
     * Helper to sync a file and create the enrol_flatfile DB entries, for use with the get, export and delete tests.
270
     */
271
    protected function create_future_enrolments() {
272
        global $CFG;
273
        $this->user1 = $this->getDataGenerator()->create_user(['idnumber' => 'u1']);
274
        $this->user2 = $this->getDataGenerator()->create_user(['idnumber' => 'u2']);
275
        $this->user3 = $this->getDataGenerator()->create_user(['idnumber' => 'u3']);
276
        $this->user4 = $this->getDataGenerator()->create_user(['idnumber' => 'u4']);
277
 
278
        $course1 = $this->getDataGenerator()->create_course(['idnumber' => 'c1']);
279
        $course2 = $this->getDataGenerator()->create_course(['idnumber' => 'c2']);
280
        $course3 = $this->getDataGenerator()->create_course(['idnumber' => 'c3']);
281
        $this->coursecontext1 = \context_course::instance($course1->id);
282
        $this->coursecontext2 = \context_course::instance($course2->id);
283
        $this->coursecontext3 = \context_course::instance($course3->id);
284
 
285
        $now = time();
286
        $future = $now + 60 * 60 * 5;
287
        $farfuture = $now + 60 * 60 * 24 * 5;
288
 
289
        $file = "$CFG->dataroot/enrol.txt";
290
        $data = "add,student,u1,c1,$future,0
291
                 add,student,u2,c2,$future,0
292
                 add,student,u3,c1,$future,0
293
                 add,student,u4,c1,$future,0
294
                 add,student,u1,c3,$future,$farfuture";
295
        file_put_contents($file, $data);
296
 
297
        $trace = new \null_progress_trace();
298
        $this->enable_plugin();
299
        $flatfileplugin = enrol_get_plugin('flatfile');
300
        $flatfileplugin->set_config('location', $file);
301
        $flatfileplugin->sync($trace);
302
    }
303
 
304
    /**
305
     * Enables the flatfile plugin for testing.
306
     */
307
    protected function enable_plugin() {
308
        $enabled = enrol_get_plugins(true);
309
        $enabled['flatfile'] = true;
310
        $enabled = array_keys($enabled);
311
        set_config('enrol_plugins_enabled', implode(',', $enabled));
312
    }
313
}