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
 * File contains the unit tests for the email certificate task.
19
 *
20
 * @package    mod_customcert
21
 * @category   test
22
 * @copyright  2017 Mark Nelson <markn@moodle.com>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace mod_customcert;
27
 
28
use stdClass;
29
use context_course;
30
use advanced_testcase;
31
use mod_customcert\task\email_certificate_task;
32
 
33
/**
34
 * Unit tests for the email certificate task.
35
 *
36
 * @package    mod_customcert
37
 * @category   test
38
 * @copyright  2017 Mark Nelson <markn@moodle.com>
39
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 * @coversDefaultClass \mod_customcert\task\email_certificate_task
41
 */
42
class email_certificate_task_test extends advanced_testcase {
43
 
44
    /**
45
     * Test set up.
46
     */
47
    public function setUp(): void {
48
        $this->resetAfterTest();
49
    }
50
 
51
    /**
52
     * Tests the email certificate task when there are no elements.
53
     *
54
     * @covers \mod_customcert\task\email_certificate_task
55
     */
56
    public function test_email_certificates_no_elements() {
57
        // Create a course.
58
        $course = $this->getDataGenerator()->create_course();
59
 
60
        // Create a user.
61
        $user1 = $this->getDataGenerator()->create_user();
62
 
63
        // Create a custom certificate with no elements.
64
        $this->getDataGenerator()->create_module('customcert', ['course' => $course->id, 'emailstudents' => 1]);
65
 
66
        // Enrol the user as a student.
67
        $this->getDataGenerator()->enrol_user($user1->id, $course->id);
68
 
69
        // Run the task.
70
        $sink = $this->redirectEmails();
71
        $task = new email_certificate_task();
72
        $task->execute();
73
        $emails = $sink->get_messages();
74
 
75
        // Confirm that we did not send any emails because the certificate has no elements.
76
        $this->assertCount(0, $emails);
77
    }
78
 
79
    /**
80
     * Tests the email certificate task for users without a capability to receive a certificate.
81
     *
82
     * @covers \mod_customcert\task\email_certificate_task
83
     */
84
    public function test_email_certificates_no_cap() {
85
        global $DB;
86
 
87
        // Create a course.
88
        $course = $this->getDataGenerator()->create_course();
89
 
90
        // Create some users.
91
        $user1 = $this->getDataGenerator()->create_user();
92
        $user2 = $this->getDataGenerator()->create_user();
93
 
94
        // Enrol two of them in the course as students but revoke their right to receive a certificate issue.
95
        $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
96
        $this->getDataGenerator()->enrol_user($user1->id, $course->id);
97
        $this->getDataGenerator()->enrol_user($user2->id, $course->id);
98
 
99
        unassign_capability('mod/customcert:receiveissue', $roleids['student']);
100
 
101
        // Create a custom certificate.
102
        $customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id, 'emailstudents' => 1]);
103
 
104
        // Create template object.
105
        $template = new stdClass();
106
        $template->id = $customcert->templateid;
107
        $template->name = 'A template';
108
        $template->contextid = context_course::instance($course->id)->id;
109
        $template = new template($template);
110
 
111
        // Add a page to this template.
112
        $pageid = $template->add_page();
113
 
114
        // Add an element to the page.
115
        $element = new stdClass();
116
        $element->pageid = $pageid;
117
        $element->name = 'Image';
118
        $DB->insert_record('customcert_elements', $element);
119
 
120
        // Run the task.
121
        $sink = $this->redirectEmails();
122
        $task = new email_certificate_task();
123
        $task->execute();
124
        $emails = $sink->get_messages();
125
 
126
        // Confirm that we did not send any emails.
127
        $this->assertCount(0, $emails);
128
    }
129
 
130
    /**
131
     * Tests the email certificate task for students.
132
     *
133
     * @covers \mod_customcert\task\email_certificate_task
134
     */
135
    public function test_email_certificates_students() {
136
        global $CFG, $DB;
137
 
138
        // Create a course.
139
        $course = $this->getDataGenerator()->create_course();
140
 
141
        // Create some users.
142
        $user1 = $this->getDataGenerator()->create_user();
143
        $user2 = $this->getDataGenerator()->create_user();
144
        $user3 = $this->getDataGenerator()->create_user(['firstname' => 'Teacher', 'lastname' => 'One']);
145
 
146
        // Enrol two of them in the course as students.
147
        $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
148
        $this->getDataGenerator()->enrol_user($user1->id, $course->id);
149
        $this->getDataGenerator()->enrol_user($user2->id, $course->id);
150
 
151
        // Enrol one of the users as a teacher.
152
        $this->getDataGenerator()->enrol_user($user3->id, $course->id, $roleids['editingteacher']);
153
 
154
        // Create a custom certificate.
155
        $customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id,
156
            'emailstudents' => 1]);
157
 
158
        // Create template object.
159
        $template = new stdClass();
160
        $template->id = $customcert->templateid;
161
        $template->name = 'A template';
162
        $template->contextid = context_course::instance($course->id)->id;
163
        $template = new template($template);
164
 
