Proyectos de Subversion Moodle

Rev

| 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 provider tests.
19
 *
20
 * @package    mod_customcert
21
 * @copyright  2018 Mark Nelson <markn@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace mod_customcert;
26
 
27
use stdClass;
28
use context_module;
29
use context_system;
30
use mod_customcert\privacy\provider;
31
 
32
/**
33
 * Privacy provider tests class.
34
 *
35
 * @package    mod_customcert
36
 * @copyright  2018 Mark Nelson <markn@moodle.com>
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class privacy_provider_test extends \core_privacy\tests\provider_testcase {
40
 
41
    /**
42
     * Test for provider::get_contexts_for_userid().
43
     *
44
     * @covers \provider::get_contexts_for_userid
45
     */
46
    public function test_get_contexts_for_userid() {
47
        $this->resetAfterTest();
48
 
49
        $course = $this->getDataGenerator()->create_course();
50
 
51
        // The customcert activity the user will have an issue from.
52
        $customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
53
 
54
        // Another customcert activity that has no issued certificates.
55
        $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
56
 
57
        // Create a user who will be issued a certificate.
58
        $user = $this->getDataGenerator()->create_user();
59
 
60
        // Issue the certificate.
61
        $this->create_certificate_issue($customcert->id, $user->id);
62
 
63
        // Check the context supplied is correct.
64
        $contextlist = provider::get_contexts_for_userid($user->id);
65
        $this->assertCount(1, $contextlist);
66
 
67
        $contextformodule = $contextlist->current();
68
        $cmcontext = context_module::instance($customcert->cmid);
69
        $this->assertEquals($cmcontext->id, $contextformodule->id);
70
    }
71
 
72
    /**
73
     * Test for provider::get_users_in_context().
74
     *
75
     * @covers \provider::get_users_in_context()
76
     */
77
    public function test_get_users_in_context() {
78
        $this->resetAfterTest();
79
 
80
        $course = $this->getDataGenerator()->create_course();
81
 
82
        // The customcert activity the user will have an issue from.
83
        $customcert1 = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
84
        $customcert2 = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
85
 
86
        // Call get_users_in_context() when the customcert hasn't any user.
87
        $cm = get_coursemodule_from_instance('customcert', $customcert1->id);
88
        $cmcontext = context_module::instance($cm->id);
89
        $userlist = new \core_privacy\local\request\userlist($cmcontext, 'mod_customcert');
90
        provider::get_users_in_context($userlist);
91
 
92
        // Check no user has been returned.
93
        $this->assertCount(0, $userlist->get_userids());
94
 
95
        // Create some users who will be issued a certificate.
96
        $user1 = $this->getDataGenerator()->create_user();
97
        $user2 = $this->getDataGenerator()->create_user();
98
        $user3 = $this->getDataGenerator()->create_user();
99
        $this->create_certificate_issue($customcert1->id, $user1->id);
100
        $this->create_certificate_issue($customcert1->id, $user2->id);
101
        $this->create_certificate_issue($customcert2->id, $user3->id);
102
 
103
        // Call get_users_in_context() again.
104
        provider::get_users_in_context($userlist);
105
 
106
        // Check this time there are 2 users.
107
        $this->assertCount(2, $userlist->get_userids());
108
        $this->assertContains((int) $user1->id, $userlist->get_userids());
109
        $this->assertContains((int) $user2->id, $userlist->get_userids());
110
        $this->assertNotContains((int) $user3->id, $userlist->get_userids());
111
    }
112
 
113
    /**
114
     * Test for provider::get_users_in_context() with invalid context type.
115
     *
116
     * @covers \provider::get_users_in_context()
117
     */
118
    public function test_get_users_in_context_invalid_context_type() {
119
        $systemcontext = context_system::instance();
120
 
121
        $userlist = new \core_privacy\local\request\userlist($systemcontext, 'mod_customcert');
122
        \mod_customcert\privacy\provider::get_users_in_context($userlist);
123
 
124
        $this->assertCount(0, $userlist->get_userids());
125
    }
126
 
