Test validation question edit form

Test validation question edit form

by Vadim Klevtsov -
Number of replies: 4
Hello, I try to create form for validation function test. But I can find what context I need to put to create form. I have error: " Fatal error: Call to undefined method context_course::having_cap() in /var/www/html/moodle/question/type/edit_question_form.php on line 138" . I tryed to use system and course context.
Average of ratings: -
In reply to Vadim Klevtsov

Re: Test validation question edit form

by Rajesh Taneja -

Hello Vadim,

having_cap is only implemented in question_edit_contexts(lib/questionlib.php), so seems the $contexts passed to form should be instance of question_edit_contexts

In reply to Vadim Klevtsov

Re: Test validation question edit form

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I'm sorry. I don't understand what you are asking. Is your code on github, or somewhere where we can see it?

In reply to Tim Hunt

Re: Test validation question edit form

by Vadim Klevtsov -
In reply to Vadim Klevtsov

Re: Test validation question edit form

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

OK, so you are trying to write a unit test for the validation method of edit form for your question type.

This seems to be a good idea, and I am suprised that I have never done that for any of my own question types. (I was hoping I could give you a link to a working example.)


The code you have writted mostly looks right. I think that should work, unless there are small mistakes in the code that you need to fix.


The other thing to think about is whether the form validate method is the best one to unit test. For example, in the STACK question type, the form method https://github.com/maths/moodle-qtype_stack/blob/master/edit_stack_form.php#L1056 calls some other methods https://github.com/maths/moodle-qtype_stack/blob/master/edit_stack_form.php#L1268, and those lower level methods are where we wrote lots of unit tests. https://github.com/maths/moodle-qtype_stack/blob/master/tests/castext_test.php

The reason to do it that way is that you can test all the deatils of the low-level validation logic without the overhead of creating an instance of the editing form.
Average of ratings: Useful (1)