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_gradecategories' 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 "{LOCATION}/webservice/rest/server.php?wstoken={TOKEN}&wsfunction=core_grades_create_gradecategories&moodlewsrestformat=json&courseid={COURSEID}&categories[0][fullname]=Test" | json_pp
|
(replacing LOCATION with the install location, and TOKEN and COURSEID with the values annotated above)
8) Verify that the returned JSON in the terminal contains the field 'categoryids' 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 "{LOCATION}/webservice/rest/server.php?wstoken={TOKEN}&wsfunction=core_grades_create_gradecategories&moodlewsrestformat=json&courseid={COURSEID}&categories[0][fullname]=Test2&categories[0][options][parentcategoryid]={CATEGORYID}&categories[0][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) Now we are going to create 2 categories in a single call, nested and connected using IDnumbers.
curl "{LOCATION}/webservice/rest/server.php?wstoken={TOKEN}&wsfunction=core_grades_create_gradecategories&moodlewsrestformat=json&courseid={COURSEID}&categories[0][fullname]=Parent&categories[0][options][idnumber]=parent&categories[1][fullname]=Child&categories[1][options][idnumber]=child&categories[1][options][parentcategoryidnumber]=parent" | json_pp
|
16) Verify there is now 2 more categories, under the top level, called Parent and Child, and that Child is a subcategory of Parent
17) Run one more curl command, with data that is invalid.
curl "{LOCATION}/webservice/rest/server.php?wstoken={TOKEN}&wsfunction=core_grades_create_gradecategories&moodlewsrestformat=json&courseid={COURSEID}&categories[0][fullname]=Test4&categories[0][options][grademax]=1&categories[0][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