Unit testing the RESTFul API of moodle

Unit testing the RESTFul API of moodle

Son Tran -
回帖数:3

Dear All,

I develop some external API functions for Moodle in the <Moodle>/local/COMPONENTFOLDER.

I create a test folder as guided at https://docs.moodle.org/dev/Web_Services_Unit_Test. I try to follow this instruction but I don,t understand how to make use the following code:

$contextid = context_XXXX::instance()->id;

$roleid = $this->assignUserCapability('moodle/CAPABILITYNAME', $contextid);

$params = array(PARAM1, PARAM2, ...);   

$returnvalue = COMPONENT_external::FUNCTION_NAME($params);

as shown in the above instruction.

I wonder how to create the class context_XXXX.

Please share me some comprehensive examples to help me overcome this issue.

Many thanks


回复Son Tran

Re: Unit testing the RESTFul API of moodle

AL Rachels -
Core developers的头像 Particularly helpful Moodlers的头像 Plugin developers的头像 Testers的头像

Hi Son Tran,

You might try looking at this: https://docs.moodle.org/dev/Tutorial

回复AL Rachels

Re: Unit testing the RESTFul API of moodle

Son Tran -

Thank you AL Rachels for your answer.

I check the link attentively but there is no any code for testing API.  I can see that most of the called functions come from the method getDataGenerator(). Do you have some more information on the retuned object of this function (As I understand it is a Generator). I wonder whether the API functions can be also derived from this object. So please give me more information on the Generator (precisely the test on API)

Many thanks

回复Son Tran

Re: Unit testing the RESTFul API of moodle

AL Rachels -
Core developers的头像 Particularly helpful Moodlers的头像 Plugin developers的头像 Testers的头像

Sorry, I can't be of more help. Generally, when I am trying to figure out how to do something I go to, "Development" and start doing some keyword searches. I also look through the code for similar type plugins and hope I can figure out what I need based on how the plugin does it.