/** * Execute test client WS request * * @param string $serverurl server url (including token parameter or username/password parameters) * @param string $function function name * @param array $params parameters of the called function * @return mixed */ public function simpletest($serverurl, $function, $params) { global $CFG; require_once $CFG->dirroot . '/webservice/soap/lib.php'; $client = new webservice_soap_client($serverurl); return $client->call($function, $params); }
/** * submit callback * * @param Pieform $form * @param array $values */ function testclient_submit(Pieform $form, $values) { global $SESSION, $params, $iterations, $function, $dbsf; if ($values['authtype'] == 'token' && !empty($values['wstoken']) || $values['authtype'] == 'user' && !empty($values['wsusername']) && !empty($values['wspassword'])) { $vars = testclient_get_interface($dbsf->functionname); $inputs = array(); for ($i = 0; $i <= $iterations; $i++) { foreach ($vars as $var) { $name = preg_replace('/NUM/', $i, $var['name']); $parts = explode('_', $name); testclient_build_inputs($inputs, $parts, $values[$name]); } } if ($values['authtype'] == 'token') { // check token $dbtoken = get_record('external_tokens', 'token', $values['wstoken']); if (empty($dbtoken)) { $SESSION->add_error_msg(get_string('invalidtoken', 'auth.webservice')); redirect('/webservice/testclient.php?' . implode('&', $params)); } } else { // check user is a valid web services account $dbuser = get_record('usr', 'username', $values['wsusername']); if (empty($dbuser)) { $SESSION->add_error_msg(get_string('invaliduser', 'auth.webservice', $values['wsusername'])); redirect('/webservice/testclient.php?' . implode('&', $params)); } // special web service login safe_require('auth', 'webservice'); // do password auth $ext_user = get_record('external_services_users', 'userid', $dbuser->id); if (empty($ext_user)) { $SESSION->add_error_msg(get_string('invaliduser', 'auth.webservice', $values['wsusername'])); redirect('/webservice/testclient.php?' . implode('&', $params)); } // determine the internal auth instance $auth_instance = get_record('auth_instance', 'institution', $ext_user->institution, 'authname', 'webservice'); if (empty($auth_instance)) { $SESSION->add_error_msg(get_string('invaliduser', 'auth.webservice', $values['wsusername'])); redirect('/webservice/testclient.php?' . implode('&', $params)); } // authenticate the user $auth = new AuthWebservice($auth_instance->id); if (!$auth->authenticate_user_account($dbuser, $values['wspassword'], 'webservice')) { // log failed login attempts $SESSION->add_error_msg(get_string('invaliduserpass', 'auth.webservice', $values['wsusername'])); redirect('/webservice/testclient.php?' . implode('&', $params)); } } // now build the test call switch ($values['protocol']) { case 'rest': error_log('creating REST client'); require_once get_config('docroot') . '/webservice/rest/lib.php'; $client = new webservice_rest_client(get_config('wwwroot') . '/webservice/rest/server.php', $values['authtype'] == 'token' ? array('wstoken' => $values['wstoken']) : array('wsusername' => $values['wsusername'], 'wspassword' => $values['wspassword']), $values['authtype']); break; case 'xmlrpc': error_log('creating XML-RPC client'); require_once get_config('docroot') . 'webservice/xmlrpc/lib.php'; $client = new webservice_xmlrpc_client(get_config('wwwroot') . '/webservice/xmlrpc/server.php', $values['authtype'] == 'token' ? array('wstoken' => $values['wstoken']) : array('wsusername' => $values['wsusername'], 'wspassword' => $values['wspassword'])); break; case 'soap': error_log('creating SOAP client'); // stop failed to load external entity error libxml_disable_entity_loader(false); require_once get_config('docroot') . 'webservice/soap/lib.php'; //force SOAP synchronous mode $client = new webservice_soap_client(get_config('wwwroot') . 'webservice/soap/server.php', $values['authtype'] == 'token' ? array('wstoken' => $values['wstoken']) : array('wsusername' => $values['wsusername'], 'wspassword' => $values['wspassword']), array("features" => SOAP_WAIT_ONE_WAY_CALLS)); $client->setWsdlCache(false); break; } try { $results = $client->call($dbsf->functionname, $inputs, true); } catch (Exception $e) { $results = "exception: " . $e->getMessage(); } $SESSION->set('ws_call_results', serialize($results)); $SESSION->add_ok_msg(get_string('executed', 'auth.webservice')); } redirect('/webservice/testclient.php?' . implode('&', $params)); }
/** * Test moodle_notes_create_notes web service function * * @param webservice_rest_client|webservice_soap_client|webservice_xmlrpc_client $client the protocol test client * @since Moodle 2.1 */ private function moodle_notes_create_notes($client) { global $DB, $CFG; $note1 = array(); $note1['userid'] = 2; // about who is the note $note1['publishstate'] = 'personal'; // can be course, site, personal $note1['courseid'] = 2; // in Moodle a notes is always created into a course, even a site note. $note1['text'] = 'This is a personal note about the user'; $note1['clientnoteid'] = 'note_1'; $note2 = array(); $note2['userid'] = 40000; // mostly likely going to fail $note2['publishstate'] = 'course'; $note2['courseid'] = 2; $note2['text'] = 'This is a teacher note about the user'; $note2['clientnoteid'] = 'note_2'; $note3 = array(); $note3['userid'] = 2; $note3['publishstate'] = 'site'; $note3['courseid'] = 30000; // mostly likely going to fail $note3['text'] = 'This is a teacher site-wide note about the user'; $note3['clientnoteid'] = 'note_3'; $function = 'moodle_notes_create_notes'; $params = array('notes' => array($note1, $note2, $note3)); $notes = $client->call($function, $params); $this->assertEqual(3, count($notes)); // 1 info is a success, 2 others should be failed }