Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   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  namespace core;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->libdir.'/adminlib.php');
  23  
  24  /**
  25   * Unit tests for parts of adminlib.php.
  26   *
  27   * @package    core
  28   * @copyright  2020 Brendan Heywood <brendan@catalyst-au.net>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class adminlib_test extends \advanced_testcase {
  32  
  33      /**
  34       * Data provider of serialized string.
  35       *
  36       * @return array
  37       */
  38      public function db_should_replace_dataprovider() {
  39          return [
  40              // Skipped tables.
  41              ['block_instances', '', false],
  42              ['config',          '', false],
  43              ['config_plugins',  '', false],
  44              ['config_log',      '', false],
  45              ['events_queue',    '', false],
  46              ['filter_config',   '', false],
  47              ['log',             '', false],
  48              ['repository_instance_config', '', false],
  49              ['sessions',        '', false],
  50              ['upgrade_log',     '', false],
  51  
  52              // Unknown skipped tables.
  53              ['foobar_log',      '', false],
  54              ['foobar_logs',     '', false],
  55  
  56              // Unknown ok tables.
  57              ['foobar_logical',  '', true],
  58  
  59              // Normal tables.
  60              ['assign',          '', true],
  61  
  62              // Normal tables with excluded columns.
  63              ['message_conversations', 'convhash',   false],
  64              ['user_password_history', 'hash',       false],
  65              ['foo',                   'barhash',    false],
  66          ];
  67      }
  68  
  69      /**
  70       * Test which tables and column should be replaced.
  71       *
  72       * @dataProvider db_should_replace_dataprovider
  73       * @covers ::db_should_replace
  74       * @param string $table name
  75       * @param string $column name
  76       * @param bool $expected whether it should be replaced
  77       */
  78      public function test_db_should_replace(string $table, string $column, bool $expected) {
  79          $actual = db_should_replace($table, $column);
  80          $this->assertSame($actual, $expected);
  81      }
  82  
  83      /**
  84       * Data provider for additional skip tables.
  85       *
  86       * @covers ::db_should_replace
  87       * @return array
  88       */
  89      public function db_should_replace_additional_skip_tables_dataprovider() {
  90          return [
  91              // Skipped tables.
  92              ['block_instances', '', false],
  93              ['config',          '', false],
  94              ['config_plugins',  '', false],
  95              ['config_log',      '', false],
  96              ['events_queue',    '', false],
  97              ['filter_config',   '', false],
  98              ['log',             '', false],
  99              ['repository_instance_config', '', false],
 100              ['sessions',        '', false],
 101              ['upgrade_log',     '', false],
 102  
 103              // Additional skipped tables.
 104              ['context',      '', false],
 105              ['quiz_attempts',     '', false],
 106              ['role_assignments',     '', false],
 107  
 108              // Normal tables.
 109              ['assign',          '', true],
 110              ['book',          '', true],
 111          ];
 112      }
 113  
 114      /**
 115       * Test additional skip tables.
 116       *
 117       * @dataProvider db_should_replace_additional_skip_tables_dataprovider
 118       * @covers ::db_should_replace
 119       * @param string $table name
 120       * @param string $column name
 121       * @param bool $expected whether it should be replaced
 122       */
 123      public function test_db_should_replace_additional_skip_tables(string $table, string $column, bool $expected) {
 124          $this->resetAfterTest();
 125          $additionalskiptables = 'context, quiz_attempts, role_assignments ';
 126          $actual = db_should_replace($table, $column, $additionalskiptables);
 127          $this->assertSame($actual, $expected);
 128      }
 129  
 130      /**
 131       * Test method used by upgradesettings.php to make sure
 132       * there are no missing settings in PHPUnit and Behat tests.
 133       *
 134       * @covers ::admin_output_new_settings_by_page
 135       */
 136      public function test_admin_output_new_settings_by_page() {
 137          $this->resetAfterTest();
 138          $this->setAdminUser();
 139  
 140          // Add settings not set during PHPUnit init.
 141          set_config('supportemail', 'support@example.com');
 142          $frontpage = new \admin_setting_special_frontpagedesc();
 143          $frontpage->write_setting('test test');
 144  
 145          // NOTE: if this test fails then it is most likely extra setting in
 146          // some additional plugin without default - developer needs to add
 147          // a workaround into their db/install.php for PHPUnit and Behat.
 148  
 149          $root = admin_get_root(true, true);
 150          $new = admin_output_new_settings_by_page($root);
 151          $this->assertSame([], $new);
 152  
 153          unset_config('numbering', 'book');
 154          unset_config('supportemail');
 155          $root = admin_get_root(true, true);
 156          $new = admin_output_new_settings_by_page($root);
 157          $this->assertCount(2, $new);
 158      }
 159  
 160      /**
 161       * Test repeated recursive application of default settings.
 162       *
 163       * @covers ::admin_apply_default_settings
 164       */
 165      public function test_admin_apply_default_settings() {
 166          global $DB;
 167  
 168          $this->resetAfterTest();
 169          $this->setAdminUser();
 170  
 171          // There should not be any pending new defaults.
 172          $saved = admin_apply_default_settings(null, false);
 173          $this->assertSame([], $saved);
 174  
 175          // Emulation of upgrades from CLI.
 176          unset_config('logocompact', 'core_admin');
 177          unset_config('grade_aggregationposition');
 178          unset_config('numbering', 'book');
 179          unset_config('enabled', 'core_competency');
 180          unset_config('pushcourseratingstouserplans', 'core_competency');
 181          $saved = admin_apply_default_settings(null, false);
 182          $expected = [
 183              'core_competency/enabled' => '1',
 184              'grade_aggregationposition' => '1',
 185              'book/numbering' => '1',
 186              'core_admin/logocompact' => '',
 187              'core_competency/pushcourseratingstouserplans' => '1',
 188          ];
 189          $this->assertEquals($expected, $saved);
 190  
 191          // Repeated application of defaults - not done usually.
 192          $saved = admin_apply_default_settings(null, true);
 193          $this->assertGreaterThan(500, count($saved));
 194          $saved = admin_apply_default_settings();
 195          $this->assertGreaterThan(500, count($saved));
 196  
 197          // Emulate initial application of defaults.
 198          $DB->delete_records('config', []);
 199          $DB->delete_records('config_plugins', []);
 200          purge_all_caches();
 201          $saved = admin_apply_default_settings(null, true);
 202          $this->assertGreaterThan(500, count($saved));
 203  
 204          // Make sure there were enough repetitions.
 205          $saved = admin_apply_default_settings(null, false);
 206          $this->assertSame([], $saved);
 207      }
 208  }