Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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 parts of adminlib.php.
  19   *
  20   * @package    core
  21   * @subpackage admin
  22   * @copyright  2020 Brendan Heywood <brendan@catalyst-au.net>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->libdir.'/adminlib.php');
  30  
  31  /**
  32   * Unit tests for parts of adminlib.php.
  33   *
  34   * @copyright  2020 Brendan Heywood <brendan@catalyst-au.net>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class core_adminlib_testcase extends advanced_testcase {
  38  
  39      /**
  40       * Data provider of serialized string.
  41       *
  42       * @return array
  43       */
  44      public function db_should_replace_dataprovider() {
  45          return [
  46              // Skipped tables.
  47              ['block_instances', '', false],
  48              ['config',          '', false],
  49              ['config_plugins',  '', false],
  50              ['config_log',      '', false],
  51              ['events_queue',    '', false],
  52              ['filter_config',   '', false],
  53              ['log',             '', false],
  54              ['repository_instance_config', '', false],
  55              ['sessions',        '', false],
  56              ['upgrade_log',     '', false],
  57  
  58              // Unknown skipped tables.
  59              ['foobar_log',      '', false],
  60              ['foobar_logs',     '', false],
  61  
  62              // Unknown ok tables.
  63              ['foobar_logical',  '', true],
  64  
  65              // Normal tables.
  66              ['assign',          '', true],
  67  
  68              // Normal tables with excluded columns.
  69              ['message_conversations', 'convhash',   false],
  70              ['user_password_history', 'hash',       false],
  71              ['foo',                   'barhash',    false],
  72          ];
  73      }
  74  
  75      /**
  76       * Test which tables and column should be replaced.
  77       *
  78       * @dataProvider db_should_replace_dataprovider
  79       * @param string $table name
  80       * @param string $column name
  81       * @param bool $expected whether it should be replaced
  82       */
  83      public function test_db_should_replace(string $table, string $column, bool $expected) {
  84          $actual = db_should_replace($table, $column);
  85          $this->assertSame($actual, $expected);
  86      }
  87  
  88      /**
  89       * Data provider for additional skip tables.
  90       *
  91       * @return array
  92       */
  93      public function db_should_replace_additional_skip_tables_dataprovider() {
  94          return [
  95              // Skipped tables.
  96              ['block_instances', '', false],
  97              ['config',          '', false],
  98              ['config_plugins',  '', false],
  99              ['config_log',      '', false],
 100              ['events_queue',    '', false],
 101              ['filter_config',   '', false],
 102              ['log',             '', false],
 103              ['repository_instance_config', '', false],
 104              ['sessions',        '', false],
 105              ['upgrade_log',     '', false],
 106  
 107              // Additional skipped tables.
 108              ['context',      '', false],
 109              ['quiz_attempts',     '', false],
 110              ['role_assignments',     '', false],
 111  
 112              // Normal tables.
 113              ['assign',          '', true],
 114              ['book',          '', true],
 115          ];
 116      }
 117  
 118      /**
 119       * Test additional skip tables.
 120       *
 121       * @dataProvider db_should_replace_additional_skip_tables_dataprovider
 122       * @param string $table name
 123       * @param string $column name
 124       * @param bool $expected whether it should be replaced
 125       */
 126      public function test_db_should_replace_additional_skip_tables(string $table, string $column, bool $expected) {
 127          $this->resetAfterTest();
 128          $additionalskiptables = 'context, quiz_attempts, role_assignments ';
 129          $actual = db_should_replace($table, $column, $additionalskiptables);
 130          $this->assertSame($actual, $expected);
 131      }
 132  }