/** * Function to generate subscription data. * * @throws coding_exception if $record->ruleid or $record->userid not present. * @param \stdClass|array $record data to insert as subscription entry. * * @return \tool_monitor\subscription An instance of the subscription class. */ public function create_subscription($record = null) { if (!isset($record->timecreated)) { $record->timecreated = time(); } if (!isset($record->courseid)) { $record->courseid = 0; } if (!isset($record->ruleid)) { throw new coding_exception('$record->ruleid must be present in tool_monitor_generator::create_subscription()'); } if (!isset($record->cmid)) { $record->cmid = 0; } if (!isset($record->userid)) { throw new coding_exception('$record->userid must be present in tool_monitor_generator::create_subscription()'); } $sid = \tool_monitor\subscription_manager::create_subscription($record->ruleid, $record->courseid, $record->cmid, $record->userid); return \tool_monitor\subscription_manager::get_subscription($sid); }
/** * Test the subscription created event. */ public function test_subscription_created() { // Create the items we need to test this. $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(); $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor'); // Create a rule to subscribe to. $rule = $monitorgenerator->create_rule(); // Trigger and capture the event. $sink = $this->redirectEvents(); $subscriptionid = \tool_monitor\subscription_manager::create_subscription($rule->id, $course->id, 0, $user->id); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); // Confirm that the event contains the expected values. $this->assertInstanceOf('\\tool_monitor\\event\\subscription_created', $event); $this->assertEquals(context_course::instance($course->id), $event->get_context()); $this->assertEquals($subscriptionid, $event->objectid); $this->assertEventContextNotUsed($event); // Create a system subscription - trigger and capture the event. $sink = $this->redirectEvents(); \tool_monitor\subscription_manager::create_subscription($rule->id, 0, 0, $user->id); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); // Confirm that the event uses the system context. $this->assertInstanceOf('\\tool_monitor\\event\\subscription_created', $event); $this->assertEquals(context_system::instance(), $event->get_context()); }