Commit | Line | Data |
---|---|---|
2a7a0216 JM |
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 | * External course functions unit tests | |
19 | * | |
20 | * @package core_course | |
21 | * @category external | |
22 | * @copyright 2012 Jerome Mouneyrac | |
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 | ||
30 | require_once($CFG->dirroot . '/webservice/tests/helpers.php'); | |
31 | ||
32 | /** | |
33 | * External course functions unit tests | |
34 | * | |
35 | * @package core_course | |
36 | * @category external | |
37 | * @copyright 2012 Jerome Mouneyrac | |
38 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
39 | */ | |
40 | class core_course_external_testcase extends externallib_testcase { | |
41 | ||
42 | /** | |
43 | * Tests set up | |
44 | */ | |
45 | protected function setUp() { | |
46 | global $CFG; | |
47 | require_once($CFG->dirroot . '/course/externallib.php'); | |
48 | } | |
49 | ||
50 | /** | |
51 | * Test create_categories | |
52 | */ | |
53 | public function test_create_categories() { | |
54 | ||
55 | global $DB; | |
56 | ||
57 | $this->resetAfterTest(true); | |
58 | ||
59 | // Set the required capabilities by the external function | |
60 | $contextid = context_system::instance()->id; | |
61 | $roleid = $this->assignUserCapability('moodle/category:manage', $contextid); | |
62 | ||
63 | // Create base categories. | |
64 | $category1 = new stdClass(); | |
65 | $category1->name = 'Root Test Category 1'; | |
66 | $category2 = new stdClass(); | |
67 | $category2->name = 'Root Test Category 2'; | |
68 | $category2->idnumber = 'rootcattest2'; | |
69 | $category2->desc = 'Description for root test category 1'; | |
70 | $category2->theme = 'base'; | |
71 | $categories = array( | |
72 | array('name' => $category1->name, 'parent' => 0), | |
73 | array('name' => $category2->name, 'parent' => 0, 'idnumber' => $category2->idnumber, | |
74 | 'description' => $category2->desc, 'theme' => $category2->theme) | |
75 | ); | |
76 | ||
77 | $createdcats = core_course_external::create_categories($categories); | |
78 | ||
79 | // Initially confirm that base data was inserted correctly. | |
80 | $this->assertEquals($category1->name, $createdcats[0]['name']); | |
81 | $this->assertEquals($category2->name, $createdcats[1]['name']); | |
82 | ||
83 | // Save the ids. | |
84 | $category1->id = $createdcats[0]['id']; | |
85 | $category2->id = $createdcats[1]['id']; | |
86 | ||
87 | // Create on sub category. | |
88 | $category3 = new stdClass(); | |
89 | $category3->name = 'Sub Root Test Category 3'; | |
90 | $subcategories = array( | |
91 | array('name' => $category3->name, 'parent' => $category1->id) | |
92 | ); | |
93 | ||
94 | $createdsubcats = core_course_external::create_categories($subcategories); | |
95 | ||
96 | // Confirm that sub categories were inserted correctly. | |
97 | $this->assertEquals($category3->name, $createdsubcats[0]['name']); | |
98 | ||
99 | // Save the ids. | |
100 | $category3->id = $createdsubcats[0]['id']; | |
101 | ||
102 | // Calling the ws function should provide a new sortorder to give category1, | |
103 | // category2, category3. New course categories are ordered by id not name. | |
104 | $category1 = $DB->get_record('course_categories', array('id' => $category1->id)); | |
105 | $category2 = $DB->get_record('course_categories', array('id' => $category2->id)); | |
106 | $category3 = $DB->get_record('course_categories', array('id' => $category3->id)); | |
107 | ||
108 | $this->assertGreaterThanOrEqual($category1->sortorder, $category3->sortorder); | |
109 | $this->assertGreaterThanOrEqual($category2->sortorder, $category3->sortorder); | |
110 | ||
111 | // Call without required capability | |
112 | $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid); | |
113 | $this->setExpectedException('required_capability_exception'); | |
114 | $createdsubcats = core_course_external::create_categories($subcategories); | |
115 | ||
116 | } | |
117 | ||
118 | /** | |
119 | * Test delete categories | |
120 | */ | |
121 | public function test_delete_categories() { | |
122 | global $DB; | |
123 | ||
124 | $this->resetAfterTest(true); | |
125 | ||
126 | // Set the required capabilities by the external function | |
127 | $contextid = context_system::instance()->id; | |
128 | $roleid = $this->assignUserCapability('moodle/category:manage', $contextid); | |
129 | ||
130 | $category1 = self::getDataGenerator()->create_category(); | |
131 | $category2 = self::getDataGenerator()->create_category( | |
132 | array('parent' => $category1->id)); | |
133 | $category3 = self::getDataGenerator()->create_category(); | |
134 | $category4 = self::getDataGenerator()->create_category( | |
135 | array('parent' => $category3->id)); | |
136 | $category5 = self::getDataGenerator()->create_category( | |
137 | array('parent' => $category4->id)); | |
138 | ||
139 | //delete category 1 and 2 + delete category 4, category 5 moved under category 3 | |
140 | core_course_external::delete_categories(array( | |
141 | array('id' => $category1->id, 'recursive' => 1), | |
142 | array('id' => $category4->id) | |
143 | )); | |
144 | ||
145 | //check $category 1 and 2 are deleted | |
146 | $notdeletedcount = $DB->count_records_select('course_categories', | |
147 | 'id IN ( ' . $category1->id . ',' . $category2->id . ',' . $category4->id . ')'); | |
148 | $this->assertEquals(0, $notdeletedcount); | |
149 | ||
150 | //check that $category5 as $category3 for parent | |
151 | $dbcategory5 = $DB->get_record('course_categories', array('id' => $category5->id)); | |
152 | $this->assertEquals($dbcategory5->path, $category3->path . '/' . $category5->id); | |
153 | ||
154 | // Call without required capability | |
155 | $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid); | |
156 | $this->setExpectedException('required_capability_exception'); | |
157 | $createdsubcats = core_course_external::delete_categories( | |
158 | array(array('id' => $category3->id))); | |
159 | } | |
160 | ||
161 | /** | |
162 | * Test get categories | |
163 | */ | |
164 | public function test_get_categories() { | |
165 | global $DB; | |
166 | ||
167 | $this->resetAfterTest(true); | |
168 | $category1data['idnumber'] = 'idnumbercat1'; | |
169 | $category1data['name'] = 'Category 1 for PHPunit test'; | |
170 | $category1data['description'] = 'Category 1 description'; | |
171 | $category1data['descriptionformat'] = FORMAT_MOODLE; | |
172 | $category1 = self::getDataGenerator()->create_category($category1data); | |
173 | $category2 = self::getDataGenerator()->create_category( | |
174 | array('parent' => $category1->id)); | |
175 | $category6 = self::getDataGenerator()->create_category( | |
176 | array('parent' => $category1->id, 'visible' => 0)); | |
177 | $category3 = self::getDataGenerator()->create_category(); | |
178 | $category4 = self::getDataGenerator()->create_category( | |
179 | array('parent' => $category3->id)); | |
180 | $category5 = self::getDataGenerator()->create_category( | |
181 | array('parent' => $category4->id)); | |
182 | ||
183 | // Set the required capabilities by the external function. | |
184 | $context = context_system::instance(); | |
185 | $roleid = $this->assignUserCapability('moodle/category:manage', $context->id); | |
186 | ||
187 | // Retrieve category1 + sub-categories except not visible ones | |
188 | $categories = core_course_external::get_categories(array( | |
189 | array('key' => 'id', 'value' => $category1->id), | |
190 | array('key' => 'visible', 'value' => 1)), 1); | |
191 | ||
192 | // Check we retrieve the good total number of categories. | |
193 | $this->assertEquals(2, count($categories)); | |
194 | ||
195 | // Check the return values | |
196 | $this->assertEquals($categories[0]['id'], $category1->id); | |
197 | $this->assertEquals($categories[0]['idnumber'], $category1->idnumber); | |
198 | $this->assertEquals($categories[0]['name'], $category1->name); | |
199 | $this->assertEquals($categories[0]['description'], $category1->description); | |
200 | $this->assertEquals($categories[0]['descriptionformat'], FORMAT_HTML); | |
201 | ||
202 | // Check different params. | |
203 | $categories = core_course_external::get_categories(array( | |
204 | array('key' => 'id', 'value' => $category1->id), | |
205 | array('key' => 'idnumber', 'value' => $category1->idnumber), | |
206 | array('key' => 'visible', 'value' => 1)), 0); | |
207 | $this->assertEquals(1, count($categories)); | |
208 | ||
209 | // Retrieve categories from parent. | |
210 | $categories = core_course_external::get_categories(array( | |
211 | array('key' => 'parent', 'value' => $category3->id)), 1); | |
212 | $this->assertEquals(2, count($categories)); | |
213 | ||
214 | // Retrieve all categories. | |
215 | $categories = core_course_external::get_categories(); | |
216 | $this->assertEquals($DB->count_records('course_categories'), count($categories)); | |
217 | ||
218 | // Call without required capability (it will fail cause of the search on idnumber). | |
219 | $this->unassignUserCapability('moodle/category:manage', $context->id, $roleid); | |
220 | $this->setExpectedException('moodle_exception'); | |
221 | $categories = core_course_external::get_categories(array( | |
222 | array('key' => 'id', 'value' => $category1->id), | |
223 | array('key' => 'idnumber', 'value' => $category1->idnumber), | |
224 | array('key' => 'visible', 'value' => 1)), 0); | |
225 | } | |
226 | ||
227 | /** | |
228 | * Test update_categories | |
229 | */ | |
230 | public function test_update_categories() { | |
231 | global $DB; | |
232 | ||
233 | $this->resetAfterTest(true); | |
234 | ||
235 | // Set the required capabilities by the external function | |
236 | $contextid = context_system::instance()->id; | |
237 | $roleid = $this->assignUserCapability('moodle/category:manage', $contextid); | |
238 | ||
239 | // Create base categories. | |
240 | $category1data['idnumber'] = 'idnumbercat1'; | |
241 | $category1data['name'] = 'Category 1 for PHPunit test'; | |
242 | $category1data['description'] = 'Category 1 description'; | |
243 | $category1data['descriptionformat'] = FORMAT_MOODLE; | |
244 | $category1 = self::getDataGenerator()->create_category($category1data); | |
245 | $category2 = self::getDataGenerator()->create_category( | |
246 | array('parent' => $category1->id)); | |
247 | $category3 = self::getDataGenerator()->create_category(); | |
248 | $category4 = self::getDataGenerator()->create_category( | |
249 | array('parent' => $category3->id)); | |
250 | $category5 = self::getDataGenerator()->create_category( | |
251 | array('parent' => $category4->id)); | |
252 | ||
253 | // We update all category1 attribut. | |
254 | // Then we move cat4 and cat5 parent: cat3 => cat1 | |
255 | $categories = array( | |
256 | array('id' => $category1->id, | |
257 | 'name' => $category1->name . '_updated', | |
258 | 'idnumber' => $category1->idnumber . '_updated', | |
259 | 'description' => $category1->description . '_updated', | |
260 | 'descriptionformat' => FORMAT_HTML, | |
261 | 'theme' => $category1->theme), | |
262 | array('id' => $category4->id, 'parent' => $category1->id)); | |
263 | ||
264 | core_course_external::update_categories($categories); | |
265 | ||
266 | // Check the values were updated. | |
267 | $dbcategories = $DB->get_records_select('course_categories', | |
268 | 'id IN (' . $category1->id . ',' . $category2->id . ',' . $category2->id | |
269 | . ',' . $category3->id . ',' . $category4->id . ',' . $category5->id .')'); | |
270 | $this->assertEquals($category1->name . '_updated', | |
271 | $dbcategories[$category1->id]->name); | |
272 | $this->assertEquals($category1->idnumber . '_updated', | |
273 | $dbcategories[$category1->id]->idnumber); | |
274 | $this->assertEquals($category1->description . '_updated', | |
275 | $dbcategories[$category1->id]->description); | |
276 | $this->assertEquals(FORMAT_HTML, $dbcategories[$category1->id]->descriptionformat); | |
277 | ||
278 | // Check that category4 and category5 have been properly moved. | |
279 | $this->assertEquals('/' . $category1->id . '/' . $category4->id, | |
280 | $dbcategories[$category4->id]->path); | |
281 | $this->assertEquals('/' . $category1->id . '/' . $category4->id . '/' . $category5->id, | |
282 | $dbcategories[$category5->id]->path); | |
283 | ||
284 | // Call without required capability. | |
285 | $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid); | |
286 | $this->setExpectedException('required_capability_exception'); | |
287 | core_course_external::update_categories($categories); | |
288 | } | |
289 | ||
290 | /** | |
291 | * Test create_courses | |
292 | */ | |
293 | public function test_create_courses() { | |
294 | global $DB; | |
295 | ||
296 | $this->resetAfterTest(true); | |
297 | ||
298 | // Set the required capabilities by the external function | |
299 | $contextid = context_system::instance()->id; | |
300 | $roleid = $this->assignUserCapability('moodle/course:create', $contextid); | |
301 | $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid); | |
302 | ||
303 | $category = self::getDataGenerator()->create_category(); | |
304 | ||
305 | // Create base categories. | |
306 | $course1['fullname'] = 'Test course 1'; | |
307 | $course1['shortname'] = 'Testcourse1'; | |
308 | $course1['categoryid'] = $category->id; | |
309 | $course2['fullname'] = 'Test course 2'; | |
310 | $course2['shortname'] = 'Testcourse2'; | |
311 | $course2['categoryid'] = $category->id; | |
312 | $course2['idnumber'] = 'testcourse2idnumber'; | |
313 | $course2['summary'] = 'Description for course 2'; | |
314 | $course2['summaryformat'] = FORMAT_MOODLE; | |
315 | $course2['format'] = 'weeks'; | |
316 | $course2['showgrades'] = 1; | |
317 | $course2['newsitems'] = 3; | |
318 | $course2['startdate'] = 32882306400; // 01/01/3012 | |
319 | $course2['numsections'] = 4; | |
320 | $course2['maxbytes'] = 100000; | |
321 | $course2['showreports'] = 1; | |
322 | $course2['visible'] = 0; | |
323 | $course2['hiddensections'] = 0; | |
324 | $course2['groupmode'] = 0; | |
325 | $course2['groupmodeforce'] = 0; | |
326 | $course2['defaultgroupingid'] = 0; | |
327 | $course2['enablecompletion'] = 1; | |
328 | $course2['completionstartonenrol'] = 1; | |
329 | $course2['completionnotify'] = 1; | |
330 | $course2['lang'] = 'en'; | |
331 | $course2['forcetheme'] = 'base'; | |
332 | $courses = array($course1, $course2); | |
333 | ||
334 | $createdcourses = core_course_external::create_courses($courses); | |
335 | ||
336 | // Check that right number of courses were created. | |
337 | $this->assertEquals(2, count($createdcourses)); | |
338 | ||
339 | // Check that the courses were correctly created. | |
340 | foreach ($createdcourses as $createdcourse) { | |
341 | $dbcourse = $DB->get_record('course', array('id' => $createdcourse['id'])); | |
342 | ||
343 | if ($createdcourse['shortname'] == $course2['shortname']) { | |
344 | $this->assertEquals($dbcourse->fullname, $course2['fullname']); | |
345 | $this->assertEquals($dbcourse->shortname, $course2['shortname']); | |
346 | $this->assertEquals($dbcourse->category, $course2['categoryid']); | |
347 | $this->assertEquals($dbcourse->idnumber, $course2['idnumber']); | |
348 | $this->assertEquals($dbcourse->summary, $course2['summary']); | |
349 | $this->assertEquals($dbcourse->summaryformat, $course2['summaryformat']); | |
350 | $this->assertEquals($dbcourse->format, $course2['format']); | |
351 | $this->assertEquals($dbcourse->showgrades, $course2['showgrades']); | |
352 | $this->assertEquals($dbcourse->newsitems, $course2['newsitems']); | |
353 | $this->assertEquals($dbcourse->startdate, $course2['startdate']); | |
354 | $this->assertEquals($dbcourse->numsections, $course2['numsections']); | |
355 | $this->assertEquals($dbcourse->maxbytes, $course2['maxbytes']); | |
356 | $this->assertEquals($dbcourse->showreports, $course2['showreports']); | |
357 | $this->assertEquals($dbcourse->visible, $course2['visible']); | |
358 | $this->assertEquals($dbcourse->hiddensections, $course2['hiddensections']); | |
359 | $this->assertEquals($dbcourse->groupmode, $course2['groupmode']); | |
360 | $this->assertEquals($dbcourse->groupmodeforce, $course2['groupmodeforce']); | |
361 | $this->assertEquals($dbcourse->defaultgroupingid, $course2['defaultgroupingid']); | |
362 | $this->assertEquals($dbcourse->completionnotify, $course2['completionnotify']); | |
363 | $this->assertEquals($dbcourse->lang, $course2['lang']); | |
364 | ||
365 | if (!empty($CFG->allowcoursethemes)) { | |
366 | $this->assertEquals($dbcourse->theme, $course2['forcetheme']); | |
367 | } | |
368 | ||
369 | if (completion_info::is_enabled_for_site()) { | |
370 | $this->assertEquals($dbcourse->enablecompletion, $course2['enabledcompletion']); | |
371 | $this->assertEquals($dbcourse->completionstartonenrol, $course2['completionstartonenrol']); | |
372 | } else { | |
373 | $this->assertEquals($dbcourse->enablecompletion, 0); | |
374 | $this->assertEquals($dbcourse->completionstartonenrol, 0); | |
375 | } | |
376 | ||
377 | } else if ($createdcourse['shortname'] == $course1['shortname']) { | |
378 | $courseconfig = get_config('moodlecourse'); | |
379 | $this->assertEquals($dbcourse->fullname, $course1['fullname']); | |
380 | $this->assertEquals($dbcourse->shortname, $course1['shortname']); | |
381 | $this->assertEquals($dbcourse->category, $course1['categoryid']); | |
382 | $this->assertEquals($dbcourse->summaryformat, FORMAT_HTML); | |
383 | $this->assertEquals($dbcourse->format, $courseconfig->format); | |
384 | $this->assertEquals($dbcourse->showgrades, $courseconfig->showgrades); | |
385 | $this->assertEquals($dbcourse->newsitems, $courseconfig->newsitems); | |
386 | $this->assertEquals($dbcourse->numsections, $courseconfig->numsections); | |
387 | $this->assertEquals($dbcourse->maxbytes, $courseconfig->maxbytes); | |
388 | $this->assertEquals($dbcourse->showreports, $courseconfig->showreports); | |
389 | $this->assertEquals($dbcourse->hiddensections, $courseconfig->hiddensections); | |
390 | $this->assertEquals($dbcourse->groupmode, $courseconfig->groupmode); | |
391 | $this->assertEquals($dbcourse->groupmodeforce, $courseconfig->groupmodeforce); | |
392 | $this->assertEquals($dbcourse->defaultgroupingid, 0); | |
393 | } else { | |
394 | throw moodle_exception('Unexpected shortname'); | |
395 | } | |
396 | } | |
397 | ||
398 | // Call without required capability | |
399 | $this->unassignUserCapability('moodle/course:create', $contextid, $roleid); | |
400 | $this->setExpectedException('required_capability_exception'); | |
401 | $createdsubcats = core_course_external::create_courses($courses); | |
402 | } | |
403 | ||
404 | /** | |
405 | * Test delete_courses | |
406 | */ | |
407 | public function test_delete_courses() { | |
408 | global $DB, $USER; | |
409 | ||
410 | $this->resetAfterTest(true); | |
411 | ||
412 | // Admin can delete a course. | |
413 | $this->setAdminUser(); | |
414 | // Validate_context() will fail as the email is not set by $this->setAdminUser(). | |
415 | $USER->email = 'emailtopass@contextvalidation.me'; | |
416 | ||
417 | $course1 = self::getDataGenerator()->create_course(); | |
418 | $course2 = self::getDataGenerator()->create_course(); | |
419 | $course3 = self::getDataGenerator()->create_course(); | |
420 | ||
421 | // Delete courses. | |
422 | core_course_external::delete_courses(array($course1->id, $course2->id)); | |
423 | ||
424 | // Check $course 1 and 2 are deleted. | |
425 | $notdeletedcount = $DB->count_records_select('course', | |
426 | 'id IN ( ' . $course1->id . ',' . $course2->id . ')'); | |
427 | $this->assertEquals(0, $notdeletedcount); | |
428 | ||
429 | // Fail when the user is not allow to access the course (enrolled) or is not admin. | |
430 | $this->setGuestUser(); | |
431 | $this->setExpectedException('require_login_exception'); | |
432 | $createdsubcats = core_course_external::delete_courses(array($course3->id)); | |
433 | } | |
434 | ||
435 | /** | |
436 | * Test get_courses | |
437 | */ | |
438 | public function test_get_courses () { | |
439 | global $DB; | |
440 | ||
441 | $this->resetAfterTest(true); | |
442 | ||
443 | $coursedata['idnumber'] = 'idnumbercourse1'; | |
444 | $coursedata['fullname'] = 'Course 1 for PHPunit test'; | |
445 | $coursedata['summary'] = 'Course 1 description'; | |
446 | $coursedata['summaryformat'] = FORMAT_MOODLE; | |
447 | $course1 = self::getDataGenerator()->create_course($coursedata); | |
448 | $course2 = self::getDataGenerator()->create_course(); | |
449 | $course3 = self::getDataGenerator()->create_course(); | |
450 | ||
451 | // Set the required capabilities by the external function. | |
452 | $context = context_system::instance(); | |
453 | $roleid = $this->assignUserCapability('moodle/course:view', $context->id); | |
454 | $this->assignUserCapability('moodle/course:update', | |
455 | context_course::instance($course1->id)->id, $roleid); | |
456 | $this->assignUserCapability('moodle/course:update', | |
457 | context_course::instance($course2->id)->id, $roleid); | |
458 | $this->assignUserCapability('moodle/course:update', | |
459 | context_course::instance($course3->id)->id, $roleid); | |
460 | ||
461 | $courses = core_course_external::get_courses(array('ids' => | |
462 | array($course1->id, $course2->id))); | |
463 | ||
464 | // Check we retrieve the good total number of categories. | |
465 | $this->assertEquals(2, count($courses)); | |
466 | ||
467 | // Check the return values for course 1 | |
468 | $dbcourse = $DB->get_record('course', array('id' => $course1->id)); | |
469 | $this->assertEquals($courses[0]['id'], $dbcourse->id); | |
470 | $this->assertEquals($courses[0]['idnumber'], $coursedata['idnumber']); | |
471 | $this->assertEquals($courses[0]['fullname'], $coursedata['fullname']); | |
472 | $this->assertEquals($courses[0]['summary'], $coursedata['summary']); | |
473 | $this->assertEquals($courses[0]['summaryformat'], FORMAT_HTML); | |
474 | $this->assertEquals($courses[0]['shortname'], $dbcourse->shortname); | |
475 | $this->assertEquals($courses[0]['categoryid'], $dbcourse->category); | |
476 | $this->assertEquals($courses[0]['format'], $dbcourse->format); | |
477 | $this->assertEquals($courses[0]['showgrades'], $dbcourse->showgrades); | |
478 | $this->assertEquals($courses[0]['newsitems'], $dbcourse->newsitems); | |
479 | $this->assertEquals($courses[0]['startdate'], $dbcourse->startdate); | |
480 | $this->assertEquals($courses[0]['numsections'], $dbcourse->numsections); | |
481 | $this->assertEquals($courses[0]['maxbytes'], $dbcourse->maxbytes); | |
482 | $this->assertEquals($courses[0]['showreports'], $dbcourse->showreports); | |
483 | $this->assertEquals($courses[0]['visible'], $dbcourse->visible); | |
484 | $this->assertEquals($courses[0]['hiddensections'], $dbcourse->hiddensections); | |
485 | $this->assertEquals($courses[0]['groupmode'], $dbcourse->groupmode); | |
486 | $this->assertEquals($courses[0]['groupmodeforce'], $dbcourse->groupmodeforce); | |
487 | $this->assertEquals($courses[0]['defaultgroupingid'], $dbcourse->defaultgroupingid); | |
488 | $this->assertEquals($courses[0]['completionnotify'], $dbcourse->completionnotify); | |
489 | $this->assertEquals($courses[0]['lang'], $dbcourse->lang); | |
490 | $this->assertEquals($courses[0]['forcetheme'], $dbcourse->theme); | |
491 | $this->assertEquals($courses[0]['completionstartonenrol'], $dbcourse->completionstartonenrol); | |
492 | $this->assertEquals($courses[0]['enablecompletion'], $dbcourse->enablecompletion); | |
493 | $this->assertEquals($courses[0]['completionstartonenrol'], $dbcourse->completionstartonenrol); | |
494 | ||
495 | // Get all courses in the DB | |
496 | $courses = core_course_external::get_courses(array()); | |
497 | $this->assertEquals($DB->count_records('course'), count($courses)); | |
498 | } | |
499 | ||
500 | /** | |
501 | * Test get_course_contents | |
502 | */ | |
503 | public function test_get_course_contents() { | |
504 | $this->resetAfterTest(true); | |
505 | ||
506 | $course = self::getDataGenerator()->create_course(); | |
507 | $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id)); | |
508 | $forumcm = get_coursemodule_from_id('forum', $forum->cmid); | |
509 | $forumcontext = context_module::instance($forum->cmid); | |
510 | $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id)); | |
511 | $datacontext = context_module::instance($data->cmid); | |
512 | $datacm = get_coursemodule_from_instance('page', $data->id); | |
513 | $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id)); | |
514 | $pagecontext = context_module::instance($page->cmid); | |
515 | $pagecm = get_coursemodule_from_instance('page', $page->id); | |
516 | ||
517 | // Set the required capabilities by the external function. | |
518 | $context = context_course::instance($course->id); | |
519 | $roleid = $this->assignUserCapability('moodle/course:view', $context->id); | |
520 | $this->assignUserCapability('moodle/course:update', $context->id, $roleid); | |
521 | ||
522 | $courses = core_course_external::get_course_contents($course->id, array()); | |
523 | ||
524 | // Check that the course has the 3 created modules | |
525 | $this->assertEquals(3, count($courses[0]['modules'])); | |
526 | } | |
527 | ||
528 | /** | |
529 | * Test duplicate_course | |
530 | */ | |
531 | public function test_duplicate_course() { | |
532 | $this->resetAfterTest(true); | |
533 | ||
534 | // Create one course with three modules. | |
535 | $course = self::getDataGenerator()->create_course(); | |
536 | $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id)); | |
537 | $forumcm = get_coursemodule_from_id('forum', $forum->cmid); | |
538 | $forumcontext = context_module::instance($forum->cmid); | |
539 | $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id)); | |
540 | $datacontext = context_module::instance($data->cmid); | |
541 | $datacm = get_coursemodule_from_instance('page', $data->id); | |
542 | $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id)); | |
543 | $pagecontext = context_module::instance($page->cmid); | |
544 | $pagecm = get_coursemodule_from_instance('page', $page->id); | |
545 | ||
546 | // Set the required capabilities by the external function. | |
547 | $coursecontext = context_course::instance($course->id); | |
548 | $categorycontext = context_coursecat::instance($course->category); | |
549 | $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id); | |
550 | $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid); | |
551 | $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid); | |
552 | $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid); | |
553 | $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid); | |
554 | // Optional capabilities to copy user data. | |
555 | $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid); | |
556 | $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid); | |
557 | ||
558 | $newcourse['fullname'] = 'Course duplicate'; | |
559 | $newcourse['shortname'] = 'courseduplicate'; | |
560 | $newcourse['categoryid'] = $course->category; | |
561 | $newcourse['visible'] = true; | |
562 | $newcourse['options'][] = array('name' => 'users', 'value' => true); | |
563 | ||
564 | $duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'], | |
565 | $newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']); | |
566 | ||
567 | // Check that the course has been duplicated. | |
568 | $this->assertEquals($newcourse['shortname'], $duplicate['shortname']); | |
569 | } | |
570 | } |