This content of this page has been updated and migrated to the new Moodle Developer Resources. The information contained on the page should no longer be seen up-to-date.
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);
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.