MDL-74816 mod_resource: Allow specify default file name in generator
[moodle.git] / mod / resource / tests / generator / lib.php
blob4860d2fd10708441c598586f763273a401839ee6
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Data generator.
20 * @package mod_resource
21 * @copyright 2013 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Resource module data generator class.
31 * @package mod_resource
32 * @copyright 2013 The Open University
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class mod_resource_generator extends testing_module_generator {
37 /**
38 * Creates new resource module instance. By default it contains a short
39 * text file.
41 * @param array|stdClass $record data for module being generated. Requires 'course' key
42 * (an id or the full object). Also can have any fields from add module form, and a
43 * 'defaultfilename' to set the name of the file created if no draft ID is supplied.
44 * @param null|array $options general options for course module. Since 2.6 it is
45 * possible to omit this argument by merging options into $record
46 * @return stdClass record from module-defined table with additional field
47 * cmid (corresponding id in course_modules table)
49 public function create_instance($record = null, array $options = null) {
50 global $CFG, $USER;
51 require_once($CFG->dirroot . '/lib/resourcelib.php');
52 // Ensure the record can be modified without affecting calling code.
53 $record = (object)(array)$record;
55 // Fill in optional values if not specified.
56 if (!isset($record->display)) {
57 $record->display = RESOURCELIB_DISPLAY_AUTO;
59 if (!isset($record->printintro)) {
60 $record->printintro = 0;
62 if (!isset($record->showsize)) {
63 $record->showsize = 0;
65 if (!isset($record->showtype)) {
66 $record->showtype = 0;
69 // The 'files' value corresponds to the draft file area ID. If not
70 // specified, create a default file.
71 if (!isset($record->files)) {
72 if (empty($USER->username) || $USER->username === 'guest') {
73 throw new coding_exception('resource generator requires a current user');
75 $usercontext = context_user::instance($USER->id);
76 $filename = $record->defaultfilename ?? 'resource' . ($this->instancecount + 1) . '.txt';
78 // Pick a random context id for specified user.
79 $record->files = file_get_unused_draft_itemid();
81 // Add actual file there.
82 $filerecord = ['component' => 'user', 'filearea' => 'draft',
83 'contextid' => $usercontext->id, 'itemid' => $record->files,
84 'filename' => $filename, 'filepath' => '/'];
85 $fs = get_file_storage();
86 $fs->create_file_from_string($filerecord, 'Test resource ' . $filename . ' file');
89 // Do work to actually add the instance.
90 return parent::create_instance($record, (array)$options);