MDL-52811 course: fix unit tests broken by setforcedlanguage
[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 numsections
366      */
367     public function test_create_course_numsections() {
368         global $DB;
370         $this->resetAfterTest(true);
372         // Set the required capabilities by the external function.
373         $contextid = context_system::instance()->id;
374         $roleid = $this->assignUserCapability('moodle/course:create', $contextid);
375         $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
377         $numsections = 10;
378         $category  = self::getDataGenerator()->create_category();
380         // Create base categories.
381         $course1['fullname'] = 'Test course 1';
382         $course1['shortname'] = 'Testcourse1';
383         $course1['categoryid'] = $category->id;
384         $course1['courseformatoptions'][] = array('name' => 'numsections', 'value' => $numsections);
386         $courses = array($course1);
388         $createdcourses = core_course_external::create_courses($courses);
389         foreach ($createdcourses as $createdcourse) {
390             $existingsections = $DB->get_records('course_sections', array('course' => $createdcourse['id']));
391             $modinfo = get_fast_modinfo($createdcourse['id']);
392             $sections = $modinfo->get_section_info_all();
393             $this->assertEquals(count($sections), $numsections + 1); // Includes generic section.
394             $this->assertEquals(count($existingsections), $numsections + 1); // Includes generic section.
395         }
396     }
398     /**
399      * Test create_courses
400      */
401     public function test_create_courses() {
402         global $DB;
404         $this->resetAfterTest(true);
406         // Enable course completion.
407         set_config('enablecompletion', 1);
408         // Enable course themes.
409         set_config('allowcoursethemes', 1);
411         // Set the required capabilities by the external function
412         $contextid = context_system::instance()->id;
413         $roleid = $this->assignUserCapability('moodle/course:create', $contextid);
414         $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
415         $this->assignUserCapability('moodle/course:setforcedlanguage', $contextid, $roleid);
417         $category  = self::getDataGenerator()->create_category();
419         // Create base categories.
420         $course1['fullname'] = 'Test course 1';
421         $course1['shortname'] = 'Testcourse1';
422         $course1['categoryid'] = $category->id;
423         $course2['fullname'] = 'Test course 2';
424         $course2['shortname'] = 'Testcourse2';
425         $course2['categoryid'] = $category->id;
426         $course2['idnumber'] = 'testcourse2idnumber';
427         $course2['summary'] = 'Description for course 2';
428         $course2['summaryformat'] = FORMAT_MOODLE;
429         $course2['format'] = 'weeks';
430         $course2['showgrades'] = 1;
431         $course2['newsitems'] = 3;
432         $course2['startdate'] = 1420092000; // 01/01/2015.
433         $course2['enddate'] = 1422669600; // 01/31/2015.
434         $course2['numsections'] = 4;
435         $course2['maxbytes'] = 100000;
436         $course2['showreports'] = 1;
437         $course2['visible'] = 0;
438         $course2['hiddensections'] = 0;
439         $course2['groupmode'] = 0;
440         $course2['groupmodeforce'] = 0;
441         $course2['defaultgroupingid'] = 0;
442         $course2['enablecompletion'] = 1;
443         $course2['completionnotify'] = 1;
444         $course2['lang'] = 'en';
445         $course2['forcetheme'] = 'bootstrapbase';
446         $course2['courseformatoptions'][] = array('name' => 'automaticenddate', 'value' => 0);
447         $course3['fullname'] = 'Test course 3';
448         $course3['shortname'] = 'Testcourse3';
449         $course3['categoryid'] = $category->id;
450         $course3['format'] = 'topics';
451         $course3options = array('numsections' => 8,
452             'hiddensections' => 1,
453             'coursedisplay' => 1);
454         $course3['courseformatoptions'] = array();
455         foreach ($course3options as $key => $value) {
456             $course3['courseformatoptions'][] = array('name' => $key, 'value' => $value);
457         }
458         $courses = array($course1, $course2, $course3);
460         $createdcourses = core_course_external::create_courses($courses);
462         // We need to execute the return values cleaning process to simulate the web service server.
463         $createdcourses = external_api::clean_returnvalue(core_course_external::create_courses_returns(), $createdcourses);
465         // Check that right number of courses were created.
466         $this->assertEquals(3, count($createdcourses));
468         // Check that the courses were correctly created.
469         foreach ($createdcourses as $createdcourse) {
470             $courseinfo = course_get_format($createdcourse['id'])->get_course();
472             if ($createdcourse['shortname'] == $course2['shortname']) {
473                 $this->assertEquals($courseinfo->fullname, $course2['fullname']);
474                 $this->assertEquals($courseinfo->shortname, $course2['shortname']);
475                 $this->assertEquals($courseinfo->category, $course2['categoryid']);
476                 $this->assertEquals($courseinfo->idnumber, $course2['idnumber']);
477                 $this->assertEquals($courseinfo->summary, $course2['summary']);
478                 $this->assertEquals($courseinfo->summaryformat, $course2['summaryformat']);
479                 $this->assertEquals($courseinfo->format, $course2['format']);
480                 $this->assertEquals($courseinfo->showgrades, $course2['showgrades']);
481                 $this->assertEquals($courseinfo->newsitems, $course2['newsitems']);
482                 $this->assertEquals($courseinfo->startdate, $course2['startdate']);
483                 $this->assertEquals($courseinfo->enddate, $course2['enddate']);
484                 $this->assertEquals(course_get_format($createdcourse['id'])->get_last_section_number(), $course2['numsections']);
485                 $this->assertEquals($courseinfo->maxbytes, $course2['maxbytes']);
486                 $this->assertEquals($courseinfo->showreports, $course2['showreports']);
487                 $this->assertEquals($courseinfo->visible, $course2['visible']);
488                 $this->assertEquals($courseinfo->hiddensections, $course2['hiddensections']);
489                 $this->assertEquals($courseinfo->groupmode, $course2['groupmode']);
490                 $this->assertEquals($courseinfo->groupmodeforce, $course2['groupmodeforce']);
491                 $this->assertEquals($courseinfo->defaultgroupingid, $course2['defaultgroupingid']);
492                 $this->assertEquals($courseinfo->completionnotify, $course2['completionnotify']);
493                 $this->assertEquals($courseinfo->lang, $course2['lang']);
494                 $this->assertEquals($courseinfo->theme, $course2['forcetheme']);
496                 // We enabled completion at the beginning of the test.
497                 $this->assertEquals($courseinfo->enablecompletion, $course2['enablecompletion']);
499             } else if ($createdcourse['shortname'] == $course1['shortname']) {
500                 $courseconfig = get_config('moodlecourse');
501                 $this->assertEquals($courseinfo->fullname, $course1['fullname']);
502                 $this->assertEquals($courseinfo->shortname, $course1['shortname']);
503                 $this->assertEquals($courseinfo->category, $course1['categoryid']);
504                 $this->assertEquals($courseinfo->summaryformat, FORMAT_HTML);
505                 $this->assertEquals($courseinfo->format, $courseconfig->format);
506                 $this->assertEquals($courseinfo->showgrades, $courseconfig->showgrades);
507                 $this->assertEquals($courseinfo->newsitems, $courseconfig->newsitems);
508                 $this->assertEquals($courseinfo->maxbytes, $courseconfig->maxbytes);
509                 $this->assertEquals($courseinfo->showreports, $courseconfig->showreports);
510                 $this->assertEquals($courseinfo->groupmode, $courseconfig->groupmode);
511                 $this->assertEquals($courseinfo->groupmodeforce, $courseconfig->groupmodeforce);
512                 $this->assertEquals($courseinfo->defaultgroupingid, 0);
513             } else if ($createdcourse['shortname'] == $course3['shortname']) {
514                 $this->assertEquals($courseinfo->fullname, $course3['fullname']);
515                 $this->assertEquals($courseinfo->shortname, $course3['shortname']);
516                 $this->assertEquals($courseinfo->category, $course3['categoryid']);
517                 $this->assertEquals($courseinfo->format, $course3['format']);
518                 $this->assertEquals($courseinfo->hiddensections, $course3options['hiddensections']);
519                 $this->assertEquals(course_get_format($createdcourse['id'])->get_last_section_number(),
520                     $course3options['numsections']);
521                 $this->assertEquals($courseinfo->coursedisplay, $course3options['coursedisplay']);
522             } else {
523                 throw new moodle_exception('Unexpected shortname');
524             }
525         }
527         // Call without required capability
528         $this->unassignUserCapability('moodle/course:create', $contextid, $roleid);
529         $this->expectException('required_capability_exception');
530         $createdsubcats = core_course_external::create_courses($courses);
531     }
533     /**
534      * Test delete_courses
535      */
536     public function test_delete_courses() {
537         global $DB, $USER;
539         $this->resetAfterTest(true);
541         // Admin can delete a course.
542         $this->setAdminUser();
543         // Validate_context() will fail as the email is not set by $this->setAdminUser().
544         $USER->email = 'emailtopass@example.com';
546         $course1  = self::getDataGenerator()->create_course();
547         $course2  = self::getDataGenerator()->create_course();
548         $course3  = self::getDataGenerator()->create_course();
550         // Delete courses.
551         $result = core_course_external::delete_courses(array($course1->id, $course2->id));
552         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
553         // Check for 0 warnings.
554         $this->assertEquals(0, count($result['warnings']));
556         // Check $course 1 and 2 are deleted.
557         $notdeletedcount = $DB->count_records_select('course',
558             'id IN ( ' . $course1->id . ',' . $course2->id . ')');
559         $this->assertEquals(0, $notdeletedcount);
561         // Try to delete non-existent course.
562         $result = core_course_external::delete_courses(array($course1->id));
563         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
564         // Check for 1 warnings.
565         $this->assertEquals(1, count($result['warnings']));
567         // Try to delete Frontpage course.
568         $result = core_course_external::delete_courses(array(0));
569         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
570         // Check for 1 warnings.
571         $this->assertEquals(1, count($result['warnings']));
573          // Fail when the user has access to course (enrolled) but does not have permission or is not admin.
574         $student1 = self::getDataGenerator()->create_user();
575         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
576         $this->getDataGenerator()->enrol_user($student1->id,
577                                               $course3->id,
578                                               $studentrole->id);
579         $this->setUser($student1);
580         $result = core_course_external::delete_courses(array($course3->id));
581         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
582         // Check for 1 warnings.
583         $this->assertEquals(1, count($result['warnings']));
585          // Fail when the user is not allow to access the course (enrolled) or is not admin.
586         $this->setGuestUser();
587         $this->expectException('require_login_exception');
589         $result = core_course_external::delete_courses(array($course3->id));
590         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
591     }
593     /**
594      * Test get_courses
595      */
596     public function test_get_courses () {
597         global $DB;
599         $this->resetAfterTest(true);
601         $generatedcourses = array();
602         $coursedata['idnumber'] = 'idnumbercourse1';
603         // Adding tags here to check that format_string is applied.
604         $coursedata['fullname'] = '<b>Course 1 for PHPunit test</b>';
605         $coursedata['shortname'] = '<b>Course 1 for PHPunit test</b>';
606         $coursedata['summary'] = 'Course 1 description';
607         $coursedata['summaryformat'] = FORMAT_MOODLE;
608         $course1  = self::getDataGenerator()->create_course($coursedata);
610         $generatedcourses[$course1->id] = $course1;
611         $course2  = self::getDataGenerator()->create_course();
612         $generatedcourses[$course2->id] = $course2;
613         $course3  = self::getDataGenerator()->create_course(array('format' => 'topics'));
614         $generatedcourses[$course3->id] = $course3;
616         // Set the required capabilities by the external function.
617         $context = context_system::instance();
618         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
619         $this->assignUserCapability('moodle/course:update',
620                 context_course::instance($course1->id)->id, $roleid);
621         $this->assignUserCapability('moodle/course:update',
622                 context_course::instance($course2->id)->id, $roleid);
623         $this->assignUserCapability('moodle/course:update',
624                 context_course::instance($course3->id)->id, $roleid);
626         $courses = core_course_external::get_courses(array('ids' =>
627             array($course1->id, $course2->id)));
629         // We need to execute the return values cleaning process to simulate the web service server.
630         $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
632         // Check we retrieve the good total number of categories.
633         $this->assertEquals(2, count($courses));
635         foreach ($courses as $course) {
636             $coursecontext = context_course::instance($course['id']);
637             $dbcourse = $generatedcourses[$course['id']];
638             $this->assertEquals($course['idnumber'], $dbcourse->idnumber);
639             $this->assertEquals($course['fullname'], external_format_string($dbcourse->fullname, $coursecontext->id));
640             $this->assertEquals($course['displayname'], external_format_string(get_course_display_name_for_list($dbcourse),
641                 $coursecontext->id));
642             // Summary was converted to the HTML format.
643             $this->assertEquals($course['summary'], format_text($dbcourse->summary, FORMAT_MOODLE, array('para' => false)));
644             $this->assertEquals($course['summaryformat'], FORMAT_HTML);
645             $this->assertEquals($course['shortname'], external_format_string($dbcourse->shortname, $coursecontext->id));
646             $this->assertEquals($course['categoryid'], $dbcourse->category);
647             $this->assertEquals($course['format'], $dbcourse->format);
648             $this->assertEquals($course['showgrades'], $dbcourse->showgrades);
649             $this->assertEquals($course['newsitems'], $dbcourse->newsitems);
650             $this->assertEquals($course['startdate'], $dbcourse->startdate);
651             $this->assertEquals($course['enddate'], $dbcourse->enddate);
652             $this->assertEquals($course['numsections'], course_get_format($dbcourse)->get_last_section_number());
653             $this->assertEquals($course['maxbytes'], $dbcourse->maxbytes);
654             $this->assertEquals($course['showreports'], $dbcourse->showreports);
655             $this->assertEquals($course['visible'], $dbcourse->visible);
656             $this->assertEquals($course['hiddensections'], $dbcourse->hiddensections);
657             $this->assertEquals($course['groupmode'], $dbcourse->groupmode);
658             $this->assertEquals($course['groupmodeforce'], $dbcourse->groupmodeforce);
659             $this->assertEquals($course['defaultgroupingid'], $dbcourse->defaultgroupingid);
660             $this->assertEquals($course['completionnotify'], $dbcourse->completionnotify);
661             $this->assertEquals($course['lang'], $dbcourse->lang);
662             $this->assertEquals($course['forcetheme'], $dbcourse->theme);
663             $this->assertEquals($course['enablecompletion'], $dbcourse->enablecompletion);
664             if ($dbcourse->format === 'topics') {
665                 $this->assertEquals($course['courseformatoptions'], array(
666                     array('name' => 'hiddensections', 'value' => $dbcourse->hiddensections),
667                     array('name' => 'coursedisplay', 'value' => $dbcourse->coursedisplay),
668                 ));
669             }
670         }
672         // Get all courses in the DB
673         $courses = core_course_external::get_courses(array());
675         // We need to execute the return values cleaning process to simulate the web service server.
676         $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
678         $this->assertEquals($DB->count_records('course'), count($courses));
679     }
681     /**
682      * Test get_courses without capability
683      */
684     public function test_get_courses_without_capability() {
685         $this->resetAfterTest(true);
687         $course1 = $this->getDataGenerator()->create_course();
688         $this->setUser($this->getDataGenerator()->create_user());
690         // No permissions are required to get the site course.
691         $courses = core_course_external::get_courses(array('ids' => [SITEID]));
692         $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
694         $this->assertEquals(1, count($courses));
695         $this->assertEquals('PHPUnit test site', $courses[0]['fullname']);
696         $this->assertEquals('site', $courses[0]['format']);
698         // Requesting course without being enrolled or capability to view it will throw an exception.
699         try {
700             core_course_external::get_courses(array('ids' => [$course1->id]));
701             $this->fail('Exception expected');
702         } catch (moodle_exception $e) {
703             $this->assertEquals(1, preg_match('/Course or activity not accessible. \(Not enrolled\)/', $e->getMessage()));
704         }
705     }
707     /**
708      * Test search_courses
709      */
710     public function test_search_courses () {
712         global $DB;
714         $this->resetAfterTest(true);
715         $this->setAdminUser();
716         $generatedcourses = array();
717         $coursedata1['fullname'] = 'FIRST COURSE';
718         $course1  = self::getDataGenerator()->create_course($coursedata1);
720         $page = new moodle_page();
721         $page->set_course($course1);
722         $page->blocks->add_blocks([BLOCK_POS_LEFT => ['news_items'], BLOCK_POS_RIGHT => []], 'course-view-*');
724         $coursedata2['fullname'] = 'SECOND COURSE';
725         $course2  = self::getDataGenerator()->create_course($coursedata2);
727         $page = new moodle_page();
728         $page->set_course($course2);
729         $page->blocks->add_blocks([BLOCK_POS_LEFT => ['news_items'], BLOCK_POS_RIGHT => []], 'course-view-*');
730         // Search by name.
731         $results = core_course_external::search_courses('search', 'FIRST');
732         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
733         $this->assertEquals($coursedata1['fullname'], $results['courses'][0]['fullname']);
734         $this->assertCount(1, $results['courses']);
736         // Create the forum.
737         $record = new stdClass();
738         $record->introformat = FORMAT_HTML;
739         $record->course = $course2->id;
740         // Set Aggregate type = Average of ratings.
741         $forum = self::getDataGenerator()->create_module('forum', $record);
743         // Search by module.
744         $results = core_course_external::search_courses('modulelist', 'forum');
745         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
746         $this->assertEquals(1, $results['total']);
748         // Enable coursetag option.
749         set_config('block_tags_showcoursetags', true);
750         // Add tag 'TAG-LABEL ON SECOND COURSE' to Course2.
751         core_tag_tag::set_item_tags('core', 'course', $course2->id, context_course::instance($course2->id),
752                 array('TAG-LABEL ON SECOND COURSE'));
753         $taginstance = $DB->get_record('tag_instance',
754                 array('itemtype' => 'course', 'itemid' => $course2->id), '*', MUST_EXIST);
755         // Search by tagid.
756         $results = core_course_external::search_courses('tagid', $taginstance->tagid);
757         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
758         $this->assertEquals($coursedata2['fullname'], $results['courses'][0]['fullname']);
760         // Search by block (use news_items default block).
761         $blockid = $DB->get_field('block', 'id', array('name' => 'news_items'));
762         $results = core_course_external::search_courses('blocklist', $blockid);
763         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
764         $this->assertEquals(2, $results['total']);
766         // Now as a normal user.
767         $user = self::getDataGenerator()->create_user();
769         // Add a 3rd, hidden, course we shouldn't see, even when enrolled as student.
770         $coursedata3['fullname'] = 'HIDDEN COURSE';
771         $coursedata3['visible'] = 0;
772         $course3  = self::getDataGenerator()->create_course($coursedata3);
773         $this->getDataGenerator()->enrol_user($user->id, $course3->id, 'student');
775         $this->getDataGenerator()->enrol_user($user->id, $course2->id, 'student');
776         $this->setUser($user);
778         $results = core_course_external::search_courses('search', 'FIRST');
779         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
780         $this->assertCount(1, $results['courses']);
781         $this->assertEquals(1, $results['total']);
782         $this->assertEquals($coursedata1['fullname'], $results['courses'][0]['fullname']);
784         // Check that we can see both without the limit to enrolled setting.
785         $results = core_course_external::search_courses('search', 'COURSE', 0, 0, array(), 0);
786         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
787         $this->assertCount(2, $results['courses']);
788         $this->assertEquals(2, $results['total']);
790         // Check that we only see our enrolled course when limiting.
791         $results = core_course_external::search_courses('search', 'COURSE', 0, 0, array(), 1);
792         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
793         $this->assertCount(1, $results['courses']);
794         $this->assertEquals(1, $results['total']);
795         $this->assertEquals($coursedata2['fullname'], $results['courses'][0]['fullname']);
797         // Search by block (use news_items default block). Should fail (only admins allowed).
798         $this->expectException('required_capability_exception');
799         $results = core_course_external::search_courses('blocklist', $blockid);
801     }
803     /**
804      * Create a course with contents
805      * @return array A list with the course object and course modules objects
806      */
807     private function prepare_get_course_contents_test() {
808         global $DB;
809         $course  = self::getDataGenerator()->create_course(['numsections' => 3]);
810         $forumdescription = 'This is the forum description';
811         $forum = $this->getDataGenerator()->create_module('forum',
812             array('course' => $course->id, 'intro' => $forumdescription),
813             array('showdescription' => true));
814         $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
815         $data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
816         $datacm = get_coursemodule_from_instance('page', $data->id);
817         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
818         $pagecm = get_coursemodule_from_instance('page', $page->id);
819         $labeldescription = 'This is a very long label to test if more than 50 characters are returned.
820                 So bla bla bla bla <b>bold bold bold</b> bla bla bla bla.';
821         $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id,
822             'intro' => $labeldescription));
823         $labelcm = get_coursemodule_from_instance('label', $label->id);
824         $tomorrow = time() + DAYSECS;
825         // Module with availability restrictions not met.
826         $url = $this->getDataGenerator()->create_module('url',
827             array('course' => $course->id, 'name' => 'URL: % & $ ../', 'section' => 2),
828             array('availability' => '{"op":"&","c":[{"type":"date","d":">=","t":' . $tomorrow . '}],"showc":[true]}'));
829         $urlcm = get_coursemodule_from_instance('url', $url->id);
830         // Module for the last section.
831         $this->getDataGenerator()->create_module('url',
832             array('course' => $course->id, 'name' => 'URL for last section', 'section' => 3));
833         // Module for section 1 with availability restrictions met.
834         $yesterday = time() - DAYSECS;
835         $this->getDataGenerator()->create_module('url',
836             array('course' => $course->id, 'name' => 'URL restrictions met', 'section' => 1),
837             array('availability' => '{"op":"&","c":[{"type":"date","d":">=","t":'. $yesterday .'}],"showc":[true]}'));
839         // Set the required capabilities by the external function.
840         $context = context_course::instance($course->id);
841         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
842         $this->assignUserCapability('moodle/course:update', $context->id, $roleid);
843         $this->assignUserCapability('mod/data:view', $context->id, $roleid);
845         $conditions = array('course' => $course->id, 'section' => 2);
846         $DB->set_field('course_sections', 'summary', 'Text with iframe <iframe src="https://moodle.org"></iframe>', $conditions);
848         // Add date availability condition not met for last section.
849         $availability = '{"op":"&","c":[{"type":"date","d":">=","t":' . $tomorrow . '}],"showc":[true]}';
850         $DB->set_field('course_sections', 'availability', $availability,
851                 array('course' => $course->id, 'section' => 3));
852         rebuild_course_cache($course->id, true);
854         return array($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm);
855     }
857     /**
858      * Test get_course_contents
859      */
860     public function test_get_course_contents() {
861         $this->resetAfterTest(true);
863         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
865         $sections = core_course_external::get_course_contents($course->id, array());
866         // We need to execute the return values cleaning process to simulate the web service server.
867         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
869         $modinfo = get_fast_modinfo($course);
870         $testexecuted = 0;
871         foreach ($sections[0]['modules'] as $module) {
872             if ($module['id'] == $forumcm->id and $module['modname'] == 'forum') {
873                 $cm = $modinfo->cms[$forumcm->id];
874                 $formattedtext = format_text($cm->content, FORMAT_HTML,
875                     array('noclean' => true, 'para' => false, 'filter' => false));
876                 $this->assertEquals($formattedtext, $module['description']);
877                 $this->assertEquals($forumcm->instance, $module['instance']);
878                 $testexecuted = $testexecuted + 1;
879             } else if ($module['id'] == $labelcm->id and $module['modname'] == 'label') {
880                 $cm = $modinfo->cms[$labelcm->id];
881                 $formattedtext = format_text($cm->content, FORMAT_HTML,
882                     array('noclean' => true, 'para' => false, 'filter' => false));
883                 $this->assertEquals($formattedtext, $module['description']);
884                 $this->assertEquals($labelcm->instance, $module['instance']);
885                 $testexecuted = $testexecuted + 1;
886             }
887         }
888         $this->assertEquals(2, $testexecuted);
889         $this->assertEquals(0, $sections[0]['section']);
891         // Check that the only return section has the 5 created modules.
892         $this->assertCount(4, $sections[0]['modules']);
893         $this->assertCount(1, $sections[1]['modules']);
894         $this->assertCount(1, $sections[2]['modules']);
895         $this->assertCount(0, $sections[3]['modules']); // No modules for the section with availability restrictions.
896         $this->assertNotEmpty($sections[3]['availabilityinfo']);
897         $this->assertEquals(1, $sections[1]['section']);
898         $this->assertEquals(2, $sections[2]['section']);
899         $this->assertEquals(3, $sections[3]['section']);
900         $this->assertContains('<iframe', $sections[2]['summary']);
901         $this->assertContains('</iframe>', $sections[2]['summary']);
902         // The module with the availability restriction met is returning contents.
903         $this->assertNotEmpty($sections[1]['modules'][0]['contents']);
904         // The module with the availability restriction not met is not returning contents.
905         $this->assertArrayNotHasKey('contents', $sections[2]['modules'][0]);
906         $this->assertNotEmpty($sections[2]['modules'][0]['availabilityinfo']);
907         try {
908             $sections = core_course_external::get_course_contents($course->id,
909                                                                     array(array("name" => "invalid", "value" => 1)));
910             $this->fail('Exception expected due to invalid option.');
911         } catch (moodle_exception $e) {
912             $this->assertEquals('errorinvalidparam', $e->errorcode);
913         }
914     }
917     /**
918      * Test get_course_contents excluding modules
919      */
920     public function test_get_course_contents_excluding_modules() {
921         $this->resetAfterTest(true);
923         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
925         // Test exclude modules.
926         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludemodules", "value" => 1)));
928         // We need to execute the return values cleaning process to simulate the web service server.
929         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
931         $this->assertEmpty($sections[0]['modules']);
932         $this->assertEmpty($sections[1]['modules']);
933     }
935     /**
936      * Test get_course_contents excluding contents
937      */
938     public function test_get_course_contents_excluding_contents() {
939         $this->resetAfterTest(true);
941         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
943         // Test exclude modules.
944         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludecontents", "value" => 1)));
946         // We need to execute the return values cleaning process to simulate the web service server.
947         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
949         foreach ($sections as $section) {
950             foreach ($section['modules'] as $module) {
951                 // Only resources return contents.
952                 if (isset($module['contents'])) {
953                     $this->assertEmpty($module['contents']);
954                 }
955             }
956         }
957     }
959     /**
960      * Test get_course_contents filtering by section number
961      */
962     public function test_get_course_contents_section_number() {
963         $this->resetAfterTest(true);
965         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
967         // Test exclude modules.
968         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "sectionnumber", "value" => 0)));
970         // We need to execute the return values cleaning process to simulate the web service server.
971         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
973         $this->assertCount(1, $sections);
974         $this->assertCount(4, $sections[0]['modules']);
975     }
977     /**
978      * Test get_course_contents filtering by cmid
979      */
980     public function test_get_course_contents_cmid() {
981         $this->resetAfterTest(true);
983         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
985         // Test exclude modules.
986         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "cmid", "value" => $forumcm->id)));
988         // We need to execute the return values cleaning process to simulate the web service server.
989         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
991         $this->assertCount(4, $sections);
992         $this->assertCount(1, $sections[0]['modules']);
993         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
994     }
997     /**
998      * Test get_course_contents filtering by cmid and section
999      */
1000     public function test_get_course_contents_section_cmid() {
1001         $this->resetAfterTest(true);
1003         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
1005         // Test exclude modules.
1006         $sections = core_course_external::get_course_contents($course->id, array(
1007                                                                         array("name" => "cmid", "value" => $forumcm->id),
1008                                                                         array("name" => "sectionnumber", "value" => 0)
1009                                                                         ));
1011         // We need to execute the return values cleaning process to simulate the web service server.
1012         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
1014         $this->assertCount(1, $sections);
1015         $this->assertCount(1, $sections[0]['modules']);
1016         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
1017     }
1019     /**
1020      * Test get_course_contents filtering by modname
1021      */
1022     public function test_get_course_contents_modname() {
1023         $this->resetAfterTest(true);
1025         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
1027         // Test exclude modules.
1028         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "modname", "value" => "forum")));
1030         // We need to execute the return values cleaning process to simulate the web service server.
1031         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
1033         $this->assertCount(4, $sections);
1034         $this->assertCount(1, $sections[0]['modules']);
1035         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
1036     }
1038     /**
1039      * Test get_course_contents filtering by modname
1040      */
1041     public function test_get_course_contents_modid() {
1042         $this->resetAfterTest(true);
1044         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
1046         // Test exclude modules.
1047         $sections = core_course_external::get_course_contents($course->id, array(
1048                                                                             array("name" => "modname", "value" => "page"),
1049                                                                             array("name" => "modid", "value" => $pagecm->instance),
1050                                                                             ));
1052         // We need to execute the return values cleaning process to simulate the web service server.
1053         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
1055         $this->assertCount(4, $sections);
1056         $this->assertCount(1, $sections[0]['modules']);
1057         $this->assertEquals("page", $sections[0]['modules'][0]["modname"]);
1058         $this->assertEquals($pagecm->instance, $sections[0]['modules'][0]["instance"]);
1059     }
1061     /**
1062      * Test duplicate_course
1063      */
1064     public function test_duplicate_course() {
1065         $this->resetAfterTest(true);
1067         // Create one course with three modules.
1068         $course  = self::getDataGenerator()->create_course();
1069         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
1070         $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
1071         $forumcontext = context_module::instance($forum->cmid);
1072         $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
1073         $datacontext = context_module::instance($data->cmid);
1074         $datacm = get_coursemodule_from_instance('page', $data->id);
1075         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
1076         $pagecontext = context_module::instance($page->cmid);
1077         $pagecm = get_coursemodule_from_instance('page', $page->id);
1079         // Set the required capabilities by the external function.
1080         $coursecontext = context_course::instance($course->id);
1081         $categorycontext = context_coursecat::instance($course->category);
1082         $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id);
1083         $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid);
1084         $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid);
1085         $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid);
1086         $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid);
1087         // Optional capabilities to copy user data.
1088         $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid);
1089         $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid);
1091         $newcourse['fullname'] = 'Course duplicate';
1092         $newcourse['shortname'] = 'courseduplicate';
1093         $newcourse['categoryid'] = $course->category;
1094         $newcourse['visible'] = true;
1095         $newcourse['options'][] = array('name' => 'users', 'value' => true);
1097         $duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'],
1098                 $newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']);
1100         // We need to execute the return values cleaning process to simulate the web service server.
1101         $duplicate = external_api::clean_returnvalue(core_course_external::duplicate_course_returns(), $duplicate);
1103         // Check that the course has been duplicated.
1104         $this->assertEquals($newcourse['shortname'], $duplicate['shortname']);
1105     }
1107     /**
1108      * Test update_courses
1109      */
1110     public function test_update_courses() {
1111         global $DB, $CFG, $USER, $COURSE;
1113         // Get current $COURSE to be able to restore it later (defaults to $SITE). We need this
1114         // trick because we are both updating and getting (for testing) course information
1115         // in the same request and core_course_external::update_courses()
1116         // is overwriting $COURSE all over the time with OLD values, so later
1117         // use of get_course() fetches those OLD values instead of the updated ones.
1118         // See MDL-39723 for more info.
1119         $origcourse = clone($COURSE);
1121         $this->resetAfterTest(true);
1123         // Set the required capabilities by the external function.
1124         $contextid = context_system::instance()->id;
1125         $roleid = $this->assignUserCapability('moodle/course:update', $contextid);
1126         $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1127         $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1128         $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1129         $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1130         $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1131         $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
1132         $this->assignUserCapability('moodle/course:viewhiddencourses', $contextid, $roleid);
1133         $this->assignUserCapability('moodle/course:setforcedlanguage', $contextid, $roleid);
1135         // Create category and course.
1136         $category1  = self::getDataGenerator()->create_category();
1137         $category2  = self::getDataGenerator()->create_category();
1138         $originalcourse1 = self::getDataGenerator()->create_course();
1139         self::getDataGenerator()->enrol_user($USER->id, $originalcourse1->id, $roleid);
1140         $originalcourse2 = self::getDataGenerator()->create_course();
1141         self::getDataGenerator()->enrol_user($USER->id, $originalcourse2->id, $roleid);
1143         // Course values to be updated.
1144         $course1['id'] = $originalcourse1->id;
1145         $course1['fullname'] = 'Updated test course 1';
1146         $course1['shortname'] = 'Udestedtestcourse1';
1147         $course1['categoryid'] = $category1->id;
1148         $course2['id'] = $originalcourse2->id;
1149         $course2['fullname'] = 'Updated test course 2';
1150         $course2['shortname'] = 'Updestedtestcourse2';
1151         $course2['categoryid'] = $category2->id;
1152         $course2['idnumber'] = 'Updatedidnumber2';
1153         $course2['summary'] = 'Updaated description for course 2';
1154         $course2['summaryformat'] = FORMAT_HTML;
1155         $course2['format'] = 'topics';
1156         $course2['showgrades'] = 1;
1157         $course2['newsitems'] = 3;
1158         $course2['startdate'] = 1420092000; // 01/01/2015.
1159         $course2['enddate'] = 1422669600; // 01/31/2015.
1160         $course2['maxbytes'] = 100000;
1161         $course2['showreports'] = 1;
1162         $course2['visible'] = 0;
1163         $course2['hiddensections'] = 0;
1164         $course2['groupmode'] = 0;
1165         $course2['groupmodeforce'] = 0;
1166         $course2['defaultgroupingid'] = 0;
1167         $course2['enablecompletion'] = 1;
1168         $course2['lang'] = 'en';
1169         $course2['forcetheme'] = 'bootstrapbase';
1170         $courses = array($course1, $course2);
1172         $updatedcoursewarnings = core_course_external::update_courses($courses);
1173         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1174                                                                     $updatedcoursewarnings);
1175         $COURSE = $origcourse; // Restore $COURSE. Instead of using the OLD one set by the previous line.
1177         // Check that right number of courses were created.
1178         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1180         // Check that the courses were correctly created.
1181         foreach ($courses as $course) {
1182             $courseinfo = course_get_format($course['id'])->get_course();
1183             if ($course['id'] == $course2['id']) {
1184                 $this->assertEquals($course2['fullname'], $courseinfo->fullname);
1185                 $this->assertEquals($course2['shortname'], $courseinfo->shortname);
1186                 $this->assertEquals($course2['categoryid'], $courseinfo->category);
1187                 $this->assertEquals($course2['idnumber'], $courseinfo->idnumber);
1188                 $this->assertEquals($course2['summary'], $courseinfo->summary);
1189                 $this->assertEquals($course2['summaryformat'], $courseinfo->summaryformat);
1190                 $this->assertEquals($course2['format'], $courseinfo->format);
1191                 $this->assertEquals($course2['showgrades'], $courseinfo->showgrades);
1192                 $this->assertEquals($course2['newsitems'], $courseinfo->newsitems);
1193                 $this->assertEquals($course2['startdate'], $courseinfo->startdate);
1194                 $this->assertEquals($course2['enddate'], $courseinfo->enddate);
1195                 $this->assertEquals($course2['maxbytes'], $courseinfo->maxbytes);
1196                 $this->assertEquals($course2['showreports'], $courseinfo->showreports);
1197                 $this->assertEquals($course2['visible'], $courseinfo->visible);
1198                 $this->assertEquals($course2['hiddensections'], $courseinfo->hiddensections);
1199                 $this->assertEquals($course2['groupmode'], $courseinfo->groupmode);
1200                 $this->assertEquals($course2['groupmodeforce'], $courseinfo->groupmodeforce);
1201                 $this->assertEquals($course2['defaultgroupingid'], $courseinfo->defaultgroupingid);
1202                 $this->assertEquals($course2['lang'], $courseinfo->lang);
1204                 if (!empty($CFG->allowcoursethemes)) {
1205                     $this->assertEquals($course2['forcetheme'], $courseinfo->theme);
1206                 }
1208                 $this->assertEquals($course2['enablecompletion'], $courseinfo->enablecompletion);
1209             } else if ($course['id'] == $course1['id']) {
1210                 $this->assertEquals($course1['fullname'], $courseinfo->fullname);
1211                 $this->assertEquals($course1['shortname'], $courseinfo->shortname);
1212                 $this->assertEquals($course1['categoryid'], $courseinfo->category);
1213                 $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
1214                 $this->assertEquals('topics', $courseinfo->format);
1215                 $this->assertEquals(5, course_get_format($course['id'])->get_last_section_number());
1216                 $this->assertEquals(0, $courseinfo->newsitems);
1217                 $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
1218             } else {
1219                 throw new moodle_exception('Unexpected shortname');
1220             }
1221         }
1223         $courses = array($course1);
1224         // Try update course without update capability.
1225         $user = self::getDataGenerator()->create_user();
1226         $this->setUser($user);
1227         $this->unassignUserCapability('moodle/course:update', $contextid, $roleid);
1228         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1229         $updatedcoursewarnings = core_course_external::update_courses($courses);
1230         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1231                                                                     $updatedcoursewarnings);
1232         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1234         // Try update course category without capability.
1235         $this->assignUserCapability('moodle/course:update', $contextid, $roleid);
1236         $this->unassignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1237         $user = self::getDataGenerator()->create_user();
1238         $this->setUser($user);
1239         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1240         $course1['categoryid'] = $category2->id;
1241         $courses = array($course1);
1242         $updatedcoursewarnings = core_course_external::update_courses($courses);
1243         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1244                                                                     $updatedcoursewarnings);
1245         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1247         // Try update course fullname without capability.
1248         $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1249         $this->unassignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1250         $user = self::getDataGenerator()->create_user();
1251         $this->setUser($user);
1252         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1253         $updatedcoursewarnings = core_course_external::update_courses($courses);
1254         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1255                                                                     $updatedcoursewarnings);
1256         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1257         $course1['fullname'] = 'Testing fullname without permission';
1258         $courses = array($course1);
1259         $updatedcoursewarnings = core_course_external::update_courses($courses);
1260         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1261                                                                     $updatedcoursewarnings);
1262         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1264         // Try update course shortname without capability.
1265         $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1266         $this->unassignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1267         $user = self::getDataGenerator()->create_user();
1268         $this->setUser($user);
1269         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1270         $updatedcoursewarnings = core_course_external::update_courses($courses);
1271         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1272                                                                     $updatedcoursewarnings);
1273         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1274         $course1['shortname'] = 'Testing shortname without permission';
1275         $courses = array($course1);
1276         $updatedcoursewarnings = core_course_external::update_courses($courses);
1277         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1278                                                                     $updatedcoursewarnings);
1279         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1281         // Try update course idnumber without capability.
1282         $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1283         $this->unassignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1284         $user = self::getDataGenerator()->create_user();
1285         $this->setUser($user);
1286         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1287         $updatedcoursewarnings = core_course_external::update_courses($courses);
1288         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1289                                                                     $updatedcoursewarnings);
1290         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1291         $course1['idnumber'] = 'NEWIDNUMBER';
1292         $courses = array($course1);
1293         $updatedcoursewarnings = core_course_external::update_courses($courses);
1294         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1295                                                                     $updatedcoursewarnings);
1296         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1298         // Try update course summary without capability.
1299         $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1300         $this->unassignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1301         $user = self::getDataGenerator()->create_user();
1302         $this->setUser($user);
1303         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1304         $updatedcoursewarnings = core_course_external::update_courses($courses);
1305         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1306                                                                     $updatedcoursewarnings);
1307         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1308         $course1['summary'] = 'New summary';
1309         $courses = array($course1);
1310         $updatedcoursewarnings = core_course_external::update_courses($courses);
1311         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1312                                                                     $updatedcoursewarnings);
1313         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1315         // Try update course with invalid summary format.
1316         $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1317         $user = self::getDataGenerator()->create_user();
1318         $this->setUser($user);
1319         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1320         $updatedcoursewarnings = core_course_external::update_courses($courses);
1321         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1322                                                                     $updatedcoursewarnings);
1323         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1324         $course1['summaryformat'] = 10;
1325         $courses = array($course1);
1326         $updatedcoursewarnings = core_course_external::update_courses($courses);
1327         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1328                                                                     $updatedcoursewarnings);
1329         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1331         // Try update course visibility without capability.
1332         $this->unassignUserCapability('moodle/course:visibility', $contextid, $roleid);
1333         $user = self::getDataGenerator()->create_user();
1334         $this->setUser($user);
1335         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1336         $course1['summaryformat'] = FORMAT_MOODLE;
1337         $courses = array($course1);
1338         $updatedcoursewarnings = core_course_external::update_courses($courses);
1339         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1340                                                                     $updatedcoursewarnings);
1341         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1342         $course1['visible'] = 0;
1343         $courses = array($course1);
1344         $updatedcoursewarnings = core_course_external::update_courses($courses);
1345         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1346                                                                     $updatedcoursewarnings);
1347         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1348     }
1350     /**
1351      * Test delete course_module.
1352      */
1353     public function test_delete_modules() {
1354         global $DB;
1356         // Ensure we reset the data after this test.
1357         $this->resetAfterTest(true);
1359         // Create a user.
1360         $user = self::getDataGenerator()->create_user();
1362         // Set the tests to run as the user.
1363         self::setUser($user);
1365         // Create a course to add the modules.
1366         $course = self::getDataGenerator()->create_course();
1368         // Create two test modules.
1369         $record = new stdClass();
1370         $record->course = $course->id;
1371         $module1 = self::getDataGenerator()->create_module('forum', $record);
1372         $module2 = self::getDataGenerator()->create_module('assign', $record);
1374         // Check the forum was correctly created.
1375         $this->assertEquals(1, $DB->count_records('forum', array('id' => $module1->id)));
1377         // Check the assignment was correctly created.
1378         $this->assertEquals(1, $DB->count_records('assign', array('id' => $module2->id)));
1380         // Check data exists in the course modules table.
1381         $this->assertEquals(2, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
1382                 array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
1384         // Enrol the user in the course.
1385         $enrol = enrol_get_plugin('manual');
1386         $enrolinstances = enrol_get_instances($course->id, true);
1387         foreach ($enrolinstances as $courseenrolinstance) {
1388             if ($courseenrolinstance->enrol == "manual") {
1389                 $instance = $courseenrolinstance;
1390                 break;
1391             }
1392         }
1393         $enrol->enrol_user($instance, $user->id);
1395         // Assign capabilities to delete module 1.
1396         $modcontext = context_module::instance($module1->cmid);
1397         $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id);
1399         // Assign capabilities to delete module 2.
1400         $modcontext = context_module::instance($module2->cmid);
1401         $newrole = create_role('Role 2', 'role2', 'Role 2 description');
1402         $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id, $newrole);
1404         // Deleting these module instances.
1405         core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1407         // Check the forum was deleted.
1408         $this->assertEquals(0, $DB->count_records('forum', array('id' => $module1->id)));
1410         // Check the assignment was deleted.
1411         $this->assertEquals(0, $DB->count_records('assign', array('id' => $module2->id)));
1413         // Check we retrieve no data in the course modules table.
1414         $this->assertEquals(0, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
1415                 array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
1417         // Call with non-existent course module id and ensure exception thrown.
1418         try {
1419             core_course_external::delete_modules(array('1337'));
1420             $this->fail('Exception expected due to missing course module.');
1421         } catch (dml_missing_record_exception $e) {
1422             $this->assertEquals('invalidcoursemodule', $e->errorcode);
1423         }
1425         // Create two modules.
1426         $module1 = self::getDataGenerator()->create_module('forum', $record);
1427         $module2 = self::getDataGenerator()->create_module('assign', $record);
1429         // Since these modules were recreated the user will not have capabilities
1430         // to delete them, ensure exception is thrown if they try.
1431         try {
1432             core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1433             $this->fail('Exception expected due to missing capability.');
1434         } catch (moodle_exception $e) {
1435             $this->assertEquals('nopermissions', $e->errorcode);
1436         }
1438         // Unenrol user from the course.
1439         $enrol->unenrol_user($instance, $user->id);
1441         // Try and delete modules from the course the user was unenrolled in, make sure exception thrown.
1442         try {
1443             core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1444             $this->fail('Exception expected due to being unenrolled from the course.');
1445         } catch (moodle_exception $e) {
1446             $this->assertEquals('requireloginerror', $e->errorcode);
1447         }
1448     }
1450     /**
1451      * Test import_course into an empty course
1452      */
1453     public function test_import_course_empty() {
1454         global $USER;
1456         $this->resetAfterTest(true);
1458         $course1  = self::getDataGenerator()->create_course();
1459         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course1->id, 'name' => 'Forum test'));
1460         $page = $this->getDataGenerator()->create_module('page', array('course' => $course1->id, 'name' => 'Page test'));
1462         $course2  = self::getDataGenerator()->create_course();
1464         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1465         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1467         // Verify the state of the courses before we do the import.
1468         $this->assertCount(2, $course1cms);
1469         $this->assertEmpty($course2cms);
1471         // Setup the user to run the operation (ugly hack because validate_context() will
1472         // fail as the email is not set by $this->setAdminUser()).
1473         $this->setAdminUser();
1474         $USER->email = 'emailtopass@example.com';
1476         // Import from course1 to course2.
1477         core_course_external::import_course($course1->id, $course2->id, 0);
1479         // Verify that now we have two modules in both courses.
1480         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1481         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1482         $this->assertCount(2, $course1cms);
1483         $this->assertCount(2, $course2cms);
1485         // Verify that the names transfered across correctly.
1486         foreach ($course2cms as $cm) {
1487             if ($cm->modname === 'page') {
1488                 $this->assertEquals($cm->name, $page->name);
1489             } else if ($cm->modname === 'forum') {
1490                 $this->assertEquals($cm->name, $forum->name);
1491             } else {
1492                 $this->fail('Unknown CM found.');
1493             }
1494         }
1495     }
1497     /**
1498      * Test import_course into an filled course
1499      */
1500     public function test_import_course_filled() {
1501         global $USER;
1503         $this->resetAfterTest(true);
1505         // Add forum and page to course1.
1506         $course1  = self::getDataGenerator()->create_course();
1507         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1508         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
1510         // Add quiz to course 2.
1511         $course2  = self::getDataGenerator()->create_course();
1512         $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
1514         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1515         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1517         // Verify the state of the courses before we do the import.
1518         $this->assertCount(2, $course1cms);
1519         $this->assertCount(1, $course2cms);
1521         // Setup the user to run the operation (ugly hack because validate_context() will
1522         // fail as the email is not set by $this->setAdminUser()).
1523         $this->setAdminUser();
1524         $USER->email = 'emailtopass@example.com';
1526         // Import from course1 to course2 without deleting content.
1527         core_course_external::import_course($course1->id, $course2->id, 0);
1529         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1531         // Verify that now we have three modules in course2.
1532         $this->assertCount(3, $course2cms);
1534         // Verify that the names transfered across correctly.
1535         foreach ($course2cms as $cm) {
1536             if ($cm->modname === 'page') {
1537                 $this->assertEquals($cm->name, $page->name);
1538             } else if ($cm->modname === 'forum') {
1539                 $this->assertEquals($cm->name, $forum->name);
1540             } else if ($cm->modname === 'quiz') {
1541                 $this->assertEquals($cm->name, $quiz->name);
1542             } else {
1543                 $this->fail('Unknown CM found.');
1544             }
1545         }
1546     }
1548     /**
1549      * Test import_course with only blocks set to backup
1550      */
1551     public function test_import_course_blocksonly() {
1552         global $USER, $DB;
1554         $this->resetAfterTest(true);
1556         // Add forum and page to course1.
1557         $course1  = self::getDataGenerator()->create_course();
1558         $course1ctx = context_course::instance($course1->id);
1559         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1560         $block = $this->getDataGenerator()->create_block('online_users', array('parentcontextid' => $course1ctx->id));
1562         $course2  = self::getDataGenerator()->create_course();
1563         $course2ctx = context_course::instance($course2->id);
1564         $initialblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
1565         $initialcmcount = count(get_fast_modinfo($course2->id)->get_cms());
1567         // Setup the user to run the operation (ugly hack because validate_context() will
1568         // fail as the email is not set by $this->setAdminUser()).
1569         $this->setAdminUser();
1570         $USER->email = 'emailtopass@example.com';
1572         // Import from course1 to course2 without deleting content, but excluding
1573         // activities.
1574         $options = array(
1575             array('name' => 'activities', 'value' => 0),
1576             array('name' => 'blocks', 'value' => 1),
1577             array('name' => 'filters', 'value' => 0),
1578         );
1580         core_course_external::import_course($course1->id, $course2->id, 0, $options);
1582         $newcmcount = count(get_fast_modinfo($course2->id)->get_cms());
1583         $newblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
1584         // Check that course modules haven't changed, but that blocks have.
1585         $this->assertEquals($initialcmcount, $newcmcount);
1586         $this->assertEquals(($initialblockcount + 1), $newblockcount);
1587     }
1589     /**
1590      * Test import_course into an filled course, deleting content.
1591      */
1592     public function test_import_course_deletecontent() {
1593         global $USER;
1594         $this->resetAfterTest(true);
1596         // Add forum and page to course1.
1597         $course1  = self::getDataGenerator()->create_course();
1598         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1599         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
1601         // Add quiz to course 2.
1602         $course2  = self::getDataGenerator()->create_course();
1603         $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
1605         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1606         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1608         // Verify the state of the courses before we do the import.
1609         $this->assertCount(2, $course1cms);
1610         $this->assertCount(1, $course2cms);
1612         // Setup the user to run the operation (ugly hack because validate_context() will
1613         // fail as the email is not set by $this->setAdminUser()).
1614         $this->setAdminUser();
1615         $USER->email = 'emailtopass@example.com';
1617         // Import from course1 to course2,  deleting content.
1618         core_course_external::import_course($course1->id, $course2->id, 1);
1620         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1622         // Verify that now we have two modules in course2.
1623         $this->assertCount(2, $course2cms);
1625         // Verify that the course only contains the imported modules.
1626         foreach ($course2cms as $cm) {
1627             if ($cm->modname === 'page') {
1628                 $this->assertEquals($cm->name, $page->name);
1629             } else if ($cm->modname === 'forum') {
1630                 $this->assertEquals($cm->name, $forum->name);
1631             } else {
1632                 $this->fail('Unknown CM found: '.$cm->name);
1633             }
1634         }
1635     }
1637     /**
1638      * Ensure import_course handles incorrect deletecontent option correctly.
1639      */
1640     public function test_import_course_invalid_deletecontent_option() {
1641         $this->resetAfterTest(true);
1643         $course1  = self::getDataGenerator()->create_course();
1644         $course2  = self::getDataGenerator()->create_course();
1646         $this->expectException('moodle_exception');
1647         $this->expectExceptionMessage(get_string('invalidextparam', 'webservice', -1));
1648         // Import from course1 to course2, with invalid option
1649         core_course_external::import_course($course1->id, $course2->id, -1);;
1650     }
1652     /**
1653      * Test view_course function
1654      */
1655     public function test_view_course() {
1657         $this->resetAfterTest();
1659         // Course without sections.
1660         $course = $this->getDataGenerator()->create_course(array('numsections' => 5), array('createsections' => true));
1661         $this->setAdminUser();
1663         // Redirect events to the sink, so we can recover them later.
1664         $sink = $this->redirectEvents();
1666         $result = core_course_external::view_course($course->id, 1);
1667         $result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result);
1668         $events = $sink->get_events();
1669         $event = reset($events);
1671         // Check the event details are correct.
1672         $this->assertInstanceOf('\core\event\course_viewed', $event);
1673         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1674         $this->assertEquals(1, $event->other['coursesectionnumber']);
1676         $result = core_course_external::view_course($course->id);
1677         $result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result);
1678         $events = $sink->get_events();
1679         $event = array_pop($events);
1680         $sink->close();
1682         // Check the event details are correct.
1683         $this->assertInstanceOf('\core\event\course_viewed', $event);
1684         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1685         $this->assertEmpty($event->other);
1687     }
1689     /**
1690      * Test get_course_module
1691      */
1692     public function test_get_course_module() {
1693         global $DB;
1695         $this->resetAfterTest(true);
1697         $this->setAdminUser();
1698         $course = self::getDataGenerator()->create_course();
1699         $record = array(
1700             'course' => $course->id,
1701             'name' => 'First Assignment'
1702         );
1703         $options = array(
1704             'idnumber' => 'ABC',
1705             'visible' => 0
1706         );
1707         // Hidden activity.
1708         $assign = self::getDataGenerator()->create_module('assign', $record, $options);
1710         $outcomescale = 'Distinction, Very Good, Good, Pass, Fail';
1712         // Insert a custom grade scale to be used by an outcome.
1713         $gradescale = new grade_scale();
1714         $gradescale->name        = 'gettcoursemodulescale';
1715         $gradescale->courseid    = $course->id;
1716         $gradescale->userid      = 0;
1717         $gradescale->scale       = $outcomescale;
1718         $gradescale->description = 'This scale is used to mark standard assignments.';
1719         $gradescale->insert();
1721         // Insert an outcome.
1722         $data = new stdClass();
1723         $data->courseid = $course->id;
1724         $data->fullname = 'Team work';
1725         $data->shortname = 'Team work';
1726         $data->scaleid = $gradescale->id;
1727         $outcome = new grade_outcome($data, false);
1728         $outcome->insert();
1730         $outcomegradeitem = new grade_item();
1731         $outcomegradeitem->itemname = $outcome->shortname;
1732         $outcomegradeitem->itemtype = 'mod';
1733         $outcomegradeitem->itemmodule = 'assign';
1734         $outcomegradeitem->iteminstance = $assign->id;
1735         $outcomegradeitem->outcomeid = $outcome->id;
1736         $outcomegradeitem->cmid = 0;
1737         $outcomegradeitem->courseid = $course->id;
1738         $outcomegradeitem->aggregationcoef = 0;
1739         $outcomegradeitem->itemnumber = 1; // The activity's original grade item will be 0.
1740         $outcomegradeitem->gradetype = GRADE_TYPE_SCALE;
1741         $outcomegradeitem->scaleid = $outcome->scaleid;
1742         $outcomegradeitem->insert();
1744         $assignmentgradeitem = grade_item::fetch(
1745             array(
1746                 'itemtype' => 'mod',
1747                 'itemmodule' => 'assign',
1748                 'iteminstance' => $assign->id,
1749                 'itemnumber' => 0,
1750                 'courseid' => $course->id
1751             )
1752         );
1753         $outcomegradeitem->set_parent($assignmentgradeitem->categoryid);
1754         $outcomegradeitem->move_after_sortorder($assignmentgradeitem->sortorder);
1756         // Test admin user can see the complete hidden activity.
1757         $result = core_course_external::get_course_module($assign->cmid);
1758         $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result);
1760         $this->assertCount(0, $result['warnings']);
1761         // Test we retrieve all the fields.
1762         $this->assertCount(28, $result['cm']);
1763         $this->assertEquals($record['name'], $result['cm']['name']);
1764         $this->assertEquals($options['idnumber'], $result['cm']['idnumber']);
1765         $this->assertEquals(100, $result['cm']['grade']);
1766         $this->assertEquals(0.0, $result['cm']['gradepass']);
1767         $this->assertEquals('submissions', $result['cm']['advancedgrading'][0]['area']);
1768         $this->assertEmpty($result['cm']['advancedgrading'][0]['method']);
1769         $this->assertEquals($outcomescale, $result['cm']['outcomes'][0]['scale']);
1771         $student = $this->getDataGenerator()->create_user();
1772         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1774         self::getDataGenerator()->enrol_user($student->id,  $course->id, $studentrole->id);
1775         $this->setUser($student);
1777         // The user shouldn't be able to see the activity.
1778         try {
1779             core_course_external::get_course_module($assign->cmid);
1780             $this->fail('Exception expected due to invalid permissions.');
1781         } catch (moodle_exception $e) {
1782             $this->assertEquals('requireloginerror', $e->errorcode);
1783         }
1785         // Make module visible.
1786         set_coursemodule_visible($assign->cmid, 1);
1788         // Test student user.
1789         $result = core_course_external::get_course_module($assign->cmid);
1790         $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result);
1792         $this->assertCount(0, $result['warnings']);
1793         // Test we retrieve only the few files we can see.
1794         $this->assertCount(11, $result['cm']);
1795         $this->assertEquals($assign->cmid, $result['cm']['id']);
1796         $this->assertEquals($course->id, $result['cm']['course']);
1797         $this->assertEquals('assign', $result['cm']['modname']);
1798         $this->assertEquals($assign->id, $result['cm']['instance']);
1800     }
1802     /**
1803      * Test get_course_module_by_instance
1804      */
1805     public function test_get_course_module_by_instance() {
1806         global $DB;
1808         $this->resetAfterTest(true);
1810         $this->setAdminUser();
1811         $course = self::getDataGenerator()->create_course();
1812         $record = array(
1813             'course' => $course->id,
1814             'name' => 'First quiz',
1815             'grade' => 90.00
1816         );
1817         $options = array(
1818             'idnumber' => 'ABC',
1819             'visible' => 0
1820         );
1821         // Hidden activity.
1822         $quiz = self::getDataGenerator()->create_module('quiz', $record, $options);
1824         // Test admin user can see the complete hidden activity.
1825         $result = core_course_external::get_course_module_by_instance('quiz', $quiz->id);
1826         $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result);
1828         $this->assertCount(0, $result['warnings']);
1829         // Test we retrieve all the fields.
1830         $this->assertCount(26, $result['cm']);
1831         $this->assertEquals($record['name'], $result['cm']['name']);
1832         $this->assertEquals($record['grade'], $result['cm']['grade']);
1833         $this->assertEquals($options['idnumber'], $result['cm']['idnumber']);
1835         $student = $this->getDataGenerator()->create_user();
1836         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1838         self::getDataGenerator()->enrol_user($student->id,  $course->id, $studentrole->id);
1839         $this->setUser($student);
1841         // The user shouldn't be able to see the activity.
1842         try {
1843             core_course_external::get_course_module_by_instance('quiz', $quiz->id);
1844             $this->fail('Exception expected due to invalid permissions.');
1845         } catch (moodle_exception $e) {
1846             $this->assertEquals('requireloginerror', $e->errorcode);
1847         }
1849         // Make module visible.
1850         set_coursemodule_visible($quiz->cmid, 1);
1852         // Test student user.
1853         $result = core_course_external::get_course_module_by_instance('quiz', $quiz->id);
1854         $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result);
1856         $this->assertCount(0, $result['warnings']);
1857         // Test we retrieve only the few files we can see.
1858         $this->assertCount(11, $result['cm']);
1859         $this->assertEquals($quiz->cmid, $result['cm']['id']);
1860         $this->assertEquals($course->id, $result['cm']['course']);
1861         $this->assertEquals('quiz', $result['cm']['modname']);
1862         $this->assertEquals($quiz->id, $result['cm']['instance']);
1864         // Try with an invalid module name.
1865         try {
1866             core_course_external::get_course_module_by_instance('abc', $quiz->id);
1867             $this->fail('Exception expected due to invalid module name.');
1868         } catch (dml_read_exception $e) {
1869             $this->assertEquals('dmlreadexception', $e->errorcode);
1870         }
1872     }
1874     /**
1875      * Test get_activities_overview
1876      */
1877     public function test_get_activities_overview() {
1878         global $USER;
1880         $this->resetAfterTest();
1881         $course1 = self::getDataGenerator()->create_course();
1882         $course2 = self::getDataGenerator()->create_course();
1884         // Create a viewer user.
1885         $viewer = self::getDataGenerator()->create_user((object) array('trackforums' => 1));
1886         $this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
1887         $this->getDataGenerator()->enrol_user($viewer->id, $course2->id);
1889         // Create two forums - one in each course.
1890         $record = new stdClass();
1891         $record->course = $course1->id;
1892         $forum1 = self::getDataGenerator()->create_module('forum', (object) array('course' => $course1->id));
1893         $forum2 = self::getDataGenerator()->create_module('forum', (object) array('course' => $course2->id));
1895         $this->setAdminUser();
1896         // A standard post in the forum.
1897         $record = new stdClass();
1898         $record->course = $course1->id;
1899         $record->userid = $USER->id;
1900         $record->forum = $forum1->id;
1901         $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1903         $this->setUser($viewer->id);
1904         $courses = array($course1->id , $course2->id);
1906         $result = core_course_external::get_activities_overview($courses);
1907         $this->assertDebuggingCalledCount(8);
1908         $result = external_api::clean_returnvalue(core_course_external::get_activities_overview_returns(), $result);
1910         // There should be one entry for course1, and no others.
1911         $this->assertCount(1, $result['courses']);
1912         $this->assertEquals($course1->id, $result['courses'][0]['id']);
1913         // Check expected overview data for the module.
1914         $this->assertEquals('forum', $result['courses'][0]['overviews'][0]['module']);
1915         $this->assertContains('1 total unread', $result['courses'][0]['overviews'][0]['overviewtext']);
1916     }
1918     /**
1919      * Test get_user_navigation_options
1920      */
1921     public function test_get_user_navigation_options() {
1922         global $USER;
1924         $this->resetAfterTest();
1925         $course1 = self::getDataGenerator()->create_course();
1926         $course2 = self::getDataGenerator()->create_course();
1928         // Create a viewer user.
1929         $viewer = self::getDataGenerator()->create_user();
1930         $this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
1931         $this->getDataGenerator()->enrol_user($viewer->id, $course2->id);
1933         $this->setUser($viewer->id);
1934         $courses = array($course1->id , $course2->id, SITEID);
1936         $result = core_course_external::get_user_navigation_options($courses);
1937         $result = external_api::clean_returnvalue(core_course_external::get_user_navigation_options_returns(), $result);
1939         $this->assertCount(0, $result['warnings']);
1940         $this->assertCount(3, $result['courses']);
1942         foreach ($result['courses'] as $course) {
1943             $navoptions = new stdClass;
1944             foreach ($course['options'] as $option) {
1945                 $navoptions->{$option['name']} = $option['available'];
1946             }
1947             $this->assertCount(9, $course['options']);
1948             if ($course['id'] == SITEID) {
1949                 $this->assertTrue($navoptions->blogs);
1950                 $this->assertFalse($navoptions->notes);
1951                 $this->assertFalse($navoptions->participants);
1952                 $this->assertTrue($navoptions->badges);
1953                 $this->assertTrue($navoptions->tags);
1954                 $this->assertFalse($navoptions->grades);
1955                 $this->assertFalse($navoptions->search);
1956                 $this->assertTrue($navoptions->calendar);
1957                 $this->assertTrue($navoptions->competencies);
1958             } else {
1959                 $this->assertTrue($navoptions->blogs);
1960                 $this->assertFalse($navoptions->notes);
1961                 $this->assertTrue($navoptions->participants);
1962                 $this->assertTrue($navoptions->badges);
1963                 $this->assertFalse($navoptions->tags);
1964                 $this->assertTrue($navoptions->grades);
1965                 $this->assertFalse($navoptions->search);
1966                 $this->assertFalse($navoptions->calendar);
1967                 $this->assertTrue($navoptions->competencies);
1968             }
1969         }
1970     }
1972     /**
1973      * Test get_user_administration_options
1974      */
1975     public function test_get_user_administration_options() {
1976         global $USER;
1978         $this->resetAfterTest();
1979         $course1 = self::getDataGenerator()->create_course();
1980         $course2 = self::getDataGenerator()->create_course();
1982         // Create a viewer user.
1983         $viewer = self::getDataGenerator()->create_user();
1984         $this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
1985         $this->getDataGenerator()->enrol_user($viewer->id, $course2->id);
1987         $this->setUser($viewer->id);
1988         $courses = array($course1->id , $course2->id, SITEID);
1990         $result = core_course_external::get_user_administration_options($courses);
1991         $result = external_api::clean_returnvalue(core_course_external::get_user_administration_options_returns(), $result);
1993         $this->assertCount(0, $result['warnings']);
1994         $this->assertCount(3, $result['courses']);
1996         foreach ($result['courses'] as $course) {
1997             $adminoptions = new stdClass;
1998             foreach ($course['options'] as $option) {
1999                 $adminoptions->{$option['name']} = $option['available'];
2000             }
2001             if ($course['id'] == SITEID) {
2002                 $this->assertCount(16, $course['options']);
2003                 $this->assertFalse($adminoptions->update);
2004                 $this->assertFalse($adminoptions->filters);
2005                 $this->assertFalse($adminoptions->reports);
2006                 $this->assertFalse($adminoptions->backup);
2007                 $this->assertFalse($adminoptions->restore);
2008                 $this->assertFalse($adminoptions->files);
2009                 $this->assertFalse(!isset($adminoptions->tags));
2010                 $this->assertFalse($adminoptions->gradebook);
2011                 $this->assertFalse($adminoptions->outcomes);
2012                 $this->assertFalse($adminoptions->badges);
2013                 $this->assertFalse($adminoptions->import);
2014                 $this->assertFalse($adminoptions->publish);
2015                 $this->assertFalse($adminoptions->reset);
2016                 $this->assertFalse($adminoptions->roles);
2017                 $this->assertFalse($adminoptions->editcompletion);
2018             } else {
2019                 $this->assertCount(15, $course['options']);
2020                 $this->assertFalse($adminoptions->update);
2021                 $this->assertFalse($adminoptions->filters);
2022                 $this->assertFalse($adminoptions->reports);
2023                 $this->assertFalse($adminoptions->backup);
2024                 $this->assertFalse($adminoptions->restore);
2025                 $this->assertFalse($adminoptions->files);
2026                 $this->assertFalse($adminoptions->tags);
2027                 $this->assertFalse($adminoptions->gradebook);
2028                 $this->assertFalse($adminoptions->outcomes);
2029                 $this->assertTrue($adminoptions->badges);
2030                 $this->assertFalse($adminoptions->import);
2031                 $this->assertFalse($adminoptions->publish);
2032                 $this->assertFalse($adminoptions->reset);
2033                 $this->assertFalse($adminoptions->roles);
2034                 $this->assertFalse($adminoptions->editcompletion);
2035             }
2036         }
2037     }
2039     /**
2040      * Test get_courses_by_fields
2041      */
2042     public function test_get_courses_by_field() {
2043         global $DB;
2044         $this->resetAfterTest(true);
2046         $category1 = self::getDataGenerator()->create_category();
2047         $category2 = self::getDataGenerator()->create_category(array('parent' => $category1->id));
2048         $course1 = self::getDataGenerator()->create_course(array('category' => $category1->id, 'shortname' => 'c1'));
2049         $course2 = self::getDataGenerator()->create_course(array('visible' => 0, 'category' => $category2->id, 'idnumber' => 'i2'));
2051         $student1 = self::getDataGenerator()->create_user();
2052         $user1 = self::getDataGenerator()->create_user();
2053         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
2054         self::getDataGenerator()->enrol_user($student1->id, $course1->id, $studentrole->id);
2055         self::getDataGenerator()->enrol_user($student1->id, $course2->id, $studentrole->id);
2057         self::setAdminUser();
2058         // As admins, we should be able to retrieve everything.
2059         $result = core_course_external::get_courses_by_field();
2060         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2061         $this->assertCount(3, $result['courses']);
2062         // Expect to receive all the fields.
2063         $this->assertCount(37, $result['courses'][0]);
2064         $this->assertCount(37, $result['courses'][1]);
2065         $this->assertCount(37, $result['courses'][2]);
2067         $result = core_course_external::get_courses_by_field('id', $course1->id);
2068         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2069         $this->assertCount(1, $result['courses']);
2070         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2071         // Expect to receive all the fields.
2072         $this->assertCount(37, $result['courses'][0]);
2074         $result = core_course_external::get_courses_by_field('id', $course2->id);
2075         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2076         $this->assertCount(1, $result['courses']);
2077         $this->assertEquals($course2->id, $result['courses'][0]['id']);
2079         $result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id");
2080         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2081         $this->assertCount(2, $result['courses']);
2083         // Check default filters.
2084         $this->assertCount(3, $result['courses'][0]['filters']);
2085         $this->assertCount(3, $result['courses'][1]['filters']);
2087         $result = core_course_external::get_courses_by_field('category', $category1->id);
2088         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2089         $this->assertCount(1, $result['courses']);
2090         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2092         $result = core_course_external::get_courses_by_field('shortname', 'c1');
2093         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2094         $this->assertCount(1, $result['courses']);
2095         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2097         $result = core_course_external::get_courses_by_field('idnumber', 'i2');
2098         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2099         $this->assertCount(1, $result['courses']);
2100         $this->assertEquals($course2->id, $result['courses'][0]['id']);
2102         $result = core_course_external::get_courses_by_field('idnumber', 'x');
2103         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2104         $this->assertCount(0, $result['courses']);
2106         // Change filter value.
2107         filter_set_local_state('mediaplugin', context_course::instance($course1->id)->id, TEXTFILTER_OFF);
2109         self::setUser($student1);
2110         // All visible courses  (including front page) for normal student.
2111         $result = core_course_external::get_courses_by_field();
2112         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2113         $this->assertCount(2, $result['courses']);
2114         $this->assertCount(30, $result['courses'][0]);
2115         $this->assertCount(30, $result['courses'][1]);
2117         $result = core_course_external::get_courses_by_field('id', $course1->id);
2118         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2119         $this->assertCount(1, $result['courses']);
2120         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2121         // Expect to receive all the files that a student can see.
2122         $this->assertCount(30, $result['courses'][0]);
2124         // Check default filters.
2125         $filters = $result['courses'][0]['filters'];
2126         $this->assertCount(3, $filters);
2127         $found = false;
2128         foreach ($filters as $filter) {
2129             if ($filter['filter'] == 'mediaplugin' and $filter['localstate'] == TEXTFILTER_OFF) {
2130                 $found = true;
2131             }
2132         }
2133         $this->assertTrue($found);
2135         // Course 2 is not visible.
2136         $result = core_course_external::get_courses_by_field('id', $course2->id);
2137         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2138         $this->assertCount(0, $result['courses']);
2140         $result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id");
2141         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2142         $this->assertCount(1, $result['courses']);
2144         $result = core_course_external::get_courses_by_field('category', $category1->id);
2145         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2146         $this->assertCount(1, $result['courses']);
2147         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2149         $result = core_course_external::get_courses_by_field('shortname', 'c1');
2150         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2151         $this->assertCount(1, $result['courses']);
2152         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2154         $result = core_course_external::get_courses_by_field('idnumber', 'i2');
2155         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2156         $this->assertCount(0, $result['courses']);
2158         $result = core_course_external::get_courses_by_field('idnumber', 'x');
2159         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2160         $this->assertCount(0, $result['courses']);
2162         self::setUser($user1);
2163         // All visible courses (including front page) for authenticated user.
2164         $result = core_course_external::get_courses_by_field();
2165         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2166         $this->assertCount(2, $result['courses']);
2167         $this->assertCount(30, $result['courses'][0]);  // Site course.
2168         $this->assertCount(13, $result['courses'][1]);  // Only public information, not enrolled.
2170         $result = core_course_external::get_courses_by_field('id', $course1->id);
2171         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2172         $this->assertCount(1, $result['courses']);
2173         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2174         // Expect to receive all the files that a authenticated can see.
2175         $this->assertCount(13, $result['courses'][0]);
2177         // Course 2 is not visible.
2178         $result = core_course_external::get_courses_by_field('id', $course2->id);
2179         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2180         $this->assertCount(0, $result['courses']);
2182         $result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id");
2183         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2184         $this->assertCount(1, $result['courses']);
2186         $result = core_course_external::get_courses_by_field('category', $category1->id);
2187         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2188         $this->assertCount(1, $result['courses']);
2189         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2191         $result = core_course_external::get_courses_by_field('shortname', 'c1');
2192         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2193         $this->assertCount(1, $result['courses']);
2194         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2196         $result = core_course_external::get_courses_by_field('idnumber', 'i2');
2197         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2198         $this->assertCount(0, $result['courses']);
2200         $result = core_course_external::get_courses_by_field('idnumber', 'x');
2201         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2202         $this->assertCount(0, $result['courses']);
2203     }
2205     public function test_get_courses_by_field_invalid_field() {
2206         $this->expectException('invalid_parameter_exception');
2207         $result = core_course_external::get_courses_by_field('zyx', 'x');
2208     }
2210     public function test_get_courses_by_field_invalid_courses() {
2211         $result = core_course_external::get_courses_by_field('id', '-1');
2212         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2213         $this->assertCount(0, $result['courses']);
2214     }
2216     /**
2217      * Test get_courses_by_field_invalid_theme_and_lang
2218      */
2219     public function test_get_courses_by_field_invalid_theme_and_lang() {
2220         $this->resetAfterTest(true);
2221         $this->setAdminUser();
2223         $course = self::getDataGenerator()->create_course(array('theme' => 'kkt', 'lang' => 'kkl'));
2224         $result = core_course_external::get_courses_by_field('id', $course->id);
2225         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2226         $this->assertEmpty($result['courses']['0']['theme']);
2227         $this->assertEmpty($result['courses']['0']['lang']);
2228     }
2231     public function test_check_updates() {
2232         global $DB;
2233         $this->resetAfterTest(true);
2234         $this->setAdminUser();
2236         // Create different types of activities.
2237         $course  = self::getDataGenerator()->create_course();
2238         $tocreate = array('assign', 'book', 'choice', 'folder', 'forum', 'glossary', 'imscp', 'label', 'lti', 'page', 'quiz',
2239                             'resource', 'scorm', 'survey', 'url', 'wiki');
2241         $modules = array();
2242         foreach ($tocreate as $modname) {
2243             $modules[$modname]['instance'] = $this->getDataGenerator()->create_module($modname, array('course' => $course->id));
2244             $modules[$modname]['cm'] = get_coursemodule_from_id(false, $modules[$modname]['instance']->cmid);
2245             $modules[$modname]['context'] = context_module::instance($modules[$modname]['instance']->cmid);
2246         }
2248         $student = self::getDataGenerator()->create_user();
2249         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
2250         self::getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
2251         $this->setUser($student);
2253         $since = time();
2254         $this->waitForSecond();
2255         $params = array();
2256         foreach ($modules as $modname => $data) {
2257             $params[$data['cm']->id] = array(
2258                 'contextlevel' => 'module',
2259                 'id' => $data['cm']->id,
2260                 'since' => $since
2261             );
2262         }
2264         // Check there is nothing updated because modules are fresh new.
2265         $result = core_course_external::check_updates($course->id, $params);
2266         $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result);
2267         $this->assertCount(0, $result['instances']);
2268         $this->assertCount(0, $result['warnings']);
2270         // Test with get_updates_since the same data.
2271         $result = core_course_external::get_updates_since($course->id, $since);
2272         $result = external_api::clean_returnvalue(core_course_external::get_updates_since_returns(), $result);
2273         $this->assertCount(0, $result['instances']);
2274         $this->assertCount(0, $result['warnings']);
2276         // Update a module after a second.
2277         $this->waitForSecond();
2278         set_coursemodule_name($modules['forum']['cm']->id, 'New forum name');
2280         $found = false;
2281         $result = core_course_external::check_updates($course->id, $params);
2282         $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result);
2283         $this->assertCount(1, $result['instances']);
2284         $this->assertCount(0, $result['warnings']);
2285         foreach ($result['instances'] as $module) {
2286             foreach ($module['updates'] as $update) {
2287                 if ($module['id'] == $modules['forum']['cm']->id and $update['name'] == 'configuration') {
2288                     $found = true;
2289                 }
2290             }
2291         }
2292         $this->assertTrue($found);
2294         // Test with get_updates_since the same data.
2295         $result = core_course_external::get_updates_since($course->id, $since);
2296         $result = external_api::clean_returnvalue(core_course_external::get_updates_since_returns(), $result);
2297         $this->assertCount(1, $result['instances']);
2298         $this->assertCount(0, $result['warnings']);
2299         $found = false;
2300         $this->assertCount(1, $result['instances']);
2301         $this->assertCount(0, $result['warnings']);
2302         foreach ($result['instances'] as $module) {
2303             foreach ($module['updates'] as $update) {
2304                 if ($module['id'] == $modules['forum']['cm']->id and $update['name'] == 'configuration') {
2305                     $found = true;
2306                 }
2307             }
2308         }
2309         $this->assertTrue($found);
2311         // Do not retrieve the configuration field.
2312         $filter = array('files');
2313         $found = false;
2314         $result = core_course_external::check_updates($course->id, $params, $filter);
2315         $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result);
2316         $this->assertCount(0, $result['instances']);
2317         $this->assertCount(0, $result['warnings']);
2318         $this->assertFalse($found);
2320         // Add invalid cmid.
2321         $params[] = array(
2322             'contextlevel' => 'module',
2323             'id' => -2,
2324             'since' => $since
2325         );
2326         $result = core_course_external::check_updates($course->id, $params);
2327         $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result);
2328         $this->assertCount(1, $result['warnings']);
2329         $this->assertEquals(-2, $result['warnings'][0]['itemid']);
2330     }