lib/formslib.php Testing: Difference between revisions
Jamie Pratt (talk | contribs) |
David Mudrak (talk | contribs) m (Text replacement - "</code>" to "</syntaxhighlight>") |
||
Line 19: | Line 19: | ||
$actualquestiondata = question_load_questions(array($actualfromform->id)); | $actualquestiondata = question_load_questions(array($actualfromform->id)); | ||
$this->assertEquals($questiondata, $actualquestiondata); | $this->assertEquals($questiondata, $actualquestiondata); | ||
</ | </syntaxhighlight> | ||
There are a number of examples of the use of this method in the question/type unit tests. Search for 'mock_submit'. See also the phpdoc comments on the method itself. | There are a number of examples of the use of this method in the question/type unit tests. Search for 'mock_submit'. See also the phpdoc comments on the method itself. | ||
[[Category:Formslib]] | [[Category:Formslib]] |
Revision as of 20:24, 14 July 2021
Moodle 2.4
Testing Your Form Processing Code
In order to test the processing of submitted form contents in unit tests we have added a new method to formslib, mock_submit(). It is available in the latest 2.4, 2.5 releases as well as in 2.6 onwards.
The mock_submit method makes the form behave as if the array of data to supplied to it has been submitted in a form. Then if the form validates, get_data will then return all the values passed into the form, along with any defaults. So the following for example is possible :
$questiondata = test_question_maker::get_question_data('multichoice', 'single');
$expectedfromform = test_question_maker::get_question_form_data('multichoice', 'single');
$form = new qtype_multichoice_edit_form(...);
$form->mock_submit($questiondata);
$actualfromform = $form->get_data();
$this->assertEquals($expectedfromform, $actualfromform);
save_question($actualfromform);
$actualquestiondata = question_load_questions(array($actualfromform->id));
$this->assertEquals($questiondata, $actualquestiondata);
</syntaxhighlight>
There are a number of examples of the use of this method in the question/type unit tests. Search for 'mock_submit'. See also the phpdoc comments on the method itself.