MDL-37696 SCORM css: don't include style.css multiple times
[moodle.git] / course / tests / externallib_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * 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  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
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_advanced_testcase {
42     /**
43      * Tests set up
44      */
45     protected function setUp() {
46         global $CFG;
47         require_once($CFG->dirroot . '/course/externallib.php');
48     }
50     /**
51      * Test create_categories
52      */
53     public function test_create_categories() {
55         global $DB;
57         $this->resetAfterTest(true);
59         // Set the required capabilities by the external function
60         $contextid = context_system::instance()->id;
61         $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
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         );
77         $createdcats = core_course_external::create_categories($categories);
79         // We need to execute the return values cleaning process to simulate the web service server.
80         $createdcats = external_api::clean_returnvalue(core_course_external::create_categories_returns(), $createdcats);
82         // Initially confirm that base data was inserted correctly.
83         $this->assertEquals($category1->name, $createdcats[0]['name']);
84         $this->assertEquals($category2->name, $createdcats[1]['name']);
86         // Save the ids.
87         $category1->id = $createdcats[0]['id'];
88         $category2->id = $createdcats[1]['id'];
90         // Create on sub category.
91         $category3 = new stdClass();
92         $category3->name = 'Sub Root Test Category 3';
93         $subcategories = array(
94             array('name' => $category3->name, 'parent' => $category1->id)
95         );
97         $createdsubcats = core_course_external::create_categories($subcategories);
99         // We need to execute the return values cleaning process to simulate the web service server.
100         $createdsubcats = external_api::clean_returnvalue(core_course_external::create_categories_returns(), $createdsubcats);
102         // Confirm that sub categories were inserted correctly.
103         $this->assertEquals($category3->name, $createdsubcats[0]['name']);
105         // Save the ids.
106         $category3->id = $createdsubcats[0]['id'];
108         // Calling the ws function should provide a new sortorder to give category1,
109         // category2, category3. New course categories are ordered by id not name.
110         $category1 = $DB->get_record('course_categories', array('id' => $category1->id));
111         $category2 = $DB->get_record('course_categories', array('id' => $category2->id));
112         $category3 = $DB->get_record('course_categories', array('id' => $category3->id));
114         $this->assertGreaterThanOrEqual($category1->sortorder, $category3->sortorder);
115         $this->assertGreaterThanOrEqual($category2->sortorder, $category3->sortorder);
117         // Call without required capability
118         $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
119         $this->setExpectedException('required_capability_exception');
120         $createdsubcats = core_course_external::create_categories($subcategories);
122     }
124     /**
125      * Test delete categories
126      */
127     public function test_delete_categories() {
128         global $DB;
130         $this->resetAfterTest(true);
132         // Set the required capabilities by the external function
133         $contextid = context_system::instance()->id;
134         $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
136         $category1  = self::getDataGenerator()->create_category();
137         $category2  = self::getDataGenerator()->create_category(
138                 array('parent' => $category1->id));
139         $category3  = self::getDataGenerator()->create_category();
140         $category4  = self::getDataGenerator()->create_category(
141                 array('parent' => $category3->id));
142         $category5  = self::getDataGenerator()->create_category(
143                 array('parent' => $category4->id));
145         //delete category 1 and 2 + delete category 4, category 5 moved under category 3
146         core_course_external::delete_categories(array(
147             array('id' => $category1->id, 'recursive' => 1),
148             array('id' => $category4->id)
149         ));
151         //check $category 1 and 2 are deleted
152         $notdeletedcount = $DB->count_records_select('course_categories',
153             'id IN ( ' . $category1->id . ',' . $category2->id . ',' . $category4->id . ')');
154         $this->assertEquals(0, $notdeletedcount);
156         //check that $category5 as $category3 for parent
157         $dbcategory5 = $DB->get_record('course_categories', array('id' => $category5->id));
158         $this->assertEquals($dbcategory5->path, $category3->path . '/' . $category5->id);
160          // Call without required capability
161         $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
162         $this->setExpectedException('required_capability_exception');
163         $createdsubcats = core_course_external::delete_categories(
164                 array(array('id' => $category3->id)));
165     }
167     /**
168      * Test get categories
169      */
170     public function test_get_categories() {
171         global $DB;
173         $this->resetAfterTest(true);
175         $generatedcats = array();
176         $category1data['idnumber'] = 'idnumbercat1';
177         $category1data['name'] = 'Category 1 for PHPunit test';
178         $category1data['description'] = 'Category 1 description';
179         $category1data['descriptionformat'] = FORMAT_MOODLE;
180         $category1  = self::getDataGenerator()->create_category($category1data);
181         $generatedcats[$category1->id] = $category1;
182         $category2  = self::getDataGenerator()->create_category(
183                 array('parent' => $category1->id));
184         $generatedcats[$category2->id] = $category2;
185         $category6  = self::getDataGenerator()->create_category(
186                 array('parent' => $category1->id, 'visible' => 0));
187         $generatedcats[$category6->id] = $category6;
188         $category3  = self::getDataGenerator()->create_category();
189         $generatedcats[$category3->id] = $category3;
190         $category4  = self::getDataGenerator()->create_category(
191                 array('parent' => $category3->id));
192         $generatedcats[$category4->id] = $category4;
193         $category5  = self::getDataGenerator()->create_category(
194                 array('parent' => $category4->id));
195         $generatedcats[$category5->id] = $category5;
197         // Set the required capabilities by the external function.
198         $context = context_system::instance();
199         $roleid = $this->assignUserCapability('moodle/category:manage', $context->id);
201         // Retrieve category1 + sub-categories except not visible ones
202         $categories = core_course_external::get_categories(array(
203             array('key' => 'id', 'value' => $category1->id),
204             array('key' => 'visible', 'value' => 1)), 1);
206         // We need to execute the return values cleaning process to simulate the web service server.
207         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
209         // Check we retrieve the good total number of categories.
210         $this->assertEquals(2, count($categories));
212         // Check the return values
213         foreach ($categories as $category) {
214             $generatedcat = $generatedcats[$category['id']];
215             $this->assertEquals($category['idnumber'], $generatedcat->idnumber);
216             $this->assertEquals($category['name'], $generatedcat->name);
217             $this->assertEquals($category['description'], $generatedcat->description);
218             $this->assertEquals($category['descriptionformat'], FORMAT_HTML);
219         }
221         // Check different params.
222         $categories = core_course_external::get_categories(array(
223             array('key' => 'id', 'value' => $category1->id),
224             array('key' => 'idnumber', 'value' => $category1->idnumber),
225             array('key' => 'visible', 'value' => 1)), 0);
227         // We need to execute the return values cleaning process to simulate the web service server.
228         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
230         $this->assertEquals(1, count($categories));
232         // Retrieve categories from parent.
233         $categories = core_course_external::get_categories(array(
234             array('key' => 'parent', 'value' => $category3->id)), 1);
235         $this->assertEquals(2, count($categories));
237         // Retrieve all categories.
238         $categories = core_course_external::get_categories();
240         // We need to execute the return values cleaning process to simulate the web service server.
241         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
243         $this->assertEquals($DB->count_records('course_categories'), count($categories));
245         // Call without required capability (it will fail cause of the search on idnumber).
246         $this->unassignUserCapability('moodle/category:manage', $context->id, $roleid);
247         $this->setExpectedException('moodle_exception');
248         $categories = core_course_external::get_categories(array(
249             array('key' => 'id', 'value' => $category1->id),
250             array('key' => 'idnumber', 'value' => $category1->idnumber),
251             array('key' => 'visible', 'value' => 1)), 0);
252     }
254     /**
255      * Test update_categories
256      */
257     public function test_update_categories() {
258         global $DB;
260         $this->resetAfterTest(true);
262         // Set the required capabilities by the external function
263         $contextid = context_system::instance()->id;
264         $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
266         // Create base categories.
267         $category1data['idnumber'] = 'idnumbercat1';
268         $category1data['name'] = 'Category 1 for PHPunit test';
269         $category1data['description'] = 'Category 1 description';
270         $category1data['descriptionformat'] = FORMAT_MOODLE;
271         $category1  = self::getDataGenerator()->create_category($category1data);
272         $category2  = self::getDataGenerator()->create_category(
273                 array('parent' => $category1->id));
274         $category3  = self::getDataGenerator()->create_category();
275         $category4  = self::getDataGenerator()->create_category(
276                 array('parent' => $category3->id));
277         $category5  = self::getDataGenerator()->create_category(
278                 array('parent' => $category4->id));
280         // We update all category1 attribut.
281         // Then we move cat4 and cat5 parent: cat3 => cat1
282         $categories = array(
283             array('id' => $category1->id,
284                 'name' => $category1->name . '_updated',
285                 'idnumber' => $category1->idnumber . '_updated',
286                 'description' => $category1->description . '_updated',
287                 'descriptionformat' => FORMAT_HTML,
288                 'theme' => $category1->theme),
289             array('id' => $category4->id, 'parent' => $category1->id));
291         core_course_external::update_categories($categories);
293         // Check the values were updated.
294         $dbcategories = $DB->get_records_select('course_categories',
295                 'id IN (' . $category1->id . ',' . $category2->id . ',' . $category2->id
296                 . ',' . $category3->id . ',' . $category4->id . ',' . $category5->id .')');
297         $this->assertEquals($category1->name . '_updated',
298                 $dbcategories[$category1->id]->name);
299         $this->assertEquals($category1->idnumber . '_updated',
300                 $dbcategories[$category1->id]->idnumber);
301         $this->assertEquals($category1->description . '_updated',
302                 $dbcategories[$category1->id]->description);
303         $this->assertEquals(FORMAT_HTML, $dbcategories[$category1->id]->descriptionformat);
305         // Check that category4 and category5 have been properly moved.
306         $this->assertEquals('/' . $category1->id . '/' . $category4->id,
307                 $dbcategories[$category4->id]->path);
308         $this->assertEquals('/' . $category1->id . '/' . $category4->id . '/' . $category5->id,
309                 $dbcategories[$category5->id]->path);
311         // Call without required capability.
312         $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
313         $this->setExpectedException('required_capability_exception');
314         core_course_external::update_categories($categories);
315     }
317     /**
318      * Test create_courses
319      */
320     public function test_create_courses() {
321         global $DB;
323         $this->resetAfterTest(true);
325         // Set the required capabilities by the external function
326         $contextid = context_system::instance()->id;
327         $roleid = $this->assignUserCapability('moodle/course:create', $contextid);
328         $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
330         $category  = self::getDataGenerator()->create_category();
332         // Create base categories.
333         $course1['fullname'] = 'Test course 1';
334         $course1['shortname'] = 'Testcourse1';
335         $course1['categoryid'] = $category->id;
336         $course2['fullname'] = 'Test course 2';
337         $course2['shortname'] = 'Testcourse2';
338         $course2['categoryid'] = $category->id;
339         $course2['idnumber'] = 'testcourse2idnumber';
340         $course2['summary'] = 'Description for course 2';
341         $course2['summaryformat'] = FORMAT_MOODLE;
342         $course2['format'] = 'weeks';
343         $course2['showgrades'] = 1;
344         $course2['newsitems'] = 3;
345         $course2['startdate'] = 1420092000; // 01/01/2015
346         $course2['numsections'] = 4;
347         $course2['maxbytes'] = 100000;
348         $course2['showreports'] = 1;
349         $course2['visible'] = 0;
350         $course2['hiddensections'] = 0;
351         $course2['groupmode'] = 0;
352         $course2['groupmodeforce'] = 0;
353         $course2['defaultgroupingid'] = 0;
354         $course2['enablecompletion'] = 1;
355         $course2['completionstartonenrol'] = 1;
356         $course2['completionnotify'] = 1;
357         $course2['lang'] = 'en';
358         $course2['forcetheme'] = 'base';
359         $courses = array($course1, $course2);
361         $createdcourses = core_course_external::create_courses($courses);
363         // We need to execute the return values cleaning process to simulate the web service server.
364         $createdcourses = external_api::clean_returnvalue(core_course_external::create_courses_returns(), $createdcourses);
366         // Check that right number of courses were created.
367         $this->assertEquals(2, count($createdcourses));
369         // Check that the courses were correctly created.
370         foreach ($createdcourses as $createdcourse) {
371             $dbcourse = $DB->get_record('course', array('id' => $createdcourse['id']));
373             if ($createdcourse['shortname'] == $course2['shortname']) {
374                 $this->assertEquals($dbcourse->fullname, $course2['fullname']);
375                 $this->assertEquals($dbcourse->shortname, $course2['shortname']);
376                 $this->assertEquals($dbcourse->category, $course2['categoryid']);
377                 $this->assertEquals($dbcourse->idnumber, $course2['idnumber']);
378                 $this->assertEquals($dbcourse->summary, $course2['summary']);
379                 $this->assertEquals($dbcourse->summaryformat, $course2['summaryformat']);
380                 $this->assertEquals($dbcourse->format, $course2['format']);
381                 $this->assertEquals($dbcourse->showgrades, $course2['showgrades']);
382                 $this->assertEquals($dbcourse->newsitems, $course2['newsitems']);
383                 $this->assertEquals($dbcourse->startdate, $course2['startdate']);
384                 $this->assertEquals($dbcourse->numsections, $course2['numsections']);
385                 $this->assertEquals($dbcourse->maxbytes, $course2['maxbytes']);
386                 $this->assertEquals($dbcourse->showreports, $course2['showreports']);
387                 $this->assertEquals($dbcourse->visible, $course2['visible']);
388                 $this->assertEquals($dbcourse->hiddensections, $course2['hiddensections']);
389                 $this->assertEquals($dbcourse->groupmode, $course2['groupmode']);
390                 $this->assertEquals($dbcourse->groupmodeforce, $course2['groupmodeforce']);
391                 $this->assertEquals($dbcourse->defaultgroupingid, $course2['defaultgroupingid']);
392                 $this->assertEquals($dbcourse->completionnotify, $course2['completionnotify']);
393                 $this->assertEquals($dbcourse->lang, $course2['lang']);
395                 if (!empty($CFG->allowcoursethemes)) {
396                     $this->assertEquals($dbcourse->theme, $course2['forcetheme']);
397                 }
399                 if (completion_info::is_enabled_for_site()) {
400                     $this->assertEquals($dbcourse->enablecompletion, $course2['enabledcompletion']);
401                     $this->assertEquals($dbcourse->completionstartonenrol, $course2['completionstartonenrol']);
402                 } else {
403                     $this->assertEquals($dbcourse->enablecompletion, 0);
404                     $this->assertEquals($dbcourse->completionstartonenrol, 0);
405                 }
407             } else if ($createdcourse['shortname'] == $course1['shortname']) {
408                 $courseconfig = get_config('moodlecourse');
409                 $this->assertEquals($dbcourse->fullname, $course1['fullname']);
410                 $this->assertEquals($dbcourse->shortname, $course1['shortname']);
411                 $this->assertEquals($dbcourse->category, $course1['categoryid']);
412                 $this->assertEquals($dbcourse->summaryformat, FORMAT_HTML);
413                 $this->assertEquals($dbcourse->format, $courseconfig->format);
414                 $this->assertEquals($dbcourse->showgrades, $courseconfig->showgrades);
415                 $this->assertEquals($dbcourse->newsitems, $courseconfig->newsitems);
416                 $this->assertEquals($dbcourse->numsections, $courseconfig->numsections);
417                 $this->assertEquals($dbcourse->maxbytes, $courseconfig->maxbytes);
418                 $this->assertEquals($dbcourse->showreports, $courseconfig->showreports);
419                 $this->assertEquals($dbcourse->hiddensections, $courseconfig->hiddensections);
420                 $this->assertEquals($dbcourse->groupmode, $courseconfig->groupmode);
421                 $this->assertEquals($dbcourse->groupmodeforce, $courseconfig->groupmodeforce);
422                 $this->assertEquals($dbcourse->defaultgroupingid, 0);
423             } else {
424                 throw moodle_exception('Unexpected shortname');
425             }
426         }
428         // Call without required capability
429         $this->unassignUserCapability('moodle/course:create', $contextid, $roleid);
430         $this->setExpectedException('required_capability_exception');
431         $createdsubcats = core_course_external::create_courses($courses);
432     }
434     /**
435      * Test delete_courses
436      */
437     public function test_delete_courses() {
438         global $DB, $USER;
440         $this->resetAfterTest(true);
442         // Admin can delete a course.
443         $this->setAdminUser();
444         // Validate_context() will fail as the email is not set by $this->setAdminUser().
445         $USER->email = 'emailtopass@contextvalidation.me';
447         $course1  = self::getDataGenerator()->create_course();
448         $course2  = self::getDataGenerator()->create_course();
449         $course3  = self::getDataGenerator()->create_course();
451         // Delete courses.
452         core_course_external::delete_courses(array($course1->id, $course2->id));
454         // Check $course 1 and 2 are deleted.
455         $notdeletedcount = $DB->count_records_select('course',
456             'id IN ( ' . $course1->id . ',' . $course2->id . ')');
457         $this->assertEquals(0, $notdeletedcount);
459          // Fail when the user is not allow to access the course (enrolled) or is not admin.
460         $this->setGuestUser();
461         $this->setExpectedException('require_login_exception');
462         $createdsubcats = core_course_external::delete_courses(array($course3->id));
463     }
465     /**
466      * Test get_courses
467      */
468     public function test_get_courses () {
469         global $DB;
471         $this->resetAfterTest(true);
473         $generatedcourses = array();
474         $coursedata['idnumber'] = 'idnumbercourse1';
475         $coursedata['fullname'] = 'Course 1 for PHPunit test';
476         $coursedata['summary'] = 'Course 1 description';
477         $coursedata['summaryformat'] = FORMAT_MOODLE;
478         $course1  = self::getDataGenerator()->create_course($coursedata);
479         $generatedcourses[$course1->id] = $course1;
480         $course2  = self::getDataGenerator()->create_course();
481         $generatedcourses[$course2->id] = $course2;
482         $course3  = self::getDataGenerator()->create_course();
483         $generatedcourses[$course3->id] = $course3;
485         // Set the required capabilities by the external function.
486         $context = context_system::instance();
487         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
488         $this->assignUserCapability('moodle/course:update',
489                 context_course::instance($course1->id)->id, $roleid);
490         $this->assignUserCapability('moodle/course:update',
491                 context_course::instance($course2->id)->id, $roleid);
492         $this->assignUserCapability('moodle/course:update',
493                 context_course::instance($course3->id)->id, $roleid);
495         $courses = core_course_external::get_courses(array('ids' =>
496             array($course1->id, $course2->id)));
498         // We need to execute the return values cleaning process to simulate the web service server.
499         $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
501         // Check we retrieve the good total number of categories.
502         $this->assertEquals(2, count($courses));
504         foreach ($courses as $course) {
505             $dbcourse = $generatedcourses[$course['id']];
506             $this->assertEquals($course['idnumber'], $dbcourse->idnumber);
507             $this->assertEquals($course['fullname'], $dbcourse->fullname);
508             $this->assertEquals($course['summary'], $dbcourse->summary);
509             $this->assertEquals($course['summaryformat'], FORMAT_HTML);
510             $this->assertEquals($course['shortname'], $dbcourse->shortname);
511             $this->assertEquals($course['categoryid'], $dbcourse->category);
512             $this->assertEquals($course['format'], $dbcourse->format);
513             $this->assertEquals($course['showgrades'], $dbcourse->showgrades);
514             $this->assertEquals($course['newsitems'], $dbcourse->newsitems);
515             $this->assertEquals($course['startdate'], $dbcourse->startdate);
516             $this->assertEquals($course['numsections'], $dbcourse->numsections);
517             $this->assertEquals($course['maxbytes'], $dbcourse->maxbytes);
518             $this->assertEquals($course['showreports'], $dbcourse->showreports);
519             $this->assertEquals($course['visible'], $dbcourse->visible);
520             $this->assertEquals($course['hiddensections'], $dbcourse->hiddensections);
521             $this->assertEquals($course['groupmode'], $dbcourse->groupmode);
522             $this->assertEquals($course['groupmodeforce'], $dbcourse->groupmodeforce);
523             $this->assertEquals($course['defaultgroupingid'], $dbcourse->defaultgroupingid);
524             $this->assertEquals($course['completionnotify'], $dbcourse->completionnotify);
525             $this->assertEquals($course['lang'], $dbcourse->lang);
526             $this->assertEquals($course['forcetheme'], $dbcourse->theme);
527             $this->assertEquals($course['completionstartonenrol'], $dbcourse->completionstartonenrol);
528             $this->assertEquals($course['enablecompletion'], $dbcourse->enablecompletion);
529             $this->assertEquals($course['completionstartonenrol'], $dbcourse->completionstartonenrol);
530         }
532         // Get all courses in the DB
533         $courses = core_course_external::get_courses(array());
535         // We need to execute the return values cleaning process to simulate the web service server.
536         $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
538         $this->assertEquals($DB->count_records('course'), count($courses));
539     }
541     /**
542      * Test get_course_contents
543      */
544     public function test_get_course_contents() {
545         $this->resetAfterTest(true);
547         $course  = self::getDataGenerator()->create_course();
548         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
549         $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
550         $forumcontext = context_module::instance($forum->cmid);
551         $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
552         $datacontext = context_module::instance($data->cmid);
553         $datacm = get_coursemodule_from_instance('page', $data->id);
554         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
555         $pagecontext = context_module::instance($page->cmid);
556         $pagecm = get_coursemodule_from_instance('page', $page->id);
558         // Set the required capabilities by the external function.
559         $context = context_course::instance($course->id);
560         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
561         $this->assignUserCapability('moodle/course:update', $context->id, $roleid);
563         $courses = core_course_external::get_course_contents($course->id, array());
565         // We need to execute the return values cleaning process to simulate the web service server.
566         $courses = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $courses);
568         // Check that the course has the 3 created modules
569         $this->assertEquals(3, count($courses[0]['modules']));
570     }
572     /**
573      * Test duplicate_course
574      */
575     public function test_duplicate_course() {
576         $this->resetAfterTest(true);
578         // Create one course with three modules.
579         $course  = self::getDataGenerator()->create_course();
580         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
581         $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
582         $forumcontext = context_module::instance($forum->cmid);
583         $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
584         $datacontext = context_module::instance($data->cmid);
585         $datacm = get_coursemodule_from_instance('page', $data->id);
586         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
587         $pagecontext = context_module::instance($page->cmid);
588         $pagecm = get_coursemodule_from_instance('page', $page->id);
590         // Set the required capabilities by the external function.
591         $coursecontext = context_course::instance($course->id);
592         $categorycontext = context_coursecat::instance($course->category);
593         $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id);
594         $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid);
595         $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid);
596         $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid);
597         $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid);
598         // Optional capabilities to copy user data.
599         $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid);
600         $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid);
602         $newcourse['fullname'] = 'Course duplicate';
603         $newcourse['shortname'] = 'courseduplicate';
604         $newcourse['categoryid'] = $course->category;
605         $newcourse['visible'] = true;
606         $newcourse['options'][] = array('name' => 'users', 'value' => true);
608         $duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'],
609                 $newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']);
611         // We need to execute the return values cleaning process to simulate the web service server.
612         $duplicate = external_api::clean_returnvalue(core_course_external::duplicate_course_returns(), $duplicate);
614         // Check that the course has been duplicated.
615         $this->assertEquals($newcourse['shortname'], $duplicate['shortname']);
616     }