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 |
|
1441 |
ariadna |
19 |
use core\tests\courses_tasks_testcase;
|
1 |
efrain |
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 |
*/
|
1441 |
ariadna |
31 |
final class show_started_courses_task_test extends courses_tasks_testcase {
|
1 |
efrain |
32 |
/**
|
|
|
33 |
* Test show_started_courses cron task.
|
|
|
34 |
*
|
|
|
35 |
* @dataProvider get_courses_provider
|
|
|
36 |
* @covers ::execute
|
|
|
37 |
*
|
1441 |
ariadna |
38 |
* @param int $lastweek Number of courses with the start date set to last week to be created.
|
|
|
39 |
* @param int $yesterday Number of courses with the start date set to yesterday to be created.
|
|
|
40 |
* @param int $tomorrow Number of courses with the start date set to tomorrow to be created.
|
1 |
efrain |
41 |
* @param bool $createvisible Whether visible courses should be created or not.
|
|
|
42 |
*/
|
|
|
43 |
public function test_show_started_courses(
|
1441 |
ariadna |
44 |
int $lastweekcount,
|
|
|
45 |
int $yesterdaycount,
|
|
|
46 |
int $tomorrowcount,
|
1 |
efrain |
47 |
bool $createvisible = true
|
11 |
efrain |
48 |
): void {
|
1 |
efrain |
49 |
global $DB;
|
|
|
50 |
|
|
|
51 |
$this->resetAfterTest();
|
|
|
52 |
|
|
|
53 |
$generator = $this->getDataGenerator();
|
|
|
54 |
|
|
|
55 |
$visiblecourses = [];
|
|
|
56 |
$hiddencourses = [];
|
|
|
57 |
|
|
|
58 |
$now = time();
|
|
|
59 |
$lastweek = $now - WEEKSECS;
|
|
|
60 |
$yesterday = $now - DAYSECS + 60;
|
|
|
61 |
$tomorrow = $now + DAYSECS;
|
|
|
62 |
|
|
|
63 |
// Hidden course that started last week.
|
1441 |
ariadna |
64 |
for ($i = 0; $i < $lastweekcount; $i++) {
|
1 |
efrain |
65 |
$generator->create_course(['visible' => false, 'startdate' => $lastweek]);
|
|
|
66 |
}
|
|
|
67 |
// Hidden course that started yesterday.
|
1441 |
ariadna |
68 |
for ($i = 0; $i < $yesterdaycount; $i++) {
|
1 |
efrain |
69 |
$hiddencourses[] = $generator->create_course(['visible' => false, 'startdate' => $yesterday])->id;
|
|
|
70 |
}
|
|
|
71 |
// Hidden course that hasn't started yet.
|
1441 |
ariadna |
72 |
for ($i = 0; $i < $tomorrowcount; $i++) {
|
1 |
efrain |
73 |
$generator->create_course(['visible' => false, 'startdate' => $tomorrow]);
|
|
|
74 |
}
|
|
|
75 |
if ($createvisible) {
|
|
|
76 |
// Visible course that already started.
|
|
|
77 |
$visiblecourses[] = $generator->create_course(['visible' => true, 'startdate' => $yesterday])->id;
|
|
|
78 |
// Visible course that hasn't started yet.
|
|
|
79 |
$visiblecourses[] = $generator->create_course(['visible' => true, 'startdate' => $tomorrow])->id;
|
|
|
80 |
}
|
|
|
81 |
$visibletotal = count($visiblecourses) + 1;
|
1441 |
ariadna |
82 |
$coursetotal = $visibletotal + $lastweekcount + $yesterdaycount + $tomorrowcount;
|
1 |
efrain |
83 |
|
|
|
84 |
// Check current courses have been created correctly.
|
|
|
85 |
$this->assertEquals($coursetotal, $DB->count_records('course'));
|
|
|
86 |
$this->assertEquals($visibletotal, $DB->count_records('course', ['visible' => 1]));
|
|
|
87 |
|
|
|
88 |
$sink = $this->redirectEvents();
|
|
|
89 |
|
|
|
90 |
// Run the show started courses task.
|
|
|
91 |
ob_start();
|
|
|
92 |
$task = new show_started_courses_task();
|
|
|
93 |
$task->execute();
|
|
|
94 |
ob_end_clean();
|
|
|
95 |
|
|
|
96 |
// Confirm the courses with yesterday as starting date are visible too. The rest should remain hidden.
|
|
|
97 |
$this->assertEquals($coursetotal, $DB->count_records('course'));
|
|
|
98 |
$courses = $DB->get_records('course', ['visible' => 1], '', 'id');
|
1441 |
ariadna |
99 |
$this->assertCount($visibletotal + $yesterdaycount, $courses);
|
1 |
efrain |
100 |
$expected = array_merge($hiddencourses, $visiblecourses);
|
|
|
101 |
$this->assertEquals(asort($expected), asort($courses));
|
|
|
102 |
|
|
|
103 |
// Check the started course event has been raised.
|
|
|
104 |
$events = $sink->get_events();
|
|
|
105 |
$sink->close();
|
1441 |
ariadna |
106 |
$this->assertCount($yesterdaycount, $events);
|
1 |
efrain |
107 |
foreach ($events as $event) {
|
|
|
108 |
$this->assertInstanceOf('\\core\\event\\course_started', $event);
|
|
|
109 |
$this->assertArrayHasKey($event->courseid, array_flip($expected));
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
}
|