|
25 | 25 |
|
26 | 26 | namespace mod_customcert;
|
27 | 27 |
|
| 28 | +use completion_info; |
28 | 29 | use stdClass;
|
29 | 30 | use context_course;
|
30 | 31 | use advanced_testcase;
|
@@ -436,4 +437,85 @@ public function test_email_certificates_students_havent_met_required_time(): voi
|
436 | 437 | // Confirm no emails were sent.
|
437 | 438 | $this->assertCount(0, $emails);
|
438 | 439 | }
|
| 440 | + |
| 441 | + /** |
| 442 | + * Tests the email certificate task when the student has not met the completion criteria. |
| 443 | + * |
| 444 | + * @covers \mod_customcert\task\email_certificate_task |
| 445 | + */ |
| 446 | + public function test_email_certificates_students_havent_met_required_criteria(): void { |
| 447 | + global $CFG, $DB; |
| 448 | + |
| 449 | + $CFG->enablecompletion = true; |
| 450 | + |
| 451 | + // Create a course. |
| 452 | + $course = $this->getDataGenerator()->create_course(); |
| 453 | + |
| 454 | + // Create a user. |
| 455 | + $user1 = $this->getDataGenerator()->create_user(); |
| 456 | + |
| 457 | + // Enrol them in the course. |
| 458 | + $this->getDataGenerator()->enrol_user($user1->id, $course->id); |
| 459 | + |
| 460 | + // Create a quiz. |
| 461 | + $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]); |
| 462 | + |
| 463 | + $quizmodule = $DB->get_record('course_modules', ['id' => $quiz->cmid]); |
| 464 | + |
| 465 | + // Set completion criteria for the quiz. |
| 466 | + $quizmodule->completion = COMPLETION_TRACKING_AUTOMATIC; |
| 467 | + $quizmodule->completionview = 1; // Require view to complete. |
| 468 | + $quizmodule->completionexpected = 0; |
| 469 | + $DB->update_record('course_modules', $quizmodule); |
| 470 | + |
| 471 | + // Set restrict access to the customcert activity based on the completion of the quiz. |
| 472 | + $customcert = $this->getDataGenerator()->create_module('customcert', [ |
| 473 | + 'course' => $course->id, |
| 474 | + 'emailstudents' => 1, |
| 475 | + 'availability' => json_encode( |
| 476 | + [ |
| 477 | + 'op' => '&', |
| 478 | + 'c' => [ |
| 479 | + [ |
| 480 | + 'type' => 'completion', |
| 481 | + 'cm' => $quiz->cmid, |
| 482 | + 'e' => COMPLETION_COMPLETE, // Ensure the quiz is marked as complete. |
| 483 | + ], |
| 484 | + ], |
| 485 | + 'showc' => [ |
| 486 | + false |
| 487 | + ], |
| 488 | + ] |
| 489 | + ) |
| 490 | + ]); |
| 491 | + |
| 492 | + // Create template object. |
| 493 | + $template = new stdClass(); |
| 494 | + $template->id = $customcert->templateid; |
| 495 | + $template->name = 'A template'; |
| 496 | + $template->contextid = context_course::instance($course->id)->id; |
| 497 | + $template = new template($template); |
| 498 | + |
| 499 | + // Add a page to this template. |
| 500 | + $pageid = $template->add_page(); |
| 501 | + |
| 502 | + // Add an element to the page. |
| 503 | + $element = new stdClass(); |
| 504 | + $element->pageid = $pageid; |
| 505 | + $element->name = 'Image'; |
| 506 | + $DB->insert_record('customcert_elements', $element); |
| 507 | + |
| 508 | + // Run the task. |
| 509 | + $sink = $this->redirectEmails(); |
| 510 | + $task = new email_certificate_task(); |
| 511 | + $task->execute(); |
| 512 | + $emails = $sink->get_messages(); |
| 513 | + |
| 514 | + // Confirm there are no issues as the user can not view the certificate. |
| 515 | + $issues = $DB->get_records('customcert_issues'); |
| 516 | + $this->assertCount(0, $issues); |
| 517 | + |
| 518 | + // Confirm no emails were sent. |
| 519 | + $this->assertCount(0, $emails); |
| 520 | + } |
439 | 521 | }
|
0 commit comments