MDL-67673 phpunit: Fix the return type of template methods
[moodle.git] / backup / util / plan / tests / task_test.php
blob3cb0bef732f789046bbe1d4d3eecf7b0d67ce897
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 * @package core_backup
19 * @category phpunit
20 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 defined('MOODLE_INTERNAL') || die();
26 require_once(__DIR__.'/fixtures/plan_fixtures.php');
29 /**
30 * task tests (all)
32 class backup_task_testcase extends advanced_testcase {
34 protected $moduleid; // course_modules id used for testing
35 protected $sectionid; // course_sections id used for testing
36 protected $courseid; // course id used for testing
37 protected $userid; // user record used for testing
39 protected function setUp(): void {
40 global $DB, $CFG;
41 parent::setUp();
43 $this->resetAfterTest(true);
45 $course = $this->getDataGenerator()->create_course();
46 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3));
47 $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid));
49 $this->moduleid = $coursemodule->id;
50 $this->sectionid = $DB->get_field("course_sections", 'id', array("section"=>$coursemodule->section, "course"=>$course->id));
51 $this->courseid = $coursemodule->course;
52 $this->userid = 2; // admin
54 // Disable all loggers
55 $CFG->backup_error_log_logger_level = backup::LOG_NONE;
56 $CFG->backup_file_logger_level = backup::LOG_NONE;
57 $CFG->backup_database_logger_level = backup::LOG_NONE;
58 $CFG->backup_file_logger_level_extra = backup::LOG_NONE;
61 /**
62 * test base_task class
64 function test_base_task() {
66 $bp = new mock_base_plan('planname'); // We need one plan
67 // Instantiate
68 $bt = new mock_base_task('taskname', $bp);
69 $this->assertTrue($bt instanceof base_task);
70 $this->assertEquals($bt->get_name(), 'taskname');
71 $this->assertTrue(is_array($bt->get_settings()));
72 $this->assertEquals(count($bt->get_settings()), 0);
73 $this->assertTrue(is_array($bt->get_steps()));
74 $this->assertEquals(count($bt->get_steps()), 0);
77 /**
78 * test backup_task class
80 function test_backup_task() {
82 // We need one (non interactive) controller for instatiating plan
83 $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
84 backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
85 // We need one plan
86 $bp = new backup_plan($bc);
87 // Instantiate task
88 $bt = new mock_backup_task('taskname', $bp);
89 $this->assertTrue($bt instanceof backup_task);
90 $this->assertEquals($bt->get_name(), 'taskname');
92 // Calculate checksum and check it
93 $checksum = $bt->calculate_checksum();
94 $this->assertTrue($bt->is_checksum_correct($checksum));
96 $bc->destroy();
99 /**
100 * wrong base_task class tests
102 function test_base_task_wrong() {
104 // Try to pass one wrong plan
105 try {
106 $bt = new mock_base_task('tasktest', new stdclass());
107 $this->assertTrue(false, 'base_task_exception expected');
108 } catch (exception $e) {
109 $this->assertTrue($e instanceof base_task_exception);
110 $this->assertEquals($e->errorcode, 'wrong_base_plan_specified');
113 // Add wrong step to task
114 $bp = new mock_base_plan('planname'); // We need one plan
115 // Instantiate
116 $bt = new mock_base_task('taskname', $bp);
117 try {
118 $bt->add_step(new stdclass());
119 $this->assertTrue(false, 'base_task_exception expected');
120 } catch (exception $e) {
121 $this->assertTrue($e instanceof base_task_exception);
122 $this->assertEquals($e->errorcode, 'wrong_base_step_specified');
128 * wrong backup_task class tests
130 function test_backup_task_wrong() {
132 // Try to pass one wrong plan
133 try {
134 $bt = new mock_backup_task('tasktest', new stdclass());
135 $this->assertTrue(false, 'backup_task_exception expected');
136 } catch (exception $e) {
137 $this->assertTrue($e instanceof backup_task_exception);
138 $this->assertEquals($e->errorcode, 'wrong_backup_plan_specified');