See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
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. 13 // 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/>. 16 17 /** 18 * Unit tests for importziplib. 19 * 20 * @package assignfeedback_file 21 * @copyright 2020 Eric Merrill <merrill@oakland.edu> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 global $CFG; 28 require_once($CFG->dirroot . '/mod/assign/tests/generator.php'); 29 require_once($CFG->dirroot . '/mod/assign/feedback/file/importziplib.php'); 30 31 /** 32 * Unit tests for importziplib. 33 * 34 * @copyright 2020 Eric Merrill <merrill@oakland.edu> 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class assignfeedback_importziplib_testcase extends advanced_testcase { 38 39 // Use the generator helper. 40 use mod_assign_test_generator; 41 42 /** 43 * Test the assignfeedback_file_zip_importer->is_valid_filename_for_import() method. 44 */ 45 public function test_is_valid_filename_for_import() { 46 // Do the initial assign setup. 47 $this->resetAfterTest(); 48 $course = $this->getDataGenerator()->create_course(); 49 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 50 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 51 52 $assign = $this->create_instance($course, [ 53 'assignsubmission_onlinetext_enabled' => 1, 54 'assignfeedback_file_enabled' => 1, 55 ]); 56 57 // Create an online text submission. 58 $this->add_submission($student, $assign); 59 60 // Now onto the file work. 61 $fs = get_file_storage(); 62 63 // Setup a basic file we will work with. We will keep renaming and repathing it. 64 $record = new stdClass; 65 $record->contextid = $assign->get_context()->id; 66 $record->component = 'assignfeedback_file'; 67 $record->filearea = ASSIGNFEEDBACK_FILE_FILEAREA; 68 $record->itemid = $assign->get_user_grade($student->id, true)->id; 69 $record->filepath = '/'; 70 $record->filename = '1.txt'; 71 $record->source = 'test'; 72 $file = $fs->create_file_from_string($record, 'file content'); 73 74 // The importer we will use. 75 $importer = new assignfeedback_file_zip_importer(); 76 77 // Setup some variable we use. 78 $user = null; 79 $plugin = null; 80 $filename = ''; 81 82 $allusers = $assign->list_participants(0, false); 83 $participants = array(); 84 foreach ($allusers as $user) { 85 $participants[$assign->get_uniqueid_for_user($user->id)] = $user; 86 } 87 88 $file->rename('/import/', '.hiddenfile'); 89 $result = $importer->is_valid_filename_for_import($assign, $file, $participants, $user, $plugin, $filename); 90 $this->assertFalse($result); 91 92 $file->rename('/import/', '~hiddenfile'); 93 $result = $importer->is_valid_filename_for_import($assign, $file, $participants, $user, $plugin, $filename); 94 $this->assertFalse($result); 95 96 $file->rename('/import/some_path_here/', 'RandomFile.txt'); 97 $result = $importer->is_valid_filename_for_import($assign, $file, $participants, $user, $plugin, $filename); 98 $this->assertFalse($result); 99 100 $file->rename('/import/', '~hiddenfile'); 101 $result = $importer->is_valid_filename_for_import($assign, $file, $participants, $user, $plugin, $filename); 102 $this->assertFalse($result); 103 104 // Get the students assign id. 105 $studentid = $assign->get_uniqueid_for_user($student->id); 106 107 // Submissions are identified with the format: 108 // StudentName_StudentID_PluginType_Plugin_FilePathAndName. 109 110 // Test a string student id. 111 $badname = 'Student Name_StringID_assignsubmission_file_My_cool_filename.txt'; 112 $file->rename('/import/', $badname); 113 $result = $importer->is_valid_filename_for_import($assign, $file, $participants, $user, $plugin, $filename); 114 $this->assertFalse($result); 115 116 // Test an invalid student id. 117 $badname = 'Student Name_' . ($studentid + 100) . '_assignsubmission_file_My_cool_filename.txt'; 118 $file->rename('/import/', $badname); 119 $result = $importer->is_valid_filename_for_import($assign, $file, $participants, $user, $plugin, $filename); 120 $this->assertFalse($result); 121 122 // Test an invalid submission plugin. 123 $badname = 'Student Name_' . $studentid . '_assignsubmission_noplugin_My_cool_filename.txt'; 124 $file->rename('/import/', $badname); 125 $result = $importer->is_valid_filename_for_import($assign, $file, $participants, $user, $plugin, $filename); 126 $this->assertFalse($result); 127 128 // Test a basic, good file. 129 $goodbase = 'Student Name_' . $studentid . '_assignsubmission_file_'; 130 $file->rename('/import/', $goodbase . "My_cool_filename.txt"); 131 $result = $importer->is_valid_filename_for_import($assign, $file, $participants, $user, $plugin, $filename); 132 $this->assertTrue($result); 133 $this->assertEquals($participants[$studentid], $user); 134 $this->assertEquals('My_cool_filename.txt', $filename); 135 $this->assertInstanceOf(assign_submission_file::class, $plugin); 136 137 // Test another good file, with some additional path and underscores. 138 $user = null; 139 $plugin = null; 140 $filename = ''; 141 $file->rename('/import/some_path_here/' . $goodbase . '/some_path/', 'My File.txt'); 142 $result = $importer->is_valid_filename_for_import($assign, $file, $participants, $user, $plugin, $filename); 143 $this->assertTrue($result); 144 $this->assertEquals($participants[$studentid], $user); 145 $this->assertEquals('/some_path/My File.txt', $filename); 146 $this->assertInstanceOf(assign_submission_file::class, $plugin); 147 } 148 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body