PHPUnit test for question type with files as a response

Re: PHPUnit test for question type with files as a response

by Tim Hunt -
Number of replies: 4
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 keep meaning to finish MDL-34640, and it gets to number 2 or 3 on my todo list, then something else comes up. The main reason I don't consider it finished yet is the lack of unit tests. Will it get done before Moodle 2.5? Well, I am crossing my fingers, but I am not sure.

In reply to Tim Hunt

Re: PHPUnit test for question type with files as a response

by Gilles-Philippe Leblanc -

Hi Tim,

In the first place thank you for the answer. I understand that you have several priority.

Otherwise, I do understand that it is not possible at the moment, or at least, not easy to make the unit test that I have mentionned unless you add the code required for unit testing the task MDL-34640. Is that correct?

Thank you.

In reply to Gilles-Philippe Leblanc

Re: PHPUnit test for question type with files as a response

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

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.

In reply to Tim Hunt

Re: PHPUnit test for question type with files as a response

by Gilles-Philippe Leblanc -

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?

In reply to Gilles-Philippe Leblanc

Re: PHPUnit test for question type with files as a response

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

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?