MDL-42084 Unit tests: Remove unnecessary 'clear time limit change' lines
[moodle.git] / backup / util / plan / tests / step_test.php
CommitLineData
dbf5a447
PS
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/**
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
22 */
23
24defined('MOODLE_INTERNAL') || die();
25
4ac88d6d 26require_once(__DIR__.'/fixtures/plan_fixtures.php');
dbf5a447
PS
27
28
29/*
30 * step tests (all)
31 */
4b027431 32class backup_step_testcase extends advanced_testcase {
dbf5a447
PS
33
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
38
1cbf2a20 39 protected function setUp() {
dbf5a447
PS
40 global $DB, $CFG;
41 parent::setUp();
42
43 $this->resetAfterTest(true);
44
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));
48
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
53
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;
59 }
60
61 /**
62 * test base_step class
63 */
64 function test_base_step() {
65
4ac88d6d
PS
66 $bp = new mock_base_plan('planname'); // We need one plan
67 $bt = new mock_base_task('taskname', $bp); // We need one task
dbf5a447
PS
68 // Instantiate
69 $bs = new mock_base_step('stepname', $bt);
70 $this->assertTrue($bs instanceof base_step);
71 $this->assertEquals($bs->get_name(), 'stepname');
72 }
73
74 /**
75 * test backup_step class
76 */
77 function test_backup_step() {
78
79 // We need one (non interactive) controller for instatiating plan
80 $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
81 backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
82 // We need one plan
83 $bp = new backup_plan($bc);
84 // We need one task
4ac88d6d 85 $bt = new mock_backup_task('taskname', $bp);
dbf5a447
PS
86 // Instantiate step
87 $bs = new mock_backup_step('stepname', $bt);
88 $this->assertTrue($bs instanceof backup_step);
89 $this->assertEquals($bs->get_name(), 'stepname');
90
91 }
92
93 /**
94 * test backup_structure_step class
95 */
96 function test_backup_structure_step() {
97 global $CFG;
98
99 $file = $CFG->tempdir . '/test/test_backup_structure_step.txt';
100 // Remove the test dir and any content
101 @remove_dir(dirname($file));
102 // Recreate test dir
103 if (!check_dir_exists(dirname($file), true, true)) {
104 throw new moodle_exception('error_creating_temp_dir', 'error', dirname($file));
105 }
106
107 // We need one (non interactive) controller for instatiating plan
108 $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
109 backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
110 // We need one plan
111 $bp = new backup_plan($bc);
112 // We need one task with mocked basepath
113 $bt = new mock_backup_task_basepath('taskname');
114 $bp->add_task($bt);
115 // Instantiate backup_structure_step (and add it to task)
116 $bs = new mock_backup_structure_step('steptest', basename($file), $bt);
117 // Execute backup_structure_step
118 $bs->execute();
119
120 // Test file has been created
121 $this->assertTrue(file_exists($file));
122
123 // Some simple tests with contents
124 $contents = file_get_contents($file);
125 $this->assertTrue(strpos($contents, '<?xml version="1.0"') !== false);
126 $this->assertTrue(strpos($contents, '<test id="1">') !== false);
127 $this->assertTrue(strpos($contents, '<field1>value1</field1>') !== false);
128 $this->assertTrue(strpos($contents, '<field2>value2</field2>') !== false);
129 $this->assertTrue(strpos($contents, '</test>') !== false);
130
131 unlink($file); // delete file
132
133 // Remove the test dir and any content
134 @remove_dir(dirname($file));
135 }
136
137 /**
138 * wrong base_step class tests
139 */
140 function test_base_step_wrong() {
141
142 // Try to pass one wrong task
143 try {
144 $bt = new mock_base_step('teststep', new stdclass());
145 $this->assertTrue(false, 'base_step_exception expected');
146 } catch (exception $e) {
147 $this->assertTrue($e instanceof base_step_exception);
148 $this->assertEquals($e->errorcode, 'wrong_base_task_specified');
149 }
150 }
151
152 /**
153 * wrong backup_step class tests
154 */
155 function test_backup_test_wrong() {
156
157 // Try to pass one wrong task
158 try {
159 $bt = new mock_backup_step('teststep', new stdclass());
160 $this->assertTrue(false, 'backup_step_exception expected');
161 } catch (exception $e) {
162 $this->assertTrue($e instanceof backup_step_exception);
163 $this->assertEquals($e->errorcode, 'wrong_backup_task_specified');
164 }
165 }
166}