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\task;
18
 
1441 ariadna 19
use core\tests\courses_tasks_testcase;
1 efrain 20
 
21
/**
22
 * Class containing unit tests for the hide ended courses task.
23
 *
24
 * It automatically sets the course visibility to hidden when the course end 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\hide_ended_courses_task
30
 */
1441 ariadna 31
final class hide_ended_courses_task_test extends courses_tasks_testcase {
1 efrain 32
    /**
33
     * Test hide_ended_courses cron task.
34
     *
1441 ariadna 35
     * @dataProvider hide_end_courses_provider
1 efrain 36
     * @covers ::execute
37
     *
38
     * @param int $nextweekvisible Number of courses with the end date set to next week to be created.
39
     * @param int $yesterdayvisible Number of courses with the end date set to yesterday to be created.
40
     * @param int $tomorrowvisible Number of courses with the end date set to tomorrow to be created.
41
     * @param bool $createhidden Whether hidden courses should be created or not.
42
     */
43
    public function test_hide_ended_courses(
44
        int $nextweekvisible,
45
        int $yesterdayvisible,
46
        int $tomorrowvisible,
47
        bool $createhidden = 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
        $nextweek = $now + WEEKSECS;
60
        $yesterday = $now - DAYSECS + MINSECS;
61
        $tomorrow = $now + DAYSECS;
62
 
63
        // Visible course that finishes last week.
64
        for ($i = 0; $i < $nextweekvisible; $i++) {
65
             $generator->create_course(['visible' => true, 'enddate' => $nextweek]);
66
        }
67
        // Visible course that finished yesterday.
68
        for ($i = 0; $i < $yesterdayvisible; $i++) {
69
            $visiblecourses[] = $generator->create_course(
70
                ['visible' => true, 'startdate' => $yesterday - MINSECS , 'enddate' => $yesterday]
71
            )->id;
72
        }
73
        // Visible course that hasn't finished yet.
74
        for ($i = 0; $i < $tomorrowvisible; $i++) {
75
            $generator->create_course(['visible' => true, 'enddate' => $tomorrow]);
76
        }
77
        if ($createhidden) {
78
            // Visible course that already finished.
79
            $hiddencourses[] = $generator->create_course(
80
                ['visible' => false, 'startdate' => $yesterday - MINSECS, 'enddate' => $yesterday]
81
            )->id;
82
            // Visible course that hasn't finished yet.
83
            $hiddencourses[] = $generator->create_course(['visible' => false, 'enddate' => $tomorrow])->id;
84
        }
85
        $hiddentotal = count($hiddencourses);
86
        // Course total also includes site course.
87
        $coursetotal = $hiddentotal + $nextweekvisible + $yesterdayvisible + $tomorrowvisible + 1;
88
 
89
        // Check current courses have been created correctly.
90
        $this->assertEquals($coursetotal, $DB->count_records('course'));
91
        $this->assertEquals(count($hiddencourses), $DB->count_records('course', ['visible' => 0]));
92
 
93
        $sink = $this->redirectEvents();
94
 
95
        // Run the hide ended courses task.
96
        ob_start();
97
        $task = new hide_ended_courses_task();
98
        $task->execute();
99
        ob_end_clean();
100
 
101
        // Confirm the courses with yesterday as ending date are hidden too. The rest should remain visible.
102
        $courses = $DB->get_records('course', ['visible' => 0], '', 'id');
103
        $this->assertCount($hiddentotal + $yesterdayvisible, $courses);
104
        $expected = array_merge($hiddencourses, $visiblecourses);
105
        $this->assertEquals(asort($expected), asort($courses));
106
 
107
        // Check the ended course event has been raised.
108
        $events = $sink->get_events();
109
        $sink->close();
110
        $this->assertCount($yesterdayvisible, $events);
111
        foreach ($events as $event) {
112
            $this->assertInstanceOf('\\core\\event\\course_ended', $event);
113
            $this->assertArrayHasKey($event->courseid, array_flip($expected));
114
        }
115
    }
1441 ariadna 116
 
117
    /**
118
     * Data provider for test_hide_ended_courses.
119
     *
120
     * @return array
121
     */
122
    public static function hide_end_courses_provider(): array {
123
        return array_map(
124
            function ($args): array {
125
                return [
126
                    'nextweekvisible' => $args['lastweekcount'],
127
                    'yesterdayvisible' => $args['yesterdaycount'],
128
                    'tomorrowvisible' => $args['tomorrowcount'],
129
                    'createhidden' => $args['createvisible'] ?? true,
130
                ];
131
            },
132
            self::get_courses_provider()
133
        );
134
    }
1 efrain 135
}