MDL-71367 unit tests: Add missing ->destroy() call to test backups
[moodle.git] / backup / tests / backup_cleanup_task_test.php
CommitLineData
e9eb3ad6
MG
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.
13//
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/>.
16
17/**
18 * Tests for the \core\task\backup_cleanup_task scheduled task.
19 *
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
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27global $CFG;
28require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
29
30/**
31 * Tests for the \core\task\backup_cleanup_task scheduled task.
32 *
33 * @copyright 2021 Mikhail Golenkov <mikhailgolenkov@catalyst-au.net>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class core_backup_cleanup_task_testcase extends advanced_testcase {
37
38 /**
39 * Set up tasks for all tests.
40 */
41 protected function setUp(): void {
42 $this->resetAfterTest(true);
43 }
44
45 /**
46 * Take a backup of the course provided and return backup id.
47 *
48 * @param int $courseid Course id to be backed up.
49 * @return string Backup id.
50 */
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
61 );
62 $controller->execute_plan();
fc530b18 63 $controller->destroy(); // Unset all structures, close files...
e9eb3ad6
MG
64 return $controller->get_backupid();
65 }
66
67 /**
68 * Test the task idle run. Nothing should explode.
69 */
70 public function test_backup_cleanup_task_idle() {
71 $task = new \core\task\backup_cleanup_task();
72 $task->execute();
73 }
74
75 /**
76 * Test the task exits when backup | loglifetime setting is not set.
77 */
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);
86 }
87
88 /**
89 * Test the task deletes records from DB.
90 */
91 public function test_backup_cleanup_task_deletes_records() {
92 global $DB;
93
94 // Create a course.
95 $generator = $this->getDataGenerator();
96 $course = $generator->create_course();
97
98 // Take two backups of the course.
99 $backupid1 = $this->backup_course($course->id);
100 $backupid2 = $this->backup_course($course->id);
101
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);
106
107 // Run the task.
108 $task = new \core\task\backup_cleanup_task();
109 $task->execute();
110
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]));
114
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]));
118 }
119
120 /**
121 * Test the task deletes files from file system.
122 */
123 public function test_backup_cleanup_task_deletes_files() {
124 global $CFG;
125
126 // Create a course.
127 $generator = $this->getDataGenerator();
128 $course = $generator->create_course();
129
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';
135
136 // Create a subdirectory.
137 $subdir = $CFG->backuptempdir . '/subdir';
138 make_writable_directory($subdir);
139
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));
144
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);
148
149 // Run the task.
150 $task = new \core\task\backup_cleanup_task();
151 $task->execute();
152
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));
157 }
158}