Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions tests/event/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,87 @@ public function test_deleting_an_element(): void {
$this->assertEquals($templateupdatedevent->contextid, \context_system::instance()->id);
$this->assertDebuggingNotCalled();
}

/**
* Tests that the issue_created event is fired correctly.
*/
public function test_issue_created_event(): void {
global $DB;

$this->resetAfterTest();

$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_user();
$customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
$context = \context_module::instance($customcert->cmid);

// Simulate certificate issue.
$issue = new \stdClass();
$issue->userid = $user->id;
$issue->customcertid = $customcert->id;
$issue->timecreated = time();
$issueid = $DB->insert_record('customcert_issues', $issue);

$sink = $this->redirectEvents();

// Trigger event manually.
\mod_customcert\event\issue_created::create([
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than triggering this manually can we please call the functions that trigger this and then check the event is called?

'objectid' => $issueid,
'context' => $context,
'relateduserid' => $user->id,
'userid' => $user->id,
])->trigger();

$events = $sink->get_events();
$this->assertCount(1, $events);

$event = reset($events);
$this->assertInstanceOf(\mod_customcert\event\issue_created::class, $event);
$this->assertEquals($issueid, $event->objectid);
$this->assertEquals($context->id, $event->contextid);
$this->assertDebuggingNotCalled();
}

/**
* Tests that the issue_deleted event is fired correctly.
*/
public function test_issue_deleted_event(): void {
global $DB;

$this->resetAfterTest();

$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_user();
$customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
$context = \context_module::instance($customcert->cmid);

// Create an issue.
$issue = new \stdClass();
$issue->userid = $user->id;
$issue->customcertid = $customcert->id;
$issue->timecreated = time();
$issueid = $DB->insert_record('customcert_issues', $issue);

$sink = $this->redirectEvents();

// Simulate deleting the issue.
$DB->delete_records('customcert_issues', ['id' => $issueid]);

// Trigger event manually.
\mod_customcert\event\issue_deleted::create([
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than triggering this manually can we please call the functions that trigger this and then check the event is called?

'objectid' => $issueid,
'context' => $context,
'relateduserid' => $user->id,
'userid' => $user->id,
])->trigger();

$events = $sink->get_events();
$this->assertCount(1, $events);

$event = reset($events);
$this->assertInstanceOf(\mod_customcert\event\issue_deleted::class, $event);
$this->assertEquals($issueid, $event->objectid);
$this->assertEquals($context->id, $event->contextid);
$this->assertDebuggingNotCalled();
}
}
Loading