Skip to content

Commit

Permalink
Unit test to ensure student gets certificate when criteria met
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Aug 26, 2024
1 parent c8556e6 commit 92850b0
Showing 1 changed file with 86 additions and 1 deletion.
87 changes: 86 additions & 1 deletion tests/email_certificate_task_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public function test_email_certificates_students_havent_met_required_criteria():
$CFG->enablecompletion = true;

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

// Create a user.
$user1 = $this->getDataGenerator()->create_user();
Expand Down Expand Up @@ -518,4 +518,89 @@ public function test_email_certificates_students_havent_met_required_criteria():
// Confirm no emails were sent.
$this->assertCount(0, $emails);
}

/**
* Tests the email certificate task when the student has not met the completion criteria.
*
* @covers \mod_customcert\task\email_certificate_task
*/
public function test_email_certificates_students_have_met_required_criteria(): void {
global $CFG, $DB;

$CFG->enablecompletion = true;

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

// Create a user.
$user1 = $this->getDataGenerator()->create_user();

// Enrol them in the course.
$this->getDataGenerator()->enrol_user($user1->id, $course->id);

// Create a quiz.
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);

$quizmodule = $DB->get_record('course_modules', ['id' => $quiz->cmid]);

// Set completion criteria for the quiz.
$quizmodule->completion = COMPLETION_TRACKING_AUTOMATIC;
$quizmodule->completionview = 1; // Require view to complete.
$quizmodule->completionexpected = 0;
$DB->update_record('course_modules', $quizmodule);

// Mark the quiz as complete for the user.
$completion = new completion_info($course);
$completion->update_state($quizmodule, COMPLETION_COMPLETE, $user1->id);

// Set restrict access to the customcert activity based on the completion of the quiz.
$customcert = $this->getDataGenerator()->create_module('customcert', [
'course' => $course->id,
'emailstudents' => 1,
'availability' => json_encode(
[
'op' => '&',
'c' => [
[
'type' => 'completion',
'cm' => $quiz->cmid,
'e' => COMPLETION_COMPLETE, // Ensure the quiz is marked as complete.
],
],
'showc' => [
false,
],
],
),
]);

// Create template object.
$template = new stdClass();
$template->id = $customcert->templateid;
$template->name = 'A template';
$template->contextid = context_course::instance($course->id)->id;
$template = new template($template);

// Add a page to this template.
$pageid = $template->add_page();

// Add an element to the page.
$element = new stdClass();
$element->pageid = $pageid;
$element->name = 'Image';
$DB->insert_record('customcert_elements', $element);

// Run the task.
$sink = $this->redirectEmails();
$task = new email_certificate_task();
$task->execute();
$emails = $sink->get_messages();

// Confirm there is an issue as the user can view the certificate.
$issues = $DB->get_records('customcert_issues');
$this->assertCount(1, $issues);

// Confirm an email was sent.
$this->assertCount(1, $emails);
}
}

0 comments on commit 92850b0

Please sign in to comment.