Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace core_completion;
18
 
19
/**
20
 * Test completion criteria.
21
 *
22
 * @package   core_completion
23
 * @category  test
24
 * @copyright 2021 Mikhail Golenkov <mikhailgolenkov@catalyst-au.net>
25
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
class completion_criteria_test extends \advanced_testcase {
28
 
29
    /**
30
     * Test setup.
31
     */
32
    public function setUp(): void {
33
        global $CFG;
11 efrain 34
        require_once($CFG->dirroot.'/completion/criteria/completion_criteria.php');
1 efrain 35
        require_once($CFG->dirroot.'/completion/criteria/completion_criteria_course.php');
36
        require_once($CFG->dirroot.'/completion/criteria/completion_criteria_activity.php');
37
        require_once($CFG->dirroot.'/completion/criteria/completion_criteria_duration.php');
38
        require_once($CFG->dirroot.'/completion/criteria/completion_criteria_grade.php');
39
        require_once($CFG->dirroot.'/completion/criteria/completion_criteria_date.php');
40
 
41
        $this->setAdminUser();
42
        $this->resetAfterTest();
43
    }
44
 
45
    /**
46
     * Test that activity completion dates are used when activity criteria is marked as completed.
47
     */
48
    public function test_completion_criteria_activity(): void {
49
        global $DB;
50
        $timestarted = time();
51
 
52
        // Create a course, an activity and enrol a user.
53
        $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
54
        $assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id], ['completion' => 1]);
55
        $user = $this->getDataGenerator()->create_user();
56
        $studentrole = $DB->get_record('role', ['shortname' => 'student']);
57
        $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
58
 
59
        // Set completion criteria and mark the user to complete the criteria.
60
        $criteriadata = (object) [
61
            'id' => $course->id,
62
            'criteria_activity' => [$assign->cmid => 1],
63
        ];
64
        $criterion = new \completion_criteria_activity();
65
        $criterion->update_config($criteriadata);
66
        $cmassign = get_coursemodule_from_id('assign', $assign->cmid);
67
        $completion = new \completion_info($course);
68
        $completion->update_state($cmassign, COMPLETION_COMPLETE, $user->id);
69
 
70
        // Completion criteria for the user is supposed to be marked as completed at now().
71
        $result = \core_completion_external::get_activities_completion_status($course->id, $user->id);
72
        $actual = reset($result['statuses']);
73
        $this->assertEquals(1, $actual['state']);
74
        $this->assertGreaterThanOrEqual($timestarted, $actual['timecompleted']);
75
 
76
        // And the whole course is marked as completed at now().
77
        $ccompletion = new \completion_completion(['userid' => $user->id, 'course' => $course->id]);
78
        $this->assertGreaterThanOrEqual($timestarted, $ccompletion->timecompleted);
79
        $this->assertTrue($ccompletion->is_complete());
80
    }
81
 
82
    /**
83
     * Test that enrolment timestart are used when duration criteria is marked as completed.
84
     */
