MDL-31356 enrol_imsenterprise: Fix behaviour with course idnumber
[moodle.git] / enrol / imsenterprise / tests / imsenterprise_test.php
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/>.
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  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot . '/enrol/imsenterprise/locallib.php');
30 require_once($CFG->dirroot . '/enrol/imsenterprise/lib.php');
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 {
42     /**
43      * @var $imsplugin enrol_imsenterprise_plugin IMS plugin instance.
44      */
45     public $imsplugin;
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     }
56     /**
57      * With an empty IMS enterprise file
58      */
59     public function test_emptyfile() {
60         global $DB;
62         $prevncourses = $DB->count_records('course');
63         $prevnusers = $DB->count_records('user');
65         $this->set_xml_file(false, false);
66         $this->imsplugin->cron();
68         $this->assertEquals($prevncourses, $DB->count_records('course'));
69         $this->assertEquals($prevnusers, $DB->count_records('user'));
70     }
72     /**
73      * Existing users are not created again
74      */
75     public function test_users_existing() {
76         global $DB;
78         $user1 = $this->getDataGenerator()->create_user();
79         $user2 = $this->getDataGenerator()->create_user();
81         $prevnusers = $DB->count_records('user');
83         $users = array($user1, $user2);
84         $this->set_xml_file($users);
85         $this->imsplugin->cron();
87         $this->assertEquals($prevnusers, $DB->count_records('user'));
88     }
90     /**
91      * Add new users
92      */
93     public function test_users_add() {
94         global $DB;
96         $prevnusers = $DB->count_records('user');
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';
105         $users = array($user1);
106         $this->set_xml_file($users);
107         $this->imsplugin->cron();
109         $this->assertEquals(($prevnusers + 1), $DB->count_records('user'));
110     }
112     /**
113      * Add new users and set an auth type
114      */
115     public function test_users_add_with_auth() {
116         global $DB;
118         $prevnusers = $DB->count_records('user');
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';
128         $users = array($user2);
129         $this->set_xml_file($users);
130         $this->imsplugin->cron();
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);
137         $this->assertEquals(($prevnusers + 1), $DB->count_records('user'));
138         $this->assertEquals($dbauth, $user2->auth);
139     }
142     /**
143      * Update user
144      */
145     public function test_user_update() {
146         global $DB;
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';
155         $users = array($user3);
156         $this->set_xml_file($users);
157         $this->imsplugin->cron();
159         $user3u = $DB->get_record('user', array('username' => $user3->username));
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';
166         $users = array($user3u);
167         $this->set_xml_file($users);
168         $this->imsplugin->cron();
170         $dbuser = $DB->get_record('user', array('username' => $user3->username));
172         $this->assertEquals($dbuser->email, $user3u->email);
173         $this->assertEquals($dbuser->firstname, $user3u->firstname);
174         $this->assertEquals($dbuser->lastname, $user3u->lastname);
175     }
177     /**
178      * Update user disabled
179      */
180     public function test_user_update_disabled() {
181         global $DB;
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     }
202     /**
203      * Delete user
204      */
205     public function test_user_delete() {
206         global $DB;
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     }
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     }
240     /**
241      * Existing courses are not created again
242      */
243     public function test_courses_existing() {
244         global $DB;
246         $course1 = $this->getDataGenerator()->create_course(array('idnumber' => 'id1'));
247         $course2 = $this->getDataGenerator()->create_course(array('idnumber' => 'id2'));
249         // Default mapping according to default course attributes - IMS description tags mapping.
250         $course1->imsshort = $course1->fullname;
251         $course2->imsshort = $course2->fullname;
253         $prevncourses = $DB->count_records('course');
255         $courses = array($course1, $course2);
256         $this->set_xml_file(false, $courses);
257         $this->imsplugin->cron();
259         $this->assertEquals($prevncourses, $DB->count_records('course'));
260     }
262     /**
263      * Add new courses
264      */
265     public function test_courses_add() {
266         global $DB;
268         $prevncourses = $DB->count_records('course');
270         $course1 = new StdClass();
271         $course1->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
272         $course1->idnumber = 'id1';
273         $course1->imsshort = 'id1';
274         $course1->category = 'DEFAULT CATNAME';
276         $course2 = new StdClass();
277         $course2->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
278         $course2->idnumber = 'id2';
279         $course2->imsshort = 'id2';
280         $course2->category = 'DEFAULT CATNAME';
282         $courses = array($course1, $course2);
283         $this->set_xml_file(false, $courses);
284         $this->imsplugin->cron();
286         $this->assertEquals(($prevncourses + 2), $DB->count_records('course'));
287         $this->assertTrue($DB->record_exists('course', array('idnumber' => $course1->idnumber)));
288         $this->assertTrue($DB->record_exists('course', array('idnumber' => $course2->idnumber)));
289     }
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;
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     }
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     }
336     /**
337      * Add new course with the truncateidnumber setting.
338      */
339     public function test_courses_add_truncate_idnumber() {
340         global $DB;
342         $truncatelength = 4;
344         $this->imsplugin->set_config('truncatecoursecodes', $truncatelength);
345         $prevncourses = $DB->count_records('course');
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';
353         $this->set_xml_file(false, array($course1));
354         $this->imsplugin->cron();
356         // Verify the new course has been added.
357         $this->assertEquals(($prevncourses + 1), $DB->count_records('course'));
359         $truncatedidnumber = substr($course1->idnumber, 0, $truncatelength);
361         $this->assertTrue($DB->record_exists('course', array('idnumber' => $truncatedidnumber)));
362     }
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');
371         $this->imsplugin->set_config('createnewcategories', false);
373         // Delete the default category, to ensure the plugin handles this gracefully.
374         $defaultcat = coursecat::get_default();
375         $defaultcat->delete_full(false);
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();
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     }
391     /**
392      * Course attributes mapping to IMS enterprise group description tags
393      */
394     public function test_courses_attrmapping() {
395         global $DB;
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');
402         $course1 = new StdClass();
403         $course1->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
404         $course1->idnumber = 'id1';
405         $course1->imsshort = 'description_short1';
406         $course1->imslong = 'description_long';
407         $course1->imsfull = 'description_full';
408         $course1->category = 'DEFAULT CATNAME';
410         $this->set_xml_file(false, array($course1));
411         $this->imsplugin->cron();
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);
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');
424         $course2 = new StdClass();
425         $course2->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
426         $course2->idnumber = 'id2';
427         $course2->imsshort = 'description_short2';
428         $course2->imslong = 'description_long';
429         $course2->imsfull = 'description_full';
430         $course2->category = 'DEFAULT CATNAME';
432         $this->set_xml_file(false, array($course2));
433         $this->imsplugin->cron();
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);
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');
446         $course3 = new StdClass();
447         $course3->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_ADD;
448         $course3->idnumber = 'id3';
449         $course3->imsshort = 'description_short3';
450         $course3->category = 'DEFAULT CATNAME';
452         $this->set_xml_file(false, array($course3));
453         $this->imsplugin->cron();
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);
461     }
463     /**
464      * Course updates
465      */
466     public function test_course_update() {
467         global $DB;
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';
476         $this->set_xml_file(false, array($course4));
477         $this->imsplugin->cron();
479         $course4u = $DB->get_record('course', array('idnumber' => $course4->idnumber));
481         $course4u->recstatus = enrol_imsenterprise_plugin::IMSENTERPRISE_UPDATE;
482         $course4u->imsshort = 'description_short_updated';
483         $course4u->imsfull = 'description_full_updated';
485         $this->set_xml_file(false, array($course4u));
486         $this->imsplugin->cron();
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     }
495     /**
496      * Nested categories during course creation
497      */
498     public function test_nested_categories() {
499         global $DB;
501         $catsep = trim($this->imsplugin->get_config('categoryseparator'));
503         $topcat = 'DEFAULT CATNAME';
504         $subcat = 'DEFAULT SUB CATNAME';
506         $fullcat = $topcat.$catsep.$subcat;
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;
516         $this->set_xml_file(false, array($course5));
517         $this->imsplugin->cron();
519         $parentcatid = $DB->get_field('course_categories', 'id', array('name' => $topcat));
520         $subcatid = $DB->get_field('course_categories', 'id', array('name' => $subcat,'parent' => $parentcatid));
522         $this->assertTrue(isset($subcatid));
523         $this->assertTrue($subcatid > 0);
525         # Change the category separator character
526         $this->imsplugin->set_config('categoryseparator', ':');
528         $catsep = trim($this->imsplugin->get_config('categoryseparator'));
530         $topcat = 'DEFAULT CATNAME';
531         $subcat = 'DEFAULT SUB CATNAME TEST2';
533         $fullcat = $topcat.$catsep.$subcat;
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;
543         $this->set_xml_file(false, array($course6));
544         $this->imsplugin->cron();
546         $parentcatid = $DB->get_field('course_categories', 'id', array('name' => $topcat));
547         $subcatid = $DB->get_field('course_categories', 'id', array('name' => $subcat,'parent' => $parentcatid));
549         $this->assertTrue(isset($subcatid));
550         $this->assertTrue($subcatid > 0);
551     }
554     /**
555      * Test that duplicate nested categories are not created
556      */
557     public function test_nested_categories_for_dups() {
558         global $DB;
560         $catsep = trim($this->imsplugin->get_config('categoryseparator'));
562         $topcat = 'DEFAULT CATNAME';
563         $subcat = 'DEFAULT SUB CATNAME DUPTEST';
565         $fullcat = $topcat.$catsep.$subcat;
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;
575         $this->set_xml_file(false, array($course7));
576         $this->imsplugin->cron();
578         $prevncategories = $DB->count_records('course_categories');
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;
588         $this->set_xml_file(false, array($course8));
589         $this->imsplugin->cron();
591         $this->assertEquals($prevncategories, $DB->count_records('course_categories'));
592     }
595     /**
596      * Sets the plugin configuration for testing
597      */
598     public function set_test_config() {
599         $this->imsplugin->set_config('mailadmins', false);
600         $this->imsplugin->set_config('prev_path', '');
601         $this->imsplugin->set_config('createnewusers', true);
602         $this->imsplugin->set_config('imsupdateusers', true);
603         $this->imsplugin->set_config('createnewcourses', true);
604         $this->imsplugin->set_config('updatecourses', true);
605         $this->imsplugin->set_config('createnewcategories', true);
606     }
608     /**
609      * Creates an IMS enterprise XML file and adds it's path to config settings.
610      *
611      * @param bool|array $users false or array of users StdClass
612      * @param bool|array $courses false or of courses StdClass
613      */
614     public function set_xml_file($users = false, $courses = false) {
616         $xmlcontent = '<enterprise>';
618         // Users.
619         if (!empty($users)) {
620             foreach ($users as $user) {
621                 $xmlcontent .= '
622   <person';
624                 // Optional recstatus (1=add, 2=update, 3=delete)
625                 if (!empty($user->recstatus)) {
626                     $xmlcontent .= ' recstatus="'.$user->recstatus.'"';
627                 }
629                 $xmlcontent .= '>
630     <sourcedid>
631       <source>TestSource</source>
632       <id>'.$user->username.'</id>
633     </sourcedid>
634     <userid';
636             // Optional authentication type
637             if (!empty($user->auth)) {
638                 $xmlcontent .= ' authenticationtype="'.$user->auth.'"';
639             }
641             $xmlcontent .= '>'.$user->username.'</userid>
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         }
654         // Courses.
655         // Mapping based on default course attributes - IMS group tags mapping.
656         if (!empty($courses)) {
657             foreach ($courses as $course) {
659                 $xmlcontent .= '
660   <group';
662                 // Optional recstatus (1=add, 2=update, 3=delete)
663                 if (!empty($course->recstatus)) {
664                     $xmlcontent .= ' recstatus="'.$course->recstatus.'"';
665                 }
667                 $xmlcontent .= '>
668     <sourcedid>
669       <source>TestSource</source>
670       <id>'.$course->idnumber.'</id>
671     </sourcedid>
672     <description>';
674                 // Optional to test course attributes mappings.
675                 if (!empty($course->imsshort)) {
676                     $xmlcontent .= '
677       <short>'.$course->imsshort.'</short>';
678                 }
680                 // Optional to test course attributes mappings.
681                 if (!empty($course->imslong)) {
682                     $xmlcontent .= '
683       <long>'.$course->imslong.'</long>';
684                 }
686                 // Optional to test course attributes mappings.
687                 if (!empty($course->imsfull)) {
688                     $xmlcontent .= '
689       <full>'.$course->imsfull.'</full>';
690                 }
692                 // The orgunit tag value is used by moodle as category name.
693                 $xmlcontent .= '
694     </description>
695     <org>
696       <orgunit>'.$course->category.'</orgunit>
697     </org>
698   </group>';
699             }
700         }
702         $xmlcontent .= '
703 </enterprise>';
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);
711         // Setting the file path in CFG.
712         $this->imsplugin->set_config('imsfilelocation', $xmlfilepath);
713     }
715     /**
716      * IMS Enterprise enrolment task test.
717      */
718     public function test_imsenterprise_cron_task() {
719         global $DB;
720         $prevnusers = $DB->count_records('user');
722         $user1 = new StdClass();
723         $user1->username = 'u1';
724         $user1->email = 'u1@example.com';
725         $user1->firstname = 'U';
726         $user1->lastname = '1';
728         $users = array($user1);
729         $this->set_xml_file($users);
731         $task = new enrol_imsenterprise\task\cron_task();
732         $task->execute();
734         $this->assertEquals(($prevnusers + 1), $DB->count_records('user'));
735     }