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 defined('MOODLE_INTERNAL') || die; 20 21 require_once (__DIR__ . '/show_started_courses_task_test.php'); 22 23 /** 24 * Class containing unit tests for the hide ended courses task. 25 * 26 * It automatically sets the course visibility to hidden when the course end date matches the current day. 27 * 28 * @package core 29 * @copyright 2023 Sara Arjona <sara@moodle.com> 30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 31 * @coversDefaultClass \core\task\hide_ended_courses_task 32 */ 33 class hide_ended_courses_task_test extends \core\task\show_started_courses_task_test { 34 35 /** 36 * Test hide_ended_courses cron task. 37 * 38 * @dataProvider get_courses_provider 39 * @covers ::execute 40 * 41 * @param int $nextweekvisible Number of courses with the end date set to next week to be created. 42 * @param int $yesterdayvisible Number of courses with the end date set to yesterday to be created. 43 * @param int $tomorrowvisible Number of courses with the end date set to tomorrow to be created. 44 * @param bool $createhidden Whether hidden courses should be created or not. 45 */ 46 public function test_hide_ended_courses( 47 int $nextweekvisible, 48 int $yesterdayvisible, 49 int $tomorrowvisible, 50 bool $createhidden = true 51 ) { 52 global $DB; 53 54 $this->resetAfterTest(); 55 56 $generator = $this->getDataGenerator(); 57 58 $visiblecourses = []; 59 $hiddencourses = []; 60 61 $now = time(); 62 $nextweek = $now + WEEKSECS; 63 $yesterday = $now - DAYSECS + MINSECS; 64 $tomorrow = $now + DAYSECS; 65 66 // Visible course that finishes last week. 67 for ($i = 0; $i < $nextweekvisible; $i++) { 68 $generator->create_course(['visible' => true, 'enddate' => $nextweek]); 69 } 70 // Visible course that finished yesterday. 71 for ($i = 0; $i < $yesterdayvisible; $i++) { 72 $visiblecourses[] = $generator->create_course( 73 ['visible' => true, 'startdate' => $yesterday - MINSECS , 'enddate' => $yesterday] 74 )->id; 75 } 76 // Visible course that hasn't finished yet. 77 for ($i = 0; $i < $tomorrowvisible; $i++) { 78 $generator->create_course(['visible' => true, 'enddate' => $tomorrow]); 79 } 80 if ($createhidden) { 81 // Visible course that already finished. 82 $hiddencourses[] = $generator->create_course( 83 ['visible' => false, 'startdate' => $yesterday - MINSECS, 'enddate' => $yesterday] 84 )->id; 85 // Visible course that hasn't finished yet. 86 $hiddencourses[] = $generator->create_course(['visible' => false, 'enddate' => $tomorrow])->id; 87 } 88 $hiddentotal = count($hiddencourses); 89 // Course total also includes site course. 90 $coursetotal = $hiddentotal + $nextweekvisible + $yesterdayvisible + $tomorrowvisible + 1; 91 92 // Check current courses have been created correctly. 93 $this->assertEquals($coursetotal, $DB->count_records('course')); 94 $this->assertEquals(count($hiddencourses), $DB->count_records('course', ['visible' => 0])); 95 96 $sink = $this->redirectEvents(); 97 98 // Run the hide ended courses task. 99 ob_start(); 100 $task = new hide_ended_courses_task(); 101 $task->execute(); 102 ob_end_clean(); 103 104 // Confirm the courses with yesterday as ending date are hidden too. The rest should remain visible. 105 $courses = $DB->get_records('course', ['visible' => 0], '', 'id'); 106 $this->assertCount($hiddentotal + $yesterdayvisible, $courses); 107 $expected = array_merge($hiddencourses, $visiblecourses); 108 $this->assertEquals(asort($expected), asort($courses)); 109 110 // Check the ended course event has been raised. 111 $events = $sink->get_events(); 112 $sink->close(); 113 $this->assertCount($yesterdayvisible, $events); 114 foreach ($events as $event) { 115 $this->assertInstanceOf('\\core\\event\\course_ended', $event); 116 $this->assertArrayHasKey($event->courseid, array_flip($expected)); 117 } 118 } 119 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body