Merge branch 'MDL-49244-master' of git://github.com/jleyva/moodle
[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_externallib_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         // sortorder sequence (and sortorder) must be:
115         // category 1
116         //   category 3
117         // category 2
118         $this->assertGreaterThan($category1->sortorder, $category3->sortorder);
119         $this->assertGreaterThan($category3->sortorder, $category2->sortorder);
121         // Call without required capability
122         $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
123         $this->setExpectedException('required_capability_exception');
124         $createdsubcats = core_course_external::create_categories($subcategories);
126     }
128     /**
129      * Test delete categories
130      */
131     public function test_delete_categories() {
132         global $DB;
134         $this->resetAfterTest(true);
136         // Set the required capabilities by the external function
137         $contextid = context_system::instance()->id;
138         $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
140         $category1  = self::getDataGenerator()->create_category();
141         $category2  = self::getDataGenerator()->create_category(
142                 array('parent' => $category1->id));
143         $category3  = self::getDataGenerator()->create_category();
144         $category4  = self::getDataGenerator()->create_category(
145                 array('parent' => $category3->id));
146         $category5  = self::getDataGenerator()->create_category(
147                 array('parent' => $category4->id));
149         //delete category 1 and 2 + delete category 4, category 5 moved under category 3
150         core_course_external::delete_categories(array(
151             array('id' => $category1->id, 'recursive' => 1),
152             array('id' => $category4->id)
153         ));
155         //check $category 1 and 2 are deleted
156         $notdeletedcount = $DB->count_records_select('course_categories',
157             'id IN ( ' . $category1->id . ',' . $category2->id . ',' . $category4->id . ')');
158         $this->assertEquals(0, $notdeletedcount);
160         //check that $category5 as $category3 for parent
161         $dbcategory5 = $DB->get_record('course_categories', array('id' => $category5->id));
162         $this->assertEquals($dbcategory5->path, $category3->path . '/' . $category5->id);
164          // Call without required capability
165         $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
166         $this->setExpectedException('required_capability_exception');
167         $createdsubcats = core_course_external::delete_categories(
168                 array(array('id' => $category3->id)));
169     }
171     /**
172      * Test get categories
173      */
174     public function test_get_categories() {
175         global $DB;
177         $this->resetAfterTest(true);
179         $generatedcats = array();
180         $category1data['idnumber'] = 'idnumbercat1';
181         $category1data['name'] = 'Category 1 for PHPunit test';
182         $category1data['description'] = 'Category 1 description';
183         $category1data['descriptionformat'] = FORMAT_MOODLE;
184         $category1  = self::getDataGenerator()->create_category($category1data);
185         $generatedcats[$category1->id] = $category1;
186         $category2  = self::getDataGenerator()->create_category(
187                 array('parent' => $category1->id));
188         $generatedcats[$category2->id] = $category2;
189         $category6  = self::getDataGenerator()->create_category(
190                 array('parent' => $category1->id, 'visible' => 0));
191         $generatedcats[$category6->id] = $category6;
192         $category3  = self::getDataGenerator()->create_category();
193         $generatedcats[$category3->id] = $category3;
194         $category4  = self::getDataGenerator()->create_category(
195                 array('parent' => $category3->id));
196         $generatedcats[$category4->id] = $category4;
197         $category5  = self::getDataGenerator()->create_category(
198                 array('parent' => $category4->id));
199         $generatedcats[$category5->id] = $category5;
201         // Set the required capabilities by the external function.
202         $context = context_system::instance();
203         $roleid = $this->assignUserCapability('moodle/category:manage', $context->id);
205         // Retrieve category1 + sub-categories except not visible ones
206         $categories = core_course_external::get_categories(array(
207             array('key' => 'id', 'value' => $category1->id),
208             array('key' => 'visible', 'value' => 1)), 1);
210         // We need to execute the return values cleaning process to simulate the web service server.
211         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
213         // Check we retrieve the good total number of categories.
214         $this->assertEquals(2, count($categories));
216         // Check the return values
217         foreach ($categories as $category) {
218             $generatedcat = $generatedcats[$category['id']];
219             $this->assertEquals($category['idnumber'], $generatedcat->idnumber);
220             $this->assertEquals($category['name'], $generatedcat->name);
221             // Description was converted to the HTML format.
222             $this->assertEquals($category['description'], format_text($generatedcat->description, FORMAT_MOODLE, array('para' => false)));
223             $this->assertEquals($category['descriptionformat'], FORMAT_HTML);
224         }
226         // Check different params.
227         $categories = core_course_external::get_categories(array(
228             array('key' => 'id', 'value' => $category1->id),
229             array('key' => 'idnumber', 'value' => $category1->idnumber),
230             array('key' => 'visible', 'value' => 1)), 0);
232         // We need to execute the return values cleaning process to simulate the web service server.
233         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
235         $this->assertEquals(1, count($categories));
237         // Same query, but forcing a parameters clean.
238         $categories = core_course_external::get_categories(array(
239             array('key' => 'id', 'value' => "$category1->id"),
240             array('key' => 'idnumber', 'value' => $category1->idnumber),
241             array('key' => 'name', 'value' => $category1->name . "<br/>"),
242             array('key' => 'visible', 'value' => '1')), 0);
243         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
245         $this->assertEquals(1, count($categories));
247         // Retrieve categories from parent.
248         $categories = core_course_external::get_categories(array(
249             array('key' => 'parent', 'value' => $category3->id)), 1);
250         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
252         $this->assertEquals(2, count($categories));
254         // Retrieve all categories.
255         $categories = core_course_external::get_categories();
257         // We need to execute the return values cleaning process to simulate the web service server.
258         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
260         $this->assertEquals($DB->count_records('course_categories'), count($categories));
262         // Call without required capability (it will fail cause of the search on idnumber).
263         $this->unassignUserCapability('moodle/category:manage', $context->id, $roleid);
264         $this->setExpectedException('moodle_exception');
265         $categories = core_course_external::get_categories(array(
266             array('key' => 'id', 'value' => $category1->id),
267             array('key' => 'idnumber', 'value' => $category1->idnumber),
268             array('key' => 'visible', 'value' => 1)), 0);
269     }
271     /**
272      * Test update_categories
273      */
274     public function test_update_categories() {
275         global $DB;
277         $this->resetAfterTest(true);
279         // Set the required capabilities by the external function
280         $contextid = context_system::instance()->id;
281         $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
283         // Create base categories.
284         $category1data['idnumber'] = 'idnumbercat1';
285         $category1data['name'] = 'Category 1 for PHPunit test';
286         $category1data['description'] = 'Category 1 description';
287         $category1data['descriptionformat'] = FORMAT_MOODLE;
288         $category1  = self::getDataGenerator()->create_category($category1data);
289         $category2  = self::getDataGenerator()->create_category(
290                 array('parent' => $category1->id));
291         $category3  = self::getDataGenerator()->create_category();
292         $category4  = self::getDataGenerator()->create_category(
293                 array('parent' => $category3->id));
294         $category5  = self::getDataGenerator()->create_category(
295                 array('parent' => $category4->id));
297         // We update all category1 attribut.
298         // Then we move cat4 and cat5 parent: cat3 => cat1
299         $categories = array(
300             array('id' => $category1->id,
301                 'name' => $category1->name . '_updated',
302                 'idnumber' => $category1->idnumber . '_updated',
303                 'description' => $category1->description . '_updated',
304                 'descriptionformat' => FORMAT_HTML,
305                 'theme' => $category1->theme),
306             array('id' => $category4->id, 'parent' => $category1->id));
308         core_course_external::update_categories($categories);
310         // Check the values were updated.
311         $dbcategories = $DB->get_records_select('course_categories',
312                 'id IN (' . $category1->id . ',' . $category2->id . ',' . $category2->id
313                 . ',' . $category3->id . ',' . $category4->id . ',' . $category5->id .')');
314         $this->assertEquals($category1->name . '_updated',
315                 $dbcategories[$category1->id]->name);
316         $this->assertEquals($category1->idnumber . '_updated',
317                 $dbcategories[$category1->id]->idnumber);
318         $this->assertEquals($category1->description . '_updated',
319                 $dbcategories[$category1->id]->description);
320         $this->assertEquals(FORMAT_HTML, $dbcategories[$category1->id]->descriptionformat);
322         // Check that category4 and category5 have been properly moved.
323         $this->assertEquals('/' . $category1->id . '/' . $category4->id,
324                 $dbcategories[$category4->id]->path);
325         $this->assertEquals('/' . $category1->id . '/' . $category4->id . '/' . $category5->id,
326                 $dbcategories[$category5->id]->path);
328         // Call without required capability.
329         $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
330         $this->setExpectedException('required_capability_exception');
331         core_course_external::update_categories($categories);
332     }
334     /**
335      * Test create_courses
336      */
337     public function test_create_courses() {
338         global $DB;
340         $this->resetAfterTest(true);
342         // Enable course completion.
343         set_config('enablecompletion', 1);
344         // Enable course themes.
345         set_config('allowcoursethemes', 1);
347         // Set the required capabilities by the external function
348         $contextid = context_system::instance()->id;
349         $roleid = $this->assignUserCapability('moodle/course:create', $contextid);
350         $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
352         $category  = self::getDataGenerator()->create_category();
354         // Create base categories.
355         $course1['fullname'] = 'Test course 1';
356         $course1['shortname'] = 'Testcourse1';
357         $course1['categoryid'] = $category->id;
358         $course2['fullname'] = 'Test course 2';
359         $course2['shortname'] = 'Testcourse2';
360         $course2['categoryid'] = $category->id;
361         $course2['idnumber'] = 'testcourse2idnumber';
362         $course2['summary'] = 'Description for course 2';
363         $course2['summaryformat'] = FORMAT_MOODLE;
364         $course2['format'] = 'weeks';
365         $course2['showgrades'] = 1;
366         $course2['newsitems'] = 3;
367         $course2['startdate'] = 1420092000; // 01/01/2015
368         $course2['numsections'] = 4;
369         $course2['maxbytes'] = 100000;
370         $course2['showreports'] = 1;
371         $course2['visible'] = 0;
372         $course2['hiddensections'] = 0;
373         $course2['groupmode'] = 0;
374         $course2['groupmodeforce'] = 0;
375         $course2['defaultgroupingid'] = 0;
376         $course2['enablecompletion'] = 1;
377         $course2['completionnotify'] = 1;
378         $course2['lang'] = 'en';
379         $course2['forcetheme'] = 'base';
380         $course3['fullname'] = 'Test course 3';
381         $course3['shortname'] = 'Testcourse3';
382         $course3['categoryid'] = $category->id;
383         $course3['format'] = 'topics';
384         $course3options = array('numsections' => 8,
385             'hiddensections' => 1,
386             'coursedisplay' => 1);
387         $course3['courseformatoptions'] = array();
388         foreach ($course3options as $key => $value) {
389             $course3['courseformatoptions'][] = array('name' => $key, 'value' => $value);
390         }
391         $courses = array($course1, $course2, $course3);
393         $createdcourses = core_course_external::create_courses($courses);
395         // We need to execute the return values cleaning process to simulate the web service server.
396         $createdcourses = external_api::clean_returnvalue(core_course_external::create_courses_returns(), $createdcourses);
398         // Check that right number of courses were created.
399         $this->assertEquals(3, count($createdcourses));
401         // Check that the courses were correctly created.
402         foreach ($createdcourses as $createdcourse) {
403             $courseinfo = course_get_format($createdcourse['id'])->get_course();
405             if ($createdcourse['shortname'] == $course2['shortname']) {
406                 $this->assertEquals($courseinfo->fullname, $course2['fullname']);
407                 $this->assertEquals($courseinfo->shortname, $course2['shortname']);
408                 $this->assertEquals($courseinfo->category, $course2['categoryid']);
409                 $this->assertEquals($courseinfo->idnumber, $course2['idnumber']);
410                 $this->assertEquals($courseinfo->summary, $course2['summary']);
411                 $this->assertEquals($courseinfo->summaryformat, $course2['summaryformat']);
412                 $this->assertEquals($courseinfo->format, $course2['format']);
413                 $this->assertEquals($courseinfo->showgrades, $course2['showgrades']);
414                 $this->assertEquals($courseinfo->newsitems, $course2['newsitems']);
415                 $this->assertEquals($courseinfo->startdate, $course2['startdate']);
416                 $this->assertEquals($courseinfo->numsections, $course2['numsections']);
417                 $this->assertEquals($courseinfo->maxbytes, $course2['maxbytes']);
418                 $this->assertEquals($courseinfo->showreports, $course2['showreports']);
419                 $this->assertEquals($courseinfo->visible, $course2['visible']);
420                 $this->assertEquals($courseinfo->hiddensections, $course2['hiddensections']);
421                 $this->assertEquals($courseinfo->groupmode, $course2['groupmode']);
422                 $this->assertEquals($courseinfo->groupmodeforce, $course2['groupmodeforce']);
423                 $this->assertEquals($courseinfo->defaultgroupingid, $course2['defaultgroupingid']);
424                 $this->assertEquals($courseinfo->completionnotify, $course2['completionnotify']);
425                 $this->assertEquals($courseinfo->lang, $course2['lang']);
426                 $this->assertEquals($courseinfo->theme, $course2['forcetheme']);
428                 // We enabled completion at the beginning of the test.
429                 $this->assertEquals($courseinfo->enablecompletion, $course2['enablecompletion']);
431             } else if ($createdcourse['shortname'] == $course1['shortname']) {
432                 $courseconfig = get_config('moodlecourse');
433                 $this->assertEquals($courseinfo->fullname, $course1['fullname']);
434                 $this->assertEquals($courseinfo->shortname, $course1['shortname']);
435                 $this->assertEquals($courseinfo->category, $course1['categoryid']);
436                 $this->assertEquals($courseinfo->summaryformat, FORMAT_HTML);
437                 $this->assertEquals($courseinfo->format, $courseconfig->format);
438                 $this->assertEquals($courseinfo->showgrades, $courseconfig->showgrades);
439                 $this->assertEquals($courseinfo->newsitems, $courseconfig->newsitems);
440                 $this->assertEquals($courseinfo->maxbytes, $courseconfig->maxbytes);
441                 $this->assertEquals($courseinfo->showreports, $courseconfig->showreports);
442                 $this->assertEquals($courseinfo->groupmode, $courseconfig->groupmode);
443                 $this->assertEquals($courseinfo->groupmodeforce, $courseconfig->groupmodeforce);
444                 $this->assertEquals($courseinfo->defaultgroupingid, 0);
445             } else if ($createdcourse['shortname'] == $course3['shortname']) {
446                 $this->assertEquals($courseinfo->fullname, $course3['fullname']);
447                 $this->assertEquals($courseinfo->shortname, $course3['shortname']);
448                 $this->assertEquals($courseinfo->category, $course3['categoryid']);
449                 $this->assertEquals($courseinfo->format, $course3['format']);
450                 $this->assertEquals($courseinfo->hiddensections, $course3options['hiddensections']);
451                 $this->assertEquals($courseinfo->numsections, $course3options['numsections']);
452                 $this->assertEquals($courseinfo->coursedisplay, $course3options['coursedisplay']);
453             } else {
454                 throw moodle_exception('Unexpected shortname');
455             }
456         }
458         // Call without required capability
459         $this->unassignUserCapability('moodle/course:create', $contextid, $roleid);
460         $this->setExpectedException('required_capability_exception');
461         $createdsubcats = core_course_external::create_courses($courses);
462     }
464     /**
465      * Test delete_courses
466      */
467     public function test_delete_courses() {
468         global $DB, $USER;
470         $this->resetAfterTest(true);
472         // Admin can delete a course.
473         $this->setAdminUser();
474         // Validate_context() will fail as the email is not set by $this->setAdminUser().
475         $USER->email = 'emailtopass@example.com';
477         $course1  = self::getDataGenerator()->create_course();
478         $course2  = self::getDataGenerator()->create_course();
479         $course3  = self::getDataGenerator()->create_course();
481         // Delete courses.
482         $result = core_course_external::delete_courses(array($course1->id, $course2->id));
483         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
484         // Check for 0 warnings.
485         $this->assertEquals(0, count($result['warnings']));
487         // Check $course 1 and 2 are deleted.
488         $notdeletedcount = $DB->count_records_select('course',
489             'id IN ( ' . $course1->id . ',' . $course2->id . ')');
490         $this->assertEquals(0, $notdeletedcount);
492         // Try to delete non-existent course.
493         $result = core_course_external::delete_courses(array($course1->id));
494         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
495         // Check for 1 warnings.
496         $this->assertEquals(1, count($result['warnings']));
498         // Try to delete Frontpage course.
499         $result = core_course_external::delete_courses(array(0));
500         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
501         // Check for 1 warnings.
502         $this->assertEquals(1, count($result['warnings']));
504          // Fail when the user has access to course (enrolled) but does not have permission or is not admin.
505         $student1 = self::getDataGenerator()->create_user();
506         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
507         $this->getDataGenerator()->enrol_user($student1->id,
508                                               $course3->id,
509                                               $studentrole->id);
510         $this->setUser($student1);
511         $result = core_course_external::delete_courses(array($course3->id));
512         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
513         // Check for 1 warnings.
514         $this->assertEquals(1, count($result['warnings']));
516          // Fail when the user is not allow to access the course (enrolled) or is not admin.
517         $this->setGuestUser();
518         $this->setExpectedException('require_login_exception');
520         $result = core_course_external::delete_courses(array($course3->id));
521         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
522     }
524     /**
525      * Test get_courses
526      */
527     public function test_get_courses () {
528         global $DB;
530         $this->resetAfterTest(true);
532         $generatedcourses = array();
533         $coursedata['idnumber'] = 'idnumbercourse1';
534         $coursedata['fullname'] = 'Course 1 for PHPunit test';
535         $coursedata['summary'] = 'Course 1 description';
536         $coursedata['summaryformat'] = FORMAT_MOODLE;
537         $course1  = self::getDataGenerator()->create_course($coursedata);
538         $generatedcourses[$course1->id] = $course1;
539         $course2  = self::getDataGenerator()->create_course();
540         $generatedcourses[$course2->id] = $course2;
541         $course3  = self::getDataGenerator()->create_course(array('format' => 'topics'));
542         $generatedcourses[$course3->id] = $course3;
544         // Set the required capabilities by the external function.
545         $context = context_system::instance();
546         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
547         $this->assignUserCapability('moodle/course:update',
548                 context_course::instance($course1->id)->id, $roleid);
549         $this->assignUserCapability('moodle/course:update',
550                 context_course::instance($course2->id)->id, $roleid);
551         $this->assignUserCapability('moodle/course:update',
552                 context_course::instance($course3->id)->id, $roleid);
554         $courses = core_course_external::get_courses(array('ids' =>
555             array($course1->id, $course2->id)));
557         // We need to execute the return values cleaning process to simulate the web service server.
558         $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
560         // Check we retrieve the good total number of categories.
561         $this->assertEquals(2, count($courses));
563         foreach ($courses as $course) {
564             $dbcourse = $generatedcourses[$course['id']];
565             $this->assertEquals($course['idnumber'], $dbcourse->idnumber);
566             $this->assertEquals($course['fullname'], $dbcourse->fullname);
567             $this->assertEquals($course['displayname'], get_course_display_name_for_list($dbcourse));
568             // Summary was converted to the HTML format.
569             $this->assertEquals($course['summary'], format_text($dbcourse->summary, FORMAT_MOODLE, array('para' => false)));
570             $this->assertEquals($course['summaryformat'], FORMAT_HTML);
571             $this->assertEquals($course['shortname'], $dbcourse->shortname);
572             $this->assertEquals($course['categoryid'], $dbcourse->category);
573             $this->assertEquals($course['format'], $dbcourse->format);
574             $this->assertEquals($course['showgrades'], $dbcourse->showgrades);
575             $this->assertEquals($course['newsitems'], $dbcourse->newsitems);
576             $this->assertEquals($course['startdate'], $dbcourse->startdate);
577             $this->assertEquals($course['numsections'], $dbcourse->numsections);
578             $this->assertEquals($course['maxbytes'], $dbcourse->maxbytes);
579             $this->assertEquals($course['showreports'], $dbcourse->showreports);
580             $this->assertEquals($course['visible'], $dbcourse->visible);
581             $this->assertEquals($course['hiddensections'], $dbcourse->hiddensections);
582             $this->assertEquals($course['groupmode'], $dbcourse->groupmode);
583             $this->assertEquals($course['groupmodeforce'], $dbcourse->groupmodeforce);
584             $this->assertEquals($course['defaultgroupingid'], $dbcourse->defaultgroupingid);
585             $this->assertEquals($course['completionnotify'], $dbcourse->completionnotify);
586             $this->assertEquals($course['lang'], $dbcourse->lang);
587             $this->assertEquals($course['forcetheme'], $dbcourse->theme);
588             $this->assertEquals($course['enablecompletion'], $dbcourse->enablecompletion);
589             if ($dbcourse->format === 'topics') {
590                 $this->assertEquals($course['courseformatoptions'], array(
591                     array('name' => 'numsections', 'value' => $dbcourse->numsections),
592                     array('name' => 'hiddensections', 'value' => $dbcourse->hiddensections),
593                     array('name' => 'coursedisplay', 'value' => $dbcourse->coursedisplay),
594                 ));
595             }
596         }
598         // Get all courses in the DB
599         $courses = core_course_external::get_courses(array());
601         // We need to execute the return values cleaning process to simulate the web service server.
602         $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
604         $this->assertEquals($DB->count_records('course'), count($courses));
605     }
607     /**
608      * Test search_courses
609      */
610     public function test_search_courses () {
612         global $DB;
614         $this->resetAfterTest(true);
615         $this->setAdminUser();
616         $generatedcourses = array();
617         $coursedata1['fullname'] = 'FIRST COURSE';
618         $course1  = self::getDataGenerator()->create_course($coursedata1);
619         $coursedata2['fullname'] = 'SECOND COURSE';
620         $course2  = self::getDataGenerator()->create_course($coursedata2);
621         // Search by name.
622         $results = core_course_external::search_courses('search', 'FIRST');
623         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
624         $this->assertEquals($coursedata1['fullname'], $results['courses'][0]['fullname']);
625         $this->assertCount(1, $results['courses']);
627         // Create the forum.
628         $record = new stdClass();
629         $record->introformat = FORMAT_HTML;
630         $record->course = $course2->id;
631         // Set Aggregate type = Average of ratings.
632         $forum = self::getDataGenerator()->create_module('forum', $record);
634         // Search by module.
635         $results = core_course_external::search_courses('modulelist', 'forum');
636         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
637         $this->assertEquals(1, $results['total']);
639         // Enable coursetag option.
640         set_config('block_tags_showcoursetags', true);
641         // Add tag 'TAG-LABEL ON SECOND COURSE' to Course2.
642         core_tag_tag::set_item_tags('core', 'course', $course2->id, context_course::instance($course2->id),
643                 array('TAG-LABEL ON SECOND COURSE'));
644         $taginstance = $DB->get_record('tag_instance',
645                 array('itemtype' => 'course', 'itemid' => $course2->id), '*', MUST_EXIST);
646         // Search by tagid.
647         $results = core_course_external::search_courses('tagid', $taginstance->tagid);
648         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
649         $this->assertEquals($coursedata2['fullname'], $results['courses'][0]['fullname']);
651         // Search by block (use news_items default block).
652         $blockid = $DB->get_field('block', 'id', array('name' => 'news_items'));
653         $results = core_course_external::search_courses('blocklist', $blockid);
654         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
655         $this->assertEquals(2, $results['total']);
657         // Now as a normal user.
658         $user = self::getDataGenerator()->create_user();
660         // Add a 3rd, hidden, course we shouldn't see, even when enrolled as student.
661         $coursedata3['fullname'] = 'HIDDEN COURSE';
662         $coursedata3['visible'] = 0;
663         $course3  = self::getDataGenerator()->create_course($coursedata3);
664         $this->getDataGenerator()->enrol_user($user->id, $course3->id, 'student');
666         $this->getDataGenerator()->enrol_user($user->id, $course2->id, 'student');
667         $this->setUser($user);
669         $results = core_course_external::search_courses('search', 'FIRST');
670         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
671         $this->assertCount(1, $results['courses']);
672         $this->assertEquals(1, $results['total']);
673         $this->assertEquals($coursedata1['fullname'], $results['courses'][0]['fullname']);
675         // Check that we can see both without the limit to enrolled setting.
676         $results = core_course_external::search_courses('search', 'COURSE', 0, 0, array(), 0);
677         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
678         $this->assertCount(2, $results['courses']);
679         $this->assertEquals(2, $results['total']);
681         // Check that we only see our enrolled course when limiting.
682         $results = core_course_external::search_courses('search', 'COURSE', 0, 0, array(), 1);
683         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
684         $this->assertCount(1, $results['courses']);
685         $this->assertEquals(1, $results['total']);
686         $this->assertEquals($coursedata2['fullname'], $results['courses'][0]['fullname']);
688         // Search by block (use news_items default block). Should fail (only admins allowed).
689         $this->setExpectedException('required_capability_exception');
690         $results = core_course_external::search_courses('blocklist', $blockid);
692     }
694     /**
695      * Create a course with contents
696      * @return array A list with the course object and course modules objects
697      */
698     private function prepare_get_course_contents_test() {
699         global $DB;
700         $course  = self::getDataGenerator()->create_course();
701         $forumdescription = 'This is the forum description';
702         $forum = $this->getDataGenerator()->create_module('forum',
703             array('course' => $course->id, 'intro' => $forumdescription),
704             array('showdescription' => true));
705         $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
706         $data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
707         $datacm = get_coursemodule_from_instance('page', $data->id);
708         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
709         $pagecm = get_coursemodule_from_instance('page', $page->id);
710         $labeldescription = 'This is a very long label to test if more than 50 characters are returned.
711                 So bla bla bla bla <b>bold bold bold</b> bla bla bla bla.';
712         $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id,
713             'intro' => $labeldescription));
714         $labelcm = get_coursemodule_from_instance('label', $label->id);
715         $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id,
716             'name' => 'URL: % & $ ../', 'section' => 2));
717         $urlcm = get_coursemodule_from_instance('url', $url->id);
719         // Set the required capabilities by the external function.
720         $context = context_course::instance($course->id);
721         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
722         $this->assignUserCapability('moodle/course:update', $context->id, $roleid);
724         $conditions = array('course' => $course->id, 'section' => 2);
725         $DB->set_field('course_sections', 'summary', 'Text with iframe <iframe src="https://moodle.org"></iframe>', $conditions);
726         rebuild_course_cache($course->id, true);
728         return array($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm);
729     }
731     /**
732      * Test get_course_contents
733      */
734     public function test_get_course_contents() {
735         $this->resetAfterTest(true);
737         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
739         $sections = core_course_external::get_course_contents($course->id, array());
740         // We need to execute the return values cleaning process to simulate the web service server.
741         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
743         // Check that forum and label descriptions are correctly returned.
744         $firstsection = array_shift($sections);
745         $lastsection = array_pop($sections);
747         $modinfo = get_fast_modinfo($course);
748         $testexecuted = 0;
749         foreach ($firstsection['modules'] as $module) {
750             if ($module['id'] == $forumcm->id and $module['modname'] == 'forum') {
751                 $cm = $modinfo->cms[$forumcm->id];
752                 $formattedtext = format_text($cm->content, FORMAT_HTML,
753                     array('noclean' => true, 'para' => false, 'filter' => false));
754                 $this->assertEquals($formattedtext, $module['description']);
755                 $this->assertEquals($forumcm->instance, $module['instance']);
756                 $testexecuted = $testexecuted + 1;
757             } else if ($module['id'] == $labelcm->id and $module['modname'] == 'label') {
758                 $cm = $modinfo->cms[$labelcm->id];
759                 $formattedtext = format_text($cm->content, FORMAT_HTML,
760                     array('noclean' => true, 'para' => false, 'filter' => false));
761                 $this->assertEquals($formattedtext, $module['description']);
762                 $this->assertEquals($labelcm->instance, $module['instance']);
763                 $testexecuted = $testexecuted + 1;
764             }
765         }
766         $this->assertEquals(2, $testexecuted);
767         $this->assertEquals(0, $firstsection['section']);
769         // Check that the only return section has the 5 created modules.
770         $this->assertCount(4, $firstsection['modules']);
771         $this->assertCount(1, $lastsection['modules']);
772         $this->assertEquals(2, $lastsection['section']);
773         $this->assertContains('<iframe', $lastsection['summary']);
774         $this->assertContains('</iframe>', $lastsection['summary']);
776         try {
777             $sections = core_course_external::get_course_contents($course->id,
778                                                                     array(array("name" => "invalid", "value" => 1)));
779             $this->fail('Exception expected due to invalid option.');
780         } catch (moodle_exception $e) {
781             $this->assertEquals('errorinvalidparam', $e->errorcode);
782         }
783     }
786     /**
787      * Test get_course_contents excluding modules
788      */
789     public function test_get_course_contents_excluding_modules() {
790         $this->resetAfterTest(true);
792         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
794         // Test exclude modules.
795         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludemodules", "value" => 1)));
797         // We need to execute the return values cleaning process to simulate the web service server.
798         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
800         $firstsection = array_shift($sections);
801         $lastsection = array_pop($sections);
803         $this->assertEmpty($firstsection['modules']);
804         $this->assertEmpty($lastsection['modules']);
805     }
807     /**
808      * Test get_course_contents excluding contents
809      */
810     public function test_get_course_contents_excluding_contents() {
811         $this->resetAfterTest(true);
813         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
815         // Test exclude modules.
816         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludecontents", "value" => 1)));
818         // We need to execute the return values cleaning process to simulate the web service server.
819         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
821         foreach ($sections as $section) {
822             foreach ($section['modules'] as $module) {
823                 // Only resources return contents.
824                 if (isset($module['contents'])) {
825                     $this->assertEmpty($module['contents']);
826                 }
827             }
828         }
829     }
831     /**
832      * Test get_course_contents filtering by section number
833      */
834     public function test_get_course_contents_section_number() {
835         $this->resetAfterTest(true);
837         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
839         // Test exclude modules.
840         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "sectionnumber", "value" => 0)));
842         // We need to execute the return values cleaning process to simulate the web service server.
843         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
845         $this->assertCount(1, $sections);
846         $this->assertCount(4, $sections[0]['modules']);
847     }
849     /**
850      * Test get_course_contents filtering by cmid
851      */
852     public function test_get_course_contents_cmid() {
853         $this->resetAfterTest(true);
855         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
857         // Test exclude modules.
858         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "cmid", "value" => $forumcm->id)));
860         // We need to execute the return values cleaning process to simulate the web service server.
861         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
863         $this->assertCount(2, $sections);
864         $this->assertCount(1, $sections[0]['modules']);
865         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
866     }
869     /**
870      * Test get_course_contents filtering by cmid and section
871      */
872     public function test_get_course_contents_section_cmid() {
873         $this->resetAfterTest(true);
875         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
877         // Test exclude modules.
878         $sections = core_course_external::get_course_contents($course->id, array(
879                                                                         array("name" => "cmid", "value" => $forumcm->id),
880                                                                         array("name" => "sectionnumber", "value" => 0)
881                                                                         ));
883         // We need to execute the return values cleaning process to simulate the web service server.
884         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
886         $this->assertCount(1, $sections);
887         $this->assertCount(1, $sections[0]['modules']);
888         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
889     }
891     /**
892      * Test get_course_contents filtering by modname
893      */
894     public function test_get_course_contents_modname() {
895         $this->resetAfterTest(true);
897         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
899         // Test exclude modules.
900         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "modname", "value" => "forum")));
902         // We need to execute the return values cleaning process to simulate the web service server.
903         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
905         $this->assertCount(2, $sections);
906         $this->assertCount(1, $sections[0]['modules']);
907         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
908     }
910     /**
911      * Test get_course_contents filtering by modname
912      */
913     public function test_get_course_contents_modid() {
914         $this->resetAfterTest(true);
916         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
918         // Test exclude modules.
919         $sections = core_course_external::get_course_contents($course->id, array(
920                                                                             array("name" => "modname", "value" => "page"),
921                                                                             array("name" => "modid", "value" => $pagecm->instance),
922                                                                             ));
924         // We need to execute the return values cleaning process to simulate the web service server.
925         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
927         $this->assertCount(2, $sections);
928         $this->assertCount(1, $sections[0]['modules']);
929         $this->assertEquals("page", $sections[0]['modules'][0]["modname"]);
930         $this->assertEquals($pagecm->instance, $sections[0]['modules'][0]["instance"]);
931     }
933     /**
934      * Test duplicate_course
935      */
936     public function test_duplicate_course() {
937         $this->resetAfterTest(true);
939         // Create one course with three modules.
940         $course  = self::getDataGenerator()->create_course();
941         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
942         $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
943         $forumcontext = context_module::instance($forum->cmid);
944         $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
945         $datacontext = context_module::instance($data->cmid);
946         $datacm = get_coursemodule_from_instance('page', $data->id);
947         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
948         $pagecontext = context_module::instance($page->cmid);
949         $pagecm = get_coursemodule_from_instance('page', $page->id);
951         // Set the required capabilities by the external function.
952         $coursecontext = context_course::instance($course->id);
953         $categorycontext = context_coursecat::instance($course->category);
954         $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id);
955         $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid);
956         $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid);
957         $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid);
958         $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid);
959         // Optional capabilities to copy user data.
960         $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid);
961         $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid);
963         $newcourse['fullname'] = 'Course duplicate';
964         $newcourse['shortname'] = 'courseduplicate';
965         $newcourse['categoryid'] = $course->category;
966         $newcourse['visible'] = true;
967         $newcourse['options'][] = array('name' => 'users', 'value' => true);
969         $duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'],
970                 $newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']);
972         // We need to execute the return values cleaning process to simulate the web service server.
973         $duplicate = external_api::clean_returnvalue(core_course_external::duplicate_course_returns(), $duplicate);
975         // Check that the course has been duplicated.
976         $this->assertEquals($newcourse['shortname'], $duplicate['shortname']);
977     }
979     /**
980      * Test update_courses
981      */
982     public function test_update_courses() {
983         global $DB, $CFG, $USER, $COURSE;
985         // Get current $COURSE to be able to restore it later (defaults to $SITE). We need this
986         // trick because we are both updating and getting (for testing) course information
987         // in the same request and core_course_external::update_courses()
988         // is overwriting $COURSE all over the time with OLD values, so later
989         // use of get_course() fetches those OLD values instead of the updated ones.
990         // See MDL-39723 for more info.
991         $origcourse = clone($COURSE);
993         $this->resetAfterTest(true);
995         // Set the required capabilities by the external function.
996         $contextid = context_system::instance()->id;
997         $roleid = $this->assignUserCapability('moodle/course:update', $contextid);
998         $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
999         $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1000         $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1001         $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1002         $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1003         $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
1004         $this->assignUserCapability('moodle/course:viewhiddencourses', $contextid, $roleid);
1006         // Create category and course.
1007         $category1  = self::getDataGenerator()->create_category();
1008         $category2  = self::getDataGenerator()->create_category();
1009         $originalcourse1 = self::getDataGenerator()->create_course();
1010         self::getDataGenerator()->enrol_user($USER->id, $originalcourse1->id, $roleid);
1011         $originalcourse2 = self::getDataGenerator()->create_course();
1012         self::getDataGenerator()->enrol_user($USER->id, $originalcourse2->id, $roleid);
1014         // Course values to be updated.
1015         $course1['id'] = $originalcourse1->id;
1016         $course1['fullname'] = 'Updated test course 1';
1017         $course1['shortname'] = 'Udestedtestcourse1';
1018         $course1['categoryid'] = $category1->id;
1019         $course2['id'] = $originalcourse2->id;
1020         $course2['fullname'] = 'Updated test course 2';
1021         $course2['shortname'] = 'Updestedtestcourse2';
1022         $course2['categoryid'] = $category2->id;
1023         $course2['idnumber'] = 'Updatedidnumber2';
1024         $course2['summary'] = 'Updaated description for course 2';
1025         $course2['summaryformat'] = FORMAT_HTML;
1026         $course2['format'] = 'topics';
1027         $course2['showgrades'] = 1;
1028         $course2['newsitems'] = 3;
1029         $course2['startdate'] = 1420092000; // 01/01/2015.
1030         $course2['numsections'] = 4;
1031         $course2['maxbytes'] = 100000;
1032         $course2['showreports'] = 1;
1033         $course2['visible'] = 0;
1034         $course2['hiddensections'] = 0;
1035         $course2['groupmode'] = 0;
1036         $course2['groupmodeforce'] = 0;
1037         $course2['defaultgroupingid'] = 0;
1038         $course2['enablecompletion'] = 1;
1039         $course2['lang'] = 'en';
1040         $course2['forcetheme'] = 'base';
1041         $courses = array($course1, $course2);
1043         $updatedcoursewarnings = core_course_external::update_courses($courses);
1044         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1045                                                                     $updatedcoursewarnings);
1046         $COURSE = $origcourse; // Restore $COURSE. Instead of using the OLD one set by the previous line.
1048         // Check that right number of courses were created.
1049         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1051         // Check that the courses were correctly created.
1052         foreach ($courses as $course) {
1053             $courseinfo = course_get_format($course['id'])->get_course();
1054             if ($course['id'] == $course2['id']) {
1055                 $this->assertEquals($course2['fullname'], $courseinfo->fullname);
1056                 $this->assertEquals($course2['shortname'], $courseinfo->shortname);
1057                 $this->assertEquals($course2['categoryid'], $courseinfo->category);
1058                 $this->assertEquals($course2['idnumber'], $courseinfo->idnumber);
1059                 $this->assertEquals($course2['summary'], $courseinfo->summary);
1060                 $this->assertEquals($course2['summaryformat'], $courseinfo->summaryformat);
1061                 $this->assertEquals($course2['format'], $courseinfo->format);
1062                 $this->assertEquals($course2['showgrades'], $courseinfo->showgrades);
1063                 $this->assertEquals($course2['newsitems'], $courseinfo->newsitems);
1064                 $this->assertEquals($course2['startdate'], $courseinfo->startdate);
1065                 $this->assertEquals($course2['numsections'], $courseinfo->numsections);
1066                 $this->assertEquals($course2['maxbytes'], $courseinfo->maxbytes);
1067                 $this->assertEquals($course2['showreports'], $courseinfo->showreports);
1068                 $this->assertEquals($course2['visible'], $courseinfo->visible);
1069                 $this->assertEquals($course2['hiddensections'], $courseinfo->hiddensections);
1070                 $this->assertEquals($course2['groupmode'], $courseinfo->groupmode);
1071                 $this->assertEquals($course2['groupmodeforce'], $courseinfo->groupmodeforce);
1072                 $this->assertEquals($course2['defaultgroupingid'], $courseinfo->defaultgroupingid);
1073                 $this->assertEquals($course2['lang'], $courseinfo->lang);
1075                 if (!empty($CFG->allowcoursethemes)) {
1076                     $this->assertEquals($course2['forcetheme'], $courseinfo->theme);
1077                 }
1079                 $this->assertEquals($course2['enablecompletion'], $courseinfo->enablecompletion);
1080             } else if ($course['id'] == $course1['id']) {
1081                 $this->assertEquals($course1['fullname'], $courseinfo->fullname);
1082                 $this->assertEquals($course1['shortname'], $courseinfo->shortname);
1083                 $this->assertEquals($course1['categoryid'], $courseinfo->category);
1084                 $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
1085                 $this->assertEquals('topics', $courseinfo->format);
1086                 $this->assertEquals(5, $courseinfo->numsections);
1087                 $this->assertEquals(0, $courseinfo->newsitems);
1088                 $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
1089             } else {
1090                 throw moodle_exception('Unexpected shortname');
1091             }
1092         }
1094         $courses = array($course1);
1095         // Try update course without update capability.
1096         $user = self::getDataGenerator()->create_user();
1097         $this->setUser($user);
1098         $this->unassignUserCapability('moodle/course:update', $contextid, $roleid);
1099         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1100         $updatedcoursewarnings = core_course_external::update_courses($courses);
1101         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1102                                                                     $updatedcoursewarnings);
1103         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1105         // Try update course category without capability.
1106         $this->assignUserCapability('moodle/course:update', $contextid, $roleid);
1107         $this->unassignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1108         $user = self::getDataGenerator()->create_user();
1109         $this->setUser($user);
1110         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1111         $course1['categoryid'] = $category2->id;
1112         $courses = array($course1);
1113         $updatedcoursewarnings = core_course_external::update_courses($courses);
1114         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1115                                                                     $updatedcoursewarnings);
1116         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1118         // Try update course fullname without capability.
1119         $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1120         $this->unassignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1121         $user = self::getDataGenerator()->create_user();
1122         $this->setUser($user);
1123         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1124         $updatedcoursewarnings = core_course_external::update_courses($courses);
1125         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1126                                                                     $updatedcoursewarnings);
1127         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1128         $course1['fullname'] = 'Testing fullname without permission';
1129         $courses = array($course1);
1130         $updatedcoursewarnings = core_course_external::update_courses($courses);
1131         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1132                                                                     $updatedcoursewarnings);
1133         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1135         // Try update course shortname without capability.
1136         $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1137         $this->unassignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1138         $user = self::getDataGenerator()->create_user();
1139         $this->setUser($user);
1140         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1141         $updatedcoursewarnings = core_course_external::update_courses($courses);
1142         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1143                                                                     $updatedcoursewarnings);
1144         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1145         $course1['shortname'] = 'Testing shortname without permission';
1146         $courses = array($course1);
1147         $updatedcoursewarnings = core_course_external::update_courses($courses);
1148         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1149                                                                     $updatedcoursewarnings);
1150         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1152         // Try update course idnumber without capability.
1153         $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1154         $this->unassignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1155         $user = self::getDataGenerator()->create_user();
1156         $this->setUser($user);
1157         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1158         $updatedcoursewarnings = core_course_external::update_courses($courses);
1159         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1160                                                                     $updatedcoursewarnings);
1161         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1162         $course1['idnumber'] = 'NEWIDNUMBER';
1163         $courses = array($course1);
1164         $updatedcoursewarnings = core_course_external::update_courses($courses);
1165         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1166                                                                     $updatedcoursewarnings);
1167         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1169         // Try update course summary without capability.
1170         $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1171         $this->unassignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1172         $user = self::getDataGenerator()->create_user();
1173         $this->setUser($user);
1174         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1175         $updatedcoursewarnings = core_course_external::update_courses($courses);
1176         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1177                                                                     $updatedcoursewarnings);
1178         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1179         $course1['summary'] = 'New summary';
1180         $courses = array($course1);
1181         $updatedcoursewarnings = core_course_external::update_courses($courses);
1182         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1183                                                                     $updatedcoursewarnings);
1184         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1186         // Try update course with invalid summary format.
1187         $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1188         $user = self::getDataGenerator()->create_user();
1189         $this->setUser($user);
1190         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1191         $updatedcoursewarnings = core_course_external::update_courses($courses);
1192         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1193                                                                     $updatedcoursewarnings);
1194         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1195         $course1['summaryformat'] = 10;
1196         $courses = array($course1);
1197         $updatedcoursewarnings = core_course_external::update_courses($courses);
1198         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1199                                                                     $updatedcoursewarnings);
1200         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1202         // Try update course visibility without capability.
1203         $this->unassignUserCapability('moodle/course:visibility', $contextid, $roleid);
1204         $user = self::getDataGenerator()->create_user();
1205         $this->setUser($user);
1206         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1207         $course1['summaryformat'] = FORMAT_MOODLE;
1208         $courses = array($course1);
1209         $updatedcoursewarnings = core_course_external::update_courses($courses);
1210         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1211                                                                     $updatedcoursewarnings);
1212         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1213         $course1['visible'] = 0;
1214         $courses = array($course1);
1215         $updatedcoursewarnings = core_course_external::update_courses($courses);
1216         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1217                                                                     $updatedcoursewarnings);
1218         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1219     }
1221     /**
1222      * Test delete course_module.
1223      */
1224     public function test_delete_modules() {
1225         global $DB;
1227         // Ensure we reset the data after this test.
1228         $this->resetAfterTest(true);
1230         // Create a user.
1231         $user = self::getDataGenerator()->create_user();
1233         // Set the tests to run as the user.
1234         self::setUser($user);
1236         // Create a course to add the modules.
1237         $course = self::getDataGenerator()->create_course();
1239         // Create two test modules.
1240         $record = new stdClass();
1241         $record->course = $course->id;
1242         $module1 = self::getDataGenerator()->create_module('forum', $record);
1243         $module2 = self::getDataGenerator()->create_module('assign', $record);
1245         // Check the forum was correctly created.
1246         $this->assertEquals(1, $DB->count_records('forum', array('id' => $module1->id)));
1248         // Check the assignment was correctly created.
1249         $this->assertEquals(1, $DB->count_records('assign', array('id' => $module2->id)));
1251         // Check data exists in the course modules table.
1252         $this->assertEquals(2, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
1253                 array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
1255         // Enrol the user in the course.
1256         $enrol = enrol_get_plugin('manual');
1257         $enrolinstances = enrol_get_instances($course->id, true);
1258         foreach ($enrolinstances as $courseenrolinstance) {
1259             if ($courseenrolinstance->enrol == "manual") {
1260                 $instance = $courseenrolinstance;
1261                 break;
1262             }
1263         }
1264         $enrol->enrol_user($instance, $user->id);
1266         // Assign capabilities to delete module 1.
1267         $modcontext = context_module::instance($module1->cmid);
1268         $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id);
1270         // Assign capabilities to delete module 2.
1271         $modcontext = context_module::instance($module2->cmid);
1272         $newrole = create_role('Role 2', 'role2', 'Role 2 description');
1273         $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id, $newrole);
1275         // Deleting these module instances.
1276         core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1278         // Check the forum was deleted.
1279         $this->assertEquals(0, $DB->count_records('forum', array('id' => $module1->id)));
1281         // Check the assignment was deleted.
1282         $this->assertEquals(0, $DB->count_records('assign', array('id' => $module2->id)));
1284         // Check we retrieve no data in the course modules table.
1285         $this->assertEquals(0, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
1286                 array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
1288         // Call with non-existent course module id and ensure exception thrown.
1289         try {
1290             core_course_external::delete_modules(array('1337'));
1291             $this->fail('Exception expected due to missing course module.');
1292         } catch (dml_missing_record_exception $e) {
1293             $this->assertEquals('invalidcoursemodule', $e->errorcode);
1294         }
1296         // Create two modules.
1297         $module1 = self::getDataGenerator()->create_module('forum', $record);
1298         $module2 = self::getDataGenerator()->create_module('assign', $record);
1300         // Since these modules were recreated the user will not have capabilities
1301         // to delete them, ensure exception is thrown if they try.
1302         try {
1303             core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1304             $this->fail('Exception expected due to missing capability.');
1305         } catch (moodle_exception $e) {
1306             $this->assertEquals('nopermissions', $e->errorcode);
1307         }
1309         // Unenrol user from the course.
1310         $enrol->unenrol_user($instance, $user->id);
1312         // Try and delete modules from the course the user was unenrolled in, make sure exception thrown.
1313         try {
1314             core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1315             $this->fail('Exception expected due to being unenrolled from the course.');
1316         } catch (moodle_exception $e) {
1317             $this->assertEquals('requireloginerror', $e->errorcode);
1318         }
1319     }
1321     /**
1322      * Test import_course into an empty course
1323      */
1324     public function test_import_course_empty() {
1325         global $USER;
1327         $this->resetAfterTest(true);
1329         $course1  = self::getDataGenerator()->create_course();
1330         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course1->id, 'name' => 'Forum test'));
1331         $page = $this->getDataGenerator()->create_module('page', array('course' => $course1->id, 'name' => 'Page test'));
1333         $course2  = self::getDataGenerator()->create_course();
1335         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1336         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1338         // Verify the state of the courses before we do the import.
1339         $this->assertCount(2, $course1cms);
1340         $this->assertEmpty($course2cms);
1342         // Setup the user to run the operation (ugly hack because validate_context() will
1343         // fail as the email is not set by $this->setAdminUser()).
1344         $this->setAdminUser();
1345         $USER->email = 'emailtopass@example.com';
1347         // Import from course1 to course2.
1348         core_course_external::import_course($course1->id, $course2->id, 0);
1350         // Verify that now we have two modules in both courses.
1351         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1352         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1353         $this->assertCount(2, $course1cms);
1354         $this->assertCount(2, $course2cms);
1356         // Verify that the names transfered across correctly.
1357         foreach ($course2cms as $cm) {
1358             if ($cm->modname === 'page') {
1359                 $this->assertEquals($cm->name, $page->name);
1360             } else if ($cm->modname === 'forum') {
1361                 $this->assertEquals($cm->name, $forum->name);
1362             } else {
1363                 $this->fail('Unknown CM found.');
1364             }
1365         }
1366     }
1368     /**
1369      * Test import_course into an filled course
1370      */
1371     public function test_import_course_filled() {
1372         global $USER;
1374         $this->resetAfterTest(true);
1376         // Add forum and page to course1.
1377         $course1  = self::getDataGenerator()->create_course();
1378         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1379         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
1381         // Add quiz to course 2.
1382         $course2  = self::getDataGenerator()->create_course();
1383         $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
1385         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1386         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1388         // Verify the state of the courses before we do the import.
1389         $this->assertCount(2, $course1cms);
1390         $this->assertCount(1, $course2cms);
1392         // Setup the user to run the operation (ugly hack because validate_context() will
1393         // fail as the email is not set by $this->setAdminUser()).
1394         $this->setAdminUser();
1395         $USER->email = 'emailtopass@example.com';
1397         // Import from course1 to course2 without deleting content.
1398         core_course_external::import_course($course1->id, $course2->id, 0);
1400         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1402         // Verify that now we have three modules in course2.
1403         $this->assertCount(3, $course2cms);
1405         // Verify that the names transfered across correctly.
1406         foreach ($course2cms as $cm) {
1407             if ($cm->modname === 'page') {
1408                 $this->assertEquals($cm->name, $page->name);
1409             } else if ($cm->modname === 'forum') {
1410                 $this->assertEquals($cm->name, $forum->name);
1411             } else if ($cm->modname === 'quiz') {
1412                 $this->assertEquals($cm->name, $quiz->name);
1413             } else {
1414                 $this->fail('Unknown CM found.');
1415             }
1416         }
1417     }
1419     /**
1420      * Test import_course with only blocks set to backup
1421      */
1422     public function test_import_course_blocksonly() {
1423         global $USER, $DB;
1425         $this->resetAfterTest(true);
1427         // Add forum and page to course1.
1428         $course1  = self::getDataGenerator()->create_course();
1429         $course1ctx = context_course::instance($course1->id);
1430         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1431         $block = $this->getDataGenerator()->create_block('online_users', array('parentcontextid' => $course1ctx->id));
1433         $course2  = self::getDataGenerator()->create_course();
1434         $course2ctx = context_course::instance($course2->id);
1435         $initialblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
1436         $initialcmcount = count(get_fast_modinfo($course2->id)->get_cms());
1438         // Setup the user to run the operation (ugly hack because validate_context() will
1439         // fail as the email is not set by $this->setAdminUser()).
1440         $this->setAdminUser();
1441         $USER->email = 'emailtopass@example.com';
1443         // Import from course1 to course2 without deleting content, but excluding
1444         // activities.
1445         $options = array(
1446             array('name' => 'activities', 'value' => 0),
1447             array('name' => 'blocks', 'value' => 1),
1448             array('name' => 'filters', 'value' => 0),
1449         );
1451         core_course_external::import_course($course1->id, $course2->id, 0, $options);
1453         $newcmcount = count(get_fast_modinfo($course2->id)->get_cms());
1454         $newblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
1455         // Check that course modules haven't changed, but that blocks have.
1456         $this->assertEquals($initialcmcount, $newcmcount);
1457         $this->assertEquals(($initialblockcount + 1), $newblockcount);
1458     }
1460     /**
1461      * Test import_course into an filled course, deleting content.
1462      */
1463     public function test_import_course_deletecontent() {
1464         global $USER;
1465         $this->resetAfterTest(true);
1467         // Add forum and page to course1.
1468         $course1  = self::getDataGenerator()->create_course();
1469         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1470         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
1472         // Add quiz to course 2.
1473         $course2  = self::getDataGenerator()->create_course();
1474         $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
1476         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1477         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1479         // Verify the state of the courses before we do the import.
1480         $this->assertCount(2, $course1cms);
1481         $this->assertCount(1, $course2cms);
1483         // Setup the user to run the operation (ugly hack because validate_context() will
1484         // fail as the email is not set by $this->setAdminUser()).
1485         $this->setAdminUser();
1486         $USER->email = 'emailtopass@example.com';
1488         // Import from course1 to course2,  deleting content.
1489         core_course_external::import_course($course1->id, $course2->id, 1);
1491         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1493         // Verify that now we have two modules in course2.
1494         $this->assertCount(2, $course2cms);
1496         // Verify that the course only contains the imported modules.
1497         foreach ($course2cms as $cm) {
1498             if ($cm->modname === 'page') {
1499                 $this->assertEquals($cm->name, $page->name);
1500             } else if ($cm->modname === 'forum') {
1501                 $this->assertEquals($cm->name, $forum->name);
1502             } else {
1503                 $this->fail('Unknown CM found: '.$cm->name);
1504             }
1505         }
1506     }
1508     /**
1509      * Ensure import_course handles incorrect deletecontent option correctly.
1510      */
1511     public function test_import_course_invalid_deletecontent_option() {
1512         $this->resetAfterTest(true);
1514         $course1  = self::getDataGenerator()->create_course();
1515         $course2  = self::getDataGenerator()->create_course();
1517         $this->setExpectedException('moodle_exception', get_string('invalidextparam', 'webservice', -1));
1518         // Import from course1 to course2, with invalid option
1519         core_course_external::import_course($course1->id, $course2->id, -1);;
1520     }
1522     /**
1523      * Test view_course function
1524      */
1525     public function test_view_course() {
1527         $this->resetAfterTest();
1529         // Course without sections.
1530         $course = $this->getDataGenerator()->create_course(array('numsections' => 5), array('createsections' => true));
1531         $this->setAdminUser();
1533         // Redirect events to the sink, so we can recover them later.
1534         $sink = $this->redirectEvents();
1536         $result = core_course_external::view_course($course->id, 1);
1537         $result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result);
1538         $events = $sink->get_events();
1539         $event = reset($events);
1541         // Check the event details are correct.
1542         $this->assertInstanceOf('\core\event\course_viewed', $event);
1543         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1544         $this->assertEquals(1, $event->other['coursesectionnumber']);
1546         $result = core_course_external::view_course($course->id);
1547         $result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result);
1548         $events = $sink->get_events();
1549         $event = array_pop($events);
1550         $sink->close();
1552         // Check the event details are correct.
1553         $this->assertInstanceOf('\core\event\course_viewed', $event);
1554         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1555         $this->assertEmpty($event->other);
1557     }
1559     /**
1560      * Test get_course_module
1561      */
1562     public function test_get_course_module() {
1563         global $DB;
1565         $this->resetAfterTest(true);
1567         $this->setAdminUser();
1568         $course = self::getDataGenerator()->create_course();
1569         $record = array(
1570             'course' => $course->id,
1571             'name' => 'First Chat'
1572         );
1573         $options = array(
1574             'idnumber' => 'ABC',
1575             'visible' => 0
1576         );
1577         // Hidden activity.
1578         $chat = self::getDataGenerator()->create_module('chat', $record, $options);
1580         // Test admin user can see the complete hidden activity.
1581         $result = core_course_external::get_course_module($chat->cmid);
1582         $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result);
1584         $this->assertCount(0, $result['warnings']);
1585         // Test we retrieve all the fields.
1586         $this->assertCount(22, $result['cm']);
1587         $this->assertEquals($record['name'], $result['cm']['name']);
1588         $this->assertEquals($options['idnumber'], $result['cm']['idnumber']);
1590         $student = $this->getDataGenerator()->create_user();
1591         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1593         self::getDataGenerator()->enrol_user($student->id,  $course->id, $studentrole->id);
1594         $this->setUser($student);
1596         // The user shouldn't be able to see the activity.
1597         try {
1598             core_course_external::get_course_module($chat->cmid);
1599             $this->fail('Exception expected due to invalid permissions.');
1600         } catch (moodle_exception $e) {
1601             $this->assertEquals('requireloginerror', $e->errorcode);
1602         }
1604         // Make module visible.
1605         set_coursemodule_visible($chat->cmid, 1);
1607         // Test student user.
1608         $result = core_course_external::get_course_module($chat->cmid);
1609         $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result);
1611         $this->assertCount(0, $result['warnings']);
1612         // Test we retrieve only the few files we can see.
1613         $this->assertCount(11, $result['cm']);
1614         $this->assertEquals($chat->cmid, $result['cm']['id']);
1615         $this->assertEquals($course->id, $result['cm']['course']);
1616         $this->assertEquals('chat', $result['cm']['modname']);
1617         $this->assertEquals($chat->id, $result['cm']['instance']);
1619     }
1621     /**
1622      * Test get_course_module_by_instance
1623      */
1624     public function test_get_course_module_by_instance() {
1625         global $DB;
1627         $this->resetAfterTest(true);
1629         $this->setAdminUser();
1630         $course = self::getDataGenerator()->create_course();
1631         $record = array(
1632             'course' => $course->id,
1633             'name' => 'First Chat'
1634         );
1635         $options = array(
1636             'idnumber' => 'ABC',
1637             'visible' => 0
1638         );
1639         // Hidden activity.
1640         $chat = self::getDataGenerator()->create_module('chat', $record, $options);
1642         // Test admin user can see the complete hidden activity.
1643         $result = core_course_external::get_course_module_by_instance('chat', $chat->id);
1644         $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result);
1646         $this->assertCount(0, $result['warnings']);
1647         // Test we retrieve all the fields.
1648         $this->assertCount(22, $result['cm']);
1649         $this->assertEquals($record['name'], $result['cm']['name']);
1650         $this->assertEquals($options['idnumber'], $result['cm']['idnumber']);
1652         $student = $this->getDataGenerator()->create_user();
1653         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1655         self::getDataGenerator()->enrol_user($student->id,  $course->id, $studentrole->id);
1656         $this->setUser($student);
1658         // The user shouldn't be able to see the activity.
1659         try {
1660             core_course_external::get_course_module_by_instance('chat', $chat->id);
1661             $this->fail('Exception expected due to invalid permissions.');
1662         } catch (moodle_exception $e) {
1663             $this->assertEquals('requireloginerror', $e->errorcode);
1664         }
1666         // Make module visible.
1667         set_coursemodule_visible($chat->cmid, 1);
1669         // Test student user.
1670         $result = core_course_external::get_course_module_by_instance('chat', $chat->id);
1671         $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result);
1673         $this->assertCount(0, $result['warnings']);
1674         // Test we retrieve only the few files we can see.
1675         $this->assertCount(11, $result['cm']);
1676         $this->assertEquals($chat->cmid, $result['cm']['id']);
1677         $this->assertEquals($course->id, $result['cm']['course']);
1678         $this->assertEquals('chat', $result['cm']['modname']);
1679         $this->assertEquals($chat->id, $result['cm']['instance']);
1681         // Try with an invalid module name.
1682         try {
1683             core_course_external::get_course_module_by_instance('abc', $chat->id);
1684             $this->fail('Exception expected due to invalid module name.');
1685         } catch (dml_read_exception $e) {
1686             $this->assertEquals('dmlreadexception', $e->errorcode);
1687         }
1689     }