weekly release 5.0dev
[moodle.git] / backup / tests / backup_cleanup_task_test.php
blobf32f7804348a043e86030ecea3446481411a0f17
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 namespace core_backup;
19 defined('MOODLE_INTERNAL') || die();
21 global $CFG;
22 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
24 /**
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 {
33 /**
34 * Set up tasks for all tests.
36 protected function setUp(): void {
37 parent::setUp();
38 $this->resetAfterTest(true);
41 /**
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 {
48 // Backup the course.
49 $user = get_admin();
50 $controller = new \backup_controller(
51 \backup::TYPE_1COURSE,
52 $courseid,
53 \backup::FORMAT_MOODLE,
54 \backup::INTERACTIVE_NO,
55 \backup::MODE_AUTOMATED,
56 $user->id
58 $controller->execute_plan();
59 $controller->destroy(); // Unset all structures, close files...
60 return $controller->get_backupid();
63 /**
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();
68 $task->execute();
71 /**
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();
77 ob_start();
78 $task->execute();
79 $output = ob_get_contents();
80 ob_end_clean();
81 $this->assertStringContainsString('config is not set', $output);
84 /**
85 * Test the task deletes records from DB.
87 public function test_backup_cleanup_task_deletes_records(): void {
88 global $DB;
90 // Create a course.
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);
103 // Run the task.
104 $task = new \core\task\backup_cleanup_task();
105 $task->execute();
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 {
120 global $CFG;
122 // Create a course.
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);
145 // Run the task.
146 $task = new \core\task\backup_cleanup_task();
147 $task->execute();
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));