Skip to content

Commit

Permalink
Merge branch 'MDL-74816-400' of https://github.com/dpalou/moodle into…
Browse files Browse the repository at this point in the history
… MOODLE_400_STABLE
  • Loading branch information
junpataleta committed Jun 14, 2022
2 parents 2d9c344 + 8beef3f commit eab8c74
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
10 changes: 6 additions & 4 deletions mod/resource/tests/generator/lib.php
Expand Up @@ -39,7 +39,8 @@ class mod_resource_generator extends testing_module_generator {
* text file.
*
* @param array|stdClass $record data for module being generated. Requires 'course' key
* (an id or the full object). Also can have any fields from add module form.
* (an id or the full object). Also can have any fields from add module form, and a
* 'defaultfilename' to set the name of the file created if no draft ID is supplied.
* @param null|array $options general options for course module. Since 2.6 it is
* possible to omit this argument by merging options into $record
* @return stdClass record from module-defined table with additional field
Expand Down Expand Up @@ -72,16 +73,17 @@ public function create_instance($record = null, array $options = null) {
throw new coding_exception('resource generator requires a current user');
}
$usercontext = context_user::instance($USER->id);
$filename = $record->defaultfilename ?? 'resource' . ($this->instancecount + 1) . '.txt';

// Pick a random context id for specified user.
$record->files = file_get_unused_draft_itemid();

// Add actual file there.
$filerecord = array('component' => 'user', 'filearea' => 'draft',
$filerecord = ['component' => 'user', 'filearea' => 'draft',
'contextid' => $usercontext->id, 'itemid' => $record->files,
'filename' => 'resource' . ($this->instancecount+1) . '.txt', 'filepath' => '/');
'filename' => $filename, 'filepath' => '/'];
$fs = get_file_storage();
$fs->create_file_from_string($filerecord, 'Test resource ' . ($this->instancecount+1) . ' file');
$fs->create_file_from_string($filerecord, 'Test resource ' . $filename . ' file');
}

// Do work to actually add the instance.
Expand Down
17 changes: 16 additions & 1 deletion mod/resource/tests/generator_test.php
Expand Up @@ -60,6 +60,21 @@ public function test_generator() {
// Check that generated resource module contains a file.
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'mod_resource', 'content', false, '', false);
$this->assertEquals(1, count($files));
$file = array_values($files)[0];
$this->assertCount(1, $files);
$this->assertEquals('resource3.txt', $file->get_filename());
$this->assertEquals('Test resource resource3.txt file', $file->get_content());

// Create a new resource specifying the file name.
$resource = $generator->create_instance(['course' => $SITE->id, 'defaultfilename' => 'myfile.pdf']);

// Check that generated resource module contains a file with the specified name.
$cm = get_coursemodule_from_instance('resource', $resource->id);
$context = \context_module::instance($cm->id);
$files = $fs->get_area_files($context->id, 'mod_resource', 'content', false, '', false);
$file = array_values($files)[0];
$this->assertCount(1, $files);
$this->assertEquals('myfile.pdf', $file->get_filename());
$this->assertEquals('Test resource myfile.pdf file', $file->get_content());
}
}

0 comments on commit eab8c74

Please sign in to comment.