[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/tests/ -> admintree_test.php (source)

   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   * Unit tests for those parts of adminlib.php that implement the admin tree
  19   * functionality.
  20   *
  21   * @package     core
  22   * @category    phpunit
  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   */
  35  class core_admintree_testcase extends advanced_testcase {
  36  
  37      /**
  38       * Adding nodes into the admin tree.
  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  
  74          // Insert a node into the middle of the subcategory.
  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      }
 113  
 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.
 132          $result = $executable->output_html($CFG->dirroot . '/filter/tex/readme_moodle.txt');
 133          $this->assertRegexp('/class="patherror"/', $result);
 134  
 135          // Check for an executable file.
 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);
 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  
 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      }
 248  
 249      public function test_preventexecpath() {
 250          $this->resetAfterTest();
 251  
 252          set_config('preventexecpath', 0);
 253          set_config('execpath', null, 'abc_cde');
 254          $this->assertFalse(get_config('abc_cde', 'execpath'));
 255          $setting = new admin_setting_configexecutable('abc_cde/execpath', 'some desc', '', '/xx/yy');
 256          $setting->write_setting('/oo/pp');
 257          $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath'));
 258  
 259          // Prevent changes.
 260          set_config('preventexecpath', 1);
 261          $setting->write_setting('/mm/nn');
 262          $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath'));
 263  
 264          // Use default in install.
 265          set_config('execpath', null, 'abc_cde');
 266          $setting->write_setting('/mm/nn');
 267          $this->assertSame('/xx/yy', get_config('abc_cde', 'execpath'));
 268  
 269          // Use empty value if no default.
 270          $setting = new admin_setting_configexecutable('abc_cde/execpath', 'some desc', '', null);
 271          set_config('execpath', null, 'abc_cde');
 272          $setting->write_setting('/mm/nn');
 273          $this->assertSame('', get_config('abc_cde', 'execpath'));
 274  
 275          // This also affects admin_setting_configfile and admin_setting_configdirectory.
 276  
 277          set_config('preventexecpath', 0);
 278          set_config('execpath', null, 'abc_cde');
 279          $this->assertFalse(get_config('abc_cde', 'execpath'));
 280          $setting = new admin_setting_configfile('abc_cde/execpath', 'some desc', '', '/xx/yy');
 281          $setting->write_setting('/oo/pp');
 282          $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath'));
 283  
 284          // Prevent changes.
 285          set_config('preventexecpath', 1);
 286          $setting->write_setting('/mm/nn');
 287          $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath'));
 288  
 289          // Use default in install.
 290          set_config('execpath', null, 'abc_cde');
 291          $setting->write_setting('/mm/nn');
 292          $this->assertSame('/xx/yy', get_config('abc_cde', 'execpath'));
 293  
 294          // Use empty value if no default.
 295          $setting = new admin_setting_configfile('abc_cde/execpath', 'some desc', '', null);
 296          set_config('execpath', null, 'abc_cde');
 297          $setting->write_setting('/mm/nn');
 298          $this->assertSame('', get_config('abc_cde', 'execpath'));
 299  
 300          set_config('preventexecpath', 0);
 301          set_config('execpath', null, 'abc_cde');
 302          $this->assertFalse(get_config('abc_cde', 'execpath'));
 303          $setting = new admin_setting_configdirectory('abc_cde/execpath', 'some desc', '', '/xx/yy');
 304          $setting->write_setting('/oo/pp');
 305          $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath'));
 306  
 307          // Prevent changes.
 308          set_config('preventexecpath', 1);
 309          $setting->write_setting('/mm/nn');
 310          $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath'));
 311  
 312          // Use default in install.
 313          set_config('execpath', null, 'abc_cde');
 314          $setting->write_setting('/mm/nn');
 315          $this->assertSame('/xx/yy', get_config('abc_cde', 'execpath'));
 316  
 317          // Use empty value if no default.
 318          $setting = new admin_setting_configdirectory('abc_cde/execpath', 'some desc', '', null);
 319          set_config('execpath', null, 'abc_cde');
 320          $setting->write_setting('/mm/nn');
 321          $this->assertSame('', get_config('abc_cde', 'execpath'));
 322      }
 323  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1