165
        // Add a page to this template.
166
        $pageid = $template->add_page();
167
 
168
        // Add an element to the page.
169
        $element = new stdClass();
170
        $element->pageid = $pageid;
171
        $element->name = 'Image';
172
        $DB->insert_record('customcert_elements', $element);
173
 
174
        // Ok, now issue this to one user.
175
        \mod_customcert\certificate::issue_certificate($customcert->id, $user1->id);
176
 
177
        // Confirm there is only one entry in this table.
178
        $this->assertEquals(1, $DB->count_records('customcert_issues'));
179
 
180
        // Run the task.
181
        $sink = $this->redirectEmails();
182
        $task = new email_certificate_task();
183
        $task->execute();
184
        $emails = $sink->get_messages();
185
 
186
        // Get the issues from the issues table now.
187
        $issues = $DB->get_records('customcert_issues');
188
        $this->assertCount(2, $issues);
189
 
190
        // Confirm that it was marked as emailed and was not issued to the teacher.
191
        foreach ($issues as $issue) {
192
            $this->assertEquals(1, $issue->emailed);
193
            $this->assertNotEquals($user3->id, $issue->userid);
194
        }
195
 
196
        // Confirm that we sent out emails to the two users.
197
        $this->assertCount(2, $emails);
198
 
199
        $this->assertEquals($CFG->noreplyaddress, $emails[0]->from);
200
        $this->assertEquals($user1->email, $emails[0]->to);
201
 
202
        $this->assertEquals($CFG->noreplyaddress, $emails[1]->from);
203
        $this->assertEquals($user2->email, $emails[1]->to);
204
 
205
        // Now, run the task again and ensure we did not issue any more certificates.
206
        $sink = $this->redirectEmails();
207
        $task = new email_certificate_task();
208
        $task->execute();
209
        $emails = $sink->get_messages();
210
 
211
        $issues = $DB->get_records('customcert_issues');
212
 
213
        $this->assertCount(2, $issues);
214
        $this->assertCount(0, $emails);
215
    }
216
 
217
    /**
218
     * Tests the email certificate task for teachers.
219
     *
220
     * @covers \mod_customcert\task\email_certificate_task
221
     */
222
    public function test_email_certificates_teachers() {
223
        global $CFG, $DB;
224
 
225
        // Create a course.
226
        $course = $this->getDataGenerator()->create_course();
227
 
228
        // Create some users.
229
        $user1 = $this->getDataGenerator()->create_user();
230
        $user2 = $this->getDataGenerator()->create_user();
231
        $user3 = $this->getDataGenerator()->create_user(['firstname' => 'Teacher', 'lastname' => 'One']);
232
 
233
        // Enrol two of them in the course as students.
234
        $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
235
        $this->getDataGenerator()->enrol_user($user1->id, $course->id);
236
        $this->getDataGenerator()->enrol_user($user2->id, $course->id);
237
 
238
        // Enrol one of the users as a teacher.
239
        $this->getDataGenerator()->enrol_user($user3->id, $course->id, $roleids['editingteacher']);
240
 
241
        // Create a custom certificate.
242
        $customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id,
243
            'emailteachers' => 1]);
244
 
245
        // Create template object.
246
        $template = new stdClass();
247
        $template->id = $customcert->templateid;
248
        $template->name = 'A template';
249
        $template->contextid = context_course::instance($course->id)->id;
250
        $template = new template($template);
251
 
252
        // Add a page to this template.
253
        $pageid = $template->add_page();
254
 
255
        // Add an element to the page.
256
        $element = new stdClass();
257
        $element->pageid = $pageid;
258
        $element->name = 'Image';
259
        $DB->insert_record('customcert_elements', $element);
260
 
261
        // Run the task.
262
        $sink = $this->redirectEmails();
263
        $task = new email_certificate_task();
264
        $task->execute();
265
        $emails = $sink->get_messages();
266
 
267
        // Confirm that we only sent out 2 emails, both emails to the teacher for the two students.
268
        $this->assertCount(2, $emails);
269
 
270
        $this->assertEquals($CFG->noreplyaddress, $emails[0]->from);
271
        $this->assertEquals($user3->email, $emails[0]->to);
272
 
273
        $this->assertEquals($CFG->noreplyaddress, $emails[1]->from);
274
        $this->assertEquals($user3->email, $emails[1]->to);
275
    }
276
 
277
    /**
278
     * Tests the email certificate task for others.
279
     *
280
     * @covers \mod_customcert\task\email_certificate_task
281
     */
282
    public function test_email_certificates_others() {
283
        global $CFG, $DB;
284
 
285
        // Create a course.
286
        $course = $this->getDataGenerator()->create_course();
287
 
288
        // Create some users.
289
        $user1 = $this->getDataGenerator()->create_user();
290
        $user2 = $this->getDataGenerator()->create_user();
291
 
292
        // Enrol two of them in the course as students.
293
        $this->getDataGenerator()->enrol_user($user1->id, $course->id);
294
        $this->getDataGenerator()->enrol_user($user2->id, $course->id);
295
 
296
        // Create a custom certificate.
297
        $customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id,
298
            'emailothers' => 'testcustomcert@example.com, doo@dah']);
