MDL-31356 enrol_imsenterprise: Fix behaviour with course idnumber
[moodle.git] / enrol / imsenterprise / tests / imsenterprise_test.php
CommitLineData
2b8f692f
DM
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
295e4fa8 21 * @category test
2b8f692f
DM
22 * @copyright 2012 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->dirroot . '/enrol/imsenterprise/locallib.php');
30require_once($CFG->dirroot . '/enrol/imsenterprise/lib.php');
31
32/**
33 * IMS Enterprise test case
34 *
35 * @package enrol_imsenterprise
295e4fa8 36 * @category test
2b8f692f
DM
37 * @copyright 2012 David MonllaĆ³
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class enrol_imsenterprise_testcase extends advanced_testcase {
41
295e4fa8
DP
42 /**
43 * @var $imsplugin enrol_imsenterprise_plugin IMS plugin instance.
44 */
7cbb5070 45 public $imsplugin;
2b8f692f 46
295e4fa8
DP
47 /**
48 * Setup required for all tests.
49 */
2b8f692f
DM
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
2b8f692f
DM
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
2b8f692f
DM
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();
93fdbb42 99 $user1->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
2b8f692f 100 $user1->username = 'u1';
0fe86bbd 101 $user1->email = 'u1@example.com';
2b8f692f
DM
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
93fdbb42
BB
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 $user3 = new StdClass();
149 $user3->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
150 $user3->username = 'u3';
151 $user3->email = 'u3@u3.org';
152 $user3->firstname = 'U';
153 $user3->lastname = '3';
154
155 $users = array($user3);
156 $this->set_xml_file($users);
157 $this->imsplugin->cron();
158
159 $user3u = $DB->get_record('user', array('username' => $user3->username));
160
161 $user3u->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_UPDATE;
162 $user3u->email = 'updated_u3@updated_u3.org';
163 $user3u->firstname = 'updated_U';
164 $user3u->lastname = 'updated_3';
165
166 $users = array($user3u);
167 $this->set_xml_file($users);
168 $this->imsplugin->cron();
169
170 $dbuser = $DB->get_record('user', array('username' => $user3->username));
171
172 $this->assertEquals($dbuser->email, $user3u->email);
173 $this->assertEquals($dbuser->firstname, $user3u->firstname);
174 $this->assertEquals($dbuser->lastname, $user3u->lastname);
175 }
176
ecf6a0ab
DP
177 /**
178 * Update user disabled
179 */
180 public function test_user_update_disabled() {
181 global $DB;
182
183 $this->imsplugin->set_config('imsupdateusers', false);
184 $user = $this->getDataGenerator()->create_user(array('idnumber' => 'test-update-user'));
185 $imsuser = new stdClass();
186 $imsuser ->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_UPDATE;
187 // THIS SHOULD WORK, surely?: $imsuser->username = $user->username;
188 // But this is required...
189 $imsuser->username = $user->idnumber;
190 $imsuser->email = 'u3@u3.org';
191 $imsuser->firstname = 'U';
192 $imsuser->lastname = '3';
193 $this->set_xml_file(array($imsuser));
194 $this->imsplugin->cron();
195 // Verify no changes have been made.
196 $dbuser = $DB->get_record('user', array('id' => $user->id), '*', MUST_EXIST);
197 $this->assertEquals($user->email, $dbuser->email);
198 $this->assertEquals($user->firstname, $dbuser->firstname);
199 $this->assertEquals($user->lastname, $dbuser->lastname);
200 }
201
202 /**
203 * Delete user
204 */
205 public function test_user_delete() {
206 global $DB;
207
208 $this->imsplugin->set_config('imsdeleteusers', true);
209 $user = $this->getDataGenerator()->create_user(array('idnumber' => 'test-update-user'));
210 $imsuser = new stdClass();
211 $imsuser->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_DELETE;
212 $imsuser->username = $user->username;
213 $imsuser->firstname = $user->firstname;
214 $imsuser->lastname = $user->lastname;
215 $imsuser->email = $user->email;
216 $this->set_xml_file(array($imsuser));
217 $this->imsplugin->cron();
218 $this->assertEquals(1, $DB->get_field('user', 'deleted', array('id' => $user->id), '*', MUST_EXIST));
219 }
220
221 /**
222 * Delete user disabled
223 */
224 public function test_user_delete_disabled() {
225 global $DB;
226 $this->imsplugin->set_config('imsdeleteusers', false);
227 $user = $this->getDataGenerator()->create_user(array('idnumber' => 'test-update-user'));
228 $imsuser = new stdClass();
229 $imsuser->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_DELETE;
230 $imsuser->username = $user->username;
231 $imsuser->firstname = $user->firstname;
232 $imsuser->lastname = $user->lastname;
233 $imsuser->email = $user->email;
234 $this->set_xml_file(array($imsuser));
235 $this->imsplugin->cron();
236 $this->assertEquals(0, $DB->get_field('user', 'deleted', array('id' => $user->id), '*', MUST_EXIST));
237 }
238
93fdbb42 239
2b8f692f
DM
240 /**
241 * Existing courses are not created again
242 */
243 public function test_courses_existing() {
244 global $DB;
245
246 $course1 = $this->getDataGenerator()->create_course(array('idnumber' => 'id1'));
247 $course2 = $this->getDataGenerator()->create_course(array('idnumber' => 'id2'));
248
249 // Default mapping according to default course attributes - IMS description tags mapping.
250 $course1->imsshort = $course1->fullname;
251 $course2->imsshort = $course2->fullname;
252
253 $prevncourses = $DB->count_records('course');
254
255 $courses = array($course1, $course2);
256 $this->set_xml_file(false, $courses);
257 $this->imsplugin->cron();
258
259 $this->assertEquals($prevncourses, $DB->count_records('course'));
260 }
261
2b8f692f
DM
262 /**
263 * Add new courses
264 */
265 public function test_courses_add() {
266 global $DB;
267
268 $prevncourses = $DB->count_records('course');
269
270 $course1 = new StdClass();
93fdbb42 271 $course1->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
2b8f692f
DM
272 $course1->idnumber = 'id1';
273 $course1->imsshort = 'id1';
274 $course1->category = 'DEFAULT CATNAME';
275
276 $course2 = new StdClass();
93fdbb42 277 $course2->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
2b8f692f
DM
278 $course2->idnumber = 'id2';
279 $course2->imsshort = 'id2';
280 $course2->category = 'DEFAULT CATNAME';
281
282 $courses = array($course1, $course2);
283 $this->set_xml_file(false, $courses);
284 $this->imsplugin->cron();
285
286 $this->assertEquals(($prevncourses + 2), $DB->count_records('course'));
ecf6a0ab
DP
287 $this->assertTrue($DB->record_exists('course', array('idnumber' => $course1->idnumber)));
288 $this->assertTrue($DB->record_exists('course', array('idnumber' => $course2->idnumber)));
289 }
290
291 /**
292 * Verify that courses are not created when createnewcourses
293 * option is diabled.
294 */
295 public function test_courses_add_createnewcourses_disabled() {
296 global $DB;
297
298 $this->imsplugin->set_config('createnewcourses', false);
299 $prevncourses = $DB->count_records('course');
300 $course1 = new StdClass();
301 $course1->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
302 $course1->idnumber = 'id1';
303 $course1->imsshort = 'id1';
304 $course1->category = 'DEFAULT CATNAME';
305 $course2 = new StdClass();
306 $course2->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
307 $course2->idnumber = 'id2';
308 $course2->imsshort = 'id2';
309 $course2->category = 'DEFAULT CATNAME';
310 $courses = array($course1, $course2);
311 $this->set_xml_file(false, $courses);
312 $this->imsplugin->cron();
313 // Verify the courses have not ben creased.
314 $this->assertEquals($prevncourses , $DB->count_records('course'));
315 $this->assertFalse($DB->record_exists('course', array('idnumber' => $course1->idnumber)));
316 $this->assertFalse($DB->record_exists('course', array('idnumber' => $course2->idnumber)));
317 }
318
319 /**
320 * Test adding a course with no idnumber.
321 */
322 public function test_courses_no_idnumber() {
323 global $DB;
324 $prevncourses = $DB->count_records('course');
325 $course1 = new StdClass();
326 $course1->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
327 $course1->idnumber = '';
328 $course1->imsshort = 'id1';
329 $course1->category = 'DEFAULT CATNAME';
330 $this->set_xml_file(false, array($course1));
331 $this->imsplugin->cron();
332 // Verify no action.
333 $this->assertEquals($prevncourses, $DB->count_records('course'));
334 }
335
336 /**
337 * Add new course with the truncateidnumber setting.
338 */
339 public function test_courses_add_truncate_idnumber() {
340 global $DB;
341
342 $truncatelength = 4;
343
344 $this->imsplugin->set_config('truncatecoursecodes', $truncatelength);
345 $prevncourses = $DB->count_records('course');
346
347 $course1 = new StdClass();
348 $course1->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
349 $course1->idnumber = '123456789';
350 $course1->imsshort = 'id1';
351 $course1->category = 'DEFAULT CATNAME';
352
353 $this->set_xml_file(false, array($course1));
354 $this->imsplugin->cron();
355
356 // Verify the new course has been added.
357 $this->assertEquals(($prevncourses + 1), $DB->count_records('course'));
358
359 $truncatedidnumber = substr($course1->idnumber, 0, $truncatelength);
360
361 $this->assertTrue($DB->record_exists('course', array('idnumber' => $truncatedidnumber)));
2b8f692f
DM
362 }
363
259e7e62
DP
364 /**
365 * Add new course without a category.
366 */
367 public function test_course_add_default_category() {
368 global $DB, $CFG;
369 require_once($CFG->libdir.'/coursecatlib.php');
370
371 $this->imsplugin->set_config('createnewcategories', false);
372
373 // Delete the default category, to ensure the plugin handles this gracefully.
374 $defaultcat = coursecat::get_default();
375 $defaultcat->delete_full(false);
376
377 // Create an course with the IMS plugin without a category.
378 $course1 = new stdClass();
379 $course1->idnumber = 'id1';
380 $course1->imsshort = 'id1';
381 $course1->category = '';
382 $this->set_xml_file(false, array($course1));
383 $this->imsplugin->cron();
384
385 // Check the course has been created.
386 $dbcourse = $DB->get_record('course', array('idnumber' => $course1->idnumber), '*', MUST_EXIST);
387 // Check that it belongs to a category which exists.
388 $this->assertTrue($DB->record_exists('course_categories', array('id' => $dbcourse->category)));
389 }
2b8f692f 390
c25c3c07
DM
391 /**
392 * Course attributes mapping to IMS enterprise group description tags
393 */
394 public function test_courses_attrmapping() {
395 global $DB;
396
397 // Setting a all = coursecode (idnumber) mapping.
398 $this->imsplugin->set_config('imscoursemapshortname', 'coursecode');
399 $this->imsplugin->set_config('imscoursemapfullname', 'coursecode');
400 $this->imsplugin->set_config('imscoursemapsummary', 'coursecode');
401
402 $course1 = new StdClass();
93fdbb42 403 $course1->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
c25c3c07
DM
404 $course1->idnumber = 'id1';
405 $course1->imsshort = 'description_short1';
406 $course1->imslong = 'description_long';
407 $course1->imsfull = 'description_full';
408 $course1->category = 'DEFAULT CATNAME';
409
410 $this->set_xml_file(false, array($course1));
411 $this->imsplugin->cron();
412
413 $dbcourse = $DB->get_record('course', array('idnumber' => $course1->idnumber));
414 $this->assertFalse(!$dbcourse);
415 $this->assertEquals($dbcourse->shortname, $course1->idnumber);
416 $this->assertEquals($dbcourse->fullname, $course1->idnumber);
417 $this->assertEquals($dbcourse->summary, $course1->idnumber);
418
c25c3c07
DM
419 // Setting a mapping using all the description tags.
420 $this->imsplugin->set_config('imscoursemapshortname', 'short');
421 $this->imsplugin->set_config('imscoursemapfullname', 'long');
422 $this->imsplugin->set_config('imscoursemapsummary', 'full');
423
424 $course2 = new StdClass();
93fdbb42 425 $course2->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
c25c3c07
DM
426 $course2->idnumber = 'id2';
427 $course2->imsshort = 'description_short2';
428 $course2->imslong = 'description_long';
429 $course2->imsfull = 'description_full';
430 $course2->category = 'DEFAULT CATNAME';
431
432 $this->set_xml_file(false, array($course2));
433 $this->imsplugin->cron();
434
435 $dbcourse = $DB->get_record('course', array('idnumber' => $course2->idnumber));
436 $this->assertFalse(!$dbcourse);
437 $this->assertEquals($dbcourse->shortname, $course2->imsshort);
438 $this->assertEquals($dbcourse->fullname, $course2->imslong);
439 $this->assertEquals($dbcourse->summary, $course2->imsfull);
440
c25c3c07
DM
441 // Setting a mapping where the specified description tags doesn't exist in the XML file (must delegate into idnumber).
442 $this->imsplugin->set_config('imscoursemapshortname', 'short');
443 $this->imsplugin->set_config('imscoursemapfullname', 'long');
444 $this->imsplugin->set_config('imscoursemapsummary', 'full');
445
446 $course3 = new StdClass();
93fdbb42 447 $course3->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
c25c3c07
DM
448 $course3->idnumber = 'id3';
449 $course3->imsshort = 'description_short3';
450 $course3->category = 'DEFAULT CATNAME';
451
452 $this->set_xml_file(false, array($course3));
453 $this->imsplugin->cron();
454
455 $dbcourse = $DB->get_record('course', array('idnumber' => $course3->idnumber));
456 $this->assertFalse(!$dbcourse);
457 $this->assertEquals($dbcourse->shortname, $course3->imsshort);
458 $this->assertEquals($dbcourse->fullname, $course3->idnumber);
459 $this->assertEquals($dbcourse->summary, $course3->idnumber);
460
461 }
462
93fdbb42
BB
463 /**
464 * Course updates
465 */
466 public function test_course_update() {
467 global $DB;
468
469 $course4 = new StdClass();
470 $course4->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
471 $course4->idnumber = 'id4';
472 $course4->imsshort = 'id4';
473 $course4->imsfull = 'id4';
474 $course4->category = 'DEFAULT CATNAME';
475
476 $this->set_xml_file(false, array($course4));
477 $this->imsplugin->cron();
478
479 $course4u = $DB->get_record('course', array('idnumber' => $course4->idnumber));
480
481 $course4u->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_UPDATE;
482 $course4u->imsshort = 'description_short_updated';
483 $course4u->imsfull = 'description_full_updated';
484
485 $this->set_xml_file(false, array($course4u));
486 $this->imsplugin->cron();
487
488 $dbcourse = $DB->get_record('course', array('idnumber' => $course4->idnumber));
489 $this->assertFalse(!$dbcourse);
490 $this->assertEquals($dbcourse->shortname, $course4u->imsshort);
491 $this->assertEquals($dbcourse->fullname, $course4u->imsfull);
492 }
493
494
495 /**
496 * Nested categories during course creation
497 */
498 public function test_nested_categories() {
499 global $DB;
500
501 $catsep = trim($this->imsplugin->get_config('categoryseparator'));
502
503 $topcat = 'DEFAULT CATNAME';
504 $subcat = 'DEFAULT SUB CATNAME';
505
506 $fullcat = $topcat.$catsep.$subcat;
507
508 $course5 = new StdClass();
509 $course5->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
510 $course5->idnumber = 'id5';
511 $course5->imsshort = 'description_short';
512 $course5->imslong = 'description_long';
513 $course5->imsfull = 'description_full';
514 $course5->category = $fullcat;
515
516 $this->set_xml_file(false, array($course5));
517 $this->imsplugin->cron();
518
519 $parentcatid = $DB->get_field('course_categories', 'id', array('name' => $topcat));
520 $subcatid = $DB->get_field('course_categories', 'id', array('name' => $subcat,'parent' => $parentcatid));
521
522 $this->assertTrue(isset($subcatid));
523 $this->assertTrue($subcatid > 0);
524
525 # Change the category separator character
526 $this->imsplugin->set_config('categoryseparator', ':');
527
528 $catsep = trim($this->imsplugin->get_config('categoryseparator'));
529
530 $topcat = 'DEFAULT CATNAME';
531 $subcat = 'DEFAULT SUB CATNAME TEST2';
532
533 $fullcat = $topcat.$catsep.$subcat;
534
535 $course6 = new StdClass();
536 $course6->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
537 $course6->idnumber = 'id6';
538 $course6->imsshort = 'description_short';
539 $course6->imslong = 'description_long';
540 $course6->imsfull = 'description_full';
541 $course6->category = $fullcat;
542
543 $this->set_xml_file(false, array($course6));
544 $this->imsplugin->cron();
545
546 $parentcatid = $DB->get_field('course_categories', 'id', array('name' => $topcat));
547 $subcatid = $DB->get_field('course_categories', 'id', array('name' => $subcat,'parent' => $parentcatid));
548
549 $this->assertTrue(isset($subcatid));
550 $this->assertTrue($subcatid > 0);
551 }
552
553
554 /**
555 * Test that duplicate nested categories are not created
556 */
557 public function test_nested_categories_for_dups() {
558 global $DB;
559
560 $catsep = trim($this->imsplugin->get_config('categoryseparator'));
561
562 $topcat = 'DEFAULT CATNAME';
563 $subcat = 'DEFAULT SUB CATNAME DUPTEST';
564
565 $fullcat = $topcat.$catsep.$subcat;
566
567 $course7 = new StdClass();
568 $course7->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
569 $course7->idnumber = 'id7';
570 $course7->imsshort = 'description_short';
571 $course7->imslong = 'description_long';
572 $course7->imsfull = 'description_full';
573 $course7->category = $fullcat;
574
575 $this->set_xml_file(false, array($course7));
576 $this->imsplugin->cron();
577
578 $prevncategories = $DB->count_records('course_categories');
579
580 $course8 = new StdClass();
581 $course8->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
582 $course8->idnumber = 'id8';
583 $course8->imsshort = 'description_short';
584 $course8->imslong = 'description_long';
585 $course8->imsfull = 'description_full';
586 $course8->category = $fullcat;
587
588 $this->set_xml_file(false, array($course8));
589 $this->imsplugin->cron();
590
591 $this->assertEquals($prevncategories, $DB->count_records('course_categories'));
592 }
593
594
2b8f692f
DM
595 /**
596 * Sets the plugin configuration for testing
597 */
7cbb5070 598 public function set_test_config() {
2b8f692f
DM
599 $this->imsplugin->set_config('mailadmins', false);
600 $this->imsplugin->set_config('prev_path', '');
601 $this->imsplugin->set_config('createnewusers', true);
93fdbb42 602 $this->imsplugin->set_config('imsupdateusers', true);
2b8f692f 603 $this->imsplugin->set_config('createnewcourses', true);
93fdbb42 604 $this->imsplugin->set_config('updatecourses', true);
2b8f692f
DM
605 $this->imsplugin->set_config('createnewcategories', true);
606 }
607
2b8f692f 608 /**
295e4fa8 609 * Creates an IMS enterprise XML file and adds it's path to config settings.
2b8f692f 610 *
295e4fa8
DP
611 * @param bool|array $users false or array of users StdClass
612 * @param bool|array $courses false or of courses StdClass
2b8f692f 613 */
7cbb5070 614 public function set_xml_file($users = false, $courses = false) {
2b8f692f
DM
615
616 $xmlcontent = '<enterprise>';
617
618 // Users.
619 if (!empty($users)) {
620 foreach ($users as $user) {
621 $xmlcontent .= '
93fdbb42
BB
622 <person';
623
624 // Optional recstatus (1=add, 2=update, 3=delete)
625 if (!empty($user->recstatus)) {
626 $xmlcontent .= ' recstatus="'.$user->recstatus.'"';
627 }
628
629 $xmlcontent .= '>
2b8f692f
DM
630 <sourcedid>
631 <source>TestSource</source>
632 <id>'.$user->username.'</id>
633 </sourcedid>
93fdbb42
BB
634 <userid';
635
636 // Optional authentication type
637 if (!empty($user->auth)) {
638 $xmlcontent .= ' authenticationtype="'.$user->auth.'"';
639 }
640
641 $xmlcontent .= '>'.$user->username.'</userid>
2b8f692f
DM
642 <name>
643 <fn>'.$user->firstname.' '.$user->lastname.'</fn>
644 <n>
645 <family>'.$user->lastname.'</family>
646 <given>'.$user->firstname.'</given>
647 </n>
648 </name>
649 <email>'.$user->email.'</email>
650 </person>';
651 }
652 }
653
654 // Courses.
655 // Mapping based on default course attributes - IMS group tags mapping.
656 if (!empty($courses)) {
657 foreach ($courses as $course) {
658
2b8f692f 659 $xmlcontent .= '
93fdbb42
BB
660 <group';
661
662 // Optional recstatus (1=add, 2=update, 3=delete)
663 if (!empty($course->recstatus)) {
664 $xmlcontent .= ' recstatus="'.$course->recstatus.'"';
665 }
666
667 $xmlcontent .= '>
2b8f692f
DM
668 <sourcedid>
669 <source>TestSource</source>
670 <id>'.$course->idnumber.'</id>
671 </sourcedid>
672 <description>';
673
674 // Optional to test course attributes mappings.
675 if (!empty($course->imsshort)) {
676 $xmlcontent .= '
677 <short>'.$course->imsshort.'</short>';
678 }
679
680 // Optional to test course attributes mappings.
681 if (!empty($course->imslong)) {
682 $xmlcontent .= '
683 <long>'.$course->imslong.'</long>';
684 }
685
686 // Optional to test course attributes mappings.
687 if (!empty($course->imsfull)) {
688 $xmlcontent .= '
689 <full>'.$course->imsfull.'</full>';
690 }
691
295e4fa8 692 // The orgunit tag value is used by moodle as category name.
2b8f692f
DM
693 $xmlcontent .= '
694 </description>
695 <org>
7cd5340f 696 <orgunit>'.$course->category.'</orgunit>
2b8f692f
DM
697 </org>
698 </group>';
699 }
700 }
701
702 $xmlcontent .= '
703</enterprise>';
704
705 // Creating the XML file.
706 $filename = 'ims_' . rand(1000, 9999) . '.xml';
707 $tmpdir = make_temp_directory('enrol_imsenterprise');
708 $xmlfilepath = $tmpdir . '/' . $filename;
709 file_put_contents($xmlfilepath, $xmlcontent);
710
711 // Setting the file path in CFG.
712 $this->imsplugin->set_config('imsfilelocation', $xmlfilepath);
713 }
302b8abd
DL
714
715 /**
716 * IMS Enterprise enrolment task test.
717 */
718 public function test_imsenterprise_cron_task() {
719 global $DB;
720 $prevnusers = $DB->count_records('user');
721
722 $user1 = new StdClass();
723 $user1->username = 'u1';
0fe86bbd 724 $user1->email = 'u1@example.com';
302b8abd
DL
725 $user1->firstname = 'U';
726 $user1->lastname = '1';
727
728 $users = array($user1);
729 $this->set_xml_file($users);
730
731 $task = new enrol_imsenterprise\task\cron_task();
732 $task->execute();
733
734 $this->assertEquals(($prevnusers + 1), $DB->count_records('user'));
735 }
2b8f692f 736}