/** * common test framework for all tests - cycles through the number * of iterations, auth types, and protocols */ public function testRun() { $this->markTestSkipped('cURL requests need to be mocked properly. Skipping for now.'); // do we have any tests if (!$this->testrest and !$this->testxmlrpc and !$this->testsoap) { log_debug("Web service unit tests are not run as not setup (see /webservice/simpletest/testwebservice.php)"); } // need a token to test if (!empty($this->testtoken)) { // test the REST interface if ($this->testrest) { log_debug("Testing REST"); $this->timerrest = time(); require_once get_config('docroot') . "webservice/rest/lib.php"; // iterate the token and user auth types foreach ($this->auths as $type) { log_debug("Auth Type: " . $type); switch ($type) { case 'token': $restclient = new webservice_rest_client(get_config('wwwroot') . 'webservice/rest/server.php', array('wstoken' => $this->testtoken), $type); break; case 'user': $restclient = new webservice_rest_client(get_config('wwwroot') . 'webservice/rest/server.php', array('wsusername' => $this->testuser, 'wspassword' => $this->testuser), $type); break; case 'oauth': $restclient = new webservice_rest_client(get_config('wwwroot') . 'webservice/rest/server.php', array(), $type); $restclient->set_oauth($this->consumer, $this->access_token); break; } for ($i = 1; $i <= $this->iteration; $i = $i + 1) { foreach ($this->readonlytests as $functionname => $run) { if ($run) { $this->{$functionname}($restclient); } } foreach ($this->writetests as $functionname => $run) { if ($run) { $this->{$functionname}($restclient); } } } } $this->timerrest = time() - $this->timerrest; } // test the XML-RPC interface if ($this->testxmlrpc) { log_debug("Testing XML RPC"); $this->timerxmlrpc = time(); require_once get_config('docroot') . "webservice/xmlrpc/lib.php"; // iterate the token and user auth types foreach (array('token', 'user') as $type) { $xmlrpcclient = new webservice_xmlrpc_client(get_config('wwwroot') . 'webservice/xmlrpc/server.php', $type == 'token' ? array('wstoken' => $this->testtoken) : array('wsusername' => $this->testuser, 'wspassword' => $this->testuser)); for ($i = 1; $i <= $this->iteration; $i = $i + 1) { foreach ($this->readonlytests as $functionname => $run) { if ($run) { $this->{$functionname}($xmlrpcclient); } } foreach ($this->writetests as $functionname => $run) { if ($run) { $this->{$functionname}($xmlrpcclient); } } } } $this->timerxmlrpc = time() - $this->timerxmlrpc; } // test the SOAP interface if ($this->testsoap) { log_debug("Testing SOAP"); $this->timersoap = time(); require_once get_config('docroot') . "webservice/soap/lib.php"; // iterate the token and user auth types foreach (array(array('wstoken' => $this->testtoken), array('wsusername' => $this->testuser, 'wspassword' => $this->testuser), array('wsse' => 1)) as $parms) { // stop failed to load external entity error libxml_disable_entity_loader(false); if (isset($parms['wsse'])) { //force SOAP synchronous mode $soapclient = new webservice_soap_client(get_config('wwwroot') . 'webservice/soap/server.php', array('wsservice' => $this->servicename), array("features" => SOAP_WAIT_ONE_WAY_CALLS)); //when function return null $wsseSoapClient = new webservice_soap_client_wsse(array($soapclient, '_doRequest'), $soapclient->wsdl, $soapclient->getOptions()); $wsseSoapClient->__setUsernameToken($this->testuser, $this->testuser); $soapclient->setSoapClient($wsseSoapClient); } else { //force SOAP synchronous mode $soapclient = new webservice_soap_client(get_config('wwwroot') . 'webservice/soap/server.php', $parms, array("features" => SOAP_WAIT_ONE_WAY_CALLS)); //when function return null } $soapclient->setWsdlCache(false); for ($i = 1; $i <= $this->iteration; $i = $i + 1) { foreach ($this->readonlytests as $functionname => $run) { if ($run) { $this->{$functionname}($soapclient); } } foreach ($this->writetests as $functionname => $run) { if ($run) { $this->{$functionname}($soapclient); } } } } $this->timersoap = time() - $this->timersoap; } } }
/** * 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 }