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 enrol_lti\local\ltiadvantage\entity; 18 19 /** 20 * Tests for user. 21 * 22 * @package enrol_lti 23 * @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com> 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 * @coversDefaultClass \enrol_lti\local\ltiadvantage\entity\user 26 */ 27 class user_test extends \advanced_testcase { 28 29 /** 30 * Test creation of a user instance using the factory method. 31 * 32 * @dataProvider create_data_provider 33 * @param array $args the arguments to the creation method. 34 * @param array $expectations various expectations for the test cases. 35 * @covers ::create 36 */ 37 public function test_create(array $args, array $expectations) { 38 if ($expectations['valid']) { 39 $user = user::create(...array_values($args)); 40 $this->assertInstanceOf(user::class, $user); 41 $this->assertEquals($expectations['id'], $user->get_id()); 42 $this->assertEquals($expectations['localid'], $user->get_localid()); 43 $this->assertEquals($expectations['resourcelinkid'], $user->get_resourcelinkid()); 44 $this->assertEquals($expectations['resourceid'], $user->get_resourceid()); 45 $this->assertEquals($expectations['deploymentid'], $user->get_deploymentid()); 46 $this->assertEquals($expectations['sourceid'], $user->get_sourceid()); 47 $this->assertEquals($expectations['lang'], $user->get_lang()); 48 $this->assertEquals($expectations['timezone'], $user->get_timezone()); 49 $this->assertEquals($expectations['city'], $user->get_city()); 50 $this->assertEquals($expectations['country'], $user->get_country()); 51 $this->assertEquals($expectations['institution'], $user->get_institution()); 52 $this->assertEquals($expectations['maildisplay'], $user->get_maildisplay()); 53 $this->assertEquals($expectations['lastgrade'], $user->get_lastgrade()); 54 $this->assertEquals($expectations['lastaccess'], $user->get_lastaccess()); 55 } else { 56 $this->expectException($expectations['exception']); 57 $this->expectExceptionMessage($expectations['exceptionmessage']); 58 user::create(...array_values($args)); 59 } 60 } 61 62 /** 63 * Data provider for testing the user::create() method. 64 * 65 * @return array the data for testing. 66 */ 67 public function create_data_provider(): array { 68 global $CFG; 69 return [ 70 'Valid create, only required args provided' => [ 71 'args' => [ 72 'resourceid' => 22, 73 'userid' => 2, 74 'deploymentid' => 33, 75 'sourceid' => 'user-id-123', 76 'lang' => $CFG->lang, 77 'timezone' => '99' 78 ], 79 'expectations' => [ 80 'valid' => true, 81 'resourceid' => 22, 82 'deploymentid' => 33, 83 'sourceid' => 'user-id-123', 84 'lang' => $CFG->lang, 85 'timezone' => '99', 86 'city' => '', 87 'country' => '', 88 'institution' => '', 89 'maildisplay' => 2, 90 'lastgrade' => 0.0, 91 'lastaccess' => null, 92 'id' => null, 93 'localid' => 2, 94 'resourcelinkid' => null, 95 ] 96 ], 97 'Valid create, all args provided explicitly' => [ 98 'args' => [ 99 'resourceid' => 22, 100 'userid' => 2, 101 'deploymentid' => 33, 102 'sourceid' => 'user-id-123', 103 'lang' => $CFG->lang, 104 'timezone' => '99', 105 'city' => 'Melbourne', 106 'country' => 'AU', 107 'institution' => 'My institution', 108 'maildisplay' => 1, 109 'lastgrade' => 50.55, 110 'lastaccess' => 14567888, 111 'resourcelinkid' => 44, 112 'id' => 22 113 ], 114 'expectations' => [ 115 'valid' => true, 116 'resourceid' => 22, 117 'deploymentid' => 33, 118 'sourceid' => 'user-id-123', 119 'lang' => $CFG->lang, 120 'timezone' => '99', 121 'city' => 'Melbourne', 122 'country' => 'AU', 123 'institution' => 'My institution', 124 'maildisplay' => 1, 125 'lastgrade' => 50.55, 126 'lastaccess' => 14567888, 127 'resourcelinkid' => 44, 128 'localid' => 2, 129 'id' => 22, 130 ] 131 ], 132 'Valid create, optional args explicitly nulled for default values' => [ 133 'args' => [ 134 'resourceid' => 22, 135 'userid' => 2, 136 'deploymentid' => 33, 137 'sourceid' => 'user-id-123', 138 'lang' => $CFG->lang, 139 'timezone' => '99', 140 'city' => 'Melbourne', 141 'country' => 'AU', 142 'institution' => 'My institution', 143 'maildisplay' => null, 144 'lastgrade' => null, 145 'lastaccess' => null, 146 'resourcelinkid' => null, 147 'id' => null 148 149 ], 150 'expectations' => [ 151 'valid' => true, 152 'resourceid' => 22, 153 'deploymentid' => 33, 154 'sourceid' => 'user-id-123', 155 'lang' => $CFG->lang, 156 'timezone' => '99', 157 'city' => 'Melbourne', 158 'country' => 'AU', 159 'institution' => 'My institution', 160 'maildisplay' => 2, 161 'lastgrade' => 0.0, 162 'lastaccess' => null, 163 'resourcelinkid' => null, 164 'localid' => 2, 165 'id' => null 166 ] 167 ], 168 'Invalid create, lang with bad value (vvvv not installed)' => [ 169 'args' => [ 170 'resourceid' => 22, 171 'userid' => 2, 172 'deploymentid' => 33, 173 'sourceid' => 'user-id-123', 174 'lang' => 'vvvv', 175 'timezone' => '99', 176 ], 177 'expectations' => [ 178 'valid' => false, 179 'exception' => \coding_exception::class, 180 'exceptionmessage' => "Invalid lang 'vvvv' provided." 181 ] 182 ], 183 'Invalid create, timezone with bad value' => [ 184 'args' => [ 185 'resourceid' => 22, 186 'userid' => 2, 187 'deploymentid' => 33, 188 'sourceid' => 'user-id-123', 189 'lang' => $CFG->lang, 190 'timezone' => 'NOT/FOUND', 191 ], 192 'expectations' => [ 193 'valid' => false, 194 'exception' => \coding_exception::class, 195 'exceptionmessage' => "Invalid timezone 'NOT/FOUND' provided." 196 ] 197 ], 198 'Invalid create, explicitly provided country with bad value' => [ 199 'args' => [ 200 'resourceid' => 22, 201 'userid' => 2, 202 'deploymentid' => 33, 203 'sourceid' => 'user-id-123', 204 'lang' => $CFG->lang, 205 'timezone' => '99', 206 'city' => '', 207 'country' => 'FFF', 208 ], 209 'expectations' => [ 210 'valid' => false, 211 'exception' => \coding_exception::class, 212 'exceptionmessage' => "Invalid country code 'FFF'." 213 ] 214 ], 215 'Invalid create, explicit maildisplay with bad value' => [ 216 'args' => [ 217 'resourceid' => 22, 218 'userid' => 2, 219 'deploymentid' => 33, 220 'sourceid' => 'user-id-123', 221 'lang' => $CFG->lang, 222 'timezone' => '99', 223 'city' => '', 224 'country' => '', 225 'institution' => '', 226 'maildisplay' => 3, 227 ], 228 'expectations' => [ 229 'valid' => false, 230 'exception' => \coding_exception::class, 231 'exceptionmessage' => "Invalid maildisplay value '3'. Must be in the range {0..2}." 232 ] 233 ], 234 ]; 235 } 236 237 /** 238 * Test creation of a user instance from a resource link. 239 * 240 * @dataProvider create_from_resource_link_data_provider 241 * @param array $args the arguments to the creation method. 242 * @param array $expectations various expectations for the test cases. 243 * @covers ::create_from_resource_link 244 */ 245 public function test_create_from_resource_link(array $args, array $expectations) { 246 if ($expectations['valid']) { 247 $user = user::create_from_resource_link(...array_values($args)); 248 $this->assertInstanceOf(user::class, $user); 249 $this->assertEquals($expectations['id'], $user->get_id()); 250 $this->assertEquals($expectations['localid'], $user->get_localid()); 251 $this->assertEquals($expectations['resourcelinkid'], $user->get_resourcelinkid()); 252 $this->assertEquals($expectations['resourceid'], $user->get_resourceid()); 253 $this->assertEquals($expectations['deploymentid'], $user->get_deploymentid()); 254 $this->assertEquals($expectations['sourceid'], $user->get_sourceid()); 255 $this->assertEquals($expectations['lang'], $user->get_lang()); 256 $this->assertEquals($expectations['city'], $user->get_city()); 257 $this->assertEquals($expectations['country'], $user->get_country()); 258 $this->assertEquals($expectations['institution'], $user->get_institution()); 259 $this->assertEquals($expectations['timezone'], $user->get_timezone()); 260 $this->assertEquals($expectations['maildisplay'], $user->get_maildisplay()); 261 $this->assertEquals($expectations['lastgrade'], $user->get_lastgrade()); 262 $this->assertEquals($expectations['lastaccess'], $user->get_lastaccess()); 263 } else { 264 $this->expectException($expectations['exception']); 265 $this->expectExceptionMessage($expectations['exceptionmessage']); 266 user::create_from_resource_link(...array_values($args)); 267 } 268 } 269 270 /** 271 * Data provider used in testing the user::create_from_resource_link() method. 272 * 273 * @return array the data for testing. 274 */ 275 public function create_from_resource_link_data_provider(): array { 276 global $CFG; 277 return [ 278 'Valid creation, all args provided explicitly' => [ 279 'args' => [ 280 'resourcelinkid' => 11, 281 'resourceid' => 22, 282 'userid' => 2, 283 'deploymentid' => 33, 284 'sourceid' => 'user-id-123', 285 'lang' => $CFG->lang, 286 'timezone' => '99', 287 'city' => 'Melbourne', 288 'country' => 'AU', 289 'institution' => 'platform', 290 'maildisplay' => 1 291 ], 292 'expectations' => [ 293 'valid' => true, 294 'id' => null, 295 'localid' => 2, 296 'resourcelinkid' => 11, 297 'resourceid' => 22, 298 'deploymentid' => 33, 299 'sourceid' => 'user-id-123', 300 'lang' => $CFG->lang, 301 'timezone' => '99', 302 'city' => 'Melbourne', 303 'country' => 'AU', 304 'institution' => 'platform', 305 'maildisplay' => 1, 306 'lastgrade' => 0.0, 307 'lastaccess' => null 308 ] 309 ], 310 'Valid creation, only required args provided, explicit values' => [ 311 'args' => [ 312 'resourcelinkid' => 11, 313 'resourceid' => 22, 314 'userid' => 2, 315 'deploymentid' => 33, 316 'sourceid' => 'user-id-123', 317 'lang' => $CFG->lang, 318 'timezone' => 'UTC' 319 ], 320 'expectations' => [ 321 'valid' => true, 322 'id' => null, 323 'localid' => 2, 324 'resourcelinkid' => 11, 325 'resourceid' => 22, 326 'deploymentid' => 33, 327 'sourceid' => 'user-id-123', 328 'lang' => $CFG->lang, 329 'timezone' => 'UTC', 330 'city' => '', 331 'country' => '', 332 'institution' => '', 333 'maildisplay' => 2, 334 'lastgrade' => 0.0, 335 'lastaccess' => null 336 ] 337 ], 338 'Invalid creation, only required args provided, empty sourceid' => [ 339 'args' => [ 340 'resourcelinkid' => 11, 341 'resourceid' => 22, 342 'user' => 2, 343 'deploymentid' => 33, 344 'sourceid' => '', 345 'lang' => $CFG->lang, 346 'timezone' => 'UTC' 347 ], 348 'expectations' => [ 349 'valid' => false, 350 'exception' => \coding_exception::class, 351 'exceptionmessage' => 'Invalid sourceid value. Cannot be an empty string.' 352 ] 353 ], 354 'Invalid creation, only required args provided, empty lang' => [ 355 'args' => [ 356 'resourcelinkid' => 11, 357 'resourceid' => 22, 358 'user' => 2, 359 'deploymentid' => 33, 360 'sourceid' => 'user-id-123', 361 'lang' => '', 362 'timezone' => 'UTC' 363 ], 364 'expectations' => [ 365 'valid' => false, 366 'exception' => \coding_exception::class, 367 'exceptionmessage' => 'Invalid lang value. Cannot be an empty string.' 368 ] 369 ], 370 'Invalid creation, only required args provided, empty timezone' => [ 371 'args' => [ 372 'resourcelinkid' => 11, 373 'resourceid' => 22, 374 'userid' => 2, 375 'deploymentid' => 33, 376 'sourceid' => 'user-id-123', 377 'lang' => $CFG->lang, 378 'timezone' => '' 379 ], 380 'expectations' => [ 381 'valid' => false, 382 'exception' => \coding_exception::class, 383 'exceptionmessage' => 'Invalid timezone value. Cannot be an empty string.' 384 ] 385 ], 386 'Invalid creation, only required args provided, invalid lang (vvvv not installed)' => [ 387 'args' => [ 388 'resourcelinkid' => 11, 389 'resourceid' => 22, 390 'userid' => 2, 391 'deploymentid' => 33, 392 'sourceid' => 'user-id-123', 393 'lang' => 'vvvv', 394 'timezone' => 'UTC' 395 ], 396 'expectations' => [ 397 'valid' => false, 398 'exception' => \coding_exception::class, 399 'exceptionmessage' => "Invalid lang 'vvvv' provided." 400 ] 401 ], 402 'Invalid creation, only required args provided, invalid timezone' => [ 403 'args' => [ 404 'resourcelinkid' => 11, 405 'resourceid' => 22, 406 'userid' => 2, 407 'deploymentid' => 33, 408 'sourceid' => 'user-id-123', 409 'lang' => $CFG->lang, 410 'timezone' => 'NOT/FOUND' 411 ], 412 'expectations' => [ 413 'valid' => false, 414 'exception' => \coding_exception::class, 415 'exceptionmessage' => "Invalid timezone 'NOT/FOUND' provided." 416 ] 417 ], 418 'Invalid creation, all args provided explicitly, invalid country' => [ 419 'args' => [ 420 'resourcelinkid' => 11, 421 'resourceid' => 22, 422 'userid' => 2, 423 'deploymentid' => 33, 424 'sourceid' => 'user-id-123', 425 'lang' => $CFG->lang, 426 'timezone' => '99', 427 'city' => 'Melbourne', 428 'country' => 'FFF', 429 'institution' => 'platform', 430 'maildisplay' => 1 431 ], 432 'expectations' => [ 433 'valid' => false, 434 'exception' => \coding_exception::class, 435 'exceptionmessage' => "Invalid country code 'FFF'." 436 ] 437 ], 438 'Invalid creation, all args provided explicitly, invalid maildisplay' => [ 439 'args' => [ 440 'resourcelinkid' => 11, 441 'resourceid' => 22, 442 'userid' => 2, 443 'deploymentid' => 33, 444 'sourceid' => 'user-id-123', 445 'lang' => $CFG->lang, 446 'timezone' => '99', 447 'city' => 'Melbourne', 448 'country' => 'AU', 449 'institution' => 'platform', 450 'maildisplay' => 4 451 ], 452 'expectations' => [ 453 'valid' => false, 454 'exception' => \coding_exception::class, 455 'exceptionmessage' => "Invalid maildisplay value '4'. Must be in the range {0..2}." 456 ] 457 ], 458 ]; 459 } 460 461 /** 462 * Helper to create a simple, working user for testing. 463 * 464 * @return user a user instance. 465 */ 466 protected function create_test_user(): user { 467 global $CFG; 468 $args = [ 469 'resourcelinkid' => 11, 470 'resourceid' => 22, 471 'userid' => 2, 472 'deploymentid' => 33, 473 'sourceid' => 'user-id-123', 474 'lang' => $CFG->lang, 475 'timezone' => 'UTC' 476 ]; 477 return user::create_from_resource_link(...array_values($args)); 478 } 479 480 481 /** 482 * Test the behaviour of the user setters and getters. 483 * 484 * @dataProvider setters_getters_data_provider 485 * @param string $methodname the name of the setter 486 * @param mixed $arg the argument to the setter 487 * @param array $expectations the array of expectations 488 * @covers ::__construct 489 */ 490 public function test_setters_and_getters(string $methodname, $arg, array $expectations) { 491 $user = $this->create_test_user(); 492 $setter = 'set_'.$methodname; 493 $getter = 'get_'.$methodname; 494 if ($expectations['valid']) { 495 $user->$setter($arg); 496 if (isset($expectations['expectedvalue'])) { 497 $this->assertEquals($expectations['expectedvalue'], $user->$getter()); 498 } else { 499 $this->assertEquals($arg, $user->$getter()); 500 } 501 502 } else { 503 $this->expectException($expectations['exception']); 504 $this->expectExceptionMessage($expectations['exceptionmessage']); 505 $user->$setter($arg); 506 } 507 } 508 509 /** 510 * Data provider for testing the user object setters. 511 * 512 * @return array the array of test data. 513 */ 514 public function setters_getters_data_provider(): array { 515 global $CFG; 516 return [ 517 'Testing set_resourcelinkid with valid id' => [ 518 'methodname' => 'resourcelinkid', 519 'arg' => 8, 520 'expectations' => [ 521 'valid' => true, 522 ] 523 ], 524 'Testing set_resourcelinkid with invalid id' => [ 525 'methodname' => 'resourcelinkid', 526 'arg' => -1, 527 'expectations' => [ 528 'valid' => false, 529 'exception' => \coding_exception::class, 530 'exceptionmessage' => "Invalid resourcelinkid '-1' provided. Must be > 0." 531 ] 532 ], 533 'Testing set_city with a non-empty string' => [ 534 'methodname' => 'city', 535 'arg' => 'Melbourne', 536 'expectations' => [ 537 'valid' => true, 538 ] 539 ], 540 'Testing set_city with an empty string' => [ 541 'methodname' => 'city', 542 'arg' => '', 543 'expectations' => [ 544 'valid' => true, 545 ] 546 ], 547 'Testing set_country with a valid country code' => [ 548 'methodname' => 'country', 549 'arg' => 'AU', 550 'expectations' => [ 551 'valid' => true, 552 ] 553 ], 554 'Testing set_country with an empty string' => [ 555 'methodname' => 'country', 556 'arg' => '', 557 'expectations' => [ 558 'valid' => true, 559 ] 560 ], 561 'Testing set_country with an invalid country code' => [ 562 'methodname' => 'country', 563 'arg' => 'FFF', 564 'expectations' => [ 565 'valid' => false, 566 'exception' => \coding_exception::class, 567 'exceptionmessage' => "Invalid country code 'FFF'." 568 ] 569 ], 570 'Testing set_institution with a non-empty string' => [ 571 'methodname' => 'institution', 572 'arg' => 'Some institution', 573 'expectations' => [ 574 'valid' => true, 575 ] 576 ], 577 'Testing set_institution with an empty string' => [ 578 'methodname' => 'institution', 579 'arg' => '', 580 'expectations' => [ 581 'valid' => true, 582 ] 583 ], 584 'Testing set_timezone with a valid real timezone' => [ 585 'methodname' => 'timezone', 586 'arg' => 'Pacific/Wallis', 587 'expectations' => [ 588 'valid' => true, 589 ] 590 ], 591 'Testing set_timezone with a valid server timezone value' => [ 592 'methodname' => 'timezone', 593 'arg' => '99', 594 'expectations' => [ 595 'valid' => true, 596 ] 597 ], 598 'Testing set_timezone with an invalid timezone value' => [ 599 'methodname' => 'timezone', 600 'arg' => 'NOT/FOUND', 601 'expectations' => [ 602 'valid' => false, 603 'exception' => \coding_exception::class, 604 'exceptionmessage' => "Invalid timezone 'NOT/FOUND' provided." 605 ] 606 ], 607 'Testing set_maildisplay with a valid int: 0' => [ 608 'methodname' => 'maildisplay', 609 'arg' => '0', 610 'expectations' => [ 611 'valid' => true, 612 ] 613 ], 614 'Testing set_maildisplay with a valid int: 1' => [ 615 'methodname' => 'maildisplay', 616 'arg' => '1', 617 'expectations' => [ 618 'valid' => true, 619 ] 620 ], 621 'Testing set_maildisplay with a valid int: 2' => [ 622 'methodname' => 'maildisplay', 623 'arg' => '1', 624 'expectations' => [ 625 'valid' => true, 626 ] 627 ], 628 'Testing set_maildisplay with invalid int' => [ 629 'methodname' => 'maildisplay', 630 'arg' => '-1', 631 'expectations' => [ 632 'valid' => false, 633 'exception' => \coding_exception::class, 634 'exceptionmessage' => "Invalid maildisplay value '-1'. Must be in the range {0..2}." 635 ] 636 ], 637 'Testing set_maildisplay with invalid int' => [ 638 'methodname' => 'maildisplay', 639 'arg' => '3', 640 'expectations' => [ 641 'valid' => false, 642 'exception' => \coding_exception::class, 643 'exceptionmessage' => "Invalid maildisplay value '3'. Must be in the range {0..2}." 644 ] 645 ], 646 'Testing set_lang with valid lang code' => [ 647 'methodname' => 'lang', 648 'arg' => $CFG->lang, 649 'expectations' => [ 650 'valid' => true, 651 ] 652 ], 653 'Testing set_lang with an empty string' => [ 654 'methodname' => 'lang', 655 'arg' => '', 656 'expectations' => [ 657 'valid' => false, 658 'exception' => \coding_exception::class, 659 'exceptionmessage' => 'Invalid lang value. Cannot be an empty string.' 660 ] 661 ], 662 'Testing set_lang with an empty string' => [ 663 'methodname' => 'lang', 664 'arg' => 'ff', 665 'expectations' => [ 666 'valid' => false, 667 'exception' => \coding_exception::class, 668 'exceptionmessage' => "Invalid lang 'ff' provided." 669 ] 670 ], 671 'Testing set_lastgrade with valid grade' => [ 672 'methodname' => 'lastgrade', 673 'arg' => 0.0, 674 'expectations' => [ 675 'valid' => true 676 ] 677 ], 678 'Testing set_lastgrade with valid non zero grade' => [ 679 'methodname' => 'lastgrade', 680 'arg' => 150.0, 681 'expectations' => [ 682 'valid' => true 683 ] 684 ], 685 'Testing set_lastgrade with valid non zero long decimal grade' => [ 686 'methodname' => 'lastgrade', 687 'arg' => 150.777779, 688 'expectations' => [ 689 'valid' => true, 690 'expectedvalue' => 150.77778 691 ] 692 ], 693 'Testing set_lastaccess with valid time' => [ 694 'methodname' => 'lastaccess', 695 'arg' => 4, 696 'expectations' => [ 697 'valid' => true 698 ] 699 ], 700 'Testing set_lastaccess with invalid time' => [ 701 'methodname' => 'lastaccess', 702 'arg' => -1, 703 'expectations' => [ 704 'valid' => false, 705 'exception' => \coding_exception::class, 706 'exceptionmessage' => 'Cannot set negative access time' 707 ] 708 ], 709 ]; 710 } 711 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body