Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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  namespace core_backup;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  23  
  24  /**
  25   * Tests for the \core\task\backup_cleanup_task scheduled task.
  26   *
  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
  30   */
  31  class backup_cleanup_task_test extends \advanced_testcase {
  32  
  33      /**
  34       * Set up tasks for all tests.
  35       */
  36      protected function setUp(): void {
  37          $this->resetAfterTest(true);
  38      }
  39  
  40      /**
  41       * Take a backup of the course provided and return backup id.
  42       *
  43       * @param int $courseid Course id to be backed up.
  44       * @return string Backup id.
  45       */
  46      private function backup_course(int $courseid): string {
  47          // Backup the course.
  48          $user = get_admin();
  49          $controller = new \backup_controller(
  50              \backup::TYPE_1COURSE,
  51              $courseid,
  52              \backup::FORMAT_MOODLE,
  53              \backup::INTERACTIVE_NO,
  54              \backup::MODE_AUTOMATED,
  55              $user->id
  56          );
  57          $controller->execute_plan();
  58          $controller->destroy(); // Unset all structures, close files...
  59          return $controller->get_backupid();
  60      }
  61  
  62      /**
  63       * Test the task idle run. Nothing should explode.
  64       */
  65      public function test_backup_cleanup_task_idle() {
  66          $task = new \core\task\backup_cleanup_task();
  67          $task->execute();
  68      }
  69  
  70      /**
  71       * Test the task exits when backup | loglifetime setting is not set.
  72       */
  73      public function test_backup_cleanup_task_exits() {
  74          set_config('loglifetime', 0, 'backup');
  75          $task = new \core\task\backup_cleanup_task();
  76          ob_start();
  77          $task->execute();
  78          $output = ob_get_contents();
  79          ob_end_clean();
  80          $this->assertStringContainsString('config is not set', $output);
  81      }
  82  
  83      /**
  84       * Test the task deletes records from DB.
  85       */
  86      public function test_backup_cleanup_task_deletes_records() {
  87          global $DB;
  88  
  89          // Create a course.
  90          $generator = $this->getDataGenerator();
  91          $course = $generator->create_course();
  92  
  93          // Take two backups of the course.
  94          $backupid1 = $this->backup_course($course->id);
  95          $backupid2 = $this->backup_course($course->id);
  96  
  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);
 101  
 102          // Run the task.
 103          $task = new \core\task\backup_cleanup_task();
 104          $task->execute();
 105  
 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]));
 109  
 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]));
 113      }
 114  
 115      /**
 116       * Test the task deletes files from file system.
 117       */
 118      public function test_backup_cleanup_task_deletes_files() {
 119          global $CFG;
 120  
 121          // Create a course.
 122          $generator = $this->getDataGenerator();
 123          $course = $generator->create_course();
 124  
 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';
 130  
 131          // Create a subdirectory.
 132          $subdir = $CFG->backuptempdir . '/subdir';
 133          make_writable_directory($subdir);
 134  
 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));
 139  
 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);
 143  
 144          // Run the task.
 145          $task = new \core\task\backup_cleanup_task();
 146          $task->execute();
 147  
 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));
 152      }
 153  }