weekly on-sync release 2.5dev
[moodle.git] / course / tests / externallib_test.php
CommitLineData
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
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29
30require_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 */
1b1ad55b 40class core_course_external_testcase extends externallib_advanced_testcase {
2a7a0216
JM
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);
7d6c58bc
JM
168
169 $generatedcats = array();
2a7a0216
JM
170 $category1data['idnumber'] = 'idnumbercat1';
171 $category1data['name'] = 'Category 1 for PHPunit test';
172 $category1data['description'] = 'Category 1 description';
173 $category1data['descriptionformat'] = FORMAT_MOODLE;
174 $category1 = self::getDataGenerator()->create_category($category1data);
7d6c58bc 175 $generatedcats[$category1->id] = $category1;
2a7a0216
JM
176 $category2 = self::getDataGenerator()->create_category(
177 array('parent' => $category1->id));
7d6c58bc 178 $generatedcats[$category2->id] = $category2;
2a7a0216
JM
179 $category6 = self::getDataGenerator()->create_category(
180 array('parent' => $category1->id, 'visible' => 0));
7d6c58bc 181 $generatedcats[$category6->id] = $category6;
2a7a0216 182 $category3 = self::getDataGenerator()->create_category();
7d6c58bc 183 $generatedcats[$category3->id] = $category3;
2a7a0216
JM
184 $category4 = self::getDataGenerator()->create_category(
185 array('parent' => $category3->id));
7d6c58bc 186 $generatedcats[$category4->id] = $category4;
2a7a0216
JM
187 $category5 = self::getDataGenerator()->create_category(
188 array('parent' => $category4->id));
7d6c58bc 189 $generatedcats[$category5->id] = $category5;
2a7a0216
JM
190
191 // Set the required capabilities by the external function.
192 $context = context_system::instance();
193 $roleid = $this->assignUserCapability('moodle/category:manage', $context->id);
194
195 // Retrieve category1 + sub-categories except not visible ones
196 $categories = core_course_external::get_categories(array(
197 array('key' => 'id', 'value' => $category1->id),
198 array('key' => 'visible', 'value' => 1)), 1);
199
200 // Check we retrieve the good total number of categories.
201 $this->assertEquals(2, count($categories));
202
203 // Check the return values
7d6c58bc
JM
204 foreach ($categories as $category) {
205 $generatedcat = $generatedcats[$category['id']];
206 $this->assertEquals($category['idnumber'], $generatedcat->idnumber);
207 $this->assertEquals($category['name'], $generatedcat->name);
208 $this->assertEquals($category['description'], $generatedcat->description);
209 $this->assertEquals($category['descriptionformat'], FORMAT_HTML);
210 }
2a7a0216
JM
211
212 // Check different params.
213 $categories = core_course_external::get_categories(array(
214 array('key' => 'id', 'value' => $category1->id),
215 array('key' => 'idnumber', 'value' => $category1->idnumber),
216 array('key' => 'visible', 'value' => 1)), 0);
217 $this->assertEquals(1, count($categories));
218
219 // Retrieve categories from parent.
220 $categories = core_course_external::get_categories(array(
221 array('key' => 'parent', 'value' => $category3->id)), 1);
222 $this->assertEquals(2, count($categories));
223
224 // Retrieve all categories.
225 $categories = core_course_external::get_categories();
226 $this->assertEquals($DB->count_records('course_categories'), count($categories));
227
228 // Call without required capability (it will fail cause of the search on idnumber).
229 $this->unassignUserCapability('moodle/category:manage', $context->id, $roleid);
230 $this->setExpectedException('moodle_exception');
231 $categories = core_course_external::get_categories(array(
232 array('key' => 'id', 'value' => $category1->id),
233 array('key' => 'idnumber', 'value' => $category1->idnumber),
234 array('key' => 'visible', 'value' => 1)), 0);
235 }
236
237 /**
238 * Test update_categories
239 */
240 public function test_update_categories() {
241 global $DB;
242
243 $this->resetAfterTest(true);
244
245 // Set the required capabilities by the external function
246 $contextid = context_system::instance()->id;
247 $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
248
249 // Create base categories.
250 $category1data['idnumber'] = 'idnumbercat1';
251 $category1data['name'] = 'Category 1 for PHPunit test';
252 $category1data['description'] = 'Category 1 description';
253 $category1data['descriptionformat'] = FORMAT_MOODLE;
254 $category1 = self::getDataGenerator()->create_category($category1data);
255 $category2 = self::getDataGenerator()->create_category(
256 array('parent' => $category1->id));
257 $category3 = self::getDataGenerator()->create_category();
258 $category4 = self::getDataGenerator()->create_category(
259 array('parent' => $category3->id));
260 $category5 = self::getDataGenerator()->create_category(
261 array('parent' => $category4->id));
262
263 // We update all category1 attribut.
264 // Then we move cat4 and cat5 parent: cat3 => cat1
265 $categories = array(
266 array('id' => $category1->id,
267 'name' => $category1->name . '_updated',
268 'idnumber' => $category1->idnumber . '_updated',
269 'description' => $category1->description . '_updated',
270 'descriptionformat' => FORMAT_HTML,
271 'theme' => $category1->theme),
272 array('id' => $category4->id, 'parent' => $category1->id));
273
274 core_course_external::update_categories($categories);
275
276 // Check the values were updated.
277 $dbcategories = $DB->get_records_select('course_categories',
278 'id IN (' . $category1->id . ',' . $category2->id . ',' . $category2->id
279 . ',' . $category3->id . ',' . $category4->id . ',' . $category5->id .')');
280 $this->assertEquals($category1->name . '_updated',
281 $dbcategories[$category1->id]->name);
282 $this->assertEquals($category1->idnumber . '_updated',
283 $dbcategories[$category1->id]->idnumber);
284 $this->assertEquals($category1->description . '_updated',
285 $dbcategories[$category1->id]->description);
286 $this->assertEquals(FORMAT_HTML, $dbcategories[$category1->id]->descriptionformat);
287
288 // Check that category4 and category5 have been properly moved.
289 $this->assertEquals('/' . $category1->id . '/' . $category4->id,
290 $dbcategories[$category4->id]->path);
291 $this->assertEquals('/' . $category1->id . '/' . $category4->id . '/' . $category5->id,
292 $dbcategories[$category5->id]->path);
293
294 // Call without required capability.
295 $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
296 $this->setExpectedException('required_capability_exception');
297 core_course_external::update_categories($categories);
298 }
299
300 /**
301 * Test create_courses
302 */
303 public function test_create_courses() {
304 global $DB;
305
306 $this->resetAfterTest(true);
307
308 // Set the required capabilities by the external function
309 $contextid = context_system::instance()->id;
310 $roleid = $this->assignUserCapability('moodle/course:create', $contextid);
311 $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
312
313 $category = self::getDataGenerator()->create_category();
314
315 // Create base categories.
316 $course1['fullname'] = 'Test course 1';
317 $course1['shortname'] = 'Testcourse1';
318 $course1['categoryid'] = $category->id;
319 $course2['fullname'] = 'Test course 2';
320 $course2['shortname'] = 'Testcourse2';
321 $course2['categoryid'] = $category->id;
322 $course2['idnumber'] = 'testcourse2idnumber';
323 $course2['summary'] = 'Description for course 2';
324 $course2['summaryformat'] = FORMAT_MOODLE;
325 $course2['format'] = 'weeks';
326 $course2['showgrades'] = 1;
327 $course2['newsitems'] = 3;
4491273b 328 $course2['startdate'] = 1420092000; // 01/01/2015
2a7a0216
JM
329 $course2['numsections'] = 4;
330 $course2['maxbytes'] = 100000;
331 $course2['showreports'] = 1;
332 $course2['visible'] = 0;
333 $course2['hiddensections'] = 0;
334 $course2['groupmode'] = 0;
335 $course2['groupmodeforce'] = 0;
336 $course2['defaultgroupingid'] = 0;
337 $course2['enablecompletion'] = 1;
338 $course2['completionstartonenrol'] = 1;
339 $course2['completionnotify'] = 1;
340 $course2['lang'] = 'en';
341 $course2['forcetheme'] = 'base';
0e984d98
MG
342 $course3['fullname'] = 'Test course 3';
343 $course3['shortname'] = 'Testcourse3';
344 $course3['categoryid'] = $category->id;
345 $course3['format'] = 'topics';
346 $course3options = array('numsections' => 8,
347 'hiddensections' => 1,
348 'coursedisplay' => 1);
8d8d4da4 349 $course3['courseformatoptions'] = array();
0e984d98 350 foreach ($course3options as $key => $value) {
8d8d4da4 351 $course3['courseformatoptions'][] = array('name' => $key, 'value' => $value);
0e984d98 352 }
2a7a0216
JM
353 $courses = array($course1, $course2);
354
355 $createdcourses = core_course_external::create_courses($courses);
356
357 // Check that right number of courses were created.
358 $this->assertEquals(2, count($createdcourses));
359
360 // Check that the courses were correctly created.
361 foreach ($createdcourses as $createdcourse) {
850acb35 362 $courseinfo = course_get_format($createdcourse['id'])->get_course();
2a7a0216
JM
363
364 if ($createdcourse['shortname'] == $course2['shortname']) {
850acb35
MG
365 $this->assertEquals($courseinfo->fullname, $course2['fullname']);
366 $this->assertEquals($courseinfo->shortname, $course2['shortname']);
367 $this->assertEquals($courseinfo->category, $course2['categoryid']);
368 $this->assertEquals($courseinfo->idnumber, $course2['idnumber']);
369 $this->assertEquals($courseinfo->summary, $course2['summary']);
370 $this->assertEquals($courseinfo->summaryformat, $course2['summaryformat']);
371 $this->assertEquals($courseinfo->format, $course2['format']);
372 $this->assertEquals($courseinfo->showgrades, $course2['showgrades']);
373 $this->assertEquals($courseinfo->newsitems, $course2['newsitems']);
374 $this->assertEquals($courseinfo->startdate, $course2['startdate']);
375 $this->assertEquals($courseinfo->numsections, $course2['numsections']);
376 $this->assertEquals($courseinfo->maxbytes, $course2['maxbytes']);
377 $this->assertEquals($courseinfo->showreports, $course2['showreports']);
378 $this->assertEquals($courseinfo->visible, $course2['visible']);
379 $this->assertEquals($courseinfo->hiddensections, $course2['hiddensections']);
380 $this->assertEquals($courseinfo->groupmode, $course2['groupmode']);
381 $this->assertEquals($courseinfo->groupmodeforce, $course2['groupmodeforce']);
382 $this->assertEquals($courseinfo->defaultgroupingid, $course2['defaultgroupingid']);
383 $this->assertEquals($courseinfo->completionnotify, $course2['completionnotify']);
384 $this->assertEquals($courseinfo->lang, $course2['lang']);
2a7a0216
JM
385
386 if (!empty($CFG->allowcoursethemes)) {
850acb35 387 $this->assertEquals($courseinfo->theme, $course2['forcetheme']);
2a7a0216
JM
388 }
389
390 if (completion_info::is_enabled_for_site()) {
850acb35
MG
391 $this->assertEquals($courseinfo->enablecompletion, $course2['enabledcompletion']);
392 $this->assertEquals($courseinfo->completionstartonenrol, $course2['completionstartonenrol']);
2a7a0216 393 } else {
850acb35
MG
394 $this->assertEquals($courseinfo->enablecompletion, 0);
395 $this->assertEquals($courseinfo->completionstartonenrol, 0);
2a7a0216
JM
396 }
397
398 } else if ($createdcourse['shortname'] == $course1['shortname']) {
399 $courseconfig = get_config('moodlecourse');
850acb35
MG
400 $this->assertEquals($courseinfo->fullname, $course1['fullname']);
401 $this->assertEquals($courseinfo->shortname, $course1['shortname']);
402 $this->assertEquals($courseinfo->category, $course1['categoryid']);
403 $this->assertEquals($courseinfo->summaryformat, FORMAT_HTML);
404 $this->assertEquals($courseinfo->format, $courseconfig->format);
405 $this->assertEquals($courseinfo->showgrades, $courseconfig->showgrades);
406 $this->assertEquals($courseinfo->newsitems, $courseconfig->newsitems);
407 $this->assertEquals($courseinfo->maxbytes, $courseconfig->maxbytes);
408 $this->assertEquals($courseinfo->showreports, $courseconfig->showreports);
409 $this->assertEquals($courseinfo->groupmode, $courseconfig->groupmode);
410 $this->assertEquals($courseinfo->groupmodeforce, $courseconfig->groupmodeforce);
411 $this->assertEquals($courseinfo->defaultgroupingid, 0);
0e984d98 412 } else if ($createdcourse['shortname'] == $course3['shortname']) {
850acb35
MG
413 $this->assertEquals($courseinfo->fullname, $course3['fullname']);
414 $this->assertEquals($courseinfo->shortname, $course3['shortname']);
415 $this->assertEquals($courseinfo->category, $course3['categoryid']);
416 $this->assertEquals($courseinfo->format, $course3['format']);
417 $this->assertEquals($courseinfo->hiddensections, $course3options['hiddensections']);
418 $this->assertEquals($courseinfo->numsections, $course3options['numsections']);
419 $this->assertEquals($courseinfo->coursedisplay, $course3options['coursedisplay']);
2a7a0216
JM
420 } else {
421 throw moodle_exception('Unexpected shortname');
422 }
423 }
424
425 // Call without required capability
426 $this->unassignUserCapability('moodle/course:create', $contextid, $roleid);
427 $this->setExpectedException('required_capability_exception');
428 $createdsubcats = core_course_external::create_courses($courses);
429 }
430
431 /**
432 * Test delete_courses
433 */
434 public function test_delete_courses() {
435 global $DB, $USER;
436
437 $this->resetAfterTest(true);
438
439 // Admin can delete a course.
440 $this->setAdminUser();
441 // Validate_context() will fail as the email is not set by $this->setAdminUser().
442 $USER->email = 'emailtopass@contextvalidation.me';
443
444 $course1 = self::getDataGenerator()->create_course();
445 $course2 = self::getDataGenerator()->create_course();
446 $course3 = self::getDataGenerator()->create_course();
447
448 // Delete courses.
449 core_course_external::delete_courses(array($course1->id, $course2->id));
450
451 // Check $course 1 and 2 are deleted.
452 $notdeletedcount = $DB->count_records_select('course',
453 'id IN ( ' . $course1->id . ',' . $course2->id . ')');
454 $this->assertEquals(0, $notdeletedcount);
455
456 // Fail when the user is not allow to access the course (enrolled) or is not admin.
457 $this->setGuestUser();
458 $this->setExpectedException('require_login_exception');
459 $createdsubcats = core_course_external::delete_courses(array($course3->id));
460 }
461
462 /**
463 * Test get_courses
464 */
465 public function test_get_courses () {
466 global $DB;
467
468 $this->resetAfterTest(true);
469
7d6c58bc 470 $generatedcourses = array();
2a7a0216
JM
471 $coursedata['idnumber'] = 'idnumbercourse1';
472 $coursedata['fullname'] = 'Course 1 for PHPunit test';
473 $coursedata['summary'] = 'Course 1 description';
474 $coursedata['summaryformat'] = FORMAT_MOODLE;
475 $course1 = self::getDataGenerator()->create_course($coursedata);
7d6c58bc 476 $generatedcourses[$course1->id] = $course1;
2a7a0216 477 $course2 = self::getDataGenerator()->create_course();
7d6c58bc 478 $generatedcourses[$course2->id] = $course2;
0e984d98 479 $course3 = self::getDataGenerator()->create_course(array('format' => 'topics'));
7d6c58bc 480 $generatedcourses[$course3->id] = $course3;
2a7a0216
JM
481
482 // Set the required capabilities by the external function.
483 $context = context_system::instance();
484 $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
485 $this->assignUserCapability('moodle/course:update',
486 context_course::instance($course1->id)->id, $roleid);
487 $this->assignUserCapability('moodle/course:update',
488 context_course::instance($course2->id)->id, $roleid);
489 $this->assignUserCapability('moodle/course:update',
490 context_course::instance($course3->id)->id, $roleid);
491
492 $courses = core_course_external::get_courses(array('ids' =>
493 array($course1->id, $course2->id)));
494
495 // Check we retrieve the good total number of categories.
496 $this->assertEquals(2, count($courses));
497
7d6c58bc
JM
498 foreach ($courses as $course) {
499 $dbcourse = $generatedcourses[$course['id']];
500 $this->assertEquals($course['idnumber'], $dbcourse->idnumber);
501 $this->assertEquals($course['fullname'], $dbcourse->fullname);
502 $this->assertEquals($course['summary'], $dbcourse->summary);
503 $this->assertEquals($course['summaryformat'], FORMAT_HTML);
504 $this->assertEquals($course['shortname'], $dbcourse->shortname);
505 $this->assertEquals($course['categoryid'], $dbcourse->category);
506 $this->assertEquals($course['format'], $dbcourse->format);
507 $this->assertEquals($course['showgrades'], $dbcourse->showgrades);
508 $this->assertEquals($course['newsitems'], $dbcourse->newsitems);
509 $this->assertEquals($course['startdate'], $dbcourse->startdate);
510 $this->assertEquals($course['numsections'], $dbcourse->numsections);
511 $this->assertEquals($course['maxbytes'], $dbcourse->maxbytes);
512 $this->assertEquals($course['showreports'], $dbcourse->showreports);
513 $this->assertEquals($course['visible'], $dbcourse->visible);
514 $this->assertEquals($course['hiddensections'], $dbcourse->hiddensections);
515 $this->assertEquals($course['groupmode'], $dbcourse->groupmode);
516 $this->assertEquals($course['groupmodeforce'], $dbcourse->groupmodeforce);
517 $this->assertEquals($course['defaultgroupingid'], $dbcourse->defaultgroupingid);
518 $this->assertEquals($course['completionnotify'], $dbcourse->completionnotify);
519 $this->assertEquals($course['lang'], $dbcourse->lang);
520 $this->assertEquals($course['forcetheme'], $dbcourse->theme);
521 $this->assertEquals($course['completionstartonenrol'], $dbcourse->completionstartonenrol);
522 $this->assertEquals($course['enablecompletion'], $dbcourse->enablecompletion);
523 $this->assertEquals($course['completionstartonenrol'], $dbcourse->completionstartonenrol);
0e984d98 524 if ($dbcourse->format === 'topics') {
8d8d4da4
MG
525 $this->assertEquals($course['courseformatoptions'], array(
526 array('name' => 'numsections', 'value' => $dbcourse->numsections),
527 array('name' => 'hiddensections', 'value' => $dbcourse->hiddensections),
528 array('name' => 'coursedisplay', 'value' => $dbcourse->coursedisplay),
0e984d98
MG
529 ));
530 }
7d6c58bc 531 }
2a7a0216
JM
532
533 // Get all courses in the DB
534 $courses = core_course_external::get_courses(array());
535 $this->assertEquals($DB->count_records('course'), count($courses));
536 }
537
538 /**
539 * Test get_course_contents
540 */
541 public function test_get_course_contents() {
542 $this->resetAfterTest(true);
543
544 $course = self::getDataGenerator()->create_course();
545 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
546 $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
547 $forumcontext = context_module::instance($forum->cmid);
548 $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
549 $datacontext = context_module::instance($data->cmid);
550 $datacm = get_coursemodule_from_instance('page', $data->id);
551 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
552 $pagecontext = context_module::instance($page->cmid);
553 $pagecm = get_coursemodule_from_instance('page', $page->id);
554
555 // Set the required capabilities by the external function.
556 $context = context_course::instance($course->id);
557 $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
558 $this->assignUserCapability('moodle/course:update', $context->id, $roleid);
559
560 $courses = core_course_external::get_course_contents($course->id, array());
561
562 // Check that the course has the 3 created modules
563 $this->assertEquals(3, count($courses[0]['modules']));
564 }
565
566 /**
567 * Test duplicate_course
568 */
569 public function test_duplicate_course() {
570 $this->resetAfterTest(true);
571
572 // Create one course with three modules.
573 $course = self::getDataGenerator()->create_course();
574 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
575 $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
576 $forumcontext = context_module::instance($forum->cmid);
577 $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
578 $datacontext = context_module::instance($data->cmid);
579 $datacm = get_coursemodule_from_instance('page', $data->id);
580 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
581 $pagecontext = context_module::instance($page->cmid);
582 $pagecm = get_coursemodule_from_instance('page', $page->id);
583
584 // Set the required capabilities by the external function.
585 $coursecontext = context_course::instance($course->id);
586 $categorycontext = context_coursecat::instance($course->category);
587 $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id);
588 $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid);
589 $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid);
590 $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid);
591 $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid);
592 // Optional capabilities to copy user data.
593 $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid);
594 $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid);
595
596 $newcourse['fullname'] = 'Course duplicate';
597 $newcourse['shortname'] = 'courseduplicate';
598 $newcourse['categoryid'] = $course->category;
599 $newcourse['visible'] = true;
600 $newcourse['options'][] = array('name' => 'users', 'value' => true);
601
602 $duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'],
603 $newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']);
604
605 // Check that the course has been duplicated.
606 $this->assertEquals($newcourse['shortname'], $duplicate['shortname']);
607 }
608}