/** * Test get_user_mappings */ public function test_get_user_mappings() { global $DB, $USER; $this->resetAfterTest(true); // Create a course and assignment. $coursedata['idnumber'] = 'idnumbercourse'; $coursedata['fullname'] = 'Lightwork Course'; $coursedata['summary'] = 'Lightwork Course description'; $coursedata['summaryformat'] = FORMAT_MOODLE; $course = self::getDataGenerator()->create_course($coursedata); $assigndata['course'] = $course->id; $assigndata['name'] = 'lightwork assignment'; $assign = self::getDataGenerator()->create_module('assign', $assigndata); // Create a manual enrolment record. $manualenroldata['enrol'] = 'manual'; $manualenroldata['status'] = 0; $manualenroldata['courseid'] = $course->id; $enrolid = $DB->insert_record('enrol', $manualenroldata); // Create a teacher and give them capabilities. $context = context_course::instance($course->id); $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3); $context = context_module::instance($assign->cmid); $this->assignUserCapability('mod/assign:revealidentities', $context->id, $roleid); // Create the teacher's enrolment record. $userenrolmentdata['status'] = 0; $userenrolmentdata['enrolid'] = $enrolid; $userenrolmentdata['userid'] = $USER->id; $DB->insert_record('user_enrolments', $userenrolmentdata); // Create a student and give them a user mapping record. $student = self::getDataGenerator()->create_user(); $mapping = new stdClass(); $mapping->assignment = $assign->id; $mapping->userid = $student->id; $DB->insert_record('assign_user_mapping', $mapping); $assignmentids[] = $assign->id; $result = mod_assign_external::get_user_mappings($assignmentids); // We need to execute the return values cleaning process to simulate the web service server. $result = external_api::clean_returnvalue(mod_assign_external::get_user_mappings_returns(), $result); // Check that the correct user mapping information for the student is returned. $this->assertEquals(1, count($result['assignments'])); $assignment = $result['assignments'][0]; $this->assertEquals($assign->id, $assignment['assignmentid']); // Should be one user mapping record. $this->assertEquals(1, count($assignment['mappings'])); $mapping = $assignment['mappings'][0]; $this->assertEquals($student->id, $mapping['userid']); }