forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
completion_daily_task_test.php
123 lines (100 loc) · 5.13 KB
/
completion_daily_task_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains the class containing unit tests for the daily completion cron task.
*
* @package core
* @copyright 2020 Jun Pataleta
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\task;
use advanced_testcase;
/**
* Class containing unit tests for the daily completion cron task.
*
* @package core
* @copyright 2020 Jun Pataleta
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_completion_cron_task_testcase extends advanced_testcase {
/**
* Test calendar cron task with a broken subscription URL.
*/
public function test_completion_daily_cron() {
global $DB;
$this->resetAfterTest();
set_config('enablecompletion', 1);
set_config('enrol_plugins_enabled', 'self,manual');
$generator = $this->getDataGenerator();
$now = time();
$lastweek = $now - WEEKSECS;
$yesterday = $now - DAYSECS;
$tomorrow = $now + DAYSECS;
// Course with completion enabled and has already started.
$c1 = $generator->create_course(['enablecompletion' => 1, 'startdate' => $lastweek]);
// Course with completion enabled but hasn't started yet.
$c2 = $generator->create_course(['enablecompletion' => 1, 'startdate' => $tomorrow]);
// Completion not enabled.
$c3 = $generator->create_course();
// Create users.
$t1 = $generator->create_user(['username' => 't1']);
$t2 = $generator->create_user(['username' => 't2']);
$s1 = $generator->create_user(['username' => 's1']);
$s2 = $generator->create_user(['username' => 's2']);
// Enrol s1 by self and manual methods to c1.
$generator->enrol_user($s1->id, $c1->id, 'student', 'self', $lastweek);
$generator->enrol_user($s1->id, $c1->id, 'student', 'manual', $yesterday);
// Enrol s1 by self and manual methods to c2.
$generator->enrol_user($s1->id, $c2->id, 'student', 'self');
$generator->enrol_user($s1->id, $c2->id, 'student', 'manual', $tomorrow);
// Enrol s1 by self and manual methods to c3.
$generator->enrol_user($s1->id, $c3->id, 'student', 'self', $lastweek);
$generator->enrol_user($s1->id, $c3->id, 'student');
// Enrol the rest.
foreach ([$c1, $c2, $c3] as $course) {
// Enrol s2 by manual and self enrol methods.
$generator->enrol_user($s2->id, $course->id, 'student', 'self');
$generator->enrol_user($s2->id, $course->id, 'student', 'manual', $course->startdate);
// Enrol t1 as teacher to these courses.
$generator->enrol_user($t1->id, $course->id, 'editingteacher', 'manual', $course->startdate);
$generator->enrol_user($t1->id, $course->id, 'editingteacher', 'manual', $course->startdate);
// Enrol t2 as a non-editing teacher to these courses.
$generator->enrol_user($t1->id, $course->id, 'teacher', 'manual', $course->startdate);
$generator->enrol_user($t1->id, $course->id, 'teacher', 'manual', $course->startdate);
}
// The course completion table should be empty prior to running the task.
$this->assertEquals(0, $DB->count_records('course_completions'));
// Run the daily completion task.
ob_start();
$task = new completion_daily_task();
$task->execute();
ob_end_clean();
// Confirm there are no completion records for teachers nor for courses that haven't started yet or without completion.
list($tsql, $tparams) = $DB->get_in_or_equal([$t1->id, $t2->id], SQL_PARAMS_NAMED);
list($csql, $cparams) = $DB->get_in_or_equal([$c2->id, $c3->id], SQL_PARAMS_NAMED);
$select = "userid $tsql OR course $csql";
$params = array_merge($tparams, $cparams);
$this->assertEmpty($DB->get_records_select('course_completions', $select, $params));
// We should have 2 completion records for both s1 and s2 in course c1.
$this->assertCount(2, $DB->get_records('course_completions'));
// Get s1's completion record from c1.
$s1c1 = $DB->get_record('course_completions', ['userid' => $s1->id, 'course' => $c1->id]);
$this->assertEquals(userdate($now), userdate($s1c1->timeenrolled));
// Get s2's completion record from c1.
$s2c1 = $DB->get_record('course_completions', ['userid' => $s2->id, 'course' => $c1->id]);
$this->assertEquals(userdate($now), userdate($s2c1->timeenrolled));
}
}