| 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\task;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | global $CFG;
 | 
        
           |  |  | 22 | require_once($CFG->dirroot . '/backup/util/helper/backup_cron_helper.class.php');
 | 
        
           |  |  | 23 |   | 
        
           |  |  | 24 | /**
 | 
        
           |  |  | 25 |  * Class containing unit tests for the task do the automation backup and report.
 | 
        
           |  |  | 26 |  *
 | 
        
           |  |  | 27 |  * @package    core
 | 
        
           |  |  | 28 |  * @copyright  2024 Huong Nguyen <huongnv13@gmail.com>
 | 
        
           |  |  | 29 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 30 |  */
 | 
        
           | 1441 | ariadna | 31 | final class automated_backup_task_test extends \advanced_testcase {
 | 
        
           | 1 | efrain | 32 |   | 
        
           |  |  | 33 |     use task_trait;
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 35 |     /**
 | 
        
           |  |  | 36 |      * Test the automated backup and report tasks.
 | 
        
           |  |  | 37 |      *
 | 
        
           | 1441 | ariadna | 38 |      * @covers \core\task\automated_backup_report_task::execute
 | 
        
           | 1 | efrain | 39 |      * @covers \backup_cron_automated_helper::send_backup_status_to_admin
 | 
        
           |  |  | 40 |      * @covers \backup_cron_automated_helper::run_automated_backup
 | 
        
           |  |  | 41 |      * @covers \backup_cron_automated_helper::check_and_push_automated_backups
 | 
        
           |  |  | 42 |      */
 | 
        
           |  |  | 43 |     public function test_automated_backup(): void {
 | 
        
           |  |  | 44 |         global $DB;
 | 
        
           |  |  | 45 |         $this->resetAfterTest();
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 |         // Enable automated back up.
 | 
        
           |  |  | 48 |         set_config(
 | 
        
           |  |  | 49 |             'backup_auto_active',
 | 
        
           |  |  | 50 |             true,
 | 
        
           |  |  | 51 |             'backup',
 | 
        
           |  |  | 52 |         );
 | 
        
           |  |  | 53 |         set_config(
 | 
        
           |  |  | 54 |             'backup_auto_weekdays',
 | 
        
           |  |  | 55 |             '1111111',
 | 
        
           |  |  | 56 |             'backup',
 | 
        
           |  |  | 57 |         );
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 |         // Create courses.
 | 
        
           |  |  | 60 |         $course1 = $this->getDataGenerator()->create_course();
 | 
        
           |  |  | 61 |         $course2 = $this->getDataGenerator()->create_course();
 | 
        
           |  |  | 62 |   | 
        
           |  |  | 63 |         // Create course backups.
 | 
        
           |  |  | 64 |         $DB->insert_records(
 | 
        
           |  |  | 65 |             'backup_courses',
 | 
        
           |  |  | 66 |             [
 | 
        
           |  |  | 67 |                 [
 | 
        
           |  |  | 68 |                     'courseid' => $course1->id,
 | 
        
           |  |  | 69 |                     'laststatus' => \backup_cron_automated_helper::BACKUP_STATUS_NOTYETRUN,
 | 
        
           |  |  | 70 |                     'nextstarttime' => time() - 10,
 | 
        
           |  |  | 71 |                 ],
 | 
        
           |  |  | 72 |                 [
 | 
        
           |  |  | 73 |                     'courseid' => $course2->id,
 | 
        
           |  |  | 74 |                     'laststatus' => \backup_cron_automated_helper::BACKUP_STATUS_NOTYETRUN,
 | 
        
           |  |  | 75 |                     'nextstarttime' => time() - 10,
 | 
        
           |  |  | 76 |                 ],
 | 
        
           |  |  | 77 |             ],
 | 
        
           |  |  | 78 |         );
 | 
        
           |  |  | 79 |   | 
        
           |  |  | 80 |         // Verify that we don't have any running backup tasks.
 | 
        
           |  |  | 81 |         $this->assertEmpty(get_config('backup', 'backup_auto_adhoctasks'));
 | 
        
           |  |  | 82 |         $this->assertEmpty(get_config('backup', 'backup_auto_emailpending'));
 | 
        
           |  |  | 83 |   | 
        
           |  |  | 84 |         // Redirect messages to sink.
 | 
        
           |  |  | 85 |         $sink = $this->redirectMessages();
 | 
        
           |  |  | 86 |         // Trigger the automated backup scheduled task.
 | 
        
           |  |  | 87 |         $this->execute_task('\core\task\automated_backup_task');
 | 
        
           |  |  | 88 |         $messages = $sink->get_messages();
 | 
        
           |  |  | 89 |         $sink->close();
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 |         // Scheduled task should not send report yet, because there are still running backup tasks.
 | 
        
           |  |  | 92 |         $this->assertCount(0, $messages);
 | 
        
           |  |  | 93 |   | 
        
           |  |  | 94 |         // Check that the backup tasks have been created.
 | 
        
           |  |  | 95 |         $this->assertTrue($DB->record_exists('backup_courses', ['courseid' => $course1->id]));
 | 
        
           |  |  | 96 |         $this->assertTrue($DB->record_exists('backup_courses', ['courseid' => $course2->id]));
 | 
        
           |  |  | 97 |         $this->assertNotEmpty(get_config('backup', 'backup_auto_adhoctasks'));
 | 
        
           |  |  | 98 |         $this->assertEquals(1, get_config('backup', 'backup_auto_emailpending'));
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 |         // Redirect messages to sink and stop buffer output from CLI task.
 | 
        
           |  |  | 101 |         $sink = $this->redirectMessages();
 | 
        
           |  |  | 102 |         // Trigger the automated backup report scheduled task.
 | 
        
           |  |  | 103 |         $this->execute_task('\core\task\automated_backup_report_task');
 | 
        
           |  |  | 104 |         $messages = $sink->get_messages();
 | 
        
           |  |  | 105 |         $sink->close();
 | 
        
           |  |  | 106 |   | 
        
           |  |  | 107 |         // Scheduled task should not send report yet, because there are still running backup tasks.
 | 
        
           |  |  | 108 |         $this->assertCount(0, $messages);
 | 
        
           |  |  | 109 |   | 
        
           |  |  | 110 |         // Execute only one ad-hoc backup task.
 | 
        
           |  |  | 111 |         $value = get_config('backup', 'backup_auto_adhoctasks');
 | 
        
           |  |  | 112 |         $queuedtasks = explode(',', $value);
 | 
        
           |  |  | 113 |         $task = manager::get_adhoc_task($queuedtasks[0]);
 | 
        
           |  |  | 114 |         $this->start_output_buffering();
 | 
        
           |  |  | 115 |         $task->execute();
 | 
        
           |  |  | 116 |         $this->stop_output_buffering();
 | 
        
           |  |  | 117 |         manager::adhoc_task_complete($task);
 | 
        
           |  |  | 118 |   | 
        
           |  |  | 119 |         // Redirect messages to sink and stop buffer output from CLI task.
 | 
        
           |  |  | 120 |         $sink = $this->redirectMessages();
 | 
        
           |  |  | 121 |         // Trigger the automated backup report scheduled task.
 | 
        
           |  |  | 122 |         $this->execute_task('\core\task\automated_backup_report_task');
 | 
        
           |  |  | 123 |         $messages = $sink->get_messages();
 | 
        
           |  |  | 124 |         $sink->close();
 | 
        
           |  |  | 125 |   | 
        
           |  |  | 126 |         // Scheduled task should not send report yet, because there are still running backup tasks.
 | 
        
           |  |  | 127 |         $this->assertCount(0, $messages);
 | 
        
           |  |  | 128 |   | 
        
           |  |  | 129 |         // Execute the remaining ad-hoc backup task.
 | 
        
           |  |  | 130 |         $this->start_output_buffering();
 | 
        
           |  |  | 131 |         $this->runAdhocTasks('\core\task\course_backup_task');
 | 
        
           |  |  | 132 |         $this->stop_output_buffering();
 | 
        
           |  |  | 133 |   | 
        
           |  |  | 134 |         // Redirect messages to sink.
 | 
        
           |  |  | 135 |         $sink = $this->redirectMessages();
 | 
        
           |  |  | 136 |         // Trigger the automated backup report scheduled task.
 | 
        
           |  |  | 137 |         $this->execute_task('\core\task\automated_backup_report_task');
 | 
        
           |  |  | 138 |         $messages = $sink->get_messages();
 | 
        
           |  |  | 139 |         $sink->close();
 | 
        
           |  |  | 140 |   | 
        
           |  |  | 141 |         // Verify that all the backup tasks have been completed and all the configs have been cleared.
 | 
        
           |  |  | 142 |         $this->assertEmpty(get_config('backup', 'backup_auto_adhoctasks'));
 | 
        
           |  |  | 143 |         $this->assertEmpty(get_config('backup', 'backup_auto_emailpending'));
 | 
        
           |  |  | 144 |         // Verify that the report has been sent.
 | 
        
           |  |  | 145 |         $this->assertCount(1, $messages);
 | 
        
           |  |  | 146 |         $message = reset($messages);
 | 
        
           |  |  | 147 |         $this->assertEquals(get_admin()->id, $message->useridto);
 | 
        
           |  |  | 148 |         $this->assertEquals('backup', $message->eventtype);
 | 
        
           |  |  | 149 |     }
 | 
        
           |  |  | 150 | }
 |