Merge branch 'MDL-72887-M311' of https://github.com/michael-milette/moodle into MOODL...
[moodle.git] / backup / tests / async_backup_test.php
blob509c83cf25f9600703d9b08217f6e177fef3e2ac
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 /**
18 * Asyncronhous backup tests.
20 * @package core_backup
21 * @copyright 2018 Matt Porritt <mattp@catalyst-au.net>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
29 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
30 require_once($CFG->libdir . '/completionlib.php');
32 /**
33 * Asyncronhous backup tests.
35 * @copyright 2018 Matt Porritt <mattp@catalyst-au.net>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class core_backup_async_backup_testcase extends \core_privacy\tests\provider_testcase {
40 /**
41 * Tests the asynchronous backup.
43 public function test_async_backup() {
44 global $DB, $CFG, $USER;
46 $this->resetAfterTest(true);
47 $this->setAdminUser();
48 $CFG->enableavailability = true;
49 $CFG->enablecompletion = true;
51 // Create a course with some availability data set.
52 $generator = $this->getDataGenerator();
53 $course = $generator->create_course(
54 array('format' => 'topics', 'numsections' => 3,
55 'enablecompletion' => COMPLETION_ENABLED),
56 array('createsections' => true));
57 $forum = $generator->create_module('forum', array(
58 'course' => $course->id));
59 $forum2 = $generator->create_module('forum', array(
60 'course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL));
62 // We need a grade, easiest is to add an assignment.
63 $assignrow = $generator->create_module('assign', array(
64 'course' => $course->id));
65 $assign = new assign(context_module::instance($assignrow->cmid), false, false);
66 $item = $assign->get_grade_item();
68 // Make a test grouping as well.
69 $grouping = $generator->create_grouping(array('courseid' => $course->id,
70 'name' => 'Grouping!'));
72 $availability = '{"op":"|","show":false,"c":[' .
73 '{"type":"completion","cm":' . $forum2->cmid .',"e":1},' .
74 '{"type":"grade","id":' . $item->id . ',"min":4,"max":94},' .
75 '{"type":"grouping","id":' . $grouping->id . '}' .
76 ']}';
77 $DB->set_field('course_modules', 'availability', $availability, array(
78 'id' => $forum->cmid));
79 $DB->set_field('course_sections', 'availability', $availability, array(
80 'course' => $course->id, 'section' => 1));
82 // Start backup process.
84 // Make the backup controller for an async backup.
85 $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
86 backup::INTERACTIVE_YES, backup::MODE_ASYNC, $USER->id);
87 $bc->finish_ui();
88 $backupid = $bc->get_backupid();
89 $bc->destroy();
91 $prebackuprec = $DB->get_record('backup_controllers', array('backupid' => $backupid));
93 // Check the initial backup controller was created correctly.
94 $this->assertEquals(backup::STATUS_AWAITING, $prebackuprec->status);
95 $this->assertEquals(2, $prebackuprec->execution);
97 // Create the adhoc task.
98 $asynctask = new \core\task\asynchronous_backup_task();
99 $asynctask->set_blocking(false);
100 $asynctask->set_custom_data(array('backupid' => $backupid));
101 \core\task\manager::queue_adhoc_task($asynctask);
103 // We are expecting trace output during this test.
104 $this->expectOutputRegex("/$backupid/");
106 // Execute adhoc task.
107 $now = time();
108 $task = \core\task\manager::get_next_adhoc_task($now);
109 $this->assertInstanceOf('\\core\\task\\asynchronous_backup_task', $task);
110 $task->execute();
111 \core\task\manager::adhoc_task_complete($task);
113 $postbackuprec = $DB->get_record('backup_controllers', array('backupid' => $backupid));
115 // Check backup was created successfully.
116 $this->assertEquals(backup::STATUS_FINISHED_OK, $postbackuprec->status);
117 $this->assertEquals(1.0, $postbackuprec->progress);