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 * Persistent class tests. 19 * 20 * @package core 21 * @copyright 2015 Frédéric Massart - FMCorz.net 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 global $CFG; 27 28 /** 29 * Persistent testcase. 30 * 31 * @package core 32 * @copyright 2015 Frédéric Massart - FMCorz.net 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 class core_persistent_testcase extends advanced_testcase { 36 37 public function setUp(): void { 38 $this->make_persistent_table(); 39 $this->resetAfterTest(); 40 } 41 42 /** 43 * Make the table for the persistent. 44 */ 45 protected function make_persistent_table() { 46 global $DB; 47 $dbman = $DB->get_manager(); 48 49 $table = new xmldb_table(core_testable_persistent::TABLE); 50 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); 51 $table->add_field('shortname', XMLDB_TYPE_CHAR, '100', null, null, null, null); 52 $table->add_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null); 53 $table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null); 54 $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0'); 55 $table->add_field('parentid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); 56 $table->add_field('path', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null); 57 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); 58 $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', null, null, null, null); 59 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); 60 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); 61 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); 62 63 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); 64 65 if ($dbman->table_exists($table)) { 66 $dbman->drop_table($table); 67 } 68 69 $dbman->create_table($table); 70 } 71 72 public function test_properties_definition() { 73 $expected = array( 74 'shortname' => array( 75 'type' => PARAM_TEXT, 76 'default' => '', 77 'null' => NULL_NOT_ALLOWED 78 ), 79 'idnumber' => array( 80 'type' => PARAM_TEXT, 81 'null' => NULL_NOT_ALLOWED 82 ), 83 'description' => array( 84 'type' => PARAM_TEXT, 85 'default' => '', 86 'null' => NULL_NOT_ALLOWED 87 ), 88 'descriptionformat' => array( 89 'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN), 90 'type' => PARAM_INT, 91 'default' => FORMAT_HTML, 92 'null' => NULL_NOT_ALLOWED 93 ), 94 'parentid' => array( 95 'type' => PARAM_INT, 96 'default' => 0, 97 'null' => NULL_NOT_ALLOWED 98 ), 99 'path' => array( 100 'type' => PARAM_RAW, 101 'default' => '', 102 'null' => NULL_NOT_ALLOWED 103 ), 104 'sortorder' => array( 105 'type' => PARAM_INT, 106 'message' => new lang_string('invalidrequest', 'error'), 107 'null' => NULL_NOT_ALLOWED 108 ), 109 'scaleid' => array( 110 'default' => null, 111 'type' => PARAM_INT, 112 'null' => NULL_ALLOWED 113 ), 114 'id' => array( 115 'default' => 0, 116 'type' => PARAM_INT, 117 'null' => NULL_NOT_ALLOWED 118 ), 119 'timecreated' => array( 120 'default' => 0, 121 'type' => PARAM_INT, 122 'null' => NULL_NOT_ALLOWED 123 ), 124 'timemodified' => array( 125 'default' => 0, 126 'type' => PARAM_INT, 127 'null' => NULL_NOT_ALLOWED 128 ), 129 'usermodified' => array( 130 'default' => 0, 131 'type' => PARAM_INT, 132 'null' => NULL_NOT_ALLOWED 133 ), 134 ); 135 $this->assertEquals($expected, core_testable_persistent::properties_definition()); 136 } 137 138 public function test_to_record() { 139 $p = new core_testable_persistent(); 140 $expected = (object) array( 141 'shortname' => '', 142 'idnumber' => null, 143 'description' => '', 144 'descriptionformat' => FORMAT_HTML, 145 'parentid' => 0, 146 'path' => '', 147 'sortorder' => null, 148 'id' => 0, 149 'timecreated' => 0, 150 'timemodified' => 0, 151 'usermodified' => 0, 152 'scaleid' => null, 153 ); 154 $this->assertEquals($expected, $p->to_record()); 155 } 156 157 public function test_from_record() { 158 $p = new core_testable_persistent(); 159 $data = (object) array( 160 'shortname' => 'ddd', 161 'idnumber' => 'abc', 162 'description' => 'xyz', 163 'descriptionformat' => FORMAT_PLAIN, 164 'parentid' => 999, 165 'path' => '/a/b/c', 166 'sortorder' => 12, 167 'id' => 1, 168 'timecreated' => 2, 169 'timemodified' => 3, 170 'usermodified' => 4, 171 'scaleid' => null, 172 ); 173 $p->from_record($data); 174 $this->assertEquals($data, $p->to_record()); 175 } 176 177 public function test_from_record_invalid_param() { 178 $p = new core_testable_persistent(); 179 $data = (object) array( 180 'invalidparam' => 'abc' 181 ); 182 183 $this->expectException(coding_exception::class); 184 $p->from_record($data); 185 } 186 187 public function test_validate() { 188 $data = (object) array( 189 'idnumber' => 'abc', 190 'sortorder' => 0 191 ); 192 $p = new core_testable_persistent(0, $data); 193 $this->assertFalse(isset($p->beforevalidate)); 194 $this->assertTrue($p->validate()); 195 $this->assertTrue(isset($p->beforevalidate)); 196 $this->assertTrue($p->is_valid()); 197 $this->assertEquals(array(), $p->get_errors()); 198 $p->set('descriptionformat', -100); 199 200 $expected = array( 201 'descriptionformat' => new lang_string('invaliddata', 'error'), 202 ); 203 $this->assertEquals($expected, $p->validate()); 204 $this->assertFalse($p->is_valid()); 205 $this->assertEquals($expected, $p->get_errors()); 206 } 207 208 public function test_validation_required() { 209 $data = (object) array( 210 'idnumber' => 'abc' 211 ); 212 $p = new core_testable_persistent(0, $data); 213 $expected = array( 214 'sortorder' => new lang_string('requiredelement', 'form'), 215 ); 216 $this->assertFalse($p->is_valid()); 217 $this->assertEquals($expected, $p->get_errors()); 218 } 219 220 public function test_validation_custom() { 221 $data = (object) array( 222 'idnumber' => 'abc', 223 'sortorder' => 10, 224 ); 225 $p = new core_testable_persistent(0, $data); 226 $expected = array( 227 'sortorder' => new lang_string('invalidkey', 'error'), 228 ); 229 $this->assertFalse($p->is_valid()); 230 $this->assertEquals($expected, $p->get_errors()); 231 } 232 233 public function test_validation_custom_message() { 234 $data = (object) array( 235 'idnumber' => 'abc', 236 'sortorder' => 'abc', 237 ); 238 $p = new core_testable_persistent(0, $data); 239 $expected = array( 240 'sortorder' => new lang_string('invalidrequest', 'error'), 241 ); 242 $this->assertFalse($p->is_valid()); 243 $this->assertEquals($expected, $p->get_errors()); 244 } 245 246 public function test_validation_choices() { 247 $data = (object) array( 248 'idnumber' => 'abc', 249 'sortorder' => 0, 250 'descriptionformat' => -100 251 ); 252 $p = new core_testable_persistent(0, $data); 253 $expected = array( 254 'descriptionformat' => new lang_string('invaliddata', 'error'), 255 ); 256 $this->assertFalse($p->is_valid()); 257 $this->assertEquals($expected, $p->get_errors()); 258 } 259 260 public function test_validation_type() { 261 $data = (object) array( 262 'idnumber' => 'abc', 263 'sortorder' => 'NaN' 264 ); 265 $p = new core_testable_persistent(0, $data); 266 $this->assertFalse($p->is_valid()); 267 $this->assertArrayHasKey('sortorder', $p->get_errors()); 268 } 269 270 public function test_validation_null() { 271 $data = (object) array( 272 'idnumber' => null, 273 'sortorder' => 0, 274 'scaleid' => 'bad!' 275 ); 276 $p = new core_testable_persistent(0, $data); 277 $this->assertFalse($p->is_valid()); 278 $this->assertArrayHasKey('idnumber', $p->get_errors()); 279 $this->assertArrayHasKey('scaleid', $p->get_errors()); 280 $p->set('idnumber', 'abc'); 281 $this->assertFalse($p->is_valid()); 282 $this->assertArrayNotHasKey('idnumber', $p->get_errors()); 283 $this->assertArrayHasKey('scaleid', $p->get_errors()); 284 $p->set('scaleid', null); 285 $this->assertTrue($p->is_valid()); 286 $this->assertArrayNotHasKey('scaleid', $p->get_errors()); 287 } 288 289 public function test_create() { 290 global $DB; 291 $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc')); 292 $this->assertFalse(isset($p->beforecreate)); 293 $this->assertFalse(isset($p->aftercreate)); 294 $p->create(); 295 $record = $DB->get_record(core_testable_persistent::TABLE, array('id' => $p->get('id')), '*', MUST_EXIST); 296 $expected = $p->to_record(); 297 $this->assertTrue(isset($p->beforecreate)); 298 $this->assertTrue(isset($p->aftercreate)); 299 $this->assertEquals($expected->sortorder, $record->sortorder); 300 $this->assertEquals($expected->idnumber, $record->idnumber); 301 $this->assertEquals($expected->id, $record->id); 302 $this->assertTrue($p->is_valid()); // Should always be valid after a create. 303 } 304 305 public function test_update() { 306 global $DB; 307 $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc')); 308 $p->create(); 309 $id = $p->get('id'); 310 $p->set('sortorder', 456); 311 $p->from_record((object) array('idnumber' => 'def')); 312 $this->assertFalse(isset($p->beforeupdate)); 313 $this->assertFalse(isset($p->afterupdate)); 314 $p->update(); 315 316 $expected = $p->to_record(); 317 $record = $DB->get_record(core_testable_persistent::TABLE, array('id' => $p->get('id')), '*', MUST_EXIST); 318 $this->assertTrue(isset($p->beforeupdate)); 319 $this->assertTrue(isset($p->afterupdate)); 320 $this->assertEquals($id, $record->id); 321 $this->assertEquals(456, $record->sortorder); 322 $this->assertEquals('def', $record->idnumber); 323 $this->assertTrue($p->is_valid()); // Should always be valid after an update. 324 } 325 326 public function test_save() { 327 global $DB; 328 $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc')); 329 $this->assertFalse(isset($p->beforecreate)); 330 $this->assertFalse(isset($p->aftercreate)); 331 $this->assertFalse(isset($p->beforeupdate)); 332 $this->assertFalse(isset($p->beforeupdate)); 333 $p->save(); 334 $record = $DB->get_record(core_testable_persistent::TABLE, array('id' => $p->get('id')), '*', MUST_EXIST); 335 $expected = $p->to_record(); 336 $this->assertTrue(isset($p->beforecreate)); 337 $this->assertTrue(isset($p->aftercreate)); 338 $this->assertFalse(isset($p->beforeupdate)); 339 $this->assertFalse(isset($p->beforeupdate)); 340 $this->assertEquals($expected->sortorder, $record->sortorder); 341 $this->assertEquals($expected->idnumber, $record->idnumber); 342 $this->assertEquals($expected->id, $record->id); 343 $this->assertTrue($p->is_valid()); // Should always be valid after a save/create. 344 345 $p->set('idnumber', 'abcd'); 346 $p->save(); 347 $record = $DB->get_record(core_testable_persistent::TABLE, array('id' => $p->get('id')), '*', MUST_EXIST); 348 $expected = $p->to_record(); 349 $this->assertTrue(isset($p->beforeupdate)); 350 $this->assertTrue(isset($p->beforeupdate)); 351 $this->assertEquals($expected->sortorder, $record->sortorder); 352 $this->assertEquals($expected->idnumber, $record->idnumber); 353 $this->assertEquals($expected->id, $record->id); 354 $this->assertTrue($p->is_valid()); // Should always be valid after a save/update. 355 } 356 357 public function test_read() { 358 $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc')); 359 $p->create(); 360 unset($p->beforevalidate); 361 unset($p->beforecreate); 362 unset($p->aftercreate); 363 364 $p2 = new core_testable_persistent($p->get('id')); 365 $this->assertEquals($p, $p2); 366 367 $p3 = new core_testable_persistent(); 368 $p3->set('id', $p->get('id')); 369 $p3->read(); 370 $this->assertEquals($p, $p3); 371 } 372 373 public function test_delete() { 374 global $DB; 375 376 $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc')); 377 $p->create(); 378 $this->assertNotEquals(0, $p->get('id')); 379 $this->assertTrue($DB->record_exists_select(core_testable_persistent::TABLE, 'id = ?', array($p->get('id')))); 380 $this->assertFalse(isset($p->beforedelete)); 381 $this->assertFalse(isset($p->afterdelete)); 382 383 $p->delete(); 384 $this->assertFalse($DB->record_exists_select(core_testable_persistent::TABLE, 'id = ?', array($p->get('id')))); 385 $this->assertEquals(0, $p->get('id')); 386 $this->assertEquals(true, $p->beforedelete); 387 $this->assertEquals(true, $p->afterdelete); 388 } 389 390 public function test_has_property() { 391 $this->assertFalse(core_testable_persistent::has_property('unknown')); 392 $this->assertTrue(core_testable_persistent::has_property('idnumber')); 393 } 394 395 public function test_custom_setter_getter() { 396 global $DB; 397 398 $path = array(1, 2, 3); 399 $json = json_encode($path); 400 401 $p = new core_testable_persistent(0, (object) array('sortorder' => 0, 'idnumber' => 'abc')); 402 $p->set('path', $path); 403 $this->assertEquals($path, $p->get('path')); 404 $this->assertEquals($json, $p->to_record()->path); 405 406 $p->create(); 407 $record = $DB->get_record(core_testable_persistent::TABLE, array('id' => $p->get('id')), 'id, path', MUST_EXIST); 408 $this->assertEquals($json, $record->path); 409 } 410 411 public function test_record_exists() { 412 global $DB; 413 $this->assertFalse($DB->record_exists(core_testable_persistent::TABLE, array('idnumber' => 'abc'))); 414 $p = new core_testable_persistent(0, (object) array('sortorder' => 123, 'idnumber' => 'abc')); 415 $p->create(); 416 $id = $p->get('id'); 417 $this->assertTrue(core_testable_persistent::record_exists($id)); 418 $this->assertTrue($DB->record_exists(core_testable_persistent::TABLE, array('idnumber' => 'abc'))); 419 $p->delete(); 420 $this->assertFalse(core_testable_persistent::record_exists($id)); 421 } 422 423 public function test_get_sql_fields() { 424 $expected = '' . 425 'c.id AS prefix_id, ' . 426 'c.shortname AS prefix_shortname, ' . 427 'c.idnumber AS prefix_idnumber, ' . 428 'c.description AS prefix_description, ' . 429 'c.descriptionformat AS prefix_descriptionformat, ' . 430 'c.parentid AS prefix_parentid, ' . 431 'c.path AS prefix_path, ' . 432 'c.sortorder AS prefix_sortorder, ' . 433 'c.scaleid AS prefix_scaleid, ' . 434 'c.timecreated AS prefix_timecreated, ' . 435 'c.timemodified AS prefix_timemodified, ' . 436 'c.usermodified AS prefix_usermodified'; 437 $this->assertEquals($expected, core_testable_persistent::get_sql_fields('c', 'prefix_')); 438 } 439 440 public function test_get_sql_fields_too_long() { 441 $this->expectException(coding_exception::class); 442 $this->expectExceptionMessageMatches('/The alias .+ exceeds 30 characters/'); 443 core_testable_persistent::get_sql_fields('c'); 444 } 445 } 446 447 /** 448 * Example persistent class. 449 * 450 * @package core 451 * @copyright 2015 Frédéric Massart - FMCorz.net 452 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 453 */ 454 class core_testable_persistent extends \core\persistent { 455 456 const TABLE = 'phpunit_persistent'; 457 458 protected static function define_properties() { 459 return array( 460 'shortname' => array( 461 'type' => PARAM_TEXT, 462 'default' => '' 463 ), 464 'idnumber' => array( 465 'type' => PARAM_TEXT, 466 ), 467 'description' => array( 468 'type' => PARAM_TEXT, 469 'default' => '' 470 ), 471 'descriptionformat' => array( 472 'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN), 473 'type' => PARAM_INT, 474 'default' => FORMAT_HTML 475 ), 476 'parentid' => array( 477 'type' => PARAM_INT, 478 'default' => 0 479 ), 480 'path' => array( 481 'type' => PARAM_RAW, 482 'default' => '' 483 ), 484 'sortorder' => array( 485 'type' => PARAM_INT, 486 'message' => new lang_string('invalidrequest', 'error') 487 ), 488 'scaleid' => array( 489 'type' => PARAM_INT, 490 'default' => null, 491 'null' => NULL_ALLOWED 492 ) 493 ); 494 } 495 496 protected function before_validate() { 497 $this->beforevalidate = true; 498 } 499 500 protected function before_create() { 501 $this->beforecreate = true; 502 } 503 504 protected function before_update() { 505 $this->beforeupdate = true; 506 } 507 508 protected function before_delete() { 509 $this->beforedelete = true; 510 } 511 512 protected function after_create() { 513 $this->aftercreate = true; 514 } 515 516 protected function after_update($result) { 517 $this->afterupdate = true; 518 } 519 520 protected function after_delete($result) { 521 $this->afterdelete = true; 522 } 523 524 protected function get_path() { 525 $value = $this->raw_get('path'); 526 if (!empty($value)) { 527 $value = json_decode($value); 528 } 529 return $value; 530 } 531 532 protected function set_path($value) { 533 if (!empty($value)) { 534 $value = json_encode($value); 535 } 536 $this->raw_set('path', $value); 537 } 538 539 protected function validate_sortorder($value) { 540 if ($value == 10) { 541 return new lang_string('invalidkey', 'error'); 542 } 543 return true; 544 } 545 546 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body