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 * Tests behaviour of the assign_portfolio_caller class. 19 * 20 * @package mod_assign 21 * @category test 22 * @copyright Brendan Cox <brendan.cox@totaralearning.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 require_once($CFG->dirroot . '/mod/assign/locallib.php'); 30 require_once($CFG->dirroot . '/group/lib.php'); 31 32 /** 33 * Class mod_assign_portfolio_caller_testcase 34 * 35 * Tests behaviour of the assign_portfolio_caller class. 36 * 37 * @copyright Brendan Cox <brendan.cox@totaralearning.com> 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 class mod_assign_portfolio_caller_testcase extends advanced_testcase { 41 42 /** 43 * Test an assignment file is loaded for a user who submitted it. 44 */ 45 public function test_user_submission_file_is_loaded() { 46 $this->resetAfterTest(true); 47 48 $user = $this->getDataGenerator()->create_user(); 49 $course = $this->getDataGenerator()->create_course(); 50 51 /* @var mod_assign_generator $assigngenerator */ 52 $assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); 53 54 $activityrecord = $assigngenerator->create_instance(array('course' => $course->id)); 55 $cm = get_coursemodule_from_instance('assign', $activityrecord->id); 56 $context = context_module::instance($cm->id); 57 $assign = new mod_assign_testable_assign($context, $cm, $course); 58 59 $submission = $assign->get_user_submission($user->id, true); 60 61 $fs = get_file_storage(); 62 $dummy = (object) array( 63 'contextid' => $context->id, 64 'component' => 'assignsubmission_file', 65 'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA, 66 'itemid' => $submission->id, 67 'filepath' => '/', 68 'filename' => 'myassignmnent.pdf' 69 ); 70 $file = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename); 71 72 $caller = new assign_portfolio_caller(array('cmid' => $cm->id, 'fileid' => $file->get_id())); 73 $caller->set('user', $user); 74 $caller->load_data(); 75 $this->assertEquals($file->get_contenthash(), $caller->get_sha1_file()); 76 77 // This processes the file either by fileid or by other fields in the file table. 78 // We should get the same outcome with either approach. 79 $caller = new assign_portfolio_caller( 80 array( 81 'cmid' => $cm->id, 82 'sid' => $submission->id, 83 'area' => ASSIGNSUBMISSION_FILE_FILEAREA, 84 'component' => 'assignsubmission_file', 85 ) 86 ); 87 $caller->set('user', $user); 88 $caller->load_data(); 89 $this->assertEquals($file->get_contenthash(), $caller->get_sha1_file()); 90 } 91 92 /** 93 * Test an assignment file is not loaded for a user that did not submit it. 94 */ 95 public function test_different_user_submission_file_is_not_loaded() { 96 $this->resetAfterTest(true); 97 98 $user = $this->getDataGenerator()->create_user(); 99 $course = $this->getDataGenerator()->create_course(); 100 101 /* @var mod_assign_generator $assigngenerator */ 102 $assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); 103 104 $activityrecord = $assigngenerator->create_instance(array('course' => $course->id)); 105 $cm = get_coursemodule_from_instance('assign', $activityrecord->id); 106 $context = context_module::instance($cm->id); 107 $assign = new mod_assign_testable_assign($context, $cm, $course); 108 109 $submission = $assign->get_user_submission($user->id, true); 110 111 $fs = get_file_storage(); 112 $dummy = (object) array( 113 'contextid' => $context->id, 114 'component' => 'assignsubmission_file', 115 'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA, 116 'itemid' => $submission->id, 117 'filepath' => '/', 118 'filename' => 'myassignmnent.pdf' 119 ); 120 $file = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename); 121 122 // Now add second user. 123 $wronguser = $this->getDataGenerator()->create_user(); 124 125 $caller = new assign_portfolio_caller(array('cmid' => $cm->id, 'fileid' => $file->get_id())); 126 $caller->set('user', $wronguser); 127 128 $this->expectException(portfolio_caller_exception::class); 129 $this->expectExceptionMessage('Sorry, the requested file could not be found'); 130 131 $caller->load_data(); 132 } 133 134 /** 135 * Test an assignment file is loaded for a user who is part of a group that submitted it. 136 */ 137 public function test_group_submission_file_is_loaded() { 138 $this->resetAfterTest(true); 139 140 $user = $this->getDataGenerator()->create_user(); 141 $course = $this->getDataGenerator()->create_course(); 142 143 $groupdata = new stdClass(); 144 $groupdata->courseid = $course->id; 145 $groupdata->name = 'group1'; 146 $groupid = groups_create_group($groupdata); 147 $this->getDataGenerator()->enrol_user($user->id, $course->id); 148 groups_add_member($groupid, $user); 149 150 /* @var mod_assign_generator $assigngenerator */ 151 $assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); 152 153 $activityrecord = $assigngenerator->create_instance(array('course' => $course->id)); 154 $cm = get_coursemodule_from_instance('assign', $activityrecord->id); 155 $context = context_module::instance($cm->id); 156 $assign = new mod_assign_testable_assign($context, $cm, $course); 157 158 $submission = $assign->get_group_submission($user->id, $groupid, true); 159 160 $fs = get_file_storage(); 161 $dummy = (object) array( 162 'contextid' => $context->id, 163 'component' => 'assignsubmission_file', 164 'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA, 165 'itemid' => $submission->id, 166 'filepath' => '/', 167 'filename' => 'myassignmnent.pdf' 168 ); 169 $file = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename); 170 171 $caller = new assign_portfolio_caller(array('cmid' => $cm->id, 'fileid' => $file->get_id())); 172 $caller->set('user', $user); 173 $caller->load_data(); 174 $this->assertEquals($file->get_contenthash(), $caller->get_sha1_file()); 175 176 // This processes the file either by fileid or by other fields in the file table. 177 // We should get the same outcome with either approach. 178 $caller = new assign_portfolio_caller( 179 array( 180 'cmid' => $cm->id, 181 'sid' => $submission->id, 182 'area' => ASSIGNSUBMISSION_FILE_FILEAREA, 183 'component' => 'assignsubmission_file', 184 ) 185 ); 186 $caller->set('user', $user); 187 $caller->load_data(); 188 $this->assertEquals($file->get_contenthash(), $caller->get_sha1_file()); 189 } 190 191 /** 192 * Test an assignment file is not loaded for a user who is not part of a group that submitted it. 193 */ 194 public function test_different_group_submission_file_is_not_loaded() { 195 $this->resetAfterTest(true); 196 197 $user = $this->getDataGenerator()->create_user(); 198 $course = $this->getDataGenerator()->create_course(); 199 200 $groupdata = new stdClass(); 201 $groupdata->courseid = $course->id; 202 $groupdata->name = 'group1'; 203 $groupid = groups_create_group($groupdata); 204 $this->getDataGenerator()->enrol_user($user->id, $course->id); 205 groups_add_member($groupid, $user); 206 207 /* @var mod_assign_generator $assigngenerator */ 208 $assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); 209 210 $activityrecord = $assigngenerator->create_instance(array('course' => $course->id)); 211 $cm = get_coursemodule_from_instance('assign', $activityrecord->id); 212 $context = context_module::instance($cm->id); 213 $assign = new mod_assign_testable_assign($context, $cm, $course); 214 215 $submission = $assign->get_group_submission($user->id, $groupid,true); 216 217 $fs = get_file_storage(); 218 $dummy = (object) array( 219 'contextid' => $context->id, 220 'component' => 'assignsubmission_file', 221 'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA, 222 'itemid' => $submission->id, 223 'filepath' => '/', 224 'filename' => 'myassignmnent.pdf' 225 ); 226 $file = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename); 227 228 // Now add second user. 229 $wronguser = $this->getDataGenerator()->create_user(); 230 231 // Create a new group for the wrong user. 232 $groupdata = new stdClass(); 233 $groupdata->courseid = $course->id; 234 $groupdata->name = 'group2'; 235 $groupid = groups_create_group($groupdata); 236 $this->getDataGenerator()->enrol_user($wronguser->id, $course->id); 237 groups_add_member($groupid, $wronguser); 238 239 // In the negative test for the user, we loaded the caller via fileid. Switching to the other approach this time. 240 $caller = new assign_portfolio_caller( 241 array( 242 'cmid' => $cm->id, 243 'sid' => $submission->id, 244 'area' => ASSIGNSUBMISSION_FILE_FILEAREA, 245 'component' => 'assignsubmission_file', 246 ) 247 ); 248 $caller->set('user', $wronguser); 249 250 $this->expectException(portfolio_caller_exception::class); 251 $this->expectExceptionMessage('Sorry, the requested file could not be found'); 252 253 $caller->load_data(); 254 } 255 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body