Proyectos de Subversion Moodle

Rev

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