127
    /**
128
     * Test for provider::export_user_data().
129
     *
130
     * @covers \provider::export_user_data()
131
     */
132
    public function test_export_for_context() {
133
        $this->resetAfterTest();
134
 
135
        $course = $this->getDataGenerator()->create_course();
136
 
137
        $customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
138
 
139
        // Create users who will be issued a certificate.
140
        $user1 = $this->getDataGenerator()->create_user();
141
        $user2 = $this->getDataGenerator()->create_user();
142
 
143
        $this->create_certificate_issue($customcert->id, $user1->id);
144
        $this->create_certificate_issue($customcert->id, $user2->id);
145
 
146
        // Export all of the data for the context for user 1.
147
        $cmcontext = context_module::instance($customcert->cmid);
148
        $this->export_context_data_for_user($user1->id, $cmcontext, 'mod_customcert');
149
        $writer = \core_privacy\local\request\writer::with_context($cmcontext);
150
 
151
        $this->assertTrue($writer->has_any_data());
152
 
153
        $data = $writer->get_data();
154
        $this->assertCount(1, $data->issues);
155
 
156
        $issues = $data->issues;
157
        foreach ($issues as $issue) {
158
            $this->assertArrayHasKey('code', $issue);
159
            $this->assertArrayHasKey('emailed', $issue);
160
            $this->assertArrayHasKey('timecreated', $issue);
161
        }
162
    }
163
 
164
    /**
165
     * Test for provider::delete_data_for_all_users_in_context().
166
     *
167
     * @covers \provider::delete_data_for_all_users_in_context()
168
     */
169
    public function test_delete_data_for_all_users_in_context() {
170
        global $DB;
171
 
172
        $this->resetAfterTest();
173
 
174
        $course = $this->getDataGenerator()->create_course();
175
 
176
        $customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
177
        $customcert2 = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
178
 
179
        // Create users who will be issued a certificate.
180
        $user1 = $this->getDataGenerator()->create_user();
181
        $user2 = $this->getDataGenerator()->create_user();
182
 
183
        $this->create_certificate_issue($customcert->id, $user1->id);
184
        $this->create_certificate_issue($customcert->id, $user2->id);
185
 
186
        $this->create_certificate_issue($customcert2->id, $user1->id);
187
        $this->create_certificate_issue($customcert2->id, $user2->id);
188
 
189
        // Before deletion, we should have 2 issued certificates for the first certificate.
190
        $count = $DB->count_records('customcert_issues', ['customcertid' => $customcert->id]);
191
        $this->assertEquals(2, $count);
192
 
193
        // Delete data based on context.
194
        $cmcontext = context_module::instance($customcert->cmid);
195
        provider::delete_data_for_all_users_in_context($cmcontext);
196
 
197
        // After deletion, the issued certificates for the activity should have been deleted.
198
        $count = $DB->count_records('customcert_issues', ['customcertid' => $customcert->id]);
199
        $this->assertEquals(0, $count);
200
 
201
        // We should still have the issues for the second certificate.
202
        $count = $DB->count_records('customcert_issues', ['customcertid' => $customcert2->id]);
203
        $this->assertEquals(2, $count);
204
    }
205
 
206
    /**
207
     * Test for provider::delete_data_for_user().
208
     *
209
     * @covers \provider::delete_data_for_user()
210
     */
211
    public function test_delete_data_for_user() {
212
        global $DB;
213
 
214
        $this->resetAfterTest();
215
 
216
        $course = $this->getDataGenerator()->create_course();
217
 
218
        $customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
219
 
220
        // Create users who will be issued a certificate.
221
        $user1 = $this->getDataGenerator()->create_user();
222
        $user2 = $this->getDataGenerator()->create_user();
223
 
224
        $this->create_certificate_issue($customcert->id, $user1->id);
225
        $this->create_certificate_issue($customcert->id, $user2->id);
226
 
227
        // Before deletion we should have 2 issued certificates.
228
        $count = $DB->count_records('customcert_issues', ['customcertid' => $customcert->id]);
229
        $this->assertEquals(2, $count);
230
 
231
        $context = \context_module::instance($customcert->cmid);
232
        $contextlist = new \core_privacy\local\request\approved_contextlist($user1, 'customcert',
233
            [$context->id]);
234
        provider::delete_data_for_user($contextlist);
235
 
236
        // After deletion, the issued certificates for the first user should have been deleted.
237
        $count = $DB->count_records('customcert_issues', ['customcertid' => $customcert->id, 'userid' => $user1->id]);
238
        $this->assertEquals(0, $count);
239
 
240
        // Check the issue for the other user is still there.
241
        $customcertissue = $DB->get_records('customcert_issues');
242
        $this->assertCount(1, $customcertissue);
243
        $lastissue = reset($customcertissue);
244
        $this->assertEquals($user2->id, $lastissue->userid);
245
    }
