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