MDL-28705 enrol_imsenterprise Unit tests for course attributes mapping
[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
21 * @category phpunit
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
36 * @category phpunit
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
42 protected $imsplugin;
43
44 protected function setUp() {
45 $this->resetAfterTest(true);
46 $this->imsplugin = enrol_get_plugin('imsenterprise');
47 $this->set_test_config();
48 }
49
50 /**
51 * With an empty IMS enterprise file
52 */
53 public function test_emptyfile() {
54 global $DB;
55
56 $prevncourses = $DB->count_records('course');
57 $prevnusers = $DB->count_records('user');
58
59 $this->set_xml_file(false, false);
60 $this->imsplugin->cron();
61
62 $this->assertEquals($prevncourses, $DB->count_records('course'));
63 $this->assertEquals($prevnusers, $DB->count_records('user'));
64 }
65
66
67 /**
68 * Existing users are not created again
69 */
70 public function test_users_existing() {
71 global $DB;
72
73 $user1 = $this->getDataGenerator()->create_user();
74 $user2 = $this->getDataGenerator()->create_user();
75
76 $prevnusers = $DB->count_records('user');
77
78 $users = array($user1, $user2);
79 $this->set_xml_file($users);
80 $this->imsplugin->cron();
81
82 $this->assertEquals($prevnusers, $DB->count_records('user'));
83 }
84
85
86 /**
87 * Add new users
88 */
89 public function test_users_add() {
90 global $DB;
91
92 $prevnusers = $DB->count_records('user');
93
94 $user1 = new StdClass();
95 $user1->username = 'u1';
96 $user1->email = 'u1@u1.org';
97 $user1->firstname = 'U';
98 $user1->lastname = '1';
99
100 $users = array($user1);
101 $this->set_xml_file($users);
102 $this->imsplugin->cron();
103
104 $this->assertEquals(($prevnusers + 1), $DB->count_records('user'));
105 }
106
107
108 /**
109 * Existing courses are not created again
110 */
111 public function test_courses_existing() {
112 global $DB;
113
114 $course1 = $this->getDataGenerator()->create_course(array('idnumber' => 'id1'));
115 $course2 = $this->getDataGenerator()->create_course(array('idnumber' => 'id2'));
116
117 // Default mapping according to default course attributes - IMS description tags mapping.
118 $course1->imsshort = $course1->fullname;
119 $course2->imsshort = $course2->fullname;
120
121 $prevncourses = $DB->count_records('course');
122
123 $courses = array($course1, $course2);
124 $this->set_xml_file(false, $courses);
125 $this->imsplugin->cron();
126
127 $this->assertEquals($prevncourses, $DB->count_records('course'));
128 }
129
130
131 /**
132 * Add new courses
133 */
134 public function test_courses_add() {
135 global $DB;
136
137 $prevncourses = $DB->count_records('course');
138
139 $course1 = new StdClass();
140 $course1->idnumber = 'id1';
141 $course1->imsshort = 'id1';
142 $course1->category = 'DEFAULT CATNAME';
143
144 $course2 = new StdClass();
145 $course2->idnumber = 'id2';
146 $course2->imsshort = 'id2';
147 $course2->category = 'DEFAULT CATNAME';
148
149 $courses = array($course1, $course2);
150 $this->set_xml_file(false, $courses);
151 $this->imsplugin->cron();
152
153 $this->assertEquals(($prevncourses + 2), $DB->count_records('course'));
154 }
155
156
c25c3c07
DM
157 /**
158 * Course attributes mapping to IMS enterprise group description tags
159 */
160 public function test_courses_attrmapping() {
161 global $DB;
162
163 // Setting a all = coursecode (idnumber) mapping.
164 $this->imsplugin->set_config('imscoursemapshortname', 'coursecode');
165 $this->imsplugin->set_config('imscoursemapfullname', 'coursecode');
166 $this->imsplugin->set_config('imscoursemapsummary', 'coursecode');
167
168 $course1 = new StdClass();
169 $course1->idnumber = 'id1';
170 $course1->imsshort = 'description_short1';
171 $course1->imslong = 'description_long';
172 $course1->imsfull = 'description_full';
173 $course1->category = 'DEFAULT CATNAME';
174
175 $this->set_xml_file(false, array($course1));
176 $this->imsplugin->cron();
177
178 $dbcourse = $DB->get_record('course', array('idnumber' => $course1->idnumber));
179 $this->assertFalse(!$dbcourse);
180 $this->assertEquals($dbcourse->shortname, $course1->idnumber);
181 $this->assertEquals($dbcourse->fullname, $course1->idnumber);
182 $this->assertEquals($dbcourse->summary, $course1->idnumber);
183
184
185 // Setting a mapping using all the description tags.
186 $this->imsplugin->set_config('imscoursemapshortname', 'short');
187 $this->imsplugin->set_config('imscoursemapfullname', 'long');
188 $this->imsplugin->set_config('imscoursemapsummary', 'full');
189
190 $course2 = new StdClass();
191 $course2->idnumber = 'id2';
192 $course2->imsshort = 'description_short2';
193 $course2->imslong = 'description_long';
194 $course2->imsfull = 'description_full';
195 $course2->category = 'DEFAULT CATNAME';
196
197 $this->set_xml_file(false, array($course2));
198 $this->imsplugin->cron();
199
200 $dbcourse = $DB->get_record('course', array('idnumber' => $course2->idnumber));
201 $this->assertFalse(!$dbcourse);
202 $this->assertEquals($dbcourse->shortname, $course2->imsshort);
203 $this->assertEquals($dbcourse->fullname, $course2->imslong);
204 $this->assertEquals($dbcourse->summary, $course2->imsfull);
205
206
207 // Setting a mapping where the specified description tags doesn't exist in the XML file (must delegate into idnumber).
208 $this->imsplugin->set_config('imscoursemapshortname', 'short');
209 $this->imsplugin->set_config('imscoursemapfullname', 'long');
210 $this->imsplugin->set_config('imscoursemapsummary', 'full');
211
212 $course3 = new StdClass();
213 $course3->idnumber = 'id3';
214 $course3->imsshort = 'description_short3';
215 $course3->category = 'DEFAULT CATNAME';
216
217 $this->set_xml_file(false, array($course3));
218 $this->imsplugin->cron();
219
220 $dbcourse = $DB->get_record('course', array('idnumber' => $course3->idnumber));
221 $this->assertFalse(!$dbcourse);
222 $this->assertEquals($dbcourse->shortname, $course3->imsshort);
223 $this->assertEquals($dbcourse->fullname, $course3->idnumber);
224 $this->assertEquals($dbcourse->summary, $course3->idnumber);
225
226 }
227
228
2b8f692f
DM
229 /**
230 * Sets the plugin configuration for testing
231 */
232 protected function set_test_config() {
233 $this->imsplugin->set_config('mailadmins', false);
234 $this->imsplugin->set_config('prev_path', '');
235 $this->imsplugin->set_config('createnewusers', true);
236 $this->imsplugin->set_config('createnewcourses', true);
237 $this->imsplugin->set_config('createnewcategories', true);
238 }
239
240
241 /**
242 * Creates an IMS enterprise XML file and adds it's path to config settings
243 *
244 * @param array Array of users StdClass
245 * @param array Array of courses StdClass
246 */
247 protected function set_xml_file($users = false, $courses = false) {
248 global $DB;
249
250 $xmlcontent = '<enterprise>';
251
252 // Users.
253 if (!empty($users)) {
254 foreach ($users as $user) {
255 $xmlcontent .= '
256 <person>
257 <sourcedid>
258 <source>TestSource</source>
259 <id>'.$user->username.'</id>
260 </sourcedid>
261 <userid>'.$user->username.'</userid>
262 <name>
263 <fn>'.$user->firstname.' '.$user->lastname.'</fn>
264 <n>
265 <family>'.$user->lastname.'</family>
266 <given>'.$user->firstname.'</given>
267 </n>
268 </name>
269 <email>'.$user->email.'</email>
270 </person>';
271 }
272 }
273
274 // Courses.
275 // Mapping based on default course attributes - IMS group tags mapping.
276 if (!empty($courses)) {
277 foreach ($courses as $course) {
278
279 // orgunit tag value is used by moodle as category name.
280 // If the category does not exists the id is used as name.
281 if ($categoryname = $DB->get_field('course_categories', 'name', array('id' => $course->category))) {
282 $categoryname = $course->category;
283 }
284
285 $xmlcontent .= '
286 <group>
287 <sourcedid>
288 <source>TestSource</source>
289 <id>'.$course->idnumber.'</id>
290 </sourcedid>
291 <description>';
292
293 // Optional to test course attributes mappings.
294 if (!empty($course->imsshort)) {
295 $xmlcontent .= '
296 <short>'.$course->imsshort.'</short>';
297 }
298
299 // Optional to test course attributes mappings.
300 if (!empty($course->imslong)) {
301 $xmlcontent .= '
302 <long>'.$course->imslong.'</long>';
303 }
304
305 // Optional to test course attributes mappings.
306 if (!empty($course->imsfull)) {
307 $xmlcontent .= '
308 <full>'.$course->imsfull.'</full>';
309 }
310
311 $xmlcontent .= '
312 </description>
313 <org>
314 <orgunit>'.$categoryname.'</orgunit>
315 </org>
316 </group>';
317 }
318 }
319
320 $xmlcontent .= '
321</enterprise>';
322
323 // Creating the XML file.
324 $filename = 'ims_' . rand(1000, 9999) . '.xml';
325 $tmpdir = make_temp_directory('enrol_imsenterprise');
326 $xmlfilepath = $tmpdir . '/' . $filename;
327 file_put_contents($xmlfilepath, $xmlcontent);
328
329 // Setting the file path in CFG.
330 $this->imsplugin->set_config('imsfilelocation', $xmlfilepath);
331 }
332}