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