Setup
1) Enable web services for the site, and enable the rest protocol.
2) Go to "Admin -> Plugins -> Web services -> External services" and add a service named 'gradecat, enabled, and add the function 'core_grades_create_gradecategory' to it.
3) Go to "Admin -> Plugins -> Web services -> Manage tokens" and add a token for the Admin user account and the just created "gradecat" service, so there are no permission issues. Annotate it for later use.
4) Create a blank test course to test the webservice in. Annotate its "courseid" from the url.
5) Visit the gradebook setup page for the newly created course.
Testing
6) Open a terminal (with curl installed)
7) Execute the following command to call the webservice with basic parameters
curl "http://moodlecore.lan/webservice/rest/server.php?wstoken={TOKEN}&wsfunction=core_grades_create_gradecategory&moodlewsrestformat=json&courseid={COURSEID}&fullname=Test" | json_pp
|
(replacing TOKEN and COURSEID with the values annotated above)
8) Verify that the returned JSON in the terminal contains the field 'categoryid' with a number, and the warnings array is empty. Make note of the categoryid
9) Reload the gradebook setup, and verify that there is now a grade category underneath the course grade category, called 'Test'.
10) Execute the following command with the annotated token, courseid and categoryid from above.
curl "http://moodlecore.lan/webservice/rest/server.php?wstoken={TOKEN}&wsfunction=core_grades_create_gradecategory&moodlewsrestformat=json&courseid={COURSEID}&fullname=Test2&options[parentcategoryid]={CATEGORYID}&options[idnumber]=IDNumber" | json_pp
|
11) Verify the returned JSON contains a similar response to the above command, with no warnings
12) Reload the gradebook setup and verify there is now a 'Test 2' category nested as a child of the 'Test category'
13) Edit the Test2 Gradecategory settings and confirm that the ID number field is set to 'IDNumber'
15) Repeat the same command as above, this time, specifying the idnumber of the parent category instead of the id.
curl "http://moodlecore.lan/webservice/rest/server.php?wstoken={TOKEN}&wsfunction=core_grades_create_gradecategory&moodlewsrestformat=json&courseid={COURSEID}&fullname=Test3&options[parentcategoryidnumber]=IDNumber" | json_pp
|
16) Verify there is now another category Test3 as a child of the Test2 category.
17) Run one more curl command, with data that is invalid.
curl "http://moodlecore.lan/webservice/rest/server.php?wstoken={TOKEN}&wsfunction=core_grades_create_gradecategory&moodlewsrestformat=json&courseid={COURSEID}&fullname=Test4&options[grademax]=1&options[grademin]=2" | json_pp
|
18) Verify that there is no category ID returned, and that there is a warning for invalidgrade for item grademax.
19) Verify that there is no Test4 Category created in the gradebook setup page.
The rest is covered by unit tests