56bf16b88ff7fe280f8d7730b521109c8d1c2976
[moodle.git] / course / tests / externallib_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * External course functions unit tests
19  *
20  * @package    core_course
21  * @category   external
22  * @copyright  2012 Jerome Mouneyrac
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32 /**
33  * External course functions unit tests
34  *
35  * @package    core_course
36  * @category   external
37  * @copyright  2012 Jerome Mouneyrac
38  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39  */
40 class core_course_externallib_testcase extends externallib_advanced_testcase {
42     /**
43      * Tests set up
44      */
45     protected function setUp() {
46         global $CFG;
47         require_once($CFG->dirroot . '/course/externallib.php');
48     }
50     /**
51      * Tidy up open files that may be left open.
52      */
53     protected function tearDown() {
54         gc_collect_cycles();
55     }
57     /**
58      * Test create_categories
59      */
60     public function test_create_categories() {
62         global $DB;
64         $this->resetAfterTest(true);
66         // Set the required capabilities by the external function
67         $contextid = context_system::instance()->id;
68         $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
70         // Create base categories.
71         $category1 = new stdClass();
72         $category1->name = 'Root Test Category 1';
73         $category2 = new stdClass();
74         $category2->name = 'Root Test Category 2';
75         $category2->idnumber = 'rootcattest2';
76         $category2->desc = 'Description for root test category 1';
77         $category2->theme = 'base';
78         $categories = array(
79             array('name' => $category1->name, 'parent' => 0),
80             array('name' => $category2->name, 'parent' => 0, 'idnumber' => $category2->idnumber,
81                 'description' => $category2->desc, 'theme' => $category2->theme)
82         );
84         $createdcats = core_course_external::create_categories($categories);
86         // We need to execute the return values cleaning process to simulate the web service server.
87         $createdcats = external_api::clean_returnvalue(core_course_external::create_categories_returns(), $createdcats);
89         // Initially confirm that base data was inserted correctly.
90         $this->assertEquals($category1->name, $createdcats[0]['name']);
91         $this->assertEquals($category2->name, $createdcats[1]['name']);
93         // Save the ids.
94         $category1->id = $createdcats[0]['id'];
95         $category2->id = $createdcats[1]['id'];
97         // Create on sub category.
98         $category3 = new stdClass();
99         $category3->name = 'Sub Root Test Category 3';
100         $subcategories = array(
101             array('name' => $category3->name, 'parent' => $category1->id)
102         );
104         $createdsubcats = core_course_external::create_categories($subcategories);
106         // We need to execute the return values cleaning process to simulate the web service server.
107         $createdsubcats = external_api::clean_returnvalue(core_course_external::create_categories_returns(), $createdsubcats);
109         // Confirm that sub categories were inserted correctly.
110         $this->assertEquals($category3->name, $createdsubcats[0]['name']);
112         // Save the ids.
113         $category3->id = $createdsubcats[0]['id'];
115         // Calling the ws function should provide a new sortorder to give category1,
116         // category2, category3. New course categories are ordered by id not name.
117         $category1 = $DB->get_record('course_categories', array('id' => $category1->id));
118         $category2 = $DB->get_record('course_categories', array('id' => $category2->id));
119         $category3 = $DB->get_record('course_categories', array('id' => $category3->id));
121         // sortorder sequence (and sortorder) must be:
122         // category 1
123         //   category 3
124         // category 2
125         $this->assertGreaterThan($category1->sortorder, $category3->sortorder);
126         $this->assertGreaterThan($category3->sortorder, $category2->sortorder);
128         // Call without required capability
129         $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
130         $this->setExpectedException('required_capability_exception');
131         $createdsubcats = core_course_external::create_categories($subcategories);
133     }
135     /**
136      * Test delete categories
137      */
138     public function test_delete_categories() {
139         global $DB;
141         $this->resetAfterTest(true);
143         // Set the required capabilities by the external function
144         $contextid = context_system::instance()->id;
145         $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
147         $category1  = self::getDataGenerator()->create_category();
148         $category2  = self::getDataGenerator()->create_category(
149                 array('parent' => $category1->id));
150         $category3  = self::getDataGenerator()->create_category();
151         $category4  = self::getDataGenerator()->create_category(
152                 array('parent' => $category3->id));
153         $category5  = self::getDataGenerator()->create_category(
154                 array('parent' => $category4->id));
156         //delete category 1 and 2 + delete category 4, category 5 moved under category 3
157         core_course_external::delete_categories(array(
158             array('id' => $category1->id, 'recursive' => 1),
159             array('id' => $category4->id)
160         ));
162         //check $category 1 and 2 are deleted
163         $notdeletedcount = $DB->count_records_select('course_categories',
164             'id IN ( ' . $category1->id . ',' . $category2->id . ',' . $category4->id . ')');
165         $this->assertEquals(0, $notdeletedcount);
167         //check that $category5 as $category3 for parent
168         $dbcategory5 = $DB->get_record('course_categories', array('id' => $category5->id));
169         $this->assertEquals($dbcategory5->path, $category3->path . '/' . $category5->id);
171          // Call without required capability
172         $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
173         $this->setExpectedException('required_capability_exception');
174         $createdsubcats = core_course_external::delete_categories(
175                 array(array('id' => $category3->id)));
176     }
178     /**
179      * Test get categories
180      */
181     public function test_get_categories() {
182         global $DB;
184         $this->resetAfterTest(true);
186         $generatedcats = array();
187         $category1data['idnumber'] = 'idnumbercat1';
188         $category1data['name'] = 'Category 1 for PHPunit test';
189         $category1data['description'] = 'Category 1 description';
190         $category1data['descriptionformat'] = FORMAT_MOODLE;
191         $category1  = self::getDataGenerator()->create_category($category1data);
192         $generatedcats[$category1->id] = $category1;
193         $category2  = self::getDataGenerator()->create_category(
194                 array('parent' => $category1->id));
195         $generatedcats[$category2->id] = $category2;
196         $category6  = self::getDataGenerator()->create_category(
197                 array('parent' => $category1->id, 'visible' => 0));
198         $generatedcats[$category6->id] = $category6;
199         $category3  = self::getDataGenerator()->create_category();
200         $generatedcats[$category3->id] = $category3;
201         $category4  = self::getDataGenerator()->create_category(
202                 array('parent' => $category3->id));
203         $generatedcats[$category4->id] = $category4;
204         $category5  = self::getDataGenerator()->create_category(
205                 array('parent' => $category4->id));
206         $generatedcats[$category5->id] = $category5;
208         // Set the required capabilities by the external function.
209         $context = context_system::instance();
210         $roleid = $this->assignUserCapability('moodle/category:manage', $context->id);
212         // Retrieve category1 + sub-categories except not visible ones
213         $categories = core_course_external::get_categories(array(
214             array('key' => 'id', 'value' => $category1->id),
215             array('key' => 'visible', 'value' => 1)), 1);
217         // We need to execute the return values cleaning process to simulate the web service server.
218         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
220         // Check we retrieve the good total number of categories.
221         $this->assertEquals(2, count($categories));
223         // Check the return values
224         foreach ($categories as $category) {
225             $generatedcat = $generatedcats[$category['id']];
226             $this->assertEquals($category['idnumber'], $generatedcat->idnumber);
227             $this->assertEquals($category['name'], $generatedcat->name);
228             // Description was converted to the HTML format.
229             $this->assertEquals($category['description'], format_text($generatedcat->description, FORMAT_MOODLE, array('para' => false)));
230             $this->assertEquals($category['descriptionformat'], FORMAT_HTML);
231         }
233         // Check different params.
234         $categories = core_course_external::get_categories(array(
235             array('key' => 'id', 'value' => $category1->id),
236             array('key' => 'idnumber', 'value' => $category1->idnumber),
237             array('key' => 'visible', 'value' => 1)), 0);
239         // We need to execute the return values cleaning process to simulate the web service server.
240         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
242         $this->assertEquals(1, count($categories));
244         // Retrieve categories from parent.
245         $categories = core_course_external::get_categories(array(
246             array('key' => 'parent', 'value' => $category3->id)), 1);
247         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
249         $this->assertEquals(2, count($categories));
251         // Retrieve all categories.
252         $categories = core_course_external::get_categories();
254         // We need to execute the return values cleaning process to simulate the web service server.
255         $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
257         $this->assertEquals($DB->count_records('course_categories'), count($categories));
259         // Call without required capability (it will fail cause of the search on idnumber).
260         $this->unassignUserCapability('moodle/category:manage', $context->id, $roleid);
261         $this->setExpectedException('moodle_exception');
262         $categories = core_course_external::get_categories(array(
263             array('key' => 'id', 'value' => $category1->id),
264             array('key' => 'idnumber', 'value' => $category1->idnumber),
265             array('key' => 'visible', 'value' => 1)), 0);
266     }
268     /**
269      * Test update_categories
270      */
271     public function test_update_categories() {
272         global $DB;
274         $this->resetAfterTest(true);
276         // Set the required capabilities by the external function
277         $contextid = context_system::instance()->id;
278         $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
280         // Create base categories.
281         $category1data['idnumber'] = 'idnumbercat1';
282         $category1data['name'] = 'Category 1 for PHPunit test';
283         $category1data['description'] = 'Category 1 description';
284         $category1data['descriptionformat'] = FORMAT_MOODLE;
285         $category1  = self::getDataGenerator()->create_category($category1data);
286         $category2  = self::getDataGenerator()->create_category(
287                 array('parent' => $category1->id));
288         $category3  = self::getDataGenerator()->create_category();
289         $category4  = self::getDataGenerator()->create_category(
290                 array('parent' => $category3->id));
291         $category5  = self::getDataGenerator()->create_category(
292                 array('parent' => $category4->id));
294         // We update all category1 attribut.
295         // Then we move cat4 and cat5 parent: cat3 => cat1
296         $categories = array(
297             array('id' => $category1->id,
298                 'name' => $category1->name . '_updated',
299                 'idnumber' => $category1->idnumber . '_updated',
300                 'description' => $category1->description . '_updated',
301                 'descriptionformat' => FORMAT_HTML,
302                 'theme' => $category1->theme),
303             array('id' => $category4->id, 'parent' => $category1->id));
305         core_course_external::update_categories($categories);
307         // Check the values were updated.
308         $dbcategories = $DB->get_records_select('course_categories',
309                 'id IN (' . $category1->id . ',' . $category2->id . ',' . $category2->id
310                 . ',' . $category3->id . ',' . $category4->id . ',' . $category5->id .')');
311         $this->assertEquals($category1->name . '_updated',
312                 $dbcategories[$category1->id]->name);
313         $this->assertEquals($category1->idnumber . '_updated',
314                 $dbcategories[$category1->id]->idnumber);
315         $this->assertEquals($category1->description . '_updated',
316                 $dbcategories[$category1->id]->description);
317         $this->assertEquals(FORMAT_HTML, $dbcategories[$category1->id]->descriptionformat);
319         // Check that category4 and category5 have been properly moved.
320         $this->assertEquals('/' . $category1->id . '/' . $category4->id,
321                 $dbcategories[$category4->id]->path);
322         $this->assertEquals('/' . $category1->id . '/' . $category4->id . '/' . $category5->id,
323                 $dbcategories[$category5->id]->path);
325         // Call without required capability.
326         $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
327         $this->setExpectedException('required_capability_exception');
328         core_course_external::update_categories($categories);
329     }
331     /**
332      * Test create_courses
333      */
334     public function test_create_courses() {
335         global $DB;
337         $this->resetAfterTest(true);
339         // Enable course completion.
340         set_config('enablecompletion', 1);
341         // Enable course themes.
342         set_config('allowcoursethemes', 1);
344         // Set the required capabilities by the external function
345         $contextid = context_system::instance()->id;
346         $roleid = $this->assignUserCapability('moodle/course:create', $contextid);
347         $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
349         $category  = self::getDataGenerator()->create_category();
351         // Create base categories.
352         $course1['fullname'] = 'Test course 1';
353         $course1['shortname'] = 'Testcourse1';
354         $course1['categoryid'] = $category->id;
355         $course2['fullname'] = 'Test course 2';
356         $course2['shortname'] = 'Testcourse2';
357         $course2['categoryid'] = $category->id;
358         $course2['idnumber'] = 'testcourse2idnumber';
359         $course2['summary'] = 'Description for course 2';
360         $course2['summaryformat'] = FORMAT_MOODLE;
361         $course2['format'] = 'weeks';
362         $course2['showgrades'] = 1;
363         $course2['newsitems'] = 3;
364         $course2['startdate'] = 1420092000; // 01/01/2015
365         $course2['numsections'] = 4;
366         $course2['maxbytes'] = 100000;
367         $course2['showreports'] = 1;
368         $course2['visible'] = 0;
369         $course2['hiddensections'] = 0;
370         $course2['groupmode'] = 0;
371         $course2['groupmodeforce'] = 0;
372         $course2['defaultgroupingid'] = 0;
373         $course2['enablecompletion'] = 1;
374         $course2['completionnotify'] = 1;
375         $course2['lang'] = 'en';
376         $course2['forcetheme'] = 'base';
377         $course3['fullname'] = 'Test course 3';
378         $course3['shortname'] = 'Testcourse3';
379         $course3['categoryid'] = $category->id;
380         $course3['format'] = 'topics';
381         $course3options = array('numsections' => 8,
382             'hiddensections' => 1,
383             'coursedisplay' => 1);
384         $course3['courseformatoptions'] = array();
385         foreach ($course3options as $key => $value) {
386             $course3['courseformatoptions'][] = array('name' => $key, 'value' => $value);
387         }
388         $courses = array($course1, $course2, $course3);
390         $createdcourses = core_course_external::create_courses($courses);
392         // We need to execute the return values cleaning process to simulate the web service server.
393         $createdcourses = external_api::clean_returnvalue(core_course_external::create_courses_returns(), $createdcourses);
395         // Check that right number of courses were created.
396         $this->assertEquals(3, count($createdcourses));
398         // Check that the courses were correctly created.
399         foreach ($createdcourses as $createdcourse) {
400             $courseinfo = course_get_format($createdcourse['id'])->get_course();
402             if ($createdcourse['shortname'] == $course2['shortname']) {
403                 $this->assertEquals($courseinfo->fullname, $course2['fullname']);
404                 $this->assertEquals($courseinfo->shortname, $course2['shortname']);
405                 $this->assertEquals($courseinfo->category, $course2['categoryid']);
406                 $this->assertEquals($courseinfo->idnumber, $course2['idnumber']);
407                 $this->assertEquals($courseinfo->summary, $course2['summary']);
408                 $this->assertEquals($courseinfo->summaryformat, $course2['summaryformat']);
409                 $this->assertEquals($courseinfo->format, $course2['format']);
410                 $this->assertEquals($courseinfo->showgrades, $course2['showgrades']);
411                 $this->assertEquals($courseinfo->newsitems, $course2['newsitems']);
412                 $this->assertEquals($courseinfo->startdate, $course2['startdate']);
413                 $this->assertEquals($courseinfo->numsections, $course2['numsections']);
414                 $this->assertEquals($courseinfo->maxbytes, $course2['maxbytes']);
415                 $this->assertEquals($courseinfo->showreports, $course2['showreports']);
416                 $this->assertEquals($courseinfo->visible, $course2['visible']);
417                 $this->assertEquals($courseinfo->hiddensections, $course2['hiddensections']);
418                 $this->assertEquals($courseinfo->groupmode, $course2['groupmode']);
419                 $this->assertEquals($courseinfo->groupmodeforce, $course2['groupmodeforce']);
420                 $this->assertEquals($courseinfo->defaultgroupingid, $course2['defaultgroupingid']);
421                 $this->assertEquals($courseinfo->completionnotify, $course2['completionnotify']);
422                 $this->assertEquals($courseinfo->lang, $course2['lang']);
423                 $this->assertEquals($courseinfo->theme, $course2['forcetheme']);
425                 // We enabled completion at the beginning of the test.
426                 $this->assertEquals($courseinfo->enablecompletion, $course2['enablecompletion']);
428             } else if ($createdcourse['shortname'] == $course1['shortname']) {
429                 $courseconfig = get_config('moodlecourse');
430                 $this->assertEquals($courseinfo->fullname, $course1['fullname']);
431                 $this->assertEquals($courseinfo->shortname, $course1['shortname']);
432                 $this->assertEquals($courseinfo->category, $course1['categoryid']);
433                 $this->assertEquals($courseinfo->summaryformat, FORMAT_HTML);
434                 $this->assertEquals($courseinfo->format, $courseconfig->format);
435                 $this->assertEquals($courseinfo->showgrades, $courseconfig->showgrades);
436                 $this->assertEquals($courseinfo->newsitems, $courseconfig->newsitems);
437                 $this->assertEquals($courseinfo->maxbytes, $courseconfig->maxbytes);
438                 $this->assertEquals($courseinfo->showreports, $courseconfig->showreports);
439                 $this->assertEquals($courseinfo->groupmode, $courseconfig->groupmode);
440                 $this->assertEquals($courseinfo->groupmodeforce, $courseconfig->groupmodeforce);
441                 $this->assertEquals($courseinfo->defaultgroupingid, 0);
442             } else if ($createdcourse['shortname'] == $course3['shortname']) {
443                 $this->assertEquals($courseinfo->fullname, $course3['fullname']);
444                 $this->assertEquals($courseinfo->shortname, $course3['shortname']);
445                 $this->assertEquals($courseinfo->category, $course3['categoryid']);
446                 $this->assertEquals($courseinfo->format, $course3['format']);
447                 $this->assertEquals($courseinfo->hiddensections, $course3options['hiddensections']);
448                 $this->assertEquals($courseinfo->numsections, $course3options['numsections']);
449                 $this->assertEquals($courseinfo->coursedisplay, $course3options['coursedisplay']);
450             } else {
451                 throw moodle_exception('Unexpected shortname');
452             }
453         }
455         // Call without required capability
456         $this->unassignUserCapability('moodle/course:create', $contextid, $roleid);
457         $this->setExpectedException('required_capability_exception');
458         $createdsubcats = core_course_external::create_courses($courses);
459     }
461     /**
462      * Test delete_courses
463      */
464     public function test_delete_courses() {
465         global $DB, $USER;
467         $this->resetAfterTest(true);
469         // Admin can delete a course.
470         $this->setAdminUser();
471         // Validate_context() will fail as the email is not set by $this->setAdminUser().
472         $USER->email = 'emailtopass@example.com';
474         $course1  = self::getDataGenerator()->create_course();
475         $course2  = self::getDataGenerator()->create_course();
476         $course3  = self::getDataGenerator()->create_course();
478         // Delete courses.
479         $result = core_course_external::delete_courses(array($course1->id, $course2->id));
480         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
481         // Check for 0 warnings.
482         $this->assertEquals(0, count($result['warnings']));
484         // Check $course 1 and 2 are deleted.
485         $notdeletedcount = $DB->count_records_select('course',
486             'id IN ( ' . $course1->id . ',' . $course2->id . ')');
487         $this->assertEquals(0, $notdeletedcount);
489         // Try to delete non-existent course.
490         $result = core_course_external::delete_courses(array($course1->id));
491         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
492         // Check for 1 warnings.
493         $this->assertEquals(1, count($result['warnings']));
495         // Try to delete Frontpage course.
496         $result = core_course_external::delete_courses(array(0));
497         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
498         // Check for 1 warnings.
499         $this->assertEquals(1, count($result['warnings']));
501          // Fail when the user has access to course (enrolled) but does not have permission or is not admin.
502         $student1 = self::getDataGenerator()->create_user();
503         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
504         $this->getDataGenerator()->enrol_user($student1->id,
505                                               $course3->id,
506                                               $studentrole->id);
507         $this->setUser($student1);
508         $result = core_course_external::delete_courses(array($course3->id));
509         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
510         // Check for 1 warnings.
511         $this->assertEquals(1, count($result['warnings']));
513          // Fail when the user is not allow to access the course (enrolled) or is not admin.
514         $this->setGuestUser();
515         $this->setExpectedException('require_login_exception');
517         $result = core_course_external::delete_courses(array($course3->id));
518         $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
519     }
521     /**
522      * Test get_courses
523      */
524     public function test_get_courses () {
525         global $DB;
527         $this->resetAfterTest(true);
529         $generatedcourses = array();
530         $coursedata['idnumber'] = 'idnumbercourse1';
531         $coursedata['fullname'] = 'Course 1 for PHPunit test';
532         $coursedata['summary'] = 'Course 1 description';
533         $coursedata['summaryformat'] = FORMAT_MOODLE;
534         $course1  = self::getDataGenerator()->create_course($coursedata);
535         $generatedcourses[$course1->id] = $course1;
536         $course2  = self::getDataGenerator()->create_course();
537         $generatedcourses[$course2->id] = $course2;
538         $course3  = self::getDataGenerator()->create_course(array('format' => 'topics'));
539         $generatedcourses[$course3->id] = $course3;
541         // Set the required capabilities by the external function.
542         $context = context_system::instance();
543         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
544         $this->assignUserCapability('moodle/course:update',
545                 context_course::instance($course1->id)->id, $roleid);
546         $this->assignUserCapability('moodle/course:update',
547                 context_course::instance($course2->id)->id, $roleid);
548         $this->assignUserCapability('moodle/course:update',
549                 context_course::instance($course3->id)->id, $roleid);
551         $courses = core_course_external::get_courses(array('ids' =>
552             array($course1->id, $course2->id)));
554         // We need to execute the return values cleaning process to simulate the web service server.
555         $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
557         // Check we retrieve the good total number of categories.
558         $this->assertEquals(2, count($courses));
560         foreach ($courses as $course) {
561             $dbcourse = $generatedcourses[$course['id']];
562             $this->assertEquals($course['idnumber'], $dbcourse->idnumber);
563             $this->assertEquals($course['fullname'], $dbcourse->fullname);
564             // Summary was converted to the HTML format.
565             $this->assertEquals($course['summary'], format_text($dbcourse->summary, FORMAT_MOODLE, array('para' => false)));
566             $this->assertEquals($course['summaryformat'], FORMAT_HTML);
567             $this->assertEquals($course['shortname'], $dbcourse->shortname);
568             $this->assertEquals($course['categoryid'], $dbcourse->category);
569             $this->assertEquals($course['format'], $dbcourse->format);
570             $this->assertEquals($course['showgrades'], $dbcourse->showgrades);
571             $this->assertEquals($course['newsitems'], $dbcourse->newsitems);
572             $this->assertEquals($course['startdate'], $dbcourse->startdate);
573             $this->assertEquals($course['numsections'], $dbcourse->numsections);
574             $this->assertEquals($course['maxbytes'], $dbcourse->maxbytes);
575             $this->assertEquals($course['showreports'], $dbcourse->showreports);
576             $this->assertEquals($course['visible'], $dbcourse->visible);
577             $this->assertEquals($course['hiddensections'], $dbcourse->hiddensections);
578             $this->assertEquals($course['groupmode'], $dbcourse->groupmode);
579             $this->assertEquals($course['groupmodeforce'], $dbcourse->groupmodeforce);
580             $this->assertEquals($course['defaultgroupingid'], $dbcourse->defaultgroupingid);
581             $this->assertEquals($course['completionnotify'], $dbcourse->completionnotify);
582             $this->assertEquals($course['lang'], $dbcourse->lang);
583             $this->assertEquals($course['forcetheme'], $dbcourse->theme);
584             $this->assertEquals($course['enablecompletion'], $dbcourse->enablecompletion);
585             if ($dbcourse->format === 'topics') {
586                 $this->assertEquals($course['courseformatoptions'], array(
587                     array('name' => 'numsections', 'value' => $dbcourse->numsections),
588                     array('name' => 'hiddensections', 'value' => $dbcourse->hiddensections),
589                     array('name' => 'coursedisplay', 'value' => $dbcourse->coursedisplay),
590                 ));
591             }
592         }
594         // Get all courses in the DB
595         $courses = core_course_external::get_courses(array());
597         // We need to execute the return values cleaning process to simulate the web service server.
598         $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
600         $this->assertEquals($DB->count_records('course'), count($courses));
601     }
603     /**
604      * Test search_courses
605      */
606     public function test_search_courses () {
608         global $DB, $CFG;
609         require_once($CFG->dirroot . '/tag/lib.php');
611         $this->resetAfterTest(true);
612         $this->setAdminUser();
613         $generatedcourses = array();
614         $coursedata1['fullname'] = 'FIRST COURSE';
615         $course1  = self::getDataGenerator()->create_course($coursedata1);
616         $coursedata2['fullname'] = 'SECOND COURSE';
617         $course2  = self::getDataGenerator()->create_course($coursedata2);
618         // Search by name.
619         $results = core_course_external::search_courses('search', 'FIRST');
620         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
621         $this->assertEquals($coursedata1['fullname'], $results['courses'][0]['fullname']);
622         $this->assertCount(1, $results['courses']);
624         // Create the forum.
625         $record = new stdClass();
626         $record->introformat = FORMAT_HTML;
627         $record->course = $course2->id;
628         // Set Aggregate type = Average of ratings.
629         $forum = self::getDataGenerator()->create_module('forum', $record);
631         // Search by module.
632         $results = core_course_external::search_courses('modulelist', 'forum');
633         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
634         $this->assertEquals(1, $results['total']);
636         // Enable coursetag option.
637         set_config('block_tags_showcoursetags', true);
638         // Add tag 'TAG-LABEL ON SECOND COURSE' to Course2.
639         tag_set('course', $course2->id, array('TAG-LABEL ON SECOND COURSE'), 'core', context_course::instance($course2->id)->id);
640         $taginstance = $DB->get_record('tag_instance', array('itemtype' => 'course', 'itemid' => $course2->id), '*', MUST_EXIST);
641         // Search by tagid.
642         $results = core_course_external::search_courses('tagid', $taginstance->tagid);
643         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
644         $this->assertEquals($coursedata2['fullname'], $results['courses'][0]['fullname']);
646         // Search by block (use news_items default block).
647         $blockid = $DB->get_field('block', 'id', array('name' => 'news_items'));
648         $results = core_course_external::search_courses('blocklist', $blockid);
649         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
650         $this->assertEquals(2, $results['total']);
652         // Now as a normal user.
653         $user = self::getDataGenerator()->create_user();
654         $this->setUser($user);
656         $results = core_course_external::search_courses('search', 'FIRST');
657         $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
658         $this->assertCount(1, $results['courses']);
659         $this->assertEquals(1, $results['total']);
660         $this->assertEquals($coursedata1['fullname'], $results['courses'][0]['fullname']);
662         // Search by block (use news_items default block). Should fail (only admins allowed).
663         $this->setExpectedException('required_capability_exception');
664         $results = core_course_external::search_courses('blocklist', $blockid);
666     }
668     /**
669      * Create a course with contents
670      * @return array A list with the course object and course modules objects
671      */
672     private function prepare_get_course_contents_test() {
673         $course  = self::getDataGenerator()->create_course();
674         $forumdescription = 'This is the forum description';
675         $forum = $this->getDataGenerator()->create_module('forum',
676             array('course' => $course->id, 'intro' => $forumdescription),
677             array('showdescription' => true));
678         $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
679         $data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
680         $datacm = get_coursemodule_from_instance('page', $data->id);
681         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
682         $pagecm = get_coursemodule_from_instance('page', $page->id);
683         $labeldescription = 'This is a very long label to test if more than 50 characters are returned.
684                 So bla bla bla bla <b>bold bold bold</b> bla bla bla bla.';
685         $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id,
686             'intro' => $labeldescription));
687         $labelcm = get_coursemodule_from_instance('label', $label->id);
688         $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id,
689             'name' => 'URL: % & $ ../', 'section' => 2));
690         $urlcm = get_coursemodule_from_instance('url', $url->id);
692         // Set the required capabilities by the external function.
693         $context = context_course::instance($course->id);
694         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
695         $this->assignUserCapability('moodle/course:update', $context->id, $roleid);
697         return array($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm);
698     }
700     /**
701      * Test get_course_contents
702      */
703     public function test_get_course_contents() {
704         $this->resetAfterTest(true);
706         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
708         $sections = core_course_external::get_course_contents($course->id, array());
709         // We need to execute the return values cleaning process to simulate the web service server.
710         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
712         // Check that forum and label descriptions are correctly returned.
713         $firstsection = array_shift($sections);
714         $lastsection = array_pop($sections);
716         $modinfo = get_fast_modinfo($course);
717         $testexecuted = 0;
718         foreach ($firstsection['modules'] as $module) {
719             if ($module['id'] == $forumcm->id and $module['modname'] == 'forum') {
720                 $cm = $modinfo->cms[$forumcm->id];
721                 $formattedtext = format_text($cm->content, FORMAT_HTML,
722                     array('noclean' => true, 'para' => false, 'filter' => false));
723                 $this->assertEquals($formattedtext, $module['description']);
724                 $this->assertEquals($forumcm->instance, $module['instance']);
725                 $testexecuted = $testexecuted + 1;
726             } else if ($module['id'] == $labelcm->id and $module['modname'] == 'label') {
727                 $cm = $modinfo->cms[$labelcm->id];
728                 $formattedtext = format_text($cm->content, FORMAT_HTML,
729                     array('noclean' => true, 'para' => false, 'filter' => false));
730                 $this->assertEquals($formattedtext, $module['description']);
731                 $this->assertEquals($labelcm->instance, $module['instance']);
732                 $testexecuted = $testexecuted + 1;
733             }
734         }
735         $this->assertEquals(2, $testexecuted);
737         // Check that the only return section has the 5 created modules.
738         $this->assertCount(4, $firstsection['modules']);
739         $this->assertCount(1, $lastsection['modules']);
741         try {
742             $sections = core_course_external::get_course_contents($course->id,
743                                                                     array(array("name" => "invalid", "value" => 1)));
744             $this->fail('Exception expected due to invalid option.');
745         } catch (moodle_exception $e) {
746             $this->assertEquals('errorinvalidparam', $e->errorcode);
747         }
748     }
751     /**
752      * Test get_course_contents excluding modules
753      */
754     public function test_get_course_contents_excluding_modules() {
755         $this->resetAfterTest(true);
757         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
759         // Test exclude modules.
760         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludemodules", "value" => 1)));
762         // We need to execute the return values cleaning process to simulate the web service server.
763         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
765         $firstsection = array_shift($sections);
766         $lastsection = array_pop($sections);
768         $this->assertEmpty($firstsection['modules']);
769         $this->assertEmpty($lastsection['modules']);
770     }
772     /**
773      * Test get_course_contents excluding contents
774      */
775     public function test_get_course_contents_excluding_contents() {
776         $this->resetAfterTest(true);
778         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
780         // Test exclude modules.
781         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludecontents", "value" => 1)));
783         // We need to execute the return values cleaning process to simulate the web service server.
784         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
786         foreach ($sections as $section) {
787             foreach ($section['modules'] as $module) {
788                 // Only resources return contents.
789                 if (isset($module['contents'])) {
790                     $this->assertEmpty($module['contents']);
791                 }
792             }
793         }
794     }
796     /**
797      * Test get_course_contents filtering by section number
798      */
799     public function test_get_course_contents_section_number() {
800         $this->resetAfterTest(true);
802         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
804         // Test exclude modules.
805         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "sectionnumber", "value" => 0)));
807         // We need to execute the return values cleaning process to simulate the web service server.
808         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
810         $this->assertCount(1, $sections);
811         $this->assertCount(4, $sections[0]['modules']);
812     }
814     /**
815      * Test get_course_contents filtering by cmid
816      */
817     public function test_get_course_contents_cmid() {
818         $this->resetAfterTest(true);
820         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
822         // Test exclude modules.
823         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "cmid", "value" => $forumcm->id)));
825         // We need to execute the return values cleaning process to simulate the web service server.
826         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
828         $this->assertCount(2, $sections);
829         $this->assertCount(1, $sections[0]['modules']);
830         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
831     }
834     /**
835      * Test get_course_contents filtering by cmid and section
836      */
837     public function test_get_course_contents_section_cmid() {
838         $this->resetAfterTest(true);
840         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
842         // Test exclude modules.
843         $sections = core_course_external::get_course_contents($course->id, array(
844                                                                         array("name" => "cmid", "value" => $forumcm->id),
845                                                                         array("name" => "sectionnumber", "value" => 0)
846                                                                         ));
848         // We need to execute the return values cleaning process to simulate the web service server.
849         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
851         $this->assertCount(1, $sections);
852         $this->assertCount(1, $sections[0]['modules']);
853         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
854     }
856     /**
857      * Test get_course_contents filtering by modname
858      */
859     public function test_get_course_contents_modname() {
860         $this->resetAfterTest(true);
862         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
864         // Test exclude modules.
865         $sections = core_course_external::get_course_contents($course->id, array(array("name" => "modname", "value" => "forum")));
867         // We need to execute the return values cleaning process to simulate the web service server.
868         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
870         $this->assertCount(2, $sections);
871         $this->assertCount(1, $sections[0]['modules']);
872         $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
873     }
875     /**
876      * Test get_course_contents filtering by modname
877      */
878     public function test_get_course_contents_modid() {
879         $this->resetAfterTest(true);
881         list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
883         // Test exclude modules.
884         $sections = core_course_external::get_course_contents($course->id, array(
885                                                                             array("name" => "modname", "value" => "page"),
886                                                                             array("name" => "modid", "value" => $pagecm->instance),
887                                                                             ));
889         // We need to execute the return values cleaning process to simulate the web service server.
890         $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
892         $this->assertCount(2, $sections);
893         $this->assertCount(1, $sections[0]['modules']);
894         $this->assertEquals("page", $sections[0]['modules'][0]["modname"]);
895         $this->assertEquals($pagecm->instance, $sections[0]['modules'][0]["instance"]);
896     }
898     /**
899      * Test duplicate_course
900      */
901     public function test_duplicate_course() {
902         $this->resetAfterTest(true);
904         // Create one course with three modules.
905         $course  = self::getDataGenerator()->create_course();
906         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
907         $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
908         $forumcontext = context_module::instance($forum->cmid);
909         $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
910         $datacontext = context_module::instance($data->cmid);
911         $datacm = get_coursemodule_from_instance('page', $data->id);
912         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
913         $pagecontext = context_module::instance($page->cmid);
914         $pagecm = get_coursemodule_from_instance('page', $page->id);
916         // Set the required capabilities by the external function.
917         $coursecontext = context_course::instance($course->id);
918         $categorycontext = context_coursecat::instance($course->category);
919         $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id);
920         $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid);
921         $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid);
922         $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid);
923         $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid);
924         // Optional capabilities to copy user data.
925         $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid);
926         $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid);
928         $newcourse['fullname'] = 'Course duplicate';
929         $newcourse['shortname'] = 'courseduplicate';
930         $newcourse['categoryid'] = $course->category;
931         $newcourse['visible'] = true;
932         $newcourse['options'][] = array('name' => 'users', 'value' => true);
934         $duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'],
935                 $newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']);
937         // We need to execute the return values cleaning process to simulate the web service server.
938         $duplicate = external_api::clean_returnvalue(core_course_external::duplicate_course_returns(), $duplicate);
940         // Check that the course has been duplicated.
941         $this->assertEquals($newcourse['shortname'], $duplicate['shortname']);
942     }
944     /**
945      * Test update_courses
946      */
947     public function test_update_courses() {
948         global $DB, $CFG, $USER, $COURSE;
950         // Get current $COURSE to be able to restore it later (defaults to $SITE). We need this
951         // trick because we are both updating and getting (for testing) course information
952         // in the same request and core_course_external::update_courses()
953         // is overwriting $COURSE all over the time with OLD values, so later
954         // use of get_course() fetches those OLD values instead of the updated ones.
955         // See MDL-39723 for more info.
956         $origcourse = clone($COURSE);
958         $this->resetAfterTest(true);
960         // Set the required capabilities by the external function.
961         $contextid = context_system::instance()->id;
962         $roleid = $this->assignUserCapability('moodle/course:update', $contextid);
963         $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
964         $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
965         $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
966         $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
967         $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
968         $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
969         $this->assignUserCapability('moodle/course:viewhiddencourses', $contextid, $roleid);
971         // Create category and course.
972         $category1  = self::getDataGenerator()->create_category();
973         $category2  = self::getDataGenerator()->create_category();
974         $originalcourse1 = self::getDataGenerator()->create_course();
975         self::getDataGenerator()->enrol_user($USER->id, $originalcourse1->id, $roleid);
976         $originalcourse2 = self::getDataGenerator()->create_course();
977         self::getDataGenerator()->enrol_user($USER->id, $originalcourse2->id, $roleid);
979         // Course values to be updated.
980         $course1['id'] = $originalcourse1->id;
981         $course1['fullname'] = 'Updated test course 1';
982         $course1['shortname'] = 'Udestedtestcourse1';
983         $course1['categoryid'] = $category1->id;
984         $course2['id'] = $originalcourse2->id;
985         $course2['fullname'] = 'Updated test course 2';
986         $course2['shortname'] = 'Updestedtestcourse2';
987         $course2['categoryid'] = $category2->id;
988         $course2['idnumber'] = 'Updatedidnumber2';
989         $course2['summary'] = 'Updaated description for course 2';
990         $course2['summaryformat'] = FORMAT_HTML;
991         $course2['format'] = 'topics';
992         $course2['showgrades'] = 1;
993         $course2['newsitems'] = 3;
994         $course2['startdate'] = 1420092000; // 01/01/2015.
995         $course2['numsections'] = 4;
996         $course2['maxbytes'] = 100000;
997         $course2['showreports'] = 1;
998         $course2['visible'] = 0;
999         $course2['hiddensections'] = 0;
1000         $course2['groupmode'] = 0;
1001         $course2['groupmodeforce'] = 0;
1002         $course2['defaultgroupingid'] = 0;
1003         $course2['enablecompletion'] = 1;
1004         $course2['lang'] = 'en';
1005         $course2['forcetheme'] = 'base';
1006         $courses = array($course1, $course2);
1008         $updatedcoursewarnings = core_course_external::update_courses($courses);
1009         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1010                                                                     $updatedcoursewarnings);
1011         $COURSE = $origcourse; // Restore $COURSE. Instead of using the OLD one set by the previous line.
1013         // Check that right number of courses were created.
1014         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1016         // Check that the courses were correctly created.
1017         foreach ($courses as $course) {
1018             $courseinfo = course_get_format($course['id'])->get_course();
1019             if ($course['id'] == $course2['id']) {
1020                 $this->assertEquals($course2['fullname'], $courseinfo->fullname);
1021                 $this->assertEquals($course2['shortname'], $courseinfo->shortname);
1022                 $this->assertEquals($course2['categoryid'], $courseinfo->category);
1023                 $this->assertEquals($course2['idnumber'], $courseinfo->idnumber);
1024                 $this->assertEquals($course2['summary'], $courseinfo->summary);
1025                 $this->assertEquals($course2['summaryformat'], $courseinfo->summaryformat);
1026                 $this->assertEquals($course2['format'], $courseinfo->format);
1027                 $this->assertEquals($course2['showgrades'], $courseinfo->showgrades);
1028                 $this->assertEquals($course2['newsitems'], $courseinfo->newsitems);
1029                 $this->assertEquals($course2['startdate'], $courseinfo->startdate);
1030                 $this->assertEquals($course2['numsections'], $courseinfo->numsections);
1031                 $this->assertEquals($course2['maxbytes'], $courseinfo->maxbytes);
1032                 $this->assertEquals($course2['showreports'], $courseinfo->showreports);
1033                 $this->assertEquals($course2['visible'], $courseinfo->visible);
1034                 $this->assertEquals($course2['hiddensections'], $courseinfo->hiddensections);
1035                 $this->assertEquals($course2['groupmode'], $courseinfo->groupmode);
1036                 $this->assertEquals($course2['groupmodeforce'], $courseinfo->groupmodeforce);
1037                 $this->assertEquals($course2['defaultgroupingid'], $courseinfo->defaultgroupingid);
1038                 $this->assertEquals($course2['lang'], $courseinfo->lang);
1040                 if (!empty($CFG->allowcoursethemes)) {
1041                     $this->assertEquals($course2['forcetheme'], $courseinfo->theme);
1042                 }
1044                 if (completion_info::is_enabled_for_site()) {
1045                     $this->assertEquals($course2['enabledcompletion'], $courseinfo->enablecompletion);
1046                 }
1047             } else if ($course['id'] == $course1['id']) {
1048                 $this->assertEquals($course1['fullname'], $courseinfo->fullname);
1049                 $this->assertEquals($course1['shortname'], $courseinfo->shortname);
1050                 $this->assertEquals($course1['categoryid'], $courseinfo->category);
1051                 $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
1052                 $this->assertEquals('topics', $courseinfo->format);
1053                 $this->assertEquals(5, $courseinfo->numsections);
1054                 $this->assertEquals(0, $courseinfo->newsitems);
1055                 $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
1056             } else {
1057                 throw moodle_exception('Unexpected shortname');
1058             }
1059         }
1061         $courses = array($course1);
1062         // Try update course without update capability.
1063         $user = self::getDataGenerator()->create_user();
1064         $this->setUser($user);
1065         $this->unassignUserCapability('moodle/course:update', $contextid, $roleid);
1066         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1067         $updatedcoursewarnings = core_course_external::update_courses($courses);
1068         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1069                                                                     $updatedcoursewarnings);
1070         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1072         // Try update course category without capability.
1073         $this->assignUserCapability('moodle/course:update', $contextid, $roleid);
1074         $this->unassignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1075         $user = self::getDataGenerator()->create_user();
1076         $this->setUser($user);
1077         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1078         $course1['categoryid'] = $category2->id;
1079         $courses = array($course1);
1080         $updatedcoursewarnings = core_course_external::update_courses($courses);
1081         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1082                                                                     $updatedcoursewarnings);
1083         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1085         // Try update course fullname without capability.
1086         $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1087         $this->unassignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1088         $user = self::getDataGenerator()->create_user();
1089         $this->setUser($user);
1090         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1091         $updatedcoursewarnings = core_course_external::update_courses($courses);
1092         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1093                                                                     $updatedcoursewarnings);
1094         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1095         $course1['fullname'] = 'Testing fullname without permission';
1096         $courses = array($course1);
1097         $updatedcoursewarnings = core_course_external::update_courses($courses);
1098         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1099                                                                     $updatedcoursewarnings);
1100         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1102         // Try update course shortname without capability.
1103         $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1104         $this->unassignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1105         $user = self::getDataGenerator()->create_user();
1106         $this->setUser($user);
1107         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1108         $updatedcoursewarnings = core_course_external::update_courses($courses);
1109         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1110                                                                     $updatedcoursewarnings);
1111         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1112         $course1['shortname'] = 'Testing shortname without permission';
1113         $courses = array($course1);
1114         $updatedcoursewarnings = core_course_external::update_courses($courses);
1115         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1116                                                                     $updatedcoursewarnings);
1117         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1119         // Try update course idnumber without capability.
1120         $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1121         $this->unassignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1122         $user = self::getDataGenerator()->create_user();
1123         $this->setUser($user);
1124         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1125         $updatedcoursewarnings = core_course_external::update_courses($courses);
1126         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1127                                                                     $updatedcoursewarnings);
1128         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1129         $course1['idnumber'] = 'NEWIDNUMBER';
1130         $courses = array($course1);
1131         $updatedcoursewarnings = core_course_external::update_courses($courses);
1132         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1133                                                                     $updatedcoursewarnings);
1134         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1136         // Try update course summary without capability.
1137         $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1138         $this->unassignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1139         $user = self::getDataGenerator()->create_user();
1140         $this->setUser($user);
1141         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1142         $updatedcoursewarnings = core_course_external::update_courses($courses);
1143         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1144                                                                     $updatedcoursewarnings);
1145         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1146         $course1['summary'] = 'New summary';
1147         $courses = array($course1);
1148         $updatedcoursewarnings = core_course_external::update_courses($courses);
1149         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1150                                                                     $updatedcoursewarnings);
1151         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1153         // Try update course with invalid summary format.
1154         $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1155         $user = self::getDataGenerator()->create_user();
1156         $this->setUser($user);
1157         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1158         $updatedcoursewarnings = core_course_external::update_courses($courses);
1159         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1160                                                                     $updatedcoursewarnings);
1161         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1162         $course1['summaryformat'] = 10;
1163         $courses = array($course1);
1164         $updatedcoursewarnings = core_course_external::update_courses($courses);
1165         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1166                                                                     $updatedcoursewarnings);
1167         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1169         // Try update course visibility without capability.
1170         $this->unassignUserCapability('moodle/course:visibility', $contextid, $roleid);
1171         $user = self::getDataGenerator()->create_user();
1172         $this->setUser($user);
1173         self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1174         $course1['summaryformat'] = FORMAT_MOODLE;
1175         $courses = array($course1);
1176         $updatedcoursewarnings = core_course_external::update_courses($courses);
1177         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1178                                                                     $updatedcoursewarnings);
1179         $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1180         $course1['visible'] = 0;
1181         $courses = array($course1);
1182         $updatedcoursewarnings = core_course_external::update_courses($courses);
1183         $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1184                                                                     $updatedcoursewarnings);
1185         $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1186     }
1188     /**
1189      * Test delete course_module.
1190      */
1191     public function test_delete_modules() {
1192         global $DB;
1194         // Ensure we reset the data after this test.
1195         $this->resetAfterTest(true);
1197         // Create a user.
1198         $user = self::getDataGenerator()->create_user();
1200         // Set the tests to run as the user.
1201         self::setUser($user);
1203         // Create a course to add the modules.
1204         $course = self::getDataGenerator()->create_course();
1206         // Create two test modules.
1207         $record = new stdClass();
1208         $record->course = $course->id;
1209         $module1 = self::getDataGenerator()->create_module('forum', $record);
1210         $module2 = self::getDataGenerator()->create_module('assign', $record);
1212         // Check the forum was correctly created.
1213         $this->assertEquals(1, $DB->count_records('forum', array('id' => $module1->id)));
1215         // Check the assignment was correctly created.
1216         $this->assertEquals(1, $DB->count_records('assign', array('id' => $module2->id)));
1218         // Check data exists in the course modules table.
1219         $this->assertEquals(2, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
1220                 array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
1222         // Enrol the user in the course.
1223         $enrol = enrol_get_plugin('manual');
1224         $enrolinstances = enrol_get_instances($course->id, true);
1225         foreach ($enrolinstances as $courseenrolinstance) {
1226             if ($courseenrolinstance->enrol == "manual") {
1227                 $instance = $courseenrolinstance;
1228                 break;
1229             }
1230         }
1231         $enrol->enrol_user($instance, $user->id);
1233         // Assign capabilities to delete module 1.
1234         $modcontext = context_module::instance($module1->cmid);
1235         $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id);
1237         // Assign capabilities to delete module 2.
1238         $modcontext = context_module::instance($module2->cmid);
1239         $newrole = create_role('Role 2', 'role2', 'Role 2 description');
1240         $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id, $newrole);
1242         // Deleting these module instances.
1243         core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1245         // Check the forum was deleted.
1246         $this->assertEquals(0, $DB->count_records('forum', array('id' => $module1->id)));
1248         // Check the assignment was deleted.
1249         $this->assertEquals(0, $DB->count_records('assign', array('id' => $module2->id)));
1251         // Check we retrieve no data in the course modules table.
1252         $this->assertEquals(0, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
1253                 array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
1255         // Call with non-existent course module id and ensure exception thrown.
1256         try {
1257             core_course_external::delete_modules(array('1337'));
1258             $this->fail('Exception expected due to missing course module.');
1259         } catch (dml_missing_record_exception $e) {
1260             $this->assertEquals('invalidcoursemodule', $e->errorcode);
1261         }
1263         // Create two modules.
1264         $module1 = self::getDataGenerator()->create_module('forum', $record);
1265         $module2 = self::getDataGenerator()->create_module('assign', $record);
1267         // Since these modules were recreated the user will not have capabilities
1268         // to delete them, ensure exception is thrown if they try.
1269         try {
1270             core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1271             $this->fail('Exception expected due to missing capability.');
1272         } catch (moodle_exception $e) {
1273             $this->assertEquals('nopermissions', $e->errorcode);
1274         }
1276         // Unenrol user from the course.
1277         $enrol->unenrol_user($instance, $user->id);
1279         // Try and delete modules from the course the user was unenrolled in, make sure exception thrown.
1280         try {
1281             core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1282             $this->fail('Exception expected due to being unenrolled from the course.');
1283         } catch (moodle_exception $e) {
1284             $this->assertEquals('requireloginerror', $e->errorcode);
1285         }
1286     }
1288     /**
1289      * Test import_course into an empty course
1290      */
1291     public function test_import_course_empty() {
1292         global $USER;
1294         $this->resetAfterTest(true);
1296         $course1  = self::getDataGenerator()->create_course();
1297         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course1->id, 'name' => 'Forum test'));
1298         $page = $this->getDataGenerator()->create_module('page', array('course' => $course1->id, 'name' => 'Page test'));
1300         $course2  = self::getDataGenerator()->create_course();
1302         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1303         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1305         // Verify the state of the courses before we do the import.
1306         $this->assertCount(2, $course1cms);
1307         $this->assertEmpty($course2cms);
1309         // Setup the user to run the operation (ugly hack because validate_context() will
1310         // fail as the email is not set by $this->setAdminUser()).
1311         $this->setAdminUser();
1312         $USER->email = 'emailtopass@example.com';
1314         // Import from course1 to course2.
1315         core_course_external::import_course($course1->id, $course2->id, 0);
1317         // Verify that now we have two modules in both courses.
1318         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1319         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1320         $this->assertCount(2, $course1cms);
1321         $this->assertCount(2, $course2cms);
1323         // Verify that the names transfered across correctly.
1324         foreach ($course2cms as $cm) {
1325             if ($cm->modname === 'page') {
1326                 $this->assertEquals($cm->name, $page->name);
1327             } else if ($cm->modname === 'forum') {
1328                 $this->assertEquals($cm->name, $forum->name);
1329             } else {
1330                 $this->fail('Unknown CM found.');
1331             }
1332         }
1333     }
1335     /**
1336      * Test import_course into an filled course
1337      */
1338     public function test_import_course_filled() {
1339         global $USER;
1341         $this->resetAfterTest(true);
1343         // Add forum and page to course1.
1344         $course1  = self::getDataGenerator()->create_course();
1345         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1346         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
1348         // Add quiz to course 2.
1349         $course2  = self::getDataGenerator()->create_course();
1350         $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
1352         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1353         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1355         // Verify the state of the courses before we do the import.
1356         $this->assertCount(2, $course1cms);
1357         $this->assertCount(1, $course2cms);
1359         // Setup the user to run the operation (ugly hack because validate_context() will
1360         // fail as the email is not set by $this->setAdminUser()).
1361         $this->setAdminUser();
1362         $USER->email = 'emailtopass@example.com';
1364         // Import from course1 to course2 without deleting content.
1365         core_course_external::import_course($course1->id, $course2->id, 0);
1367         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1369         // Verify that now we have three modules in course2.
1370         $this->assertCount(3, $course2cms);
1372         // Verify that the names transfered across correctly.
1373         foreach ($course2cms as $cm) {
1374             if ($cm->modname === 'page') {
1375                 $this->assertEquals($cm->name, $page->name);
1376             } else if ($cm->modname === 'forum') {
1377                 $this->assertEquals($cm->name, $forum->name);
1378             } else if ($cm->modname === 'quiz') {
1379                 $this->assertEquals($cm->name, $quiz->name);
1380             } else {
1381                 $this->fail('Unknown CM found.');
1382             }
1383         }
1384     }
1386     /**
1387      * Test import_course with only blocks set to backup
1388      */
1389     public function test_import_course_blocksonly() {
1390         global $USER, $DB;
1392         $this->resetAfterTest(true);
1394         // Add forum and page to course1.
1395         $course1  = self::getDataGenerator()->create_course();
1396         $course1ctx = context_course::instance($course1->id);
1397         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1398         $block = $this->getDataGenerator()->create_block('online_users', array('parentcontextid' => $course1ctx->id));
1400         $course2  = self::getDataGenerator()->create_course();
1401         $course2ctx = context_course::instance($course2->id);
1402         $initialblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
1403         $initialcmcount = count(get_fast_modinfo($course2->id)->get_cms());
1405         // Setup the user to run the operation (ugly hack because validate_context() will
1406         // fail as the email is not set by $this->setAdminUser()).
1407         $this->setAdminUser();
1408         $USER->email = 'emailtopass@example.com';
1410         // Import from course1 to course2 without deleting content, but excluding
1411         // activities.
1412         $options = array(
1413             array('name' => 'activities', 'value' => 0),
1414             array('name' => 'blocks', 'value' => 1),
1415             array('name' => 'filters', 'value' => 0),
1416         );
1418         core_course_external::import_course($course1->id, $course2->id, 0, $options);
1420         $newcmcount = count(get_fast_modinfo($course2->id)->get_cms());
1421         $newblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
1422         // Check that course modules haven't changed, but that blocks have.
1423         $this->assertEquals($initialcmcount, $newcmcount);
1424         $this->assertEquals(($initialblockcount + 1), $newblockcount);
1425     }
1427     /**
1428      * Test import_course into an filled course, deleting content.
1429      */
1430     public function test_import_course_deletecontent() {
1431         global $USER;
1432         $this->resetAfterTest(true);
1434         // Add forum and page to course1.
1435         $course1  = self::getDataGenerator()->create_course();
1436         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1437         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
1439         // Add quiz to course 2.
1440         $course2  = self::getDataGenerator()->create_course();
1441         $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
1443         $course1cms = get_fast_modinfo($course1->id)->get_cms();
1444         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1446         // Verify the state of the courses before we do the import.
1447         $this->assertCount(2, $course1cms);
1448         $this->assertCount(1, $course2cms);
1450         // Setup the user to run the operation (ugly hack because validate_context() will
1451         // fail as the email is not set by $this->setAdminUser()).
1452         $this->setAdminUser();
1453         $USER->email = 'emailtopass@example.com';
1455         // Import from course1 to course2,  deleting content.
1456         core_course_external::import_course($course1->id, $course2->id, 1);
1458         $course2cms = get_fast_modinfo($course2->id)->get_cms();
1460         // Verify that now we have two modules in course2.
1461         $this->assertCount(2, $course2cms);
1463         // Verify that the course only contains the imported modules.
1464         foreach ($course2cms as $cm) {
1465             if ($cm->modname === 'page') {
1466                 $this->assertEquals($cm->name, $page->name);
1467             } else if ($cm->modname === 'forum') {
1468                 $this->assertEquals($cm->name, $forum->name);
1469             } else {
1470                 $this->fail('Unknown CM found: '.$cm->name);
1471             }
1472         }
1473     }
1475     /**
1476      * Ensure import_course handles incorrect deletecontent option correctly.
1477      */
1478     public function test_import_course_invalid_deletecontent_option() {
1479         $this->resetAfterTest(true);
1481         $course1  = self::getDataGenerator()->create_course();
1482         $course2  = self::getDataGenerator()->create_course();
1484         $this->setExpectedException('moodle_exception', get_string('invalidextparam', 'webservice', -1));
1485         // Import from course1 to course2, with invalid option
1486         core_course_external::import_course($course1->id, $course2->id, -1);;
1487     }
1489     /**
1490      * Test view_course function
1491      */
1492     public function test_view_course() {
1494         $this->resetAfterTest();
1496         // Course without sections.
1497         $course = $this->getDataGenerator()->create_course(array('numsections' => 5), array('createsections' => true));
1498         $this->setAdminUser();
1500         // Redirect events to the sink, so we can recover them later.
1501         $sink = $this->redirectEvents();
1503         $result = core_course_external::view_course($course->id, 1);
1504         $result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result);
1505         $events = $sink->get_events();
1506         $event = reset($events);
1508         // Check the event details are correct.
1509         $this->assertInstanceOf('\core\event\course_viewed', $event);
1510         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1511         $this->assertEquals(1, $event->other['coursesectionnumber']);
1513         $result = core_course_external::view_course($course->id);
1514         $result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result);
1515         $events = $sink->get_events();
1516         $event = array_pop($events);
1517         $sink->close();
1519         // Check the event details are correct.
1520         $this->assertInstanceOf('\core\event\course_viewed', $event);
1521         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1522         $this->assertEmpty($event->other);
1524     }
1526     /**
1527      * Test get_course_module
1528      */
1529     public function test_get_course_module() {
1530         global $DB;
1532         $this->resetAfterTest(true);
1534         $this->setAdminUser();
1535         $course = self::getDataGenerator()->create_course();
1536         $record = array(
1537             'course' => $course->id,
1538             'name' => 'First Chat'
1539         );
1540         $options = array(
1541             'idnumber' => 'ABC',
1542             'visible' => 0
1543         );
1544         // Hidden activity.
1545         $chat = self::getDataGenerator()->create_module('chat', $record, $options);
1547         // Test admin user can see the complete hidden activity.
1548         $result = core_course_external::get_course_module($chat->cmid);
1549         $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result);
1551         $this->assertCount(0, $result['warnings']);
1552         // Test we retrieve all the fields.
1553         $this->assertCount(21, $result['cm']);
1554         $this->assertEquals($record['name'], $result['cm']['name']);
1555         $this->assertEquals($options['idnumber'], $result['cm']['idnumber']);
1557         $student = $this->getDataGenerator()->create_user();
1558         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1560         self::getDataGenerator()->enrol_user($student->id,  $course->id, $studentrole->id);
1561         $this->setUser($student);
1563         // The user shouldn't be able to see the activity.
1564         try {
1565             core_course_external::get_course_module($chat->cmid);
1566             $this->fail('Exception expected due to invalid permissions.');
1567         } catch (moodle_exception $e) {
1568             $this->assertEquals('requireloginerror', $e->errorcode);
1569         }
1571         // Make module visible.
1572         set_coursemodule_visible($chat->cmid, 1);
1574         // Test student user.
1575         $result = core_course_external::get_course_module($chat->cmid);
1576         $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result);
1578         $this->assertCount(0, $result['warnings']);
1579         // Test we retrieve only the few files we can see.
1580         $this->assertCount(11, $result['cm']);
1581         $this->assertEquals($chat->cmid, $result['cm']['id']);
1582         $this->assertEquals($course->id, $result['cm']['course']);
1583         $this->assertEquals('chat', $result['cm']['modname']);
1584         $this->assertEquals($chat->id, $result['cm']['instance']);
1586     }