2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Unit tests for those parts of adminlib.php that implement the admin tree
23 * @copyright 2013 David Mudrak <david@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->libdir.'/adminlib.php');
33 * Provides the unit tests for admin tree functionality.
35 class core_admintree_testcase extends advanced_testcase {
38 * Adding nodes into the admin tree.
40 public function test_add_nodes() {
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'));
48 // Check the order of nodes in the root.
50 foreach ($tree->children as $child) {
51 $map[] = $child->name;
53 $this->assertEquals(array('one', 'three'), $map);
55 // Insert a node into the middle.
56 $tree->add('root', new admin_category('two', 'Two'), 'three');
58 foreach ($tree->children as $child) {
59 $map[] = $child->name;
61 $this->assertEquals(array('one', 'two', 'three'), $map);
63 // Non-existing sibling.
64 $tree->add('root', new admin_category('four', 'Four'), 'five');
65 $this->assertDebuggingCalled('Sibling five not found', DEBUG_DEVELOPER);
67 $tree->add('root', new admin_category('five', 'Five'));
69 foreach ($tree->children as $child) {
70 $map[] = $child->name;
72 $this->assertEquals(array('one', 'two', 'three', 'four', 'five'), $map);
74 // Insert a node into the middle of the subcategory.
75 $tree->add('one', new admin_category('one-two', 'One-two'), 'one-three');
77 foreach ($one->children as $child) {
78 $map[] = $child->name;
80 $this->assertEquals(array('one-one', 'one-two', 'one-three'), $map);
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);
86 $tree->add('root', new admin_category('six', 'Six'), 'one-two');
87 $this->assertDebuggingCalled('Sibling one-two not found', DEBUG_DEVELOPER);
89 // Me! Me! I wanna be first!
90 $tree->add('root', new admin_externalpage('zero', 'Zero', 'http://foo.bar'), 'one');
92 foreach ($tree->children as $child) {
93 $map[] = $child->name;
95 $this->assertEquals(array('zero', 'one', 'two', 'three', 'four', 'five', 'six'), $map);
99 * @expectedException coding_exception
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'));
107 * @expectedException coding_exception
109 public function test_add_nodes_before_invalid2() {
110 $tree = new admin_root(true);
111 $tree->add('root', new admin_category('bar', 'Bar'), '');
115 * Testing whether a configexecutable setting is executable.
117 public function test_admin_setting_configexecutable() {
119 $this->resetAfterTest();
121 $CFG->theme = 'clean';
122 $executable = new admin_setting_configexecutable('test1', 'Text 1', 'Help Path', '');
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);
128 // Check for a directory.
129 $result = $executable->output_html($CFG->dirroot);
130 $this->assertRegexp('/class="patherror"/', $result);
132 // Check for a file which is not executable.
133 $result = $executable->output_html($CFG->dirroot . '/filter/tex/readme_moodle.txt');
134 $this->assertRegexp('/class="patherror"/', $result);
136 // Check for an executable file.
137 if ($CFG->ostype == 'WINDOWS') {
138 $filetocheck = 'mimetex.exe';
140 $filetocheck = 'mimetex.darwin';
142 $result = $executable->output_html($CFG->dirroot . '/filter/tex/' . $filetocheck);
143 $this->assertRegexp('/class="pathok"/', $result);
145 // Check for no file specified.
146 $result = $executable->output_html('');
147 $this->assertRegexp('/name="s__test1"/', $result);
148 $this->assertRegexp('/value=""/', $result);
154 public function test_config_logging() {
156 $this->resetAfterTest();
158 $DB->delete_records('config_log', array());
160 $adminroot = new admin_root(true);
161 $adminroot->add('root', $one = new admin_category('one', 'One'));
162 $page = new admin_settingpage('page', 'Page');
163 $page->add(new admin_setting_configtext('text1', 'Text 1', '', ''));
164 $page->add(new admin_setting_configpasswordunmask('pass1', 'Password 1', '', ''));
165 $adminroot->add('one', $page);
167 $this->assertEmpty($DB->get_records('config_log'));
168 $data = array('s__text1'=>'sometext', 's__pass1'=>'');
169 $count = $this->save_config_data($adminroot, $data);
171 $this->assertEquals(2, $count);
172 $records = $DB->get_records('config_log', array(), 'id asc');
173 $this->assertCount(2, $records);
175 $record = array_shift($records);
176 $this->assertNull($record->plugin);
177 $this->assertSame('text1', $record->name);
178 $this->assertNull($record->oldvalue);
179 $this->assertSame('sometext', $record->value);
180 $record = array_shift($records);
181 $this->assertNull($record->plugin);
182 $this->assertSame('pass1', $record->name);
183 $this->assertNull($record->oldvalue);
184 $this->assertSame('', $record->value);
186 $DB->delete_records('config_log', array());
187 $data = array('s__text1'=>'other', 's__pass1'=>'nice password');
188 $count = $this->save_config_data($adminroot, $data);
190 $this->assertEquals(2, $count);
191 $records = $DB->get_records('config_log', array(), 'id asc');
192 $this->assertCount(2, $records);
194 $record = array_shift($records);
195 $this->assertNull($record->plugin);
196 $this->assertSame('text1', $record->name);
197 $this->assertSame('sometext', $record->oldvalue);
198 $this->assertSame('other', $record->value);
199 $record = array_shift($records);
200 $this->assertNull($record->plugin);
201 $this->assertSame('pass1', $record->name);
202 $this->assertSame('', $record->oldvalue);
203 $this->assertSame('********', $record->value);
205 $DB->delete_records('config_log', array());
206 $data = array('s__text1'=>'', 's__pass1'=>'');
207 $count = $this->save_config_data($adminroot, $data);
209 $this->assertEquals(2, $count);
210 $records = $DB->get_records('config_log', array(), 'id asc');
211 $this->assertCount(2, $records);
213 $record = array_shift($records);
214 $this->assertNull($record->plugin);
215 $this->assertSame('text1', $record->name);
216 $this->assertSame('other', $record->oldvalue);
217 $this->assertSame('', $record->value);
218 $record = array_shift($records);
219 $this->assertNull($record->plugin);
220 $this->assertSame('pass1', $record->name);
221 $this->assertSame('********', $record->oldvalue);
222 $this->assertSame('', $record->value);
225 protected function save_config_data(admin_root $adminroot, array $data) {
226 $adminroot->errors = array();
228 $settings = admin_find_write_settings($adminroot, $data);
231 foreach ($settings as $fullname=>$setting) {
232 /** @var $setting admin_setting */
233 $original = $setting->get_setting();
234 $error = $setting->write_setting($data[$fullname]);
236 $adminroot->errors[$fullname] = new stdClass();
237 $adminroot->errors[$fullname]->data = $data[$fullname];
238 $adminroot->errors[$fullname]->id = $setting->get_id();
239 $adminroot->errors[$fullname]->error = $error;
241 $setting->write_setting_flags($data);
243 if ($setting->post_write_settings($original)) {
251 public function test_preventexecpath() {
252 $this->resetAfterTest();
254 set_config('preventexecpath', 0);
255 set_config('execpath', null, 'abc_cde');
256 $this->assertFalse(get_config('abc_cde', 'execpath'));
257 $setting = new admin_setting_configexecutable('abc_cde/execpath', 'some desc', '', '/xx/yy');
258 $setting->write_setting('/oo/pp');
259 $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath'));
262 set_config('preventexecpath', 1);
263 $setting->write_setting('/mm/nn');
264 $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath'));
266 // Use default in install.
267 set_config('execpath', null, 'abc_cde');
268 $setting->write_setting('/mm/nn');
269 $this->assertSame('/xx/yy', get_config('abc_cde', 'execpath'));
271 // Use empty value if no default.
272 $setting = new admin_setting_configexecutable('abc_cde/execpath', 'some desc', '', null);
273 set_config('execpath', null, 'abc_cde');
274 $setting->write_setting('/mm/nn');
275 $this->assertSame('', get_config('abc_cde', 'execpath'));
277 // This also affects admin_setting_configfile and admin_setting_configdirectory.
279 set_config('preventexecpath', 0);
280 set_config('execpath', null, 'abc_cde');
281 $this->assertFalse(get_config('abc_cde', 'execpath'));
282 $setting = new admin_setting_configfile('abc_cde/execpath', 'some desc', '', '/xx/yy');
283 $setting->write_setting('/oo/pp');
284 $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath'));
287 set_config('preventexecpath', 1);
288 $setting->write_setting('/mm/nn');
289 $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath'));
291 // Use default in install.
292 set_config('execpath', null, 'abc_cde');
293 $setting->write_setting('/mm/nn');
294 $this->assertSame('/xx/yy', get_config('abc_cde', 'execpath'));
296 // Use empty value if no default.
297 $setting = new admin_setting_configfile('abc_cde/execpath', 'some desc', '', null);
298 set_config('execpath', null, 'abc_cde');
299 $setting->write_setting('/mm/nn');
300 $this->assertSame('', get_config('abc_cde', 'execpath'));
302 set_config('preventexecpath', 0);
303 set_config('execpath', null, 'abc_cde');
304 $this->assertFalse(get_config('abc_cde', 'execpath'));
305 $setting = new admin_setting_configdirectory('abc_cde/execpath', 'some desc', '', '/xx/yy');
306 $setting->write_setting('/oo/pp');
307 $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath'));
310 set_config('preventexecpath', 1);
311 $setting->write_setting('/mm/nn');
312 $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath'));
314 // Use default in install.
315 set_config('execpath', null, 'abc_cde');
316 $setting->write_setting('/mm/nn');
317 $this->assertSame('/xx/yy', get_config('abc_cde', 'execpath'));
319 // Use empty value if no default.
320 $setting = new admin_setting_configdirectory('abc_cde/execpath', 'some desc', '', null);
321 set_config('execpath', null, 'abc_cde');
322 $setting->write_setting('/mm/nn');
323 $this->assertSame('', get_config('abc_cde', 'execpath'));