Skip to content

Commit ff8ad9c

Browse files
committed
Unit test to ensure student gets certificate when criteria met
1 parent 3d819ee commit ff8ad9c

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

tests/email_certificate_task_test.php

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public function test_email_certificates_students_havent_met_required_criteria():
449449
$CFG->enablecompletion = true;
450450

451451
// Create a course.
452-
$course = $this->getDataGenerator()->create_course();
452+
$course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
453453

454454
// Create a user.
455455
$user1 = $this->getDataGenerator()->create_user();
@@ -518,4 +518,89 @@ public function test_email_certificates_students_havent_met_required_criteria():
518518
// Confirm no emails were sent.
519519
$this->assertCount(0, $emails);
520520
}
521+
522+
/**
523+
* Tests the email certificate task when the student has not met the completion criteria.
524+
*
525+
* @covers \mod_customcert\task\email_certificate_task
526+
*/
527+
public function test_email_certificates_students_have_met_required_criteria(): void {
528+
global $CFG, $DB;
529+
530+
$CFG->enablecompletion = true;
531+
532+
// Create a course.
533+
$course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
534+
535+
// Create a user.
536+
$user1 = $this->getDataGenerator()->create_user();
537+
538+
// Enrol them in the course.
539+
$this->getDataGenerator()->enrol_user($user1->id, $course->id);
540+
541+
// Create a quiz.
542+
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
543+
544+
$quizmodule = $DB->get_record('course_modules', ['id' => $quiz->cmid]);
545+
546+
// Set completion criteria for the quiz.
547+
$quizmodule->completion = COMPLETION_TRACKING_AUTOMATIC;
548+
$quizmodule->completionview = 1; // Require view to complete.
549+
$quizmodule->completionexpected = 0;
550+
$DB->update_record('course_modules', $quizmodule);
551+
552+
// Mark the quiz as complete for the user.
553+
$completion = new completion_info($course);
554+
$completion->update_state($quizmodule, COMPLETION_COMPLETE, $user1->id);
555+
556+
// Set restrict access to the customcert activity based on the completion of the quiz.
557+
$customcert = $this->getDataGenerator()->create_module('customcert', [
558+
'course' => $course->id,
559+
'emailstudents' => 1,
560+
'availability' => json_encode(
561+
[
562+
'op' => '&',
563+
'c' => [
564+
[
565+
'type' => 'completion',
566+
'cm' => $quiz->cmid,
567+
'e' => COMPLETION_COMPLETE, // Ensure the quiz is marked as complete.
568+
],
569+
],
570+
'showc' => [
571+
false,
572+
],
573+
],
574+
),
575+
]);
576+
577+
// Create template object.
578+
$template = new stdClass();
579+
$template->id = $customcert->templateid;
580+
$template->name = 'A template';
581+
$template->contextid = context_course::instance($course->id)->id;
582+
$template = new template($template);
583+
584+
// Add a page to this template.
585+
$pageid = $template->add_page();
586+
587+
// Add an element to the page.
588+
$element = new stdClass();
589+
$element->pageid = $pageid;
590+
$element->name = 'Image';
591+
$DB->insert_record('customcert_elements', $element);
592+
593+
// Run the task.
594+
$sink = $this->redirectEmails();
595+
$task = new email_certificate_task();
596+
$task->execute();
597+
$emails = $sink->get_messages();
598+
599+
// Confirm there is an issue as the user can view the certificate.
600+
$issues = $DB->get_records('customcert_issues');
601+
$this->assertCount(1, $issues);
602+
603+
// Confirm an email was sent.
604+
$this->assertCount(1, $emails);
605+
}
521606
}

0 commit comments

Comments
 (0)