Testing and QA

PHPUnit test for question type with files as a response

Picture of Gilles-Philippe Leblanc
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:

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:

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
Core developersDocumentation writersParticularly helpful MoodlersPlugin 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?

Average of ratings: -