on-demand release 2.4dev
[moodle.git] / mod / assign / tests / externallib_test.php
CommitLineData
1f8c8f61
PC
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
17defined('MOODLE_INTERNAL') || die();
18
19global $CFG;
20
21require_once($CFG->dirroot . '/webservice/tests/helpers.php');
22
23/**
24 * External mod assign functions unit tests
25 *
26 * @package mod_assign
27 * @category external
28 * @copyright 2012 Paul Charsley
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 */
31class mod_assign_external_testcase extends externallib_advanced_testcase {
32
33 /**
34 * Tests set up
35 */
36 protected function setUp() {
37 global $CFG;
38 require_once($CFG->dirroot . '/mod/assign/externallib.php');
39 }
40
41 /**
42 * Test get_grades
43 */
44 public function test_get_grades () {
45 global $DB, $USER;
46
47 $this->resetAfterTest(true);
48 // Create a course and assignment.
49 $coursedata['idnumber'] = 'idnumbercourse';
50 $coursedata['fullname'] = 'Lightwork Course';
51 $coursedata['summary'] = 'Lightwork Course description';
52 $coursedata['summaryformat'] = FORMAT_MOODLE;
53 $course = self::getDataGenerator()->create_course($coursedata);
54
55 $assigndata['course'] = $course->id;
56 $assigndata['name'] = 'lightwork assignment';
57
58 $assign = self::getDataGenerator()->create_module('assign', $assigndata);
59
60 // Create a manual enrolment record.
61 $manual_enrol_data['enrol'] = 'manual';
62 $manual_enrol_data['status'] = 0;
63 $manual_enrol_data['courseid'] = $course->id;
64 $enrolid = $DB->insert_record('enrol', $manual_enrol_data);
65
66 // Create a teacher and give them capabilities.
67 $context = context_course::instance($course->id);
68 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
69 $context = context_module::instance($assign->id);
70 $this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
71
72 // Create the teacher's enrolment record.
73 $user_enrolment_data['status'] = 0;
74 $user_enrolment_data['enrolid'] = $enrolid;
75 $user_enrolment_data['userid'] = $USER->id;
76 $DB->insert_record('user_enrolments', $user_enrolment_data);
77
78 // Create a student and give them a grade.
79 $student = self::getDataGenerator()->create_user();
80 $grade = new stdClass();
81 $grade->assignment = $assign->id;
82 $grade->userid = $student->id;
83 $grade->timecreated = time();
84 $grade->timemodified = $grade->timecreated;
85 $grade->grader = $USER->id;
86 $grade->grade = 75;
87 $grade->locked = false;
88 $grade->mailed = true;
89 $DB->insert_record('assign_grades', $grade);
90
91 $assignmentids[] = $assign->id;
92 $result = mod_assign_external::get_grades($assignmentids);
93
94 // Check that the correct grade information for the student is returned.
95 $this->assertEquals(1, count($result['assignments']));
96 $assignment = $result['assignments'][0];
97 $this->assertEquals($assign->id, $assignment['assignmentid']);
98 $this->assertEquals(1, count($assignment['grades']));
99 $grade = $assignment['grades'][0];
100 $this->assertEquals($student->id, $grade['userid']);
101 $this->assertEquals(75, $grade['grade']);
102 }
103
104}