Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 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  }