85
    public function test_completion_criteria_duration_timestart(): void {
86
        global $DB;
87
        $timestarted = 1610000000;
88
        $durationperiod = DAYSECS;
89
 
90
        // Create a course and users.
91
        $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
92
        $user = $this->getDataGenerator()->create_and_enrol($course, 'student', null, 'manual', $timestarted);
93
 
94
        // Set completion criteria.
95
        $criteriadata = (object) [
96
            'id' => $course->id,
97
            'criteria_duration' => 1,
98
            'criteria_duration_days' => $durationperiod,
99
        ];
100
        $criterion = new \completion_criteria_duration();
101
        $criterion->update_config($criteriadata);
102
 
103
        // Run completion scheduled task.
104
        $task = new \core\task\completion_regular_task();
105
        $this->expectOutputRegex("/Marking complete/");
106
        $task->execute();
107
        // Hopefully, some day MDL-33320 will be fixed and all these sleeps
108
        // and double cron calls in behat and unit tests will be removed.
109
        sleep(1);
110
        $task->execute();
111
 
112
        // The course for User is supposed to be marked as completed at $timestarted + $durationperiod.
113
        $ccompletion = new \completion_completion(['userid' => $user->id, 'course' => $course->id]);
114
        $this->assertEquals($timestarted + $durationperiod, $ccompletion->timecompleted);
115
        $this->assertTrue($ccompletion->is_complete());
11 efrain 116
 
117
        // Now we want to check the scenario where "now" sits in the middle of the timestart + duration
118
        // and timecreated + duration window.
119
        $nowtime = time();
120
        $timestarted = $nowtime - $durationperiod + (2 * DAYSECS);
121
        $timecreated = $nowtime - $durationperiod - (2 * DAYSECS);
122
 
123
        // Using a new user for this.
124
        $user = $this->getDataGenerator()->create_and_enrol($course, 'student', null, 'manual', $timestarted);
125
 
126
        // We need to manually update the enrollment's time created.
127
        $DB->set_field('user_enrolments', 'timecreated', $timecreated, ['userid' => $user->id]);
128
 
129
        // Run the completion cron. See MDL-33320.
130
        $task->execute();
131
        sleep(1);
132
        $task->execute();
133
 
134
        // We do NOT expect the user to be complete currently.
135
        $ccompletion = new \completion_completion(['userid' => $user->id, 'course' => $course->id]);
136
        $this->assertFalse($ccompletion->is_complete());
137
 
138
        // Now, finally, we will move the timestart to be in the past, but still after the timecreated.
139
        $timestarted = $timecreated + DAYSECS;
140
        $DB->set_field('user_enrolments', 'timestart', $timestarted, ['userid' => $user->id]);
141
 
142
        // Run the completion cron. See MDL-33320.
143
        $task->execute();
144
        sleep(1);
145
        $task->execute();
146
 
147
        // Now they should be complete.
148
        $ccompletion = new \completion_completion(['userid' => $user->id, 'course' => $course->id]);
149
        $this->assertEquals($timestarted + $durationperiod, $ccompletion->timecompleted);
150
        $this->assertTrue($ccompletion->is_complete());
1 efrain 151
    }
152
 
153
    /**
154
     * Test that enrolment timecreated are used when duration criteria is marked as completed.
155
     */
156
    public function test_completion_criteria_duration_timecreated(): void {
157
        global $DB;
158
 
159
        $timecreated = 1620000000;
160
        $durationperiod = DAYSECS;
161
 
162
        // Create a course and users.
163
        $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
164
 
165
        // Create and enrol user with an empty time start, but update the record like it was created at $timecreated.
166
        $user = $this->getDataGenerator()->create_and_enrol($course);
167
        $DB->set_field('user_enrolments', 'timecreated', $timecreated, ['userid' => $user->id]);
168
 
169
        // Set completion criteria.
170
        $criteriadata = (object) [
171
            'id' => $course->id,
172
            'criteria_duration' => 1,
173
            'criteria_duration_days' => $durationperiod,
174
        ];
175
        $criterion = new \completion_criteria_duration();
176
        $criterion->update_config($criteriadata);
177
 
178
        // Run completion scheduled task.
179
        $task = new \core\task\completion_regular_task();
180
        $this->expectOutputRegex("/Marking complete/");
181
        $task->execute();
182
 
183
        // Hopefully, some day MDL-33320 will be fixed and all these sleeps
184
        // and double cron calls in behat and unit tests will be removed.
185
        sleep(1);
186
        $task->execute();
187
 
188
        // The course for user is supposed to be marked as completed at $timecreated + $durationperiod.
189
        $ccompletion = new \completion_completion(['userid' => $user->id, 'course' => $course->id]);
190
        $this->assertEquals($timecreated + $durationperiod, $ccompletion->timecompleted);
191
        $this->assertTrue($ccompletion->is_complete());
192
    }
193
 
194
    /**
195
     * Test that criteria date is used as a course completion date.
196
     */
