d991ac7c22d4f549f9c6d59d6f2ff8c491026ad8
[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         // Call without required capability (it will fail cause of the search on idnumber).
282         $this->unassignUserCapability('moodle/category:manage', $context->id, $roleid);
283         $this->expectException('moodle_exception');
284         $categories = core_course_external::get_categories(array(
285             array('key' => 'id', 'value' => $category1->id),
286             array('key' => 'idnumber', 'value' => $category1->idnumber),
287             array('key' => 'visible', 'value' => 1)), 0);
288     }
290     /**
291      * Test update_categories
292      */
293     public function test_update_categories() {
294         global $DB;
296         $this->resetAfterTest(true);
298         // Set the required capabilities by the external function
299         $contextid = context_system::instance()->id;
300         $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
302         // Create base categories.
303         $category1data['idnumber'] = 'idnumbercat1';
304         $category1data['name'] = 'Category 1 for PHPunit test';
305         $category1data['description'] = 'Category 1 description';
306         $category1data['descriptionformat'] = FORMAT_MOODLE;
307         $category1  = self::getDataGenerator()->create_category($category1data);
308         $category2  = self::getDataGenerator()->create_category(
309                 array('parent' => $category1->id));
310         $category3  = self::getDataGenerator()->create_category();
311         $category4  = self::getDataGenerator()->create_category(
312                 array('parent' => $category3->id));
313         $category5  = self::getDataGenerator()->create_category(
314                 array('parent' => $category4->id));
316         // We update all category1 attribut.
317         // Then we move cat4 and cat5 parent: cat3 => cat1
318         $categories = array(
319             array('id' => $category1->id,
320                 'name' => $category1->name . '_updated',
321                 'idnumber' => $category1->idnumber . '_updated',
322                 'description' => $category1->description . '_updated',
323                 'descriptionformat' => FORMAT_HTML,
324                 'theme' => $category1->theme),
325             array('id' => $category4->id, 'parent' => $category1->id));
327         core_course_external::update_categories($categories);
329         // Check the values were updated.
330         $dbcategories = $DB->get_records_select('course_categories',
331                 'id IN (' . $category1->id . ',' . $category2->id . ',' . $category2->id
332                 . ',' . $category3->id . ',' . $category4->id . ',' . $category5->id .')');
333         $this->assertEquals($category1->name . '_updated',
334                 $dbcategories[$category1->id]->name);
335         $this->assertEquals($category1->idnumber . '_updated',
336                 $dbcategories[$category1->id]->idnumber);
337         $this->assertEquals($category1->description . '_updated',
338                 $dbcategories[$category1->id]->description);
339         $this->assertEquals(FORMAT_HTML, $dbcategories[$category1->id]->descriptionformat);
341         // Check that category4 and category5 have been properly moved.
342         $this->assertEquals('/' . $category1->id . '/' . $category4->id,
343                 $dbcategories[$category4->id]->path);
344         $this->assertEquals('/' . $category1->id . '/' . $category4->id . '/' . $category5->id,
345                 $dbcategories[$category5->id]->path);
347         // Call without required capability.
348         $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
349         $this->expectException('required_capability_exception');
350         core_course_external::update_categories($categories);
351     }
353     /**
354      * Test create_courses
355      */
356     public function test_create_courses() {
357         global $DB;
359         $this->resetAfterTest(true);
361         // Enable course completion.
362         set_config('enablecompletion', 1);
363         // Enable course themes.
364         set_config('allowcoursethemes', 1);
366         // Set the required capabilities by the external function
367         $contextid = context_system::instance()->id;
368         $roleid = $this->assignUserCapability('moodle/course:create', $contextid);
369         $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
371         $category  = self::getDataGenerator()->create_category();
373         // Create base categories.
374         $course1['fullname'] = 'Test course 1';
375         $course1['shortname'] = 'Testcourse1';
376         $course1['categoryid'] = $category->id;
377         $course2['fullname'] = 'Test course 2';
378         $course2['shortname'] = 'Testcourse2';
379         $course2['categoryid'] = $category->id;
380         $course2['idnumber'] = 'testcourse2idnumber';
381         $course2['summary'] = 'Description for course 2';
382         $course2['summaryformat'] = FORMAT_MOODLE;
383         $course2['format'] = 'weeks';
384         $course2['showgrades'] = 1;
385         $course2['newsitems'] = 3;
386         $course2['startdate'] = 1420092000; // 01/01/2015.
387         $course2['enddate'] = 1422669600; // 01/31/2015.
388         $course2['numsections'] = 4;
389         $course2['maxbytes'] = 100000;
390         $course2['showreports'] = 1;
391         $course2['visible'] = 0;
392         $course2['hiddensections'] = 0;
393         $course2['groupmode'] = 0;
394         $course2['groupmodeforce'] = 0;
395         $course2['defaultgroupingid'] = 0;
396         $course2['enablecompletion'] = 1;
397         $course2['completionnotify'] = 1;
398         $course2['lang'] = 'en';
399         $course2['forcetheme'] = 'bootstrapbase';
400         $course3['fullname'] = 'Test course 3';
401         $course3['shortname'] = 'Testcourse3';
402         $course3['categoryid'] = $category->id;
403         $course3['format'] = 'topics';
404         $course3options = array('numsections' => 8,
405             'hiddensections' => 1,
406             'coursedisplay' => 1);
407         $course3['courseformatoptions'] = array();
408         foreach ($course3options as $key => $value) {
409             $course3['courseformatoptions'][] = array('name' => $key, 'value' => $value);
410         }
411         $courses = array($course1, $course2, $course3);
413         $createdcourses = core_course_external::create_courses($courses);
415         // We need to execute the return values cleaning process to simulate the web service server.
416         $createdcourses = external_api::clean_returnvalue(core_course_external::create_courses_returns(), $createdcourses);
418         // Check that right number of courses were created.
419         $this->assertEquals(3, count($createdcourses));
421         // Check that the courses were correctly created.
422         foreach ($createdcourses as $createdcourse) {
423             $courseinfo = course_get_format($createdcourse['id'])->get_course();
425             if ($createdcourse['shortname'] == $course2['shortname']) {
426                 $this->assertEquals($courseinfo->fullname, $course2['fullname']);
427                 $this->assertEquals($courseinfo->shortname, $course2['shortname']);
428                 $this->assertEquals($courseinfo->category, $course2['categoryid']);
429                 $this->assertEquals($courseinfo->idnumber, $course2['idnumber']);
430                 $this->assertEquals($courseinfo->summary, $course2['summary']);
431                 $this->assertEquals($courseinfo->summaryformat, $course2['summaryformat']);
432                 $this->assertEquals($courseinfo->format, $course2['format']);
433                 $this->assertEquals($courseinfo->showgrades, $course2['showgrades']);
434                 $this->assertEquals($courseinfo->newsitems, $course2['newsitems']);
435                 $this->assertEquals($courseinfo->startdate, $course2['startdate']);
436                 $this->assertEquals($courseinfo->enddate, $course2['enddate']);
437                 $this->assertEquals($courseinfo->numsections, $course2['numsections']);
438                 $this->assertEquals($courseinfo->maxbytes, $course2['maxbytes']);
439                 $this->assertEquals($courseinfo->showreports, $course2['showreports']);
440                 $this->assertEquals($courseinfo->visible, $course2['visible']);
441                 $this->assertEquals($courseinfo->hiddensections, $course2['hiddensections']);
442                 $this->assertEquals($courseinfo->groupmode, $course2['groupmode']);
443                 $this->assertEquals($courseinfo->groupmodeforce, $course2['groupmodeforce']);
444                 $this->assertEquals($courseinfo->defaultgroupingid, $course2['defaultgroupingid']);
445                 $this->assertEquals($courseinfo->completionnotify, $course2['completionnotify']);
446                 $this->assertEquals($courseinfo->lang, $course2['lang']);
447                 $this->assertEquals($courseinfo->theme, $course2['forcetheme']);
449                 // We enabled completion at the beginning of the test.
450                 $this->assertEquals($courseinfo->enablecompletion, $course2['enablecompletion']);
452             } else if ($createdcourse['shortname'] == $course1['shortname']) {
453                 $courseconfig = get_config('moodlecourse');
454                 $this->assertEquals($courseinfo->fullname, $course1['fullname']);
455                 $this->assertEquals($courseinfo->shortname, $course1['shortname']);
456                 $this->assertEquals($courseinfo->category, $course1['categoryid']);
457                 $this->assertEquals($courseinfo->summaryformat, FORMAT_HTML);
458                 $this->assertEquals($courseinfo->format, $courseconfig->format);
459                 $this->assertEquals($courseinfo->showgrades, $courseconfig->showgrades);
460                 $this->assertEquals($courseinfo->newsitems, $courseconfig->newsitems);
461                 $this->assertEquals($courseinfo->maxbytes, $courseconfig->maxbytes);
462                 $this->assertEquals($courseinfo->showreports, $courseconfig->showreports);
463                 $this->assertEquals($courseinfo->groupmode, $courseconfig->groupmode);
464                 $this->assertEquals($courseinfo->groupmodeforce, $courseconfig->groupmodeforce);
465                 $this->assertEquals($courseinfo->defaultgroupingid, 0);
466             } else if ($createdcourse['shortname'] == $course3['shortname']) {
467                 $this->assertEquals($courseinfo->fullname, $course3['fullname']);
468                 $this->assertEquals($courseinfo->shortname, $course3['shortname']);
469                 $this->assertEquals($courseinfo->category, $course3['categoryid']);
470                 $this->assertEquals($courseinfo->format, $course3['format']);
471                 $this->assertEquals($courseinfo->hiddensections, $course3options['hiddensections']);
472                 $this->assertEquals($courseinfo->numsections, $course3options['numsections']);
473                 $this->assertEquals($courseinfo->coursedisplay, $course3options['coursedisplay']);
474             } else {
475                 throw moodle_exception('Unexpected shortname');
476             }
477         }
479         // Call without required capability
480         $this->unassignUserCapability('moodle/course:create', $contextid, $roleid);
481         $this->expectException('required_capability_exception');
482         $createdsubcats = core_course_external::create_courses($courses);
483     }
485     /**
486      * Test delete_courses
487      */
488     public function test_delete_courses() {
489         global $DB, $USER;
491         $this->resetAfterTest(true);
493         // Admin can delete a course.
494         $this->setAdminUser();
495         // Validate_context() will fail as the email is not set by $this->setAdminUser().
496         $USER->email = 'emailtopass@example.com';
498         $course1  = self::getDataGenerator()->create_course();
499         $course2  = self::getDataGenerator()->create_course();
500         $course3  = self::getDataGenerator()->create_course();
502         // Delete courses.
503         $result = core_course_external::delete_courses(array($course1->id, $course2->id));
504         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
505         // Check for 0 warnings.
506         $this->assertEquals(0, count($result['warnings']));
508         // Check $course 1 and 2 are deleted.
509         $notdeletedcount = $DB->count_records_select('course',
510             'id IN ( ' . $course1->id . ',' . $course2->id . ')');
511         $this->assertEquals(0, $notdeletedcount);
513         // Try to delete non-existent course.
514         $result = core_course_external::delete_courses(array($course1->id));
515         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
516         // Check for 1 warnings.
517         $this->assertEquals(1, count($result['warnings']));
519         // Try to delete Frontpage course.
520         $result = core_course_external::delete_courses(array(0));
521         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
522         // Check for 1 warnings.
523         $this->assertEquals(1, count($result['warnings']));
525          // Fail when the user has access to course (enrolled) but does not have permission or is not admin.
526         $student1 = self::getDataGenerator()->create_user();
527         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
528         $this->getDataGenerator()->enrol_user($student1->id,
529                                               $course3->id,
530                                               $studentrole->id);
531         $this->setUser($student1);
532         $result = core_course_external::delete_courses(array($course3->id));
533         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
534         // Check for 1 warnings.
535         $this->assertEquals(1, count($result['warnings']));
537          // Fail when the user is not allow to access the course (enrolled) or is not admin.
538         $this->setGuestUser();
539         $this->expectException('require_login_exception');
541         $result = core_course_external::delete_courses(array($course3->id));
542         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
543     }
545     /**
546      * Test get_courses
547      */
548     public function test_get_courses () {
549         global $DB;
551         $this->resetAfterTest(true);
553         $generatedcourses = array();
554         $coursedata['idnumber'] = 'idnumbercourse1';
555         // Adding tags here to check that format_string is applied.
556         $coursedata['fullname'] = '<b>Course 1 for PHPunit test</b>';
557         $coursedata['shortname'] = '<b>Course 1 for PHPunit test</b>';
558         $coursedata['summary'] = 'Course 1 description';
559         $coursedata['summaryformat'] = FORMAT_MOODLE;
560         $course1  = self::getDataGenerator()->create_course($coursedata);
562         $generatedcourses[$course1->id] = $course1;
563         $course2  = self::getDataGenerator()->create_course();
564         $generatedcourses[$course2->id] = $course2;
565         $course3  = self::getDataGenerator()->create_course(array('format' => 'topics'));
566         $generatedcourses[$course3->id] = $course3;
568         // Set the required capabilities by the external function.
569         $context = context_system::instance();
570         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
571         $this->assignUserCapability('moodle/course:update',
572                 context_course::instance($course1->id)->id, $roleid);
573         $this->assignUserCapability('moodle/course:update',
574                 context_course::instance($course2->id)->id, $roleid);
575         $this->assignUserCapability('moodle/course:update',
576                 context_course::instance($course3->id)->id, $roleid);
578         $courses = core_course_external::get_courses(array('ids' =>
579             array($course1->id, $course2->id)));
581         // We need to execute the return values cleaning process to simulate the web service server.
582         $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
584         // Check we retrieve the good total number of categories.
585         $this->assertEquals(2, count($courses));
587         foreach ($courses as $course) {
588             $coursecontext = context_course::instance($course['id']);
589             $dbcourse = $generatedcourses[$course['id']];
590             $this->assertEquals($course['idnumber'], $dbcourse->idnumber);
591             $this->assertEquals($course['fullname'], external_format_string($dbcourse->fullname, $coursecontext->id));
592             $this->assertEquals($course['displayname'], external_format_string(get_course_display_name_for_list($dbcourse),
593                 $coursecontext->id));
594             // Summary was converted to the HTML format.
595             $this->assertEquals($course['summary'], format_text($dbcourse->summary, FORMAT_MOODLE, array('para' => false)));
596             $this->assertEquals($course['summaryformat'], FORMAT_HTML);
597             $this->assertEquals($course['shortname'], external_format_string($dbcourse->shortname, $coursecontext->id));
598             $this->assertEquals($course['categoryid'], $dbcourse->category);
599             $this->assertEquals($course['format'], $dbcourse->format);
600             $this->assertEquals($course['showgrades'], $dbcourse->showgrades);
601             $this->assertEquals($course['newsitems'], $dbcourse->newsitems);
602             $this->assertEquals($course['startdate'], $dbcourse->startdate);
603             $this->assertEquals($course['enddate'], $dbcourse->enddate);
604             $this->assertEquals($course['numsections'], $dbcourse->numsections);
605             $this->assertEquals($course['maxbytes'], $dbcourse->maxbytes);
606             $this->assertEquals($course['showreports'], $dbcourse->showreports);
607             $this->assertEquals($course['visible'], $dbcourse->visible);
608             $this->assertEquals($course['hiddensections'], $dbcourse->hiddensections);
609             $this->assertEquals($course['groupmode'], $dbcourse->groupmode);
610             $this->assertEquals($course['groupmodeforce'], $dbcourse->groupmodeforce);
611             $this->assertEquals($course['defaultgroupingid'], $dbcourse->defaultgroupingid);
612             $this->assertEquals($course['completionnotify'], $dbcourse->completionnotify);
613             $this->assertEquals($course['lang'], $dbcourse->lang);
614             $this->assertEquals($course['forcetheme'], $dbcourse->theme);
615             $this->assertEquals($course['enablecompletion'], $dbcourse->enablecompletion);
616             if ($dbcourse->format === 'topics') {
617                 $this->assertEquals($course['courseformatoptions'], array(
618                     array('name' => 'numsections', 'value' => $dbcourse->numsections),
619                     array('name' => 'hiddensections', 'value' => $dbcourse->hiddensections),
620                     array('name' => 'coursedisplay', 'value' => $dbcourse->coursedisplay),
621                 ));
622             }
623         }
625         // Get all courses in the DB
626         $courses = core_course_external::get_courses(array());
628         // We need to execute the return values cleaning process to simulate the web service server.
629         $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
631         $this->assertEquals($DB->count_records('course'), count($courses));
632     }
634     /**
635      * Test search_courses
636      */
637     public function test_search_courses () {
639         global $DB;
641         $this->resetAfterTest(true);
642         $this->setAdminUser();
643         $generatedcourses = array();
644         $coursedata1['fullname'] = 'FIRST COURSE';
645         $course1  = self::getDataGenerator()->create_course($coursedata1);
647         $page = new moodle_page();
648         $page->set_course($course1);
649         $page->blocks->add_blocks([BLOCK_POS_LEFT => ['news_items'], BLOCK_POS_RIGHT => []], 'course-view-*');
651         $coursedata2['fullname'] = 'SECOND COURSE';
652         $course2  = self::getDataGenerator()->create_course($coursedata2);
654         $page = new moodle_page();
655         $page->set_course($course2);
656         $page->blocks->add_blocks([BLOCK_POS_LEFT => ['news_items'], BLOCK_POS_RIGHT => []], 'course-view-*');
657         // Search by name.
658         $results = core_course_external::search_courses('search', 'FIRST');
659         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
660         $this->assertEquals($coursedata1['fullname'], $results['courses'][0]['fullname']);
661         $this->assertCount(1, $results['courses']);
663         // Create the forum.
664         $record = new stdClass();
665         $record->introformat = FORMAT_HTML;
666         $record->course = $course2->id;
667         // Set Aggregate type = Average of ratings.
668         $forum = self::getDataGenerator()->create_module('forum', $record);
670         // Search by module.
671         $results = core_course_external::search_courses('modulelist', 'forum');
672         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
673         $this->assertEquals(1, $results['total']);
675         // Enable coursetag option.
676         set_config('block_tags_showcoursetags', true);
677         // Add tag 'TAG-LABEL ON SECOND COURSE' to Course2.
678         core_tag_tag::set_item_tags('core', 'course', $course2->id, context_course::instance($course2->id),
679                 array('TAG-LABEL ON SECOND COURSE'));
680         $taginstance = $DB->get_record('tag_instance',
681                 array('itemtype' => 'course', 'itemid' => $course2->id), '*', MUST_EXIST);
682         // Search by tagid.
683         $results = core_course_external::search_courses('tagid', $taginstance->tagid);
684         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
685         $this->assertEquals($coursedata2['fullname'], $results['courses'][0]['fullname']);
687         // Search by block (use news_items default block).
688         $blockid = $DB->get_field('block', 'id', array('name' => 'news_items'));
689         $results = core_course_external::search_courses('blocklist', $blockid);
690         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
691         $this->assertEquals(2, $results['total']);
693         // Now as a normal user.
694         $user = self::getDataGenerator()->create_user();
696         // Add a 3rd, hidden, course we shouldn't see, even when enrolled as student.
697         $coursedata3['fullname'] = 'HIDDEN COURSE';
698         $coursedata3['visible'] = 0;
699         $course3  = self::getDataGenerator()->create_course($coursedata3);
700         $this->getDataGenerator()->enrol_user($user->id, $course3->id, 'student');
702         $this->getDataGenerator()->enrol_user($user->id, $course2->id, 'student');
703         $this->setUser($user);
705         $results = core_course_external::search_courses('search', 'FIRST');
706         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
707         $this->assertCount(1, $results['courses']);
708         $this->assertEquals(1, $results['total']);
709         $this->assertEquals($coursedata1['fullname'], $results['courses'][0]['fullname']);
711         // Check that we can see both without the limit to enrolled setting.
712         $results = core_course_external::search_courses('search', 'COURSE', 0, 0, array(), 0);
713         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
714         $this->assertCount(2, $results['courses']);
715         $this->assertEquals(2, $results['total']);
717         // Check that we only see our enrolled course when limiting.
718         $results = core_course_external::search_courses('search', 'COURSE', 0, 0, array(), 1);
719         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
720         $this->assertCount(1, $results['courses']);
721         $this->assertEquals(1, $results['total']);
722         $this->assertEquals($coursedata2['fullname'], $results['courses'][0]['fullname']);
724         // Search by block (use news_items default block). Should fail (only admins allowed).
725         $this->expectException('required_capability_exception');
726         $results = core_course_external::search_courses('blocklist', $blockid);
728     }
730     /**
731      * Create a course with contents
732      * @return array A list with the course object and course modules objects
733      */
734     private function prepare_get_course_contents_test() {
735         global $DB;
736         $course  = self::getDataGenerator()->create_course();
737         $forumdescription = 'This is the forum description';
738         $forum = $this->getDataGenerator()->create_module('forum',
739             array('course' => $course->id, 'intro' => $forumdescription),
740             array('showdescription' => true));
741         $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
742         $data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
743         $datacm = get_coursemodule_from_instance('page', $data->id);
744         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
745         $pagecm = get_coursemodule_from_instance('page', $page->id);
746         $labeldescription = 'This is a very long label to test if more than 50 characters are returned.
747                 So bla bla bla bla <b>bold bold bold</b> bla bla bla bla.';
748         $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id,
749             'intro' => $labeldescription));
750         $labelcm = get_coursemodule_from_instance('label', $label->id);
751         $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id,
752             'name' => 'URL: % & $ ../', 'section' => 2));
753         $urlcm = get_coursemodule_from_instance('url', $url->id);
755         // Set the required capabilities by the external function.
756         $context = context_course::instance($course->id);
757         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
758         $this->assignUserCapability('moodle/course:update', $context->id, $roleid);
759         $this->assignUserCapability('mod/data:view', $context->id, $roleid);
761         $conditions = array('course' => $course->id, 'section' => 2);
762         $DB->set_field('course_sections', 'summary', 'Text with iframe <iframe src="https://moodle.org"></iframe>', $conditions);
763         rebuild_course_cache($course->id, true);
765         return array($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm);
766     }
768     /**
769      * Test get_course_contents
770      */
771     public function test_get_course_contents() {
772         $this->resetAfterTest(true);
774         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
776         $sections = core_course_external::get_course_contents($course->id, array());
777         // We need to execute the return values cleaning process to simulate the web service server.
778         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
780         // Check that forum and label descriptions are correctly returned.
781         $firstsection = array_shift($sections);
782         $lastsection = array_pop($sections);
784         $modinfo = get_fast_modinfo($course);
785         $testexecuted = 0;
786         foreach ($firstsection['modules'] as $module) {
787             if ($module['id'] == $forumcm->id and $module['modname'] == 'forum') {
788                 $cm = $modinfo->cms[$forumcm->id];
789                 $formattedtext = format_text($cm->content, FORMAT_HTML,
790                     array('noclean' => true, 'para' => false, 'filter' => false));
791                 $this->assertEquals($formattedtext, $module['description']);
792                 $this->assertEquals($forumcm->instance, $module['instance']);
793                 $testexecuted = $testexecuted + 1;
794             } else if ($module['id'] == $labelcm->id and $module['modname'] == 'label') {
795                 $cm = $modinfo->cms[$labelcm->id];
796                 $formattedtext = format_text($cm->content, FORMAT_HTML,
797                     array('noclean' => true, 'para' => false, 'filter' => false));
798                 $this->assertEquals($formattedtext, $module['description']);
799                 $this->assertEquals($labelcm->instance, $module['instance']);
800                 $testexecuted = $testexecuted + 1;
801             }
802         }
803         $this->assertEquals(2, $testexecuted);
804         $this->assertEquals(0, $firstsection['section']);
806         // Check that the only return section has the 5 created modules.
807         $this->assertCount(4, $firstsection['modules']);
808         $this->assertCount(1, $lastsection['modules']);
809         $this->assertEquals(2, $lastsection['section']);
810         $this->assertContains('<iframe', $lastsection['summary']);
811         $this->assertContains('</iframe>', $lastsection['summary']);
813         try {
814             $sections = core_course_external::get_course_contents($course->id,
815                                                                     array(array("name" => "invalid", "value" => 1)));
816             $this->fail('Exception expected due to invalid option.');
817         } catch (moodle_exception $e) {
818             $this->assertEquals('errorinvalidparam', $e->errorcode);
819         }
820     }
823     /**
824      * Test get_course_contents excluding modules
825      */
826     public function test_get_course_contents_excluding_modules() {
827         $this->resetAfterTest(true);
829         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
831         // Test exclude modules.
832         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludemodules", "value" => 1)));
834         // We need to execute the return values cleaning process to simulate the web service server.
835         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
837         $firstsection = array_shift($sections);
838         $lastsection = array_pop($sections);
840         $this->assertEmpty($firstsection['modules']);
841         $this->assertEmpty($lastsection['modules']);
842     }
844     /**
845      * Test get_course_contents excluding contents
846      */
847     public function test_get_course_contents_excluding_contents() {
848         $this->resetAfterTest(true);
850         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
852         // Test exclude modules.
853         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludecontents", "value" => 1)));
855         // We need to execute the return values cleaning process to simulate the web service server.
856         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
858         foreach ($sections as $section) {
859             foreach ($section['modules'] as $module) {
860                 // Only resources return contents.
861                 if (isset($module['contents'])) {
862                     $this->assertEmpty($module['contents']);
863                 }
864             }
865         }
866     }
868     /**
869      * Test get_course_contents filtering by section number
870      */
871     public function test_get_course_contents_section_number() {
872         $this->resetAfterTest(true);
874         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
876         // Test exclude modules.
877         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "sectionnumber", "value" => 0)));
879         // We need to execute the return values cleaning process to simulate the web service server.
880         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
882         $this->assertCount(1, $sections);
883         $this->assertCount(4, $sections[0]['modules']);
884     }
886     /**
887      * Test get_course_contents filtering by cmid
888      */
889     public function test_get_course_contents_cmid() {
890         $this->resetAfterTest(true);
892         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
894         // Test exclude modules.
895         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "cmid", "value" => $forumcm->id)));
897         // We need to execute the return values cleaning process to simulate the web service server.
898         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
900         $this->assertCount(2, $sections);
901         $this->assertCount(1, $sections[0]['modules']);
902         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
903     }
906     /**
907      * Test get_course_contents filtering by cmid and section
908      */
909     public function test_get_course_contents_section_cmid() {
910         $this->resetAfterTest(true);
912         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
914         // Test exclude modules.
915         $sections = core_course_external::get_course_contents($course->id, array(
916                                                                         array("name" => "cmid", "value" => $forumcm->id),
917                                                                         array("name" => "sectionnumber", "value" => 0)
918                                                                         ));
920         // We need to execute the return values cleaning process to simulate the web service server.
921         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
923         $this->assertCount(1, $sections);
924         $this->assertCount(1, $sections[0]['modules']);
925         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
926     }
928     /**
929      * Test get_course_contents filtering by modname
930      */
931     public function test_get_course_contents_modname() {
932         $this->resetAfterTest(true);
934         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
936         // Test exclude modules.
937         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "modname", "value" => "forum")));
939         // We need to execute the return values cleaning process to simulate the web service server.
940         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
942         $this->assertCount(2, $sections);
943         $this->assertCount(1, $sections[0]['modules']);
944         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
945     }
947     /**
948      * Test get_course_contents filtering by modname
949      */
950     public function test_get_course_contents_modid() {
951         $this->resetAfterTest(true);
953         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
955         // Test exclude modules.
956         $sections = core_course_external::get_course_contents($course->id, array(
957                                                                             array("name" => "modname", "value" => "page"),
958                                                                             array("name" => "modid", "value" => $pagecm->instance),
959                                                                             ));
961         // We need to execute the return values cleaning process to simulate the web service server.
962         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
964         $this->assertCount(2, $sections);
965         $this->assertCount(1, $sections[0]['modules']);
966         $this->assertEquals("page", $sections[0]['modules'][0]["modname"]);
967         $this->assertEquals($pagecm->instance, $sections[0]['modules'][0]["instance"]);
968     }
970     /**
971      * Test duplicate_course
972      */
973     public function test_duplicate_course() {
974         $this->resetAfterTest(true);
976         // Create one course with three modules.
977         $course  = self::getDataGenerator()->create_course();
978         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
979         $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
980         $forumcontext = context_module::instance($forum->cmid);
981         $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
982         $datacontext = context_module::instance($data->cmid);
983         $datacm = get_coursemodule_from_instance('page', $data->id);
984         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
985         $pagecontext = context_module::instance($page->cmid);
986         $pagecm = get_coursemodule_from_instance('page', $page->id);
988         // Set the required capabilities by the external function.
989         $coursecontext = context_course::instance($course->id);
990         $categorycontext = context_coursecat::instance($course->category);
991         $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id);
992         $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid);
993         $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid);
994         $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid);
995         $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid);
996         // Optional capabilities to copy user data.
997         $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid);
998         $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid);
1000         $newcourse['fullname'] = 'Course duplicate';
1001         $newcourse['shortname'] = 'courseduplicate';
1002         $newcourse['categoryid'] = $course->category;
1003         $newcourse['visible'] = true;
1004         $newcourse['options'][] = array('name' => 'users', 'value' => true);
1006         $duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'],
1007                 $newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']);
1009         // We need to execute the return values cleaning process to simulate the web service server.
1010         $duplicate = external_api::clean_returnvalue(core_course_external::duplicate_course_returns(), $duplicate);
1012         // Check that the course has been duplicated.
1013         $this->assertEquals($newcourse['shortname'], $duplicate['shortname']);
1014     }
1016     /**
1017      * Test update_courses
1018      */
1019     public function test_update_courses() {
1020         global $DB, $CFG, $USER, $COURSE;
1022         // Get current $COURSE to be able to restore it later (defaults to $SITE). We need this
1023         // trick because we are both updating and getting (for testing) course information
1024         // in the same request and core_course_external::update_courses()
1025         // is overwriting $COURSE all over the time with OLD values, so later
1026         // use of get_course() fetches those OLD values instead of the updated ones.
1027         // See MDL-39723 for more info.
1028         $origcourse = clone($COURSE);
1030         $this->resetAfterTest(true);
1032         // Set the required capabilities by the external function.
1033         $contextid = context_system::instance()->id;
1034         $roleid = $this->assignUserCapability('moodle/course:update', $contextid);
1035         $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1036         $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1037         $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1038         $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1039         $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1040         $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
1041         $this->assignUserCapability('moodle/course:viewhiddencourses', $contextid, $roleid);
1043         // Create category and course.
1044         $category1  = self::getDataGenerator()->create_category();
1045         $category2  = self::getDataGenerator()->create_category();
1046         $originalcourse1 = self::getDataGenerator()->create_course();
1047         self::getDataGenerator()->enrol_user($USER->id, $originalcourse1->id, $roleid);
1048         $originalcourse2 = self::getDataGenerator()->create_course();
1049         self::getDataGenerator()->enrol_user($USER->id, $originalcourse2->id, $roleid);
1051         // Course values to be updated.
1052         $course1['id'] = $originalcourse1->id;
1053         $course1['fullname'] = 'Updated test course 1';
1054         $course1['shortname'] = 'Udestedtestcourse1';
1055         $course1['categoryid'] = $category1->id;
1056         $course2['id'] = $originalcourse2->id;
1057         $course2['fullname'] = 'Updated test course 2';
1058         $course2['shortname'] = 'Updestedtestcourse2';
1059         $course2['categoryid'] = $category2->id;
1060         $course2['idnumber'] = 'Updatedidnumber2';
1061         $course2['summary'] = 'Updaated description for course 2';
1062         $course2['summaryformat'] = FORMAT_HTML;
1063         $course2['format'] = 'topics';
1064         $course2['showgrades'] = 1;
1065         $course2['newsitems'] = 3;
1066         $course2['startdate'] = 1420092000; // 01/01/2015.
1067         $course2['enddate'] = 1422669600; // 01/31/2015.
1068         $course2['numsections'] = 4;
1069         $course2['maxbytes'] = 100000;
1070         $course2['showreports'] = 1;
1071         $course2['visible'] = 0;
1072         $course2['hiddensections'] = 0;
1073         $course2['groupmode'] = 0;
1074         $course2['groupmodeforce'] = 0;
1075         $course2['defaultgroupingid'] = 0;
1076         $course2['enablecompletion'] = 1;
1077         $course2['lang'] = 'en';
1078         $course2['forcetheme'] = 'bootstrapbase';
1079         $courses = array($course1, $course2);
1081         $updatedcoursewarnings = core_course_external::update_courses($courses);
1082         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1083                                                                     $updatedcoursewarnings);
1084         $COURSE = $origcourse; // Restore $COURSE. Instead of using the OLD one set by the previous line.
1086         // Check that right number of courses were created.
1087         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1089         // Check that the courses were correctly created.
1090         foreach ($courses as $course) {
1091             $courseinfo = course_get_format($course['id'])->get_course();
1092             if ($course['id'] == $course2['id']) {
1093                 $this->assertEquals($course2['fullname'], $courseinfo->fullname);
1094                 $this->assertEquals($course2['shortname'], $courseinfo->shortname);
1095                 $this->assertEquals($course2['categoryid'], $courseinfo->category);
1096                 $this->assertEquals($course2['idnumber'], $courseinfo->idnumber);
1097                 $this->assertEquals($course2['summary'], $courseinfo->summary);
1098                 $this->assertEquals($course2['summaryformat'], $courseinfo->summaryformat);
1099                 $this->assertEquals($course2['format'], $courseinfo->format);
1100                 $this->assertEquals($course2['showgrades'], $courseinfo->showgrades);
1101                 $this->assertEquals($course2['newsitems'], $courseinfo->newsitems);
1102                 $this->assertEquals($course2['startdate'], $courseinfo->startdate);
1103                 $this->assertEquals($course2['enddate'], $courseinfo->enddate);
1104                 $this->assertEquals($course2['numsections'], $courseinfo->numsections);
1105                 $this->assertEquals($course2['maxbytes'], $courseinfo->maxbytes);
1106                 $this->assertEquals($course2['showreports'], $courseinfo->showreports);
1107                 $this->assertEquals($course2['visible'], $courseinfo->visible);
1108                 $this->assertEquals($course2['hiddensections'], $courseinfo->hiddensections);
1109                 $this->assertEquals($course2['groupmode'], $courseinfo->groupmode);
1110                 $this->assertEquals($course2['groupmodeforce'], $courseinfo->groupmodeforce);
1111                 $this->assertEquals($course2['defaultgroupingid'], $courseinfo->defaultgroupingid);
1112                 $this->assertEquals($course2['lang'], $courseinfo->lang);
1114                 if (!empty($CFG->allowcoursethemes)) {
1115                     $this->assertEquals($course2['forcetheme'], $courseinfo->theme);
1116                 }
1118                 $this->assertEquals($course2['enablecompletion'], $courseinfo->enablecompletion);
1119             } else if ($course['id'] == $course1['id']) {
1120                 $this->assertEquals($course1['fullname'], $courseinfo->fullname);
1121                 $this->assertEquals($course1['shortname'], $courseinfo->shortname);
1122                 $this->assertEquals($course1['categoryid'], $courseinfo->category);
1123                 $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
1124                 $this->assertEquals('topics', $courseinfo->format);
1125                 $this->assertEquals(5, $courseinfo->numsections);
1126                 $this->assertEquals(0, $courseinfo->newsitems);
1127                 $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
1128             } else {
1129                 throw moodle_exception('Unexpected shortname');
1130             }
1131         }
1133         $courses = array($course1);
1134         // Try update course without update capability.
1135         $user = self::getDataGenerator()->create_user();
1136         $this->setUser($user);
1137         $this->unassignUserCapability('moodle/course:update', $contextid, $roleid);
1138         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1139         $updatedcoursewarnings = core_course_external::update_courses($courses);
1140         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1141                                                                     $updatedcoursewarnings);
1142         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1144         // Try update course category without capability.
1145         $this->assignUserCapability('moodle/course:update', $contextid, $roleid);
1146         $this->unassignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1147         $user = self::getDataGenerator()->create_user();
1148         $this->setUser($user);
1149         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1150         $course1['categoryid'] = $category2->id;
1151         $courses = array($course1);
1152         $updatedcoursewarnings = core_course_external::update_courses($courses);
1153         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1154                                                                     $updatedcoursewarnings);
1155         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1157         // Try update course fullname without capability.
1158         $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1159         $this->unassignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1160         $user = self::getDataGenerator()->create_user();
1161         $this->setUser($user);
1162         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1163         $updatedcoursewarnings = core_course_external::update_courses($courses);
1164         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1165                                                                     $updatedcoursewarnings);
1166         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1167         $course1['fullname'] = 'Testing fullname without permission';
1168         $courses = array($course1);
1169         $updatedcoursewarnings = core_course_external::update_courses($courses);
1170         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1171                                                                     $updatedcoursewarnings);
1172         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1174         // Try update course shortname without capability.
1175         $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1176         $this->unassignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1177         $user = self::getDataGenerator()->create_user();
1178         $this->setUser($user);
1179         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1180         $updatedcoursewarnings = core_course_external::update_courses($courses);
1181         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1182                                                                     $updatedcoursewarnings);
1183         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1184         $course1['shortname'] = 'Testing shortname without permission';
1185         $courses = array($course1);
1186         $updatedcoursewarnings = core_course_external::update_courses($courses);
1187         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1188                                                                     $updatedcoursewarnings);
1189         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1191         // Try update course idnumber without capability.
1192         $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1193         $this->unassignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1194         $user = self::getDataGenerator()->create_user();
1195         $this->setUser($user);
1196         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1197         $updatedcoursewarnings = core_course_external::update_courses($courses);
1198         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1199                                                                     $updatedcoursewarnings);
1200         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1201         $course1['idnumber'] = 'NEWIDNUMBER';
1202         $courses = array($course1);
1203         $updatedcoursewarnings = core_course_external::update_courses($courses);
1204         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1205                                                                     $updatedcoursewarnings);
1206         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1208         // Try update course summary without capability.
1209         $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1210         $this->unassignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1211         $user = self::getDataGenerator()->create_user();
1212         $this->setUser($user);
1213         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1214         $updatedcoursewarnings = core_course_external::update_courses($courses);
1215         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1216                                                                     $updatedcoursewarnings);
1217         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1218         $course1['summary'] = 'New summary';
1219         $courses = array($course1);
1220         $updatedcoursewarnings = core_course_external::update_courses($courses);
1221         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1222                                                                     $updatedcoursewarnings);
1223         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1225         // Try update course with invalid summary format.
1226         $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1227         $user = self::getDataGenerator()->create_user();
1228         $this->setUser($user);
1229         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1230         $updatedcoursewarnings = core_course_external::update_courses($courses);
1231         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1232                                                                     $updatedcoursewarnings);
1233         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1234         $course1['summaryformat'] = 10;
1235         $courses = array($course1);
1236         $updatedcoursewarnings = core_course_external::update_courses($courses);
1237         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1238                                                                     $updatedcoursewarnings);
1239         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1241         // Try update course visibility without capability.
1242         $this->unassignUserCapability('moodle/course:visibility', $contextid, $roleid);
1243         $user = self::getDataGenerator()->create_user();
1244         $this->setUser($user);
1245         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1246         $course1['summaryformat'] = FORMAT_MOODLE;
1247         $courses = array($course1);
1248         $updatedcoursewarnings = core_course_external::update_courses($courses);
1249         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1250                                                                     $updatedcoursewarnings);
1251         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1252         $course1['visible'] = 0;
1253         $courses = array($course1);
1254         $updatedcoursewarnings = core_course_external::update_courses($courses);
1255         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1256                                                                     $updatedcoursewarnings);
1257         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1258     }
1260     /**
1261      * Test delete course_module.
1262      */
1263     public function test_delete_modules() {
1264         global $DB;
1266         // Ensure we reset the data after this test.
1267         $this->resetAfterTest(true);
1269         // Create a user.
1270         $user = self::getDataGenerator()->create_user();
1272         // Set the tests to run as the user.
1273         self::setUser($user);
1275         // Create a course to add the modules.
1276         $course = self::getDataGenerator()->create_course();
1278         // Create two test modules.
1279         $record = new stdClass();
1280         $record->course = $course->id;
1281         $module1 = self::getDataGenerator()->create_module('forum', $record);
1282         $module2 = self::getDataGenerator()->create_module('assign', $record);
1284         // Check the forum was correctly created.
1285         $this->assertEquals(1, $DB->count_records('forum', array('id' => $module1->id)));
1287         // Check the assignment was correctly created.
1288         $this->assertEquals(1, $DB->count_records('assign', array('id' => $module2->id)));
1290         // Check data exists in the course modules table.
1291         $this->assertEquals(2, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
1292                 array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
1294         // Enrol the user in the course.
1295         $enrol = enrol_get_plugin('manual');
1296         $enrolinstances = enrol_get_instances($course->id, true);
1297         foreach ($enrolinstances as $courseenrolinstance) {
1298             if ($courseenrolinstance->enrol == "manual") {
1299                 $instance = $courseenrolinstance;
1300                 break;
1301             }
1302         }
1303         $enrol->enrol_user($instance, $user->id);
1305         // Assign capabilities to delete module 1.
1306         $modcontext = context_module::instance($module1->cmid);
1307         $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id);
1309         // Assign capabilities to delete module 2.
1310         $modcontext = context_module::instance($module2->cmid);
1311         $newrole = create_role('Role 2', 'role2', 'Role 2 description');
1312         $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id, $newrole);
1314         // Deleting these module instances.
1315         core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1317         // Check the forum was deleted.
1318         $this->assertEquals(0, $DB->count_records('forum', array('id' => $module1->id)));
1320         // Check the assignment was deleted.
1321         $this->assertEquals(0, $DB->count_records('assign', array('id' => $module2->id)));
1323         // Check we retrieve no data in the course modules table.
1324         $this->assertEquals(0, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
1325                 array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
1327         // Call with non-existent course module id and ensure exception thrown.
1328         try {
1329             core_course_external::delete_modules(array('1337'));
1330             $this->fail('Exception expected due to missing course module.');
1331         } catch (dml_missing_record_exception $e) {
1332             $this->assertEquals('invalidcoursemodule', $e->errorcode);
1333         }
1335         // Create two modules.
1336         $module1 = self::getDataGenerator()->create_module('forum', $record);
1337         $module2 = self::getDataGenerator()->create_module('assign', $record);
1339         // Since these modules were recreated the user will not have capabilities
1340         // to delete them, ensure exception is thrown if they try.
1341         try {
1342             core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1343             $this->fail('Exception expected due to missing capability.');
1344         } catch (moodle_exception $e) {
1345             $this->assertEquals('nopermissions', $e->errorcode);
1346         }
1348         // Unenrol user from the course.
1349         $enrol->unenrol_user($instance, $user->id);
1351         // Try and delete modules from the course the user was unenrolled in, make sure exception thrown.
1352         try {
1353             core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1354             $this->fail('Exception expected due to being unenrolled from the course.');
1355         } catch (moodle_exception $e) {
1356             $this->assertEquals('requireloginerror', $e->errorcode);
1357         }
1358     }
1360     /**
1361      * Test import_course into an empty course
1362      */
1363     public function test_import_course_empty() {
1364         global $USER;
1366         $this->resetAfterTest(true);
1368         $course1  = self::getDataGenerator()->create_course();
1369         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course1->id, 'name' => 'Forum test'));
1370         $page = $this->getDataGenerator()->create_module('page', array('course' => $course1->id, 'name' => 'Page test'));
1372         $course2  = self::getDataGenerator()->create_course();
1374         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1375         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1377         // Verify the state of the courses before we do the import.
1378         $this->assertCount(2, $course1cms);
1379         $this->assertEmpty($course2cms);
1381         // Setup the user to run the operation (ugly hack because validate_context() will
1382         // fail as the email is not set by $this->setAdminUser()).
1383         $this->setAdminUser();
1384         $USER->email = 'emailtopass@example.com';
1386         // Import from course1 to course2.
1387         core_course_external::import_course($course1->id, $course2->id, 0);
1389         // Verify that now we have two modules in both courses.
1390         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1391         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1392         $this->assertCount(2, $course1cms);
1393         $this->assertCount(2, $course2cms);
1395         // Verify that the names transfered across correctly.
1396         foreach ($course2cms as $cm) {
1397             if ($cm->modname === 'page') {
1398                 $this->assertEquals($cm->name, $page->name);
1399             } else if ($cm->modname === 'forum') {
1400                 $this->assertEquals($cm->name, $forum->name);
1401             } else {
1402                 $this->fail('Unknown CM found.');
1403             }
1404         }
1405     }
1407     /**
1408      * Test import_course into an filled course
1409      */
1410     public function test_import_course_filled() {
1411         global $USER;
1413         $this->resetAfterTest(true);
1415         // Add forum and page to course1.
1416         $course1  = self::getDataGenerator()->create_course();
1417         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1418         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
1420         // Add quiz to course 2.
1421         $course2  = self::getDataGenerator()->create_course();
1422         $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
1424         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1425         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1427         // Verify the state of the courses before we do the import.
1428         $this->assertCount(2, $course1cms);
1429         $this->assertCount(1, $course2cms);
1431         // Setup the user to run the operation (ugly hack because validate_context() will
1432         // fail as the email is not set by $this->setAdminUser()).
1433         $this->setAdminUser();
1434         $USER->email = 'emailtopass@example.com';
1436         // Import from course1 to course2 without deleting content.
1437         core_course_external::import_course($course1->id, $course2->id, 0);
1439         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1441         // Verify that now we have three modules in course2.
1442         $this->assertCount(3, $course2cms);
1444         // Verify that the names transfered across correctly.
1445         foreach ($course2cms as $cm) {
1446             if ($cm->modname === 'page') {
1447                 $this->assertEquals($cm->name, $page->name);
1448             } else if ($cm->modname === 'forum') {
1449                 $this->assertEquals($cm->name, $forum->name);
1450             } else if ($cm->modname === 'quiz') {
1451                 $this->assertEquals($cm->name, $quiz->name);
1452             } else {
1453                 $this->fail('Unknown CM found.');
1454             }
1455         }
1456     }
1458     /**
1459      * Test import_course with only blocks set to backup
1460      */
1461     public function test_import_course_blocksonly() {
1462         global $USER, $DB;
1464         $this->resetAfterTest(true);
1466         // Add forum and page to course1.
1467         $course1  = self::getDataGenerator()->create_course();
1468         $course1ctx = context_course::instance($course1->id);
1469         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1470         $block = $this->getDataGenerator()->create_block('online_users', array('parentcontextid' => $course1ctx->id));
1472         $course2  = self::getDataGenerator()->create_course();
1473         $course2ctx = context_course::instance($course2->id);
1474         $initialblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
1475         $initialcmcount = count(get_fast_modinfo($course2->id)->get_cms());
1477         // Setup the user to run the operation (ugly hack because validate_context() will
1478         // fail as the email is not set by $this->setAdminUser()).
1479         $this->setAdminUser();
1480         $USER->email = 'emailtopass@example.com';
1482         // Import from course1 to course2 without deleting content, but excluding
1483         // activities.
1484         $options = array(
1485             array('name' => 'activities', 'value' => 0),
1486             array('name' => 'blocks', 'value' => 1),
1487             array('name' => 'filters', 'value' => 0),
1488         );
1490         core_course_external::import_course($course1->id, $course2->id, 0, $options);
1492         $newcmcount = count(get_fast_modinfo($course2->id)->get_cms());
1493         $newblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
1494         // Check that course modules haven't changed, but that blocks have.
1495         $this->assertEquals($initialcmcount, $newcmcount);
1496         $this->assertEquals(($initialblockcount + 1), $newblockcount);
1497     }
1499     /**
1500      * Test import_course into an filled course, deleting content.
1501      */
1502     public function test_import_course_deletecontent() {
1503         global $USER;
1504         $this->resetAfterTest(true);
1506         // Add forum and page to course1.
1507         $course1  = self::getDataGenerator()->create_course();
1508         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1509         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
1511         // Add quiz to course 2.
1512         $course2  = self::getDataGenerator()->create_course();
1513         $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
1515         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1516         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1518         // Verify the state of the courses before we do the import.
1519         $this->assertCount(2, $course1cms);
1520         $this->assertCount(1, $course2cms);
1522         // Setup the user to run the operation (ugly hack because validate_context() will
1523         // fail as the email is not set by $this->setAdminUser()).
1524         $this->setAdminUser();
1525         $USER->email = 'emailtopass@example.com';
1527         // Import from course1 to course2,  deleting content.
1528         core_course_external::import_course($course1->id, $course2->id, 1);
1530         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1532         // Verify that now we have two modules in course2.
1533         $this->assertCount(2, $course2cms);
1535         // Verify that the course only contains the imported modules.
1536         foreach ($course2cms as $cm) {
1537             if ($cm->modname === 'page') {
1538                 $this->assertEquals($cm->name, $page->name);
1539             } else if ($cm->modname === 'forum') {
1540                 $this->assertEquals($cm->name, $forum->name);
1541             } else {
1542                 $this->fail('Unknown CM found: '.$cm->name);
1543             }
1544         }
1545     }
1547     /**
1548      * Ensure import_course handles incorrect deletecontent option correctly.
1549      */
1550     public function test_import_course_invalid_deletecontent_option() {
1551         $this->resetAfterTest(true);
1553         $course1  = self::getDataGenerator()->create_course();
1554         $course2  = self::getDataGenerator()->create_course();
1556         $this->expectException('moodle_exception');
1557         $this->expectExceptionMessage(get_string('invalidextparam', 'webservice', -1));
1558         // Import from course1 to course2, with invalid option
1559         core_course_external::import_course($course1->id, $course2->id, -1);;
1560     }
1562     /**
1563      * Test view_course function
1564      */
1565     public function test_view_course() {
1567         $this->resetAfterTest();
1569         // Course without sections.
1570         $course = $this->getDataGenerator()->create_course(array('numsections' => 5), array('createsections' => true));
1571         $this->setAdminUser();
1573         // Redirect events to the sink, so we can recover them later.
1574         $sink = $this->redirectEvents();
1576         $result = core_course_external::view_course($course->id, 1);
1577         $result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result);
1578         $events = $sink->get_events();
1579         $event = reset($events);
1581         // Check the event details are correct.
1582         $this->assertInstanceOf('\core\event\course_viewed', $event);
1583         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1584         $this->assertEquals(1, $event->other['coursesectionnumber']);
1586         $result = core_course_external::view_course($course->id);
1587         $result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result);
1588         $events = $sink->get_events();
1589         $event = array_pop($events);
1590         $sink->close();
1592         // Check the event details are correct.
1593         $this->assertInstanceOf('\core\event\course_viewed', $event);
1594         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1595         $this->assertEmpty($event->other);
1597     }
1599     /**
1600      * Test get_course_module
1601      */
1602     public function test_get_course_module() {
1603         global $DB;
1605         $this->resetAfterTest(true);
1607         $this->setAdminUser();
1608         $course = self::getDataGenerator()->create_course();
1609         $record = array(
1610             'course' => $course->id,
1611             'name' => 'First Assignment'
1612         );
1613         $options = array(
1614             'idnumber' => 'ABC',
1615             'visible' => 0
1616         );
1617         // Hidden activity.
1618         $assign = self::getDataGenerator()->create_module('assign', $record, $options);
1620         $outcomescale = 'Distinction, Very Good, Good, Pass, Fail';
1622         // Insert a custom grade scale to be used by an outcome.
1623         $gradescale = new grade_scale();
1624         $gradescale->name        = 'gettcoursemodulescale';
1625         $gradescale->courseid    = $course->id;
1626         $gradescale->userid      = 0;
1627         $gradescale->scale       = $outcomescale;
1628         $gradescale->description = 'This scale is used to mark standard assignments.';
1629         $gradescale->insert();
1631         // Insert an outcome.
1632         $data = new stdClass();
1633         $data->courseid = $course->id;
1634         $data->fullname = 'Team work';
1635         $data->shortname = 'Team work';
1636         $data->scaleid = $gradescale->id;
1637         $outcome = new grade_outcome($data, false);
1638         $outcome->insert();
1640         $outcomegradeitem = new grade_item();
1641         $outcomegradeitem->itemname = $outcome->shortname;
1642         $outcomegradeitem->itemtype = 'mod';
1643         $outcomegradeitem->itemmodule = 'assign';
1644         $outcomegradeitem->iteminstance = $assign->id;
1645         $outcomegradeitem->outcomeid = $outcome->id;
1646         $outcomegradeitem->cmid = 0;
1647         $outcomegradeitem->courseid = $course->id;
1648         $outcomegradeitem->aggregationcoef = 0;
1649         $outcomegradeitem->itemnumber = 1; // The activity's original grade item will be 0.
1650         $outcomegradeitem->gradetype = GRADE_TYPE_SCALE;
1651         $outcomegradeitem->scaleid = $outcome->scaleid;
1652         $outcomegradeitem->insert();
1654         $assignmentgradeitem = grade_item::fetch(
1655             array(
1656                 'itemtype' => 'mod',
1657                 'itemmodule' => 'assign',
1658                 'iteminstance' => $assign->id,
1659                 'itemnumber' => 0,
1660                 'courseid' => $course->id
1661             )
1662         );
1663         $outcomegradeitem->set_parent($assignmentgradeitem->categoryid);
1664         $outcomegradeitem->move_after_sortorder($assignmentgradeitem->sortorder);
1666         // Test admin user can see the complete hidden activity.
1667         $result = core_course_external::get_course_module($assign->cmid);
1668         $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result);
1670         $this->assertCount(0, $result['warnings']);
1671         // Test we retrieve all the fields.
1672         $this->assertCount(27, $result['cm']);
1673         $this->assertEquals($record['name'], $result['cm']['name']);
1674         $this->assertEquals($options['idnumber'], $result['cm']['idnumber']);
1675         $this->assertEquals(100, $result['cm']['grade']);
1676         $this->assertEquals(0.0, $result['cm']['gradepass']);
1677         $this->assertEquals('submissions', $result['cm']['advancedgrading'][0]['area']);
1678         $this->assertEmpty($result['cm']['advancedgrading'][0]['method']);
1679         $this->assertEquals($outcomescale, $result['cm']['outcomes'][0]['scale']);
1681         $student = $this->getDataGenerator()->create_user();
1682         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1684         self::getDataGenerator()->enrol_user($student->id,  $course->id, $studentrole->id);
1685         $this->setUser($student);
1687         // The user shouldn't be able to see the activity.
1688         try {
1689             core_course_external::get_course_module($assign->cmid);
1690             $this->fail('Exception expected due to invalid permissions.');
1691         } catch (moodle_exception $e) {
1692             $this->assertEquals('requireloginerror', $e->errorcode);
1693         }
1695         // Make module visible.
1696         set_coursemodule_visible($assign->cmid, 1);
1698         // Test student user.
1699         $result = core_course_external::get_course_module($assign->cmid);
1700         $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result);
1702         $this->assertCount(0, $result['warnings']);
1703         // Test we retrieve only the few files we can see.
1704         $this->assertCount(11, $result['cm']);
1705         $this->assertEquals($assign->cmid, $result['cm']['id']);
1706         $this->assertEquals($course->id, $result['cm']['course']);
1707         $this->assertEquals('assign', $result['cm']['modname']);
1708         $this->assertEquals($assign->id, $result['cm']['instance']);
1710     }
1712     /**
1713      * Test get_course_module_by_instance
1714      */
1715     public function test_get_course_module_by_instance() {
1716         global $DB;
1718         $this->resetAfterTest(true);
1720         $this->setAdminUser();
1721         $course = self::getDataGenerator()->create_course();
1722         $record = array(
1723             'course' => $course->id,
1724             'name' => 'First Chat'
1725         );
1726         $options = array(
1727             'idnumber' => 'ABC',
1728             'visible' => 0
1729         );
1730         // Hidden activity.
1731         $chat = self::getDataGenerator()->create_module('chat', $record, $options);
1733         // Test admin user can see the complete hidden activity.
1734         $result = core_course_external::get_course_module_by_instance('chat', $chat->id);
1735         $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result);
1737         $this->assertCount(0, $result['warnings']);
1738         // Test we retrieve all the fields.
1739         $this->assertCount(22, $result['cm']);
1740         $this->assertEquals($record['name'], $result['cm']['name']);
1741         $this->assertEquals($options['idnumber'], $result['cm']['idnumber']);
1743         $student = $this->getDataGenerator()->create_user();
1744         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1746         self::getDataGenerator()->enrol_user($student->id,  $course->id, $studentrole->id);
1747         $this->setUser($student);
1749         // The user shouldn't be able to see the activity.
1750         try {
1751             core_course_external::get_course_module_by_instance('chat', $chat->id);
1752             $this->fail('Exception expected due to invalid permissions.');
1753         } catch (moodle_exception $e) {
1754             $this->assertEquals('requireloginerror', $e->errorcode);
1755         }
1757         // Make module visible.
1758         set_coursemodule_visible($chat->cmid, 1);
1760         // Test student user.
1761         $result = core_course_external::get_course_module_by_instance('chat', $chat->id);
1762         $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result);
1764         $this->assertCount(0, $result['warnings']);
1765         // Test we retrieve only the few files we can see.
1766         $this->assertCount(11, $result['cm']);
1767         $this->assertEquals($chat->cmid, $result['cm']['id']);
1768         $this->assertEquals($course->id, $result['cm']['course']);
1769         $this->assertEquals('chat', $result['cm']['modname']);
1770         $this->assertEquals($chat->id, $result['cm']['instance']);
1772         // Try with an invalid module name.
1773         try {
1774             core_course_external::get_course_module_by_instance('abc', $chat->id);
1775             $this->fail('Exception expected due to invalid module name.');
1776         } catch (dml_read_exception $e) {
1777             $this->assertEquals('dmlreadexception', $e->errorcode);
1778         }
1780     }
1782     /**
1783      * Test get_activities_overview
1784      */
1785     public function test_get_activities_overview() {
1786         global $USER;
1788         $this->resetAfterTest();
1789         $course1 = self::getDataGenerator()->create_course();
1790         $course2 = self::getDataGenerator()->create_course();
1792         // Create a viewer user.
1793         $viewer = self::getDataGenerator()->create_user((object) array('trackforums' => 1));
1794         $this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
1795         $this->getDataGenerator()->enrol_user($viewer->id, $course2->id);
1797         // Create two forums - one in each course.
1798         $record = new stdClass();
1799         $record->course = $course1->id;
1800         $forum1 = self::getDataGenerator()->create_module('forum', (object) array('course' => $course1->id));
1801         $forum2 = self::getDataGenerator()->create_module('forum', (object) array('course' => $course2->id));
1803         $this->setAdminUser();
1804         // A standard post in the forum.
1805         $record = new stdClass();
1806         $record->course = $course1->id;
1807         $record->userid = $USER->id;
1808         $record->forum = $forum1->id;
1809         $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1811         $this->setUser($viewer->id);
1812         $courses = array($course1->id , $course2->id);
1814         $result = core_course_external::get_activities_overview($courses);
1815         $result = external_api::clean_returnvalue(core_course_external::get_activities_overview_returns(), $result);
1817         // There should be one entry for course1, and no others.
1818         $this->assertCount(1, $result['courses']);
1819         $this->assertEquals($course1->id, $result['courses'][0]['id']);
1820         // Check expected overview data for the module.
1821         $this->assertEquals('forum', $result['courses'][0]['overviews'][0]['module']);
1822         $this->assertContains('1 total unread', $result['courses'][0]['overviews'][0]['overviewtext']);
1823     }
1825     /**
1826      * Test get_user_navigation_options
1827      */
1828     public function test_get_user_navigation_options() {
1829         global $USER;
1831         $this->resetAfterTest();
1832         $course1 = self::getDataGenerator()->create_course();
1833         $course2 = self::getDataGenerator()->create_course();
1835         // Create a viewer user.
1836         $viewer = self::getDataGenerator()->create_user();
1837         $this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
1838         $this->getDataGenerator()->enrol_user($viewer->id, $course2->id);
1840         $this->setUser($viewer->id);
1841         $courses = array($course1->id , $course2->id, SITEID);
1843         $result = core_course_external::get_user_navigation_options($courses);
1844         $result = external_api::clean_returnvalue(core_course_external::get_user_navigation_options_returns(), $result);
1846         $this->assertCount(0, $result['warnings']);
1847         $this->assertCount(3, $result['courses']);
1849         foreach ($result['courses'] as $course) {
1850             $navoptions = new stdClass;
1851             foreach ($course['options'] as $option) {
1852                 $navoptions->{$option['name']} = $option['available'];
1853             }
1854             if ($course['id'] == SITEID) {
1855                 $this->assertCount(8, $course['options']);
1856                 $this->assertTrue($navoptions->blogs);
1857                 $this->assertFalse($navoptions->notes);
1858                 $this->assertFalse($navoptions->participants);
1859                 $this->assertTrue($navoptions->badges);
1860                 $this->assertTrue($navoptions->tags);
1861                 $this->assertFalse($navoptions->search);
1862                 $this->assertTrue($navoptions->calendar);
1863                 $this->assertTrue($navoptions->competencies);
1864             } else {
1865                 $this->assertCount(6, $course['options']);
1866                 $this->assertTrue($navoptions->blogs);
1867                 $this->assertFalse($navoptions->notes);
1868                 $this->assertTrue($navoptions->participants);
1869                 $this->assertTrue($navoptions->badges);
1870                 $this->assertTrue($navoptions->grades);
1871                 $this->assertTrue($navoptions->competencies);
1872             }
1873         }
1874     }
1876     /**
1877      * Test get_user_administration_options
1878      */
1879     public function test_get_user_administration_options() {
1880         global $USER;
1882         $this->resetAfterTest();
1883         $course1 = self::getDataGenerator()->create_course();
1884         $course2 = self::getDataGenerator()->create_course();
1886         // Create a viewer user.
1887         $viewer = self::getDataGenerator()->create_user();
1888         $this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
1889         $this->getDataGenerator()->enrol_user($viewer->id, $course2->id);
1891         $this->setUser($viewer->id);
1892         $courses = array($course1->id , $course2->id, SITEID);
1894         $result = core_course_external::get_user_administration_options($courses);
1895         $result = external_api::clean_returnvalue(core_course_external::get_user_administration_options_returns(), $result);
1897         $this->assertCount(0, $result['warnings']);
1898         $this->assertCount(3, $result['courses']);
1900         foreach ($result['courses'] as $course) {
1901             $adminoptions = new stdClass;
1902             foreach ($course['options'] as $option) {
1903                 $adminoptions->{$option['name']} = $option['available'];
1904             }
1905             if ($course['id'] == SITEID) {
1906                 $this->assertCount(15, $course['options']);
1907                 $this->assertFalse($adminoptions->update);
1908                 $this->assertFalse($adminoptions->filters);
1909                 $this->assertFalse($adminoptions->reports);
1910                 $this->assertFalse($adminoptions->backup);
1911                 $this->assertFalse($adminoptions->restore);
1912                 $this->assertFalse($adminoptions->files);
1913                 $this->assertFalse(!isset($adminoptions->tags));
1914                 $this->assertFalse($adminoptions->gradebook);
1915                 $this->assertFalse($adminoptions->outcomes);
1916                 $this->assertFalse($adminoptions->badges);
1917                 $this->assertFalse($adminoptions->import);
1918                 $this->assertFalse($adminoptions->publish);
1919                 $this->assertFalse($adminoptions->reset);
1920                 $this->assertFalse($adminoptions->roles);
1921             } else {
1922                 $this->assertCount(14, $course['options']);
1923                 $this->assertFalse($adminoptions->update);
1924                 $this->assertFalse($adminoptions->filters);
1925                 $this->assertFalse($adminoptions->reports);
1926                 $this->assertFalse($adminoptions->backup);
1927                 $this->assertFalse($adminoptions->restore);
1928                 $this->assertFalse($adminoptions->files);
1929                 $this->assertFalse($adminoptions->tags);
1930                 $this->assertFalse($adminoptions->gradebook);
1931                 $this->assertFalse($adminoptions->outcomes);
1932                 $this->assertTrue($adminoptions->badges);
1933                 $this->assertFalse($adminoptions->import);
1934                 $this->assertFalse($adminoptions->publish);
1935                 $this->assertFalse($adminoptions->reset);
1936                 $this->assertFalse($adminoptions->roles);
1937             }
1938         }
1939     }
1941     /**
1942      * Test get_courses_by_fields
1943      */
1944     public function test_get_courses_by_field() {
1945         global $DB;
1946         $this->resetAfterTest(true);
1948         $category1 = self::getDataGenerator()->create_category();
1949         $category2 = self::getDataGenerator()->create_category(array('parent' => $category1->id));
1950         $course1 = self::getDataGenerator()->create_course(array('category' => $category1->id, 'shortname' => 'c1'));
1951         $course2 = self::getDataGenerator()->create_course(array('visible' => 0, 'category' => $category2->id, 'idnumber' => 'i2'));
1953         $student1 = self::getDataGenerator()->create_user();
1954         $user1 = self::getDataGenerator()->create_user();
1955         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1956         self::getDataGenerator()->enrol_user($student1->id, $course1->id, $studentrole->id);
1957         self::getDataGenerator()->enrol_user($student1->id, $course2->id, $studentrole->id);
1959         self::setAdminUser();
1960         // As admins, we should be able to retrieve everything.
1961         $result = core_course_external::get_courses_by_field();
1962         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
1963         $this->assertCount(3, $result['courses']);
1964         // Expect to receive all the fields.
1965         $this->assertCount(35, $result['courses'][0]);
1966         $this->assertCount(35, $result['courses'][1]);
1967         $this->assertCount(35, $result['courses'][2]);
1969         $result = core_course_external::get_courses_by_field('id', $course1->id);
1970         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
1971         $this->assertCount(1, $result['courses']);
1972         $this->assertEquals($course1->id, $result['courses'][0]['id']);
1973         // Expect to receive all the fields.
1974         $this->assertCount(35, $result['courses'][0]);
1976         $result = core_course_external::get_courses_by_field('id', $course2->id);
1977         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
1978         $this->assertCount(1, $result['courses']);
1979         $this->assertEquals($course2->id, $result['courses'][0]['id']);
1981         $result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id");
1982         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
1983         $this->assertCount(2, $result['courses']);
1985         $result = core_course_external::get_courses_by_field('category', $category1->id);
1986         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
1987         $this->assertCount(1, $result['courses']);
1988         $this->assertEquals($course1->id, $result['courses'][0]['id']);
1990         $result = core_course_external::get_courses_by_field('shortname', 'c1');
1991         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
1992         $this->assertCount(1, $result['courses']);
1993         $this->assertEquals($course1->id, $result['courses'][0]['id']);
1995         $result = core_course_external::get_courses_by_field('idnumber', 'i2');
1996         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
1997         $this->assertCount(1, $result['courses']);
1998         $this->assertEquals($course2->id, $result['courses'][0]['id']);
2000         $result = core_course_external::get_courses_by_field('idnumber', 'x');
2001         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2002         $this->assertCount(0, $result['courses']);
2004         self::setUser($student1);
2005         // All visible courses  (including front page) for normal student.
2006         $result = core_course_external::get_courses_by_field();
2007         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2008         $this->assertCount(2, $result['courses']);
2009         $this->assertCount(28, $result['courses'][0]);
2010         $this->assertCount(28, $result['courses'][1]);
2012         $result = core_course_external::get_courses_by_field('id', $course1->id);
2013         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2014         $this->assertCount(1, $result['courses']);
2015         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2016         // Expect to receive all the files that a student can see.
2017         $this->assertCount(28, $result['courses'][0]);
2019         // Course 2 is not visible.
2020         $result = core_course_external::get_courses_by_field('id', $course2->id);
2021         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2022         $this->assertCount(0, $result['courses']);
2024         $result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id");
2025         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2026         $this->assertCount(1, $result['courses']);
2028         $result = core_course_external::get_courses_by_field('category', $category1->id);
2029         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2030         $this->assertCount(1, $result['courses']);
2031         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2033         $result = core_course_external::get_courses_by_field('shortname', 'c1');
2034         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2035         $this->assertCount(1, $result['courses']);
2036         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2038         $result = core_course_external::get_courses_by_field('idnumber', 'i2');
2039         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2040         $this->assertCount(0, $result['courses']);
2042         $result = core_course_external::get_courses_by_field('idnumber', 'x');
2043         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2044         $this->assertCount(0, $result['courses']);
2046         self::setUser($user1);
2047         // All visible courses (including front page) for authenticated user.
2048         $result = core_course_external::get_courses_by_field();
2049         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2050         $this->assertCount(2, $result['courses']);
2051         $this->assertCount(28, $result['courses'][0]);  // Site course.
2052         $this->assertCount(12, $result['courses'][1]);  // Only public information, not enrolled.
2054         $result = core_course_external::get_courses_by_field('id', $course1->id);
2055         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2056         $this->assertCount(1, $result['courses']);
2057         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2058         // Expect to receive all the files that a authenticated can see.
2059         $this->assertCount(12, $result['courses'][0]);
2061         // Course 2 is not visible.
2062         $result = core_course_external::get_courses_by_field('id', $course2->id);
2063         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2064         $this->assertCount(0, $result['courses']);
2066         $result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id");
2067         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2068         $this->assertCount(1, $result['courses']);
2070         $result = core_course_external::get_courses_by_field('category', $category1->id);
2071         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2072         $this->assertCount(1, $result['courses']);
2073         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2075         $result = core_course_external::get_courses_by_field('shortname', 'c1');
2076         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2077         $this->assertCount(1, $result['courses']);
2078         $this->assertEquals($course1->id, $result['courses'][0]['id']);
2080         $result = core_course_external::get_courses_by_field('idnumber', 'i2');
2081         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2082         $this->assertCount(0, $result['courses']);
2084         $result = core_course_external::get_courses_by_field('idnumber', 'x');
2085         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2086         $this->assertCount(0, $result['courses']);
2087     }
2089     public function test_get_courses_by_field_invalid_field() {
2090         $this->expectException('invalid_parameter_exception');
2091         $result = core_course_external::get_courses_by_field('zyx', 'x');
2092     }
2094     public function test_get_courses_by_field_invalid_courses() {
2095         $result = core_course_external::get_courses_by_field('id', '-1');
2096         $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2097         $this->assertCount(0, $result['courses']);
2098     }