Example #1
0
    function testRun() {
        global $CFG;

        if (!$this->testrest and !$this->testxmlrpc and !$this->testsoap) {
            print_r("Web service unit tests are not run as not setup.
                (see /webservice/simpletest/testwebservice.php)");
        }

        if (!empty($this->testtoken)) {

            //Does not work till XML => PHP is implemented (MDL-22965)
            if ($this->testrest) {

                $this->timerrest = time();

                require_once($CFG->dirroot . "/webservice/rest/lib.php");
                $restclient = new webservice_rest_client($CFG->wwwroot
                                . '/webservice/rest/server.php', $this->testtoken);

                for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
                    foreach ($this->readonlytests as $functioname => $run) {
                        if ($run) {
                            //$this->{$functioname}($restclient);
                        }
                    }
                    foreach ($this->writetests as $functioname => $run) {
                        if ($run) {
                            //$this->{$functioname}($restclient);
                        }
                    }
                }

                $this->timerrest = time() - $this->timerrest;
                //here you could call a log function to display the timer
                //example:
                //error_log('REST time: ');
                //error_log(print_r($this->timerrest));
            }

            if ($this->testxmlrpc) {

                $this->timerxmlrpc = time();

                require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
                $xmlrpcclient = new webservice_xmlrpc_client($CFG->wwwroot
                                . '/webservice/xmlrpc/server.php', $this->testtoken);

                for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
                    foreach ($this->readonlytests as $functioname => $run) {
                        if ($run) {
                            $this->{$functioname}($xmlrpcclient);
                        }
                    }
                    foreach ($this->writetests as $functioname => $run) {
                        if ($run) {
                            $this->{$functioname}($xmlrpcclient);
                        }
                    }
                }

                $this->timerxmlrpc = time() - $this->timerxmlrpc;
                //here you could call a log function to display the timer
                //example:
                //error_log('XML-RPC time: ');
                //error_log(print_r($this->timerxmlrpc));
            }

            if ($this->testsoap) {

                $this->timersoap = time();

                require_once($CFG->dirroot . "/webservice/soap/lib.php");
                $soapclient = new webservice_soap_client($CFG->wwwroot
                                . '/webservice/soap/server.php', $this->testtoken,
                        array("features" => SOAP_WAIT_ONE_WAY_CALLS)); //force SOAP synchronous mode
                                                                     //when function return null
                $soapclient->setWsdlCache(false);

                for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
                    foreach ($this->readonlytests as $functioname => $run) {
                        if ($run) {
                            $this->{$functioname}($soapclient);
                        }
                    }
                    foreach ($this->writetests as $functioname => $run) {
                        if ($run) {
                            $this->{$functioname}($soapclient);
                        }
                    }
                }

                $this->timersoap = time() - $this->timersoap;
                //here you could call a log function to display the timer
                //example:
                //error_log('SOAP time: ');
                //error_log(print_r($this->timersoap));
            }
        }
    }
Example #2
0
 function testRun()
 {
     global $CFG;
     if (!empty($this->testtoken)) {
         //Does not work till XML => PHP is implemented (MDL-22965)
         if ($this->testrest) {
             $this->timerrest = time();
             require_once $CFG->dirroot . "/webservice/rest/lib.php";
             $restclient = new webservice_rest_client($CFG->wwwroot . '/webservice/rest/server.php', $this->testtoken);
             for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
                 foreach ($this->readonlytests as $functioname => $run) {
                     if ($run) {
                         //$this->{$functioname}($restclient);
                     }
                 }
                 foreach ($this->writetests as $functioname => $run) {
                     if ($run) {
                         //$this->{$functioname}($restclient);
                     }
                 }
             }
             $this->timerrest = time() - $this->timerrest;
             //here you could call a log function to display the timer
             //example:
             //error_log('REST time: ');
             //error_log(print_r($this->timerrest));
         }
         if ($this->testxmlrpc) {
             $this->timerxmlrpc = time();
             require_once $CFG->dirroot . "/webservice/xmlrpc/lib.php";
             $xmlrpcclient = new webservice_xmlrpc_client($CFG->wwwroot . '/webservice/xmlrpc/server.php', $this->testtoken);
             for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
                 foreach ($this->readonlytests as $functioname => $run) {
                     if ($run) {
                         $this->{$functioname}($xmlrpcclient);
                     }
                 }
                 foreach ($this->writetests as $functioname => $run) {
                     if ($run) {
                         $this->{$functioname}($xmlrpcclient);
                     }
                 }
             }
             $this->timerxmlrpc = time() - $this->timerxmlrpc;
             //here you could call a log function to display the timer
             //example:
             //error_log('XML-RPC time: ');
             //error_log(print_r($this->timerxmlrpc));
         }
         if ($this->testsoap) {
             $this->timersoap = time();
             require_once $CFG->dirroot . "/webservice/soap/lib.php";
             $soapclient = new webservice_soap_client($CFG->wwwroot . '/webservice/soap/server.php', $this->testtoken);
             $soapclient->setWsdlCache(false);
             for ($i = 1; $i <= $this->iteration; $i = $i + 1) {
                 foreach ($this->readonlytests as $functioname => $run) {
                     if ($run) {
                         $this->{$functioname}($soapclient);
                     }
                 }
                 foreach ($this->writetests as $functioname => $run) {
                     if ($run) {
                         $this->{$functioname}($soapclient);
                     }
                 }
             }
             $this->timersoap = time() - $this->timersoap;
             //here you could call a log function to display the timer
             //example:
             //error_log('SOAP time: ');
             //error_log(print_r($this->timersoap));
         }
     }
 }
 /**
  * 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;
         }
     }
 }
Example #4
0
/**
 * 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));
}
Example #5
0
 /**
  * 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);
 }
Example #6
0
    /**
     * 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
    }