197
    public function test_completion_criteria_date(): void {
198
        global $DB;
199
        $timeend = 1610000000;
200
 
201
        // Create a course and enrol a user.
202
        $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
203
        $user = $this->getDataGenerator()->create_user();
204
        $studentrole = $DB->get_record('role', ['shortname' => 'student']);
205
        $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
206
 
207
        // Set completion criteria.
208
        $criteriadata = (object) [
209
            'id' => $course->id,
210
            'criteria_date' => 1,
211
            'criteria_date_value' => $timeend,
212
        ];
213
        $criterion = new \completion_criteria_date();
214
        $criterion->update_config($criteriadata);
215
 
216
        // Run completion scheduled task.
217
        $task = new \core\task\completion_regular_task();
218
        $this->expectOutputRegex("/Marking complete/");
219
        $task->execute();
220
        // Hopefully, some day MDL-33320 will be fixed and all these sleeps
221
        // and double cron calls in behat and unit tests will be removed.
222
        sleep(1);
223
        $task->execute();
224
 
225
        // The course is supposed to be marked as completed at $timeend.
226
        $ccompletion = new \completion_completion(['userid' => $user->id, 'course' => $course->id]);
227
        $this->assertEquals($timeend, $ccompletion->timecompleted);
228
        $this->assertTrue($ccompletion->is_complete());
229
    }
230
 
231
    /**
232
     * Test that grade timemodified is used when grade criteria is marked as completed.
233
     */
234
    public function test_completion_criteria_grade(): void {
235
        global $DB;
236
        $timegraded = 1610000000;
237
 
238
        // Create a course and enrol a couple of users.
239
        $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
240
        $user1 = $this->getDataGenerator()->create_user();
241
        $user2 = $this->getDataGenerator()->create_user();
242
        $studentrole = $DB->get_record('role', ['shortname' => 'student']);
243
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, $studentrole->id);
244
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id);
245
 
246
        // Set completion criteria.
247
        $criteriadata = (object) [
248
            'id' => $course->id,
249
            'criteria_grade' => 1,
250
            'criteria_grade_value' => 66,
251
        ];
252
        $criterion = new \completion_criteria_grade();
253
        $criterion->update_config($criteriadata);
254
 
255
        $coursegradeitem = \grade_item::fetch_course_item($course->id);
256
 
257
        // Grade User 1 with a passing grade.
258
        $grade1 = new \grade_grade();
259
        $grade1->itemid = $coursegradeitem->id;
260
        $grade1->timemodified = $timegraded;
261
        $grade1->userid = $user1->id;
262
        $grade1->finalgrade = 80;
263
        $grade1->insert();
264
 
265
        // Grade User 2 with a non-passing grade.
266
        $grade2 = new \grade_grade();
267
        $grade2->itemid = $coursegradeitem->id;
268
        $grade2->timemodified = $timegraded;
269
        $grade2->userid = $user2->id;
270
        $grade2->finalgrade = 40;
271
        $grade2->insert();
272
 
273
        // Run completion scheduled task.
274
        $task = new \core\task\completion_regular_task();
275
        $this->expectOutputRegex("/Marking complete/");
276
        $task->execute();
277
        // Hopefully, some day MDL-33320 will be fixed and all these sleeps
278
        // and double cron calls in behat and unit tests will be removed.
279
        sleep(1);
280
        $task->execute();
281
 
282
        // The course for User 1 is supposed to be marked as completed when the user was graded.
283
        $ccompletion = new \completion_completion(['userid' => $user1->id, 'course' => $course->id]);
284
        $this->assertEquals($timegraded, $ccompletion->timecompleted);
285
        $this->assertTrue($ccompletion->is_complete());
286
 
287
        // The course for User 2 is supposed to be marked as not completed.
288
        $ccompletion = new \completion_completion(['userid' => $user2->id, 'course' => $course->id]);
289
        $this->assertFalse($ccompletion->is_complete());
290
    }
291
}