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/>.
17 namespace core_backup
;
19 defined('MOODLE_INTERNAL') ||
die();
22 require_once($CFG->dirroot
. '/backup/util/includes/backup_includes.php');
25 * Tests for the \core\task\backup_cleanup_task scheduled task.
27 * @package core_backup
28 * @copyright 2021 Mikhail Golenkov <mikhailgolenkov@catalyst-au.net>
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 class backup_cleanup_task_test
extends \advanced_testcase
{
34 * Set up tasks for all tests.
36 protected function setUp(): void
{
37 $this->resetAfterTest(true);
41 * Take a backup of the course provided and return backup id.
43 * @param int $courseid Course id to be backed up.
44 * @return string Backup id.
46 private function backup_course(int $courseid): string {
49 $controller = new \backup_controller
(
50 \backup
::TYPE_1COURSE
,
52 \backup
::FORMAT_MOODLE
,
53 \backup
::INTERACTIVE_NO
,
54 \backup
::MODE_AUTOMATED
,
57 $controller->execute_plan();
58 $controller->destroy(); // Unset all structures, close files...
59 return $controller->get_backupid();
63 * Test the task idle run. Nothing should explode.
65 public function test_backup_cleanup_task_idle() {
66 $task = new \core\task\backup_cleanup_task
();
71 * Test the task exits when backup | loglifetime setting is not set.
73 public function test_backup_cleanup_task_exits() {
74 set_config('loglifetime', 0, 'backup');
75 $task = new \core\task\backup_cleanup_task
();
78 $output = ob_get_contents();
80 $this->assertStringContainsString('config is not set', $output);
84 * Test the task deletes records from DB.
86 public function test_backup_cleanup_task_deletes_records() {
90 $generator = $this->getDataGenerator();
91 $course = $generator->create_course();
93 // Take two backups of the course.
94 $backupid1 = $this->backup_course($course->id
);
95 $backupid2 = $this->backup_course($course->id
);
97 // Emulate the first backup to be done 31 days ago.
98 $bcrecord = $DB->get_record('backup_controllers', ['backupid' => $backupid1]);
99 $bcrecord->timecreated
-= DAYSECS
* 31;
100 $DB->update_record('backup_controllers', $bcrecord);
103 $task = new \core\task\backup_cleanup_task
();
106 // There should be no records related to the first backup.
107 $this->assertEquals(0, $DB->count_records('backup_controllers', ['backupid' => $backupid1]));
108 $this->assertEquals(0, $DB->count_records('backup_logs', ['backupid' => $backupid1]));
110 // Records related to the second backup should remain.
111 $this->assertGreaterThan(0, $DB->count_records('backup_controllers', ['backupid' => $backupid2]));
112 $this->assertGreaterThanOrEqual(0, $DB->count_records('backup_logs', ['backupid' => $backupid2]));
116 * Test the task deletes files from file system.
118 public function test_backup_cleanup_task_deletes_files() {
122 $generator = $this->getDataGenerator();
123 $course = $generator->create_course();
125 // Take two backups of the course and get their logs.
126 $backupid1 = $this->backup_course($course->id
);
127 $backupid2 = $this->backup_course($course->id
);
128 $filepath1 = $CFG->backuptempdir
. '/' . $backupid1 . '.log';
129 $filepath2 = $CFG->backuptempdir
. '/' . $backupid2 . '.log';
131 // Create a subdirectory.
132 $subdir = $CFG->backuptempdir
. '/subdir';
133 make_writable_directory($subdir);
135 // Both logs and the dir should exist.
136 $this->assertTrue(file_exists($filepath1));
137 $this->assertTrue(file_exists($filepath2));
138 $this->assertTrue(file_exists($subdir));
140 // Change modification time of the first log and the sub dir to be 8 days ago.
141 touch($filepath1, time() - 8 * DAYSECS
);
142 touch($subdir, time() - 8 * DAYSECS
);
145 $task = new \core\task\backup_cleanup_task
();
148 // Files and directories older than a week are supposed to be removed.
149 $this->assertFalse(file_exists($filepath1));
150 $this->assertFalse(file_exists($subdir));
151 $this->assertTrue(file_exists($filepath2));