246
 
247
    /**
248
     * Test for provider::delete_data_for_users().
249
     *
250
     * @covers \provider::delete_data_for_users()
251
     */
252
    public function test_delete_data_for_users() {
253
        global $DB;
254
 
255
        $this->resetAfterTest();
256
 
257
        // Create course, customcert and users who will be issued a certificate.
258
        $course = $this->getDataGenerator()->create_course();
259
        $customcert1 = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
260
        $customcert2 = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
261
        $cm1 = get_coursemodule_from_instance('customcert', $customcert1->id);
262
        $cm2 = get_coursemodule_from_instance('customcert', $customcert2->id);
263
        $user1 = $this->getDataGenerator()->create_user();
264
        $user2 = $this->getDataGenerator()->create_user();
265
        $user3 = $this->getDataGenerator()->create_user();
266
        $this->create_certificate_issue($customcert1->id, $user1->id);
267
        $this->create_certificate_issue($customcert1->id, $user2->id);
268
        $this->create_certificate_issue($customcert1->id, $user3->id);
269
        $this->create_certificate_issue($customcert2->id, $user1->id);
270
        $this->create_certificate_issue($customcert2->id, $user2->id);
271
 
272
        // Before deletion we should have 3 + 2 issued certificates.
273
        $count = $DB->count_records('customcert_issues', ['customcertid' => $customcert1->id]);
274
        $this->assertEquals(3, $count);
275
        $count = $DB->count_records('customcert_issues', ['customcertid' => $customcert2->id]);
276
        $this->assertEquals(2, $count);
277
 
278
        $context1 = context_module::instance($cm1->id);
279
        $approveduserlist = new \core_privacy\local\request\approved_userlist($context1, 'customcert',
280
                [$user1->id, $user2->id]);
281
        provider::delete_data_for_users($approveduserlist);
282
 
283
        // After deletion, the customcert of the 2 students provided above should have been deleted
284
        // from the activity. So there should only remain 1 certificate which is for $user3.
285
        $customcertissues1 = $DB->get_records('customcert_issues', ['customcertid' => $customcert1->id]);
286
        $this->assertCount(1, $customcertissues1);
287
        $lastissue = reset($customcertissues1);
288
        $this->assertEquals($user3->id, $lastissue->userid);
289
 
290
        // Confirm that the certificates issues in the other activity are intact.
291
        $customcertissues1 = $DB->get_records('customcert_issues', ['customcertid' => $customcert2->id]);
292
        $this->assertCount(2, $customcertissues1);
293
    }
294
 
295
    /**
296
     * Mimicks the creation of a customcert issue.
297
     *
298
     * There is no API we can use to insert an customcert issue, so we
299
     * will simply insert directly into the database.
300
     *
301
     * @param int $customcertid
302
     * @param int $userid
303
     */
304
    protected function create_certificate_issue(int $customcertid, int $userid) {
305
        global $DB;
306
 
307
        static $i = 1;
308
 
309
        $customcertissue = new stdClass();
310
        $customcertissue->customcertid = $customcertid;
311
        $customcertissue->userid = $userid;
312
        $customcertissue->code = certificate::generate_code();
313
        $customcertissue->timecreated = time() + $i;
314
 
315
        // Insert the record into the database.
316
        $DB->insert_record('customcert_issues', $customcertissue);
317
 
318
        $i++;
319
    }
320
}