} /// EDIT/CREATE/CANCEL operations => at the end redirect to add function page / main service page $mform = new external_service_form(null, $service); if ($mform->is_cancelled()) { redirect($returnurl); } else { if ($servicedata = $mform->get_data()) { $servicedata = (object) $servicedata; if (!empty($servicedata->requiredcapability) && $servicedata->requiredcapability == "norequiredcapability") { $servicedata->requiredcapability = ""; } //create operation if (empty($servicedata->id)) { $servicedata->id = $webservicemanager->add_external_service($servicedata); $params = array('objectid' => $servicedata->id); $event = \core\event\webservice_service_created::create($params); $event->add_record_snapshot('external_services', $servicedata); $event->trigger(); //redirect to the 'add functions to service' page $addfunctionpage = new moodle_url($CFG->wwwroot . '/' . $CFG->admin . '/webservice/service_functions.php', array('id' => $servicedata->id)); $returnurl = $addfunctionpage->out(false); } else { //update operation $webservicemanager->update_external_service($servicedata); $params = array('objectid' => $servicedata->id); $event = \core\event\webservice_service_updated::create($params); $event->add_record_snapshot('external_services', $servicedata); $event->trigger(); } redirect($returnurl); }
public function test_service_created() { global $CFG, $DB; // The Web service API doesn't allow the testing of the events directly by // calling some functions which trigger the events, so what we are going here // is just checking that the event returns the expected information. $sink = $this->redirectEvents(); // Creating a fake service. $service = (object) array('name' => 'Test', 'enabled' => 1, 'requiredcapability' => '', 'restrictedusers' => 0, 'component' => null, 'timecreated' => time(), 'timemodified' => time(), 'shortname' => null, 'downloadfiles' => 0, 'uploadfiles' => 0); $service->id = $DB->insert_record('external_services', $service); // Trigger the event. $params = array('objectid' => $service->id); $event = \core\event\webservice_service_created::create($params); $event->add_record_snapshot('external_services', $service); $event->trigger(); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); // Assert that the event contains the right information. $this->assertEquals(context_system::instance(), $event->get_context()); $this->assertEquals($service->id, $event->objectid); $returnurl = $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices"; $expected = array(SITEID, 'webservice', 'add', $returnurl, get_string('addservice', 'webservice', $service)); $this->assertEventLegacyLogData($expected, $event); }