Testing and QA

PHPUnit test for question type with files as a response

 
 
Picture of Gilles-Philippe Leblanc
PHPUnit test for question type with files as a response
Group Developers

Hello,

I try to create a phpunit test for a new type of question whose answer is a dropped file.

Unfortunately, I can not simulate this answer because the answer is as expected class type question_file_loader.

Just for information, we use the code proposed in this patch:
https://tracker.moodle.org/browse/MDL-34640

In fact, it is much easier to insert a response with the string type.

I absolutely need to simulate the response Asserts in phpunit.

Normally, I could use in example the unit test from essay type question since it is possible to accept files as a response in this type of question. Unfortunately, the tests do not include attachments. (attachments=0 in tests).

How can I take a file from my tests folder, temporarely put it in the moodle file environment, with a contextid, a hash, a database entries etc. generate my question_file_loader object with it and then delete the file entry.

Thank you for helping me.

 
Average of ratings: -
Tim at Lone Pine Koala Sanctuary
Re: PHPUnit test for question type with files as a response
Group DevelopersGroup Documentation writersGroup Particularly helpful Moodlers

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.

 
Average of ratings: -
Picture of Gilles-Philippe Leblanc
Re: PHPUnit test for question type with files as a response
Group Developers

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.

 
Average of ratings: -
Tim at Lone Pine Koala Sanctuary
Re: PHPUnit test for question type with files as a response
Group DevelopersGroup Documentation writersGroup Particularly helpful Moodlers

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: -
Picture of Gilles-Philippe Leblanc
Re: PHPUnit test for question type with files as a response
Group Developers

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: -
Tim at Lone Pine Koala Sanctuary
Re: PHPUnit test for question type with files as a response
Group DevelopersGroup Documentation writersGroup Particularly helpful Moodlers

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: -