2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Tests for the \core\task\backup_cleanup_task scheduled task.
20 * @package core_backup
21 * @copyright 2021 Mikhail Golenkov <mikhailgolenkov@catalyst-au.net>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
31 * Tests for the \core\task\backup_cleanup_task scheduled task.
33 * @copyright 2021 Mikhail Golenkov <mikhailgolenkov@catalyst-au.net>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class core_backup_cleanup_task_testcase extends advanced_testcase {
39 * Set up tasks for all tests.
41 protected function setUp(): void {
42 $this->resetAfterTest(true);
46 * Take a backup of the course provided and return backup id.
48 * @param int $courseid Course id to be backed up.
49 * @return string Backup id.
51 private function backup_course(int $courseid): string {
54 $controller = new \backup_controller(
55 \backup::TYPE_1COURSE,
57 \backup::FORMAT_MOODLE,
58 \backup::INTERACTIVE_NO,
59 \backup::MODE_AUTOMATED,
62 $controller->execute_plan();
63 return $controller->get_backupid();
67 * Test the task idle run. Nothing should explode.
69 public function test_backup_cleanup_task_idle() {
70 $task = new \core\task\backup_cleanup_task();
75 * Test the task exits when backup | loglifetime setting is not set.
77 public function test_backup_cleanup_task_exits() {
78 set_config('loglifetime', 0, 'backup');
79 $task = new \core\task\backup_cleanup_task();
82 $output = ob_get_contents();
84 $this->assertStringContainsString('config is not set', $output);
88 * Test the task deletes records from DB.
90 public function test_backup_cleanup_task_deletes_records() {
94 $generator = $this->getDataGenerator();
95 $course = $generator->create_course();
97 // Take two backups of the course.
98 $backupid1 = $this->backup_course($course->id);
99 $backupid2 = $this->backup_course($course->id);
101 // Emulate the first backup to be done 31 days ago.
102 $bcrecord = $DB->get_record('backup_controllers', ['backupid' => $backupid1]);
103 $bcrecord->timecreated -= DAYSECS * 31;
104 $DB->update_record('backup_controllers', $bcrecord);
107 $task = new \core\task\backup_cleanup_task();
110 // There should be no records related to the first backup.
111 $this->assertEquals(0, $DB->count_records('backup_controllers', ['backupid' => $backupid1]));
112 $this->assertEquals(0, $DB->count_records('backup_logs', ['backupid' => $backupid1]));
114 // Records related to the second backup should remain.
115 $this->assertGreaterThan(0, $DB->count_records('backup_controllers', ['backupid' => $backupid2]));
116 $this->assertGreaterThanOrEqual(0, $DB->count_records('backup_logs', ['backupid' => $backupid2]));
120 * Test the task deletes files from file system.
122 public function test_backup_cleanup_task_deletes_files() {
126 $generator = $this->getDataGenerator();
127 $course = $generator->create_course();
129 // Take two backups of the course and get their logs.
130 $backupid1 = $this->backup_course($course->id);
131 $backupid2 = $this->backup_course($course->id);
132 $filepath1 = $CFG->backuptempdir . '/' . $backupid1 . '.log';
133 $filepath2 = $CFG->backuptempdir . '/' . $backupid2 . '.log';
135 // Create a subdirectory.
136 $subdir = $CFG->backuptempdir . '/subdir';
137 make_writable_directory($subdir);
139 // Both logs and the dir should exist.
140 $this->assertTrue(file_exists($filepath1));
141 $this->assertTrue(file_exists($filepath2));
142 $this->assertTrue(file_exists($subdir));
144 // Change modification time of the first log and the sub dir to be 8 days ago.
145 touch($filepath1, time() - 8 * DAYSECS);
146 touch($subdir, time() - 8 * DAYSECS);
149 $task = new \core\task\backup_cleanup_task();
152 // Files and directories older than a week are supposed to be removed.
153 $this->assertFalse(file_exists($filepath1));
154 $this->assertFalse(file_exists($subdir));
155 $this->assertTrue(file_exists($filepath2));