[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 * IMS Enterprise enrolment tests. 19 * 20 * @package enrol_imsenterprise 21 * @category test 22 * @copyright 2012 David Monllaó 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 require_once($CFG->dirroot . '/enrol/imsenterprise/locallib.php'); 30 require_once($CFG->dirroot . '/enrol/imsenterprise/lib.php'); 31 32 /** 33 * IMS Enterprise test case 34 * 35 * @package enrol_imsenterprise 36 * @category test 37 * @copyright 2012 David Monllaó 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 class enrol_imsenterprise_testcase extends advanced_testcase { 41 42 /** 43 * @var $imsplugin enrol_imsenterprise_plugin IMS plugin instance. 44 */ 45 public $imsplugin; 46 47 /** 48 * Setup required for all tests. 49 */ 50 protected function setUp() { 51 $this->resetAfterTest(true); 52 $this->imsplugin = enrol_get_plugin('imsenterprise'); 53 $this->set_test_config(); 54 } 55 56 /** 57 * With an empty IMS enterprise file 58 */ 59 public function test_emptyfile() { 60 global $DB; 61 62 $prevncourses = $DB->count_records('course'); 63 $prevnusers = $DB->count_records('user'); 64 65 $this->set_xml_file(false, false); 66 $this->imsplugin->cron(); 67 68 $this->assertEquals($prevncourses, $DB->count_records('course')); 69 $this->assertEquals($prevnusers, $DB->count_records('user')); 70 } 71 72 /** 73 * Existing users are not created again 74 */ 75 public function test_users_existing() { 76 global $DB; 77 78 $user1 = $this->getDataGenerator()->create_user(); 79 $user2 = $this->getDataGenerator()->create_user(); 80 81 $prevnusers = $DB->count_records('user'); 82 83 $users = array($user1, $user2); 84 $this->set_xml_file($users); 85 $this->imsplugin->cron(); 86 87 $this->assertEquals($prevnusers, $DB->count_records('user')); 88 } 89 90 /** 91 * Add new users 92 */ 93 public function test_users_add() { 94 global $DB; 95 96 $prevnusers = $DB->count_records('user'); 97 98 $user1 = new StdClass(); 99 $user1->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 100 $user1->username = 'u1'; 101 $user1->email = 'u1@example.com'; 102 $user1->firstname = 'U'; 103 $user1->lastname = '1'; 104 105 $users = array($user1); 106 $this->set_xml_file($users); 107 $this->imsplugin->cron(); 108 109 $this->assertEquals(($prevnusers + 1), $DB->count_records('user')); 110 } 111 112 /** 113 * Add new users and set an auth type 114 */ 115 public function test_users_add_with_auth() { 116 global $DB; 117 118 $prevnusers = $DB->count_records('user'); 119 120 $user2 = new StdClass(); 121 $user2->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 122 $user2->username = 'u2'; 123 $user2->auth = 'cas'; 124 $user2->email = 'u2@u2.org'; 125 $user2->firstname = 'U'; 126 $user2->lastname = '2'; 127 128 $users = array($user2); 129 $this->set_xml_file($users); 130 $this->imsplugin->cron(); 131 132 $dbuser = $DB->get_record('user', array('username' => $user2->username)); 133 // TODO: MDL-15863 this needs more work due to multiauth changes, use first auth for now. 134 $dbauth = explode(',', $dbuser->auth); 135 $dbauth = reset($dbauth); 136 137 $this->assertEquals(($prevnusers + 1), $DB->count_records('user')); 138 $this->assertEquals($dbauth, $user2->auth); 139 } 140 141 142 /** 143 * Update user 144 */ 145 public function test_user_update() { 146 global $DB; 147 148 $user = $this->getDataGenerator()->create_user(array('idnumber' => 'test-update-user')); 149 $imsuser = new stdClass(); 150 $imsuser->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_UPDATE; 151 // THIS SHOULD WORK, surely?: $imsuser->username = $user->username; 152 // But this is required... 153 $imsuser->username = $user->idnumber; 154 $imsuser->email = 'u3@u3.org'; 155 $imsuser->firstname = 'U'; 156 $imsuser->lastname = '3'; 157 158 $this->set_xml_file(array($imsuser)); 159 $this->imsplugin->cron(); 160 $dbuser = $DB->get_record('user', array('id' => $user->id), '*', MUST_EXIST); 161 $this->assertEquals($imsuser->email, $dbuser->email); 162 $this->assertEquals($imsuser->firstname, $dbuser->firstname); 163 $this->assertEquals($imsuser->lastname, $dbuser->lastname); 164 } 165 166 public function test_user_update_disabled() { 167 global $DB; 168 169 $this->imsplugin->set_config('imsupdateusers', false); 170 171 $user = $this->getDataGenerator()->create_user(array('idnumber' => 'test-update-user')); 172 $imsuser = new stdClass(); 173 $imsuser->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_UPDATE; 174 // THIS SHOULD WORK, surely?: $imsuser->username = $user->username; 175 // But this is required... 176 $imsuser->username = $user->idnumber; 177 $imsuser->email = 'u3@u3.org'; 178 $imsuser->firstname = 'U'; 179 $imsuser->lastname = '3'; 180 181 $this->set_xml_file(array($imsuser)); 182 $this->imsplugin->cron(); 183 184 // Verify no changes have been made. 185 $dbuser = $DB->get_record('user', array('id' => $user->id), '*', MUST_EXIST); 186 $this->assertEquals($user->email, $dbuser->email); 187 $this->assertEquals($user->firstname, $dbuser->firstname); 188 $this->assertEquals($user->lastname, $dbuser->lastname); 189 } 190 191 /** 192 * Delete user 193 */ 194 public function test_user_delete() { 195 global $DB; 196 197 $this->imsplugin->set_config('imsdeleteusers', true); 198 $user = $this->getDataGenerator()->create_user(array('idnumber' => 'test-update-user')); 199 200 $imsuser = new stdClass(); 201 $imsuser->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_DELETE; 202 $imsuser->username = $user->username; 203 $imsuser->firstname = $user->firstname; 204 $imsuser->lastname = $user->lastname; 205 $imsuser->email = $user->email; 206 $this->set_xml_file(array($imsuser)); 207 208 $this->imsplugin->cron(); 209 $this->assertEquals(1, $DB->get_field('user', 'deleted', array('id' => $user->id), '*', MUST_EXIST)); 210 } 211 212 /** 213 * Delete user disabled 214 */ 215 public function test_user_delete_disabled() { 216 global $DB; 217 218 $this->imsplugin->set_config('imsdeleteusers', false); 219 $user = $this->getDataGenerator()->create_user(array('idnumber' => 'test-update-user')); 220 221 $imsuser = new stdClass(); 222 $imsuser->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_DELETE; 223 $imsuser->username = $user->username; 224 $imsuser->firstname = $user->firstname; 225 $imsuser->lastname = $user->lastname; 226 $imsuser->email = $user->email; 227 $this->set_xml_file(array($imsuser)); 228 229 $this->imsplugin->cron(); 230 $this->assertEquals(0, $DB->get_field('user', 'deleted', array('id' => $user->id), '*', MUST_EXIST)); 231 } 232 233 /** 234 * Existing courses are not created again 235 */ 236 public function test_courses_existing() { 237 global $DB; 238 239 $course1 = $this->getDataGenerator()->create_course(array('idnumber' => 'id1')); 240 $course2 = $this->getDataGenerator()->create_course(array('idnumber' => 'id2')); 241 242 // Default mapping according to default course attributes - IMS description tags mapping. 243 $course1->imsshort = $course1->fullname; 244 $course2->imsshort = $course2->fullname; 245 unset($course1->category); 246 unset($course2->category); 247 248 $prevncourses = $DB->count_records('course'); 249 250 $courses = array($course1, $course2); 251 $this->set_xml_file(false, $courses); 252 $this->imsplugin->cron(); 253 254 $this->assertEquals($prevncourses, $DB->count_records('course')); 255 } 256 257 /** 258 * Add new courses 259 */ 260 public function test_courses_add() { 261 global $DB; 262 263 $prevncourses = $DB->count_records('course'); 264 265 $course1 = new StdClass(); 266 $course1->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 267 $course1->idnumber = 'id1'; 268 $course1->imsshort = 'id1'; 269 $course1->category[] = 'DEFAULT CATNAME'; 270 271 $course2 = new StdClass(); 272 $course2->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 273 $course2->idnumber = 'id2'; 274 $course2->imsshort = 'id2'; 275 $course2->category[] = 'DEFAULT CATNAME'; 276 277 $courses = array($course1, $course2); 278 $this->set_xml_file(false, $courses); 279 $this->imsplugin->cron(); 280 281 $this->assertEquals(($prevncourses + 2), $DB->count_records('course')); 282 $this->assertTrue($DB->record_exists('course', array('idnumber' => $course1->idnumber))); 283 $this->assertTrue($DB->record_exists('course', array('idnumber' => $course2->idnumber))); 284 } 285 286 /** 287 * Verify that courses are not created when createnewcourses 288 * option is diabled. 289 */ 290 public function test_courses_add_createnewcourses_disabled() { 291 global $DB; 292 293 $this->imsplugin->set_config('createnewcourses', false); 294 $prevncourses = $DB->count_records('course'); 295 296 $course1 = new StdClass(); 297 $course1->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 298 $course1->idnumber = 'id1'; 299 $course1->imsshort = 'id1'; 300 $course1->category[] = 'DEFAULT CATNAME'; 301 302 $course2 = new StdClass(); 303 $course2->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 304 $course2->idnumber = 'id2'; 305 $course2->imsshort = 'id2'; 306 $course2->category[] = 'DEFAULT CATNAME'; 307 308 $courses = array($course1, $course2); 309 $this->set_xml_file(false, $courses); 310 $this->imsplugin->cron(); 311 312 $courses = array($course1, $course2); 313 $this->set_xml_file(false, $courses); 314 $this->imsplugin->cron(); 315 316 // Verify the courses have not ben creased. 317 $this->assertEquals($prevncourses , $DB->count_records('course')); 318 $this->assertFalse($DB->record_exists('course', array('idnumber' => $course1->idnumber))); 319 $this->assertFalse($DB->record_exists('course', array('idnumber' => $course2->idnumber))); 320 } 321 322 /** 323 * Test adding a course with no idnumber. 324 */ 325 public function test_courses_no_idnumber() { 326 global $DB; 327 328 $prevncourses = $DB->count_records('course'); 329 330 $course1 = new StdClass(); 331 $course1->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 332 $course1->idnumber = ''; 333 $course1->imsshort = 'id1'; 334 $course1->category[] = 'DEFAULT CATNAME'; 335 336 $this->set_xml_file(false, array($course1)); 337 $this->imsplugin->cron(); 338 339 // Verify no action. 340 $this->assertEquals($prevncourses, $DB->count_records('course')); 341 } 342 343 /** 344 * Add new course with the truncateidnumber setting. 345 */ 346 public function test_courses_add_truncate_idnumber() { 347 global $DB; 348 349 $truncatelength = 4; 350 351 $this->imsplugin->set_config('truncatecoursecodes', $truncatelength); 352 $prevncourses = $DB->count_records('course'); 353 354 $course1 = new StdClass(); 355 $course1->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 356 $course1->idnumber = '123456789'; 357 $course1->imsshort = 'id1'; 358 $course1->category[] = 'DEFAULT CATNAME'; 359 360 $this->set_xml_file(false, array($course1)); 361 $this->imsplugin->cron(); 362 363 // Verify the new course has been added. 364 $this->assertEquals(($prevncourses + 1), $DB->count_records('course')); 365 366 $truncatedidnumber = substr($course1->idnumber, 0, $truncatelength); 367 368 $this->assertTrue($DB->record_exists('course', array('idnumber' => $truncatedidnumber))); 369 } 370 371 /** 372 * Add new course without a category. 373 */ 374 public function test_course_add_default_category() { 375 global $DB, $CFG; 376 require_once($CFG->libdir.'/coursecatlib.php'); 377 378 $this->imsplugin->set_config('createnewcategories', false); 379 380 // Delete the default category, to ensure the plugin handles this gracefully. 381 $defaultcat = coursecat::get_default(); 382 $defaultcat->delete_full(false); 383 384 // Create an course with the IMS plugin without a category. 385 $course1 = new stdClass(); 386 $course1->idnumber = 'id1'; 387 $course1->imsshort = 'id1'; 388 $course1->category[] = ''; 389 $this->set_xml_file(false, array($course1)); 390 $this->imsplugin->cron(); 391 392 // Check the course has been created. 393 $dbcourse = $DB->get_record('course', array('idnumber' => $course1->idnumber), '*', MUST_EXIST); 394 // Check that it belongs to a category which exists. 395 $this->assertTrue($DB->record_exists('course_categories', array('id' => $dbcourse->category))); 396 } 397 398 /** 399 * Course attributes mapping to IMS enterprise group description tags 400 */ 401 public function test_courses_attrmapping() { 402 global $DB; 403 404 // Setting a all = coursecode (idnumber) mapping. 405 $this->imsplugin->set_config('imscoursemapshortname', 'coursecode'); 406 $this->imsplugin->set_config('imscoursemapfullname', 'coursecode'); 407 $this->imsplugin->set_config('imscoursemapsummary', 'coursecode'); 408 409 $course1 = new StdClass(); 410 $course1->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 411 $course1->idnumber = 'id1'; 412 $course1->imsshort = 'description_short1'; 413 $course1->imslong = 'description_long'; 414 $course1->imsfull = 'description_full'; 415 $course1->category[] = 'DEFAULT CATNAME'; 416 417 $this->set_xml_file(false, array($course1)); 418 $this->imsplugin->cron(); 419 420 $dbcourse = $DB->get_record('course', array('idnumber' => $course1->idnumber)); 421 $this->assertFalse(!$dbcourse); 422 $this->assertEquals($dbcourse->shortname, $course1->idnumber); 423 $this->assertEquals($dbcourse->fullname, $course1->idnumber); 424 $this->assertEquals($dbcourse->summary, $course1->idnumber); 425 426 // Setting a mapping using all the description tags. 427 $this->imsplugin->set_config('imscoursemapshortname', 'short'); 428 $this->imsplugin->set_config('imscoursemapfullname', 'long'); 429 $this->imsplugin->set_config('imscoursemapsummary', 'full'); 430 431 $course2 = new StdClass(); 432 $course2->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 433 $course2->idnumber = 'id2'; 434 $course2->imsshort = 'description_short2'; 435 $course2->imslong = 'description_long'; 436 $course2->imsfull = 'description_full'; 437 $course2->category[] = 'DEFAULT CATNAME'; 438 439 $this->set_xml_file(false, array($course2)); 440 $this->imsplugin->cron(); 441 442 $dbcourse = $DB->get_record('course', array('idnumber' => $course2->idnumber)); 443 $this->assertFalse(!$dbcourse); 444 $this->assertEquals($dbcourse->shortname, $course2->imsshort); 445 $this->assertEquals($dbcourse->fullname, $course2->imslong); 446 $this->assertEquals($dbcourse->summary, $course2->imsfull); 447 448 // Setting a mapping where the specified description tags doesn't exist in the XML file (must delegate into idnumber). 449 $this->imsplugin->set_config('imscoursemapshortname', 'short'); 450 $this->imsplugin->set_config('imscoursemapfullname', 'long'); 451 $this->imsplugin->set_config('imscoursemapsummary', 'full'); 452 453 $course3 = new StdClass(); 454 $course3->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 455 $course3->idnumber = 'id3'; 456 $course3->imsshort = 'description_short3'; 457 $course3->category[] = 'DEFAULT CATNAME'; 458 459 $this->set_xml_file(false, array($course3)); 460 $this->imsplugin->cron(); 461 462 $dbcourse = $DB->get_record('course', array('idnumber' => $course3->idnumber)); 463 $this->assertFalse(!$dbcourse); 464 $this->assertEquals($dbcourse->shortname, $course3->imsshort); 465 $this->assertEquals($dbcourse->fullname, $course3->idnumber); 466 $this->assertEquals($dbcourse->summary, $course3->idnumber); 467 468 } 469 470 /** 471 * Course updates 472 */ 473 public function test_course_update() { 474 global $DB; 475 476 $course4 = new StdClass(); 477 $course4->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 478 $course4->idnumber = 'id4'; 479 $course4->imsshort = 'id4'; 480 $course4->imsfull = 'id4'; 481 $course4->category[] = 'DEFAULT CATNAME'; 482 483 $this->set_xml_file(false, array($course4)); 484 $this->imsplugin->cron(); 485 486 $course4u = $DB->get_record('course', array('idnumber' => $course4->idnumber)); 487 488 $course4u->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_UPDATE; 489 $course4u->imsshort = 'description_short_updated'; 490 $course4u->imsfull = 'description_full_updated'; 491 unset($course4u->category); 492 493 $this->set_xml_file(false, array($course4u)); 494 $this->imsplugin->cron(); 495 496 $dbcourse = $DB->get_record('course', array('idnumber' => $course4->idnumber)); 497 $this->assertFalse(!$dbcourse); 498 $this->assertEquals($dbcourse->shortname, $course4u->imsshort); 499 $this->assertEquals($dbcourse->fullname, $course4u->imsfull); 500 } 501 502 /** 503 * Course delete. Make it hidden. 504 */ 505 public function test_course_delete() { 506 global $DB; 507 508 $course8 = new StdClass(); 509 $course8->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 510 $course8->idnumber = 'id8'; 511 $course8->imsshort = 'id8'; 512 $course8->imsfull = 'id8'; 513 $course8->category[] = 'DEFAULT CATNAME'; 514 515 $this->set_xml_file(false, array($course8)); 516 $this->imsplugin->cron(); 517 518 $course8d = $DB->get_record('course', array('idnumber' => $course8->idnumber)); 519 $this->assertEquals($course8d->visible, 1); 520 521 $course8d->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_DELETE; 522 unset($course8d->category); 523 524 $this->set_xml_file(false, array($course8d)); 525 $this->imsplugin->cron(); 526 527 $dbcourse = $DB->get_record('course', array('idnumber' => $course8d->idnumber)); 528 $this->assertFalse(!$dbcourse); 529 $this->assertEquals($dbcourse->visible, 0); 530 } 531 532 533 /** 534 * Nested categories with name during course creation 535 */ 536 public function test_nested_categories() { 537 global $DB; 538 539 $this->imsplugin->set_config('nestedcategories', true); 540 541 $topcat = 'DEFAULT CATNAME'; 542 $subcat = 'DEFAULT SUB CATNAME'; 543 544 $course5 = new StdClass(); 545 $course5->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 546 $course5->idnumber = 'id5'; 547 $course5->imsshort = 'description_short'; 548 $course5->imslong = 'description_long'; 549 $course5->imsfull = 'description_full'; 550 $course5->category = array(); 551 $course5->category[] = $topcat; 552 $course5->category[] = $subcat; 553 554 $this->set_xml_file(false, array($course5)); 555 $this->imsplugin->cron(); 556 557 $parentcatid = $DB->get_field('course_categories', 'id', array('name' => $topcat)); 558 $subcatid = $DB->get_field('course_categories', 'id', array('name' => $subcat, 'parent' => $parentcatid)); 559 560 $this->assertTrue(isset($subcatid)); 561 $this->assertTrue($subcatid > 0); 562 563 $topcat = 'DEFAULT CATNAME'; 564 $subcat = 'DEFAULT SUB CATNAME TEST2'; 565 566 $course6 = new StdClass(); 567 $course6->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 568 $course6->idnumber = 'id6'; 569 $course6->imsshort = 'description_short'; 570 $course6->imslong = 'description_long'; 571 $course6->imsfull = 'description_full'; 572 $course6->category = array(); 573 $course6->category[] = $topcat; 574 $course6->category[] = $subcat; 575 576 $this->set_xml_file(false, array($course6)); 577 $this->imsplugin->cron(); 578 579 $parentcatid = $DB->get_field('course_categories', 'id', array('name' => $topcat)); 580 $subcatid = $DB->get_field('course_categories', 'id', array('name' => $subcat, 'parent' => $parentcatid)); 581 582 $this->assertTrue(isset($subcatid)); 583 $this->assertTrue($subcatid > 0); 584 } 585 586 587 /** 588 * Test that duplicate nested categories with name are not created 589 */ 590 public function test_nested_categories_for_dups() { 591 global $DB; 592 593 $this->imsplugin->set_config('nestedcategories', true); 594 595 $topcat = 'DEFAULT CATNAME'; 596 $subcat = 'DEFAULT SUB CATNAME DUPTEST'; 597 598 $course7 = new StdClass(); 599 $course7->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 600 $course7->idnumber = 'id7'; 601 $course7->imsshort = 'description_short'; 602 $course7->imslong = 'description_long'; 603 $course7->imsfull = 'description_full'; 604 $course7->category[] = $topcat; 605 $course7->category[] = $subcat; 606 607 $this->set_xml_file(false, array($course7)); 608 $this->imsplugin->cron(); 609 610 $prevncategories = $DB->count_records('course_categories'); 611 612 $course8 = new StdClass(); 613 $course8->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 614 $course8->idnumber = 'id8'; 615 $course8->imsshort = 'description_short'; 616 $course8->imslong = 'description_long'; 617 $course8->imsfull = 'description_full'; 618 $course8->category[] = $topcat; 619 $course8->category[] = $subcat; 620 621 $this->set_xml_file(false, array($course8)); 622 $this->imsplugin->cron(); 623 624 $this->assertEquals($prevncategories, $DB->count_records('course_categories')); 625 } 626 627 /** 628 * Nested categories with idnumber during course creation 629 */ 630 public function test_nested_categories_idnumber() { 631 global $DB; 632 633 $this->imsplugin->set_config('nestedcategories', true); 634 $this->imsplugin->set_config('categoryidnumber', true); 635 $this->imsplugin->set_config('categoryseparator', '|'); 636 637 $catsep = trim($this->imsplugin->get_config('categoryseparator')); 638 639 $topcatname = 'DEFAULT CATNAME'; 640 $subcatname = 'DEFAULT SUB CATNAME'; 641 $topcatidnumber = '01'; 642 $subcatidnumber = '0101'; 643 644 $topcat = $topcatname.$catsep.$topcatidnumber; 645 $subcat = $subcatname.$catsep.$subcatidnumber; 646 647 $course1 = new StdClass(); 648 $course1->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 649 $course1->idnumber = 'id5'; 650 $course1->imsshort = 'description_short'; 651 $course1->imslong = 'description_long'; 652 $course1->imsfull = 'description_full'; 653 $course1->category[] = $topcat; 654 $course1->category[] = $subcat; 655 656 $this->set_xml_file(false, array($course1)); 657 $this->imsplugin->cron(); 658 659 $parentcatid = $DB->get_field('course_categories', 'id', array('idnumber' => $topcatidnumber)); 660 $subcatid = $DB->get_field('course_categories', 'id', array('idnumber' => $subcatidnumber, 'parent' => $parentcatid)); 661 662 $this->assertTrue(isset($subcatid)); 663 $this->assertTrue($subcatid > 0); 664 665 // Change the category separator character. 666 $this->imsplugin->set_config('categoryseparator', ':'); 667 668 $catsep = trim($this->imsplugin->get_config('categoryseparator')); 669 670 $topcatname = 'DEFAULT CATNAME'; 671 $subcatname = 'DEFAULT SUB CATNAME TEST2'; 672 $topcatidnumber = '01'; 673 $subcatidnumber = '0102'; 674 675 $topcat = $topcatname.$catsep.$topcatidnumber; 676 $subcat = $subcatname.$catsep.$subcatidnumber; 677 678 $course2 = new StdClass(); 679 $course2->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 680 $course2->idnumber = 'id6'; 681 $course2->imsshort = 'description_short'; 682 $course2->imslong = 'description_long'; 683 $course2->imsfull = 'description_full'; 684 $course2->category[] = $topcat; 685 $course2->category[] = $subcat; 686 687 $this->set_xml_file(false, array($course2)); 688 $this->imsplugin->cron(); 689 690 $parentcatid = $DB->get_field('course_categories', 'id', array('idnumber' => $topcatidnumber)); 691 $subcatid = $DB->get_field('course_categories', 'id', array('idnumber' => $subcatidnumber, 'parent' => $parentcatid)); 692 693 $this->assertTrue(isset($subcatid)); 694 $this->assertTrue($subcatid > 0); 695 } 696 697 /** 698 * Test that duplicate nested categories with idnumber are not created 699 */ 700 public function test_nested_categories_idnumber_for_dups() { 701 global $DB; 702 703 $this->imsplugin->set_config('nestedcategories', true); 704 $this->imsplugin->set_config('categoryidnumber', true); 705 $this->imsplugin->set_config('categoryseparator', '|'); 706 707 $catsep = trim($this->imsplugin->get_config('categoryseparator')); 708 709 $topcatname = 'DEFAULT CATNAME'; 710 $subcatname = 'DEFAULT SUB CATNAME'; 711 $topcatidnumber = '01'; 712 $subcatidnumber = '0101'; 713 714 $topcat = $topcatname.$catsep.$topcatidnumber; 715 $subcat = $subcatname.$catsep.$subcatidnumber; 716 717 $course1 = new StdClass(); 718 $course1->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 719 $course1->idnumber = 'id1'; 720 $course1->imsshort = 'description_short'; 721 $course1->imslong = 'description_long'; 722 $course1->imsfull = 'description_full'; 723 $course1->category[] = $topcat; 724 $course1->category[] = $subcat; 725 726 $this->set_xml_file(false, array($course1)); 727 $this->imsplugin->cron(); 728 729 $prevncategories = $DB->count_records('course_categories'); 730 731 $course2 = new StdClass(); 732 $course2->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 733 $course2->idnumber = 'id2'; 734 $course2->imsshort = 'description_short'; 735 $course2->imslong = 'description_long'; 736 $course2->imsfull = 'description_full'; 737 $course2->category[] = $topcat; 738 $course2->category[] = $subcat; 739 740 $this->set_xml_file(false, array($course2)); 741 $this->imsplugin->cron(); 742 743 $this->assertEquals($prevncategories, $DB->count_records('course_categories')); 744 } 745 746 /** 747 * Test that nested categories with idnumber is not created if name is missing 748 */ 749 public function test_categories_idnumber_missing_name() { 750 global $DB, $CFG; 751 752 $this->imsplugin->set_config('nestedcategories', true); 753 $this->imsplugin->set_config('categoryidnumber', true); 754 $this->imsplugin->set_config('categoryseparator', '|'); 755 $catsep = trim($this->imsplugin->get_config('categoryseparator')); 756 757 $topcatname = 'DEFAULT CATNAME'; 758 $subcatname = ''; 759 $topcatidnumber = '01'; 760 $subcatidnumber = '0101'; 761 762 $topcat = $topcatname.$catsep.$topcatidnumber; 763 $subcat = $subcatname.$catsep.$subcatidnumber; 764 765 $course1 = new StdClass(); 766 $course1->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 767 $course1->idnumber = 'id1'; 768 $course1->imsshort = 'description_short'; 769 $course1->imslong = 'description_long'; 770 $course1->imsfull = 'description_full'; 771 $course1->category[] = $topcat; 772 $course1->category[] = $subcat; 773 774 $this->set_xml_file(false, array($course1)); 775 $this->imsplugin->cron(); 776 777 // Check all categories except the last subcategory was created. 778 $parentcatid = $DB->get_field('course_categories', 'id', array('idnumber' => $topcatidnumber)); 779 $this->assertTrue((boolean)$parentcatid); 780 $subcatid = $DB->get_field('course_categories', 'id', array('idnumber' => $subcatidnumber, 'parent' => $parentcatid)); 781 $this->assertFalse((boolean)$subcatid); 782 783 // Check course was put in default category. 784 $defaultcat = coursecat::get_default(); 785 $dbcourse = $DB->get_record('course', array('idnumber' => $course1->idnumber), '*', MUST_EXIST); 786 $this->assertEquals($dbcourse->category, $defaultcat->id); 787 788 } 789 790 /** 791 * Create category with name (nested categories not activated). 792 */ 793 public function test_create_category_name_no_nested() { 794 global $DB; 795 796 $course = new StdClass(); 797 $course->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 798 $course->idnumber = 'id'; 799 $course->imsshort = 'description_short'; 800 $course->imslong = 'description_long'; 801 $course->imsfull = 'description_full'; 802 $course->category[] = 'CATNAME'; 803 804 $this->set_xml_file(false, array($course)); 805 $this->imsplugin->cron(); 806 807 $dbcat = $DB->get_record('course_categories', array('name' => $course->category[0])); 808 $this->assertFalse(!$dbcat); 809 $this->assertEquals($dbcat->parent, 0); 810 811 $dbcourse = $DB->get_record('course', array('idnumber' => $course->idnumber)); 812 $this->assertFalse(!$dbcourse); 813 $this->assertEquals($dbcourse->category, $dbcat->id); 814 815 } 816 817 /** 818 * Find a category with name (nested categories not activated). 819 */ 820 public function test_find_category_name_no_nested() { 821 global $DB; 822 823 $cattop = $this->getDataGenerator()->create_category(array('name' => 'CAT-TOP')); 824 $catsub = $this->getDataGenerator()->create_category(array('name' => 'CAT-SUB', 'parent' => $cattop->id)); 825 $prevcats = $DB->count_records('course_categories'); 826 827 $course = new StdClass(); 828 $course->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 829 $course->idnumber = 'id'; 830 $course->imsshort = 'description_short'; 831 $course->imslong = 'description_long'; 832 $course->imsfull = 'description_full'; 833 $course->category[] = 'CAT-SUB'; 834 835 $this->set_xml_file(false, array($course)); 836 $this->imsplugin->cron(); 837 838 $newcats = $DB->count_records('course_categories'); 839 840 // Check that no new category was not created. 841 $this->assertEquals($prevcats, $newcats); 842 843 // Check course is associated to CAT-SUB. 844 $dbcourse = $DB->get_record('course', array('idnumber' => $course->idnumber)); 845 $this->assertFalse(!$dbcourse); 846 $this->assertEquals($dbcourse->category, $catsub->id); 847 848 } 849 850 /** 851 * Create category with idnumber (nested categories not activated). 852 */ 853 public function test_create_category_idnumber_no_nested() { 854 global $DB; 855 856 $this->imsplugin->set_config('categoryidnumber', true); 857 $this->imsplugin->set_config('categoryseparator', '|'); 858 $catsep = trim($this->imsplugin->get_config('categoryseparator')); 859 860 $course = new StdClass(); 861 $course->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 862 $course->idnumber = 'id'; 863 $course->imsshort = 'description_short'; 864 $course->imslong = 'description_long'; 865 $course->imsfull = 'description_full'; 866 $course->category[] = 'CATNAME'. $catsep . 'CATIDNUMBER'; 867 868 $this->set_xml_file(false, array($course)); 869 $this->imsplugin->cron(); 870 871 $dbcat = $DB->get_record('course_categories', array('idnumber' => 'CATIDNUMBER')); 872 $this->assertFalse(!$dbcat); 873 $this->assertEquals($dbcat->parent, 0); 874 $this->assertEquals($dbcat->name, 'CATNAME'); 875 876 $dbcourse = $DB->get_record('course', array('idnumber' => $course->idnumber)); 877 $this->assertFalse(!$dbcourse); 878 $this->assertEquals($dbcourse->category, $dbcat->id); 879 880 } 881 882 /** 883 * Find a category with idnumber (nested categories not activated). 884 */ 885 public function test_find_category_idnumber_no_nested() { 886 global $DB; 887 888 $this->imsplugin->set_config('categoryidnumber', true); 889 $this->imsplugin->set_config('categoryseparator', '|'); 890 $catsep = trim($this->imsplugin->get_config('categoryseparator')); 891 892 $topcatname = 'CAT-TOP'; 893 $subcatname = 'CAT-SUB'; 894 $topcatidnumber = 'ID-TOP'; 895 $subcatidnumber = 'ID-SUB'; 896 897 $cattop = $this->getDataGenerator()->create_category(array('name' => $topcatname, 'idnumber' => $topcatidnumber)); 898 $catsub = $this->getDataGenerator()->create_category(array('name' => $subcatname, 'idnumber' => $subcatidnumber, 899 'parent' => $cattop->id)); 900 $prevcats = $DB->count_records('course_categories'); 901 902 $course = new StdClass(); 903 $course->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 904 $course->idnumber = 'id'; 905 $course->imsshort = 'description_short'; 906 $course->imslong = 'description_long'; 907 $course->imsfull = 'description_full'; 908 $course->category[] = $subcatname . $catsep . $subcatidnumber; 909 910 $this->set_xml_file(false, array($course)); 911 $this->imsplugin->cron(); 912 913 $newcats = $DB->count_records('course_categories'); 914 915 // Check that no new category was not created. 916 $this->assertEquals($prevcats, $newcats); 917 918 $dbcourse = $DB->get_record('course', array('idnumber' => $course->idnumber)); 919 $this->assertFalse(!$dbcourse); 920 $this->assertEquals($dbcourse->category, $catsub->id); 921 922 } 923 924 /** 925 * Test that category with idnumber is not created if name is missing (nested categories not activated). 926 */ 927 public function test_category_idnumber_missing_name_no_nested() { 928 global $DB; 929 930 $this->imsplugin->set_config('categoryidnumber', true); 931 $this->imsplugin->set_config('categoryseparator', '|'); 932 $catsep = trim($this->imsplugin->get_config('categoryseparator')); 933 934 $catidnumber = '01'; 935 936 $course = new StdClass(); 937 $course->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD; 938 $course->idnumber = 'id1'; 939 $course->imsshort = 'description_short'; 940 $course->imslong = 'description_long'; 941 $course->imsfull = 'description_full'; 942 $course->category[] = '' . $catsep . $catidnumber; 943 944 $this->set_xml_file(false, array($course)); 945 $this->imsplugin->cron(); 946 947 // Check category was not created. 948 $catid = $DB->get_record('course_categories', array('idnumber' => $catidnumber)); 949 $this->assertFalse($catid); 950 951 // Check course was put in default category. 952 $defaultcat = coursecat::get_default(); 953 $dbcourse = $DB->get_record('course', array('idnumber' => $course->idnumber), '*', MUST_EXIST); 954 $this->assertEquals($dbcourse->category, $defaultcat->id); 955 956 } 957 958 /** 959 * Sets the plugin configuration for testing 960 */ 961 public function set_test_config() { 962 $this->imsplugin->set_config('mailadmins', false); 963 $this->imsplugin->set_config('prev_path', ''); 964 $this->imsplugin->set_config('createnewusers', true); 965 $this->imsplugin->set_config('imsupdateusers', true); 966 $this->imsplugin->set_config('createnewcourses', true); 967 $this->imsplugin->set_config('updatecourses', true); 968 $this->imsplugin->set_config('createnewcategories', true); 969 $this->imsplugin->set_config('categoryseparator', ''); 970 $this->imsplugin->set_config('categoryidnumber', false); 971 $this->imsplugin->set_config('nestedcategories', false); 972 } 973 974 /** 975 * Creates an IMS enterprise XML file and adds it's path to config settings. 976 * 977 * @param bool|array $users false or array of users StdClass 978 * @param bool|array $courses false or of courses StdClass 979 */ 980 public function set_xml_file($users = false, $courses = false) { 981 982 $xmlcontent = '<enterprise>'; 983 984 // Users. 985 if (!empty($users)) { 986 foreach ($users as $user) { 987 $xmlcontent .= ' 988 <person'; 989 990 // Optional recstatus (1=add, 2=update, 3=delete). 991 if (!empty($user->recstatus)) { 992 $xmlcontent .= ' recstatus="'.$user->recstatus.'"'; 993 } 994 995 $xmlcontent .= '> 996 <sourcedid> 997 <source>TestSource</source> 998 <id>'.$user->username.'</id> 999 </sourcedid> 1000 <userid'; 1001 1002 // Optional authentication type. 1003 if (!empty($user->auth)) { 1004 $xmlcontent .= ' authenticationtype="'.$user->auth.'"'; 1005 } 1006 1007 $xmlcontent .= '>'.$user->username.'</userid> 1008 <name> 1009 <fn>'.$user->firstname.' '.$user->lastname.'</fn> 1010 <n> 1011 <family>'.$user->lastname.'</family> 1012 <given>'.$user->firstname.'</given> 1013 </n> 1014 </name> 1015 <email>'.$user->email.'</email> 1016 </person>'; 1017 } 1018 } 1019 1020 // Courses. 1021 // Mapping based on default course attributes - IMS group tags mapping. 1022 if (!empty($courses)) { 1023 foreach ($courses as $course) { 1024 1025 $xmlcontent .= ' 1026 <group'; 1027 1028 // Optional recstatus (1=add, 2=update, 3=delete). 1029 if (!empty($course->recstatus)) { 1030 $xmlcontent .= ' recstatus="'.$course->recstatus.'"'; 1031 } 1032 1033 $xmlcontent .= '> 1034 <sourcedid> 1035 <source>TestSource</source> 1036 <id>'.$course->idnumber.'</id> 1037 </sourcedid> 1038 <description>'; 1039 1040 // Optional to test course attributes mappings. 1041 if (!empty($course->imsshort)) { 1042 $xmlcontent .= ' 1043 <short>'.$course->imsshort.'</short>'; 1044 } 1045 1046 // Optional to test course attributes mappings. 1047 if (!empty($course->imslong)) { 1048 $xmlcontent .= ' 1049 <long>'.$course->imslong.'</long>'; 1050 } 1051 1052 // Optional to test course attributes mappings. 1053 if (!empty($course->imsfull)) { 1054 $xmlcontent .= ' 1055 <full>'.$course->imsfull.'</full>'; 1056 } 1057 1058 // The orgunit tag value is used by moodle as category name. 1059 $xmlcontent .= ' 1060 </description> 1061 <org>'; 1062 // Optional category name. 1063 if (isset($course->category) && !empty($course->category)) { 1064 foreach ($course->category as $category) { 1065 $xmlcontent .= ' 1066 <orgunit>'.$category.'</orgunit>'; 1067 } 1068 } 1069 1070 $xmlcontent .= ' 1071 </org> 1072 </group>'; 1073 } 1074 } 1075 1076 $xmlcontent .= ' 1077 </enterprise>'; 1078 1079 // Creating the XML file. 1080 $filename = 'ims_' . rand(1000, 9999) . '.xml'; 1081 $tmpdir = make_temp_directory('enrol_imsenterprise'); 1082 $xmlfilepath = $tmpdir . '/' . $filename; 1083 file_put_contents($xmlfilepath, $xmlcontent); 1084 1085 // Setting the file path in CFG. 1086 $this->imsplugin->set_config('imsfilelocation', $xmlfilepath); 1087 } 1088 1089 /** 1090 * IMS Enterprise enrolment task test. 1091 */ 1092 public function test_imsenterprise_cron_task() { 1093 global $DB; 1094 $prevnusers = $DB->count_records('user'); 1095 1096 $user1 = new StdClass(); 1097 $user1->username = 'u1'; 1098 $user1->email = 'u1@example.com'; 1099 $user1->firstname = 'U'; 1100 $user1->lastname = '1'; 1101 1102 $users = array($user1); 1103 $this->set_xml_file($users); 1104 1105 $task = new enrol_imsenterprise\task\cron_task(); 1106 $task->execute(); 1107 1108 $this->assertEquals(($prevnusers + 1), $DB->count_records('user')); 1109 } 1110 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |