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 39 and 401]

   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  use core\dml\table;
  20  use xmldb_table;
  21  
  22  /**
  23   * DML Table tests.
  24   *
  25   * @package    core
  26   * @category   test
  27   * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   * @coversDefaultClass \core\dml\table
  30   */
  31  class dml_table_test extends \database_driver_testcase {
  32  
  33      /**
  34       * Data provider for various \core\dml\table method tests.
  35       *
  36       * @return  array
  37       */
  38      public function get_field_select_provider() : array {
  39          return [
  40              'single field' => [
  41                  'tablename' => 'test_table_single',
  42                  'fieldlist' => [
  43                      'id' => ['id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null],
  44                  ],
  45                  'primarykey' => 'id',
  46                  'fieldprefix' => 'ban',
  47                  'tablealias' => 'banana',
  48                  'banana.id AS banid',
  49              ],
  50              'multiple fields' => [
  51                  'tablename' => 'test_table_multiple',
  52                  'fieldlist' => [
  53                      'id' => ['id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null],
  54                      'course' => ['course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'],
  55                      'name' => ['name', XMLDB_TYPE_CHAR, '255', null, null, null, 'lala'],
  56                  ],
  57                  'primarykey' => 'id',
  58                  'fieldprefix' => 'ban',
  59                  'tablealias' => 'banana',
  60                  'banana.id AS banid, banana.course AS bancourse, banana.name AS banname',
  61              ],
  62          ];
  63      }
  64  
  65      /**
  66       * Ensure that \core\dml\table::get_field_select() works as expected.
  67       *
  68       * @dataProvider get_field_select_provider
  69       * @covers ::get_field_select
  70       * @param   string      $tablename The name of the table
  71       * @param   array       $fieldlist The list of fields
  72       * @param   string      $primarykey The name of the primary key
  73       * @param   string      $fieldprefix The prefix to use for each field
  74       * @param   string      $tablealias The table AS alias name
  75       * @param   string      $expected The expected SQL
  76       */
  77      public function test_get_field_select(
  78          string $tablename,
  79          array $fieldlist,
  80          string $primarykey,
  81          string $fieldprefix,
  82          string $tablealias,
  83          string $expected
  84      ) {
  85          $dbman = $this->tdb->get_manager();
  86  
  87          $xmldbtable = new xmldb_table($tablename);
  88          $xmldbtable->setComment("This is a test'n drop table. You can drop it safely");
  89  
  90          foreach ($fieldlist as $args) {
  91              call_user_func_array([$xmldbtable, 'add_field'], $args);
  92          }
  93          $xmldbtable->add_key('primary', XMLDB_KEY_PRIMARY, [$primarykey]);
  94          $dbman->create_table($xmldbtable);
  95  
  96          $table = new table($tablename, $tablealias, $fieldprefix);
  97          $this->assertEquals($expected, $table->get_field_select());
  98      }
  99  
 100      /**
 101       * Data provider for \core\dml\table::extract_from_result() tests.
 102       *
 103       * @return  array
 104       */
 105      public function extract_from_result_provider() : array {
 106          return [
 107              'single table' => [
 108                  'fieldlist' => [
 109                      'id' => ['id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null],
 110                      'course' => ['course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'],
 111                      'flag' => ['flag', XMLDB_TYPE_CHAR, '255', null, null, null, 'lala'],
 112                  ],
 113                  'primarykey' => 'id',
 114                  'prefix' => 's',
 115                  'result' => (object) [
 116                      'sid' => 1,
 117                      'scourse' => 42,
 118                      'sflag' => 'foo',
 119                  ],
 120                  'expectedrecord' => (object) [
 121                      'id' => 1,
 122                      'course' => 42,
 123                      'flag' => 'foo',
 124                  ],
 125              ],
 126              'single table amongst others' => [
 127                  'fieldlist' => [
 128                      'id' => ['id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null],
 129                      'course' => ['course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'],
 130                      'flag' => ['flag', XMLDB_TYPE_CHAR, '255', null, null, null, 'lala'],
 131                  ],
 132                  'primarykey' => 'id',
 133                  'prefix' => 's',
 134                  'result' => (object) [
 135                      'sid' => 1,
 136                      'scourse' => 42,
 137                      'sflag' => 'foo',
 138                      'oid' => 'id',
 139                      'ocourse' => 'course',
 140                      'oflag' => 'flag',
 141                  ],
 142                  'expectedrecord' => (object) [
 143                      'id' => 1,
 144                      'course' => 42,
 145                      'flag' => 'foo',
 146                  ],
 147              ],
 148          ];
 149      }
 150  
 151      /**
 152       * Ensure that \core\dml\table::extract_from_result() works as expected.
 153       *
 154       * @dataProvider        extract_from_result_provider
 155       * @covers ::extract_from_result
 156       * @param   array       $fieldlist The list of fields
 157       * @param   string      $primarykey The name of the primary key
 158       * @param   string      $fieldprefix The prefix to use for each field
 159       * @param   \stdClass $result The result of the get_records_sql
 160       * @param   \stdClass $expected The expected output
 161       */
 162      public function test_extract_fields_from_result(
 163          array $fieldlist,
 164          string $primarykey,
 165          string $fieldprefix,
 166          \stdClass $result,
 167          \stdClass $expected
 168      ) {
 169          $dbman = $this->tdb->get_manager();
 170  
 171          $tablename = 'test_table_extraction';
 172          $xmldbtable = new xmldb_table($tablename);
 173          $xmldbtable->setComment("This is a test'n drop table. You can drop it safely");
 174  
 175          foreach ($fieldlist as $args) {
 176              call_user_func_array([$xmldbtable, 'add_field'], $args);
 177          }
 178          $xmldbtable->add_key('primary', XMLDB_KEY_PRIMARY, [$primarykey]);
 179          $dbman->create_table($xmldbtable);
 180  
 181          $table = new table($tablename, 'footable', $fieldprefix);
 182          $this->assertEquals($expected, $table->extract_from_result($result));
 183      }
 184  
 185      /**
 186       * Ensure that \core\dml\table::get_from_sql() works as expected.
 187       *
 188       * @dataProvider get_field_select_provider
 189       * @covers ::get_from_sql
 190       * @param   string      $tablename The name of the table
 191       * @param   array       $fieldlist The list of fields
 192       * @param   string      $primarykey The name of the primary key
 193       * @param   string      $fieldprefix The prefix to use for each field
 194       * @param   string      $tablealias The table AS alias name
 195       * @param   string      $expected The expected SQL
 196       */
 197      public function test_get_from_sql(
 198          string $tablename,
 199          array $fieldlist,
 200          string $primarykey,
 201          string $fieldprefix,
 202          string $tablealias,
 203          string $expected
 204      ) {
 205          $dbman = $this->tdb->get_manager();
 206  
 207          $tablename = 'test_table_extraction';
 208          $xmldbtable = new xmldb_table($tablename);
 209          $xmldbtable->setComment("This is a test'n drop table. You can drop it safely");
 210  
 211          foreach ($fieldlist as $args) {
 212              call_user_func_array([$xmldbtable, 'add_field'], $args);
 213          }
 214          $xmldbtable->add_key('primary', XMLDB_KEY_PRIMARY, [$primarykey]);
 215          $dbman->create_table($xmldbtable);
 216  
 217          $table = new table($tablename, $tablealias, $fieldprefix);
 218  
 219          $this->assertEquals("{{$tablename}} {$tablealias}", $table->get_from_sql());
 220      }
 221  }