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\task;
|
|
|
18 |
|
|
|
19 |
use advanced_testcase;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Class containing unit tests for the show started courses task.
|
|
|
23 |
*
|
|
|
24 |
* It automatically sets the course visibility to shown when the course start date matches the current day.
|
|
|
25 |
*
|
|
|
26 |
* @package core
|
|
|
27 |
* @copyright 2023 Sara Arjona <sara@moodle.com>
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
* @coversDefaultClass \core\task\show_started_courses_task
|
|
|
30 |
*/
|
|
|
31 |
class show_started_courses_task_test extends advanced_testcase {
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Test show_started_courses cron task.
|
|
|
35 |
*
|
|
|
36 |
* @dataProvider get_courses_provider
|
|
|
37 |
* @covers ::execute
|
|
|
38 |
*
|
|
|
39 |
* @param int $lastweekhidden Number of courses with the start date set to last week to be created.
|
|
|
40 |
* @param int $yesterdayhidden Number of courses with the start date set to yesterday to be created.
|
|
|
41 |
* @param int $tomorrowhidden Number of courses with the start date set to tomorrow to be created.
|
|
|
42 |
* @param bool $createvisible Whether visible courses should be created or not.
|
|
|
43 |
*/
|
|
|
44 |
public function test_show_started_courses(
|
|
|
45 |
int $lastweekhidden,
|
|
|
46 |
int $yesterdayhidden,
|
|
|
47 |
int $tomorrowhidden,
|
|
|
48 |
bool $createvisible = true
|
11 |
efrain |
49 |
): void {
|
1 |
efrain |
50 |
global $DB;
|
|
|
51 |
|
|
|
52 |
$this->resetAfterTest();
|
|
|
53 |
|
|
|
54 |
$generator = $this->getDataGenerator();
|
|
|
55 |
|
|
|
56 |
$visiblecourses = [];
|
|
|
57 |
$hiddencourses = [];
|
|
|
58 |
|
|
|
59 |
$now = time();
|
|
|
60 |
$lastweek = $now - WEEKSECS;
|
|
|
61 |
$yesterday = $now - DAYSECS + 60;
|
|
|
62 |
$tomorrow = $now + DAYSECS;
|
|
|
63 |
|
|
|
64 |
// Hidden course that started last week.
|
|
|
65 |
for ($i = 0; $i < $lastweekhidden; $i++) {
|
|
|
66 |
$generator->create_course(['visible' => false, 'startdate' => $lastweek]);
|
|
|
67 |
}
|
|
|
68 |
// Hidden course that started yesterday.
|
|
|
69 |
for ($i = 0; $i < $yesterdayhidden; $i++) {
|
|
|
70 |
$hiddencourses[] = $generator->create_course(['visible' => false, 'startdate' => $yesterday])->id;
|
|
|
71 |
}
|
|
|
72 |
// Hidden course that hasn't started yet.
|
|
|
73 |
for ($i = 0; $i < $tomorrowhidden; $i++) {
|
|
|
74 |
$generator->create_course(['visible' => false, 'startdate' => $tomorrow]);
|
|
|
75 |
}
|
|
|
76 |
if ($createvisible) {
|
|
|
77 |
// Visible course that already started.
|
|
|
78 |
$visiblecourses[] = $generator->create_course(['visible' => true, 'startdate' => $yesterday])->id;
|
|
|
79 |
// Visible course that hasn't started yet.
|
|
|
80 |
$visiblecourses[] = $generator->create_course(['visible' => true, 'startdate' => $tomorrow])->id;
|
|
|
81 |
}
|
|
|
82 |
$visibletotal = count($visiblecourses) + 1;
|
|
|
83 |
$coursetotal = $visibletotal + $lastweekhidden + $yesterdayhidden + $tomorrowhidden;
|
|
|
84 |
|
|
|
85 |
// Check current courses have been created correctly.
|
|
|
86 |
$this->assertEquals($coursetotal, $DB->count_records('course'));
|
|
|
87 |
$this->assertEquals($visibletotal, $DB->count_records('course', ['visible' => 1]));
|
|
|
88 |
|
|
|
89 |
$sink = $this->redirectEvents();
|
|
|
90 |
|
|
|
91 |
// Run the show started courses task.
|
|
|
92 |
ob_start();
|
|
|
93 |
$task = new show_started_courses_task();
|
|
|
94 |
$task->execute();
|
|
|
95 |
ob_end_clean();
|
|
|
96 |
|
|
|
97 |
// Confirm the courses with yesterday as starting date are visible too. The rest should remain hidden.
|
|
|
98 |
$this->assertEquals($coursetotal, $DB->count_records('course'));
|
|
|
99 |
$courses = $DB->get_records('course', ['visible' => 1], '', 'id');
|
|
|
100 |
$this->assertCount($visibletotal + $yesterdayhidden, $courses);
|
|
|
101 |
$expected = array_merge($hiddencourses, $visiblecourses);
|
|
|
102 |
$this->assertEquals(asort($expected), asort($courses));
|
|
|
103 |
|
|
|
104 |
// Check the started course event has been raised.
|
|
|
105 |
$events = $sink->get_events();
|
|
|
106 |
$sink->close();
|
|
|
107 |
$this->assertCount($yesterdayhidden, $events);
|
|
|
108 |
foreach ($events as $event) {
|
|
|
109 |
$this->assertInstanceOf('\\core\\event\\course_started', $event);
|
|
|
110 |
$this->assertArrayHasKey($event->courseid, array_flip($expected));
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* Data provider for test_show_started_courses.
|
|
|
116 |
*
|
|
|
117 |
* @return array
|
|
|
118 |
*/
|
|
|
119 |
public function get_courses_provider(): array {
|
|
|
120 |
return [
|
|
|
121 |
'No hidden courses' => [
|
|
|
122 |
'lastweek' => 0,
|
|
|
123 |
'yesterday' => 0,
|
|
|
124 |
'tomorrow' => 0,
|
|
|
125 |
],
|
|
|
126 |
'No hidden courses (without visible courses)' => [
|
|
|
127 |
'lastweek' => 0,
|
|
|
128 |
'yesterday' => 0,
|
|
|
129 |
'tomorrow' => 0,
|
|
|
130 |
'createvisible' => false,
|
|
|
131 |
],
|
|
|
132 |
'Hidden courses with last week or tomorrow dates' => [
|
|
|
133 |
'lastweek' => 2,
|
|
|
134 |
'yesterday' => 0,
|
|
|
135 |
'tomorrow' => 2,
|
|
|
136 |
],
|
|
|
137 |
'One hidden course of each type (last week, yesterday and tomorrow)' => [
|
|
|
138 |
'lastweek' => 1,
|
|
|
139 |
'yesterday' => 1,
|
|
|
140 |
'tomorrow' => 1,
|
|
|
141 |
],
|
|
|
142 |
'Different hidden courses of each type' => [
|
|
|
143 |
'lastweek' => 2,
|
|
|
144 |
'yesterday' => 3,
|
|
|
145 |
'tomorrow' => 4,
|
|
|
146 |
],
|
|
|
147 |
'A couple of hidden courses of each type (without visible courses)' => [
|
|
|
148 |
'lastweek' => 2,
|
|
|
149 |
'yesterday' => 2,
|
|
|
150 |
'tomorrow' => 2,
|
|
|
151 |
'createvisible' => false,
|
|
|
152 |
],
|
|
|
153 |
'Only a few hidden courses for yesterday' => [
|
|
|
154 |
'lastweek' => 0,
|
|
|
155 |
'yesterday' => 5,
|
|
|
156 |
'tomorrow' => 0,
|
|
|
157 |
],
|
|
|
158 |
'Only a few hidden courses for yesterday (without visible courses)' => [
|
|
|
159 |
'lastweek' => 0,
|
|
|
160 |
'yesterday' => 5,
|
|
|
161 |
'tomorrow' => 0,
|
|
|
162 |
'createvisible' => false,
|
|
|
163 |
],
|
|
|
164 |
];
|
|
|
165 |
}
|
|
|
166 |
}
|