Merge branch 'MDL-72887-M311' of https://github.com/michael-milette/moodle into MOODL...
[moodle.git] / backup / tests / backup_cleanup_task_test.php
blob98fdbb314fdcb01b9f09c53ed8a06238a928bce4
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.
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/>.
17 /**
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();
27 global $CFG;
28 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
30 /**
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 {
38 /**
39 * Set up tasks for all tests.
41 protected function setUp(): void {
42 $this->resetAfterTest(true);
45 /**
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 {
52 // Backup the course.
53 $user = get_admin();
54 $controller = new \backup_controller(
55 \backup::TYPE_1COURSE,
56 $courseid,
57 \backup::FORMAT_MOODLE,
58 \backup::INTERACTIVE_NO,
59 \backup::MODE_AUTOMATED,
60 $user->id
62 $controller->execute_plan();
63 $controller->destroy(); // Unset all structures, close files...
64 return $controller->get_backupid();
67 /**
68 * Test the task idle run. Nothing should explode.
70 public function test_backup_cleanup_task_idle() {
71 $task = new \core\task\backup_cleanup_task();
72 $task->execute();
75 /**
76 * Test the task exits when backup | loglifetime setting is not set.
78 public function test_backup_cleanup_task_exits() {
79 set_config('loglifetime', 0, 'backup');
80 $task = new \core\task\backup_cleanup_task();
81 ob_start();
82 $task->execute();
83 $output = ob_get_contents();
84 ob_end_clean();
85 $this->assertStringContainsString('config is not set', $output);
88 /**
89 * Test the task deletes records from DB.
91 public function test_backup_cleanup_task_deletes_records() {
92 global $DB;
94 // Create a course.
95 $generator = $this->getDataGenerator();
96 $course = $generator->create_course();
98 // Take two backups of the course.
99 $backupid1 = $this->backup_course($course->id);
100 $backupid2 = $this->backup_course($course->id);
102 // Emulate the first backup to be done 31 days ago.
103 $bcrecord = $DB->get_record('backup_controllers', ['backupid' => $backupid1]);
104 $bcrecord->timecreated -= DAYSECS * 31;
105 $DB->update_record('backup_controllers', $bcrecord);
107 // Run the task.
108 $task = new \core\task\backup_cleanup_task();
109 $task->execute();
111 // There should be no records related to the first backup.
112 $this->assertEquals(0, $DB->count_records('backup_controllers', ['backupid' => $backupid1]));
113 $this->assertEquals(0, $DB->count_records('backup_logs', ['backupid' => $backupid1]));
115 // Records related to the second backup should remain.
116 $this->assertGreaterThan(0, $DB->count_records('backup_controllers', ['backupid' => $backupid2]));
117 $this->assertGreaterThanOrEqual(0, $DB->count_records('backup_logs', ['backupid' => $backupid2]));
121 * Test the task deletes files from file system.
123 public function test_backup_cleanup_task_deletes_files() {
124 global $CFG;
126 // Create a course.
127 $generator = $this->getDataGenerator();
128 $course = $generator->create_course();
130 // Take two backups of the course and get their logs.
131 $backupid1 = $this->backup_course($course->id);
132 $backupid2 = $this->backup_course($course->id);
133 $filepath1 = $CFG->backuptempdir . '/' . $backupid1 . '.log';
134 $filepath2 = $CFG->backuptempdir . '/' . $backupid2 . '.log';
136 // Create a subdirectory.
137 $subdir = $CFG->backuptempdir . '/subdir';
138 make_writable_directory($subdir);
140 // Both logs and the dir should exist.
141 $this->assertTrue(file_exists($filepath1));
142 $this->assertTrue(file_exists($filepath2));
143 $this->assertTrue(file_exists($subdir));
145 // Change modification time of the first log and the sub dir to be 8 days ago.
146 touch($filepath1, time() - 8 * DAYSECS);
147 touch($subdir, time() - 8 * DAYSECS);
149 // Run the task.
150 $task = new \core\task\backup_cleanup_task();
151 $task->execute();
153 // Files and directories older than a week are supposed to be removed.
154 $this->assertFalse(file_exists($filepath1));
155 $this->assertFalse(file_exists($subdir));
156 $this->assertTrue(file_exists($filepath2));