Skip to content

Commit 25f3ed1

Browse files
committed
Added unit test (#659)
1 parent 869d7bc commit 25f3ed1

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

tests/email_certificate_task_test.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,4 +722,68 @@ public function test_email_certificates_adhoc(): void {
722722
$this->assertCount(2, $issues);
723723
$this->assertCount(0, $emails);
724724
}
725+
726+
/**
727+
* Tests that we still issue a certificate if there are none when 'certificateexecutionperiod' is set.
728+
*
729+
* @covers \mod_customcert\task\issue_certificates_task
730+
*/
731+
public function test_issue_certificates_task_creates_issue_when_none_exist(): void {
732+
global $CFG, $DB;
733+
734+
// Create a course.
735+
$course = $this->getDataGenerator()->create_course();
736+
737+
// Create a student user.
738+
$student = $this->getDataGenerator()->create_user();
739+
740+
// Enrol the student in the course.
741+
$this->getDataGenerator()->enrol_user($student->id, $course->id);
742+
743+
// Create a custom certificate module with emailing enabled for students.
744+
$customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id,
745+
'emailstudents' => 1]);
746+
747+
// Set up a basic certificate template.
748+
$template = new \stdClass();
749+
$template->id = $customcert->templateid;
750+
$template->name = 'Test Template';
751+
$template->contextid = \context_course::instance($course->id)->id;
752+
$template = new template($template);
753+
754+
// Add a page and an element to put the certificate in a valid state.
755+
$pageid = $template->add_page();
756+
$element = new \stdClass();
757+
$element->pageid = $pageid;
758+
$element->name = 'Test Element';
759+
$DB->insert_record('customcert_elements', $element);
760+
761+
// Verify that no certificate issues exist before task execution.
762+
$this->assertEmpty($DB->get_records('customcert_issues'),
763+
'No certificate issues should exist before executing the task.');
764+
765+
// Redirect emails to a sink so we can capture any outgoing messages.
766+
$sink = $this->redirectEmails();
767+
768+
set_config('certificateexecutionperiod', 1, 'customcert');
769+
770+
// Execute the issue certificates task.
771+
$task = new \mod_customcert\task\issue_certificates_task();
772+
$task->execute();
773+
774+
// After executing the task, verify that a certificate issue record was created.
775+
$issues = $DB->get_records('customcert_issues');
776+
$this->assertCount(1, $issues,
777+
'A certificate issue record should have been created by the task.');
778+
$issue = reset($issues);
779+
$this->assertEquals(1, $issue->emailed,
780+
'The certificate issue should be marked as emailed.');
781+
782+
// Verify that an email was sent to the student.
783+
$emails = $sink->get_messages();
784+
$this->assertCount(1, $emails, 'An email should have been sent to the student.');
785+
$this->assertEquals($CFG->noreplyaddress, $emails[0]->from, 'Email sender is incorrect.');
786+
$this->assertEquals($student->email, $emails[0]->to, 'Email recipient is incorrect.');
787+
}
788+
725789
}

0 commit comments

Comments
 (0)