MDL-59409 admin: set admin user in unittest
[moodle.git] / lib / tests / admintree_test.php
CommitLineData
77043fd6 1<?php
77043fd6
DM
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 * Unit tests for those parts of adminlib.php that implement the admin tree
19 * functionality.
20 *
21 * @package core
05fb92e9 22 * @category phpunit
77043fd6
DM
23 * @copyright 2013 David Mudrak <david@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->libdir.'/adminlib.php');
31
32/**
33 * Provides the unit tests for admin tree functionality.
34 */
05fb92e9 35class core_admintree_testcase extends advanced_testcase {
77043fd6
DM
36
37 /**
05fb92e9 38 * Adding nodes into the admin tree.
77043fd6
DM
39 */
40 public function test_add_nodes() {
41
42 $tree = new admin_root(true);
43 $tree->add('root', $one = new admin_category('one', 'One'));
44 $tree->add('root', new admin_category('three', 'Three'));
45 $tree->add('one', new admin_category('one-one', 'One-one'));
46 $tree->add('one', new admin_category('one-three', 'One-three'));
47
48 // Check the order of nodes in the root.
49 $map = array();
50 foreach ($tree->children as $child) {
51 $map[] = $child->name;
52 }
53 $this->assertEquals(array('one', 'three'), $map);
54
55 // Insert a node into the middle.
56 $tree->add('root', new admin_category('two', 'Two'), 'three');
57 $map = array();
58 foreach ($tree->children as $child) {
59 $map[] = $child->name;
60 }
61 $this->assertEquals(array('one', 'two', 'three'), $map);
62
63 // Non-existing sibling.
64 $tree->add('root', new admin_category('four', 'Four'), 'five');
65 $this->assertDebuggingCalled('Sibling five not found', DEBUG_DEVELOPER);
66
67 $tree->add('root', new admin_category('five', 'Five'));
68 $map = array();
69 foreach ($tree->children as $child) {
70 $map[] = $child->name;
71 }
72 $this->assertEquals(array('one', 'two', 'three', 'four', 'five'), $map);
73
05fb92e9 74 // Insert a node into the middle of the subcategory.
77043fd6
DM
75 $tree->add('one', new admin_category('one-two', 'One-two'), 'one-three');
76 $map = array();
77 foreach ($one->children as $child) {
78 $map[] = $child->name;
79 }
80 $this->assertEquals(array('one-one', 'one-two', 'one-three'), $map);
81
82 // Check just siblings, not parents or children.
83 $tree->add('one', new admin_category('one-four', 'One-four'), 'one');
84 $this->assertDebuggingCalled('Sibling one not found', DEBUG_DEVELOPER);
85
86 $tree->add('root', new admin_category('six', 'Six'), 'one-two');
87 $this->assertDebuggingCalled('Sibling one-two not found', DEBUG_DEVELOPER);
88
89 // Me! Me! I wanna be first!
90 $tree->add('root', new admin_externalpage('zero', 'Zero', 'http://foo.bar'), 'one');
91 $map = array();
92 foreach ($tree->children as $child) {
93 $map[] = $child->name;
94 }
95 $this->assertEquals(array('zero', 'one', 'two', 'three', 'four', 'five', 'six'), $map);
96 }
97
98 /**
99 * @expectedException coding_exception
100 */
101 public function test_add_nodes_before_invalid1() {
102 $tree = new admin_root(true);
103 $tree->add('root', new admin_externalpage('foo', 'Foo', 'http://foo.bar'), array('moodle:site/config'));
104 }
105
106 /**
107 * @expectedException coding_exception
108 */
109 public function test_add_nodes_before_invalid2() {
110 $tree = new admin_root(true);
111 $tree->add('root', new admin_category('bar', 'Bar'), '');
112 }
914499a3 113
9cd7bb37
AN
114 /**
115 * Testing whether a configexecutable setting is executable.
116 */
117 public function test_admin_setting_configexecutable() {
118 global $CFG;
119 $this->resetAfterTest();
120
e56b58e3 121 $CFG->theme = 'clean';
9cd7bb37
AN
122 $executable = new admin_setting_configexecutable('test1', 'Text 1', 'Help Path', '');
123
124 // Check for an invalid path.
125 $result = $executable->output_html($CFG->dirroot . '/lib/tests/other/file_does_not_exist');
126 $this->assertRegexp('/class="patherror"/', $result);
127
128 // Check for a directory.
129 $result = $executable->output_html($CFG->dirroot);
130 $this->assertRegexp('/class="patherror"/', $result);
131
132 // Check for a file which is not executable.
f500ff4e 133 $result = $executable->output_html($CFG->dirroot . '/filter/tex/readme_moodle.txt');
9cd7bb37
AN
134 $this->assertRegexp('/class="patherror"/', $result);
135
136 // Check for an executable file.
f500ff4e
EL
137 if ($CFG->ostype == 'WINDOWS') {
138 $filetocheck = 'mimetex.exe';
139 } else {
140 $filetocheck = 'mimetex.darwin';
141 }
142 $result = $executable->output_html($CFG->dirroot . '/filter/tex/' . $filetocheck);
9cd7bb37
AN
143 $this->assertRegexp('/class="pathok"/', $result);
144
145 // Check for no file specified.
146 $result = $executable->output_html('');
890c2603
DW
147 $this->assertRegexp('/name="s__test1"/', $result);
148 $this->assertRegexp('/value=""/', $result);
9cd7bb37
AN
149 }
150
914499a3
PS
151 /**
152 * Saving of values.
153 */
154 public function test_config_logging() {
155 global $DB;
156 $this->resetAfterTest();
6e861be6 157 $this->setAdminUser();
914499a3
PS
158
159 $DB->delete_records('config_log', array());
160
161 $adminroot = new admin_root(true);
162 $adminroot->add('root', $one = new admin_category('one', 'One'));
163 $page = new admin_settingpage('page', 'Page');
164 $page->add(new admin_setting_configtext('text1', 'Text 1', '', ''));
165 $page->add(new admin_setting_configpasswordunmask('pass1', 'Password 1', '', ''));
166 $adminroot->add('one', $page);
167
168 $this->assertEmpty($DB->get_records('config_log'));
169 $data = array('s__text1'=>'sometext', 's__pass1'=>'');
170 $count = $this->save_config_data($adminroot, $data);
171
172 $this->assertEquals(2, $count);
173 $records = $DB->get_records('config_log', array(), 'id asc');
174 $this->assertCount(2, $records);
175 reset($records);
176 $record = array_shift($records);
177 $this->assertNull($record->plugin);
178 $this->assertSame('text1', $record->name);
179 $this->assertNull($record->oldvalue);
180 $this->assertSame('sometext', $record->value);
181 $record = array_shift($records);
182 $this->assertNull($record->plugin);
183 $this->assertSame('pass1', $record->name);
184 $this->assertNull($record->oldvalue);
185 $this->assertSame('', $record->value);
186
187 $DB->delete_records('config_log', array());
188 $data = array('s__text1'=>'other', 's__pass1'=>'nice password');
189 $count = $this->save_config_data($adminroot, $data);
190
191 $this->assertEquals(2, $count);
192 $records = $DB->get_records('config_log', array(), 'id asc');
193 $this->assertCount(2, $records);
194 reset($records);
195 $record = array_shift($records);
196 $this->assertNull($record->plugin);
197 $this->assertSame('text1', $record->name);
198 $this->assertSame('sometext', $record->oldvalue);
199 $this->assertSame('other', $record->value);
200 $record = array_shift($records);
201 $this->assertNull($record->plugin);
202 $this->assertSame('pass1', $record->name);
203 $this->assertSame('', $record->oldvalue);
204 $this->assertSame('********', $record->value);
205
206 $DB->delete_records('config_log', array());
207 $data = array('s__text1'=>'', 's__pass1'=>'');
208 $count = $this->save_config_data($adminroot, $data);
209
210 $this->assertEquals(2, $count);
211 $records = $DB->get_records('config_log', array(), 'id asc');
212 $this->assertCount(2, $records);
213 reset($records);
214 $record = array_shift($records);
215 $this->assertNull($record->plugin);
216 $this->assertSame('text1', $record->name);
217 $this->assertSame('other', $record->oldvalue);
218 $this->assertSame('', $record->value);
219 $record = array_shift($records);
220 $this->assertNull($record->plugin);
221 $this->assertSame('pass1', $record->name);
222 $this->assertSame('********', $record->oldvalue);
223 $this->assertSame('', $record->value);
224 }
225
226 protected function save_config_data(admin_root $adminroot, array $data) {
227 $adminroot->errors = array();
228
229 $settings = admin_find_write_settings($adminroot, $data);
230
231 $count = 0;
232 foreach ($settings as $fullname=>$setting) {
233 /** @var $setting admin_setting */
234 $original = $setting->get_setting();
235 $error = $setting->write_setting($data[$fullname]);
236 if ($error !== '') {
237 $adminroot->errors[$fullname] = new stdClass();
238 $adminroot->errors[$fullname]->data = $data[$fullname];
239 $adminroot->errors[$fullname]->id = $setting->get_id();
240 $adminroot->errors[$fullname]->error = $error;
241 } else {
242 $setting->write_setting_flags($data);
243 }
244 if ($setting->post_write_settings($original)) {
245 $count++;
246 }
247 }
248
249 return $count;
250 }
41186f4f
PS
251
252 public function test_preventexecpath() {
253 $this->resetAfterTest();
254
255 set_config('preventexecpath', 0);
256 set_config('execpath', null, 'abc_cde');
257 $this->assertFalse(get_config('abc_cde', 'execpath'));
258 $setting = new admin_setting_configexecutable('abc_cde/execpath', 'some desc', '', '/xx/yy');
259 $setting->write_setting('/oo/pp');
260 $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath'));
261
262 // Prevent changes.
263 set_config('preventexecpath', 1);
264 $setting->write_setting('/mm/nn');
265 $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath'));
266
267 // Use default in install.
268 set_config('execpath', null, 'abc_cde');
269 $setting->write_setting('/mm/nn');
270 $this->assertSame('/xx/yy', get_config('abc_cde', 'execpath'));
271
272 // Use empty value if no default.
273 $setting = new admin_setting_configexecutable('abc_cde/execpath', 'some desc', '', null);
274 set_config('execpath', null, 'abc_cde');
275 $setting->write_setting('/mm/nn');
276 $this->assertSame('', get_config('abc_cde', 'execpath'));
277
5aaac8b2 278 // This also affects admin_setting_configfile and admin_setting_configdirectory.
41186f4f
PS
279
280 set_config('preventexecpath', 0);
281 set_config('execpath', null, 'abc_cde');
282 $this->assertFalse(get_config('abc_cde', 'execpath'));
283 $setting = new admin_setting_configfile('abc_cde/execpath', 'some desc', '', '/xx/yy');
284 $setting->write_setting('/oo/pp');
285 $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath'));
286
287 // Prevent changes.
288 set_config('preventexecpath', 1);
289 $setting->write_setting('/mm/nn');
290 $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath'));
291
292 // Use default in install.
293 set_config('execpath', null, 'abc_cde');
294 $setting->write_setting('/mm/nn');
295 $this->assertSame('/xx/yy', get_config('abc_cde', 'execpath'));
296
297 // Use empty value if no default.
298 $setting = new admin_setting_configfile('abc_cde/execpath', 'some desc', '', null);
299 set_config('execpath', null, 'abc_cde');
300 $setting->write_setting('/mm/nn');
301 $this->assertSame('', get_config('abc_cde', 'execpath'));
302
5aaac8b2
PS
303 set_config('preventexecpath', 0);
304 set_config('execpath', null, 'abc_cde');
305 $this->assertFalse(get_config('abc_cde', 'execpath'));
306 $setting = new admin_setting_configdirectory('abc_cde/execpath', 'some desc', '', '/xx/yy');
307 $setting->write_setting('/oo/pp');
308 $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath'));
309
310 // Prevent changes.
311 set_config('preventexecpath', 1);
312 $setting->write_setting('/mm/nn');
313 $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath'));
314
315 // Use default in install.
316 set_config('execpath', null, 'abc_cde');
317 $setting->write_setting('/mm/nn');
318 $this->assertSame('/xx/yy', get_config('abc_cde', 'execpath'));
319
320 // Use empty value if no default.
321 $setting = new admin_setting_configdirectory('abc_cde/execpath', 'some desc', '', null);
322 set_config('execpath', null, 'abc_cde');
323 $setting->write_setting('/mm/nn');
324 $this->assertSame('', get_config('abc_cde', 'execpath'));
41186f4f 325 }
77043fd6 326}