Commit | Line | Data |
---|---|---|
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 | ||
27 | defined('MOODLE_INTERNAL') || die(); | |
28 | ||
29 | global $CFG; | |
30 | require_once($CFG->libdir.'/adminlib.php'); | |
31 | ||
32 | /** | |
33 | * Provides the unit tests for admin tree functionality. | |
34 | */ | |
05fb92e9 | 35 | class 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 | ||
121 | $executable = new admin_setting_configexecutable('test1', 'Text 1', 'Help Path', ''); | |
122 | ||
123 | // Check for an invalid path. | |
124 | $result = $executable->output_html($CFG->dirroot . '/lib/tests/other/file_does_not_exist'); | |
125 | $this->assertRegexp('/class="patherror"/', $result); | |
126 | ||
127 | // Check for a directory. | |
128 | $result = $executable->output_html($CFG->dirroot); | |
129 | $this->assertRegexp('/class="patherror"/', $result); | |
130 | ||
131 | // Check for a file which is not executable. | |
f500ff4e | 132 | $result = $executable->output_html($CFG->dirroot . '/filter/tex/readme_moodle.txt'); |
9cd7bb37 AN |
133 | $this->assertRegexp('/class="patherror"/', $result); |
134 | ||
135 | // Check for an executable file. | |
f500ff4e EL |
136 | if ($CFG->ostype == 'WINDOWS') { |
137 | $filetocheck = 'mimetex.exe'; | |
138 | } else { | |
139 | $filetocheck = 'mimetex.darwin'; | |
140 | } | |
141 | $result = $executable->output_html($CFG->dirroot . '/filter/tex/' . $filetocheck); | |
9cd7bb37 AN |
142 | $this->assertRegexp('/class="pathok"/', $result); |
143 | ||
144 | // Check for no file specified. | |
145 | $result = $executable->output_html(''); | |
146 | $this->assertRegexp('/name="s__test1" value=""/', $result); | |
147 | } | |
148 | ||
914499a3 PS |
149 | /** |
150 | * Saving of values. | |
151 | */ | |
152 | public function test_config_logging() { | |
153 | global $DB; | |
154 | $this->resetAfterTest(); | |
155 | ||
156 | $DB->delete_records('config_log', array()); | |
157 | ||
158 | $adminroot = new admin_root(true); | |
159 | $adminroot->add('root', $one = new admin_category('one', 'One')); | |
160 | $page = new admin_settingpage('page', 'Page'); | |
161 | $page->add(new admin_setting_configtext('text1', 'Text 1', '', '')); | |
162 | $page->add(new admin_setting_configpasswordunmask('pass1', 'Password 1', '', '')); | |
163 | $adminroot->add('one', $page); | |
164 | ||
165 | $this->assertEmpty($DB->get_records('config_log')); | |
166 | $data = array('s__text1'=>'sometext', 's__pass1'=>''); | |
167 | $count = $this->save_config_data($adminroot, $data); | |
168 | ||
169 | $this->assertEquals(2, $count); | |
170 | $records = $DB->get_records('config_log', array(), 'id asc'); | |
171 | $this->assertCount(2, $records); | |
172 | reset($records); | |
173 | $record = array_shift($records); | |
174 | $this->assertNull($record->plugin); | |
175 | $this->assertSame('text1', $record->name); | |
176 | $this->assertNull($record->oldvalue); | |
177 | $this->assertSame('sometext', $record->value); | |
178 | $record = array_shift($records); | |
179 | $this->assertNull($record->plugin); | |
180 | $this->assertSame('pass1', $record->name); | |
181 | $this->assertNull($record->oldvalue); | |
182 | $this->assertSame('', $record->value); | |
183 | ||
184 | $DB->delete_records('config_log', array()); | |
185 | $data = array('s__text1'=>'other', 's__pass1'=>'nice password'); | |
186 | $count = $this->save_config_data($adminroot, $data); | |
187 | ||
188 | $this->assertEquals(2, $count); | |
189 | $records = $DB->get_records('config_log', array(), 'id asc'); | |
190 | $this->assertCount(2, $records); | |
191 | reset($records); | |
192 | $record = array_shift($records); | |
193 | $this->assertNull($record->plugin); | |
194 | $this->assertSame('text1', $record->name); | |
195 | $this->assertSame('sometext', $record->oldvalue); | |
196 | $this->assertSame('other', $record->value); | |
197 | $record = array_shift($records); | |
198 | $this->assertNull($record->plugin); | |
199 | $this->assertSame('pass1', $record->name); | |
200 | $this->assertSame('', $record->oldvalue); | |
201 | $this->assertSame('********', $record->value); | |
202 | ||
203 | $DB->delete_records('config_log', array()); | |
204 | $data = array('s__text1'=>'', 's__pass1'=>''); | |
205 | $count = $this->save_config_data($adminroot, $data); | |
206 | ||
207 | $this->assertEquals(2, $count); | |
208 | $records = $DB->get_records('config_log', array(), 'id asc'); | |
209 | $this->assertCount(2, $records); | |
210 | reset($records); | |
211 | $record = array_shift($records); | |
212 | $this->assertNull($record->plugin); | |
213 | $this->assertSame('text1', $record->name); | |
214 | $this->assertSame('other', $record->oldvalue); | |
215 | $this->assertSame('', $record->value); | |
216 | $record = array_shift($records); | |
217 | $this->assertNull($record->plugin); | |
218 | $this->assertSame('pass1', $record->name); | |
219 | $this->assertSame('********', $record->oldvalue); | |
220 | $this->assertSame('', $record->value); | |
221 | } | |
222 | ||
223 | protected function save_config_data(admin_root $adminroot, array $data) { | |
224 | $adminroot->errors = array(); | |
225 | ||
226 | $settings = admin_find_write_settings($adminroot, $data); | |
227 | ||
228 | $count = 0; | |
229 | foreach ($settings as $fullname=>$setting) { | |
230 | /** @var $setting admin_setting */ | |
231 | $original = $setting->get_setting(); | |
232 | $error = $setting->write_setting($data[$fullname]); | |
233 | if ($error !== '') { | |
234 | $adminroot->errors[$fullname] = new stdClass(); | |
235 | $adminroot->errors[$fullname]->data = $data[$fullname]; | |
236 | $adminroot->errors[$fullname]->id = $setting->get_id(); | |
237 | $adminroot->errors[$fullname]->error = $error; | |
238 | } else { | |
239 | $setting->write_setting_flags($data); | |
240 | } | |
241 | if ($setting->post_write_settings($original)) { | |
242 | $count++; | |
243 | } | |
244 | } | |
245 | ||
246 | return $count; | |
247 | } | |
77043fd6 | 248 | } |