weekly release 2.3.3+
[moodle.git] / course / tests / externallib_test.php
CommitLineData
08641906
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 */
c3f59e04 40class core_course_external_testcase extends externallib_advanced_testcase {
08641906
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);
a66863c3
JM
168
169 $generatedcats = array();
08641906
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);
a66863c3 175 $generatedcats[$category1->id] = $category1;
08641906
JM
176 $category2 = self::getDataGenerator()->create_category(
177 array('parent' => $category1->id));
a66863c3 178 $generatedcats[$category2->id] = $category2;
08641906
JM
179 $category6 = self::getDataGenerator()->create_category(
180 array('parent' => $category1->id, 'visible' => 0));
a66863c3 181 $generatedcats[$category6->id] = $category6;
08641906 182 $category3 = self::getDataGenerator()->create_category();
a66863c3 183 $generatedcats[$category3->id] = $category3;
08641906
JM
184 $category4 = self::getDataGenerator()->create_category(
185 array('parent' => $category3->id));
a66863c3 186 $generatedcats[$category4->id] = $category4;
08641906
JM
187 $category5 = self::getDataGenerator()->create_category(
188 array('parent' => $category4->id));
a66863c3 189 $generatedcats[$category5->id] = $category5;
08641906
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
a66863c3
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 }
08641906
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;
7a4f9ba8 328 $course2['startdate'] = 1420092000; // 01/01/2015
08641906
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';
342 $courses = array($course1, $course2);
343
344 $createdcourses = core_course_external::create_courses($courses);
345
346 // Check that right number of courses were created.
347 $this->assertEquals(2, count($createdcourses));
348
349 // Check that the courses were correctly created.
350 foreach ($createdcourses as $createdcourse) {
351 $dbcourse = $DB->get_record('course', array('id' => $createdcourse['id']));
352
353 if ($createdcourse['shortname'] == $course2['shortname']) {
354 $this->assertEquals($dbcourse->fullname, $course2['fullname']);
355 $this->assertEquals($dbcourse->shortname, $course2['shortname']);
356 $this->assertEquals($dbcourse->category, $course2['categoryid']);
357 $this->assertEquals($dbcourse->idnumber, $course2['idnumber']);
358 $this->assertEquals($dbcourse->summary, $course2['summary']);
359 $this->assertEquals($dbcourse->summaryformat, $course2['summaryformat']);
360 $this->assertEquals($dbcourse->format, $course2['format']);
361 $this->assertEquals($dbcourse->showgrades, $course2['showgrades']);
362 $this->assertEquals($dbcourse->newsitems, $course2['newsitems']);
363 $this->assertEquals($dbcourse->startdate, $course2['startdate']);
364 $this->assertEquals($dbcourse->numsections, $course2['numsections']);
365 $this->assertEquals($dbcourse->maxbytes, $course2['maxbytes']);
366 $this->assertEquals($dbcourse->showreports, $course2['showreports']);
367 $this->assertEquals($dbcourse->visible, $course2['visible']);
368 $this->assertEquals($dbcourse->hiddensections, $course2['hiddensections']);
369 $this->assertEquals($dbcourse->groupmode, $course2['groupmode']);
370 $this->assertEquals($dbcourse->groupmodeforce, $course2['groupmodeforce']);
371 $this->assertEquals($dbcourse->defaultgroupingid, $course2['defaultgroupingid']);
372 $this->assertEquals($dbcourse->completionnotify, $course2['completionnotify']);
373 $this->assertEquals($dbcourse->lang, $course2['lang']);
374
375 if (!empty($CFG->allowcoursethemes)) {
376 $this->assertEquals($dbcourse->theme, $course2['forcetheme']);
377 }
378
379 if (completion_info::is_enabled_for_site()) {
380 $this->assertEquals($dbcourse->enablecompletion, $course2['enabledcompletion']);
381 $this->assertEquals($dbcourse->completionstartonenrol, $course2['completionstartonenrol']);
382 } else {
383 $this->assertEquals($dbcourse->enablecompletion, 0);
384 $this->assertEquals($dbcourse->completionstartonenrol, 0);
385 }
386
387 } else if ($createdcourse['shortname'] == $course1['shortname']) {
388 $courseconfig = get_config('moodlecourse');
389 $this->assertEquals($dbcourse->fullname, $course1['fullname']);
390 $this->assertEquals($dbcourse->shortname, $course1['shortname']);
391 $this->assertEquals($dbcourse->category, $course1['categoryid']);
392 $this->assertEquals($dbcourse->summaryformat, FORMAT_HTML);
393 $this->assertEquals($dbcourse->format, $courseconfig->format);
394 $this->assertEquals($dbcourse->showgrades, $courseconfig->showgrades);
395 $this->assertEquals($dbcourse->newsitems, $courseconfig->newsitems);
396 $this->assertEquals($dbcourse->numsections, $courseconfig->numsections);
397 $this->assertEquals($dbcourse->maxbytes, $courseconfig->maxbytes);
398 $this->assertEquals($dbcourse->showreports, $courseconfig->showreports);
399 $this->assertEquals($dbcourse->hiddensections, $courseconfig->hiddensections);
400 $this->assertEquals($dbcourse->groupmode, $courseconfig->groupmode);
401 $this->assertEquals($dbcourse->groupmodeforce, $courseconfig->groupmodeforce);
402 $this->assertEquals($dbcourse->defaultgroupingid, 0);
403 } else {
404 throw moodle_exception('Unexpected shortname');
405 }
406 }
407
408 // Call without required capability
409 $this->unassignUserCapability('moodle/course:create', $contextid, $roleid);
410 $this->setExpectedException('required_capability_exception');
411 $createdsubcats = core_course_external::create_courses($courses);
412 }
413
414 /**
415 * Test delete_courses
416 */
417 public function test_delete_courses() {
418 global $DB, $USER;
419
420 $this->resetAfterTest(true);
421
422 // Admin can delete a course.
423 $this->setAdminUser();
424 // Validate_context() will fail as the email is not set by $this->setAdminUser().
425 $USER->email = 'emailtopass@contextvalidation.me';
426
427 $course1 = self::getDataGenerator()->create_course();
428 $course2 = self::getDataGenerator()->create_course();
429 $course3 = self::getDataGenerator()->create_course();
430
431 // Delete courses.
432 core_course_external::delete_courses(array($course1->id, $course2->id));
433
434 // Check $course 1 and 2 are deleted.
435 $notdeletedcount = $DB->count_records_select('course',
436 'id IN ( ' . $course1->id . ',' . $course2->id . ')');
437 $this->assertEquals(0, $notdeletedcount);
438
439 // Fail when the user is not allow to access the course (enrolled) or is not admin.
440 $this->setGuestUser();
441 $this->setExpectedException('require_login_exception');
442 $createdsubcats = core_course_external::delete_courses(array($course3->id));
443 }
444
445 /**
446 * Test get_courses
447 */
448 public function test_get_courses () {
449 global $DB;
450
451 $this->resetAfterTest(true);
452
a66863c3 453 $generatedcourses = array();
08641906
JM
454 $coursedata['idnumber'] = 'idnumbercourse1';
455 $coursedata['fullname'] = 'Course 1 for PHPunit test';
456 $coursedata['summary'] = 'Course 1 description';
457 $coursedata['summaryformat'] = FORMAT_MOODLE;
458 $course1 = self::getDataGenerator()->create_course($coursedata);
a66863c3 459 $generatedcourses[$course1->id] = $course1;
08641906 460 $course2 = self::getDataGenerator()->create_course();
a66863c3 461 $generatedcourses[$course2->id] = $course2;
08641906 462 $course3 = self::getDataGenerator()->create_course();
a66863c3 463 $generatedcourses[$course3->id] = $course3;
08641906
JM
464
465 // Set the required capabilities by the external function.
466 $context = context_system::instance();
467 $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
468 $this->assignUserCapability('moodle/course:update',
469 context_course::instance($course1->id)->id, $roleid);
470 $this->assignUserCapability('moodle/course:update',
471 context_course::instance($course2->id)->id, $roleid);
472 $this->assignUserCapability('moodle/course:update',
473 context_course::instance($course3->id)->id, $roleid);
474
475 $courses = core_course_external::get_courses(array('ids' =>
476 array($course1->id, $course2->id)));
477
478 // Check we retrieve the good total number of categories.
479 $this->assertEquals(2, count($courses));
480
a66863c3
JM
481 foreach ($courses as $course) {
482 $dbcourse = $generatedcourses[$course['id']];
483 $this->assertEquals($course['idnumber'], $dbcourse->idnumber);
484 $this->assertEquals($course['fullname'], $dbcourse->fullname);
485 $this->assertEquals($course['summary'], $dbcourse->summary);
486 $this->assertEquals($course['summaryformat'], FORMAT_HTML);
487 $this->assertEquals($course['shortname'], $dbcourse->shortname);
488 $this->assertEquals($course['categoryid'], $dbcourse->category);
489 $this->assertEquals($course['format'], $dbcourse->format);
490 $this->assertEquals($course['showgrades'], $dbcourse->showgrades);
491 $this->assertEquals($course['newsitems'], $dbcourse->newsitems);
492 $this->assertEquals($course['startdate'], $dbcourse->startdate);
493 $this->assertEquals($course['numsections'], $dbcourse->numsections);
494 $this->assertEquals($course['maxbytes'], $dbcourse->maxbytes);
495 $this->assertEquals($course['showreports'], $dbcourse->showreports);
496 $this->assertEquals($course['visible'], $dbcourse->visible);
497 $this->assertEquals($course['hiddensections'], $dbcourse->hiddensections);
498 $this->assertEquals($course['groupmode'], $dbcourse->groupmode);
499 $this->assertEquals($course['groupmodeforce'], $dbcourse->groupmodeforce);
500 $this->assertEquals($course['defaultgroupingid'], $dbcourse->defaultgroupingid);
501 $this->assertEquals($course['completionnotify'], $dbcourse->completionnotify);
502 $this->assertEquals($course['lang'], $dbcourse->lang);
503 $this->assertEquals($course['forcetheme'], $dbcourse->theme);
504 $this->assertEquals($course['completionstartonenrol'], $dbcourse->completionstartonenrol);
505 $this->assertEquals($course['enablecompletion'], $dbcourse->enablecompletion);
506 $this->assertEquals($course['completionstartonenrol'], $dbcourse->completionstartonenrol);
507 }
08641906
JM
508
509 // Get all courses in the DB
510 $courses = core_course_external::get_courses(array());
511 $this->assertEquals($DB->count_records('course'), count($courses));
512 }
513
514 /**
515 * Test get_course_contents
516 */
517 public function test_get_course_contents() {
518 $this->resetAfterTest(true);
519
520 $course = self::getDataGenerator()->create_course();
521 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
522 $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
523 $forumcontext = context_module::instance($forum->cmid);
524 $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
525 $datacontext = context_module::instance($data->cmid);
526 $datacm = get_coursemodule_from_instance('page', $data->id);
527 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
528 $pagecontext = context_module::instance($page->cmid);
529 $pagecm = get_coursemodule_from_instance('page', $page->id);
530
531 // Set the required capabilities by the external function.
532 $context = context_course::instance($course->id);
533 $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
534 $this->assignUserCapability('moodle/course:update', $context->id, $roleid);
535
536 $courses = core_course_external::get_course_contents($course->id, array());
537
538 // Check that the course has the 3 created modules
539 $this->assertEquals(3, count($courses[0]['modules']));
540 }
541
542 /**
543 * Test duplicate_course
544 */
545 public function test_duplicate_course() {
546 $this->resetAfterTest(true);
547
548 // Create one course with three modules.
549 $course = self::getDataGenerator()->create_course();
550 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
551 $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
552 $forumcontext = context_module::instance($forum->cmid);
553 $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
554 $datacontext = context_module::instance($data->cmid);
555 $datacm = get_coursemodule_from_instance('page', $data->id);
556 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
557 $pagecontext = context_module::instance($page->cmid);
558 $pagecm = get_coursemodule_from_instance('page', $page->id);
559
560 // Set the required capabilities by the external function.
561 $coursecontext = context_course::instance($course->id);
562 $categorycontext = context_coursecat::instance($course->category);
563 $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id);
564 $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid);
565 $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid);
566 $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid);
567 $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid);
568 // Optional capabilities to copy user data.
569 $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid);
570 $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid);
571
572 $newcourse['fullname'] = 'Course duplicate';
573 $newcourse['shortname'] = 'courseduplicate';
574 $newcourse['categoryid'] = $course->category;
575 $newcourse['visible'] = true;
576 $newcourse['options'][] = array('name' => 'users', 'value' => true);
577
578 $duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'],
579 $newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']);
580
581 // Check that the course has been duplicated.
582 $this->assertEquals($newcourse['shortname'], $duplicate['shortname']);
583 }
584}