299
 
300
        // Create template object.
301
        $template = new stdClass();
302
        $template->id = $customcert->templateid;
303
        $template->name = 'A template';
304
        $template->contextid = context_course::instance($course->id)->id;
305
        $template = new template($template);
306
 
307
        // Add a page to this template.
308
        $pageid = $template->add_page();
309
 
310
        // Add an element to the page.
311
        $element = new stdClass();
312
        $element->pageid = $pageid;
313
        $element->name = 'Image';
314
        $DB->insert_record('customcert_elements', $element);
315
 
316
        // Run the task.
317
        $sink = $this->redirectEmails();
318
        $task = new email_certificate_task();
319
        $task->execute();
320
        $emails = $sink->get_messages();
321
 
322
        // Confirm that we only sent out 2 emails, both emails to the other address that was valid for the two students.
323
        $this->assertCount(2, $emails);
324
 
325
        $this->assertEquals($CFG->noreplyaddress, $emails[0]->from);
326
        $this->assertEquals('testcustomcert@example.com', $emails[0]->to);
327
 
328
        $this->assertEquals($CFG->noreplyaddress, $emails[1]->from);
329
        $this->assertEquals('testcustomcert@example.com', $emails[1]->to);
330
    }
331
 
332
    /**
333
     * Tests the email certificate task when the certificate is not visible.
334
     *
335
     * @covers \mod_customcert\task\email_certificate_task
336
     */
337
    public function test_email_certificates_students_not_visible() {
338
        global $DB;
339
 
340
        // Create a course.
341
        $course = $this->getDataGenerator()->create_course();
342
 
343
        // Create a user.
344
        $user1 = $this->getDataGenerator()->create_user();
345
 
346
        // Enrol them in the course.
347
        $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
348
        $this->getDataGenerator()->enrol_user($user1->id, $course->id);
349
 
350
        // Create a custom certificate.
351
        $customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id, 'emailstudents' => 1]);
352
 
353
        // Create template object.
354
        $template = new stdClass();
355
        $template->id = $customcert->templateid;
356
        $template->name = 'A template';
357
        $template->contextid = context_course::instance($course->id)->id;
358
        $template = new template($template);
359
 
360
        // Add a page to this template.
361
        $pageid = $template->add_page();
362
 
363
        // Add an element to the page.
364
        $element = new stdClass();
365
        $element->pageid = $pageid;
366
        $element->name = 'Image';
367
        $DB->insert_record('customcert_elements', $element);
368
 
369
        // Remove the permission for the user to view the certificate.
370
        assign_capability('mod/customcert:view', CAP_PROHIBIT, $roleids['student'], \context_course::instance($course->id));
371
 
372
        // Run the task.
373
        $sink = $this->redirectEmails();
374
        $task = new email_certificate_task();
375
        $task->execute();
376
        $emails = $sink->get_messages();
377
 
378
        // Confirm there are no issues as the user did not have permissions to view it.
379
        $issues = $DB->get_records('customcert_issues');
380
        $this->assertCount(0, $issues);
381
 
382
        // Confirm no emails were sent.
383
        $this->assertCount(0, $emails);
384
    }
385
 
386
    /**
387
     * Tests the email certificate task when the student has not met the required time for the course.
388
     *
389
     * @covers \mod_customcert\task\email_certificate_task
390
     */
391
    public function test_email_certificates_students_havent_met_required_time() {
392
        global $DB;
393
 
394
        // Set the standard log to on.
395
        set_config('enabled_stores', 'logstore_standard', 'tool_log');
396
 
397
        // Create a course.
398
        $course = $this->getDataGenerator()->create_course();
399
 
400
        // Create a user.
401
        $user1 = $this->getDataGenerator()->create_user();
402
 
403
        // Enrol them in the course.
404
        $this->getDataGenerator()->enrol_user($user1->id, $course->id);
405
 
406
        // Create a custom certificate.
407
        $customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id, 'emailstudents' => 1,
408
            'requiredtime' => '60']);
409
 
410
        // Create template object.
411
        $template = new stdClass();
412
        $template->id = $customcert->templateid;
413
        $template->name = 'A template';
414
        $template->contextid = context_course::instance($course->id)->id;
415
        $template = new template($template);
416
 
417
        // Add a page to this template.
418
        $pageid = $template->add_page();
419
 
420
        // Add an element to the page.
421
        $element = new stdClass();
422
        $element->pageid = $pageid;
423
        $element->name = 'Image';
424
        $DB->insert_record('customcert_elements', $element);
425
 
426
        // Run the task.
427
        $sink = $this->redirectEmails();
428
        $task = new email_certificate_task();
429
        $task->execute();
430
        $emails = $sink->get_messages();
431
 
432
        // Confirm there are no issues as the user did not meet the required time.
433
        $issues = $DB->get_records('customcert_issues');
434
        $this->assertCount(0, $issues);
435
 
436
        // Confirm no emails were sent.
437
        $this->assertCount(0, $emails);
438
    }
439
}