|
26 | 26 | namespace mod_customcert;
|
27 | 27 |
|
28 | 28 | use completion_info;
|
| 29 | +use context_module; |
29 | 30 | use stdClass;
|
30 | 31 | use context_course;
|
31 | 32 | use advanced_testcase;
|
@@ -223,6 +224,85 @@ public function test_email_certificates_students(): void {
|
223 | 224 | $this->assertCount(0, $emails);
|
224 | 225 | }
|
225 | 226 |
|
| 227 | + /** |
| 228 | + * Tests the email certificate task for instance on the site home course. |
| 229 | + * |
| 230 | + * @covers \mod_customcert\task\issue_certificates_task |
| 231 | + * @covers \mod_customcert\task\email_certificate_task |
| 232 | + */ |
| 233 | + public function test_email_certificates_sitehome(): void { |
| 234 | + global $CFG, $DB, $SITE; |
| 235 | + |
| 236 | + // Create some users. |
| 237 | + $user1 = $this->getDataGenerator()->create_user(); |
| 238 | + $user2 = $this->getDataGenerator()->create_user(); |
| 239 | + |
| 240 | + // Create a custom certificate. |
| 241 | + $customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $SITE->id, |
| 242 | + 'emailstudents' => 1]); |
| 243 | + |
| 244 | + $role = $DB->get_record('role', ['archetype' => 'user']); |
| 245 | + role_change_permission($role->id, context_module::instance($customcert->cmid), 'mod/customcert:view', CAP_ALLOW); |
| 246 | + role_change_permission($role->id, context_module::instance($customcert->cmid), 'mod/customcert:receiveissue', CAP_ALLOW); |
| 247 | + |
| 248 | + // Create template object. |
| 249 | + $template = new stdClass(); |
| 250 | + $template->id = $customcert->templateid; |
| 251 | + $template->name = 'A template'; |
| 252 | + $template->contextid = context_course::instance($SITE->id)->id; |
| 253 | + $template = new template($template); |
| 254 | + |
| 255 | + // Add a page to this template. |
| 256 | + $pageid = $template->add_page(); |
| 257 | + |
| 258 | + // Add an element to the page. |
| 259 | + $element = new stdClass(); |
| 260 | + $element->pageid = $pageid; |
| 261 | + $element->name = 'Image'; |
| 262 | + $DB->insert_record('customcert_elements', $element); |
| 263 | + |
| 264 | + // Ok, now issue this to one user. |
| 265 | + \mod_customcert\certificate::issue_certificate($customcert->id, $user1->id); |
| 266 | + |
| 267 | + // Confirm there is only one entry in this table. |
| 268 | + $this->assertEquals(1, $DB->count_records('customcert_issues')); |
| 269 | + |
| 270 | + // Run the task. |
| 271 | + $sink = $this->redirectEmails(); |
| 272 | + $task = new issue_certificates_task(); |
| 273 | + $task->execute(); |
| 274 | + $emails = $sink->get_messages(); |
| 275 | + |
| 276 | + // Get the issues from the issues table now. |
| 277 | + $issues = $DB->get_records('customcert_issues'); |
| 278 | + $this->assertCount(3, $issues); |
| 279 | + |
| 280 | + // Confirm that it was marked as emailed and was not issued to the teacher. |
| 281 | + foreach ($issues as $issue) { |
| 282 | + $this->assertEquals(1, $issue->emailed); |
| 283 | + } |
| 284 | + |
| 285 | + // Confirm that we sent out emails to the two users. |
| 286 | + $this->assertCount(3, $emails); |
| 287 | + |
| 288 | + $this->assertEquals($CFG->noreplyaddress, $emails[1]->from); |
| 289 | + $this->assertEquals($user1->email, $emails[1]->to); |
| 290 | + |
| 291 | + $this->assertEquals($CFG->noreplyaddress, $emails[2]->from); |
| 292 | + $this->assertEquals($user2->email, $emails[2]->to); |
| 293 | + |
| 294 | + // Now, run the task again and ensure we did not issue any more certificates. |
| 295 | + $sink = $this->redirectEmails(); |
| 296 | + $task = new issue_certificates_task(); |
| 297 | + $task->execute(); |
| 298 | + $emails = $sink->get_messages(); |
| 299 | + |
| 300 | + $issues = $DB->get_records('customcert_issues'); |
| 301 | + |
| 302 | + $this->assertCount(3, $issues); |
| 303 | + $this->assertCount(0, $emails); |
| 304 | + } |
| 305 | + |
226 | 306 | /**
|
227 | 307 | * Tests the email certificate task for teachers.
|
228 | 308 | *
|
|
0 commit comments