MDL-50851 course: use new tag API
[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      * Tidy up open files that may be left open.
52      */
53     protected function tearDown() {
54         gc_collect_cycles();
55     }
57     /**
58      * Test create_categories
59      */
60     public function test_create_categories() {
62         global $DB;
64         $this->resetAfterTest(true);
66         // Set the required capabilities by the external function
67         $contextid = context_system::instance()->id;
68         $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
70         // Create base categories.
71         $category1 = new stdClass();
72         $category1->name = 'Root Test Category 1';
73         $category2 = new stdClass();
74         $category2->name = 'Root Test Category 2';
75         $category2->idnumber = 'rootcattest2';
76         $category2->desc = 'Description for root test category 1';
77         $category2->theme = 'base';
78         $categories = array(
79             array('name' => $category1->name, 'parent' => 0),
80             array('name' => $category2->name, 'parent' => 0, 'idnumber' => $category2->idnumber,
81                 'description' => $category2->desc, 'theme' => $category2->theme)
82         );
84         $createdcats = core_course_external::create_categories($categories);
86         // We need to execute the return values cleaning process to simulate the web service server.
87         $createdcats = external_api::clean_returnvalue(core_course_external::create_categories_returns(), $createdcats);
89         // Initially confirm that base data was inserted correctly.
90         $this->assertEquals($category1->name, $createdcats[0]['name']);
91         $this->assertEquals($category2->name, $createdcats[1]['name']);
93         // Save the ids.
94         $category1->id = $createdcats[0]['id'];
95         $category2->id = $createdcats[1]['id'];
97         // Create on sub category.
98         $category3 = new stdClass();
99         $category3->name = 'Sub Root Test Category 3';
100         $subcategories = array(
101             array('name' => $category3->name, 'parent' => $category1->id)
102         );
104         $createdsubcats = core_course_external::create_categories($subcategories);
106         // We need to execute the return values cleaning process to simulate the web service server.
107         $createdsubcats = external_api::clean_returnvalue(core_course_external::create_categories_returns(), $createdsubcats);
109         // Confirm that sub categories were inserted correctly.
110         $this->assertEquals($category3->name, $createdsubcats[0]['name']);
112         // Save the ids.
113         $category3->id = $createdsubcats[0]['id'];
115         // Calling the ws function should provide a new sortorder to give category1,
116         // category2, category3. New course categories are ordered by id not name.
117         $category1 = $DB->get_record('course_categories', array('id' => $category1->id));
118         $category2 = $DB->get_record('course_categories', array('id' => $category2->id));
119         $category3 = $DB->get_record('course_categories', array('id' => $category3->id));
121         // sortorder sequence (and sortorder) must be:
122         // category 1
123         //   category 3
124         // category 2
125         $this->assertGreaterThan($category1->sortorder, $category3->sortorder);
126         $this->assertGreaterThan($category3->sortorder, $category2->sortorder);
128         // Call without required capability
129         $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
130         $this->setExpectedException('required_capability_exception');
131         $createdsubcats = core_course_external::create_categories($subcategories);
133     }
135     /**
136      * Test delete categories
137      */
138     public function test_delete_categories() {
139         global $DB;
141         $this->resetAfterTest(true);
143         // Set the required capabilities by the external function
144         $contextid = context_system::instance()->id;
145         $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
147         $category1  = self::getDataGenerator()->create_category();
148         $category2  = self::getDataGenerator()->create_category(
149                 array('parent' => $category1->id));
150         $category3  = self::getDataGenerator()->create_category();
151         $category4  = self::getDataGenerator()->create_category(
152                 array('parent' => $category3->id));
153         $category5  = self::getDataGenerator()->create_category(
154                 array('parent' => $category4->id));
156         //delete category 1 and 2 + delete category 4, category 5 moved under category 3
157         core_course_external::delete_categories(array(
158             array('id' => $category1->id, 'recursive' => 1),
159             array('id' => $category4->id)
160         ));
162         //check $category 1 and 2 are deleted
163         $notdeletedcount = $DB->count_records_select('course_categories',
164             'id IN ( ' . $category1->id . ',' . $category2->id . ',' . $category4->id . ')');
165         $this->assertEquals(0, $notdeletedcount);
167         //check that $category5 as $category3 for parent
168         $dbcategory5 = $DB->get_record('course_categories', array('id' => $category5->id));
169         $this->assertEquals($dbcategory5->path, $category3->path . '/' . $category5->id);
171          // Call without required capability
172         $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
173         $this->setExpectedException('required_capability_exception');
174         $createdsubcats = core_course_external::delete_categories(
175                 array(array('id' => $category3->id)));
176     }
178     /**
179      * Test get categories
180      */
181     public function test_get_categories() {
182         global $DB;
184         $this->resetAfterTest(true);
186         $generatedcats = array();
187         $category1data['idnumber'] = 'idnumbercat1';
188         $category1data['name'] = 'Category 1 for PHPunit test';
189         $category1data['description'] = 'Category 1 description';
190         $category1data['descriptionformat'] = FORMAT_MOODLE;
191         $category1  = self::getDataGenerator()->create_category($category1data);
192         $generatedcats[$category1->id] = $category1;
193         $category2  = self::getDataGenerator()->create_category(
194                 array('parent' => $category1->id));
195         $generatedcats[$category2->id] = $category2;
196         $category6  = self::getDataGenerator()->create_category(
197                 array('parent' => $category1->id, 'visible' => 0));
198         $generatedcats[$category6->id] = $category6;
199         $category3  = self::getDataGenerator()->create_category();
200         $generatedcats[$category3->id] = $category3;
201         $category4  = self::getDataGenerator()->create_category(
202                 array('parent' => $category3->id));
203         $generatedcats[$category4->id] = $category4;
204         $category5  = self::getDataGenerator()->create_category(
205                 array('parent' => $category4->id));
206         $generatedcats[$category5->id] = $category5;
208         // Set the required capabilities by the external function.
209         $context = context_system::instance();
210         $roleid = $this->assignUserCapability('moodle/category:manage', $context->id);
212         // Retrieve category1 + sub-categories except not visible ones
213         $categories = core_course_external::get_categories(array(
214             array('key' => 'id', 'value' => $category1->id),
215             array('key' => 'visible', 'value' => 1)), 1);
217         // We need to execute the return values cleaning process to simulate the web service server.
218         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
220         // Check we retrieve the good total number of categories.
221         $this->assertEquals(2, count($categories));
223         // Check the return values
224         foreach ($categories as $category) {
225             $generatedcat = $generatedcats[$category['id']];
226             $this->assertEquals($category['idnumber'], $generatedcat->idnumber);
227             $this->assertEquals($category['name'], $generatedcat->name);
228             // Description was converted to the HTML format.
229             $this->assertEquals($category['description'], format_text($generatedcat->description, FORMAT_MOODLE, array('para' => false)));
230             $this->assertEquals($category['descriptionformat'], FORMAT_HTML);
231         }
233         // Check different params.
234         $categories = core_course_external::get_categories(array(
235             array('key' => 'id', 'value' => $category1->id),
236             array('key' => 'idnumber', 'value' => $category1->idnumber),
237             array('key' => 'visible', 'value' => 1)), 0);
239         // We need to execute the return values cleaning process to simulate the web service server.
240         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
242         $this->assertEquals(1, count($categories));
244         // Retrieve categories from parent.
245         $categories = core_course_external::get_categories(array(
246             array('key' => 'parent', 'value' => $category3->id)), 1);
247         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
249         $this->assertEquals(2, count($categories));
251         // Retrieve all categories.
252         $categories = core_course_external::get_categories();
254         // We need to execute the return values cleaning process to simulate the web service server.
255         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
257         $this->assertEquals($DB->count_records('course_categories'), count($categories));
259         // Call without required capability (it will fail cause of the search on idnumber).
260         $this->unassignUserCapability('moodle/category:manage', $context->id, $roleid);
261         $this->setExpectedException('moodle_exception');
262         $categories = core_course_external::get_categories(array(
263             array('key' => 'id', 'value' => $category1->id),
264             array('key' => 'idnumber', 'value' => $category1->idnumber),
265             array('key' => 'visible', 'value' => 1)), 0);
266     }
268     /**
269      * Test update_categories
270      */
271     public function test_update_categories() {
272         global $DB;
274         $this->resetAfterTest(true);
276         // Set the required capabilities by the external function
277         $contextid = context_system::instance()->id;
278         $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
280         // Create base categories.
281         $category1data['idnumber'] = 'idnumbercat1';
282         $category1data['name'] = 'Category 1 for PHPunit test';
283         $category1data['description'] = 'Category 1 description';
284         $category1data['descriptionformat'] = FORMAT_MOODLE;
285         $category1  = self::getDataGenerator()->create_category($category1data);
286         $category2  = self::getDataGenerator()->create_category(
287                 array('parent' => $category1->id));
288         $category3  = self::getDataGenerator()->create_category();
289         $category4  = self::getDataGenerator()->create_category(
290                 array('parent' => $category3->id));
291         $category5  = self::getDataGenerator()->create_category(
292                 array('parent' => $category4->id));
294         // We update all category1 attribut.
295         // Then we move cat4 and cat5 parent: cat3 => cat1
296         $categories = array(
297             array('id' => $category1->id,
298                 'name' => $category1->name . '_updated',
299                 'idnumber' => $category1->idnumber . '_updated',
300                 'description' => $category1->description . '_updated',
301                 'descriptionformat' => FORMAT_HTML,
302                 'theme' => $category1->theme),
303             array('id' => $category4->id, 'parent' => $category1->id));
305         core_course_external::update_categories($categories);
307         // Check the values were updated.
308         $dbcategories = $DB->get_records_select('course_categories',
309                 'id IN (' . $category1->id . ',' . $category2->id . ',' . $category2->id
310                 . ',' . $category3->id . ',' . $category4->id . ',' . $category5->id .')');
311         $this->assertEquals($category1->name . '_updated',
312                 $dbcategories[$category1->id]->name);
313         $this->assertEquals($category1->idnumber . '_updated',
314                 $dbcategories[$category1->id]->idnumber);
315         $this->assertEquals($category1->description . '_updated',
316                 $dbcategories[$category1->id]->description);
317         $this->assertEquals(FORMAT_HTML, $dbcategories[$category1->id]->descriptionformat);
319         // Check that category4 and category5 have been properly moved.
320         $this->assertEquals('/' . $category1->id . '/' . $category4->id,
321                 $dbcategories[$category4->id]->path);
322         $this->assertEquals('/' . $category1->id . '/' . $category4->id . '/' . $category5->id,
323                 $dbcategories[$category5->id]->path);
325         // Call without required capability.
326         $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
327         $this->setExpectedException('required_capability_exception');
328         core_course_external::update_categories($categories);
329     }
331     /**
332      * Test create_courses
333      */
334     public function test_create_courses() {
335         global $DB;
337         $this->resetAfterTest(true);
339         // Enable course completion.
340         set_config('enablecompletion', 1);
341         // Enable course themes.
342         set_config('allowcoursethemes', 1);
344         // Set the required capabilities by the external function
345         $contextid = context_system::instance()->id;
346         $roleid = $this->assignUserCapability('moodle/course:create', $contextid);
347         $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
349         $category  = self::getDataGenerator()->create_category();
351         // Create base categories.
352         $course1['fullname'] = 'Test course 1';
353         $course1['shortname'] = 'Testcourse1';
354         $course1['categoryid'] = $category->id;
355         $course2['fullname'] = 'Test course 2';
356         $course2['shortname'] = 'Testcourse2';
357         $course2['categoryid'] = $category->id;
358         $course2['idnumber'] = 'testcourse2idnumber';
359         $course2['summary'] = 'Description for course 2';
360         $course2['summaryformat'] = FORMAT_MOODLE;
361         $course2['format'] = 'weeks';
362         $course2['showgrades'] = 1;
363         $course2['newsitems'] = 3;
364         $course2['startdate'] = 1420092000; // 01/01/2015
365         $course2['numsections'] = 4;
366         $course2['maxbytes'] = 100000;
367         $course2['showreports'] = 1;
368         $course2['visible'] = 0;
369         $course2['hiddensections'] = 0;
370         $course2['groupmode'] = 0;
371         $course2['groupmodeforce'] = 0;
372         $course2['defaultgroupingid'] = 0;
373         $course2['enablecompletion'] = 1;
374         $course2['completionnotify'] = 1;
375         $course2['lang'] = 'en';
376         $course2['forcetheme'] = 'base';
377         $course3['fullname'] = 'Test course 3';
378         $course3['shortname'] = 'Testcourse3';
379         $course3['categoryid'] = $category->id;
380         $course3['format'] = 'topics';
381         $course3options = array('numsections' => 8,
382             'hiddensections' => 1,
383             'coursedisplay' => 1);
384         $course3['courseformatoptions'] = array();
385         foreach ($course3options as $key => $value) {
386             $course3['courseformatoptions'][] = array('name' => $key, 'value' => $value);
387         }
388         $courses = array($course1, $course2, $course3);
390         $createdcourses = core_course_external::create_courses($courses);
392         // We need to execute the return values cleaning process to simulate the web service server.
393         $createdcourses = external_api::clean_returnvalue(core_course_external::create_courses_returns(), $createdcourses);
395         // Check that right number of courses were created.
396         $this->assertEquals(3, count($createdcourses));
398         // Check that the courses were correctly created.
399         foreach ($createdcourses as $createdcourse) {
400             $courseinfo = course_get_format($createdcourse['id'])->get_course();
402             if ($createdcourse['shortname'] == $course2['shortname']) {
403                 $this->assertEquals($courseinfo->fullname, $course2['fullname']);
404                 $this->assertEquals($courseinfo->shortname, $course2['shortname']);
405                 $this->assertEquals($courseinfo->category, $course2['categoryid']);
406                 $this->assertEquals($courseinfo->idnumber, $course2['idnumber']);
407                 $this->assertEquals($courseinfo->summary, $course2['summary']);
408                 $this->assertEquals($courseinfo->summaryformat, $course2['summaryformat']);
409                 $this->assertEquals($courseinfo->format, $course2['format']);
410                 $this->assertEquals($courseinfo->showgrades, $course2['showgrades']);
411                 $this->assertEquals($courseinfo->newsitems, $course2['newsitems']);
412                 $this->assertEquals($courseinfo->startdate, $course2['startdate']);
413                 $this->assertEquals($courseinfo->numsections, $course2['numsections']);
414                 $this->assertEquals($courseinfo->maxbytes, $course2['maxbytes']);
415                 $this->assertEquals($courseinfo->showreports, $course2['showreports']);
416                 $this->assertEquals($courseinfo->visible, $course2['visible']);
417                 $this->assertEquals($courseinfo->hiddensections, $course2['hiddensections']);
418                 $this->assertEquals($courseinfo->groupmode, $course2['groupmode']);
419                 $this->assertEquals($courseinfo->groupmodeforce, $course2['groupmodeforce']);
420                 $this->assertEquals($courseinfo->defaultgroupingid, $course2['defaultgroupingid']);
421                 $this->assertEquals($courseinfo->completionnotify, $course2['completionnotify']);
422                 $this->assertEquals($courseinfo->lang, $course2['lang']);
423                 $this->assertEquals($courseinfo->theme, $course2['forcetheme']);
425                 // We enabled completion at the beginning of the test.
426                 $this->assertEquals($courseinfo->enablecompletion, $course2['enablecompletion']);
428             } else if ($createdcourse['shortname'] == $course1['shortname']) {
429                 $courseconfig = get_config('moodlecourse');
430                 $this->assertEquals($courseinfo->fullname, $course1['fullname']);
431                 $this->assertEquals($courseinfo->shortname, $course1['shortname']);
432                 $this->assertEquals($courseinfo->category, $course1['categoryid']);
433                 $this->assertEquals($courseinfo->summaryformat, FORMAT_HTML);
434                 $this->assertEquals($courseinfo->format, $courseconfig->format);
435                 $this->assertEquals($courseinfo->showgrades, $courseconfig->showgrades);
436                 $this->assertEquals($courseinfo->newsitems, $courseconfig->newsitems);
437                 $this->assertEquals($courseinfo->maxbytes, $courseconfig->maxbytes);
438                 $this->assertEquals($courseinfo->showreports, $courseconfig->showreports);
439                 $this->assertEquals($courseinfo->groupmode, $courseconfig->groupmode);
440                 $this->assertEquals($courseinfo->groupmodeforce, $courseconfig->groupmodeforce);
441                 $this->assertEquals($courseinfo->defaultgroupingid, 0);
442             } else if ($createdcourse['shortname'] == $course3['shortname']) {
443                 $this->assertEquals($courseinfo->fullname, $course3['fullname']);
444                 $this->assertEquals($courseinfo->shortname, $course3['shortname']);
445                 $this->assertEquals($courseinfo->category, $course3['categoryid']);
446                 $this->assertEquals($courseinfo->format, $course3['format']);
447                 $this->assertEquals($courseinfo->hiddensections, $course3options['hiddensections']);
448                 $this->assertEquals($courseinfo->numsections, $course3options['numsections']);
449                 $this->assertEquals($courseinfo->coursedisplay, $course3options['coursedisplay']);
450             } else {
451                 throw moodle_exception('Unexpected shortname');
452             }
453         }
455         // Call without required capability
456         $this->unassignUserCapability('moodle/course:create', $contextid, $roleid);
457         $this->setExpectedException('required_capability_exception');
458         $createdsubcats = core_course_external::create_courses($courses);
459     }
461     /**
462      * Test delete_courses
463      */
464     public function test_delete_courses() {
465         global $DB, $USER;
467         $this->resetAfterTest(true);
469         // Admin can delete a course.
470         $this->setAdminUser();
471         // Validate_context() will fail as the email is not set by $this->setAdminUser().
472         $USER->email = 'emailtopass@example.com';
474         $course1  = self::getDataGenerator()->create_course();
475         $course2  = self::getDataGenerator()->create_course();
476         $course3  = self::getDataGenerator()->create_course();
478         // Delete courses.
479         $result = core_course_external::delete_courses(array($course1->id, $course2->id));
480         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
481         // Check for 0 warnings.
482         $this->assertEquals(0, count($result['warnings']));
484         // Check $course 1 and 2 are deleted.
485         $notdeletedcount = $DB->count_records_select('course',
486             'id IN ( ' . $course1->id . ',' . $course2->id . ')');
487         $this->assertEquals(0, $notdeletedcount);
489         // Try to delete non-existent course.
490         $result = core_course_external::delete_courses(array($course1->id));
491         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
492         // Check for 1 warnings.
493         $this->assertEquals(1, count($result['warnings']));
495         // Try to delete Frontpage course.
496         $result = core_course_external::delete_courses(array(0));
497         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
498         // Check for 1 warnings.
499         $this->assertEquals(1, count($result['warnings']));
501          // Fail when the user has access to course (enrolled) but does not have permission or is not admin.
502         $student1 = self::getDataGenerator()->create_user();
503         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
504         $this->getDataGenerator()->enrol_user($student1->id,
505                                               $course3->id,
506                                               $studentrole->id);
507         $this->setUser($student1);
508         $result = core_course_external::delete_courses(array($course3->id));
509         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
510         // Check for 1 warnings.
511         $this->assertEquals(1, count($result['warnings']));
513          // Fail when the user is not allow to access the course (enrolled) or is not admin.
514         $this->setGuestUser();
515         $this->setExpectedException('require_login_exception');
517         $result = core_course_external::delete_courses(array($course3->id));
518         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
519     }
521     /**
522      * Test get_courses
523      */
524     public function test_get_courses () {
525         global $DB;
527         $this->resetAfterTest(true);
529         $generatedcourses = array();
530         $coursedata['idnumber'] = 'idnumbercourse1';
531         $coursedata['fullname'] = 'Course 1 for PHPunit test';
532         $coursedata['summary'] = 'Course 1 description';
533         $coursedata['summaryformat'] = FORMAT_MOODLE;
534         $course1  = self::getDataGenerator()->create_course($coursedata);
535         $generatedcourses[$course1->id] = $course1;
536         $course2  = self::getDataGenerator()->create_course();
537         $generatedcourses[$course2->id] = $course2;
538         $course3  = self::getDataGenerator()->create_course(array('format' => 'topics'));
539         $generatedcourses[$course3->id] = $course3;
541         // Set the required capabilities by the external function.
542         $context = context_system::instance();
543         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
544         $this->assignUserCapability('moodle/course:update',
545                 context_course::instance($course1->id)->id, $roleid);
546         $this->assignUserCapability('moodle/course:update',
547                 context_course::instance($course2->id)->id, $roleid);
548         $this->assignUserCapability('moodle/course:update',
549                 context_course::instance($course3->id)->id, $roleid);
551         $courses = core_course_external::get_courses(array('ids' =>
552             array($course1->id, $course2->id)));
554         // We need to execute the return values cleaning process to simulate the web service server.
555         $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
557         // Check we retrieve the good total number of categories.
558         $this->assertEquals(2, count($courses));
560         foreach ($courses as $course) {
561             $dbcourse = $generatedcourses[$course['id']];
562             $this->assertEquals($course['idnumber'], $dbcourse->idnumber);
563             $this->assertEquals($course['fullname'], $dbcourse->fullname);
564             // Summary was converted to the HTML format.
565             $this->assertEquals($course['summary'], format_text($dbcourse->summary, FORMAT_MOODLE, array('para' => false)));
566             $this->assertEquals($course['summaryformat'], FORMAT_HTML);
567             $this->assertEquals($course['shortname'], $dbcourse->shortname);
568             $this->assertEquals($course['categoryid'], $dbcourse->category);
569             $this->assertEquals($course['format'], $dbcourse->format);
570             $this->assertEquals($course['showgrades'], $dbcourse->showgrades);
571             $this->assertEquals($course['newsitems'], $dbcourse->newsitems);
572             $this->assertEquals($course['startdate'], $dbcourse->startdate);
573             $this->assertEquals($course['numsections'], $dbcourse->numsections);
574             $this->assertEquals($course['maxbytes'], $dbcourse->maxbytes);
575             $this->assertEquals($course['showreports'], $dbcourse->showreports);
576             $this->assertEquals($course['visible'], $dbcourse->visible);
577             $this->assertEquals($course['hiddensections'], $dbcourse->hiddensections);
578             $this->assertEquals($course['groupmode'], $dbcourse->groupmode);
579             $this->assertEquals($course['groupmodeforce'], $dbcourse->groupmodeforce);
580             $this->assertEquals($course['defaultgroupingid'], $dbcourse->defaultgroupingid);
581             $this->assertEquals($course['completionnotify'], $dbcourse->completionnotify);
582             $this->assertEquals($course['lang'], $dbcourse->lang);
583             $this->assertEquals($course['forcetheme'], $dbcourse->theme);
584             $this->assertEquals($course['enablecompletion'], $dbcourse->enablecompletion);
585             if ($dbcourse->format === 'topics') {
586                 $this->assertEquals($course['courseformatoptions'], array(
587                     array('name' => 'numsections', 'value' => $dbcourse->numsections),
588                     array('name' => 'hiddensections', 'value' => $dbcourse->hiddensections),
589                     array('name' => 'coursedisplay', 'value' => $dbcourse->coursedisplay),
590                 ));
591             }
592         }
594         // Get all courses in the DB
595         $courses = core_course_external::get_courses(array());
597         // We need to execute the return values cleaning process to simulate the web service server.
598         $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
600         $this->assertEquals($DB->count_records('course'), count($courses));
601     }
603     /**
604      * Test search_courses
605      */
606     public function test_search_courses () {
608         global $DB;
610         $this->resetAfterTest(true);
611         $this->setAdminUser();
612         $generatedcourses = array();
613         $coursedata1['fullname'] = 'FIRST COURSE';
614         $course1  = self::getDataGenerator()->create_course($coursedata1);
615         $coursedata2['fullname'] = 'SECOND COURSE';
616         $course2  = self::getDataGenerator()->create_course($coursedata2);
617         // Search by name.
618         $results = core_course_external::search_courses('search', 'FIRST');
619         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
620         $this->assertEquals($coursedata1['fullname'], $results['courses'][0]['fullname']);
621         $this->assertCount(1, $results['courses']);
623         // Create the forum.
624         $record = new stdClass();
625         $record->introformat = FORMAT_HTML;
626         $record->course = $course2->id;
627         // Set Aggregate type = Average of ratings.
628         $forum = self::getDataGenerator()->create_module('forum', $record);
630         // Search by module.
631         $results = core_course_external::search_courses('modulelist', 'forum');
632         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
633         $this->assertEquals(1, $results['total']);
635         // Enable coursetag option.
636         set_config('block_tags_showcoursetags', true);
637         // Add tag 'TAG-LABEL ON SECOND COURSE' to Course2.
638         core_tag_tag::set_item_tags('core', 'course', $course2->id, context_course::instance($course2->id),
639                 array('TAG-LABEL ON SECOND COURSE'));
640         $taginstance = $DB->get_record('tag_instance',
641                 array('itemtype' => 'course', 'itemid' => $course2->id), '*', MUST_EXIST);
642         // Search by tagid.
643         $results = core_course_external::search_courses('tagid', $taginstance->tagid);
644         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
645         $this->assertEquals($coursedata2['fullname'], $results['courses'][0]['fullname']);
647         // Search by block (use news_items default block).
648         $blockid = $DB->get_field('block', 'id', array('name' => 'news_items'));
649         $results = core_course_external::search_courses('blocklist', $blockid);
650         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
651         $this->assertEquals(2, $results['total']);
653         // Now as a normal user.
654         $user = self::getDataGenerator()->create_user();
655         $this->setUser($user);
657         $results = core_course_external::search_courses('search', 'FIRST');
658         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
659         $this->assertCount(1, $results['courses']);
660         $this->assertEquals(1, $results['total']);
661         $this->assertEquals($coursedata1['fullname'], $results['courses'][0]['fullname']);
663         // Search by block (use news_items default block). Should fail (only admins allowed).
664         $this->setExpectedException('required_capability_exception');
665         $results = core_course_external::search_courses('blocklist', $blockid);
667     }
669     /**
670      * Create a course with contents
671      * @return array A list with the course object and course modules objects
672      */
673     private function prepare_get_course_contents_test() {
674         $course  = self::getDataGenerator()->create_course();
675         $forumdescription = 'This is the forum description';
676         $forum = $this->getDataGenerator()->create_module('forum',
677             array('course' => $course->id, 'intro' => $forumdescription),
678             array('showdescription' => true));
679         $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
680         $data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
681         $datacm = get_coursemodule_from_instance('page', $data->id);
682         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
683         $pagecm = get_coursemodule_from_instance('page', $page->id);
684         $labeldescription = 'This is a very long label to test if more than 50 characters are returned.
685                 So bla bla bla bla <b>bold bold bold</b> bla bla bla bla.';
686         $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id,
687             'intro' => $labeldescription));
688         $labelcm = get_coursemodule_from_instance('label', $label->id);
689         $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id,
690             'name' => 'URL: % & $ ../', 'section' => 2));
691         $urlcm = get_coursemodule_from_instance('url', $url->id);
693         // Set the required capabilities by the external function.
694         $context = context_course::instance($course->id);
695         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
696         $this->assignUserCapability('moodle/course:update', $context->id, $roleid);
698         return array($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm);
699     }
701     /**
702      * Test get_course_contents
703      */
704     public function test_get_course_contents() {
705         $this->resetAfterTest(true);
707         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
709         $sections = core_course_external::get_course_contents($course->id, array());
710         // We need to execute the return values cleaning process to simulate the web service server.
711         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
713         // Check that forum and label descriptions are correctly returned.
714         $firstsection = array_shift($sections);
715         $lastsection = array_pop($sections);
717         $modinfo = get_fast_modinfo($course);
718         $testexecuted = 0;
719         foreach ($firstsection['modules'] as $module) {
720             if ($module['id'] == $forumcm->id and $module['modname'] == 'forum') {
721                 $cm = $modinfo->cms[$forumcm->id];
722                 $formattedtext = format_text($cm->content, FORMAT_HTML,
723                     array('noclean' => true, 'para' => false, 'filter' => false));
724                 $this->assertEquals($formattedtext, $module['description']);
725                 $this->assertEquals($forumcm->instance, $module['instance']);
726                 $testexecuted = $testexecuted + 1;
727             } else if ($module['id'] == $labelcm->id and $module['modname'] == 'label') {
728                 $cm = $modinfo->cms[$labelcm->id];
729                 $formattedtext = format_text($cm->content, FORMAT_HTML,
730                     array('noclean' => true, 'para' => false, 'filter' => false));
731                 $this->assertEquals($formattedtext, $module['description']);
732                 $this->assertEquals($labelcm->instance, $module['instance']);
733                 $testexecuted = $testexecuted + 1;
734             }
735         }
736         $this->assertEquals(2, $testexecuted);
738         // Check that the only return section has the 5 created modules.
739         $this->assertCount(4, $firstsection['modules']);
740         $this->assertCount(1, $lastsection['modules']);
742         try {
743             $sections = core_course_external::get_course_contents($course->id,
744                                                                     array(array("name" => "invalid", "value" => 1)));
745             $this->fail('Exception expected due to invalid option.');
746         } catch (moodle_exception $e) {
747             $this->assertEquals('errorinvalidparam', $e->errorcode);
748         }
749     }
752     /**
753      * Test get_course_contents excluding modules
754      */
755     public function test_get_course_contents_excluding_modules() {
756         $this->resetAfterTest(true);
758         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
760         // Test exclude modules.
761         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludemodules", "value" => 1)));
763         // We need to execute the return values cleaning process to simulate the web service server.
764         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
766         $firstsection = array_shift($sections);
767         $lastsection = array_pop($sections);
769         $this->assertEmpty($firstsection['modules']);
770         $this->assertEmpty($lastsection['modules']);
771     }
773     /**
774      * Test get_course_contents excluding contents
775      */
776     public function test_get_course_contents_excluding_contents() {
777         $this->resetAfterTest(true);
779         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
781         // Test exclude modules.
782         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludecontents", "value" => 1)));
784         // We need to execute the return values cleaning process to simulate the web service server.
785         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
787         foreach ($sections as $section) {
788             foreach ($section['modules'] as $module) {
789                 // Only resources return contents.
790                 if (isset($module['contents'])) {
791                     $this->assertEmpty($module['contents']);
792                 }
793             }
794         }
795     }
797     /**
798      * Test get_course_contents filtering by section number
799      */
800     public function test_get_course_contents_section_number() {
801         $this->resetAfterTest(true);
803         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
805         // Test exclude modules.
806         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "sectionnumber", "value" => 0)));
808         // We need to execute the return values cleaning process to simulate the web service server.
809         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
811         $this->assertCount(1, $sections);
812         $this->assertCount(4, $sections[0]['modules']);
813     }
815     /**
816      * Test get_course_contents filtering by cmid
817      */
818     public function test_get_course_contents_cmid() {
819         $this->resetAfterTest(true);
821         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
823         // Test exclude modules.
824         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "cmid", "value" => $forumcm->id)));
826         // We need to execute the return values cleaning process to simulate the web service server.
827         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
829         $this->assertCount(2, $sections);
830         $this->assertCount(1, $sections[0]['modules']);
831         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
832     }
835     /**
836      * Test get_course_contents filtering by cmid and section
837      */
838     public function test_get_course_contents_section_cmid() {
839         $this->resetAfterTest(true);
841         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
843         // Test exclude modules.
844         $sections = core_course_external::get_course_contents($course->id, array(
845                                                                         array("name" => "cmid", "value" => $forumcm->id),
846                                                                         array("name" => "sectionnumber", "value" => 0)
847                                                                         ));
849         // We need to execute the return values cleaning process to simulate the web service server.
850         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
852         $this->assertCount(1, $sections);
853         $this->assertCount(1, $sections[0]['modules']);
854         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
855     }
857     /**
858      * Test get_course_contents filtering by modname
859      */
860     public function test_get_course_contents_modname() {
861         $this->resetAfterTest(true);
863         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
865         // Test exclude modules.
866         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "modname", "value" => "forum")));
868         // We need to execute the return values cleaning process to simulate the web service server.
869         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
871         $this->assertCount(2, $sections);
872         $this->assertCount(1, $sections[0]['modules']);
873         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
874     }
876     /**
877      * Test get_course_contents filtering by modname
878      */
879     public function test_get_course_contents_modid() {
880         $this->resetAfterTest(true);
882         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
884         // Test exclude modules.
885         $sections = core_course_external::get_course_contents($course->id, array(
886                                                                             array("name" => "modname", "value" => "page"),
887                                                                             array("name" => "modid", "value" => $pagecm->instance),
888                                                                             ));
890         // We need to execute the return values cleaning process to simulate the web service server.
891         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
893         $this->assertCount(2, $sections);
894         $this->assertCount(1, $sections[0]['modules']);
895         $this->assertEquals("page", $sections[0]['modules'][0]["modname"]);
896         $this->assertEquals($pagecm->instance, $sections[0]['modules'][0]["instance"]);
897     }
899     /**
900      * Test duplicate_course
901      */
902     public function test_duplicate_course() {
903         $this->resetAfterTest(true);
905         // Create one course with three modules.
906         $course  = self::getDataGenerator()->create_course();
907         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
908         $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
909         $forumcontext = context_module::instance($forum->cmid);
910         $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
911         $datacontext = context_module::instance($data->cmid);
912         $datacm = get_coursemodule_from_instance('page', $data->id);
913         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
914         $pagecontext = context_module::instance($page->cmid);
915         $pagecm = get_coursemodule_from_instance('page', $page->id);
917         // Set the required capabilities by the external function.
918         $coursecontext = context_course::instance($course->id);
919         $categorycontext = context_coursecat::instance($course->category);
920         $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id);
921         $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid);
922         $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid);
923         $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid);
924         $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid);
925         // Optional capabilities to copy user data.
926         $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid);
927         $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid);
929         $newcourse['fullname'] = 'Course duplicate';
930         $newcourse['shortname'] = 'courseduplicate';
931         $newcourse['categoryid'] = $course->category;
932         $newcourse['visible'] = true;
933         $newcourse['options'][] = array('name' => 'users', 'value' => true);
935         $duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'],
936                 $newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']);
938         // We need to execute the return values cleaning process to simulate the web service server.
939         $duplicate = external_api::clean_returnvalue(core_course_external::duplicate_course_returns(), $duplicate);
941         // Check that the course has been duplicated.
942         $this->assertEquals($newcourse['shortname'], $duplicate['shortname']);
943     }
945     /**
946      * Test update_courses
947      */
948     public function test_update_courses() {
949         global $DB, $CFG, $USER, $COURSE;
951         // Get current $COURSE to be able to restore it later (defaults to $SITE). We need this
952         // trick because we are both updating and getting (for testing) course information
953         // in the same request and core_course_external::update_courses()
954         // is overwriting $COURSE all over the time with OLD values, so later
955         // use of get_course() fetches those OLD values instead of the updated ones.
956         // See MDL-39723 for more info.
957         $origcourse = clone($COURSE);
959         $this->resetAfterTest(true);
961         // Set the required capabilities by the external function.
962         $contextid = context_system::instance()->id;
963         $roleid = $this->assignUserCapability('moodle/course:update', $contextid);
964         $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
965         $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
966         $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
967         $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
968         $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
969         $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
970         $this->assignUserCapability('moodle/course:viewhiddencourses', $contextid, $roleid);
972         // Create category and course.
973         $category1  = self::getDataGenerator()->create_category();
974         $category2  = self::getDataGenerator()->create_category();
975         $originalcourse1 = self::getDataGenerator()->create_course();
976         self::getDataGenerator()->enrol_user($USER->id, $originalcourse1->id, $roleid);
977         $originalcourse2 = self::getDataGenerator()->create_course();
978         self::getDataGenerator()->enrol_user($USER->id, $originalcourse2->id, $roleid);
980         // Course values to be updated.
981         $course1['id'] = $originalcourse1->id;
982         $course1['fullname'] = 'Updated test course 1';
983         $course1['shortname'] = 'Udestedtestcourse1';
984         $course1['categoryid'] = $category1->id;
985         $course2['id'] = $originalcourse2->id;
986         $course2['fullname'] = 'Updated test course 2';
987         $course2['shortname'] = 'Updestedtestcourse2';
988         $course2['categoryid'] = $category2->id;
989         $course2['idnumber'] = 'Updatedidnumber2';
990         $course2['summary'] = 'Updaated description for course 2';
991         $course2['summaryformat'] = FORMAT_HTML;
992         $course2['format'] = 'topics';
993         $course2['showgrades'] = 1;
994         $course2['newsitems'] = 3;
995         $course2['startdate'] = 1420092000; // 01/01/2015.
996         $course2['numsections'] = 4;
997         $course2['maxbytes'] = 100000;
998         $course2['showreports'] = 1;
999         $course2['visible'] = 0;
1000         $course2['hiddensections'] = 0;
1001         $course2['groupmode'] = 0;
1002         $course2['groupmodeforce'] = 0;
1003         $course2['defaultgroupingid'] = 0;
1004         $course2['enablecompletion'] = 1;
1005         $course2['lang'] = 'en';
1006         $course2['forcetheme'] = 'base';
1007         $courses = array($course1, $course2);
1009         $updatedcoursewarnings = core_course_external::update_courses($courses);
1010         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1011                                                                     $updatedcoursewarnings);
1012         $COURSE = $origcourse; // Restore $COURSE. Instead of using the OLD one set by the previous line.
1014         // Check that right number of courses were created.
1015         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1017         // Check that the courses were correctly created.
1018         foreach ($courses as $course) {
1019             $courseinfo = course_get_format($course['id'])->get_course();
1020             if ($course['id'] == $course2['id']) {
1021                 $this->assertEquals($course2['fullname'], $courseinfo->fullname);
1022                 $this->assertEquals($course2['shortname'], $courseinfo->shortname);
1023                 $this->assertEquals($course2['categoryid'], $courseinfo->category);
1024                 $this->assertEquals($course2['idnumber'], $courseinfo->idnumber);
1025                 $this->assertEquals($course2['summary'], $courseinfo->summary);
1026                 $this->assertEquals($course2['summaryformat'], $courseinfo->summaryformat);
1027                 $this->assertEquals($course2['format'], $courseinfo->format);
1028                 $this->assertEquals($course2['showgrades'], $courseinfo->showgrades);
1029                 $this->assertEquals($course2['newsitems'], $courseinfo->newsitems);
1030                 $this->assertEquals($course2['startdate'], $courseinfo->startdate);
1031                 $this->assertEquals($course2['numsections'], $courseinfo->numsections);
1032                 $this->assertEquals($course2['maxbytes'], $courseinfo->maxbytes);
1033                 $this->assertEquals($course2['showreports'], $courseinfo->showreports);
1034                 $this->assertEquals($course2['visible'], $courseinfo->visible);
1035                 $this->assertEquals($course2['hiddensections'], $courseinfo->hiddensections);
1036                 $this->assertEquals($course2['groupmode'], $courseinfo->groupmode);
1037                 $this->assertEquals($course2['groupmodeforce'], $courseinfo->groupmodeforce);
1038                 $this->assertEquals($course2['defaultgroupingid'], $courseinfo->defaultgroupingid);
1039                 $this->assertEquals($course2['lang'], $courseinfo->lang);
1041                 if (!empty($CFG->allowcoursethemes)) {
1042                     $this->assertEquals($course2['forcetheme'], $courseinfo->theme);
1043                 }
1045                 $this->assertEquals($course2['enablecompletion'], $courseinfo->enablecompletion);
1046             } else if ($course['id'] == $course1['id']) {
1047                 $this->assertEquals($course1['fullname'], $courseinfo->fullname);
1048                 $this->assertEquals($course1['shortname'], $courseinfo->shortname);
1049                 $this->assertEquals($course1['categoryid'], $courseinfo->category);
1050                 $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
1051                 $this->assertEquals('topics', $courseinfo->format);
1052                 $this->assertEquals(5, $courseinfo->numsections);
1053                 $this->assertEquals(0, $courseinfo->newsitems);
1054                 $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
1055             } else {
1056                 throw moodle_exception('Unexpected shortname');
1057             }
1058         }
1060         $courses = array($course1);
1061         // Try update course without update capability.
1062         $user = self::getDataGenerator()->create_user();
1063         $this->setUser($user);
1064         $this->unassignUserCapability('moodle/course:update', $contextid, $roleid);
1065         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1066         $updatedcoursewarnings = core_course_external::update_courses($courses);
1067         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1068                                                                     $updatedcoursewarnings);
1069         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1071         // Try update course category without capability.
1072         $this->assignUserCapability('moodle/course:update', $contextid, $roleid);
1073         $this->unassignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1074         $user = self::getDataGenerator()->create_user();
1075         $this->setUser($user);
1076         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1077         $course1['categoryid'] = $category2->id;
1078         $courses = array($course1);
1079         $updatedcoursewarnings = core_course_external::update_courses($courses);
1080         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1081                                                                     $updatedcoursewarnings);
1082         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1084         // Try update course fullname without capability.
1085         $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1086         $this->unassignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1087         $user = self::getDataGenerator()->create_user();
1088         $this->setUser($user);
1089         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1090         $updatedcoursewarnings = core_course_external::update_courses($courses);
1091         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1092                                                                     $updatedcoursewarnings);
1093         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1094         $course1['fullname'] = 'Testing fullname without permission';
1095         $courses = array($course1);
1096         $updatedcoursewarnings = core_course_external::update_courses($courses);
1097         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1098                                                                     $updatedcoursewarnings);
1099         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1101         // Try update course shortname without capability.
1102         $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1103         $this->unassignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1104         $user = self::getDataGenerator()->create_user();
1105         $this->setUser($user);
1106         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1107         $updatedcoursewarnings = core_course_external::update_courses($courses);
1108         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1109                                                                     $updatedcoursewarnings);
1110         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1111         $course1['shortname'] = 'Testing shortname without permission';
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 idnumber without capability.
1119         $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1120         $this->unassignUserCapability('moodle/course:changeidnumber', $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['idnumber'] = 'NEWIDNUMBER';
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 summary without capability.
1136         $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1137         $this->unassignUserCapability('moodle/course:changesummary', $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['summary'] = 'New summary';
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 with invalid summary format.
1153         $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1154         $user = self::getDataGenerator()->create_user();
1155         $this->setUser($user);
1156         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1157         $updatedcoursewarnings = core_course_external::update_courses($courses);
1158         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1159                                                                     $updatedcoursewarnings);
1160         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1161         $course1['summaryformat'] = 10;
1162         $courses = array($course1);
1163         $updatedcoursewarnings = core_course_external::update_courses($courses);
1164         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1165                                                                     $updatedcoursewarnings);
1166         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1168         // Try update course visibility without capability.
1169         $this->unassignUserCapability('moodle/course:visibility', $contextid, $roleid);
1170         $user = self::getDataGenerator()->create_user();
1171         $this->setUser($user);
1172         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1173         $course1['summaryformat'] = FORMAT_MOODLE;
1174         $courses = array($course1);
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['visible'] = 0;
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']));
1185     }
1187     /**
1188      * Test delete course_module.
1189      */
1190     public function test_delete_modules() {
1191         global $DB;
1193         // Ensure we reset the data after this test.
1194         $this->resetAfterTest(true);
1196         // Create a user.
1197         $user = self::getDataGenerator()->create_user();
1199         // Set the tests to run as the user.
1200         self::setUser($user);
1202         // Create a course to add the modules.
1203         $course = self::getDataGenerator()->create_course();
1205         // Create two test modules.
1206         $record = new stdClass();
1207         $record->course = $course->id;
1208         $module1 = self::getDataGenerator()->create_module('forum', $record);
1209         $module2 = self::getDataGenerator()->create_module('assign', $record);
1211         // Check the forum was correctly created.
1212         $this->assertEquals(1, $DB->count_records('forum', array('id' => $module1->id)));
1214         // Check the assignment was correctly created.
1215         $this->assertEquals(1, $DB->count_records('assign', array('id' => $module2->id)));
1217         // Check data exists in the course modules table.
1218         $this->assertEquals(2, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
1219                 array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
1221         // Enrol the user in the course.
1222         $enrol = enrol_get_plugin('manual');
1223         $enrolinstances = enrol_get_instances($course->id, true);
1224         foreach ($enrolinstances as $courseenrolinstance) {
1225             if ($courseenrolinstance->enrol == "manual") {
1226                 $instance = $courseenrolinstance;
1227                 break;
1228             }
1229         }
1230         $enrol->enrol_user($instance, $user->id);
1232         // Assign capabilities to delete module 1.
1233         $modcontext = context_module::instance($module1->cmid);
1234         $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id);
1236         // Assign capabilities to delete module 2.
1237         $modcontext = context_module::instance($module2->cmid);
1238         $newrole = create_role('Role 2', 'role2', 'Role 2 description');
1239         $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id, $newrole);
1241         // Deleting these module instances.
1242         core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1244         // Check the forum was deleted.
1245         $this->assertEquals(0, $DB->count_records('forum', array('id' => $module1->id)));
1247         // Check the assignment was deleted.
1248         $this->assertEquals(0, $DB->count_records('assign', array('id' => $module2->id)));
1250         // Check we retrieve no data in the course modules table.
1251         $this->assertEquals(0, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
1252                 array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
1254         // Call with non-existent course module id and ensure exception thrown.
1255         try {
1256             core_course_external::delete_modules(array('1337'));
1257             $this->fail('Exception expected due to missing course module.');
1258         } catch (dml_missing_record_exception $e) {
1259             $this->assertEquals('invalidcoursemodule', $e->errorcode);
1260         }
1262         // Create two modules.
1263         $module1 = self::getDataGenerator()->create_module('forum', $record);
1264         $module2 = self::getDataGenerator()->create_module('assign', $record);
1266         // Since these modules were recreated the user will not have capabilities
1267         // to delete them, ensure exception is thrown if they try.
1268         try {
1269             core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1270             $this->fail('Exception expected due to missing capability.');
1271         } catch (moodle_exception $e) {
1272             $this->assertEquals('nopermissions', $e->errorcode);
1273         }
1275         // Unenrol user from the course.
1276         $enrol->unenrol_user($instance, $user->id);
1278         // Try and delete modules from the course the user was unenrolled in, make sure exception thrown.
1279         try {
1280             core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1281             $this->fail('Exception expected due to being unenrolled from the course.');
1282         } catch (moodle_exception $e) {
1283             $this->assertEquals('requireloginerror', $e->errorcode);
1284         }
1285     }
1287     /**
1288      * Test import_course into an empty course
1289      */
1290     public function test_import_course_empty() {
1291         global $USER;
1293         $this->resetAfterTest(true);
1295         $course1  = self::getDataGenerator()->create_course();
1296         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course1->id, 'name' => 'Forum test'));
1297         $page = $this->getDataGenerator()->create_module('page', array('course' => $course1->id, 'name' => 'Page test'));
1299         $course2  = self::getDataGenerator()->create_course();
1301         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1302         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1304         // Verify the state of the courses before we do the import.
1305         $this->assertCount(2, $course1cms);
1306         $this->assertEmpty($course2cms);
1308         // Setup the user to run the operation (ugly hack because validate_context() will
1309         // fail as the email is not set by $this->setAdminUser()).
1310         $this->setAdminUser();
1311         $USER->email = 'emailtopass@example.com';
1313         // Import from course1 to course2.
1314         core_course_external::import_course($course1->id, $course2->id, 0);
1316         // Verify that now we have two modules in both courses.
1317         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1318         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1319         $this->assertCount(2, $course1cms);
1320         $this->assertCount(2, $course2cms);
1322         // Verify that the names transfered across correctly.
1323         foreach ($course2cms as $cm) {
1324             if ($cm->modname === 'page') {
1325                 $this->assertEquals($cm->name, $page->name);
1326             } else if ($cm->modname === 'forum') {
1327                 $this->assertEquals($cm->name, $forum->name);
1328             } else {
1329                 $this->fail('Unknown CM found.');
1330             }
1331         }
1332     }
1334     /**
1335      * Test import_course into an filled course
1336      */
1337     public function test_import_course_filled() {
1338         global $USER;
1340         $this->resetAfterTest(true);
1342         // Add forum and page to course1.
1343         $course1  = self::getDataGenerator()->create_course();
1344         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1345         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
1347         // Add quiz to course 2.
1348         $course2  = self::getDataGenerator()->create_course();
1349         $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
1351         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1352         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1354         // Verify the state of the courses before we do the import.
1355         $this->assertCount(2, $course1cms);
1356         $this->assertCount(1, $course2cms);
1358         // Setup the user to run the operation (ugly hack because validate_context() will
1359         // fail as the email is not set by $this->setAdminUser()).
1360         $this->setAdminUser();
1361         $USER->email = 'emailtopass@example.com';
1363         // Import from course1 to course2 without deleting content.
1364         core_course_external::import_course($course1->id, $course2->id, 0);
1366         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1368         // Verify that now we have three modules in course2.
1369         $this->assertCount(3, $course2cms);
1371         // Verify that the names transfered across correctly.
1372         foreach ($course2cms as $cm) {
1373             if ($cm->modname === 'page') {
1374                 $this->assertEquals($cm->name, $page->name);
1375             } else if ($cm->modname === 'forum') {
1376                 $this->assertEquals($cm->name, $forum->name);
1377             } else if ($cm->modname === 'quiz') {
1378                 $this->assertEquals($cm->name, $quiz->name);
1379             } else {
1380                 $this->fail('Unknown CM found.');
1381             }
1382         }
1383     }
1385     /**
1386      * Test import_course with only blocks set to backup
1387      */
1388     public function test_import_course_blocksonly() {
1389         global $USER, $DB;
1391         $this->resetAfterTest(true);
1393         // Add forum and page to course1.
1394         $course1  = self::getDataGenerator()->create_course();
1395         $course1ctx = context_course::instance($course1->id);
1396         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1397         $block = $this->getDataGenerator()->create_block('online_users', array('parentcontextid' => $course1ctx->id));
1399         $course2  = self::getDataGenerator()->create_course();
1400         $course2ctx = context_course::instance($course2->id);
1401         $initialblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
1402         $initialcmcount = count(get_fast_modinfo($course2->id)->get_cms());
1404         // Setup the user to run the operation (ugly hack because validate_context() will
1405         // fail as the email is not set by $this->setAdminUser()).
1406         $this->setAdminUser();
1407         $USER->email = 'emailtopass@example.com';
1409         // Import from course1 to course2 without deleting content, but excluding
1410         // activities.
1411         $options = array(
1412             array('name' => 'activities', 'value' => 0),
1413             array('name' => 'blocks', 'value' => 1),
1414             array('name' => 'filters', 'value' => 0),
1415         );
1417         core_course_external::import_course($course1->id, $course2->id, 0, $options);
1419         $newcmcount = count(get_fast_modinfo($course2->id)->get_cms());
1420         $newblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
1421         // Check that course modules haven't changed, but that blocks have.
1422         $this->assertEquals($initialcmcount, $newcmcount);
1423         $this->assertEquals(($initialblockcount + 1), $newblockcount);
1424     }
1426     /**
1427      * Test import_course into an filled course, deleting content.
1428      */
1429     public function test_import_course_deletecontent() {
1430         global $USER;
1431         $this->resetAfterTest(true);
1433         // Add forum and page to course1.
1434         $course1  = self::getDataGenerator()->create_course();
1435         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1436         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
1438         // Add quiz to course 2.
1439         $course2  = self::getDataGenerator()->create_course();
1440         $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
1442         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1443         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1445         // Verify the state of the courses before we do the import.
1446         $this->assertCount(2, $course1cms);
1447         $this->assertCount(1, $course2cms);
1449         // Setup the user to run the operation (ugly hack because validate_context() will
1450         // fail as the email is not set by $this->setAdminUser()).
1451         $this->setAdminUser();
1452         $USER->email = 'emailtopass@example.com';
1454         // Import from course1 to course2,  deleting content.
1455         core_course_external::import_course($course1->id, $course2->id, 1);
1457         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1459         // Verify that now we have two modules in course2.
1460         $this->assertCount(2, $course2cms);
1462         // Verify that the course only contains the imported modules.
1463         foreach ($course2cms as $cm) {
1464             if ($cm->modname === 'page') {
1465                 $this->assertEquals($cm->name, $page->name);
1466             } else if ($cm->modname === 'forum') {
1467                 $this->assertEquals($cm->name, $forum->name);
1468             } else {
1469                 $this->fail('Unknown CM found: '.$cm->name);
1470             }
1471         }
1472     }
1474     /**
1475      * Ensure import_course handles incorrect deletecontent option correctly.
1476      */
1477     public function test_import_course_invalid_deletecontent_option() {
1478         $this->resetAfterTest(true);
1480         $course1  = self::getDataGenerator()->create_course();
1481         $course2  = self::getDataGenerator()->create_course();
1483         $this->setExpectedException('moodle_exception', get_string('invalidextparam', 'webservice', -1));
1484         // Import from course1 to course2, with invalid option
1485         core_course_external::import_course($course1->id, $course2->id, -1);;
1486     }
1488     /**
1489      * Test view_course function
1490      */
1491     public function test_view_course() {
1493         $this->resetAfterTest();
1495         // Course without sections.
1496         $course = $this->getDataGenerator()->create_course(array('numsections' => 5), array('createsections' => true));
1497         $this->setAdminUser();
1499         // Redirect events to the sink, so we can recover them later.
1500         $sink = $this->redirectEvents();
1502         $result = core_course_external::view_course($course->id, 1);
1503         $result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result);
1504         $events = $sink->get_events();
1505         $event = reset($events);
1507         // Check the event details are correct.
1508         $this->assertInstanceOf('\core\event\course_viewed', $event);
1509         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1510         $this->assertEquals(1, $event->other['coursesectionnumber']);
1512         $result = core_course_external::view_course($course->id);
1513         $result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result);
1514         $events = $sink->get_events();
1515         $event = array_pop($events);
1516         $sink->close();
1518         // Check the event details are correct.
1519         $this->assertInstanceOf('\core\event\course_viewed', $event);
1520         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1521         $this->assertEmpty($event->other);
1523     }
1525     /**
1526      * Test get_course_module
1527      */
1528     public function test_get_course_module() {
1529         global $DB;
1531         $this->resetAfterTest(true);
1533         $this->setAdminUser();
1534         $course = self::getDataGenerator()->create_course();
1535         $record = array(
1536             'course' => $course->id,
1537             'name' => 'First Chat'
1538         );
1539         $options = array(
1540             'idnumber' => 'ABC',
1541             'visible' => 0
1542         );
1543         // Hidden activity.
1544         $chat = self::getDataGenerator()->create_module('chat', $record, $options);
1546         // Test admin user can see the complete hidden activity.
1547         $result = core_course_external::get_course_module($chat->cmid);
1548         $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result);
1550         $this->assertCount(0, $result['warnings']);
1551         // Test we retrieve all the fields.
1552         $this->assertCount(22, $result['cm']);
1553         $this->assertEquals($record['name'], $result['cm']['name']);
1554         $this->assertEquals($options['idnumber'], $result['cm']['idnumber']);
1556         $student = $this->getDataGenerator()->create_user();
1557         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1559         self::getDataGenerator()->enrol_user($student->id,  $course->id, $studentrole->id);
1560         $this->setUser($student);
1562         // The user shouldn't be able to see the activity.
1563         try {
1564             core_course_external::get_course_module($chat->cmid);
1565             $this->fail('Exception expected due to invalid permissions.');
1566         } catch (moodle_exception $e) {
1567             $this->assertEquals('requireloginerror', $e->errorcode);
1568         }
1570         // Make module visible.
1571         set_coursemodule_visible($chat->cmid, 1);
1573         // Test student user.
1574         $result = core_course_external::get_course_module($chat->cmid);
1575         $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result);
1577         $this->assertCount(0, $result['warnings']);
1578         // Test we retrieve only the few files we can see.
1579         $this->assertCount(11, $result['cm']);
1580         $this->assertEquals($chat->cmid, $result['cm']['id']);
1581         $this->assertEquals($course->id, $result['cm']['course']);
1582         $this->assertEquals('chat', $result['cm']['modname']);
1583         $this->assertEquals($chat->id, $result['cm']['instance']);
1585     }
1587     /**
1588      * Test get_course_module_by_instance
1589      */
1590     public function test_get_course_module_by_instance() {
1591         global $DB;
1593         $this->resetAfterTest(true);
1595         $this->setAdminUser();
1596         $course = self::getDataGenerator()->create_course();
1597         $record = array(
1598             'course' => $course->id,
1599             'name' => 'First Chat'
1600         );
1601         $options = array(
1602             'idnumber' => 'ABC',
1603             'visible' => 0
1604         );
1605         // Hidden activity.
1606         $chat = self::getDataGenerator()->create_module('chat', $record, $options);
1608         // Test admin user can see the complete hidden activity.
1609         $result = core_course_external::get_course_module_by_instance('chat', $chat->id);
1610         $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result);
1612         $this->assertCount(0, $result['warnings']);
1613         // Test we retrieve all the fields.
1614         $this->assertCount(22, $result['cm']);
1615         $this->assertEquals($record['name'], $result['cm']['name']);
1616         $this->assertEquals($options['idnumber'], $result['cm']['idnumber']);
1618         $student = $this->getDataGenerator()->create_user();
1619         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1621         self::getDataGenerator()->enrol_user($student->id,  $course->id, $studentrole->id);
1622         $this->setUser($student);
1624         // The user shouldn't be able to see the activity.
1625         try {
1626             core_course_external::get_course_module_by_instance('chat', $chat->id);
1627             $this->fail('Exception expected due to invalid permissions.');
1628         } catch (moodle_exception $e) {
1629             $this->assertEquals('requireloginerror', $e->errorcode);
1630         }
1632         // Make module visible.
1633         set_coursemodule_visible($chat->cmid, 1);
1635         // Test student user.
1636         $result = core_course_external::get_course_module_by_instance('chat', $chat->id);
1637         $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result);
1639         $this->assertCount(0, $result['warnings']);
1640         // Test we retrieve only the few files we can see.
1641         $this->assertCount(11, $result['cm']);
1642         $this->assertEquals($chat->cmid, $result['cm']['id']);
1643         $this->assertEquals($course->id, $result['cm']['course']);
1644         $this->assertEquals('chat', $result['cm']['modname']);
1645         $this->assertEquals($chat->id, $result['cm']['instance']);
1647         // Try with an invalid module name.
1648         try {
1649             core_course_external::get_course_module_by_instance('abc', $chat->id);
1650             $this->fail('Exception expected due to invalid module name.');
1651         } catch (dml_read_exception $e) {
1652             $this->assertEquals('dmlreadexception', $e->errorcode);
1653         }
1655     }