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
{
38 $this->resetAfterTest(true);
42 * Take a backup of the course provided and return backup id.
44 * @param int $courseid Course id to be backed up.
45 * @return string Backup id.
47 private function backup_course(int $courseid): string {
50 $controller = new \backup_controller
(
51 \backup
::TYPE_1COURSE
,
53 \backup
::FORMAT_MOODLE
,
54 \backup
::INTERACTIVE_NO
,
55 \backup
::MODE_AUTOMATED
,
58 $controller->execute_plan();
59 $controller->destroy(); // Unset all structures, close files...
60 return $controller->get_backupid();
64 * Test the task idle run. Nothing should explode.
66 public function test_backup_cleanup_task_idle(): void
{
67 $task = new \core\task\backup_cleanup_task
();
72 * Test the task exits when backup | loglifetime setting is not set.
74 public function test_backup_cleanup_task_exits(): void
{
75 set_config('loglifetime', 0, 'backup');
76 $task = new \core\task\backup_cleanup_task
();
79 $output = ob_get_contents();
81 $this->assertStringContainsString('config is not set', $output);
85 * Test the task deletes records from DB.
87 public function test_backup_cleanup_task_deletes_records(): void
{
91 $generator = $this->getDataGenerator();
92 $course = $generator->create_course();
94 // Take two backups of the course.
95 $backupid1 = $this->backup_course($course->id
);
96 $backupid2 = $this->backup_course($course->id
);
98 // Emulate the first backup to be done 31 days ago.
99 $bcrecord = $DB->get_record('backup_controllers', ['backupid' => $backupid1]);
100 $bcrecord->timecreated
-= DAYSECS
* 31;
101 $DB->update_record('backup_controllers', $bcrecord);
104 $task = new \core\task\backup_cleanup_task
();
107 // There should be no records related to the first backup.
108 $this->assertEquals(0, $DB->count_records('backup_controllers', ['backupid' => $backupid1]));
109 $this->assertEquals(0, $DB->count_records('backup_logs', ['backupid' => $backupid1]));
111 // Records related to the second backup should remain.
112 $this->assertGreaterThan(0, $DB->count_records('backup_controllers', ['backupid' => $backupid2]));
113 $this->assertGreaterThanOrEqual(0, $DB->count_records('backup_logs', ['backupid' => $backupid2]));
117 * Test the task deletes files from file system.
119 public function test_backup_cleanup_task_deletes_files(): void
{
123 $generator = $this->getDataGenerator();
124 $course = $generator->create_course();
126 // Take two backups of the course and get their logs.
127 $backupid1 = $this->backup_course($course->id
);
128 $backupid2 = $this->backup_course($course->id
);
129 $filepath1 = $CFG->backuptempdir
. '/' . $backupid1 . '.log';
130 $filepath2 = $CFG->backuptempdir
. '/' . $backupid2 . '.log';
132 // Create a subdirectory.
133 $subdir = $CFG->backuptempdir
. '/subdir';
134 make_writable_directory($subdir);
136 // Both logs and the dir should exist.
137 $this->assertTrue(file_exists($filepath1));
138 $this->assertTrue(file_exists($filepath2));
139 $this->assertTrue(file_exists($subdir));
141 // Change modification time of the first log and the sub dir to be 8 days ago.
142 touch($filepath1, time() - 8 * DAYSECS
);
143 touch($subdir, time() - 8 * DAYSECS
);
146 $task = new \core\task\backup_cleanup_task
();
149 // Files and directories older than a week are supposed to be removed.
150 $this->assertFalse(file_exists($filepath1));
151 $this->assertFalse(file_exists($subdir));
152 $this->assertTrue(file_exists($filepath2));