Merge branch 'MDL-57392-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 = 'bootstrapbase';
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->expectException('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->expectException('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 categories by ids.
227         $ids = implode(',', array_keys($generatedcats));
228         $categories = core_course_external::get_categories(array(
229             array('key' => 'ids', 'value' => $ids)), 0);
231         // We need to execute the return values cleaning process to simulate the web service server.
232         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
234         // Check we retrieve the good total number of categories.
235         $this->assertEquals(6, count($categories));
236         // Check ids.
237         $returnedids = [];
238         foreach ($categories as $category) {
239             $returnedids[] = $category['id'];
240         }
241         // Sort the arrays upon comparision.
242         $this->assertEquals(array_keys($generatedcats), $returnedids, '', 0.0, 10, true);
244         // Check different params.
245         $categories = core_course_external::get_categories(array(
246             array('key' => 'id', 'value' => $category1->id),
247             array('key' => 'ids', 'value' => $category1->id),
248             array('key' => 'idnumber', 'value' => $category1->idnumber),
249             array('key' => 'visible', 'value' => 1)), 0);
251         // We need to execute the return values cleaning process to simulate the web service server.
252         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
254         $this->assertEquals(1, count($categories));
256         // Same query, but forcing a parameters clean.
257         $categories = core_course_external::get_categories(array(
258             array('key' => 'id', 'value' => "$category1->id"),
259             array('key' => 'idnumber', 'value' => $category1->idnumber),
260             array('key' => 'name', 'value' => $category1->name . "<br/>"),
261             array('key' => 'visible', 'value' => '1')), 0);
262         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
264         $this->assertEquals(1, count($categories));
266         // Retrieve categories from parent.
267         $categories = core_course_external::get_categories(array(
268             array('key' => 'parent', 'value' => $category3->id)), 1);
269         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
271         $this->assertEquals(2, count($categories));
273         // Retrieve all categories.
274         $categories = core_course_external::get_categories();
276         // We need to execute the return values cleaning process to simulate the web service server.
277         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
279         $this->assertEquals($DB->count_records('course_categories'), count($categories));
281         $this->unassignUserCapability('moodle/category:manage', $context->id, $roleid);
283         // Ensure maxdepthcategory is 2 and retrieve all categories without category:manage capability. It should retrieve all
284         // visible categories as well.
285         set_config('maxcategorydepth', 2);
286         $categories = core_course_external::get_categories();
288         // We need to execute the return values cleaning process to simulate the web service server.
289         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
291         $this->assertEquals($DB->count_records('course_categories', array('visible' => 1)), count($categories));
293         // Call without required capability (it will fail cause of the search on idnumber).
294         $this->expectException('moodle_exception');
295         $categories = core_course_external::get_categories(array(
296             array('key' => 'id', 'value' => $category1->id),
297             array('key' => 'idnumber', 'value' => $category1->idnumber),
298             array('key' => 'visible', 'value' => 1)), 0);
299     }
301     /**
302      * Test update_categories
303      */
304     public function test_update_categories() {
305         global $DB;
307         $this->resetAfterTest(true);
309         // Set the required capabilities by the external function
310         $contextid = context_system::instance()->id;
311         $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
313         // Create base categories.
314         $category1data['idnumber'] = 'idnumbercat1';
315         $category1data['name'] = 'Category 1 for PHPunit test';
316         $category1data['description'] = 'Category 1 description';
317         $category1data['descriptionformat'] = FORMAT_MOODLE;
318         $category1  = self::getDataGenerator()->create_category($category1data);
319         $category2  = self::getDataGenerator()->create_category(
320                 array('parent' => $category1->id));
321         $category3  = self::getDataGenerator()->create_category();
322         $category4  = self::getDataGenerator()->create_category(
323                 array('parent' => $category3->id));
324         $category5  = self::getDataGenerator()->create_category(
325                 array('parent' => $category4->id));
327         // We update all category1 attribut.
328         // Then we move cat4 and cat5 parent: cat3 => cat1
329         $categories = array(
330             array('id' => $category1->id,
331                 'name' => $category1->name . '_updated',
332                 'idnumber' => $category1->idnumber . '_updated',
333                 'description' => $category1->description . '_updated',
334                 'descriptionformat' => FORMAT_HTML,
335                 'theme' => $category1->theme),
336             array('id' => $category4->id, 'parent' => $category1->id));
338         core_course_external::update_categories($categories);
340         // Check the values were updated.
341         $dbcategories = $DB->get_records_select('course_categories',
342                 'id IN (' . $category1->id . ',' . $category2->id . ',' . $category2->id
343                 . ',' . $category3->id . ',' . $category4->id . ',' . $category5->id .')');
344         $this->assertEquals($category1->name . '_updated',
345                 $dbcategories[$category1->id]->name);
346         $this->assertEquals($category1->idnumber . '_updated',
347                 $dbcategories[$category1->id]->idnumber);
348         $this->assertEquals($category1->description . '_updated',
349                 $dbcategories[$category1->id]->description);
350         $this->assertEquals(FORMAT_HTML, $dbcategories[$category1->id]->descriptionformat);
352         // Check that category4 and category5 have been properly moved.
353         $this->assertEquals('/' . $category1->id . '/' . $category4->id,
354                 $dbcategories[$category4->id]->path);
355         $this->assertEquals('/' . $category1->id . '/' . $category4->id . '/' . $category5->id,
356                 $dbcategories[$category5->id]->path);
358         // Call without required capability.
359         $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
360         $this->expectException('required_capability_exception');
361         core_course_external::update_categories($categories);
362     }
364     /**
365      * Test create_courses
366      */
367     public function test_create_courses() {
368         global $DB;
370         $this->resetAfterTest(true);
372         // Enable course completion.
373         set_config('enablecompletion', 1);
374         // Enable course themes.
375         set_config('allowcoursethemes', 1);
377         // Set the required capabilities by the external function
378         $contextid = context_system::instance()->id;
379         $roleid = $this->assignUserCapability('moodle/course:create', $contextid);
380         $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
382         $category  = self::getDataGenerator()->create_category();
384         // Create base categories.
385         $course1['fullname'] = 'Test course 1';
386         $course1['shortname'] = 'Testcourse1';
387         $course1['categoryid'] = $category->id;
388         $course2['fullname'] = 'Test course 2';
389         $course2['shortname'] = 'Testcourse2';
390         $course2['categoryid'] = $category->id;
391         $course2['idnumber'] = 'testcourse2idnumber';
392         $course2['summary'] = 'Description for course 2';
393         $course2['summaryformat'] = FORMAT_MOODLE;
394         $course2['format'] = 'weeks';
395         $course2['showgrades'] = 1;
396         $course2['newsitems'] = 3;
397         $course2['startdate'] = 1420092000; // 01/01/2015.
398         $course2['enddate'] = 1422669600; // 01/31/2015.
399         $course2['numsections'] = 4;
400         $course2['maxbytes'] = 100000;
401         $course2['showreports'] = 1;
402         $course2['visible'] = 0;
403         $course2['hiddensections'] = 0;
404         $course2['groupmode'] = 0;
405         $course2['groupmodeforce'] = 0;
406         $course2['defaultgroupingid'] = 0;
407         $course2['enablecompletion'] = 1;
408         $course2['completionnotify'] = 1;
409         $course2['lang'] = 'en';
410         $course2['forcetheme'] = 'bootstrapbase';
411         $course3['fullname'] = 'Test course 3';
412         $course3['shortname'] = 'Testcourse3';
413         $course3['categoryid'] = $category->id;
414         $course3['format'] = 'topics';
415         $course3options = array('numsections' => 8,
416             'hiddensections' => 1,
417             'coursedisplay' => 1);
418         $course3['courseformatoptions'] = array();
419         foreach ($course3options as $key => $value) {
420             $course3['courseformatoptions'][] = array('name' => $key, 'value' => $value);
421         }
422         $courses = array($course1, $course2, $course3);
424         $createdcourses = core_course_external::create_courses($courses);
426         // We need to execute the return values cleaning process to simulate the web service server.
427         $createdcourses = external_api::clean_returnvalue(core_course_external::create_courses_returns(), $createdcourses);
429         // Check that right number of courses were created.
430         $this->assertEquals(3, count($createdcourses));
432         // Check that the courses were correctly created.
433         foreach ($createdcourses as $createdcourse) {
434             $courseinfo = course_get_format($createdcourse['id'])->get_course();
436             if ($createdcourse['shortname'] == $course2['shortname']) {
437                 $this->assertEquals($courseinfo->fullname, $course2['fullname']);
438                 $this->assertEquals($courseinfo->shortname, $course2['shortname']);
439                 $this->assertEquals($courseinfo->category, $course2['categoryid']);
440                 $this->assertEquals($courseinfo->idnumber, $course2['idnumber']);
441                 $this->assertEquals($courseinfo->summary, $course2['summary']);
442                 $this->assertEquals($courseinfo->summaryformat, $course2['summaryformat']);
443                 $this->assertEquals($courseinfo->format, $course2['format']);
444                 $this->assertEquals($courseinfo->showgrades, $course2['showgrades']);
445                 $this->assertEquals($courseinfo->newsitems, $course2['newsitems']);
446                 $this->assertEquals($courseinfo->startdate, $course2['startdate']);
447                 $this->assertEquals($courseinfo->enddate, $course2['enddate']);
448                 $this->assertEquals($courseinfo->numsections, $course2['numsections']);
449                 $this->assertEquals($courseinfo->maxbytes, $course2['maxbytes']);
450                 $this->assertEquals($courseinfo->showreports, $course2['showreports']);
451                 $this->assertEquals($courseinfo->visible, $course2['visible']);
452                 $this->assertEquals($courseinfo->hiddensections, $course2['hiddensections']);
453                 $this->assertEquals($courseinfo->groupmode, $course2['groupmode']);
454                 $this->assertEquals($courseinfo->groupmodeforce, $course2['groupmodeforce']);
455                 $this->assertEquals($courseinfo->defaultgroupingid, $course2['defaultgroupingid']);
456                 $this->assertEquals($courseinfo->completionnotify, $course2['completionnotify']);
457                 $this->assertEquals($courseinfo->lang, $course2['lang']);
458                 $this->assertEquals($courseinfo->theme, $course2['forcetheme']);
460                 // We enabled completion at the beginning of the test.
461                 $this->assertEquals($courseinfo->enablecompletion, $course2['enablecompletion']);
463             } else if ($createdcourse['shortname'] == $course1['shortname']) {
464                 $courseconfig = get_config('moodlecourse');
465                 $this->assertEquals($courseinfo->fullname, $course1['fullname']);
466                 $this->assertEquals($courseinfo->shortname, $course1['shortname']);
467                 $this->assertEquals($courseinfo->category, $course1['categoryid']);
468                 $this->assertEquals($courseinfo->summaryformat, FORMAT_HTML);
469                 $this->assertEquals($courseinfo->format, $courseconfig->format);
470                 $this->assertEquals($courseinfo->showgrades, $courseconfig->showgrades);
471                 $this->assertEquals($courseinfo->newsitems, $courseconfig->newsitems);
472                 $this->assertEquals($courseinfo->maxbytes, $courseconfig->maxbytes);
473                 $this->assertEquals($courseinfo->showreports, $courseconfig->showreports);
474                 $this->assertEquals($courseinfo->groupmode, $courseconfig->groupmode);
475                 $this->assertEquals($courseinfo->groupmodeforce, $courseconfig->groupmodeforce);
476                 $this->assertEquals($courseinfo->defaultgroupingid, 0);
477             } else if ($createdcourse['shortname'] == $course3['shortname']) {
478                 $this->assertEquals($courseinfo->fullname, $course3['fullname']);
479                 $this->assertEquals($courseinfo->shortname, $course3['shortname']);
480                 $this->assertEquals($courseinfo->category, $course3['categoryid']);
481                 $this->assertEquals($courseinfo->format, $course3['format']);
482                 $this->assertEquals($courseinfo->hiddensections, $course3options['hiddensections']);
483                 $this->assertEquals($courseinfo->numsections, $course3options['numsections']);
484                 $this->assertEquals($courseinfo->coursedisplay, $course3options['coursedisplay']);
485             } else {
486                 throw moodle_exception('Unexpected shortname');
487             }
488         }
490         // Call without required capability
491         $this->unassignUserCapability('moodle/course:create', $contextid, $roleid);
492         $this->expectException('required_capability_exception');
493         $createdsubcats = core_course_external::create_courses($courses);
494     }
496     /**
497      * Test delete_courses
498      */
499     public function test_delete_courses() {
500         global $DB, $USER;
502         $this->resetAfterTest(true);
504         // Admin can delete a course.
505         $this->setAdminUser();
506         // Validate_context() will fail as the email is not set by $this->setAdminUser().
507         $USER->email = 'emailtopass@example.com';
509         $course1  = self::getDataGenerator()->create_course();
510         $course2  = self::getDataGenerator()->create_course();
511         $course3  = self::getDataGenerator()->create_course();
513         // Delete courses.
514         $result = core_course_external::delete_courses(array($course1->id, $course2->id));
515         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
516         // Check for 0 warnings.
517         $this->assertEquals(0, count($result['warnings']));
519         // Check $course 1 and 2 are deleted.
520         $notdeletedcount = $DB->count_records_select('course',
521             'id IN ( ' . $course1->id . ',' . $course2->id . ')');
522         $this->assertEquals(0, $notdeletedcount);
524         // Try to delete non-existent course.
525         $result = core_course_external::delete_courses(array($course1->id));
526         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
527         // Check for 1 warnings.
528         $this->assertEquals(1, count($result['warnings']));
530         // Try to delete Frontpage course.
531         $result = core_course_external::delete_courses(array(0));
532         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
533         // Check for 1 warnings.
534         $this->assertEquals(1, count($result['warnings']));
536          // Fail when the user has access to course (enrolled) but does not have permission or is not admin.
537         $student1 = self::getDataGenerator()->create_user();
538         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
539         $this->getDataGenerator()->enrol_user($student1->id,
540                                               $course3->id,
541                                               $studentrole->id);
542         $this->setUser($student1);
543         $result = core_course_external::delete_courses(array($course3->id));
544         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
545         // Check for 1 warnings.
546         $this->assertEquals(1, count($result['warnings']));
548          // Fail when the user is not allow to access the course (enrolled) or is not admin.
549         $this->setGuestUser();
550         $this->expectException('require_login_exception');
552         $result = core_course_external::delete_courses(array($course3->id));
553         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
554     }
556     /**
557      * Test get_courses
558      */
559     public function test_get_courses () {
560         global $DB;
562         $this->resetAfterTest(true);
564         $generatedcourses = array();
565         $coursedata['idnumber'] = 'idnumbercourse1';
566         // Adding tags here to check that format_string is applied.
567         $coursedata['fullname'] = '<b>Course 1 for PHPunit test</b>';
568         $coursedata['shortname'] = '<b>Course 1 for PHPunit test</b>';
569         $coursedata['summary'] = 'Course 1 description';
570         $coursedata['summaryformat'] = FORMAT_MOODLE;
571         $course1  = self::getDataGenerator()->create_course($coursedata);
573         $generatedcourses[$course1->id] = $course1;
574         $course2  = self::getDataGenerator()->create_course();
575         $generatedcourses[$course2->id] = $course2;
576         $course3  = self::getDataGenerator()->create_course(array('format' => 'topics'));
577         $generatedcourses[$course3->id] = $course3;
579         // Set the required capabilities by the external function.
580         $context = context_system::instance();
581         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
582         $this->assignUserCapability('moodle/course:update',
583                 context_course::instance($course1->id)->id, $roleid);
584         $this->assignUserCapability('moodle/course:update',
585                 context_course::instance($course2->id)->id, $roleid);
586         $this->assignUserCapability('moodle/course:update',
587                 context_course::instance($course3->id)->id, $roleid);
589         $courses = core_course_external::get_courses(array('ids' =>
590             array($course1->id, $course2->id)));
592         // We need to execute the return values cleaning process to simulate the web service server.
593         $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
595         // Check we retrieve the good total number of categories.
596         $this->assertEquals(2, count($courses));
598         foreach ($courses as $course) {
599             $coursecontext = context_course::instance($course['id']);
600             $dbcourse = $generatedcourses[$course['id']];
601             $this->assertEquals($course['idnumber'], $dbcourse->idnumber);
602             $this->assertEquals($course['fullname'], external_format_string($dbcourse->fullname, $coursecontext->id));
603             $this->assertEquals($course['displayname'], external_format_string(get_course_display_name_for_list($dbcourse),
604                 $coursecontext->id));
605             // Summary was converted to the HTML format.
606             $this->assertEquals($course['summary'], format_text($dbcourse->summary, FORMAT_MOODLE, array('para' => false)));
607             $this->assertEquals($course['summaryformat'], FORMAT_HTML);
608             $this->assertEquals($course['shortname'], external_format_string($dbcourse->shortname, $coursecontext->id));
609             $this->assertEquals($course['categoryid'], $dbcourse->category);
610             $this->assertEquals($course['format'], $dbcourse->format);
611             $this->assertEquals($course['showgrades'], $dbcourse->showgrades);
612             $this->assertEquals($course['newsitems'], $dbcourse->newsitems);
613             $this->assertEquals($course['startdate'], $dbcourse->startdate);
614             $this->assertEquals($course['enddate'], $dbcourse->enddate);
615             $this->assertEquals($course['numsections'], $dbcourse->numsections);
616             $this->assertEquals($course['maxbytes'], $dbcourse->maxbytes);
617             $this->assertEquals($course['showreports'], $dbcourse->showreports);
618             $this->assertEquals($course['visible'], $dbcourse->visible);
619             $this->assertEquals($course['hiddensections'], $dbcourse->hiddensections);
620             $this->assertEquals($course['groupmode'], $dbcourse->groupmode);
621             $this->assertEquals($course['groupmodeforce'], $dbcourse->groupmodeforce);
622             $this->assertEquals($course['defaultgroupingid'], $dbcourse->defaultgroupingid);
623             $this->assertEquals($course['completionnotify'], $dbcourse->completionnotify);
624             $this->assertEquals($course['lang'], $dbcourse->lang);
625             $this->assertEquals($course['forcetheme'], $dbcourse->theme);
626             $this->assertEquals($course['enablecompletion'], $dbcourse->enablecompletion);
627             if ($dbcourse->format === 'topics') {
628                 $this->assertEquals($course['courseformatoptions'], array(
629                     array('name' => 'numsections', 'value' => $dbcourse->numsections),
630                     array('name' => 'hiddensections', 'value' => $dbcourse->hiddensections),
631                     array('name' => 'coursedisplay', 'value' => $dbcourse->coursedisplay),
632                 ));
633             }
634         }
636         // Get all courses in the DB
637         $courses = core_course_external::get_courses(array());
639         // We need to execute the return values cleaning process to simulate the web service server.
640         $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
642         $this->assertEquals($DB->count_records('course'), count($courses));
643     }
645     /**
646      * Test search_courses
647      */
648     public function test_search_courses () {
650         global $DB;
652         $this->resetAfterTest(true);
653         $this->setAdminUser();
654         $generatedcourses = array();
655         $coursedata1['fullname'] = 'FIRST COURSE';
656         $course1  = self::getDataGenerator()->create_course($coursedata1);
658         $page = new moodle_page();
659         $page->set_course($course1);
660         $page->blocks->add_blocks([BLOCK_POS_LEFT => ['news_items'], BLOCK_POS_RIGHT => []], 'course-view-*');
662         $coursedata2['fullname'] = 'SECOND COURSE';
663         $course2  = self::getDataGenerator()->create_course($coursedata2);
665         $page = new moodle_page();
666         $page->set_course($course2);
667         $page->blocks->add_blocks([BLOCK_POS_LEFT => ['news_items'], BLOCK_POS_RIGHT => []], 'course-view-*');
668         // Search by name.
669         $results = core_course_external::search_courses('search', 'FIRST');
670         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
671         $this->assertEquals($coursedata1['fullname'], $results['courses'][0]['fullname']);
672         $this->assertCount(1, $results['courses']);
674         // Create the forum.
675         $record = new stdClass();
676         $record->introformat = FORMAT_HTML;
677         $record->course = $course2->id;
678         // Set Aggregate type = Average of ratings.
679         $forum = self::getDataGenerator()->create_module('forum', $record);
681         // Search by module.
682         $results = core_course_external::search_courses('modulelist', 'forum');
683         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
684         $this->assertEquals(1, $results['total']);
686         // Enable coursetag option.
687         set_config('block_tags_showcoursetags', true);
688         // Add tag 'TAG-LABEL ON SECOND COURSE' to Course2.
689         core_tag_tag::set_item_tags('core', 'course', $course2->id, context_course::instance($course2->id),
690                 array('TAG-LABEL ON SECOND COURSE'));
691         $taginstance = $DB->get_record('tag_instance',
692                 array('itemtype' => 'course', 'itemid' => $course2->id), '*', MUST_EXIST);
693         // Search by tagid.
694         $results = core_course_external::search_courses('tagid', $taginstance->tagid);
695         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
696         $this->assertEquals($coursedata2['fullname'], $results['courses'][0]['fullname']);
698         // Search by block (use news_items default block).
699         $blockid = $DB->get_field('block', 'id', array('name' => 'news_items'));
700         $results = core_course_external::search_courses('blocklist', $blockid);
701         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
702         $this->assertEquals(2, $results['total']);
704         // Now as a normal user.
705         $user = self::getDataGenerator()->create_user();
707         // Add a 3rd, hidden, course we shouldn't see, even when enrolled as student.
708         $coursedata3['fullname'] = 'HIDDEN COURSE';
709         $coursedata3['visible'] = 0;
710         $course3  = self::getDataGenerator()->create_course($coursedata3);
711         $this->getDataGenerator()->enrol_user($user->id, $course3->id, 'student');
713         $this->getDataGenerator()->enrol_user($user->id, $course2->id, 'student');
714         $this->setUser($user);
716         $results = core_course_external::search_courses('search', 'FIRST');
717         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
718         $this->assertCount(1, $results['courses']);
719         $this->assertEquals(1, $results['total']);
720         $this->assertEquals($coursedata1['fullname'], $results['courses'][0]['fullname']);
722         // Check that we can see both without the limit to enrolled setting.
723         $results = core_course_external::search_courses('search', 'COURSE', 0, 0, array(), 0);
724         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
725         $this->assertCount(2, $results['courses']);
726         $this->assertEquals(2, $results['total']);
728         // Check that we only see our enrolled course when limiting.
729         $results = core_course_external::search_courses('search', 'COURSE', 0, 0, array(), 1);
730         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
731         $this->assertCount(1, $results['courses']);
732         $this->assertEquals(1, $results['total']);
733         $this->assertEquals($coursedata2['fullname'], $results['courses'][0]['fullname']);
735         // Search by block (use news_items default block). Should fail (only admins allowed).
736         $this->expectException('required_capability_exception');
737         $results = core_course_external::search_courses('blocklist', $blockid);
739     }
741     /**
742      * Create a course with contents
743      * @return array A list with the course object and course modules objects
744      */
745     private function prepare_get_course_contents_test() {
746         global $DB;
747         $course  = self::getDataGenerator()->create_course();
748         $forumdescription = 'This is the forum description';
749         $forum = $this->getDataGenerator()->create_module('forum',
750             array('course' => $course->id, 'intro' => $forumdescription),
751             array('showdescription' => true));
752         $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
753         $data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
754         $datacm = get_coursemodule_from_instance('page', $data->id);
755         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
756         $pagecm = get_coursemodule_from_instance('page', $page->id);
757         $labeldescription = 'This is a very long label to test if more than 50 characters are returned.
758                 So bla bla bla bla <b>bold bold bold</b> bla bla bla bla.';
759         $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id,
760             'intro' => $labeldescription));
761         $labelcm = get_coursemodule_from_instance('label', $label->id);
762         $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id,
763             'name' => 'URL: % & $ ../', 'section' => 2));
764         $urlcm = get_coursemodule_from_instance('url', $url->id);
766         // Set the required capabilities by the external function.
767         $context = context_course::instance($course->id);
768         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
769         $this->assignUserCapability('moodle/course:update', $context->id, $roleid);
770         $this->assignUserCapability('mod/data:view', $context->id, $roleid);
772         $conditions = array('course' => $course->id, 'section' => 2);
773         $DB->set_field('course_sections', 'summary', 'Text with iframe <iframe src="https://moodle.org"></iframe>', $conditions);
774         rebuild_course_cache($course->id, true);
776         return array($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm);
777     }
779     /**
780      * Test get_course_contents
781      */
782     public function test_get_course_contents() {
783         $this->resetAfterTest(true);
785         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
787         $sections = core_course_external::get_course_contents($course->id, array());
788         // We need to execute the return values cleaning process to simulate the web service server.
789         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
791         // Check that forum and label descriptions are correctly returned.
792         $firstsection = array_shift($sections);
793         $lastsection = array_pop($sections);
795         $modinfo = get_fast_modinfo($course);
796         $testexecuted = 0;
797         foreach ($firstsection['modules'] as $module) {
798             if ($module['id'] == $forumcm->id and $module['modname'] == 'forum') {
799                 $cm = $modinfo->cms[$forumcm->id];
800                 $formattedtext = format_text($cm->content, FORMAT_HTML,
801                     array('noclean' => true, 'para' => false, 'filter' => false));
802                 $this->assertEquals($formattedtext, $module['description']);
803                 $this->assertEquals($forumcm->instance, $module['instance']);
804                 $testexecuted = $testexecuted + 1;
805             } else if ($module['id'] == $labelcm->id and $module['modname'] == 'label') {
806                 $cm = $modinfo->cms[$labelcm->id];
807                 $formattedtext = format_text($cm->content, FORMAT_HTML,
808                     array('noclean' => true, 'para' => false, 'filter' => false));
809                 $this->assertEquals($formattedtext, $module['description']);
810                 $this->assertEquals($labelcm->instance, $module['instance']);
811                 $testexecuted = $testexecuted + 1;
812             }
813         }
814         $this->assertEquals(2, $testexecuted);
815         $this->assertEquals(0, $firstsection['section']);
817         // Check that the only return section has the 5 created modules.
818         $this->assertCount(4, $firstsection['modules']);
819         $this->assertCount(1, $lastsection['modules']);
820         $this->assertEquals(2, $lastsection['section']);
821         $this->assertContains('<iframe', $lastsection['summary']);
822         $this->assertContains('</iframe>', $lastsection['summary']);
824         try {
825             $sections = core_course_external::get_course_contents($course->id,
826                                                                     array(array("name" => "invalid", "value" => 1)));
827             $this->fail('Exception expected due to invalid option.');
828         } catch (moodle_exception $e) {
829             $this->assertEquals('errorinvalidparam', $e->errorcode);
830         }
831     }
834     /**
835      * Test get_course_contents excluding modules
836      */
837     public function test_get_course_contents_excluding_modules() {
838         $this->resetAfterTest(true);
840         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
842         // Test exclude modules.
843         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludemodules", "value" => 1)));
845         // We need to execute the return values cleaning process to simulate the web service server.
846         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
848         $firstsection = array_shift($sections);
849         $lastsection = array_pop($sections);
851         $this->assertEmpty($firstsection['modules']);
852         $this->assertEmpty($lastsection['modules']);
853     }
855     /**
856      * Test get_course_contents excluding contents
857      */
858     public function test_get_course_contents_excluding_contents() {
859         $this->resetAfterTest(true);
861         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
863         // Test exclude modules.
864         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludecontents", "value" => 1)));
866         // We need to execute the return values cleaning process to simulate the web service server.
867         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
869         foreach ($sections as $section) {
870             foreach ($section['modules'] as $module) {
871                 // Only resources return contents.
872                 if (isset($module['contents'])) {
873                     $this->assertEmpty($module['contents']);
874                 }
875             }
876         }
877     }
879     /**
880      * Test get_course_contents filtering by section number
881      */
882     public function test_get_course_contents_section_number() {
883         $this->resetAfterTest(true);
885         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
887         // Test exclude modules.
888         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "sectionnumber", "value" => 0)));
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(1, $sections);
894         $this->assertCount(4, $sections[0]['modules']);
895     }
897     /**
898      * Test get_course_contents filtering by cmid
899      */
900     public function test_get_course_contents_cmid() {
901         $this->resetAfterTest(true);
903         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
905         // Test exclude modules.
906         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "cmid", "value" => $forumcm->id)));
908         // We need to execute the return values cleaning process to simulate the web service server.
909         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
911         $this->assertCount(2, $sections);
912         $this->assertCount(1, $sections[0]['modules']);
913         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
914     }
917     /**
918      * Test get_course_contents filtering by cmid and section
919      */
920     public function test_get_course_contents_section_cmid() {
921         $this->resetAfterTest(true);
923         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
925         // Test exclude modules.
926         $sections = core_course_external::get_course_contents($course->id, array(
927                                                                         array("name" => "cmid", "value" => $forumcm->id),
928                                                                         array("name" => "sectionnumber", "value" => 0)
929                                                                         ));
931         // We need to execute the return values cleaning process to simulate the web service server.
932         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
934         $this->assertCount(1, $sections);
935         $this->assertCount(1, $sections[0]['modules']);
936         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
937     }
939     /**
940      * Test get_course_contents filtering by modname
941      */
942     public function test_get_course_contents_modname() {
943         $this->resetAfterTest(true);
945         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
947         // Test exclude modules.
948         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "modname", "value" => "forum")));
950         // We need to execute the return values cleaning process to simulate the web service server.
951         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
953         $this->assertCount(2, $sections);
954         $this->assertCount(1, $sections[0]['modules']);
955         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
956     }
958     /**
959      * Test get_course_contents filtering by modname
960      */
961     public function test_get_course_contents_modid() {
962         $this->resetAfterTest(true);
964         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
966         // Test exclude modules.
967         $sections = core_course_external::get_course_contents($course->id, array(
968                                                                             array("name" => "modname", "value" => "page"),
969                                                                             array("name" => "modid", "value" => $pagecm->instance),
970                                                                             ));
972         // We need to execute the return values cleaning process to simulate the web service server.
973         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
975         $this->assertCount(2, $sections);
976         $this->assertCount(1, $sections[0]['modules']);
977         $this->assertEquals("page", $sections[0]['modules'][0]["modname"]);
978         $this->assertEquals($pagecm->instance, $sections[0]['modules'][0]["instance"]);
979     }
981     /**
982      * Test duplicate_course
983      */
984     public function test_duplicate_course() {
985         $this->resetAfterTest(true);
987         // Create one course with three modules.
988         $course  = self::getDataGenerator()->create_course();
989         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
990         $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
991         $forumcontext = context_module::instance($forum->cmid);
992         $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
993         $datacontext = context_module::instance($data->cmid);
994         $datacm = get_coursemodule_from_instance('page', $data->id);
995         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
996         $pagecontext = context_module::instance($page->cmid);
997         $pagecm = get_coursemodule_from_instance('page', $page->id);
999         // Set the required capabilities by the external function.
1000         $coursecontext = context_course::instance($course->id);
1001         $categorycontext = context_coursecat::instance($course->category);
1002         $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id);
1003         $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid);
1004         $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid);
1005         $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid);
1006         $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid);
1007         // Optional capabilities to copy user data.
1008         $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid);
1009         $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid);
1011         $newcourse['fullname'] = 'Course duplicate';
1012         $newcourse['shortname'] = 'courseduplicate';
1013         $newcourse['categoryid'] = $course->category;
1014         $newcourse['visible'] = true;
1015         $newcourse['options'][] = array('name' => 'users', 'value' => true);
1017         $duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'],
1018                 $newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']);
1020         // We need to execute the return values cleaning process to simulate the web service server.
1021         $duplicate = external_api::clean_returnvalue(core_course_external::duplicate_course_returns(), $duplicate);
1023         // Check that the course has been duplicated.
1024         $this->assertEquals($newcourse['shortname'], $duplicate['shortname']);
1025     }
1027     /**
1028      * Test update_courses
1029      */
1030     public function test_update_courses() {
1031         global $DB, $CFG, $USER, $COURSE;
1033         // Get current $COURSE to be able to restore it later (defaults to $SITE). We need this
1034         // trick because we are both updating and getting (for testing) course information
1035         // in the same request and core_course_external::update_courses()
1036         // is overwriting $COURSE all over the time with OLD values, so later
1037         // use of get_course() fetches those OLD values instead of the updated ones.
1038         // See MDL-39723 for more info.
1039         $origcourse = clone($COURSE);
1041         $this->resetAfterTest(true);
1043         // Set the required capabilities by the external function.
1044         $contextid = context_system::instance()->id;
1045         $roleid = $this->assignUserCapability('moodle/course:update', $contextid);
1046         $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1047         $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1048         $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1049         $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1050         $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1051         $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
1052         $this->assignUserCapability('moodle/course:viewhiddencourses', $contextid, $roleid);
1054         // Create category and course.
1055         $category1  = self::getDataGenerator()->create_category();
1056         $category2  = self::getDataGenerator()->create_category();
1057         $originalcourse1 = self::getDataGenerator()->create_course();
1058         self::getDataGenerator()->enrol_user($USER->id, $originalcourse1->id, $roleid);
1059         $originalcourse2 = self::getDataGenerator()->create_course();
1060         self::getDataGenerator()->enrol_user($USER->id, $originalcourse2->id, $roleid);
1062         // Course values to be updated.
1063         $course1['id'] = $originalcourse1->id;
1064         $course1['fullname'] = 'Updated test course 1';
1065         $course1['shortname'] = 'Udestedtestcourse1';
1066         $course1['categoryid'] = $category1->id;
1067         $course2['id'] = $originalcourse2->id;
1068         $course2['fullname'] = 'Updated test course 2';
1069         $course2['shortname'] = 'Updestedtestcourse2';
1070         $course2['categoryid'] = $category2->id;
1071         $course2['idnumber'] = 'Updatedidnumber2';
1072         $course2['summary'] = 'Updaated description for course 2';
1073         $course2['summaryformat'] = FORMAT_HTML;
1074         $course2['format'] = 'topics';
1075         $course2['showgrades'] = 1;
1076         $course2['newsitems'] = 3;
1077         $course2['startdate'] = 1420092000; // 01/01/2015.
1078         $course2['enddate'] = 1422669600; // 01/31/2015.
1079         $course2['numsections'] = 4;
1080         $course2['maxbytes'] = 100000;
1081         $course2['showreports'] = 1;
1082         $course2['visible'] = 0;
1083         $course2['hiddensections'] = 0;
1084         $course2['groupmode'] = 0;
1085         $course2['groupmodeforce'] = 0;
1086         $course2['defaultgroupingid'] = 0;
1087         $course2['enablecompletion'] = 1;
1088         $course2['lang'] = 'en';
1089         $course2['forcetheme'] = 'bootstrapbase';
1090         $courses = array($course1, $course2);
1092         $updatedcoursewarnings = core_course_external::update_courses($courses);
1093         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1094                                                                     $updatedcoursewarnings);
1095         $COURSE = $origcourse; // Restore $COURSE. Instead of using the OLD one set by the previous line.
1097         // Check that right number of courses were created.
1098         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1100         // Check that the courses were correctly created.
1101         foreach ($courses as $course) {
1102             $courseinfo = course_get_format($course['id'])->get_course();
1103             if ($course['id'] == $course2['id']) {
1104                 $this->assertEquals($course2['fullname'], $courseinfo->fullname);
1105                 $this->assertEquals($course2['shortname'], $courseinfo->shortname);
1106                 $this->assertEquals($course2['categoryid'], $courseinfo->category);
1107                 $this->assertEquals($course2['idnumber'], $courseinfo->idnumber);
1108                 $this->assertEquals($course2['summary'], $courseinfo->summary);
1109                 $this->assertEquals($course2['summaryformat'], $courseinfo->summaryformat);
1110                 $this->assertEquals($course2['format'], $courseinfo->format);
1111                 $this->assertEquals($course2['showgrades'], $courseinfo->showgrades);
1112                 $this->assertEquals($course2['newsitems'], $courseinfo->newsitems);
1113                 $this->assertEquals($course2['startdate'], $courseinfo->startdate);
1114                 $this->assertEquals($course2['enddate'], $courseinfo->enddate);
1115                 $this->assertEquals($course2['numsections'], $courseinfo->numsections);
1116                 $this->assertEquals($course2['maxbytes'], $courseinfo->maxbytes);
1117                 $this->assertEquals($course2['showreports'], $courseinfo->showreports);
1118                 $this->assertEquals($course2['visible'], $courseinfo->visible);
1119                 $this->assertEquals($course2['hiddensections'], $courseinfo->hiddensections);
1120                 $this->assertEquals($course2['groupmode'], $courseinfo->groupmode);
1121                 $this->assertEquals($course2['groupmodeforce'], $courseinfo->groupmodeforce);
1122                 $this->assertEquals($course2['defaultgroupingid'], $courseinfo->defaultgroupingid);
1123                 $this->assertEquals($course2['lang'], $courseinfo->lang);
1125                 if (!empty($CFG->allowcoursethemes)) {
1126                     $this->assertEquals($course2['forcetheme'], $courseinfo->theme);
1127                 }
1129                 $this->assertEquals($course2['enablecompletion'], $courseinfo->enablecompletion);
1130             } else if ($course['id'] == $course1['id']) {
1131                 $this->assertEquals($course1['fullname'], $courseinfo->fullname);
1132                 $this->assertEquals($course1['shortname'], $courseinfo->shortname);
1133                 $this->assertEquals($course1['categoryid'], $courseinfo->category);
1134                 $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
1135                 $this->assertEquals('topics', $courseinfo->format);
1136                 $this->assertEquals(5, $courseinfo->numsections);
1137                 $this->assertEquals(0, $courseinfo->newsitems);
1138                 $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
1139             } else {
1140                 throw moodle_exception('Unexpected shortname');
1141             }
1142         }
1144         $courses = array($course1);
1145         // Try update course without update capability.
1146         $user = self::getDataGenerator()->create_user();
1147         $this->setUser($user);
1148         $this->unassignUserCapability('moodle/course:update', $contextid, $roleid);
1149         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1150         $updatedcoursewarnings = core_course_external::update_courses($courses);
1151         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1152                                                                     $updatedcoursewarnings);
1153         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1155         // Try update course category without capability.
1156         $this->assignUserCapability('moodle/course:update', $contextid, $roleid);
1157         $this->unassignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1158         $user = self::getDataGenerator()->create_user();
1159         $this->setUser($user);
1160         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1161         $course1['categoryid'] = $category2->id;
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 fullname without capability.
1169         $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1170         $this->unassignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1171         $user = self::getDataGenerator()->create_user();
1172         $this->setUser($user);
1173         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1174         $updatedcoursewarnings = core_course_external::update_courses($courses);
1175         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1176                                                                     $updatedcoursewarnings);
1177         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1178         $course1['fullname'] = 'Testing fullname without permission';
1179         $courses = array($course1);
1180         $updatedcoursewarnings = core_course_external::update_courses($courses);
1181         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1182                                                                     $updatedcoursewarnings);
1183         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1185         // Try update course shortname without capability.
1186         $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1187         $this->unassignUserCapability('moodle/course:changeshortname', $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['shortname'] = 'Testing shortname without permission';
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 idnumber without capability.
1203         $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1204         $this->unassignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1205         $user = self::getDataGenerator()->create_user();
1206         $this->setUser($user);
1207         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1208         $updatedcoursewarnings = core_course_external::update_courses($courses);
1209         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1210                                                                     $updatedcoursewarnings);
1211         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1212         $course1['idnumber'] = 'NEWIDNUMBER';
1213         $courses = array($course1);
1214         $updatedcoursewarnings = core_course_external::update_courses($courses);
1215         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1216                                                                     $updatedcoursewarnings);
1217         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1219         // Try update course summary without capability.
1220         $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1221         $this->unassignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1222         $user = self::getDataGenerator()->create_user();
1223         $this->setUser($user);
1224         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1225         $updatedcoursewarnings = core_course_external::update_courses($courses);
1226         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1227                                                                     $updatedcoursewarnings);
1228         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1229         $course1['summary'] = 'New summary';
1230         $courses = array($course1);
1231         $updatedcoursewarnings = core_course_external::update_courses($courses);
1232         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1233                                                                     $updatedcoursewarnings);
1234         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1236         // Try update course with invalid summary format.
1237         $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1238         $user = self::getDataGenerator()->create_user();
1239         $this->setUser($user);
1240         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1241         $updatedcoursewarnings = core_course_external::update_courses($courses);
1242         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1243                                                                     $updatedcoursewarnings);
1244         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1245         $course1['summaryformat'] = 10;
1246         $courses = array($course1);
1247         $updatedcoursewarnings = core_course_external::update_courses($courses);
1248         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1249                                                                     $updatedcoursewarnings);
1250         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1252         // Try update course visibility without capability.
1253         $this->unassignUserCapability('moodle/course:visibility', $contextid, $roleid);
1254         $user = self::getDataGenerator()->create_user();
1255         $this->setUser($user);
1256         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1257         $course1['summaryformat'] = FORMAT_MOODLE;
1258         $courses = array($course1);
1259         $updatedcoursewarnings = core_course_external::update_courses($courses);
1260         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1261                                                                     $updatedcoursewarnings);
1262         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1263         $course1['visible'] = 0;
1264         $courses = array($course1);
1265         $updatedcoursewarnings = core_course_external::update_courses($courses);
1266         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1267                                                                     $updatedcoursewarnings);
1268         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1269     }
1271     /**
1272      * Test delete course_module.
1273      */
1274     public function test_delete_modules() {
1275         global $DB;
1277         // Ensure we reset the data after this test.
1278         $this->resetAfterTest(true);
1280         // Create a user.
1281         $user = self::getDataGenerator()->create_user();
1283         // Set the tests to run as the user.
1284         self::setUser($user);
1286         // Create a course to add the modules.
1287         $course = self::getDataGenerator()->create_course();
1289         // Create two test modules.
1290         $record = new stdClass();
1291         $record->course = $course->id;
1292         $module1 = self::getDataGenerator()->create_module('forum', $record);
1293         $module2 = self::getDataGenerator()->create_module('assign', $record);
1295         // Check the forum was correctly created.
1296         $this->assertEquals(1, $DB->count_records('forum', array('id' => $module1->id)));
1298         // Check the assignment was correctly created.
1299         $this->assertEquals(1, $DB->count_records('assign', array('id' => $module2->id)));
1301         // Check data exists in the course modules table.
1302         $this->assertEquals(2, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
1303                 array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
1305         // Enrol the user in the course.
1306         $enrol = enrol_get_plugin('manual');
1307         $enrolinstances = enrol_get_instances($course->id, true);
1308         foreach ($enrolinstances as $courseenrolinstance) {
1309             if ($courseenrolinstance->enrol == "manual") {
1310                 $instance = $courseenrolinstance;
1311                 break;
1312             }
1313         }
1314         $enrol->enrol_user($instance, $user->id);
1316         // Assign capabilities to delete module 1.
1317         $modcontext = context_module::instance($module1->cmid);
1318         $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id);
1320         // Assign capabilities to delete module 2.
1321         $modcontext = context_module::instance($module2->cmid);
1322         $newrole = create_role('Role 2', 'role2', 'Role 2 description');
1323         $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id, $newrole);
1325         // Deleting these module instances.
1326         core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1328         // Check the forum was deleted.
1329         $this->assertEquals(0, $DB->count_records('forum', array('id' => $module1->id)));
1331         // Check the assignment was deleted.
1332         $this->assertEquals(0, $DB->count_records('assign', array('id' => $module2->id)));
1334         // Check we retrieve no data in the course modules table.
1335         $this->assertEquals(0, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
1336                 array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
1338         // Call with non-existent course module id and ensure exception thrown.
1339         try {
1340             core_course_external::delete_modules(array('1337'));
1341             $this->fail('Exception expected due to missing course module.');
1342         } catch (dml_missing_record_exception $e) {
1343             $this->assertEquals('invalidcoursemodule', $e->errorcode);
1344         }
1346         // Create two modules.
1347         $module1 = self::getDataGenerator()->create_module('forum', $record);
1348         $module2 = self::getDataGenerator()->create_module('assign', $record);
1350         // Since these modules were recreated the user will not have capabilities
1351         // to delete them, ensure exception is thrown if they try.
1352         try {
1353             core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1354             $this->fail('Exception expected due to missing capability.');
1355         } catch (moodle_exception $e) {
1356             $this->assertEquals('nopermissions', $e->errorcode);
1357         }
1359         // Unenrol user from the course.
1360         $enrol->unenrol_user($instance, $user->id);
1362         // Try and delete modules from the course the user was unenrolled in, make sure exception thrown.
1363         try {
1364             core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1365             $this->fail('Exception expected due to being unenrolled from the course.');
1366         } catch (moodle_exception $e) {
1367             $this->assertEquals('requireloginerror', $e->errorcode);
1368         }
1369     }
1371     /**
1372      * Test import_course into an empty course
1373      */
1374     public function test_import_course_empty() {
1375         global $USER;
1377         $this->resetAfterTest(true);
1379         $course1  = self::getDataGenerator()->create_course();
1380         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course1->id, 'name' => 'Forum test'));
1381         $page = $this->getDataGenerator()->create_module('page', array('course' => $course1->id, 'name' => 'Page test'));
1383         $course2  = self::getDataGenerator()->create_course();
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->assertEmpty($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.
1398         core_course_external::import_course($course1->id, $course2->id, 0);
1400         // Verify that now we have two modules in both courses.
1401         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1402         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1403         $this->assertCount(2, $course1cms);
1404         $this->assertCount(2, $course2cms);
1406         // Verify that the names transfered across correctly.
1407         foreach ($course2cms as $cm) {
1408             if ($cm->modname === 'page') {
1409                 $this->assertEquals($cm->name, $page->name);
1410             } else if ($cm->modname === 'forum') {
1411                 $this->assertEquals($cm->name, $forum->name);
1412             } else {
1413                 $this->fail('Unknown CM found.');
1414             }
1415         }
1416     }
1418     /**
1419      * Test import_course into an filled course
1420      */
1421     public function test_import_course_filled() {
1422         global $USER;
1424         $this->resetAfterTest(true);
1426         // Add forum and page to course1.
1427         $course1  = self::getDataGenerator()->create_course();
1428         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1429         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
1431         // Add quiz to course 2.
1432         $course2  = self::getDataGenerator()->create_course();
1433         $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
1435         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1436         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1438         // Verify the state of the courses before we do the import.
1439         $this->assertCount(2, $course1cms);
1440         $this->assertCount(1, $course2cms);
1442         // Setup the user to run the operation (ugly hack because validate_context() will
1443         // fail as the email is not set by $this->setAdminUser()).
1444         $this->setAdminUser();
1445         $USER->email = 'emailtopass@example.com';
1447         // Import from course1 to course2 without deleting content.
1448         core_course_external::import_course($course1->id, $course2->id, 0);
1450         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1452         // Verify that now we have three modules in course2.
1453         $this->assertCount(3, $course2cms);
1455         // Verify that the names transfered across correctly.
1456         foreach ($course2cms as $cm) {
1457             if ($cm->modname === 'page') {
1458                 $this->assertEquals($cm->name, $page->name);
1459             } else if ($cm->modname === 'forum') {
1460                 $this->assertEquals($cm->name, $forum->name);
1461             } else if ($cm->modname === 'quiz') {
1462                 $this->assertEquals($cm->name, $quiz->name);
1463             } else {
1464                 $this->fail('Unknown CM found.');
1465             }
1466         }
1467     }
1469     /**
1470      * Test import_course with only blocks set to backup
1471      */
1472     public function test_import_course_blocksonly() {
1473         global $USER, $DB;
1475         $this->resetAfterTest(true);
1477         // Add forum and page to course1.
1478         $course1  = self::getDataGenerator()->create_course();
1479         $course1ctx = context_course::instance($course1->id);
1480         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1481         $block = $this->getDataGenerator()->create_block('online_users', array('parentcontextid' => $course1ctx->id));
1483         $course2  = self::getDataGenerator()->create_course();
1484         $course2ctx = context_course::instance($course2->id);
1485         $initialblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
1486         $initialcmcount = count(get_fast_modinfo($course2->id)->get_cms());
1488         // Setup the user to run the operation (ugly hack because validate_context() will
1489         // fail as the email is not set by $this->setAdminUser()).
1490         $this->setAdminUser();
1491         $USER->email = 'emailtopass@example.com';
1493         // Import from course1 to course2 without deleting content, but excluding
1494         // activities.
1495         $options = array(
1496             array('name' => 'activities', 'value' => 0),
1497             array('name' => 'blocks', 'value' => 1),
1498             array('name' => 'filters', 'value' => 0),
1499         );
1501         core_course_external::import_course($course1->id, $course2->id, 0, $options);
1503         $newcmcount = count(get_fast_modinfo($course2->id)->get_cms());
1504         $newblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
1505         // Check that course modules haven't changed, but that blocks have.
1506         $this->assertEquals($initialcmcount, $newcmcount);
1507         $this->assertEquals(($initialblockcount + 1), $newblockcount);
1508     }
1510     /**
1511      * Test import_course into an filled course, deleting content.
1512      */
1513     public function test_import_course_deletecontent() {
1514         global $USER;
1515         $this->resetAfterTest(true);
1517         // Add forum and page to course1.
1518         $course1  = self::getDataGenerator()->create_course();
1519         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1520         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
1522         // Add quiz to course 2.
1523         $course2  = self::getDataGenerator()->create_course();
1524         $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
1526         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1527         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1529         // Verify the state of the courses before we do the import.
1530         $this->assertCount(2, $course1cms);
1531         $this->assertCount(1, $course2cms);
1533         // Setup the user to run the operation (ugly hack because validate_context() will
1534         // fail as the email is not set by $this->setAdminUser()).
1535         $this->setAdminUser();
1536         $USER->email = 'emailtopass@example.com';
1538         // Import from course1 to course2,  deleting content.
1539         core_course_external::import_course($course1->id, $course2->id, 1);
1541         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1543         // Verify that now we have two modules in course2.
1544         $this->assertCount(2, $course2cms);
1546         // Verify that the course only contains the imported modules.
1547         foreach ($course2cms as $cm) {
1548             if ($cm->modname === 'page') {
1549                 $this->assertEquals($cm->name, $page->name);
1550             } else if ($cm->modname === 'forum') {
1551                 $this->assertEquals($cm->name, $forum->name);
1552             } else {
1553                 $this->fail('Unknown CM found: '.$cm->name);
1554             }
1555         }
1556     }
1558     /**
1559      * Ensure import_course handles incorrect deletecontent option correctly.
1560      */
1561     public function test_import_course_invalid_deletecontent_option() {
1562         $this->resetAfterTest(true);
1564         $course1  = self::getDataGenerator()->create_course();
1565         $course2  = self::getDataGenerator()->create_course();
1567         $this->expectException('moodle_exception');
1568         $this->expectExceptionMessage(get_string('invalidextparam', 'webservice', -1));
1569         // Import from course1 to course2, with invalid option
1570         core_course_external::import_course($course1->id, $course2->id, -1);;
1571     }
1573     /**
1574      * Test view_course function
1575      */
1576     public function test_view_course() {
1578         $this->resetAfterTest();
1580         // Course without sections.
1581         $course = $this->getDataGenerator()->create_course(array('numsections' => 5), array('createsections' => true));
1582         $this->setAdminUser();
1584         // Redirect events to the sink, so we can recover them later.
1585         $sink = $this->redirectEvents();
1587         $result = core_course_external::view_course($course->id, 1);
1588         $result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result);
1589         $events = $sink->get_events();
1590         $event = reset($events);
1592         // Check the event details are correct.
1593         $this->assertInstanceOf('\core\event\course_viewed', $event);
1594         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1595         $this->assertEquals(1, $event->other['coursesectionnumber']);
1597         $result = core_course_external::view_course($course->id);
1598         $result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result);
1599         $events = $sink->get_events();
1600         $event = array_pop($events);
1601         $sink->close();
1603         // Check the event details are correct.
1604         $this->assertInstanceOf('\core\event\course_viewed', $event);
1605         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1606         $this->assertEmpty($event->other);
1608     }
1610     /**
1611      * Test get_course_module
1612      */
1613     public function test_get_course_module() {
1614         global $DB;
1616         $this->resetAfterTest(true);
1618         $this->setAdminUser();
1619         $course = self::getDataGenerator()->create_course();
1620         $record = array(
1621             'course' => $course->id,
1622             'name' => 'First Assignment'
1623         );
1624         $options = array(
1625             'idnumber' => 'ABC',
1626             'visible' => 0
1627         );
1628         // Hidden activity.
1629         $assign = self::getDataGenerator()->create_module('assign', $record, $options);
1631         $outcomescale = 'Distinction, Very Good, Good, Pass, Fail';
1633         // Insert a custom grade scale to be used by an outcome.
1634         $gradescale = new grade_scale();
1635         $gradescale->name        = 'gettcoursemodulescale';
1636         $gradescale->courseid    = $course->id;
1637         $gradescale->userid      = 0;
1638         $gradescale->scale       = $outcomescale;
1639         $gradescale->description = 'This scale is used to mark standard assignments.';
1640         $gradescale->insert();
1642         // Insert an outcome.
1643         $data = new stdClass();
1644         $data->courseid = $course->id;
1645         $data->fullname = 'Team work';
1646         $data->shortname = 'Team work';
1647         $data->scaleid = $gradescale->id;
1648         $outcome = new grade_outcome($data, false);
1649         $outcome->insert();
1651         $outcomegradeitem = new grade_item();
1652         $outcomegradeitem->itemname = $outcome->shortname;
1653         $outcomegradeitem->itemtype = 'mod';
1654         $outcomegradeitem->itemmodule = 'assign';
1655         $outcomegradeitem->iteminstance = $assign->id;
1656         $outcomegradeitem->outcomeid = $outcome->id;
1657         $outcomegradeitem->cmid = 0;
1658         $outcomegradeitem->courseid = $course->id;
1659         $outcomegradeitem->aggregationcoef = 0;
1660         $outcomegradeitem->itemnumber = 1; // The activity's original grade item will be 0.
1661         $outcomegradeitem->gradetype = GRADE_TYPE_SCALE;
1662         $outcomegradeitem->scaleid = $outcome->scaleid;
1663         $outcomegradeitem->insert();
1665         $assignmentgradeitem = grade_item::fetch(
1666             array(
1667                 'itemtype' => 'mod',
1668                 'itemmodule' => 'assign',
1669                 'iteminstance' => $assign->id,
1670                 'itemnumber' => 0,
1671                 'courseid' => $course->id
1672             )
1673         );
1674         $outcomegradeitem->set_parent($assignmentgradeitem->categoryid);
1675         $outcomegradeitem->move_after_sortorder($assignmentgradeitem->sortorder);
1677         // Test admin user can see the complete hidden activity.
1678         $result = core_course_external::get_course_module($assign->cmid);
1679         $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result);
1681         $this->assertCount(0, $result['warnings']);
1682         // Test we retrieve all the fields.
1683         $this->assertCount(27, $result['cm']);
1684         $this->assertEquals($record['name'], $result['cm']['name']);
1685         $this->assertEquals($options['idnumber'], $result['cm']['idnumber']);
1686         $this->assertEquals(100, $result['cm']['grade']);
1687         $this->assertEquals(0.0, $result['cm']['gradepass']);
1688         $this->assertEquals('submissions', $result['cm']['advancedgrading'][0]['area']);
1689         $this->assertEmpty($result['cm']['advancedgrading'][0]['method']);
1690         $this->assertEquals($outcomescale, $result['cm']['outcomes'][0]['scale']);
1692         $student = $this->getDataGenerator()->create_user();
1693         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1695         self::getDataGenerator()->enrol_user($student->id,  $course->id, $studentrole->id);
1696         $this->setUser($student);
1698         // The user shouldn't be able to see the activity.
1699         try {
1700             core_course_external::get_course_module($assign->cmid);
1701             $this->fail('Exception expected due to invalid permissions.');
1702         } catch (moodle_exception $e) {
1703             $this->assertEquals('requireloginerror', $e->errorcode);
1704         }
1706         // Make module visible.
1707         set_coursemodule_visible($assign->cmid, 1);
1709         // Test student user.
1710         $result = core_course_external::get_course_module($assign->cmid);
1711         $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result);
1713         $this->assertCount(0, $result['warnings']);
1714         // Test we retrieve only the few files we can see.
1715         $this->assertCount(11, $result['cm']);
1716         $this->assertEquals($assign->cmid, $result['cm']['id']);
1717         $this->assertEquals($course->id, $result['cm']['course']);
1718         $this->assertEquals('assign', $result['cm']['modname']);
1719         $this->assertEquals($assign->id, $result['cm']['instance']);
1721     }
1723     /**
1724      * Test get_course_module_by_instance
1725      */
1726     public function test_get_course_module_by_instance() {
1727         global $DB;
1729         $this->resetAfterTest(true);
1731         $this->setAdminUser();
1732         $course = self::getDataGenerator()->create_course();
1733         $record = array(
1734             'course' => $course->id,
1735             'name' => 'First Chat'
1736         );
1737         $options = array(
1738             'idnumber' => 'ABC',
1739             'visible' => 0
1740         );
1741         // Hidden activity.
1742         $chat = self::getDataGenerator()->create_module('chat', $record, $options);
1744         // Test admin user can see the complete hidden activity.
1745         $result = core_course_external::get_course_module_by_instance('chat', $chat->id);
1746         $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result);
1748         $this->assertCount(0, $result['warnings']);
1749         // Test we retrieve all the fields.
1750         $this->assertCount(22, $result['cm']);
1751         $this->assertEquals($record['name'], $result['cm']['name']);
1752         $this->assertEquals($options['idnumber'], $result['cm']['idnumber']);
1754         $student = $this->getDataGenerator()->create_user();
1755         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1757         self::getDataGenerator()->enrol_user($student->id,  $course->id, $studentrole->id);
1758         $this->setUser($student);
1760         // The user shouldn't be able to see the activity.
1761         try {
1762             core_course_external::get_course_module_by_instance('chat', $chat->id);
1763             $this->fail('Exception expected due to invalid permissions.');
1764         } catch (moodle_exception $e) {
1765             $this->assertEquals('requireloginerror', $e->errorcode);
1766         }
1768         // Make module visible.
1769         set_coursemodule_visible($chat->cmid, 1);
1771         // Test student user.
1772         $result = core_course_external::get_course_module_by_instance('chat', $chat->id);
1773         $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result);
1775         $this->assertCount(0, $result['warnings']);
1776         // Test we retrieve only the few files we can see.
1777         $this->assertCount(11, $result['cm']);
1778         $this->assertEquals($chat->cmid, $result['cm']['id']);
1779         $this->assertEquals($course->id, $result['cm']['course']);
1780         $this->assertEquals('chat', $result['cm']['modname']);
1781         $this->assertEquals($chat->id, $result['cm']['instance']);
1783         // Try with an invalid module name.
1784         try {
1785             core_course_external::get_course_module_by_instance('abc', $chat->id);
1786             $this->fail('Exception expected due to invalid module name.');
1787         } catch (dml_read_exception $e) {
1788             $this->assertEquals('dmlreadexception', $e->errorcode);
1789         }
1791     }
1793     /**
1794      * Test get_activities_overview
1795      */
1796     public function test_get_activities_overview() {
1797         global $USER;
1799         $this->resetAfterTest();
1800         $course1 = self::getDataGenerator()->create_course();
1801         $course2 = self::getDataGenerator()->create_course();
1803         // Create a viewer user.
1804         $viewer = self::getDataGenerator()->create_user((object) array('trackforums' => 1));
1805         $this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
1806         $this->getDataGenerator()->enrol_user($viewer->id, $course2->id);
1808         // Create two forums - one in each course.
1809         $record = new stdClass();
1810         $record->course = $course1->id;
1811         $forum1 = self::getDataGenerator()->create_module('forum', (object) array('course' => $course1->id));
1812         $forum2 = self::getDataGenerator()->create_module('forum', (object) array('course' => $course2->id));
1814         $this->setAdminUser();
1815         // A standard post in the forum.
1816         $record = new stdClass();
1817         $record->course = $course1->id;
1818         $record->userid = $USER->id;
1819         $record->forum = $forum1->id;
1820         $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1822         $this->setUser($viewer->id);
1823         $courses = array($course1->id , $course2->id);
1825         $result = core_course_external::get_activities_overview($courses);
1826         $result = external_api::clean_returnvalue(core_course_external::get_activities_overview_returns(), $result);
1828         // There should be one entry for course1, and no others.
1829         $this->assertCount(1, $result['courses']);
1830         $this->assertEquals($course1->id, $result['courses'][0]['id']);
1831         // Check expected overview data for the module.
1832         $this->assertEquals('forum', $result['courses'][0]['overviews'][0]['module']);
1833         $this->assertContains('1 total unread', $result['courses'][0]['overviews'][0]['overviewtext']);
1834     }
1836     /**
1837      * Test get_user_navigation_options
1838      */
1839     public function test_get_user_navigation_options() {
1840         global $USER;
1842         $this->resetAfterTest();
1843         $course1 = self::getDataGenerator()->create_course();
1844         $course2 = self::getDataGenerator()->create_course();
1846         // Create a viewer user.
1847         $viewer = self::getDataGenerator()->create_user();
1848         $this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
1849         $this->getDataGenerator()->enrol_user($viewer->id, $course2->id);
1851         $this->setUser($viewer->id);
1852         $courses = array($course1->id , $course2->id, SITEID);
1854         $result = core_course_external::get_user_navigation_options($courses);
1855         $result = external_api::clean_returnvalue(core_course_external::get_user_navigation_options_returns(), $result);
1857         $this->assertCount(0, $result['warnings']);
1858         $this->assertCount(3, $result['courses']);
1860         foreach ($result['courses'] as $course) {
1861             $navoptions = new stdClass;
1862             foreach ($course['options'] as $option) {
1863                 $navoptions->{$option['name']} = $option['available'];
1864             }
1865             $this->assertCount(9, $course['options']);
1866             if ($course['id'] == SITEID) {
1867                 $this->assertTrue($navoptions->blogs);
1868                 $this->assertFalse($navoptions->notes);
1869                 $this->assertFalse($navoptions->participants);
1870                 $this->assertTrue($navoptions->badges);
1871                 $this->assertTrue($navoptions->tags);
1872                 $this->assertFalse($navoptions->grades);
1873                 $this->assertFalse($navoptions->search);
1874                 $this->assertTrue($navoptions->calendar);
1875                 $this->assertTrue($navoptions->competencies);
1876             } else {
1877                 $this->assertTrue($navoptions->blogs);
1878                 $this->assertFalse($navoptions->notes);
1879                 $this->assertTrue($navoptions->participants);
1880                 $this->assertTrue($navoptions->badges);
1881                 $this->assertFalse($navoptions->tags);
1882                 $this->assertTrue($navoptions->grades);
1883                 $this->assertFalse($navoptions->search);
1884                 $this->assertFalse($navoptions->calendar);
1885                 $this->assertTrue($navoptions->competencies);
1886             }
1887         }
1888     }
1890     /**
1891      * Test get_user_administration_options
1892      */
1893     public function test_get_user_administration_options() {
1894         global $USER;
1896         $this->resetAfterTest();
1897         $course1 = self::getDataGenerator()->create_course();
1898         $course2 = self::getDataGenerator()->create_course();
1900         // Create a viewer user.
1901         $viewer = self::getDataGenerator()->create_user();
1902         $this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
1903         $this->getDataGenerator()->enrol_user($viewer->id, $course2->id);
1905         $this->setUser($viewer->id);
1906         $courses = array($course1->id , $course2->id, SITEID);
1908         $result = core_course_external::get_user_administration_options($courses);
1909         $result = external_api::clean_returnvalue(core_course_external::get_user_administration_options_returns(), $result);
1911         $this->assertCount(0, $result['warnings']);
1912         $this->assertCount(3, $result['courses']);
1914         foreach ($result['courses'] as $course) {
1915             $adminoptions = new stdClass;
1916             foreach ($course['options'] as $option) {
1917                 $adminoptions->{$option['name']} = $option['available'];
1918             }
1919             if ($course['id'] == SITEID) {
1920                 $this->assertCount(15, $course['options']);
1921                 $this->assertFalse($adminoptions->update);
1922                 $this->assertFalse($adminoptions->filters);
1923                 $this->assertFalse($adminoptions->reports);
1924                 $this->assertFalse($adminoptions->backup);
1925                 $this->assertFalse($adminoptions->restore);
1926                 $this->assertFalse($adminoptions->files);
1927                 $this->assertFalse(!isset($adminoptions->tags));
1928                 $this->assertFalse($adminoptions->gradebook);
1929                 $this->assertFalse($adminoptions->outcomes);
1930                 $this->assertFalse($adminoptions->badges);
1931                 $this->assertFalse($adminoptions->import);
1932                 $this->assertFalse($adminoptions->publish);
1933                 $this->assertFalse($adminoptions->reset);
1934                 $this->assertFalse($adminoptions->roles);
1935             } else {
1936                 $this->assertCount(14, $course['options']);
1937                 $this->assertFalse($adminoptions->update);
1938                 $this->assertFalse($adminoptions->filters);
1939                 $this->assertFalse($adminoptions->reports);
1940                 $this->assertFalse($adminoptions->backup);
1941                 $this->assertFalse($adminoptions->restore);
1942                 $this->assertFalse($adminoptions->files);
1943                 $this->assertFalse($adminoptions->tags);
1944                 $this->assertFalse($adminoptions->gradebook);
1945                 $this->assertFalse($adminoptions->outcomes);
1946                 $this->assertTrue($adminoptions->badges);
1947                 $this->assertFalse($adminoptions->import);
1948                 $this->assertFalse($adminoptions->publish);
1949                 $this->assertFalse($adminoptions->reset);
1950                 $this->assertFalse($adminoptions->roles);
1951             }
1952         }
1953     }
1955     /**
1956      * Test get_courses_by_fields
1957      */
1958     public function test_get_courses_by_field() {
1959         global $DB;
1960         $this->resetAfterTest(true);
1962         $category1 = self::getDataGenerator()->create_category();
1963         $category2 = self::getDataGenerator()->create_category(array('parent' => $category1->id));
1964         $course1 = self::getDataGenerator()->create_course(array('category' => $category1->id, 'shortname' => 'c1'));
1965         $course2 = self::getDataGenerator()->create_course(array('visible' => 0, 'category' => $category2->id, 'idnumber' => 'i2'));
1967         $student1 = self::getDataGenerator()->create_user();
1968         $user1 = self::getDataGenerator()->create_user();
1969         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1970         self::getDataGenerator()->enrol_user($student1->id, $course1->id, $studentrole->id);
1971         self::getDataGenerator()->enrol_user($student1->id, $course2->id, $studentrole->id);
1973         self::setAdminUser();
1974         // As admins, we should be able to retrieve everything.
1975         $result = core_course_external::get_courses_by_field();
1976         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
1977         $this->assertCount(3, $result['courses']);
1978         // Expect to receive all the fields.
1979         $this->assertCount(36, $result['courses'][0]);
1980         $this->assertCount(36, $result['courses'][1]);
1981         $this->assertCount(36, $result['courses'][2]);
1983         $result = core_course_external::get_courses_by_field('id', $course1->id);
1984         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
1985         $this->assertCount(1, $result['courses']);
1986         $this->assertEquals($course1->id, $result['courses'][0]['id']);
1987         // Expect to receive all the fields.
1988         $this->assertCount(36, $result['courses'][0]);
1990         $result = core_course_external::get_courses_by_field('id', $course2->id);
1991         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
1992         $this->assertCount(1, $result['courses']);
1993         $this->assertEquals($course2->id, $result['courses'][0]['id']);
1995         $result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id");
1996         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
1997         $this->assertCount(2, $result['courses']);
1999         // Check default filters.
2000         $this->assertCount(3, $result['courses'][0]['filters']);
2001         $this->assertCount(3, $result['courses'][1]['filters']);
2003         $result = core_course_external::get_courses_by_field('category', $category1->id);
2004         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2005         $this->assertCount(1, $result['courses']);
2006         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2008         $result = core_course_external::get_courses_by_field('shortname', 'c1');
2009         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2010         $this->assertCount(1, $result['courses']);
2011         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2013         $result = core_course_external::get_courses_by_field('idnumber', 'i2');
2014         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2015         $this->assertCount(1, $result['courses']);
2016         $this->assertEquals($course2->id, $result['courses'][0]['id']);
2018         $result = core_course_external::get_courses_by_field('idnumber', 'x');
2019         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2020         $this->assertCount(0, $result['courses']);
2022         // Change filter value.
2023         filter_set_local_state('mediaplugin', context_course::instance($course1->id)->id, TEXTFILTER_OFF);
2025         self::setUser($student1);
2026         // All visible courses  (including front page) for normal student.
2027         $result = core_course_external::get_courses_by_field();
2028         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2029         $this->assertCount(2, $result['courses']);
2030         $this->assertCount(29, $result['courses'][0]);
2031         $this->assertCount(29, $result['courses'][1]);
2033         $result = core_course_external::get_courses_by_field('id', $course1->id);
2034         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2035         $this->assertCount(1, $result['courses']);
2036         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2037         // Expect to receive all the files that a student can see.
2038         $this->assertCount(29, $result['courses'][0]);
2040         // Check default filters.
2041         $filters = $result['courses'][0]['filters'];
2042         $this->assertCount(3, $filters);
2043         $found = false;
2044         foreach ($filters as $filter) {
2045             if ($filter['filter'] == 'mediaplugin' and $filter['localstate'] == TEXTFILTER_OFF) {
2046                 $found = true;
2047             }
2048         }
2049         $this->assertTrue($found);
2051         // Course 2 is not visible.
2052         $result = core_course_external::get_courses_by_field('id', $course2->id);
2053         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2054         $this->assertCount(0, $result['courses']);
2056         $result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id");
2057         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2058         $this->assertCount(1, $result['courses']);
2060         $result = core_course_external::get_courses_by_field('category', $category1->id);
2061         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2062         $this->assertCount(1, $result['courses']);
2063         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2065         $result = core_course_external::get_courses_by_field('shortname', 'c1');
2066         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2067         $this->assertCount(1, $result['courses']);
2068         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2070         $result = core_course_external::get_courses_by_field('idnumber', 'i2');
2071         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2072         $this->assertCount(0, $result['courses']);
2074         $result = core_course_external::get_courses_by_field('idnumber', 'x');
2075         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2076         $this->assertCount(0, $result['courses']);
2078         self::setUser($user1);
2079         // All visible courses (including front page) for authenticated user.
2080         $result = core_course_external::get_courses_by_field();
2081         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2082         $this->assertCount(2, $result['courses']);
2083         $this->assertCount(29, $result['courses'][0]);  // Site course.
2084         $this->assertCount(12, $result['courses'][1]);  // Only public information, not enrolled.
2086         $result = core_course_external::get_courses_by_field('id', $course1->id);
2087         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2088         $this->assertCount(1, $result['courses']);
2089         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2090         // Expect to receive all the files that a authenticated can see.
2091         $this->assertCount(12, $result['courses'][0]);
2093         // Course 2 is not visible.
2094         $result = core_course_external::get_courses_by_field('id', $course2->id);
2095         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2096         $this->assertCount(0, $result['courses']);
2098         $result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id");
2099         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2100         $this->assertCount(1, $result['courses']);
2102         $result = core_course_external::get_courses_by_field('category', $category1->id);
2103         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2104         $this->assertCount(1, $result['courses']);
2105         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2107         $result = core_course_external::get_courses_by_field('shortname', 'c1');
2108         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2109         $this->assertCount(1, $result['courses']);
2110         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2112         $result = core_course_external::get_courses_by_field('idnumber', 'i2');
2113         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2114         $this->assertCount(0, $result['courses']);
2116         $result = core_course_external::get_courses_by_field('idnumber', 'x');
2117         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2118         $this->assertCount(0, $result['courses']);
2119     }
2121     public function test_get_courses_by_field_invalid_field() {
2122         $this->expectException('invalid_parameter_exception');
2123         $result = core_course_external::get_courses_by_field('zyx', 'x');
2124     }
2126     public function test_get_courses_by_field_invalid_courses() {
2127         $result = core_course_external::get_courses_by_field('id', '-1');
2128         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2129         $this->assertCount(0, $result['courses']);
2130     }
2132     public function test_check_updates() {
2133         global $DB;
2134         $this->resetAfterTest(true);
2135         $this->setAdminUser();
2137         // Create different types of activities.
2138         $course  = self::getDataGenerator()->create_course();
2139         $tocreate = array('assign', 'book', 'choice', 'folder', 'forum', 'glossary', 'imscp', 'label', 'lti', 'page', 'quiz',
2140                             'resource', 'scorm', 'survey', 'url', 'wiki');
2142         $modules = array();
2143         foreach ($tocreate as $modname) {
2144             $modules[$modname]['instance'] = $this->getDataGenerator()->create_module($modname, array('course' => $course->id));
2145             $modules[$modname]['cm'] = get_coursemodule_from_id(false, $modules[$modname]['instance']->cmid);
2146             $modules[$modname]['context'] = context_module::instance($modules[$modname]['instance']->cmid);
2147         }
2149         $student = self::getDataGenerator()->create_user();
2150         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
2151         self::getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
2152         $this->setUser($student);
2154         $since = time();
2155         $this->waitForSecond();
2156         $params = array();
2157         foreach ($modules as $modname => $data) {
2158             $params[$data['cm']->id] = array(
2159                 'contextlevel' => 'module',
2160                 'id' => $data['cm']->id,
2161                 'since' => $since
2162             );
2163         }
2165         // Check there is nothing updated because modules are fresh new.
2166         $result = core_course_external::check_updates($course->id, $params);
2167         $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result);
2168         $this->assertCount(0, $result['instances']);
2169         $this->assertCount(0, $result['warnings']);
2171         // Test with get_updates_since the same data.
2172         $result = core_course_external::get_updates_since($course->id, $since);
2173         $result = external_api::clean_returnvalue(core_course_external::get_updates_since_returns(), $result);
2174         $this->assertCount(0, $result['instances']);
2175         $this->assertCount(0, $result['warnings']);
2177         // Update a module after a second.
2178         $this->waitForSecond();
2179         set_coursemodule_name($modules['forum']['cm']->id, 'New forum name');
2181         $found = false;
2182         $result = core_course_external::check_updates($course->id, $params);
2183         $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result);
2184         $this->assertCount(1, $result['instances']);
2185         $this->assertCount(0, $result['warnings']);
2186         foreach ($result['instances'] as $module) {
2187             foreach ($module['updates'] as $update) {
2188                 if ($module['id'] == $modules['forum']['cm']->id and $update['name'] == 'configuration') {
2189                     $found = true;
2190                 }
2191             }
2192         }
2193         $this->assertTrue($found);
2195         // Test with get_updates_since the same data.
2196         $result = core_course_external::get_updates_since($course->id, $since);
2197         $result = external_api::clean_returnvalue(core_course_external::get_updates_since_returns(), $result);
2198         $this->assertCount(1, $result['instances']);
2199         $this->assertCount(0, $result['warnings']);
2200         $found = false;
2201         $this->assertCount(1, $result['instances']);
2202         $this->assertCount(0, $result['warnings']);
2203         foreach ($result['instances'] as $module) {
2204             foreach ($module['updates'] as $update) {
2205                 if ($module['id'] == $modules['forum']['cm']->id and $update['name'] == 'configuration') {
2206                     $found = true;
2207                 }
2208             }
2209         }
2210         $this->assertTrue($found);
2212         // Do not retrieve the configuration field.
2213         $filter = array('files');
2214         $found = false;
2215         $result = core_course_external::check_updates($course->id, $params, $filter);
2216         $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result);
2217         $this->assertCount(0, $result['instances']);
2218         $this->assertCount(0, $result['warnings']);
2219         $this->assertFalse($found);
2221         // Add invalid cmid.
2222         $params[] = array(
2223             'contextlevel' => 'module',
2224             'id' => -2,
2225             'since' => $since
2226         );
2227         $result = core_course_external::check_updates($course->id, $params);
2228         $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result);
2229         $this->assertCount(1, $result['warnings']);
2230         $this->assertEquals(-2, $result['warnings'][0]['itemid']);
2231     }