MDL-58659 course: Add missing enddate field in get_courses_by_field
[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(course_get_format($createdcourse['id'])->get_last_section_number(), $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(course_get_format($createdcourse['id'])->get_last_section_number(),
484                     $course3options['numsections']);
485                 $this->assertEquals($courseinfo->coursedisplay, $course3options['coursedisplay']);
486             } else {
487                 throw new moodle_exception('Unexpected shortname');
488             }
489         }
491         // Call without required capability
492         $this->unassignUserCapability('moodle/course:create', $contextid, $roleid);
493         $this->expectException('required_capability_exception');
494         $createdsubcats = core_course_external::create_courses($courses);
495     }
497     /**
498      * Test delete_courses
499      */
500     public function test_delete_courses() {
501         global $DB, $USER;
503         $this->resetAfterTest(true);
505         // Admin can delete a course.
506         $this->setAdminUser();
507         // Validate_context() will fail as the email is not set by $this->setAdminUser().
508         $USER->email = 'emailtopass@example.com';
510         $course1  = self::getDataGenerator()->create_course();
511         $course2  = self::getDataGenerator()->create_course();
512         $course3  = self::getDataGenerator()->create_course();
514         // Delete courses.
515         $result = core_course_external::delete_courses(array($course1->id, $course2->id));
516         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
517         // Check for 0 warnings.
518         $this->assertEquals(0, count($result['warnings']));
520         // Check $course 1 and 2 are deleted.
521         $notdeletedcount = $DB->count_records_select('course',
522             'id IN ( ' . $course1->id . ',' . $course2->id . ')');
523         $this->assertEquals(0, $notdeletedcount);
525         // Try to delete non-existent course.
526         $result = core_course_external::delete_courses(array($course1->id));
527         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
528         // Check for 1 warnings.
529         $this->assertEquals(1, count($result['warnings']));
531         // Try to delete Frontpage course.
532         $result = core_course_external::delete_courses(array(0));
533         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
534         // Check for 1 warnings.
535         $this->assertEquals(1, count($result['warnings']));
537          // Fail when the user has access to course (enrolled) but does not have permission or is not admin.
538         $student1 = self::getDataGenerator()->create_user();
539         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
540         $this->getDataGenerator()->enrol_user($student1->id,
541                                               $course3->id,
542                                               $studentrole->id);
543         $this->setUser($student1);
544         $result = core_course_external::delete_courses(array($course3->id));
545         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
546         // Check for 1 warnings.
547         $this->assertEquals(1, count($result['warnings']));
549          // Fail when the user is not allow to access the course (enrolled) or is not admin.
550         $this->setGuestUser();
551         $this->expectException('require_login_exception');
553         $result = core_course_external::delete_courses(array($course3->id));
554         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
555     }
557     /**
558      * Test get_courses
559      */
560     public function test_get_courses () {
561         global $DB;
563         $this->resetAfterTest(true);
565         $generatedcourses = array();
566         $coursedata['idnumber'] = 'idnumbercourse1';
567         // Adding tags here to check that format_string is applied.
568         $coursedata['fullname'] = '<b>Course 1 for PHPunit test</b>';
569         $coursedata['shortname'] = '<b>Course 1 for PHPunit test</b>';
570         $coursedata['summary'] = 'Course 1 description';
571         $coursedata['summaryformat'] = FORMAT_MOODLE;
572         $course1  = self::getDataGenerator()->create_course($coursedata);
574         $generatedcourses[$course1->id] = $course1;
575         $course2  = self::getDataGenerator()->create_course();
576         $generatedcourses[$course2->id] = $course2;
577         $course3  = self::getDataGenerator()->create_course(array('format' => 'topics'));
578         $generatedcourses[$course3->id] = $course3;
580         // Set the required capabilities by the external function.
581         $context = context_system::instance();
582         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
583         $this->assignUserCapability('moodle/course:update',
584                 context_course::instance($course1->id)->id, $roleid);
585         $this->assignUserCapability('moodle/course:update',
586                 context_course::instance($course2->id)->id, $roleid);
587         $this->assignUserCapability('moodle/course:update',
588                 context_course::instance($course3->id)->id, $roleid);
590         $courses = core_course_external::get_courses(array('ids' =>
591             array($course1->id, $course2->id)));
593         // We need to execute the return values cleaning process to simulate the web service server.
594         $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
596         // Check we retrieve the good total number of categories.
597         $this->assertEquals(2, count($courses));
599         foreach ($courses as $course) {
600             $coursecontext = context_course::instance($course['id']);
601             $dbcourse = $generatedcourses[$course['id']];
602             $this->assertEquals($course['idnumber'], $dbcourse->idnumber);
603             $this->assertEquals($course['fullname'], external_format_string($dbcourse->fullname, $coursecontext->id));
604             $this->assertEquals($course['displayname'], external_format_string(get_course_display_name_for_list($dbcourse),
605                 $coursecontext->id));
606             // Summary was converted to the HTML format.
607             $this->assertEquals($course['summary'], format_text($dbcourse->summary, FORMAT_MOODLE, array('para' => false)));
608             $this->assertEquals($course['summaryformat'], FORMAT_HTML);
609             $this->assertEquals($course['shortname'], external_format_string($dbcourse->shortname, $coursecontext->id));
610             $this->assertEquals($course['categoryid'], $dbcourse->category);
611             $this->assertEquals($course['format'], $dbcourse->format);
612             $this->assertEquals($course['showgrades'], $dbcourse->showgrades);
613             $this->assertEquals($course['newsitems'], $dbcourse->newsitems);
614             $this->assertEquals($course['startdate'], $dbcourse->startdate);
615             $this->assertEquals($course['enddate'], $dbcourse->enddate);
616             $this->assertEquals($course['numsections'], course_get_format($dbcourse)->get_last_section_number());
617             $this->assertEquals($course['maxbytes'], $dbcourse->maxbytes);
618             $this->assertEquals($course['showreports'], $dbcourse->showreports);
619             $this->assertEquals($course['visible'], $dbcourse->visible);
620             $this->assertEquals($course['hiddensections'], $dbcourse->hiddensections);
621             $this->assertEquals($course['groupmode'], $dbcourse->groupmode);
622             $this->assertEquals($course['groupmodeforce'], $dbcourse->groupmodeforce);
623             $this->assertEquals($course['defaultgroupingid'], $dbcourse->defaultgroupingid);
624             $this->assertEquals($course['completionnotify'], $dbcourse->completionnotify);
625             $this->assertEquals($course['lang'], $dbcourse->lang);
626             $this->assertEquals($course['forcetheme'], $dbcourse->theme);
627             $this->assertEquals($course['enablecompletion'], $dbcourse->enablecompletion);
628             if ($dbcourse->format === 'topics') {
629                 $this->assertEquals($course['courseformatoptions'], array(
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 get_courses without capability
647      */
648     public function test_get_courses_without_capability() {
649         $this->resetAfterTest(true);
651         $course1 = $this->getDataGenerator()->create_course();
652         $this->setUser($this->getDataGenerator()->create_user());
654         // No permissions are required to get the site course.
655         $courses = core_course_external::get_courses(array('ids' => [SITEID]));
656         $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
658         $this->assertEquals(1, count($courses));
659         $this->assertEquals('PHPUnit test site', $courses[0]['fullname']);
660         $this->assertEquals('site', $courses[0]['format']);
662         // Requesting course without being enrolled or capability to view it will throw an exception.
663         try {
664             core_course_external::get_courses(array('ids' => [$course1->id]));
665             $this->fail('Exception expected');
666         } catch (moodle_exception $e) {
667             $this->assertEquals(1, preg_match('/Course or activity not accessible. \(Not enrolled\)/', $e->getMessage()));
668         }
669     }
671     /**
672      * Test search_courses
673      */
674     public function test_search_courses () {
676         global $DB;
678         $this->resetAfterTest(true);
679         $this->setAdminUser();
680         $generatedcourses = array();
681         $coursedata1['fullname'] = 'FIRST COURSE';
682         $course1  = self::getDataGenerator()->create_course($coursedata1);
684         $page = new moodle_page();
685         $page->set_course($course1);
686         $page->blocks->add_blocks([BLOCK_POS_LEFT => ['news_items'], BLOCK_POS_RIGHT => []], 'course-view-*');
688         $coursedata2['fullname'] = 'SECOND COURSE';
689         $course2  = self::getDataGenerator()->create_course($coursedata2);
691         $page = new moodle_page();
692         $page->set_course($course2);
693         $page->blocks->add_blocks([BLOCK_POS_LEFT => ['news_items'], BLOCK_POS_RIGHT => []], 'course-view-*');
694         // Search by name.
695         $results = core_course_external::search_courses('search', 'FIRST');
696         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
697         $this->assertEquals($coursedata1['fullname'], $results['courses'][0]['fullname']);
698         $this->assertCount(1, $results['courses']);
700         // Create the forum.
701         $record = new stdClass();
702         $record->introformat = FORMAT_HTML;
703         $record->course = $course2->id;
704         // Set Aggregate type = Average of ratings.
705         $forum = self::getDataGenerator()->create_module('forum', $record);
707         // Search by module.
708         $results = core_course_external::search_courses('modulelist', 'forum');
709         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
710         $this->assertEquals(1, $results['total']);
712         // Enable coursetag option.
713         set_config('block_tags_showcoursetags', true);
714         // Add tag 'TAG-LABEL ON SECOND COURSE' to Course2.
715         core_tag_tag::set_item_tags('core', 'course', $course2->id, context_course::instance($course2->id),
716                 array('TAG-LABEL ON SECOND COURSE'));
717         $taginstance = $DB->get_record('tag_instance',
718                 array('itemtype' => 'course', 'itemid' => $course2->id), '*', MUST_EXIST);
719         // Search by tagid.
720         $results = core_course_external::search_courses('tagid', $taginstance->tagid);
721         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
722         $this->assertEquals($coursedata2['fullname'], $results['courses'][0]['fullname']);
724         // Search by block (use news_items default block).
725         $blockid = $DB->get_field('block', 'id', array('name' => 'news_items'));
726         $results = core_course_external::search_courses('blocklist', $blockid);
727         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
728         $this->assertEquals(2, $results['total']);
730         // Now as a normal user.
731         $user = self::getDataGenerator()->create_user();
733         // Add a 3rd, hidden, course we shouldn't see, even when enrolled as student.
734         $coursedata3['fullname'] = 'HIDDEN COURSE';
735         $coursedata3['visible'] = 0;
736         $course3  = self::getDataGenerator()->create_course($coursedata3);
737         $this->getDataGenerator()->enrol_user($user->id, $course3->id, 'student');
739         $this->getDataGenerator()->enrol_user($user->id, $course2->id, 'student');
740         $this->setUser($user);
742         $results = core_course_external::search_courses('search', 'FIRST');
743         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
744         $this->assertCount(1, $results['courses']);
745         $this->assertEquals(1, $results['total']);
746         $this->assertEquals($coursedata1['fullname'], $results['courses'][0]['fullname']);
748         // Check that we can see both without the limit to enrolled setting.
749         $results = core_course_external::search_courses('search', 'COURSE', 0, 0, array(), 0);
750         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
751         $this->assertCount(2, $results['courses']);
752         $this->assertEquals(2, $results['total']);
754         // Check that we only see our enrolled course when limiting.
755         $results = core_course_external::search_courses('search', 'COURSE', 0, 0, array(), 1);
756         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
757         $this->assertCount(1, $results['courses']);
758         $this->assertEquals(1, $results['total']);
759         $this->assertEquals($coursedata2['fullname'], $results['courses'][0]['fullname']);
761         // Search by block (use news_items default block). Should fail (only admins allowed).
762         $this->expectException('required_capability_exception');
763         $results = core_course_external::search_courses('blocklist', $blockid);
765     }
767     /**
768      * Create a course with contents
769      * @return array A list with the course object and course modules objects
770      */
771     private function prepare_get_course_contents_test() {
772         global $DB;
773         $course  = self::getDataGenerator()->create_course(['numsections' => 2]);
774         $forumdescription = 'This is the forum description';
775         $forum = $this->getDataGenerator()->create_module('forum',
776             array('course' => $course->id, 'intro' => $forumdescription),
777             array('showdescription' => true));
778         $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
779         $data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
780         $datacm = get_coursemodule_from_instance('page', $data->id);
781         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
782         $pagecm = get_coursemodule_from_instance('page', $page->id);
783         $labeldescription = 'This is a very long label to test if more than 50 characters are returned.
784                 So bla bla bla bla <b>bold bold bold</b> bla bla bla bla.';
785         $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id,
786             'intro' => $labeldescription));
787         $labelcm = get_coursemodule_from_instance('label', $label->id);
788         $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id,
789             'name' => 'URL: % & $ ../', 'section' => 2));
790         $urlcm = get_coursemodule_from_instance('url', $url->id);
792         // Set the required capabilities by the external function.
793         $context = context_course::instance($course->id);
794         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
795         $this->assignUserCapability('moodle/course:update', $context->id, $roleid);
796         $this->assignUserCapability('mod/data:view', $context->id, $roleid);
798         $conditions = array('course' => $course->id, 'section' => 2);
799         $DB->set_field('course_sections', 'summary', 'Text with iframe <iframe src="https://moodle.org"></iframe>', $conditions);
800         rebuild_course_cache($course->id, true);
802         return array($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm);
803     }
805     /**
806      * Test get_course_contents
807      */
808     public function test_get_course_contents() {
809         $this->resetAfterTest(true);
811         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
813         $sections = core_course_external::get_course_contents($course->id, array());
814         // We need to execute the return values cleaning process to simulate the web service server.
815         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
817         // Check that forum and label descriptions are correctly returned.
818         $firstsection = array_shift($sections);
819         $lastsection = array_pop($sections);
821         $modinfo = get_fast_modinfo($course);
822         $testexecuted = 0;
823         foreach ($firstsection['modules'] as $module) {
824             if ($module['id'] == $forumcm->id and $module['modname'] == 'forum') {
825                 $cm = $modinfo->cms[$forumcm->id];
826                 $formattedtext = format_text($cm->content, FORMAT_HTML,
827                     array('noclean' => true, 'para' => false, 'filter' => false));
828                 $this->assertEquals($formattedtext, $module['description']);
829                 $this->assertEquals($forumcm->instance, $module['instance']);
830                 $testexecuted = $testexecuted + 1;
831             } else if ($module['id'] == $labelcm->id and $module['modname'] == 'label') {
832                 $cm = $modinfo->cms[$labelcm->id];
833                 $formattedtext = format_text($cm->content, FORMAT_HTML,
834                     array('noclean' => true, 'para' => false, 'filter' => false));
835                 $this->assertEquals($formattedtext, $module['description']);
836                 $this->assertEquals($labelcm->instance, $module['instance']);
837                 $testexecuted = $testexecuted + 1;
838             }
839         }
840         $this->assertEquals(2, $testexecuted);
841         $this->assertEquals(0, $firstsection['section']);
843         // Check that the only return section has the 5 created modules.
844         $this->assertCount(4, $firstsection['modules']);
845         $this->assertCount(1, $lastsection['modules']);
846         $this->assertEquals(2, $lastsection['section']);
847         $this->assertContains('<iframe', $lastsection['summary']);
848         $this->assertContains('</iframe>', $lastsection['summary']);
850         try {
851             $sections = core_course_external::get_course_contents($course->id,
852                                                                     array(array("name" => "invalid", "value" => 1)));
853             $this->fail('Exception expected due to invalid option.');
854         } catch (moodle_exception $e) {
855             $this->assertEquals('errorinvalidparam', $e->errorcode);
856         }
857     }
860     /**
861      * Test get_course_contents excluding modules
862      */
863     public function test_get_course_contents_excluding_modules() {
864         $this->resetAfterTest(true);
866         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
868         // Test exclude modules.
869         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludemodules", "value" => 1)));
871         // We need to execute the return values cleaning process to simulate the web service server.
872         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
874         $firstsection = array_shift($sections);
875         $lastsection = array_pop($sections);
877         $this->assertEmpty($firstsection['modules']);
878         $this->assertEmpty($lastsection['modules']);
879     }
881     /**
882      * Test get_course_contents excluding contents
883      */
884     public function test_get_course_contents_excluding_contents() {
885         $this->resetAfterTest(true);
887         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
889         // Test exclude modules.
890         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludecontents", "value" => 1)));
892         // We need to execute the return values cleaning process to simulate the web service server.
893         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
895         foreach ($sections as $section) {
896             foreach ($section['modules'] as $module) {
897                 // Only resources return contents.
898                 if (isset($module['contents'])) {
899                     $this->assertEmpty($module['contents']);
900                 }
901             }
902         }
903     }
905     /**
906      * Test get_course_contents filtering by section number
907      */
908     public function test_get_course_contents_section_number() {
909         $this->resetAfterTest(true);
911         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
913         // Test exclude modules.
914         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "sectionnumber", "value" => 0)));
916         // We need to execute the return values cleaning process to simulate the web service server.
917         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
919         $this->assertCount(1, $sections);
920         $this->assertCount(4, $sections[0]['modules']);
921     }
923     /**
924      * Test get_course_contents filtering by cmid
925      */
926     public function test_get_course_contents_cmid() {
927         $this->resetAfterTest(true);
929         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
931         // Test exclude modules.
932         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "cmid", "value" => $forumcm->id)));
934         // We need to execute the return values cleaning process to simulate the web service server.
935         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
937         $this->assertCount(3, $sections);
938         $this->assertCount(1, $sections[0]['modules']);
939         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
940     }
943     /**
944      * Test get_course_contents filtering by cmid and section
945      */
946     public function test_get_course_contents_section_cmid() {
947         $this->resetAfterTest(true);
949         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
951         // Test exclude modules.
952         $sections = core_course_external::get_course_contents($course->id, array(
953                                                                         array("name" => "cmid", "value" => $forumcm->id),
954                                                                         array("name" => "sectionnumber", "value" => 0)
955                                                                         ));
957         // We need to execute the return values cleaning process to simulate the web service server.
958         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
960         $this->assertCount(1, $sections);
961         $this->assertCount(1, $sections[0]['modules']);
962         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
963     }
965     /**
966      * Test get_course_contents filtering by modname
967      */
968     public function test_get_course_contents_modname() {
969         $this->resetAfterTest(true);
971         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
973         // Test exclude modules.
974         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "modname", "value" => "forum")));
976         // We need to execute the return values cleaning process to simulate the web service server.
977         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
979         $this->assertCount(3, $sections);
980         $this->assertCount(1, $sections[0]['modules']);
981         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
982     }
984     /**
985      * Test get_course_contents filtering by modname
986      */
987     public function test_get_course_contents_modid() {
988         $this->resetAfterTest(true);
990         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
992         // Test exclude modules.
993         $sections = core_course_external::get_course_contents($course->id, array(
994                                                                             array("name" => "modname", "value" => "page"),
995                                                                             array("name" => "modid", "value" => $pagecm->instance),
996                                                                             ));
998         // We need to execute the return values cleaning process to simulate the web service server.
999         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
1001         $this->assertCount(3, $sections);
1002         $this->assertCount(1, $sections[0]['modules']);
1003         $this->assertEquals("page", $sections[0]['modules'][0]["modname"]);
1004         $this->assertEquals($pagecm->instance, $sections[0]['modules'][0]["instance"]);
1005     }
1007     /**
1008      * Test duplicate_course
1009      */
1010     public function test_duplicate_course() {
1011         $this->resetAfterTest(true);
1013         // Create one course with three modules.
1014         $course  = self::getDataGenerator()->create_course();
1015         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
1016         $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
1017         $forumcontext = context_module::instance($forum->cmid);
1018         $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
1019         $datacontext = context_module::instance($data->cmid);
1020         $datacm = get_coursemodule_from_instance('page', $data->id);
1021         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
1022         $pagecontext = context_module::instance($page->cmid);
1023         $pagecm = get_coursemodule_from_instance('page', $page->id);
1025         // Set the required capabilities by the external function.
1026         $coursecontext = context_course::instance($course->id);
1027         $categorycontext = context_coursecat::instance($course->category);
1028         $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id);
1029         $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid);
1030         $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid);
1031         $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid);
1032         $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid);
1033         // Optional capabilities to copy user data.
1034         $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid);
1035         $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid);
1037         $newcourse['fullname'] = 'Course duplicate';
1038         $newcourse['shortname'] = 'courseduplicate';
1039         $newcourse['categoryid'] = $course->category;
1040         $newcourse['visible'] = true;
1041         $newcourse['options'][] = array('name' => 'users', 'value' => true);
1043         $duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'],
1044                 $newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']);
1046         // We need to execute the return values cleaning process to simulate the web service server.
1047         $duplicate = external_api::clean_returnvalue(core_course_external::duplicate_course_returns(), $duplicate);
1049         // Check that the course has been duplicated.
1050         $this->assertEquals($newcourse['shortname'], $duplicate['shortname']);
1051     }
1053     /**
1054      * Test update_courses
1055      */
1056     public function test_update_courses() {
1057         global $DB, $CFG, $USER, $COURSE;
1059         // Get current $COURSE to be able to restore it later (defaults to $SITE). We need this
1060         // trick because we are both updating and getting (for testing) course information
1061         // in the same request and core_course_external::update_courses()
1062         // is overwriting $COURSE all over the time with OLD values, so later
1063         // use of get_course() fetches those OLD values instead of the updated ones.
1064         // See MDL-39723 for more info.
1065         $origcourse = clone($COURSE);
1067         $this->resetAfterTest(true);
1069         // Set the required capabilities by the external function.
1070         $contextid = context_system::instance()->id;
1071         $roleid = $this->assignUserCapability('moodle/course:update', $contextid);
1072         $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1073         $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1074         $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1075         $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1076         $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1077         $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
1078         $this->assignUserCapability('moodle/course:viewhiddencourses', $contextid, $roleid);
1080         // Create category and course.
1081         $category1  = self::getDataGenerator()->create_category();
1082         $category2  = self::getDataGenerator()->create_category();
1083         $originalcourse1 = self::getDataGenerator()->create_course();
1084         self::getDataGenerator()->enrol_user($USER->id, $originalcourse1->id, $roleid);
1085         $originalcourse2 = self::getDataGenerator()->create_course();
1086         self::getDataGenerator()->enrol_user($USER->id, $originalcourse2->id, $roleid);
1088         // Course values to be updated.
1089         $course1['id'] = $originalcourse1->id;
1090         $course1['fullname'] = 'Updated test course 1';
1091         $course1['shortname'] = 'Udestedtestcourse1';
1092         $course1['categoryid'] = $category1->id;
1093         $course2['id'] = $originalcourse2->id;
1094         $course2['fullname'] = 'Updated test course 2';
1095         $course2['shortname'] = 'Updestedtestcourse2';
1096         $course2['categoryid'] = $category2->id;
1097         $course2['idnumber'] = 'Updatedidnumber2';
1098         $course2['summary'] = 'Updaated description for course 2';
1099         $course2['summaryformat'] = FORMAT_HTML;
1100         $course2['format'] = 'topics';
1101         $course2['showgrades'] = 1;
1102         $course2['newsitems'] = 3;
1103         $course2['startdate'] = 1420092000; // 01/01/2015.
1104         $course2['enddate'] = 1422669600; // 01/31/2015.
1105         $course2['maxbytes'] = 100000;
1106         $course2['showreports'] = 1;
1107         $course2['visible'] = 0;
1108         $course2['hiddensections'] = 0;
1109         $course2['groupmode'] = 0;
1110         $course2['groupmodeforce'] = 0;
1111         $course2['defaultgroupingid'] = 0;
1112         $course2['enablecompletion'] = 1;
1113         $course2['lang'] = 'en';
1114         $course2['forcetheme'] = 'bootstrapbase';
1115         $courses = array($course1, $course2);
1117         $updatedcoursewarnings = core_course_external::update_courses($courses);
1118         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1119                                                                     $updatedcoursewarnings);
1120         $COURSE = $origcourse; // Restore $COURSE. Instead of using the OLD one set by the previous line.
1122         // Check that right number of courses were created.
1123         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1125         // Check that the courses were correctly created.
1126         foreach ($courses as $course) {
1127             $courseinfo = course_get_format($course['id'])->get_course();
1128             if ($course['id'] == $course2['id']) {
1129                 $this->assertEquals($course2['fullname'], $courseinfo->fullname);
1130                 $this->assertEquals($course2['shortname'], $courseinfo->shortname);
1131                 $this->assertEquals($course2['categoryid'], $courseinfo->category);
1132                 $this->assertEquals($course2['idnumber'], $courseinfo->idnumber);
1133                 $this->assertEquals($course2['summary'], $courseinfo->summary);
1134                 $this->assertEquals($course2['summaryformat'], $courseinfo->summaryformat);
1135                 $this->assertEquals($course2['format'], $courseinfo->format);
1136                 $this->assertEquals($course2['showgrades'], $courseinfo->showgrades);
1137                 $this->assertEquals($course2['newsitems'], $courseinfo->newsitems);
1138                 $this->assertEquals($course2['startdate'], $courseinfo->startdate);
1139                 $this->assertEquals($course2['enddate'], $courseinfo->enddate);
1140                 $this->assertEquals($course2['maxbytes'], $courseinfo->maxbytes);
1141                 $this->assertEquals($course2['showreports'], $courseinfo->showreports);
1142                 $this->assertEquals($course2['visible'], $courseinfo->visible);
1143                 $this->assertEquals($course2['hiddensections'], $courseinfo->hiddensections);
1144                 $this->assertEquals($course2['groupmode'], $courseinfo->groupmode);
1145                 $this->assertEquals($course2['groupmodeforce'], $courseinfo->groupmodeforce);
1146                 $this->assertEquals($course2['defaultgroupingid'], $courseinfo->defaultgroupingid);
1147                 $this->assertEquals($course2['lang'], $courseinfo->lang);
1149                 if (!empty($CFG->allowcoursethemes)) {
1150                     $this->assertEquals($course2['forcetheme'], $courseinfo->theme);
1151                 }
1153                 $this->assertEquals($course2['enablecompletion'], $courseinfo->enablecompletion);
1154             } else if ($course['id'] == $course1['id']) {
1155                 $this->assertEquals($course1['fullname'], $courseinfo->fullname);
1156                 $this->assertEquals($course1['shortname'], $courseinfo->shortname);
1157                 $this->assertEquals($course1['categoryid'], $courseinfo->category);
1158                 $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
1159                 $this->assertEquals('topics', $courseinfo->format);
1160                 $this->assertEquals(5, course_get_format($course['id'])->get_last_section_number());
1161                 $this->assertEquals(0, $courseinfo->newsitems);
1162                 $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
1163             } else {
1164                 throw new moodle_exception('Unexpected shortname');
1165             }
1166         }
1168         $courses = array($course1);
1169         // Try update course without update capability.
1170         $user = self::getDataGenerator()->create_user();
1171         $this->setUser($user);
1172         $this->unassignUserCapability('moodle/course:update', $contextid, $roleid);
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(1, count($updatedcoursewarnings['warnings']));
1179         // Try update course category without capability.
1180         $this->assignUserCapability('moodle/course:update', $contextid, $roleid);
1181         $this->unassignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1182         $user = self::getDataGenerator()->create_user();
1183         $this->setUser($user);
1184         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1185         $course1['categoryid'] = $category2->id;
1186         $courses = array($course1);
1187         $updatedcoursewarnings = core_course_external::update_courses($courses);
1188         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1189                                                                     $updatedcoursewarnings);
1190         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1192         // Try update course fullname without capability.
1193         $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1194         $this->unassignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1195         $user = self::getDataGenerator()->create_user();
1196         $this->setUser($user);
1197         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1198         $updatedcoursewarnings = core_course_external::update_courses($courses);
1199         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1200                                                                     $updatedcoursewarnings);
1201         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1202         $course1['fullname'] = 'Testing fullname without permission';
1203         $courses = array($course1);
1204         $updatedcoursewarnings = core_course_external::update_courses($courses);
1205         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1206                                                                     $updatedcoursewarnings);
1207         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1209         // Try update course shortname without capability.
1210         $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1211         $this->unassignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1212         $user = self::getDataGenerator()->create_user();
1213         $this->setUser($user);
1214         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1215         $updatedcoursewarnings = core_course_external::update_courses($courses);
1216         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1217                                                                     $updatedcoursewarnings);
1218         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1219         $course1['shortname'] = 'Testing shortname without permission';
1220         $courses = array($course1);
1221         $updatedcoursewarnings = core_course_external::update_courses($courses);
1222         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1223                                                                     $updatedcoursewarnings);
1224         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1226         // Try update course idnumber without capability.
1227         $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1228         $this->unassignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1229         $user = self::getDataGenerator()->create_user();
1230         $this->setUser($user);
1231         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1232         $updatedcoursewarnings = core_course_external::update_courses($courses);
1233         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1234                                                                     $updatedcoursewarnings);
1235         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1236         $course1['idnumber'] = 'NEWIDNUMBER';
1237         $courses = array($course1);
1238         $updatedcoursewarnings = core_course_external::update_courses($courses);
1239         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1240                                                                     $updatedcoursewarnings);
1241         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1243         // Try update course summary without capability.
1244         $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1245         $this->unassignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1246         $user = self::getDataGenerator()->create_user();
1247         $this->setUser($user);
1248         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1249         $updatedcoursewarnings = core_course_external::update_courses($courses);
1250         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1251                                                                     $updatedcoursewarnings);
1252         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1253         $course1['summary'] = 'New summary';
1254         $courses = array($course1);
1255         $updatedcoursewarnings = core_course_external::update_courses($courses);
1256         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1257                                                                     $updatedcoursewarnings);
1258         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1260         // Try update course with invalid summary format.
1261         $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1262         $user = self::getDataGenerator()->create_user();
1263         $this->setUser($user);
1264         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
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(0, count($updatedcoursewarnings['warnings']));
1269         $course1['summaryformat'] = 10;
1270         $courses = array($course1);
1271         $updatedcoursewarnings = core_course_external::update_courses($courses);
1272         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1273                                                                     $updatedcoursewarnings);
1274         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1276         // Try update course visibility without capability.
1277         $this->unassignUserCapability('moodle/course:visibility', $contextid, $roleid);
1278         $user = self::getDataGenerator()->create_user();
1279         $this->setUser($user);
1280         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1281         $course1['summaryformat'] = FORMAT_MOODLE;
1282         $courses = array($course1);
1283         $updatedcoursewarnings = core_course_external::update_courses($courses);
1284         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1285                                                                     $updatedcoursewarnings);
1286         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1287         $course1['visible'] = 0;
1288         $courses = array($course1);
1289         $updatedcoursewarnings = core_course_external::update_courses($courses);
1290         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1291                                                                     $updatedcoursewarnings);
1292         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1293     }
1295     /**
1296      * Test delete course_module.
1297      */
1298     public function test_delete_modules() {
1299         global $DB;
1301         // Ensure we reset the data after this test.
1302         $this->resetAfterTest(true);
1304         // Create a user.
1305         $user = self::getDataGenerator()->create_user();
1307         // Set the tests to run as the user.
1308         self::setUser($user);
1310         // Create a course to add the modules.
1311         $course = self::getDataGenerator()->create_course();
1313         // Create two test modules.
1314         $record = new stdClass();
1315         $record->course = $course->id;
1316         $module1 = self::getDataGenerator()->create_module('forum', $record);
1317         $module2 = self::getDataGenerator()->create_module('assign', $record);
1319         // Check the forum was correctly created.
1320         $this->assertEquals(1, $DB->count_records('forum', array('id' => $module1->id)));
1322         // Check the assignment was correctly created.
1323         $this->assertEquals(1, $DB->count_records('assign', array('id' => $module2->id)));
1325         // Check data exists in the course modules table.
1326         $this->assertEquals(2, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
1327                 array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
1329         // Enrol the user in the course.
1330         $enrol = enrol_get_plugin('manual');
1331         $enrolinstances = enrol_get_instances($course->id, true);
1332         foreach ($enrolinstances as $courseenrolinstance) {
1333             if ($courseenrolinstance->enrol == "manual") {
1334                 $instance = $courseenrolinstance;
1335                 break;
1336             }
1337         }
1338         $enrol->enrol_user($instance, $user->id);
1340         // Assign capabilities to delete module 1.
1341         $modcontext = context_module::instance($module1->cmid);
1342         $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id);
1344         // Assign capabilities to delete module 2.
1345         $modcontext = context_module::instance($module2->cmid);
1346         $newrole = create_role('Role 2', 'role2', 'Role 2 description');
1347         $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id, $newrole);
1349         // Deleting these module instances.
1350         core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1352         // Check the forum was deleted.
1353         $this->assertEquals(0, $DB->count_records('forum', array('id' => $module1->id)));
1355         // Check the assignment was deleted.
1356         $this->assertEquals(0, $DB->count_records('assign', array('id' => $module2->id)));
1358         // Check we retrieve no data in the course modules table.
1359         $this->assertEquals(0, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
1360                 array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
1362         // Call with non-existent course module id and ensure exception thrown.
1363         try {
1364             core_course_external::delete_modules(array('1337'));
1365             $this->fail('Exception expected due to missing course module.');
1366         } catch (dml_missing_record_exception $e) {
1367             $this->assertEquals('invalidcoursemodule', $e->errorcode);
1368         }
1370         // Create two modules.
1371         $module1 = self::getDataGenerator()->create_module('forum', $record);
1372         $module2 = self::getDataGenerator()->create_module('assign', $record);
1374         // Since these modules were recreated the user will not have capabilities
1375         // to delete them, ensure exception is thrown if they try.
1376         try {
1377             core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1378             $this->fail('Exception expected due to missing capability.');
1379         } catch (moodle_exception $e) {
1380             $this->assertEquals('nopermissions', $e->errorcode);
1381         }
1383         // Unenrol user from the course.
1384         $enrol->unenrol_user($instance, $user->id);
1386         // Try and delete modules from the course the user was unenrolled in, make sure exception thrown.
1387         try {
1388             core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1389             $this->fail('Exception expected due to being unenrolled from the course.');
1390         } catch (moodle_exception $e) {
1391             $this->assertEquals('requireloginerror', $e->errorcode);
1392         }
1393     }
1395     /**
1396      * Test import_course into an empty course
1397      */
1398     public function test_import_course_empty() {
1399         global $USER;
1401         $this->resetAfterTest(true);
1403         $course1  = self::getDataGenerator()->create_course();
1404         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course1->id, 'name' => 'Forum test'));
1405         $page = $this->getDataGenerator()->create_module('page', array('course' => $course1->id, 'name' => 'Page test'));
1407         $course2  = self::getDataGenerator()->create_course();
1409         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1410         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1412         // Verify the state of the courses before we do the import.
1413         $this->assertCount(2, $course1cms);
1414         $this->assertEmpty($course2cms);
1416         // Setup the user to run the operation (ugly hack because validate_context() will
1417         // fail as the email is not set by $this->setAdminUser()).
1418         $this->setAdminUser();
1419         $USER->email = 'emailtopass@example.com';
1421         // Import from course1 to course2.
1422         core_course_external::import_course($course1->id, $course2->id, 0);
1424         // Verify that now we have two modules in both courses.
1425         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1426         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1427         $this->assertCount(2, $course1cms);
1428         $this->assertCount(2, $course2cms);
1430         // Verify that the names transfered across correctly.
1431         foreach ($course2cms as $cm) {
1432             if ($cm->modname === 'page') {
1433                 $this->assertEquals($cm->name, $page->name);
1434             } else if ($cm->modname === 'forum') {
1435                 $this->assertEquals($cm->name, $forum->name);
1436             } else {
1437                 $this->fail('Unknown CM found.');
1438             }
1439         }
1440     }
1442     /**
1443      * Test import_course into an filled course
1444      */
1445     public function test_import_course_filled() {
1446         global $USER;
1448         $this->resetAfterTest(true);
1450         // Add forum and page to course1.
1451         $course1  = self::getDataGenerator()->create_course();
1452         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1453         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
1455         // Add quiz to course 2.
1456         $course2  = self::getDataGenerator()->create_course();
1457         $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
1459         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1460         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1462         // Verify the state of the courses before we do the import.
1463         $this->assertCount(2, $course1cms);
1464         $this->assertCount(1, $course2cms);
1466         // Setup the user to run the operation (ugly hack because validate_context() will
1467         // fail as the email is not set by $this->setAdminUser()).
1468         $this->setAdminUser();
1469         $USER->email = 'emailtopass@example.com';
1471         // Import from course1 to course2 without deleting content.
1472         core_course_external::import_course($course1->id, $course2->id, 0);
1474         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1476         // Verify that now we have three modules in course2.
1477         $this->assertCount(3, $course2cms);
1479         // Verify that the names transfered across correctly.
1480         foreach ($course2cms as $cm) {
1481             if ($cm->modname === 'page') {
1482                 $this->assertEquals($cm->name, $page->name);
1483             } else if ($cm->modname === 'forum') {
1484                 $this->assertEquals($cm->name, $forum->name);
1485             } else if ($cm->modname === 'quiz') {
1486                 $this->assertEquals($cm->name, $quiz->name);
1487             } else {
1488                 $this->fail('Unknown CM found.');
1489             }
1490         }
1491     }
1493     /**
1494      * Test import_course with only blocks set to backup
1495      */
1496     public function test_import_course_blocksonly() {
1497         global $USER, $DB;
1499         $this->resetAfterTest(true);
1501         // Add forum and page to course1.
1502         $course1  = self::getDataGenerator()->create_course();
1503         $course1ctx = context_course::instance($course1->id);
1504         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1505         $block = $this->getDataGenerator()->create_block('online_users', array('parentcontextid' => $course1ctx->id));
1507         $course2  = self::getDataGenerator()->create_course();
1508         $course2ctx = context_course::instance($course2->id);
1509         $initialblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
1510         $initialcmcount = count(get_fast_modinfo($course2->id)->get_cms());
1512         // Setup the user to run the operation (ugly hack because validate_context() will
1513         // fail as the email is not set by $this->setAdminUser()).
1514         $this->setAdminUser();
1515         $USER->email = 'emailtopass@example.com';
1517         // Import from course1 to course2 without deleting content, but excluding
1518         // activities.
1519         $options = array(
1520             array('name' => 'activities', 'value' => 0),
1521             array('name' => 'blocks', 'value' => 1),
1522             array('name' => 'filters', 'value' => 0),
1523         );
1525         core_course_external::import_course($course1->id, $course2->id, 0, $options);
1527         $newcmcount = count(get_fast_modinfo($course2->id)->get_cms());
1528         $newblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
1529         // Check that course modules haven't changed, but that blocks have.
1530         $this->assertEquals($initialcmcount, $newcmcount);
1531         $this->assertEquals(($initialblockcount + 1), $newblockcount);
1532     }
1534     /**
1535      * Test import_course into an filled course, deleting content.
1536      */
1537     public function test_import_course_deletecontent() {
1538         global $USER;
1539         $this->resetAfterTest(true);
1541         // Add forum and page to course1.
1542         $course1  = self::getDataGenerator()->create_course();
1543         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1544         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
1546         // Add quiz to course 2.
1547         $course2  = self::getDataGenerator()->create_course();
1548         $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
1550         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1551         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1553         // Verify the state of the courses before we do the import.
1554         $this->assertCount(2, $course1cms);
1555         $this->assertCount(1, $course2cms);
1557         // Setup the user to run the operation (ugly hack because validate_context() will
1558         // fail as the email is not set by $this->setAdminUser()).
1559         $this->setAdminUser();
1560         $USER->email = 'emailtopass@example.com';
1562         // Import from course1 to course2,  deleting content.
1563         core_course_external::import_course($course1->id, $course2->id, 1);
1565         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1567         // Verify that now we have two modules in course2.
1568         $this->assertCount(2, $course2cms);
1570         // Verify that the course only contains the imported modules.
1571         foreach ($course2cms as $cm) {
1572             if ($cm->modname === 'page') {
1573                 $this->assertEquals($cm->name, $page->name);
1574             } else if ($cm->modname === 'forum') {
1575                 $this->assertEquals($cm->name, $forum->name);
1576             } else {
1577                 $this->fail('Unknown CM found: '.$cm->name);
1578             }
1579         }
1580     }
1582     /**
1583      * Ensure import_course handles incorrect deletecontent option correctly.
1584      */
1585     public function test_import_course_invalid_deletecontent_option() {
1586         $this->resetAfterTest(true);
1588         $course1  = self::getDataGenerator()->create_course();
1589         $course2  = self::getDataGenerator()->create_course();
1591         $this->expectException('moodle_exception');
1592         $this->expectExceptionMessage(get_string('invalidextparam', 'webservice', -1));
1593         // Import from course1 to course2, with invalid option
1594         core_course_external::import_course($course1->id, $course2->id, -1);;
1595     }
1597     /**
1598      * Test view_course function
1599      */
1600     public function test_view_course() {
1602         $this->resetAfterTest();
1604         // Course without sections.
1605         $course = $this->getDataGenerator()->create_course(array('numsections' => 5), array('createsections' => true));
1606         $this->setAdminUser();
1608         // Redirect events to the sink, so we can recover them later.
1609         $sink = $this->redirectEvents();
1611         $result = core_course_external::view_course($course->id, 1);
1612         $result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result);
1613         $events = $sink->get_events();
1614         $event = reset($events);
1616         // Check the event details are correct.
1617         $this->assertInstanceOf('\core\event\course_viewed', $event);
1618         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1619         $this->assertEquals(1, $event->other['coursesectionnumber']);
1621         $result = core_course_external::view_course($course->id);
1622         $result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result);
1623         $events = $sink->get_events();
1624         $event = array_pop($events);
1625         $sink->close();
1627         // Check the event details are correct.
1628         $this->assertInstanceOf('\core\event\course_viewed', $event);
1629         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1630         $this->assertEmpty($event->other);
1632     }
1634     /**
1635      * Test get_course_module
1636      */
1637     public function test_get_course_module() {
1638         global $DB;
1640         $this->resetAfterTest(true);
1642         $this->setAdminUser();
1643         $course = self::getDataGenerator()->create_course();
1644         $record = array(
1645             'course' => $course->id,
1646             'name' => 'First Assignment'
1647         );
1648         $options = array(
1649             'idnumber' => 'ABC',
1650             'visible' => 0
1651         );
1652         // Hidden activity.
1653         $assign = self::getDataGenerator()->create_module('assign', $record, $options);
1655         $outcomescale = 'Distinction, Very Good, Good, Pass, Fail';
1657         // Insert a custom grade scale to be used by an outcome.
1658         $gradescale = new grade_scale();
1659         $gradescale->name        = 'gettcoursemodulescale';
1660         $gradescale->courseid    = $course->id;
1661         $gradescale->userid      = 0;
1662         $gradescale->scale       = $outcomescale;
1663         $gradescale->description = 'This scale is used to mark standard assignments.';
1664         $gradescale->insert();
1666         // Insert an outcome.
1667         $data = new stdClass();
1668         $data->courseid = $course->id;
1669         $data->fullname = 'Team work';
1670         $data->shortname = 'Team work';
1671         $data->scaleid = $gradescale->id;
1672         $outcome = new grade_outcome($data, false);
1673         $outcome->insert();
1675         $outcomegradeitem = new grade_item();
1676         $outcomegradeitem->itemname = $outcome->shortname;
1677         $outcomegradeitem->itemtype = 'mod';
1678         $outcomegradeitem->itemmodule = 'assign';
1679         $outcomegradeitem->iteminstance = $assign->id;
1680         $outcomegradeitem->outcomeid = $outcome->id;
1681         $outcomegradeitem->cmid = 0;
1682         $outcomegradeitem->courseid = $course->id;
1683         $outcomegradeitem->aggregationcoef = 0;
1684         $outcomegradeitem->itemnumber = 1; // The activity's original grade item will be 0.
1685         $outcomegradeitem->gradetype = GRADE_TYPE_SCALE;
1686         $outcomegradeitem->scaleid = $outcome->scaleid;
1687         $outcomegradeitem->insert();
1689         $assignmentgradeitem = grade_item::fetch(
1690             array(
1691                 'itemtype' => 'mod',
1692                 'itemmodule' => 'assign',
1693                 'iteminstance' => $assign->id,
1694                 'itemnumber' => 0,
1695                 'courseid' => $course->id
1696             )
1697         );
1698         $outcomegradeitem->set_parent($assignmentgradeitem->categoryid);
1699         $outcomegradeitem->move_after_sortorder($assignmentgradeitem->sortorder);
1701         // Test admin user can see the complete hidden activity.
1702         $result = core_course_external::get_course_module($assign->cmid);
1703         $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result);
1705         $this->assertCount(0, $result['warnings']);
1706         // Test we retrieve all the fields.
1707         $this->assertCount(28, $result['cm']);
1708         $this->assertEquals($record['name'], $result['cm']['name']);
1709         $this->assertEquals($options['idnumber'], $result['cm']['idnumber']);
1710         $this->assertEquals(100, $result['cm']['grade']);
1711         $this->assertEquals(0.0, $result['cm']['gradepass']);
1712         $this->assertEquals('submissions', $result['cm']['advancedgrading'][0]['area']);
1713         $this->assertEmpty($result['cm']['advancedgrading'][0]['method']);
1714         $this->assertEquals($outcomescale, $result['cm']['outcomes'][0]['scale']);
1716         $student = $this->getDataGenerator()->create_user();
1717         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1719         self::getDataGenerator()->enrol_user($student->id,  $course->id, $studentrole->id);
1720         $this->setUser($student);
1722         // The user shouldn't be able to see the activity.
1723         try {
1724             core_course_external::get_course_module($assign->cmid);
1725             $this->fail('Exception expected due to invalid permissions.');
1726         } catch (moodle_exception $e) {
1727             $this->assertEquals('requireloginerror', $e->errorcode);
1728         }
1730         // Make module visible.
1731         set_coursemodule_visible($assign->cmid, 1);
1733         // Test student user.
1734         $result = core_course_external::get_course_module($assign->cmid);
1735         $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result);
1737         $this->assertCount(0, $result['warnings']);
1738         // Test we retrieve only the few files we can see.
1739         $this->assertCount(11, $result['cm']);
1740         $this->assertEquals($assign->cmid, $result['cm']['id']);
1741         $this->assertEquals($course->id, $result['cm']['course']);
1742         $this->assertEquals('assign', $result['cm']['modname']);
1743         $this->assertEquals($assign->id, $result['cm']['instance']);
1745     }
1747     /**
1748      * Test get_course_module_by_instance
1749      */
1750     public function test_get_course_module_by_instance() {
1751         global $DB;
1753         $this->resetAfterTest(true);
1755         $this->setAdminUser();
1756         $course = self::getDataGenerator()->create_course();
1757         $record = array(
1758             'course' => $course->id,
1759             'name' => 'First Chat'
1760         );
1761         $options = array(
1762             'idnumber' => 'ABC',
1763             'visible' => 0
1764         );
1765         // Hidden activity.
1766         $chat = self::getDataGenerator()->create_module('chat', $record, $options);
1768         // Test admin user can see the complete hidden activity.
1769         $result = core_course_external::get_course_module_by_instance('chat', $chat->id);
1770         $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result);
1772         $this->assertCount(0, $result['warnings']);
1773         // Test we retrieve all the fields.
1774         $this->assertCount(23, $result['cm']);
1775         $this->assertEquals($record['name'], $result['cm']['name']);
1776         $this->assertEquals($options['idnumber'], $result['cm']['idnumber']);
1778         $student = $this->getDataGenerator()->create_user();
1779         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1781         self::getDataGenerator()->enrol_user($student->id,  $course->id, $studentrole->id);
1782         $this->setUser($student);
1784         // The user shouldn't be able to see the activity.
1785         try {
1786             core_course_external::get_course_module_by_instance('chat', $chat->id);
1787             $this->fail('Exception expected due to invalid permissions.');
1788         } catch (moodle_exception $e) {
1789             $this->assertEquals('requireloginerror', $e->errorcode);
1790         }
1792         // Make module visible.
1793         set_coursemodule_visible($chat->cmid, 1);
1795         // Test student user.
1796         $result = core_course_external::get_course_module_by_instance('chat', $chat->id);
1797         $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result);
1799         $this->assertCount(0, $result['warnings']);
1800         // Test we retrieve only the few files we can see.
1801         $this->assertCount(11, $result['cm']);
1802         $this->assertEquals($chat->cmid, $result['cm']['id']);
1803         $this->assertEquals($course->id, $result['cm']['course']);
1804         $this->assertEquals('chat', $result['cm']['modname']);
1805         $this->assertEquals($chat->id, $result['cm']['instance']);
1807         // Try with an invalid module name.
1808         try {
1809             core_course_external::get_course_module_by_instance('abc', $chat->id);
1810             $this->fail('Exception expected due to invalid module name.');
1811         } catch (dml_read_exception $e) {
1812             $this->assertEquals('dmlreadexception', $e->errorcode);
1813         }
1815     }
1817     /**
1818      * Test get_activities_overview
1819      */
1820     public function test_get_activities_overview() {
1821         global $USER;
1823         $this->resetAfterTest();
1824         $course1 = self::getDataGenerator()->create_course();
1825         $course2 = self::getDataGenerator()->create_course();
1827         // Create a viewer user.
1828         $viewer = self::getDataGenerator()->create_user((object) array('trackforums' => 1));
1829         $this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
1830         $this->getDataGenerator()->enrol_user($viewer->id, $course2->id);
1832         // Create two forums - one in each course.
1833         $record = new stdClass();
1834         $record->course = $course1->id;
1835         $forum1 = self::getDataGenerator()->create_module('forum', (object) array('course' => $course1->id));
1836         $forum2 = self::getDataGenerator()->create_module('forum', (object) array('course' => $course2->id));
1838         $this->setAdminUser();
1839         // A standard post in the forum.
1840         $record = new stdClass();
1841         $record->course = $course1->id;
1842         $record->userid = $USER->id;
1843         $record->forum = $forum1->id;
1844         $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1846         $this->setUser($viewer->id);
1847         $courses = array($course1->id , $course2->id);
1849         $result = core_course_external::get_activities_overview($courses);
1850         $this->assertDebuggingCalledCount(8);
1851         $result = external_api::clean_returnvalue(core_course_external::get_activities_overview_returns(), $result);
1853         // There should be one entry for course1, and no others.
1854         $this->assertCount(1, $result['courses']);
1855         $this->assertEquals($course1->id, $result['courses'][0]['id']);
1856         // Check expected overview data for the module.
1857         $this->assertEquals('forum', $result['courses'][0]['overviews'][0]['module']);
1858         $this->assertContains('1 total unread', $result['courses'][0]['overviews'][0]['overviewtext']);
1859     }
1861     /**
1862      * Test get_user_navigation_options
1863      */
1864     public function test_get_user_navigation_options() {
1865         global $USER;
1867         $this->resetAfterTest();
1868         $course1 = self::getDataGenerator()->create_course();
1869         $course2 = self::getDataGenerator()->create_course();
1871         // Create a viewer user.
1872         $viewer = self::getDataGenerator()->create_user();
1873         $this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
1874         $this->getDataGenerator()->enrol_user($viewer->id, $course2->id);
1876         $this->setUser($viewer->id);
1877         $courses = array($course1->id , $course2->id, SITEID);
1879         $result = core_course_external::get_user_navigation_options($courses);
1880         $result = external_api::clean_returnvalue(core_course_external::get_user_navigation_options_returns(), $result);
1882         $this->assertCount(0, $result['warnings']);
1883         $this->assertCount(3, $result['courses']);
1885         foreach ($result['courses'] as $course) {
1886             $navoptions = new stdClass;
1887             foreach ($course['options'] as $option) {
1888                 $navoptions->{$option['name']} = $option['available'];
1889             }
1890             $this->assertCount(9, $course['options']);
1891             if ($course['id'] == SITEID) {
1892                 $this->assertTrue($navoptions->blogs);
1893                 $this->assertFalse($navoptions->notes);
1894                 $this->assertFalse($navoptions->participants);
1895                 $this->assertTrue($navoptions->badges);
1896                 $this->assertTrue($navoptions->tags);
1897                 $this->assertFalse($navoptions->grades);
1898                 $this->assertFalse($navoptions->search);
1899                 $this->assertTrue($navoptions->calendar);
1900                 $this->assertTrue($navoptions->competencies);
1901             } else {
1902                 $this->assertTrue($navoptions->blogs);
1903                 $this->assertFalse($navoptions->notes);
1904                 $this->assertTrue($navoptions->participants);
1905                 $this->assertTrue($navoptions->badges);
1906                 $this->assertFalse($navoptions->tags);
1907                 $this->assertTrue($navoptions->grades);
1908                 $this->assertFalse($navoptions->search);
1909                 $this->assertFalse($navoptions->calendar);
1910                 $this->assertTrue($navoptions->competencies);
1911             }
1912         }
1913     }
1915     /**
1916      * Test get_user_administration_options
1917      */
1918     public function test_get_user_administration_options() {
1919         global $USER;
1921         $this->resetAfterTest();
1922         $course1 = self::getDataGenerator()->create_course();
1923         $course2 = self::getDataGenerator()->create_course();
1925         // Create a viewer user.
1926         $viewer = self::getDataGenerator()->create_user();
1927         $this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
1928         $this->getDataGenerator()->enrol_user($viewer->id, $course2->id);
1930         $this->setUser($viewer->id);
1931         $courses = array($course1->id , $course2->id, SITEID);
1933         $result = core_course_external::get_user_administration_options($courses);
1934         $result = external_api::clean_returnvalue(core_course_external::get_user_administration_options_returns(), $result);
1936         $this->assertCount(0, $result['warnings']);
1937         $this->assertCount(3, $result['courses']);
1939         foreach ($result['courses'] as $course) {
1940             $adminoptions = new stdClass;
1941             foreach ($course['options'] as $option) {
1942                 $adminoptions->{$option['name']} = $option['available'];
1943             }
1944             if ($course['id'] == SITEID) {
1945                 $this->assertCount(15, $course['options']);
1946                 $this->assertFalse($adminoptions->update);
1947                 $this->assertFalse($adminoptions->filters);
1948                 $this->assertFalse($adminoptions->reports);
1949                 $this->assertFalse($adminoptions->backup);
1950                 $this->assertFalse($adminoptions->restore);
1951                 $this->assertFalse($adminoptions->files);
1952                 $this->assertFalse(!isset($adminoptions->tags));
1953                 $this->assertFalse($adminoptions->gradebook);
1954                 $this->assertFalse($adminoptions->outcomes);
1955                 $this->assertFalse($adminoptions->badges);
1956                 $this->assertFalse($adminoptions->import);
1957                 $this->assertFalse($adminoptions->publish);
1958                 $this->assertFalse($adminoptions->reset);
1959                 $this->assertFalse($adminoptions->roles);
1960             } else {
1961                 $this->assertCount(14, $course['options']);
1962                 $this->assertFalse($adminoptions->update);
1963                 $this->assertFalse($adminoptions->filters);
1964                 $this->assertFalse($adminoptions->reports);
1965                 $this->assertFalse($adminoptions->backup);
1966                 $this->assertFalse($adminoptions->restore);
1967                 $this->assertFalse($adminoptions->files);
1968                 $this->assertFalse($adminoptions->tags);
1969                 $this->assertFalse($adminoptions->gradebook);
1970                 $this->assertFalse($adminoptions->outcomes);
1971                 $this->assertTrue($adminoptions->badges);
1972                 $this->assertFalse($adminoptions->import);
1973                 $this->assertFalse($adminoptions->publish);
1974                 $this->assertFalse($adminoptions->reset);
1975                 $this->assertFalse($adminoptions->roles);
1976             }
1977         }
1978     }
1980     /**
1981      * Test get_courses_by_fields
1982      */
1983     public function test_get_courses_by_field() {
1984         global $DB;
1985         $this->resetAfterTest(true);
1987         $category1 = self::getDataGenerator()->create_category();
1988         $category2 = self::getDataGenerator()->create_category(array('parent' => $category1->id));
1989         $course1 = self::getDataGenerator()->create_course(array('category' => $category1->id, 'shortname' => 'c1'));
1990         $course2 = self::getDataGenerator()->create_course(array('visible' => 0, 'category' => $category2->id, 'idnumber' => 'i2'));
1992         $student1 = self::getDataGenerator()->create_user();
1993         $user1 = self::getDataGenerator()->create_user();
1994         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1995         self::getDataGenerator()->enrol_user($student1->id, $course1->id, $studentrole->id);
1996         self::getDataGenerator()->enrol_user($student1->id, $course2->id, $studentrole->id);
1998         self::setAdminUser();
1999         // As admins, we should be able to retrieve everything.
2000         $result = core_course_external::get_courses_by_field();
2001         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2002         $this->assertCount(3, $result['courses']);
2003         // Expect to receive all the fields.
2004         $this->assertCount(37, $result['courses'][0]);
2005         $this->assertCount(37, $result['courses'][1]);
2006         $this->assertCount(37, $result['courses'][2]);
2008         $result = core_course_external::get_courses_by_field('id', $course1->id);
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']);
2012         // Expect to receive all the fields.
2013         $this->assertCount(37, $result['courses'][0]);
2015         $result = core_course_external::get_courses_by_field('id', $course2->id);
2016         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2017         $this->assertCount(1, $result['courses']);
2018         $this->assertEquals($course2->id, $result['courses'][0]['id']);
2020         $result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id");
2021         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2022         $this->assertCount(2, $result['courses']);
2024         // Check default filters.
2025         $this->assertCount(3, $result['courses'][0]['filters']);
2026         $this->assertCount(3, $result['courses'][1]['filters']);
2028         $result = core_course_external::get_courses_by_field('category', $category1->id);
2029         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2030         $this->assertCount(1, $result['courses']);
2031         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2033         $result = core_course_external::get_courses_by_field('shortname', 'c1');
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']);
2038         $result = core_course_external::get_courses_by_field('idnumber', 'i2');
2039         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2040         $this->assertCount(1, $result['courses']);
2041         $this->assertEquals($course2->id, $result['courses'][0]['id']);
2043         $result = core_course_external::get_courses_by_field('idnumber', 'x');
2044         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2045         $this->assertCount(0, $result['courses']);
2047         // Change filter value.
2048         filter_set_local_state('mediaplugin', context_course::instance($course1->id)->id, TEXTFILTER_OFF);
2050         self::setUser($student1);
2051         // All visible courses  (including front page) for normal student.
2052         $result = core_course_external::get_courses_by_field();
2053         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2054         $this->assertCount(2, $result['courses']);
2055         $this->assertCount(30, $result['courses'][0]);
2056         $this->assertCount(30, $result['courses'][1]);
2058         $result = core_course_external::get_courses_by_field('id', $course1->id);
2059         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2060         $this->assertCount(1, $result['courses']);
2061         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2062         // Expect to receive all the files that a student can see.
2063         $this->assertCount(30, $result['courses'][0]);
2065         // Check default filters.
2066         $filters = $result['courses'][0]['filters'];
2067         $this->assertCount(3, $filters);
2068         $found = false;
2069         foreach ($filters as $filter) {
2070             if ($filter['filter'] == 'mediaplugin' and $filter['localstate'] == TEXTFILTER_OFF) {
2071                 $found = true;
2072             }
2073         }
2074         $this->assertTrue($found);
2076         // Course 2 is not visible.
2077         $result = core_course_external::get_courses_by_field('id', $course2->id);
2078         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2079         $this->assertCount(0, $result['courses']);
2081         $result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id");
2082         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2083         $this->assertCount(1, $result['courses']);
2085         $result = core_course_external::get_courses_by_field('category', $category1->id);
2086         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2087         $this->assertCount(1, $result['courses']);
2088         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2090         $result = core_course_external::get_courses_by_field('shortname', 'c1');
2091         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2092         $this->assertCount(1, $result['courses']);
2093         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2095         $result = core_course_external::get_courses_by_field('idnumber', 'i2');
2096         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2097         $this->assertCount(0, $result['courses']);
2099         $result = core_course_external::get_courses_by_field('idnumber', 'x');
2100         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2101         $this->assertCount(0, $result['courses']);
2103         self::setUser($user1);
2104         // All visible courses (including front page) for authenticated user.
2105         $result = core_course_external::get_courses_by_field();
2106         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2107         $this->assertCount(2, $result['courses']);
2108         $this->assertCount(30, $result['courses'][0]);  // Site course.
2109         $this->assertCount(13, $result['courses'][1]);  // Only public information, not enrolled.
2111         $result = core_course_external::get_courses_by_field('id', $course1->id);
2112         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2113         $this->assertCount(1, $result['courses']);
2114         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2115         // Expect to receive all the files that a authenticated can see.
2116         $this->assertCount(13, $result['courses'][0]);
2118         // Course 2 is not visible.
2119         $result = core_course_external::get_courses_by_field('id', $course2->id);
2120         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2121         $this->assertCount(0, $result['courses']);
2123         $result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id");
2124         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2125         $this->assertCount(1, $result['courses']);
2127         $result = core_course_external::get_courses_by_field('category', $category1->id);
2128         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2129         $this->assertCount(1, $result['courses']);
2130         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2132         $result = core_course_external::get_courses_by_field('shortname', 'c1');
2133         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2134         $this->assertCount(1, $result['courses']);
2135         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2137         $result = core_course_external::get_courses_by_field('idnumber', 'i2');
2138         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2139         $this->assertCount(0, $result['courses']);
2141         $result = core_course_external::get_courses_by_field('idnumber', 'x');
2142         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2143         $this->assertCount(0, $result['courses']);
2144     }
2146     public function test_get_courses_by_field_invalid_field() {
2147         $this->expectException('invalid_parameter_exception');
2148         $result = core_course_external::get_courses_by_field('zyx', 'x');
2149     }
2151     public function test_get_courses_by_field_invalid_courses() {
2152         $result = core_course_external::get_courses_by_field('id', '-1');
2153         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2154         $this->assertCount(0, $result['courses']);
2155     }
2157     public function test_check_updates() {
2158         global $DB;
2159         $this->resetAfterTest(true);
2160         $this->setAdminUser();
2162         // Create different types of activities.
2163         $course  = self::getDataGenerator()->create_course();
2164         $tocreate = array('assign', 'book', 'choice', 'folder', 'forum', 'glossary', 'imscp', 'label', 'lti', 'page', 'quiz',
2165                             'resource', 'scorm', 'survey', 'url', 'wiki');
2167         $modules = array();
2168         foreach ($tocreate as $modname) {
2169             $modules[$modname]['instance'] = $this->getDataGenerator()->create_module($modname, array('course' => $course->id));
2170             $modules[$modname]['cm'] = get_coursemodule_from_id(false, $modules[$modname]['instance']->cmid);
2171             $modules[$modname]['context'] = context_module::instance($modules[$modname]['instance']->cmid);
2172         }
2174         $student = self::getDataGenerator()->create_user();
2175         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
2176         self::getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
2177         $this->setUser($student);
2179         $since = time();
2180         $this->waitForSecond();
2181         $params = array();
2182         foreach ($modules as $modname => $data) {
2183             $params[$data['cm']->id] = array(
2184                 'contextlevel' => 'module',
2185                 'id' => $data['cm']->id,
2186                 'since' => $since
2187             );
2188         }
2190         // Check there is nothing updated because modules are fresh new.
2191         $result = core_course_external::check_updates($course->id, $params);
2192         $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result);
2193         $this->assertCount(0, $result['instances']);
2194         $this->assertCount(0, $result['warnings']);
2196         // Test with get_updates_since the same data.
2197         $result = core_course_external::get_updates_since($course->id, $since);
2198         $result = external_api::clean_returnvalue(core_course_external::get_updates_since_returns(), $result);
2199         $this->assertCount(0, $result['instances']);
2200         $this->assertCount(0, $result['warnings']);
2202         // Update a module after a second.
2203         $this->waitForSecond();
2204         set_coursemodule_name($modules['forum']['cm']->id, 'New forum name');
2206         $found = false;
2207         $result = core_course_external::check_updates($course->id, $params);
2208         $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result);
2209         $this->assertCount(1, $result['instances']);
2210         $this->assertCount(0, $result['warnings']);
2211         foreach ($result['instances'] as $module) {
2212             foreach ($module['updates'] as $update) {
2213                 if ($module['id'] == $modules['forum']['cm']->id and $update['name'] == 'configuration') {
2214                     $found = true;
2215                 }
2216             }
2217         }
2218         $this->assertTrue($found);
2220         // Test with get_updates_since the same data.
2221         $result = core_course_external::get_updates_since($course->id, $since);
2222         $result = external_api::clean_returnvalue(core_course_external::get_updates_since_returns(), $result);
2223         $this->assertCount(1, $result['instances']);
2224         $this->assertCount(0, $result['warnings']);
2225         $found = false;
2226         $this->assertCount(1, $result['instances']);
2227         $this->assertCount(0, $result['warnings']);
2228         foreach ($result['instances'] as $module) {
2229             foreach ($module['updates'] as $update) {
2230                 if ($module['id'] == $modules['forum']['cm']->id and $update['name'] == 'configuration') {
2231                     $found = true;
2232                 }
2233             }
2234         }
2235         $this->assertTrue($found);
2237         // Do not retrieve the configuration field.
2238         $filter = array('files');
2239         $found = false;
2240         $result = core_course_external::check_updates($course->id, $params, $filter);
2241         $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result);
2242         $this->assertCount(0, $result['instances']);
2243         $this->assertCount(0, $result['warnings']);
2244         $this->assertFalse($found);
2246         // Add invalid cmid.
2247         $params[] = array(
2248             'contextlevel' => 'module',
2249             'id' => -2,
2250             'since' => $since
2251         );
2252         $result = core_course_external::check_updates($course->id, $params);
2253         $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result);
2254         $this->assertCount(1, $result['warnings']);
2255         $this->assertEquals(-2, $result['warnings'][0]['itemid']);
2256     }