Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - https://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
 * Provides {@see mod_subcourse_locallib_testcase} class.
19
 *
20
 * @package     mod_subcourse
21
 * @category    test
22
 * @copyright   2020 David Mudrák <david@moodle.com>
23
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace mod_subcourse;
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
global $CFG;
31
require_once($CFG->dirroot . '/mod/subcourse/locallib.php');
32
 
33
/**
34
 * Unit tests for the functions in the locallib.php file.
35
 *
36
 * @copyright 2020 David Mudrák <david@moodle.com>
37
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class locallib_test extends \advanced_testcase {
40
 
41
    /**
42
     * Test that it is possible to fetch grades from the referenced course.
43
     *
44
     * @covers ::subcourse_grades_update
45
     */
46
    public function test_subcourse_grades_update() {
47
 
48
        $this->resetAfterTest();
49
        $this->setAdminUser();
50
 
51
        $generator = $this->getDataGenerator();
52
 
53
        $metacourse = $generator->create_course();
54
        $refcourse = $generator->create_course();
55
 
56
        $student1 = $generator->create_user();
57
        $student2 = $generator->create_user();
58
 
59
        $generator->enrol_user($student1->id, $metacourse->id, 'student');
60
        $generator->enrol_user($student1->id, $refcourse->id, 'student');
61
        $generator->enrol_user($student2->id, $metacourse->id, 'student');
62
        $generator->enrol_user($student2->id, $refcourse->id, 'student');
63
 
64
        // Give some grades in the referenced course.
65
        $gi = new \grade_item($generator->create_grade_item(['courseid' => $refcourse->id]), false);
66
        $gi->update_final_grade($student1->id, 90, 'test');
67
        $gi->update_final_grade($student2->id, 60, 'test');
68
        $gi->force_regrading();
69
        grade_regrade_final_grades($refcourse->id);
70
 
71
        // Create the Subcourse module instance in the metacourse, representing the final grade in the referenced course.
72
        $subcourse = $generator->create_module('subcourse', [
73
            'course' => $metacourse->id,
74
            'refcourse' => $refcourse->id,
75
        ]);
76
 
77
        $strgrade = subcourse_get_current_grade($subcourse, $student1->id);
78
        $this->assertNull($strgrade);
79
 
80
        // Fetch all students' grades from the refcourse to the metacourse.
81
        subcourse_grades_update($metacourse->id, $subcourse->id, $refcourse->id, null, false, false, [], false);
82
 
83
        // Check the grades were correctly fetched.
84
        $metagrades = grade_get_grades($metacourse->id, 'mod', 'subcourse', $subcourse->id, [$student1->id, $student2->id]);
85
        $this->assertEquals(90, $metagrades->items[0]->grades[$student1->id]->grade);
86
        $this->assertEquals(60, $metagrades->items[0]->grades[$student2->id]->grade);
87
 
88
        $strgrade = subcourse_get_current_grade($subcourse, $student1->id);
89
        $this->assertEquals('90.00', $strgrade);
90
 
91
        // Update the grades in the referenced course.
92
        $gi->update_final_grade($student1->id, 80, 'test');
93
        $gi->update_final_grade($student2->id, 50, 'test');
94
        $gi->force_regrading();
95
        grade_regrade_final_grades($refcourse->id);
96
 
97
        // Fetch again, this time only one student's grades.
98
        subcourse_grades_update($metacourse->id, $subcourse->id, $refcourse->id, null, false, false, [$student1->id], false);
99
 
100
        // Re-check that the student1's grade was updated succesfully.
101
        $metagrades = grade_get_grades($metacourse->id, 'mod', 'subcourse', $subcourse->id, [$student1->id, $student2->id]);
102
        $this->assertEquals(80, $metagrades->items[0]->grades[$student1->id]->grade);
103
    }
104
 
105
    /**
106
     * Test that calling {see subcourse_set_module_viewed()} does not raise errors.
107
     *
108
     * @covers ::subcourse_set_module_viewed
109
     */
110
    public function test_subcourse_set_module_viewed() {
111
 
112
        $this->resetAfterTest();
113
        $this->setAdminUser();
114
 
115
        $generator = $this->getDataGenerator();
116
 
117
        $metacourse = $generator->create_course();
118
        $student = $generator->create_user();
119
        $subcourse = $generator->create_module('subcourse', [
120
            'course' => $metacourse->id,
121
        ]);
122
        $generator->enrol_user($student->id, $metacourse->id, 'student');
123
 
124
        list($course, $cm) = get_course_and_cm_from_instance($subcourse->id, 'subcourse');
125
        $context = \context_module::instance($cm->id);
126
 
127
        subcourse_set_module_viewed($subcourse, $context, $course, $cm);
128
    }
129
}