## Testing and QA

### PHPUnit test for question type with files as a response

This discussion has been locked because a year has elapsed since the last post. Please start a new discussion topic.
Re: PHPUnit test for question type with files as a response

Well, I should not be a single point of failure in this. In principle, anyone with the necessary knowledge of how Moodle works should be able to develop this. In practice, since this requires understanding both Moodle Files API and the Moodle question engine (and possibly the PHP unit integration) the number of people who can do this are limited. I don't even know exactly what needs to be done to make this work, and I won't until (if) I try to code it myself.

The question engine is designed so that it was possible for the unit tests to be run entirely in memory. The File API is not like that. It may well be necessarly to do unit tests here that acutally save to the database, but given what PHP unit can do (in comparison to the old Simpletest) that may not really be a problem.

By all means please try to work this out. The uni tests need to simulate what happens to a question in mod/quiz/startattempt.php, attempt.php and processattempt.php. All that code exists (and is explained at http://docs.moodle.org/dev/Using_the_question_engine_from_module). You just need to extract it, make it more abstract, and put it in a unit test.

Average of ratings: -
Re: PHPUnit test for question type with files as a response

Tim thank you for your explanation. I began to understand the time exploring everything that concerned the types of questions. I confess, however, that the link will greatly help me:
http://docs.moodle.org/dev/Using_the_question_engine_from_module

Or where is my main weakness is in everything that concerns the API files.

I do not know yet exactly how I'll be able to upload a file in the internal file system of moodle as the types of questions to expect more from a user upload. I do not know how to simulate a file picker in a phpunit test.

I discoved this documentation:
http://docs.moodle.org/dev/File_API#Serving_files_to_users

With this documentation, I should be able to import the file in the folder "test" and insert it into the database:

$filename = 'myfile.docx';$mytestfile = $CFG->dirroot . '/questions/type/myqtype/tests/files/' .$filename;
$context = get_context_instance(???);$fs = get_file_storage();
$file_record = array('contextid'=>$context->id, 'component'=>'qtype_myqtype', 'filearea'=>'???',
'itemid'=>0, 'filepath'=>'/', 'filename'=>$filename, 'timecreated'=>time(), 'timemodified'=>time());$fs->create_file_from_pathname($file_record,$mytestfile);

Once the file is inserted into the database, then I can create my object "question_file_loader" required as a response to unit test pass.

Am I on the right track for the filing of the file?

Average of ratings: -
Re: PHPUnit test for question type with files as a response

Note that you only have to simulate the server-side of what happens when a user uses the file-picker.

What happens then involves a 'draft' file area. That is, a file area with contextid = user's context, component = 'user', filearea = 'draft' and itemid = some unique ID that the file API gives you.

So, look at how the essay question (renderer) calls ->prepare_response_files_draft_itemid which eventually calls file_prepare_draft_area.

Anyway, what you need to do is to get the draft itemid, and save the file into that draft file area before calling process_submit.

Hey! I am starting to understand this now. Writing this post really helped make it clear to me. I wonder if I will have time to work on this next week?

Average of ratings: -