Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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       * @param string $table name
  74       * @param string $column name
  75       * @param bool $expected whether it should be replaced
  76       */
  77      public function test_db_should_replace(string $table, string $column, bool $expected) {
  78          $actual = db_should_replace($table, $column);
  79          $this->assertSame($actual, $expected);
  80      }
  81  
  82      /**
  83       * Data provider for additional skip tables.
  84       *
  85       * @return array
  86       */
  87      public function db_should_replace_additional_skip_tables_dataprovider() {
  88          return [
  89              // Skipped tables.
  90              ['block_instances', '', false],
  91              ['config',          '', false],
  92              ['config_plugins',  '', false],
  93              ['config_log',      '', false],
  94              ['events_queue',    '', false],
  95              ['filter_config',   '', false],
  96              ['log',             '', false],
  97              ['repository_instance_config', '', false],
  98              ['sessions',        '', false],
  99              ['upgrade_log',     '', false],
 100  
 101              // Additional skipped tables.
 102              ['context',      '', false],
 103              ['quiz_attempts',     '', false],
 104              ['role_assignments',     '', false],
 105  
 106              // Normal tables.
 107              ['assign',          '', true],
 108              ['book',          '', true],
 109          ];
 110      }
 111  
 112      /**
 113       * Test additional skip tables.
 114       *
 115       * @dataProvider db_should_replace_additional_skip_tables_dataprovider
 116       * @param string $table name
 117       * @param string $column name
 118       * @param bool $expected whether it should be replaced
 119       */
 120      public function test_db_should_replace_additional_skip_tables(string $table, string $column, bool $expected) {
 121          $this->resetAfterTest();
 122          $additionalskiptables = 'context, quiz_attempts, role_assignments ';
 123          $actual = db_should_replace($table, $column, $additionalskiptables);
 124          $this->assertSame($actual, $expected);
 125      }
 126  }