| 1 | 
           efrain | 
           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 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           31 | 
           final class backup_cleanup_task_test extends \advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           32 | 
              | 
        
        
            | 
            | 
           33 | 
               /**
  | 
        
        
            | 
            | 
           34 | 
                * Set up tasks for all tests.
  | 
        
        
            | 
            | 
           35 | 
                */
  | 
        
        
            | 
            | 
           36 | 
               protected function setUp(): void {
  | 
        
        
           | 1441 | 
           ariadna | 
           37 | 
                   parent::setUp();
  | 
        
        
           | 1 | 
           efrain | 
           38 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           39 | 
               }
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
               /**
  | 
        
        
            | 
            | 
           42 | 
                * Take a backup of the course provided and return backup id.
  | 
        
        
            | 
            | 
           43 | 
                *
  | 
        
        
            | 
            | 
           44 | 
                * @param int $courseid Course id to be backed up.
  | 
        
        
            | 
            | 
           45 | 
                * @return string Backup id.
  | 
        
        
            | 
            | 
           46 | 
                */
  | 
        
        
            | 
            | 
           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
  | 
        
        
            | 
            | 
           57 | 
                   );
  | 
        
        
            | 
            | 
           58 | 
                   $controller->execute_plan();
  | 
        
        
            | 
            | 
           59 | 
                   $controller->destroy(); // Unset all structures, close files...
  | 
        
        
            | 
            | 
           60 | 
                   return $controller->get_backupid();
  | 
        
        
            | 
            | 
           61 | 
               }
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
               /**
  | 
        
        
            | 
            | 
           64 | 
                * Test the task idle run. Nothing should explode.
  | 
        
        
            | 
            | 
           65 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           66 | 
               public function test_backup_cleanup_task_idle(): void {
  | 
        
        
           | 1 | 
           efrain | 
           67 | 
                   $task = new \core\task\backup_cleanup_task();
  | 
        
        
            | 
            | 
           68 | 
                   $task->execute();
  | 
        
        
            | 
            | 
           69 | 
               }
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
               /**
  | 
        
        
            | 
            | 
           72 | 
                * Test the task exits when backup | loglifetime setting is not set.
  | 
        
        
            | 
            | 
           73 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           74 | 
               public function test_backup_cleanup_task_exits(): void {
  | 
        
        
           | 1 | 
           efrain | 
           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);
  | 
        
        
            | 
            | 
           82 | 
               }
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
               /**
  | 
        
        
            | 
            | 
           85 | 
                * Test the task deletes records from DB.
  | 
        
        
            | 
            | 
           86 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           87 | 
               public function test_backup_cleanup_task_deletes_records(): void {
  | 
        
        
           | 1 | 
           efrain | 
           88 | 
                   global $DB;
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
                   // Create a course.
  | 
        
        
            | 
            | 
           91 | 
                   $generator = $this->getDataGenerator();
  | 
        
        
            | 
            | 
           92 | 
                   $course = $generator->create_course();
  | 
        
        
            | 
            | 
           93 | 
              | 
        
        
            | 
            | 
           94 | 
                   // Take two backups of the course.
  | 
        
        
            | 
            | 
           95 | 
                   $backupid1 = $this->backup_course($course->id);
  | 
        
        
            | 
            | 
           96 | 
                   $backupid2 = $this->backup_course($course->id);
  | 
        
        
            | 
            | 
           97 | 
              | 
        
        
            | 
            | 
           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);
  | 
        
        
            | 
            | 
           102 | 
              | 
        
        
            | 
            | 
           103 | 
                   // Run the task.
  | 
        
        
            | 
            | 
           104 | 
                   $task = new \core\task\backup_cleanup_task();
  | 
        
        
            | 
            | 
           105 | 
                   $task->execute();
  | 
        
        
            | 
            | 
           106 | 
              | 
        
        
            | 
            | 
           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]));
  | 
        
        
            | 
            | 
           110 | 
              | 
        
        
            | 
            | 
           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]));
  | 
        
        
            | 
            | 
           114 | 
               }
  | 
        
        
            | 
            | 
           115 | 
              | 
        
        
            | 
            | 
           116 | 
               /**
  | 
        
        
            | 
            | 
           117 | 
                * Test the task deletes files from file system.
  | 
        
        
            | 
            | 
           118 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           119 | 
               public function test_backup_cleanup_task_deletes_files(): void {
  | 
        
        
           | 1 | 
           efrain | 
           120 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           121 | 
              | 
        
        
            | 
            | 
           122 | 
                   // Create a course.
  | 
        
        
            | 
            | 
           123 | 
                   $generator = $this->getDataGenerator();
  | 
        
        
            | 
            | 
           124 | 
                   $course = $generator->create_course();
  | 
        
        
            | 
            | 
           125 | 
              | 
        
        
            | 
            | 
           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';
  | 
        
        
            | 
            | 
           131 | 
              | 
        
        
            | 
            | 
           132 | 
                   // Create a subdirectory.
  | 
        
        
            | 
            | 
           133 | 
                   $subdir = $CFG->backuptempdir . '/subdir';
  | 
        
        
            | 
            | 
           134 | 
                   make_writable_directory($subdir);
  | 
        
        
            | 
            | 
           135 | 
              | 
        
        
            | 
            | 
           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));
  | 
        
        
            | 
            | 
           140 | 
              | 
        
        
            | 
            | 
           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);
  | 
        
        
            | 
            | 
           144 | 
              | 
        
        
            | 
            | 
           145 | 
                   // Run the task.
  | 
        
        
            | 
            | 
           146 | 
                   $task = new \core\task\backup_cleanup_task();
  | 
        
        
            | 
            | 
           147 | 
                   $task->execute();
  | 
        
        
            | 
            | 
           148 | 
              | 
        
        
            | 
            | 
           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));
  | 
        
        
            | 
            | 
           153 | 
               }
  | 
        
        
            | 
            | 
           154 | 
           }
  |