2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
21 use mod_data\local\importer\csv_entries_importer
;
26 * Unit tests for import.php.
30 * @copyright 2019 Tobias Reischmann
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class entries_import_test
extends \advanced_testcase
{
38 protected function setUp(): void
{
42 require_once($CFG->dirroot
. '/mod/data/lib.php');
43 require_once($CFG->dirroot
. '/lib/datalib.php');
44 require_once($CFG->dirroot
. '/lib/csvlib.class.php');
45 require_once($CFG->dirroot
. '/search/tests/fixtures/testable_core_search.php');
46 require_once($CFG->dirroot
. '/mod/data/tests/generator/lib.php');
51 * In this instance we are setting up database records to be used in the unit tests.
55 protected function get_test_data(): array {
56 $this->resetAfterTest(true);
58 $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
59 $course = $this->getDataGenerator()->create_course();
60 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
61 $this->setUser($teacher);
62 $student = $this->getDataGenerator()->create_and_enrol($course, 'student', array('username' => 'student'));
64 $data = $generator->create_instance(array('course' => $course->id
));
65 $cm = get_coursemodule_from_instance('data', $data->id
);
68 $fieldrecord = new \
stdClass();
69 $fieldrecord->name
= 'ID'; // Identifier of the records for testing.
70 $fieldrecord->type
= 'number';
71 $generator->create_field($fieldrecord, $data);
73 $fieldrecord->name
= 'Param2';
74 $fieldrecord->type
= 'text';
75 $generator->create_field($fieldrecord, $data);
77 $fieldrecord->name
= 'filefield';
78 $fieldrecord->type
= 'file';
79 $generator->create_field($fieldrecord, $data);
81 $fieldrecord->name
= 'picturefield';
82 $fieldrecord->type
= 'picture';
83 $generator->create_field($fieldrecord, $data);
86 'teacher' => $teacher,
87 'student' => $student,
94 * Test uploading entries for a data instance without userdata.
96 * @throws dml_exception
98 public function test_import(): void
{
102 'teacher' => $teacher,
103 ] = $this->get_test_data();
105 $importer = new csv_entries_importer(__DIR__
. '/fixtures/test_data_import.csv',
106 'test_data_import.csv');
107 $importer->import_csv($cm, $data, 'UTF-8', 'comma');
109 // No userdata is present in the file: Fallback is to assign the uploading user as author.
110 $expecteduserids = array();
111 $expecteduserids[1] = $teacher->id
;
112 $expecteduserids[2] = $teacher->id
;
114 $records = $this->get_data_records($data->id
);
115 $this->assertCount(2, $records);
116 foreach ($records as $record) {
117 $identifier = $record->items
['ID']->content
;
118 $this->assertEquals($expecteduserids[$identifier], $record->userid
);
123 * Test uploading entries for a data instance with userdata.
125 * At least one entry has an identifiable user, which is assigned as author.
127 * @throws dml_exception
129 public function test_import_with_userdata(): void
{
133 'teacher' => $teacher,
134 'student' => $student,
135 ] = $this->get_test_data();
137 $importer = new csv_entries_importer(__DIR__
. '/fixtures/test_data_import_with_userdata.csv',
138 'test_data_import_with_userdata.csv');
139 $importer->import_csv($cm, $data, 'UTF-8', 'comma');
141 $expecteduserids = array();
142 $expecteduserids[1] = $student->id
; // User student exists and is assigned as author.
143 $expecteduserids[2] = $teacher->id
; // User student2 does not exist. Fallback is the uploading user.
145 $records = $this->get_data_records($data->id
);
146 $this->assertCount(2, $records);
147 foreach ($records as $record) {
148 $identifier = $record->items
['ID']->content
;
149 $this->assertEquals($expecteduserids[$identifier], $record->userid
);
154 * Test uploading entries for a data instance with userdata and a defined field 'Username'.
156 * This should test the corner case, in which a user has defined a data fields, which has the same name
157 * as the current lang string for username. In that case, the first Username entry is used for the field.
158 * The second one is used to identify the author.
160 * @throws coding_exception
161 * @throws dml_exception
163 public function test_import_with_field_username(): void
{
167 'teacher' => $teacher,
168 'student' => $student,
169 ] = $this->get_test_data();
170 $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
172 // Add username field.
173 $fieldrecord = new \
stdClass();
174 $fieldrecord->name
= 'Username';
175 $fieldrecord->type
= 'text';
176 $generator->create_field($fieldrecord, $data);
178 $importer = new csv_entries_importer(__DIR__
. '/fixtures/test_data_import_with_field_username.csv',
179 'test_data_import_with_field_username.csv');
180 $importer->import_csv($cm, $data, 'UTF-8', 'comma');
182 $expecteduserids = array();
183 $expecteduserids[1] = $student->id
; // User student exists and is assigned as author.
184 $expecteduserids[2] = $teacher->id
; // User student2 does not exist. Fallback is the uploading user.
185 $expecteduserids[3] = $student->id
; // User student exists and is assigned as author.
187 $expectedcontent = array();
188 $expectedcontent[1] = array(
189 'Username' => 'otherusername1',
190 'Param2' => 'My first entry',
192 $expectedcontent[2] = array(
193 'Username' => 'otherusername2',
194 'Param2' => 'My second entry',
196 $expectedcontent[3] = array(
197 'Username' => 'otherusername3',
198 'Param2' => 'My third entry',
201 $records = $this->get_data_records($data->id
);
202 $this->assertCount(3, $records);
203 foreach ($records as $record) {
204 $identifier = $record->items
['ID']->content
;
205 $this->assertEquals($expecteduserids[$identifier], $record->userid
);
207 foreach ($expectedcontent[$identifier] as $field => $value) {
208 $this->assertEquals($value, $record->items
[$field]->content
,
209 "The value of field \"$field\" for the record at position \"$identifier\" " .
210 "which is \"{$record->items[$field]->content}\" does not match the expected value \"$value\".");
216 * Test uploading entries for a data instance with a field 'Username' but only one occurrence in the csv file.
218 * This should test the corner case, in which a user has defined a data fields, which has the same name
219 * as the current lang string for username. In that case, the only Username entry is used for the field.
220 * The author should not be set.
222 * @throws coding_exception
223 * @throws dml_exception
225 public function test_import_with_field_username_without_userdata(): void
{
229 'teacher' => $teacher,
230 'student' => $student,
231 ] = $this->get_test_data();
232 $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
234 // Add username field.
235 $fieldrecord = new \
stdClass();
236 $fieldrecord->name
= 'Username';
237 $fieldrecord->type
= 'text';
238 $generator->create_field($fieldrecord, $data);
240 $importer = new csv_entries_importer(__DIR__
. '/fixtures/test_data_import_with_userdata.csv',
241 'test_data_import_with_userdata.csv');
242 $importer->import_csv($cm, $data, 'UTF-8', 'comma');
244 // No userdata is present in the file: Fallback is to assign the uploading user as author.
245 $expecteduserids = array();
246 $expecteduserids[1] = $teacher->id
;
247 $expecteduserids[2] = $teacher->id
;
249 $expectedcontent = array();
250 $expectedcontent[1] = array(
251 'Username' => 'student',
252 'Param2' => 'My first entry',
254 $expectedcontent[2] = array(
255 'Username' => 'student2',
256 'Param2' => 'My second entry',
259 $records = $this->get_data_records($data->id
);
260 $this->assertCount(2, $records);
261 foreach ($records as $record) {
262 $identifier = $record->items
['ID']->content
;
263 $this->assertEquals($expecteduserids[$identifier], $record->userid
);
265 foreach ($expectedcontent[$identifier] as $field => $value) {
266 $this->assertEquals($value, $record->items
[$field]->content
,
267 "The value of field \"$field\" for the record at position \"$identifier\" " .
268 "which is \"{$record->items[$field]->content}\" does not match the expected value \"$value\".");
274 * Tests the import including files from a zip archive.
276 * @covers \mod_data\local\importer\entries_importer
277 * @covers \mod_data\local\importer\csv_entries_importer
280 public function test_import_with_files(): void
{
284 ] = $this->get_test_data();
286 $importer = new csv_entries_importer(__DIR__
. '/fixtures/test_data_import_with_files.zip',
287 'test_data_import_with_files.zip');
288 $importer->import_csv($cm, $data, 'UTF-8', 'comma');
290 $records = $this->get_data_records($data->id
);
291 $ziparchive = new zip_archive();
292 $ziparchive->open(__DIR__
. '/fixtures/test_data_import_with_files.zip');
294 $importedcontent = array_values($records)[0]->items
;
295 $this->assertEquals(17, $importedcontent['ID']->content
);
296 $this->assertEquals('samplefile.png', $importedcontent['filefield']->content
);
297 $this->assertEquals('samplepicture.png', $importedcontent['picturefield']->content
);
299 // We now check if content of imported file from zip content is identical to the content of the file
300 // stored in the mod_data record in the field 'filefield'.
301 $fileindex = array_values(array_map(fn($file) => $file->index
,
302 array_filter($ziparchive->list_files(), fn($file) => $file->pathname
=== 'files/samplefile.png')))[0];
303 $filestream = $ziparchive->get_stream($fileindex);
304 $filefield = data_get_field_from_name('filefield', $data);
305 $filefieldfilecontent = fread($filestream, $ziparchive->get_info($fileindex)->size
);
306 $this->assertEquals($filefield->get_file(array_keys($records)[0])->get_content(),
307 $filefieldfilecontent);
310 // We now check if content of imported picture from zip content is identical to the content of the picture file
311 // stored in the mod_data record in the field 'picturefield'.
312 $fileindex = array_values(array_map(fn($file) => $file->index
,
313 array_filter($ziparchive->list_files(), fn($file) => $file->pathname
=== 'files/samplepicture.png')))[0];
314 $filestream = $ziparchive->get_stream($fileindex);
315 $filefield = data_get_field_from_name('picturefield', $data);
316 $filefieldfilecontent = fread($filestream, $ziparchive->get_info($fileindex)->size
);
317 $this->assertEquals($filefield->get_file(array_keys($records)[0])->get_content(),
318 $filefieldfilecontent);
320 $this->assertCount(1, $importer->get_added_records_messages());
321 $ziparchive->close();
325 * Tests the import including files from a zip archive.
327 * @covers \mod_data\local\importer\entries_importer
328 * @covers \mod_data\local\importer\csv_entries_importer
331 public function test_import_with_files_missing_file(): void
{
335 ] = $this->get_test_data();
337 $importer = new csv_entries_importer(__DIR__
. '/fixtures/test_data_import_with_files_missing_file.zip',
338 'test_data_import_with_files_missing_file.zip');
339 $importer->import_csv($cm, $data, 'UTF-8', 'comma');
341 $records = $this->get_data_records($data->id
);
342 $ziparchive = new zip_archive();
343 $ziparchive->open(__DIR__
. '/fixtures/test_data_import_with_files_missing_file.zip');
345 $importedcontent = array_values($records)[0]->items
;
346 $this->assertEquals(17, $importedcontent['ID']->content
);
347 $this->assertFalse(isset($importedcontent['filefield']));
348 $this->assertEquals('samplepicture.png', $importedcontent['picturefield']->content
);
349 $this->assertCount(1, $importer->get_added_records_messages());
350 $ziparchive->close();
354 * Returns the records of the data instance.
356 * Each records has an item entry, which contains all fields associated with this item.
357 * Each fields has the parameters name, type and content.
359 * @param int $dataid Id of the data instance.
360 * @return array The records of the data instance.
361 * @throws dml_exception
363 private function get_data_records(int $dataid): array {
366 $records = $DB->get_records('data_records', ['dataid' => $dataid]);
367 foreach ($records as $record) {
368 $sql = 'SELECT f.name, f.type, con.content FROM
369 {data_content} con JOIN {data_fields} f ON con.fieldid = f.id
370 WHERE con.recordid = :recordid';
371 $items = $DB->get_records_sql($sql, array('recordid' => $record->id
));
372 $record->items
= $items;
378 * Tests if the amount of imported records is counted properly.
380 * @covers \mod_data\local\importer\csv_entries_importer::import_csv
381 * @covers \mod_data\local\importer\csv_entries_importer::get_added_records_messages
382 * @dataProvider get_added_record_messages_provider
383 * @param string $datafilecontent the content of the datafile to test as string
384 * @param int $expectedcount the expected count of messages depending on the datafile content
386 public function test_get_added_record_messages(string $datafilecontent, int $expectedcount): void
{
390 ] = $this->get_test_data();
392 // First we need to create the zip file from the provided data.
393 $tmpdir = make_request_directory();
394 $datafile = $tmpdir . '/entries_import_test_datafile_tmp_' . time() . '.csv';
395 file_put_contents($datafile, $datafilecontent);
397 $importer = new csv_entries_importer($datafile, 'testdatafile.csv');
398 $importer->import_csv($cm, $data, 'UTF-8', 'comma');
399 $this->assertEquals($expectedcount, count($importer->get_added_records_messages()));
403 * Data provider method for self::test_get_added_record_messages.
405 * @return array data for testing
407 public function get_added_record_messages_provider(): array {
410 'datafilecontent' => 'ID,Param2,filefield,picturefield' . PHP_EOL
,
411 'expectedcount' => 0 // One line is being assumed to be the header.
414 'datafilecontent' => 'ID,Param2,filefield,picturefield' . PHP_EOL
415 . '5,"some short text",testfilename.pdf,testpicture.png',
419 'datafilecontent' => 'ID,Param2,filefield,picturefield' . PHP_EOL
420 . '5,"some short text",testfilename.pdf,testpicture.png' . PHP_EOL
421 . '3,"other text",testfilename2.pdf,testpicture2.png',