MDL-52811 course: fix unit tests broken by setforcedlanguage
[moodle.git] / course / tests / externallib_test.php
CommitLineData
2a7a0216
JM
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/>.
16
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 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29
30require_once($CFG->dirroot . '/webservice/tests/helpers.php');
31
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 */
8252b7c2 40class core_course_externallib_testcase extends externallib_advanced_testcase {
2a7a0216
JM
41
42 /**
43 * Tests set up
44 */
45 protected function setUp() {
46 global $CFG;
47 require_once($CFG->dirroot . '/course/externallib.php');
48 }
49
50 /**
51 * Test create_categories
52 */
53 public function test_create_categories() {
54
55 global $DB;
56
57 $this->resetAfterTest(true);
58
59 // Set the required capabilities by the external function
60 $contextid = context_system::instance()->id;
61 $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
62
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';
644fcbb6 70 $category2->theme = 'bootstrapbase';
2a7a0216
JM
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 );
76
77 $createdcats = core_course_external::create_categories($categories);
78
fb695f6e
JM
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);
81
2a7a0216
JM
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']);
85
86 // Save the ids.
87 $category1->id = $createdcats[0]['id'];
88 $category2->id = $createdcats[1]['id'];
89
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 );
96
97 $createdsubcats = core_course_external::create_categories($subcategories);
98
fb695f6e
JM
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);
101
2a7a0216
JM
102 // Confirm that sub categories were inserted correctly.
103 $this->assertEquals($category3->name, $createdsubcats[0]['name']);
104
105 // Save the ids.
106 $category3->id = $createdsubcats[0]['id'];
107
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));
113
db1eed70
MG
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);
2a7a0216
JM
120
121 // Call without required capability
122 $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
52f3e060 123 $this->expectException('required_capability_exception');
2a7a0216
JM
124 $createdsubcats = core_course_external::create_categories($subcategories);
125
126 }
127
128 /**
129 * Test delete categories
130 */
131 public function test_delete_categories() {
132 global $DB;
133
134 $this->resetAfterTest(true);
135
136 // Set the required capabilities by the external function
137 $contextid = context_system::instance()->id;
138 $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
139
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));
148
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 ));
154
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);
159
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);
163
164 // Call without required capability
165 $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
52f3e060 166 $this->expectException('required_capability_exception');
2a7a0216
JM
167 $createdsubcats = core_course_external::delete_categories(
168 array(array('id' => $category3->id)));
169 }
170
171 /**
172 * Test get categories
173 */
174 public function test_get_categories() {
175 global $DB;
176
177 $this->resetAfterTest(true);
7d6c58bc
JM
178
179 $generatedcats = array();
2a7a0216
JM
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);
7d6c58bc 185 $generatedcats[$category1->id] = $category1;
2a7a0216
JM
186 $category2 = self::getDataGenerator()->create_category(
187 array('parent' => $category1->id));
7d6c58bc 188 $generatedcats[$category2->id] = $category2;
2a7a0216
JM
189 $category6 = self::getDataGenerator()->create_category(
190 array('parent' => $category1->id, 'visible' => 0));
7d6c58bc 191 $generatedcats[$category6->id] = $category6;
2a7a0216 192 $category3 = self::getDataGenerator()->create_category();
7d6c58bc 193 $generatedcats[$category3->id] = $category3;
2a7a0216
JM
194 $category4 = self::getDataGenerator()->create_category(
195 array('parent' => $category3->id));
7d6c58bc 196 $generatedcats[$category4->id] = $category4;
2a7a0216
JM
197 $category5 = self::getDataGenerator()->create_category(
198 array('parent' => $category4->id));
7d6c58bc 199 $generatedcats[$category5->id] = $category5;
2a7a0216
JM
200
201 // Set the required capabilities by the external function.
202 $context = context_system::instance();
203 $roleid = $this->assignUserCapability('moodle/category:manage', $context->id);
204
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);
209
fb695f6e
JM
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);
212
2a7a0216
JM
213 // Check we retrieve the good total number of categories.
214 $this->assertEquals(2, count($categories));
215
216 // Check the return values
7d6c58bc
JM
217 foreach ($categories as $category) {
218 $generatedcat = $generatedcats[$category['id']];
219 $this->assertEquals($category['idnumber'], $generatedcat->idnumber);
220 $this->assertEquals($category['name'], $generatedcat->name);
46be1d58
MG
221 // Description was converted to the HTML format.
222 $this->assertEquals($category['description'], format_text($generatedcat->description, FORMAT_MOODLE, array('para' => false)));
7d6c58bc
JM
223 $this->assertEquals($category['descriptionformat'], FORMAT_HTML);
224 }
2a7a0216 225
c1da311a
JL
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);
230
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);
233
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);
243
2a7a0216
JM
244 // Check different params.
245 $categories = core_course_external::get_categories(array(
246 array('key' => 'id', 'value' => $category1->id),
c1da311a 247 array('key' => 'ids', 'value' => $category1->id),
2a7a0216
JM
248 array('key' => 'idnumber', 'value' => $category1->idnumber),
249 array('key' => 'visible', 'value' => 1)), 0);
fb695f6e
JM
250
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);
253
2a7a0216
JM
254 $this->assertEquals(1, count($categories));
255
b8b1be15
JL
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);
263
264 $this->assertEquals(1, count($categories));
265
2a7a0216
JM
266 // Retrieve categories from parent.
267 $categories = core_course_external::get_categories(array(
268 array('key' => 'parent', 'value' => $category3->id)), 1);
bdf9f4d4
JL
269 $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
270
2a7a0216
JM
271 $this->assertEquals(2, count($categories));
272
273 // Retrieve all categories.
274 $categories = core_course_external::get_categories();
fb695f6e
JM
275
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);
278
2a7a0216
JM
279 $this->assertEquals($DB->count_records('course_categories'), count($categories));
280
2a7a0216 281 $this->unassignUserCapability('moodle/category:manage', $context->id, $roleid);
7180cdc7
PFO
282
283 // Ensure maxdepthcategory is 2 and retrieve all categories without category:manage capability. It should retrieve all
284 // visible categories as well.
285 set_config('maxcategorydepth', 2);
286 $categories = core_course_external::get_categories();
287
288 // We need to execute the return values cleaning process to simulate the web service server.
289 $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
290
291 $this->assertEquals($DB->count_records('course_categories', array('visible' => 1)), count($categories));
292
293 // Call without required capability (it will fail cause of the search on idnumber).
52f3e060 294 $this->expectException('moodle_exception');
2a7a0216
JM
295 $categories = core_course_external::get_categories(array(
296 array('key' => 'id', 'value' => $category1->id),
297 array('key' => 'idnumber', 'value' => $category1->idnumber),
298 array('key' => 'visible', 'value' => 1)), 0);
299 }
300
301 /**
302 * Test update_categories
303 */
304 public function test_update_categories() {
305 global $DB;
306
307 $this->resetAfterTest(true);
308
309 // Set the required capabilities by the external function
310 $contextid = context_system::instance()->id;
311 $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
312
313 // Create base categories.
314 $category1data['idnumber'] = 'idnumbercat1';
315 $category1data['name'] = 'Category 1 for PHPunit test';
316 $category1data['description'] = 'Category 1 description';
317 $category1data['descriptionformat'] = FORMAT_MOODLE;
318 $category1 = self::getDataGenerator()->create_category($category1data);
319 $category2 = self::getDataGenerator()->create_category(
320 array('parent' => $category1->id));
321 $category3 = self::getDataGenerator()->create_category();
322 $category4 = self::getDataGenerator()->create_category(
323 array('parent' => $category3->id));
324 $category5 = self::getDataGenerator()->create_category(
325 array('parent' => $category4->id));
326
327 // We update all category1 attribut.
328 // Then we move cat4 and cat5 parent: cat3 => cat1
329 $categories = array(
330 array('id' => $category1->id,
331 'name' => $category1->name . '_updated',
332 'idnumber' => $category1->idnumber . '_updated',
333 'description' => $category1->description . '_updated',
334 'descriptionformat' => FORMAT_HTML,
335 'theme' => $category1->theme),
336 array('id' => $category4->id, 'parent' => $category1->id));
337
338 core_course_external::update_categories($categories);
339
340 // Check the values were updated.
341 $dbcategories = $DB->get_records_select('course_categories',
342 'id IN (' . $category1->id . ',' . $category2->id . ',' . $category2->id
343 . ',' . $category3->id . ',' . $category4->id . ',' . $category5->id .')');
344 $this->assertEquals($category1->name . '_updated',
345 $dbcategories[$category1->id]->name);
346 $this->assertEquals($category1->idnumber . '_updated',
347 $dbcategories[$category1->id]->idnumber);
348 $this->assertEquals($category1->description . '_updated',
349 $dbcategories[$category1->id]->description);
350 $this->assertEquals(FORMAT_HTML, $dbcategories[$category1->id]->descriptionformat);
351
352 // Check that category4 and category5 have been properly moved.
353 $this->assertEquals('/' . $category1->id . '/' . $category4->id,
354 $dbcategories[$category4->id]->path);
355 $this->assertEquals('/' . $category1->id . '/' . $category4->id . '/' . $category5->id,
356 $dbcategories[$category5->id]->path);
357
358 // Call without required capability.
359 $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
52f3e060 360 $this->expectException('required_capability_exception');
2a7a0216
JM
361 core_course_external::update_categories($categories);
362 }
363
1dac440f
EL
364 /**
365 * Test create_courses numsections
366 */
367 public function test_create_course_numsections() {
368 global $DB;
369
370 $this->resetAfterTest(true);
371
372 // Set the required capabilities by the external function.
373 $contextid = context_system::instance()->id;
374 $roleid = $this->assignUserCapability('moodle/course:create', $contextid);
375 $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
376
377 $numsections = 10;
378 $category = self::getDataGenerator()->create_category();
379
380 // Create base categories.
381 $course1['fullname'] = 'Test course 1';
382 $course1['shortname'] = 'Testcourse1';
383 $course1['categoryid'] = $category->id;
384 $course1['courseformatoptions'][] = array('name' => 'numsections', 'value' => $numsections);
385
386 $courses = array($course1);
387
388 $createdcourses = core_course_external::create_courses($courses);
389 foreach ($createdcourses as $createdcourse) {
390 $existingsections = $DB->get_records('course_sections', array('course' => $createdcourse['id']));
391 $modinfo = get_fast_modinfo($createdcourse['id']);
392 $sections = $modinfo->get_section_info_all();
393 $this->assertEquals(count($sections), $numsections + 1); // Includes generic section.
394 $this->assertEquals(count($existingsections), $numsections + 1); // Includes generic section.
395 }
396 }
397
2a7a0216
JM
398 /**
399 * Test create_courses
400 */
401 public function test_create_courses() {
402 global $DB;
403
404 $this->resetAfterTest(true);
405
821676f5
JM
406 // Enable course completion.
407 set_config('enablecompletion', 1);
141e7d87
DP
408 // Enable course themes.
409 set_config('allowcoursethemes', 1);
821676f5 410
2a7a0216
JM
411 // Set the required capabilities by the external function
412 $contextid = context_system::instance()->id;
413 $roleid = $this->assignUserCapability('moodle/course:create', $contextid);
414 $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
4a9624af 415 $this->assignUserCapability('moodle/course:setforcedlanguage', $contextid, $roleid);
2a7a0216
JM
416
417 $category = self::getDataGenerator()->create_category();
418
419 // Create base categories.
420 $course1['fullname'] = 'Test course 1';
421 $course1['shortname'] = 'Testcourse1';
422 $course1['categoryid'] = $category->id;
423 $course2['fullname'] = 'Test course 2';
424 $course2['shortname'] = 'Testcourse2';
425 $course2['categoryid'] = $category->id;
426 $course2['idnumber'] = 'testcourse2idnumber';
427 $course2['summary'] = 'Description for course 2';
428 $course2['summaryformat'] = FORMAT_MOODLE;
429 $course2['format'] = 'weeks';
430 $course2['showgrades'] = 1;
431 $course2['newsitems'] = 3;
fbcdb0d7
DNA
432 $course2['startdate'] = 1420092000; // 01/01/2015.
433 $course2['enddate'] = 1422669600; // 01/31/2015.
2a7a0216
JM
434 $course2['numsections'] = 4;
435 $course2['maxbytes'] = 100000;
436 $course2['showreports'] = 1;
437 $course2['visible'] = 0;
438 $course2['hiddensections'] = 0;
439 $course2['groupmode'] = 0;
440 $course2['groupmodeforce'] = 0;
441 $course2['defaultgroupingid'] = 0;
442 $course2['enablecompletion'] = 1;
2a7a0216
JM
443 $course2['completionnotify'] = 1;
444 $course2['lang'] = 'en';
644fcbb6 445 $course2['forcetheme'] = 'bootstrapbase';
a526c706 446 $course2['courseformatoptions'][] = array('name' => 'automaticenddate', 'value' => 0);
0e984d98
MG
447 $course3['fullname'] = 'Test course 3';
448 $course3['shortname'] = 'Testcourse3';
449 $course3['categoryid'] = $category->id;
450 $course3['format'] = 'topics';
451 $course3options = array('numsections' => 8,
452 'hiddensections' => 1,
453 'coursedisplay' => 1);
8d8d4da4 454 $course3['courseformatoptions'] = array();
0e984d98 455 foreach ($course3options as $key => $value) {
8d8d4da4 456 $course3['courseformatoptions'][] = array('name' => $key, 'value' => $value);
0e984d98 457 }
821676f5 458 $courses = array($course1, $course2, $course3);
2a7a0216
JM
459
460 $createdcourses = core_course_external::create_courses($courses);
461
fb695f6e
JM
462 // We need to execute the return values cleaning process to simulate the web service server.
463 $createdcourses = external_api::clean_returnvalue(core_course_external::create_courses_returns(), $createdcourses);
464
2a7a0216 465 // Check that right number of courses were created.
821676f5 466 $this->assertEquals(3, count($createdcourses));
2a7a0216
JM
467
468 // Check that the courses were correctly created.
469 foreach ($createdcourses as $createdcourse) {
850acb35 470 $courseinfo = course_get_format($createdcourse['id'])->get_course();
2a7a0216
JM
471
472 if ($createdcourse['shortname'] == $course2['shortname']) {
850acb35
MG
473 $this->assertEquals($courseinfo->fullname, $course2['fullname']);
474 $this->assertEquals($courseinfo->shortname, $course2['shortname']);
475 $this->assertEquals($courseinfo->category, $course2['categoryid']);
476 $this->assertEquals($courseinfo->idnumber, $course2['idnumber']);
477 $this->assertEquals($courseinfo->summary, $course2['summary']);
478 $this->assertEquals($courseinfo->summaryformat, $course2['summaryformat']);
479 $this->assertEquals($courseinfo->format, $course2['format']);
480 $this->assertEquals($courseinfo->showgrades, $course2['showgrades']);
481 $this->assertEquals($courseinfo->newsitems, $course2['newsitems']);
482 $this->assertEquals($courseinfo->startdate, $course2['startdate']);
fbcdb0d7 483 $this->assertEquals($courseinfo->enddate, $course2['enddate']);
89b909f6 484 $this->assertEquals(course_get_format($createdcourse['id'])->get_last_section_number(), $course2['numsections']);
850acb35
MG
485 $this->assertEquals($courseinfo->maxbytes, $course2['maxbytes']);
486 $this->assertEquals($courseinfo->showreports, $course2['showreports']);
487 $this->assertEquals($courseinfo->visible, $course2['visible']);
488 $this->assertEquals($courseinfo->hiddensections, $course2['hiddensections']);
489 $this->assertEquals($courseinfo->groupmode, $course2['groupmode']);
490 $this->assertEquals($courseinfo->groupmodeforce, $course2['groupmodeforce']);
491 $this->assertEquals($courseinfo->defaultgroupingid, $course2['defaultgroupingid']);
492 $this->assertEquals($courseinfo->completionnotify, $course2['completionnotify']);
493 $this->assertEquals($courseinfo->lang, $course2['lang']);
141e7d87 494 $this->assertEquals($courseinfo->theme, $course2['forcetheme']);
2a7a0216 495
821676f5
JM
496 // We enabled completion at the beginning of the test.
497 $this->assertEquals($courseinfo->enablecompletion, $course2['enablecompletion']);
2a7a0216
JM
498
499 } else if ($createdcourse['shortname'] == $course1['shortname']) {
500 $courseconfig = get_config('moodlecourse');
850acb35
MG
501 $this->assertEquals($courseinfo->fullname, $course1['fullname']);
502 $this->assertEquals($courseinfo->shortname, $course1['shortname']);
503 $this->assertEquals($courseinfo->category, $course1['categoryid']);
504 $this->assertEquals($courseinfo->summaryformat, FORMAT_HTML);
505 $this->assertEquals($courseinfo->format, $courseconfig->format);
506 $this->assertEquals($courseinfo->showgrades, $courseconfig->showgrades);
507 $this->assertEquals($courseinfo->newsitems, $courseconfig->newsitems);
508 $this->assertEquals($courseinfo->maxbytes, $courseconfig->maxbytes);
509 $this->assertEquals($courseinfo->showreports, $courseconfig->showreports);
510 $this->assertEquals($courseinfo->groupmode, $courseconfig->groupmode);
511 $this->assertEquals($courseinfo->groupmodeforce, $courseconfig->groupmodeforce);
512 $this->assertEquals($courseinfo->defaultgroupingid, 0);
0e984d98 513 } else if ($createdcourse['shortname'] == $course3['shortname']) {
850acb35
MG
514 $this->assertEquals($courseinfo->fullname, $course3['fullname']);
515 $this->assertEquals($courseinfo->shortname, $course3['shortname']);
516 $this->assertEquals($courseinfo->category, $course3['categoryid']);
517 $this->assertEquals($courseinfo->format, $course3['format']);
518 $this->assertEquals($courseinfo->hiddensections, $course3options['hiddensections']);
89b909f6
MG
519 $this->assertEquals(course_get_format($createdcourse['id'])->get_last_section_number(),
520 $course3options['numsections']);
850acb35 521 $this->assertEquals($courseinfo->coursedisplay, $course3options['coursedisplay']);
2a7a0216 522 } else {
75c597da 523 throw new moodle_exception('Unexpected shortname');
2a7a0216
JM
524 }
525 }
526
527 // Call without required capability
528 $this->unassignUserCapability('moodle/course:create', $contextid, $roleid);
52f3e060 529 $this->expectException('required_capability_exception');
2a7a0216
JM
530 $createdsubcats = core_course_external::create_courses($courses);
531 }
532
533 /**
534 * Test delete_courses
535 */
536 public function test_delete_courses() {
537 global $DB, $USER;
538
539 $this->resetAfterTest(true);
540
541 // Admin can delete a course.
542 $this->setAdminUser();
543 // Validate_context() will fail as the email is not set by $this->setAdminUser().
0fe86bbd 544 $USER->email = 'emailtopass@example.com';
2a7a0216
JM
545
546 $course1 = self::getDataGenerator()->create_course();
547 $course2 = self::getDataGenerator()->create_course();
548 $course3 = self::getDataGenerator()->create_course();
549
550 // Delete courses.
70f37963
JH
551 $result = core_course_external::delete_courses(array($course1->id, $course2->id));
552 $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
553 // Check for 0 warnings.
554 $this->assertEquals(0, count($result['warnings']));
2a7a0216
JM
555
556 // Check $course 1 and 2 are deleted.
557 $notdeletedcount = $DB->count_records_select('course',
558 'id IN ( ' . $course1->id . ',' . $course2->id . ')');
559 $this->assertEquals(0, $notdeletedcount);
560
70f37963
JH
561 // Try to delete non-existent course.
562 $result = core_course_external::delete_courses(array($course1->id));
563 $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
564 // Check for 1 warnings.
565 $this->assertEquals(1, count($result['warnings']));
566
567 // Try to delete Frontpage course.
568 $result = core_course_external::delete_courses(array(0));
569 $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
570 // Check for 1 warnings.
571 $this->assertEquals(1, count($result['warnings']));
572
573 // Fail when the user has access to course (enrolled) but does not have permission or is not admin.
574 $student1 = self::getDataGenerator()->create_user();
575 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
576 $this->getDataGenerator()->enrol_user($student1->id,
577 $course3->id,
578 $studentrole->id);
579 $this->setUser($student1);
580 $result = core_course_external::delete_courses(array($course3->id));
581 $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
582 // Check for 1 warnings.
583 $this->assertEquals(1, count($result['warnings']));
584
2a7a0216
JM
585 // Fail when the user is not allow to access the course (enrolled) or is not admin.
586 $this->setGuestUser();
52f3e060 587 $this->expectException('require_login_exception');
70f37963
JH
588
589 $result = core_course_external::delete_courses(array($course3->id));
590 $result = external_api::clean_returnvalue(core_course_external::delete_courses_returns(), $result);
2a7a0216
JM
591 }
592
593 /**
594 * Test get_courses
595 */
596 public function test_get_courses () {
597 global $DB;
598
599 $this->resetAfterTest(true);
600
7d6c58bc 601 $generatedcourses = array();
2a7a0216 602 $coursedata['idnumber'] = 'idnumbercourse1';
d889b587
JL
603 // Adding tags here to check that format_string is applied.
604 $coursedata['fullname'] = '<b>Course 1 for PHPunit test</b>';
605 $coursedata['shortname'] = '<b>Course 1 for PHPunit test</b>';
2a7a0216
JM
606 $coursedata['summary'] = 'Course 1 description';
607 $coursedata['summaryformat'] = FORMAT_MOODLE;
608 $course1 = self::getDataGenerator()->create_course($coursedata);
245d354c 609
7d6c58bc 610 $generatedcourses[$course1->id] = $course1;
2a7a0216 611 $course2 = self::getDataGenerator()->create_course();
7d6c58bc 612 $generatedcourses[$course2->id] = $course2;
0e984d98 613 $course3 = self::getDataGenerator()->create_course(array('format' => 'topics'));
7d6c58bc 614 $generatedcourses[$course3->id] = $course3;
2a7a0216
JM
615
616 // Set the required capabilities by the external function.
617 $context = context_system::instance();
618 $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
619 $this->assignUserCapability('moodle/course:update',
620 context_course::instance($course1->id)->id, $roleid);
621 $this->assignUserCapability('moodle/course:update',
622 context_course::instance($course2->id)->id, $roleid);
623 $this->assignUserCapability('moodle/course:update',
624 context_course::instance($course3->id)->id, $roleid);
625
626 $courses = core_course_external::get_courses(array('ids' =>
627 array($course1->id, $course2->id)));
628
fb695f6e
JM
629 // We need to execute the return values cleaning process to simulate the web service server.
630 $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
631
2a7a0216
JM
632 // Check we retrieve the good total number of categories.
633 $this->assertEquals(2, count($courses));
634
7d6c58bc 635 foreach ($courses as $course) {
d889b587 636 $coursecontext = context_course::instance($course['id']);
7d6c58bc
JM
637 $dbcourse = $generatedcourses[$course['id']];
638 $this->assertEquals($course['idnumber'], $dbcourse->idnumber);
d889b587
JL
639 $this->assertEquals($course['fullname'], external_format_string($dbcourse->fullname, $coursecontext->id));
640 $this->assertEquals($course['displayname'], external_format_string(get_course_display_name_for_list($dbcourse),
641 $coursecontext->id));
46be1d58
MG
642 // Summary was converted to the HTML format.
643 $this->assertEquals($course['summary'], format_text($dbcourse->summary, FORMAT_MOODLE, array('para' => false)));
7d6c58bc 644 $this->assertEquals($course['summaryformat'], FORMAT_HTML);
d889b587 645 $this->assertEquals($course['shortname'], external_format_string($dbcourse->shortname, $coursecontext->id));
7d6c58bc
JM
646 $this->assertEquals($course['categoryid'], $dbcourse->category);
647 $this->assertEquals($course['format'], $dbcourse->format);
648 $this->assertEquals($course['showgrades'], $dbcourse->showgrades);
649 $this->assertEquals($course['newsitems'], $dbcourse->newsitems);
650 $this->assertEquals($course['startdate'], $dbcourse->startdate);
fbcdb0d7 651 $this->assertEquals($course['enddate'], $dbcourse->enddate);
89b909f6 652 $this->assertEquals($course['numsections'], course_get_format($dbcourse)->get_last_section_number());
7d6c58bc
JM
653 $this->assertEquals($course['maxbytes'], $dbcourse->maxbytes);
654 $this->assertEquals($course['showreports'], $dbcourse->showreports);
655 $this->assertEquals($course['visible'], $dbcourse->visible);
656 $this->assertEquals($course['hiddensections'], $dbcourse->hiddensections);
657 $this->assertEquals($course['groupmode'], $dbcourse->groupmode);
658 $this->assertEquals($course['groupmodeforce'], $dbcourse->groupmodeforce);
659 $this->assertEquals($course['defaultgroupingid'], $dbcourse->defaultgroupingid);
660 $this->assertEquals($course['completionnotify'], $dbcourse->completionnotify);
661 $this->assertEquals($course['lang'], $dbcourse->lang);
662 $this->assertEquals($course['forcetheme'], $dbcourse->theme);
7d6c58bc 663 $this->assertEquals($course['enablecompletion'], $dbcourse->enablecompletion);
0e984d98 664 if ($dbcourse->format === 'topics') {
8d8d4da4 665 $this->assertEquals($course['courseformatoptions'], array(
8d8d4da4
MG
666 array('name' => 'hiddensections', 'value' => $dbcourse->hiddensections),
667 array('name' => 'coursedisplay', 'value' => $dbcourse->coursedisplay),
0e984d98
MG
668 ));
669 }
7d6c58bc 670 }
2a7a0216
JM
671
672 // Get all courses in the DB
673 $courses = core_course_external::get_courses(array());
fb695f6e
JM
674
675 // We need to execute the return values cleaning process to simulate the web service server.
676 $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
677
2a7a0216
JM
678 $this->assertEquals($DB->count_records('course'), count($courses));
679 }
680
a0cf7ee8
MG
681 /**
682 * Test get_courses without capability
683 */
684 public function test_get_courses_without_capability() {
685 $this->resetAfterTest(true);
686
687 $course1 = $this->getDataGenerator()->create_course();
688 $this->setUser($this->getDataGenerator()->create_user());
689
690 // No permissions are required to get the site course.
691 $courses = core_course_external::get_courses(array('ids' => [SITEID]));
692 $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
693
694 $this->assertEquals(1, count($courses));
695 $this->assertEquals('PHPUnit test site', $courses[0]['fullname']);
696 $this->assertEquals('site', $courses[0]['format']);
697
698 // Requesting course without being enrolled or capability to view it will throw an exception.
699 try {
700 core_course_external::get_courses(array('ids' => [$course1->id]));
701 $this->fail('Exception expected');
702 } catch (moodle_exception $e) {
703 $this->assertEquals(1, preg_match('/Course or activity not accessible. \(Not enrolled\)/', $e->getMessage()));
704 }
705 }
706
740c354f
JL
707 /**
708 * Test search_courses
709 */
710 public function test_search_courses () {
711
74fa9f76 712 global $DB;
740c354f
JL
713
714 $this->resetAfterTest(true);
715 $this->setAdminUser();
716 $generatedcourses = array();
717 $coursedata1['fullname'] = 'FIRST COURSE';
718 $course1 = self::getDataGenerator()->create_course($coursedata1);
245d354c
DW
719
720 $page = new moodle_page();
721 $page->set_course($course1);
722 $page->blocks->add_blocks([BLOCK_POS_LEFT => ['news_items'], BLOCK_POS_RIGHT => []], 'course-view-*');
723
740c354f
JL
724 $coursedata2['fullname'] = 'SECOND COURSE';
725 $course2 = self::getDataGenerator()->create_course($coursedata2);
245d354c
DW
726
727 $page = new moodle_page();
728 $page->set_course($course2);
729 $page->blocks->add_blocks([BLOCK_POS_LEFT => ['news_items'], BLOCK_POS_RIGHT => []], 'course-view-*');
740c354f
JL
730 // Search by name.
731 $results = core_course_external::search_courses('search', 'FIRST');
732 $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
733 $this->assertEquals($coursedata1['fullname'], $results['courses'][0]['fullname']);
734 $this->assertCount(1, $results['courses']);
735
736 // Create the forum.
737 $record = new stdClass();
738 $record->introformat = FORMAT_HTML;
739 $record->course = $course2->id;
740 // Set Aggregate type = Average of ratings.
741 $forum = self::getDataGenerator()->create_module('forum', $record);
742
743 // Search by module.
744 $results = core_course_external::search_courses('modulelist', 'forum');
745 $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
746 $this->assertEquals(1, $results['total']);
747
748 // Enable coursetag option.
749 set_config('block_tags_showcoursetags', true);
750 // Add tag 'TAG-LABEL ON SECOND COURSE' to Course2.
74fa9f76
MG
751 core_tag_tag::set_item_tags('core', 'course', $course2->id, context_course::instance($course2->id),
752 array('TAG-LABEL ON SECOND COURSE'));
753 $taginstance = $DB->get_record('tag_instance',
754 array('itemtype' => 'course', 'itemid' => $course2->id), '*', MUST_EXIST);
740c354f
JL
755 // Search by tagid.
756 $results = core_course_external::search_courses('tagid', $taginstance->tagid);
757 $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
758 $this->assertEquals($coursedata2['fullname'], $results['courses'][0]['fullname']);
759
760 // Search by block (use news_items default block).
761 $blockid = $DB->get_field('block', 'id', array('name' => 'news_items'));
762 $results = core_course_external::search_courses('blocklist', $blockid);
763 $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
764 $this->assertEquals(2, $results['total']);
765
766 // Now as a normal user.
767 $user = self::getDataGenerator()->create_user();
935ee1c6
EM
768
769 // Add a 3rd, hidden, course we shouldn't see, even when enrolled as student.
770 $coursedata3['fullname'] = 'HIDDEN COURSE';
771 $coursedata3['visible'] = 0;
772 $course3 = self::getDataGenerator()->create_course($coursedata3);
773 $this->getDataGenerator()->enrol_user($user->id, $course3->id, 'student');
774
775 $this->getDataGenerator()->enrol_user($user->id, $course2->id, 'student');
740c354f
JL
776 $this->setUser($user);
777
778 $results = core_course_external::search_courses('search', 'FIRST');
779 $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
780 $this->assertCount(1, $results['courses']);
781 $this->assertEquals(1, $results['total']);
782 $this->assertEquals($coursedata1['fullname'], $results['courses'][0]['fullname']);
783
935ee1c6
EM
784 // Check that we can see both without the limit to enrolled setting.
785 $results = core_course_external::search_courses('search', 'COURSE', 0, 0, array(), 0);
786 $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
787 $this->assertCount(2, $results['courses']);
788 $this->assertEquals(2, $results['total']);
789
790 // Check that we only see our enrolled course when limiting.
791 $results = core_course_external::search_courses('search', 'COURSE', 0, 0, array(), 1);
792 $results = external_api::clean_returnvalue(core_course_external::search_courses_returns(), $results);
793 $this->assertCount(1, $results['courses']);
794 $this->assertEquals(1, $results['total']);
795 $this->assertEquals($coursedata2['fullname'], $results['courses'][0]['fullname']);
796
740c354f 797 // Search by block (use news_items default block). Should fail (only admins allowed).
52f3e060 798 $this->expectException('required_capability_exception');
740c354f
JL
799 $results = core_course_external::search_courses('blocklist', $blockid);
800
801 }
802
2a7a0216 803 /**
8a5346a7
JL
804 * Create a course with contents
805 * @return array A list with the course object and course modules objects
2a7a0216 806 */
8a5346a7 807 private function prepare_get_course_contents_test() {
6a1131e2 808 global $DB;
935429e2 809 $course = self::getDataGenerator()->create_course(['numsections' => 3]);
487bc1b6
JM
810 $forumdescription = 'This is the forum description';
811 $forum = $this->getDataGenerator()->create_module('forum',
8a5346a7 812 array('course' => $course->id, 'intro' => $forumdescription),
487bc1b6 813 array('showdescription' => true));
2a7a0216 814 $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
8a5346a7 815 $data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
2a7a0216 816 $datacm = get_coursemodule_from_instance('page', $data->id);
8a5346a7 817 $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
2a7a0216 818 $pagecm = get_coursemodule_from_instance('page', $page->id);
487bc1b6
JM
819 $labeldescription = 'This is a very long label to test if more than 50 characters are returned.
820 So bla bla bla bla <b>bold bold bold</b> bla bla bla bla.';
821 $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id,
822 'intro' => $labeldescription));
823 $labelcm = get_coursemodule_from_instance('label', $label->id);
428f7864 824 $tomorrow = time() + DAYSECS;
935429e2
JL
825 // Module with availability restrictions not met.
826 $url = $this->getDataGenerator()->create_module('url',
827 array('course' => $course->id, 'name' => 'URL: % & $ ../', 'section' => 2),
428f7864 828 array('availability' => '{"op":"&","c":[{"type":"date","d":">=","t":' . $tomorrow . '}],"showc":[true]}'));
8a5346a7 829 $urlcm = get_coursemodule_from_instance('url', $url->id);
935429e2
JL
830 // Module for the last section.
831 $this->getDataGenerator()->create_module('url',
832 array('course' => $course->id, 'name' => 'URL for last section', 'section' => 3));
833 // Module for section 1 with availability restrictions met.
834 $yesterday = time() - DAYSECS;
835 $this->getDataGenerator()->create_module('url',
836 array('course' => $course->id, 'name' => 'URL restrictions met', 'section' => 1),
837 array('availability' => '{"op":"&","c":[{"type":"date","d":">=","t":'. $yesterday .'}],"showc":[true]}'));
2a7a0216
JM
838
839 // Set the required capabilities by the external function.
840 $context = context_course::instance($course->id);
841 $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
842 $this->assignUserCapability('moodle/course:update', $context->id, $roleid);
12306a9f 843 $this->assignUserCapability('mod/data:view', $context->id, $roleid);
2a7a0216 844
6a1131e2
JL
845 $conditions = array('course' => $course->id, 'section' => 2);
846 $DB->set_field('course_sections', 'summary', 'Text with iframe <iframe src="https://moodle.org"></iframe>', $conditions);
935429e2
JL
847
848 // Add date availability condition not met for last section.
428f7864 849 $availability = '{"op":"&","c":[{"type":"date","d":">=","t":' . $tomorrow . '}],"showc":[true]}';
935429e2
JL
850 $DB->set_field('course_sections', 'availability', $availability,
851 array('course' => $course->id, 'section' => 3));
6a1131e2
JL
852 rebuild_course_cache($course->id, true);
853
8a5346a7
JL
854 return array($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm);
855 }
856
857 /**
858 * Test get_course_contents
859 */
860 public function test_get_course_contents() {
861 $this->resetAfterTest(true);
2a7a0216 862
8a5346a7
JL
863 list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
864
865 $sections = core_course_external::get_course_contents($course->id, array());
fb695f6e 866 // We need to execute the return values cleaning process to simulate the web service server.
487bc1b6
JM
867 $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
868
487bc1b6
JM
869 $modinfo = get_fast_modinfo($course);
870 $testexecuted = 0;
935429e2 871 foreach ($sections[0]['modules'] as $module) {
487bc1b6
JM
872 if ($module['id'] == $forumcm->id and $module['modname'] == 'forum') {
873 $cm = $modinfo->cms[$forumcm->id];
73ee2fda 874 $formattedtext = format_text($cm->content, FORMAT_HTML,
487bc1b6
JM
875 array('noclean' => true, 'para' => false, 'filter' => false));
876 $this->assertEquals($formattedtext, $module['description']);
ca4154ce 877 $this->assertEquals($forumcm->instance, $module['instance']);
487bc1b6
JM
878 $testexecuted = $testexecuted + 1;
879 } else if ($module['id'] == $labelcm->id and $module['modname'] == 'label') {
880 $cm = $modinfo->cms[$labelcm->id];
73ee2fda 881 $formattedtext = format_text($cm->content, FORMAT_HTML,
487bc1b6
JM
882 array('noclean' => true, 'para' => false, 'filter' => false));
883 $this->assertEquals($formattedtext, $module['description']);
ca4154ce 884 $this->assertEquals($labelcm->instance, $module['instance']);
487bc1b6
JM
885 $testexecuted = $testexecuted + 1;
886 }
887 }
888 $this->assertEquals(2, $testexecuted);
935429e2 889 $this->assertEquals(0, $sections[0]['section']);
fb695f6e 890
8a5346a7 891 // Check that the only return section has the 5 created modules.
935429e2
JL
892 $this->assertCount(4, $sections[0]['modules']);
893 $this->assertCount(1, $sections[1]['modules']);
894 $this->assertCount(1, $sections[2]['modules']);
895 $this->assertCount(0, $sections[3]['modules']); // No modules for the section with availability restrictions.
896 $this->assertNotEmpty($sections[3]['availabilityinfo']);
897 $this->assertEquals(1, $sections[1]['section']);
898 $this->assertEquals(2, $sections[2]['section']);
899 $this->assertEquals(3, $sections[3]['section']);
900 $this->assertContains('<iframe', $sections[2]['summary']);
901 $this->assertContains('</iframe>', $sections[2]['summary']);
902 // The module with the availability restriction met is returning contents.
903 $this->assertNotEmpty($sections[1]['modules'][0]['contents']);
904 // The module with the availability restriction not met is not returning contents.
905 $this->assertArrayNotHasKey('contents', $sections[2]['modules'][0]);
906 $this->assertNotEmpty($sections[2]['modules'][0]['availabilityinfo']);
8a5346a7
JL
907 try {
908 $sections = core_course_external::get_course_contents($course->id,
909 array(array("name" => "invalid", "value" => 1)));
910 $this->fail('Exception expected due to invalid option.');
911 } catch (moodle_exception $e) {
912 $this->assertEquals('errorinvalidparam', $e->errorcode);
913 }
914 }
915
916
917 /**
918 * Test get_course_contents excluding modules
919 */
920 public function test_get_course_contents_excluding_modules() {
921 $this->resetAfterTest(true);
922
923 list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
924
925 // Test exclude modules.
926 $sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludemodules", "value" => 1)));
927
928 // We need to execute the return values cleaning process to simulate the web service server.
929 $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
930
935429e2
JL
931 $this->assertEmpty($sections[0]['modules']);
932 $this->assertEmpty($sections[1]['modules']);
8a5346a7
JL
933 }
934
935 /**
936 * Test get_course_contents excluding contents
937 */
938 public function test_get_course_contents_excluding_contents() {
939 $this->resetAfterTest(true);
940
941 list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
942
943 // Test exclude modules.
944 $sections = core_course_external::get_course_contents($course->id, array(array("name" => "excludecontents", "value" => 1)));
945
946 // We need to execute the return values cleaning process to simulate the web service server.
947 $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
948
949 foreach ($sections as $section) {
950 foreach ($section['modules'] as $module) {
951 // Only resources return contents.
952 if (isset($module['contents'])) {
953 $this->assertEmpty($module['contents']);
954 }
955 }
956 }
957 }
958
959 /**
960 * Test get_course_contents filtering by section number
961 */
962 public function test_get_course_contents_section_number() {
963 $this->resetAfterTest(true);
964
965 list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
966
967 // Test exclude modules.
968 $sections = core_course_external::get_course_contents($course->id, array(array("name" => "sectionnumber", "value" => 0)));
969
970 // We need to execute the return values cleaning process to simulate the web service server.
971 $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
972
973 $this->assertCount(1, $sections);
974 $this->assertCount(4, $sections[0]['modules']);
975 }
976
977 /**
978 * Test get_course_contents filtering by cmid
979 */
980 public function test_get_course_contents_cmid() {
981 $this->resetAfterTest(true);
982
983 list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
984
985 // Test exclude modules.
986 $sections = core_course_external::get_course_contents($course->id, array(array("name" => "cmid", "value" => $forumcm->id)));
987
988 // We need to execute the return values cleaning process to simulate the web service server.
989 $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
990
935429e2 991 $this->assertCount(4, $sections);
8a5346a7
JL
992 $this->assertCount(1, $sections[0]['modules']);
993 $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
994 }
995
996
997 /**
998 * Test get_course_contents filtering by cmid and section
999 */
1000 public function test_get_course_contents_section_cmid() {
1001 $this->resetAfterTest(true);
1002
1003 list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
1004
1005 // Test exclude modules.
1006 $sections = core_course_external::get_course_contents($course->id, array(
1007 array("name" => "cmid", "value" => $forumcm->id),
1008 array("name" => "sectionnumber", "value" => 0)
1009 ));
1010
1011 // We need to execute the return values cleaning process to simulate the web service server.
1012 $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
1013
1014 $this->assertCount(1, $sections);
1015 $this->assertCount(1, $sections[0]['modules']);
1016 $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
1017 }
1018
1019 /**
1020 * Test get_course_contents filtering by modname
1021 */
1022 public function test_get_course_contents_modname() {
1023 $this->resetAfterTest(true);
1024
1025 list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
1026
1027 // Test exclude modules.
1028 $sections = core_course_external::get_course_contents($course->id, array(array("name" => "modname", "value" => "forum")));
1029
1030 // We need to execute the return values cleaning process to simulate the web service server.
1031 $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
1032
935429e2 1033 $this->assertCount(4, $sections);
8a5346a7
JL
1034 $this->assertCount(1, $sections[0]['modules']);
1035 $this->assertEquals($forumcm->id, $sections[0]['modules'][0]["id"]);
1036 }
1037
1038 /**
1039 * Test get_course_contents filtering by modname
1040 */
1041 public function test_get_course_contents_modid() {
1042 $this->resetAfterTest(true);
1043
1044 list($course, $forumcm, $datacm, $pagecm, $labelcm, $urlcm) = $this->prepare_get_course_contents_test();
1045
1046 // Test exclude modules.
1047 $sections = core_course_external::get_course_contents($course->id, array(
1048 array("name" => "modname", "value" => "page"),
1049 array("name" => "modid", "value" => $pagecm->instance),
1050 ));
1051
1052 // We need to execute the return values cleaning process to simulate the web service server.
1053 $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
1054
935429e2 1055 $this->assertCount(4, $sections);
8a5346a7
JL
1056 $this->assertCount(1, $sections[0]['modules']);
1057 $this->assertEquals("page", $sections[0]['modules'][0]["modname"]);
1058 $this->assertEquals($pagecm->instance, $sections[0]['modules'][0]["instance"]);
2a7a0216
JM
1059 }
1060
1061 /**
1062 * Test duplicate_course
1063 */
1064 public function test_duplicate_course() {
1065 $this->resetAfterTest(true);
1066
1067 // Create one course with three modules.
1068 $course = self::getDataGenerator()->create_course();
1069 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
1070 $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
1071 $forumcontext = context_module::instance($forum->cmid);
1072 $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
1073 $datacontext = context_module::instance($data->cmid);
1074 $datacm = get_coursemodule_from_instance('page', $data->id);
1075 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
1076 $pagecontext = context_module::instance($page->cmid);
1077 $pagecm = get_coursemodule_from_instance('page', $page->id);
1078
1079 // Set the required capabilities by the external function.
1080 $coursecontext = context_course::instance($course->id);
1081 $categorycontext = context_coursecat::instance($course->category);
1082 $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id);
1083 $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid);
1084 $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid);
1085 $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid);
1086 $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid);
1087 // Optional capabilities to copy user data.
1088 $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid);
1089 $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid);
1090
1091 $newcourse['fullname'] = 'Course duplicate';
1092 $newcourse['shortname'] = 'courseduplicate';
1093 $newcourse['categoryid'] = $course->category;
1094 $newcourse['visible'] = true;
1095 $newcourse['options'][] = array('name' => 'users', 'value' => true);
1096
1097 $duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'],
1098 $newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']);
1099
fb695f6e
JM
1100 // We need to execute the return values cleaning process to simulate the web service server.
1101 $duplicate = external_api::clean_returnvalue(core_course_external::duplicate_course_returns(), $duplicate);
1102
2a7a0216
JM
1103 // Check that the course has been duplicated.
1104 $this->assertEquals($newcourse['shortname'], $duplicate['shortname']);
1105 }
791723c3
RT
1106
1107 /**
1108 * Test update_courses
1109 */
1110 public function test_update_courses() {
a182f88f
EL
1111 global $DB, $CFG, $USER, $COURSE;
1112
1113 // Get current $COURSE to be able to restore it later (defaults to $SITE). We need this
1114 // trick because we are both updating and getting (for testing) course information
1115 // in the same request and core_course_external::update_courses()
1116 // is overwriting $COURSE all over the time with OLD values, so later
1117 // use of get_course() fetches those OLD values instead of the updated ones.
1118 // See MDL-39723 for more info.
1119 $origcourse = clone($COURSE);
791723c3
RT
1120
1121 $this->resetAfterTest(true);
1122
1123 // Set the required capabilities by the external function.
1124 $contextid = context_system::instance()->id;
1125 $roleid = $this->assignUserCapability('moodle/course:update', $contextid);
1126 $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1127 $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1128 $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1129 $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1130 $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1131 $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
1132 $this->assignUserCapability('moodle/course:viewhiddencourses', $contextid, $roleid);
4a9624af 1133 $this->assignUserCapability('moodle/course:setforcedlanguage', $contextid, $roleid);
791723c3
RT
1134
1135 // Create category and course.
1136 $category1 = self::getDataGenerator()->create_category();
1137 $category2 = self::getDataGenerator()->create_category();
1138 $originalcourse1 = self::getDataGenerator()->create_course();
1139 self::getDataGenerator()->enrol_user($USER->id, $originalcourse1->id, $roleid);
1140 $originalcourse2 = self::getDataGenerator()->create_course();
1141 self::getDataGenerator()->enrol_user($USER->id, $originalcourse2->id, $roleid);
1142
1143 // Course values to be updated.
1144 $course1['id'] = $originalcourse1->id;
1145 $course1['fullname'] = 'Updated test course 1';
1146 $course1['shortname'] = 'Udestedtestcourse1';
1147 $course1['categoryid'] = $category1->id;
1148 $course2['id'] = $originalcourse2->id;
1149 $course2['fullname'] = 'Updated test course 2';
1150 $course2['shortname'] = 'Updestedtestcourse2';
1151 $course2['categoryid'] = $category2->id;
1152 $course2['idnumber'] = 'Updatedidnumber2';
1153 $course2['summary'] = 'Updaated description for course 2';
1154 $course2['summaryformat'] = FORMAT_HTML;
1155 $course2['format'] = 'topics';
1156 $course2['showgrades'] = 1;
1157 $course2['newsitems'] = 3;
1158 $course2['startdate'] = 1420092000; // 01/01/2015.
fbcdb0d7 1159 $course2['enddate'] = 1422669600; // 01/31/2015.
791723c3
RT
1160 $course2['maxbytes'] = 100000;
1161 $course2['showreports'] = 1;
1162 $course2['visible'] = 0;
1163 $course2['hiddensections'] = 0;
1164 $course2['groupmode'] = 0;
1165 $course2['groupmodeforce'] = 0;
1166 $course2['defaultgroupingid'] = 0;
1167 $course2['enablecompletion'] = 1;
1168 $course2['lang'] = 'en';
644fcbb6 1169 $course2['forcetheme'] = 'bootstrapbase';
791723c3
RT
1170 $courses = array($course1, $course2);
1171
1172 $updatedcoursewarnings = core_course_external::update_courses($courses);
bdf9f4d4
JL
1173 $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1174 $updatedcoursewarnings);
a182f88f 1175 $COURSE = $origcourse; // Restore $COURSE. Instead of using the OLD one set by the previous line.
791723c3
RT
1176
1177 // Check that right number of courses were created.
1178 $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1179
1180 // Check that the courses were correctly created.
1181 foreach ($courses as $course) {
1182 $courseinfo = course_get_format($course['id'])->get_course();
1183 if ($course['id'] == $course2['id']) {
1184 $this->assertEquals($course2['fullname'], $courseinfo->fullname);
1185 $this->assertEquals($course2['shortname'], $courseinfo->shortname);
1186 $this->assertEquals($course2['categoryid'], $courseinfo->category);
1187 $this->assertEquals($course2['idnumber'], $courseinfo->idnumber);
1188 $this->assertEquals($course2['summary'], $courseinfo->summary);
1189 $this->assertEquals($course2['summaryformat'], $courseinfo->summaryformat);
1190 $this->assertEquals($course2['format'], $courseinfo->format);
1191 $this->assertEquals($course2['showgrades'], $courseinfo->showgrades);
1192 $this->assertEquals($course2['newsitems'], $courseinfo->newsitems);
1193 $this->assertEquals($course2['startdate'], $courseinfo->startdate);
fbcdb0d7 1194 $this->assertEquals($course2['enddate'], $courseinfo->enddate);
791723c3
RT
1195 $this->assertEquals($course2['maxbytes'], $courseinfo->maxbytes);
1196 $this->assertEquals($course2['showreports'], $courseinfo->showreports);
1197 $this->assertEquals($course2['visible'], $courseinfo->visible);
1198 $this->assertEquals($course2['hiddensections'], $courseinfo->hiddensections);
1199 $this->assertEquals($course2['groupmode'], $courseinfo->groupmode);
1200 $this->assertEquals($course2['groupmodeforce'], $courseinfo->groupmodeforce);
1201 $this->assertEquals($course2['defaultgroupingid'], $courseinfo->defaultgroupingid);
1202 $this->assertEquals($course2['lang'], $courseinfo->lang);
1203
1204 if (!empty($CFG->allowcoursethemes)) {
1205 $this->assertEquals($course2['forcetheme'], $courseinfo->theme);
1206 }
1207
8be9cffb 1208 $this->assertEquals($course2['enablecompletion'], $courseinfo->enablecompletion);
791723c3
RT
1209 } else if ($course['id'] == $course1['id']) {
1210 $this->assertEquals($course1['fullname'], $courseinfo->fullname);
1211 $this->assertEquals($course1['shortname'], $courseinfo->shortname);
1212 $this->assertEquals($course1['categoryid'], $courseinfo->category);
1213 $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
1214 $this->assertEquals('topics', $courseinfo->format);
89b909f6 1215 $this->assertEquals(5, course_get_format($course['id'])->get_last_section_number());
791723c3
RT
1216 $this->assertEquals(0, $courseinfo->newsitems);
1217 $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
1218 } else {
75c597da 1219 throw new moodle_exception('Unexpected shortname');
791723c3
RT
1220 }
1221 }
1222
1223 $courses = array($course1);
1224 // Try update course without update capability.
1225 $user = self::getDataGenerator()->create_user();
1226 $this->setUser($user);
1227 $this->unassignUserCapability('moodle/course:update', $contextid, $roleid);
1228 self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1229 $updatedcoursewarnings = core_course_external::update_courses($courses);
bdf9f4d4
JL
1230 $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1231 $updatedcoursewarnings);
791723c3
RT
1232 $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1233
1234 // Try update course category without capability.
1235 $this->assignUserCapability('moodle/course:update', $contextid, $roleid);
1236 $this->unassignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1237 $user = self::getDataGenerator()->create_user();
1238 $this->setUser($user);
1239 self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1240 $course1['categoryid'] = $category2->id;
1241 $courses = array($course1);
1242 $updatedcoursewarnings = core_course_external::update_courses($courses);
bdf9f4d4
JL
1243 $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1244 $updatedcoursewarnings);
791723c3
RT
1245 $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1246
1247 // Try update course fullname without capability.
1248 $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
1249 $this->unassignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1250 $user = self::getDataGenerator()->create_user();
1251 $this->setUser($user);
1252 self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1253 $updatedcoursewarnings = core_course_external::update_courses($courses);
bdf9f4d4
JL
1254 $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1255 $updatedcoursewarnings);
791723c3
RT
1256 $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1257 $course1['fullname'] = 'Testing fullname without permission';
1258 $courses = array($course1);
1259 $updatedcoursewarnings = core_course_external::update_courses($courses);
bdf9f4d4
JL
1260 $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1261 $updatedcoursewarnings);
791723c3
RT
1262 $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1263
1264 // Try update course shortname without capability.
1265 $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
1266 $this->unassignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1267 $user = self::getDataGenerator()->create_user();
1268 $this->setUser($user);
1269 self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1270 $updatedcoursewarnings = core_course_external::update_courses($courses);
bdf9f4d4
JL
1271 $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1272 $updatedcoursewarnings);
791723c3
RT
1273 $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1274 $course1['shortname'] = 'Testing shortname without permission';
1275 $courses = array($course1);
1276 $updatedcoursewarnings = core_course_external::update_courses($courses);
bdf9f4d4
JL
1277 $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1278 $updatedcoursewarnings);
791723c3
RT
1279 $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1280
1281 // Try update course idnumber without capability.
1282 $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
1283 $this->unassignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1284 $user = self::getDataGenerator()->create_user();
1285 $this->setUser($user);
1286 self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1287 $updatedcoursewarnings = core_course_external::update_courses($courses);
bdf9f4d4
JL
1288 $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1289 $updatedcoursewarnings);
791723c3
RT
1290 $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1291 $course1['idnumber'] = 'NEWIDNUMBER';
1292 $courses = array($course1);
1293 $updatedcoursewarnings = core_course_external::update_courses($courses);
bdf9f4d4
JL
1294 $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1295 $updatedcoursewarnings);
791723c3
RT
1296 $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1297
1298 // Try update course summary without capability.
1299 $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
1300 $this->unassignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1301 $user = self::getDataGenerator()->create_user();
1302 $this->setUser($user);
1303 self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1304 $updatedcoursewarnings = core_course_external::update_courses($courses);
bdf9f4d4
JL
1305 $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1306 $updatedcoursewarnings);
791723c3
RT
1307 $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1308 $course1['summary'] = 'New summary';
1309 $courses = array($course1);
1310 $updatedcoursewarnings = core_course_external::update_courses($courses);
bdf9f4d4
JL
1311 $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1312 $updatedcoursewarnings);
791723c3
RT
1313 $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1314
1315 // Try update course with invalid summary format.
1316 $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
1317 $user = self::getDataGenerator()->create_user();
1318 $this->setUser($user);
1319 self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1320 $updatedcoursewarnings = core_course_external::update_courses($courses);
bdf9f4d4
JL
1321 $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1322 $updatedcoursewarnings);
791723c3
RT
1323 $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1324 $course1['summaryformat'] = 10;
1325 $courses = array($course1);
1326 $updatedcoursewarnings = core_course_external::update_courses($courses);
bdf9f4d4
JL
1327 $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1328 $updatedcoursewarnings);
791723c3
RT
1329 $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1330
1331 // Try update course visibility without capability.
1332 $this->unassignUserCapability('moodle/course:visibility', $contextid, $roleid);
1333 $user = self::getDataGenerator()->create_user();
1334 $this->setUser($user);
1335 self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
1336 $course1['summaryformat'] = FORMAT_MOODLE;
1337 $courses = array($course1);
1338 $updatedcoursewarnings = core_course_external::update_courses($courses);
bdf9f4d4
JL
1339 $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1340 $updatedcoursewarnings);
791723c3
RT
1341 $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
1342 $course1['visible'] = 0;
1343 $courses = array($course1);
1344 $updatedcoursewarnings = core_course_external::update_courses($courses);
bdf9f4d4
JL
1345 $updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
1346 $updatedcoursewarnings);
791723c3
RT
1347 $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
1348 }
05fc7ccc 1349
79949c1b
MN
1350 /**
1351 * Test delete course_module.
1352 */
1353 public function test_delete_modules() {
1354 global $DB;
1355
1356 // Ensure we reset the data after this test.
1357 $this->resetAfterTest(true);
1358
1359 // Create a user.
1360 $user = self::getDataGenerator()->create_user();
1361
1362 // Set the tests to run as the user.
1363 self::setUser($user);
1364
1365 // Create a course to add the modules.
1366 $course = self::getDataGenerator()->create_course();
1367
1368 // Create two test modules.
1369 $record = new stdClass();
1370 $record->course = $course->id;
1371 $module1 = self::getDataGenerator()->create_module('forum', $record);
40cb4879 1372 $module2 = self::getDataGenerator()->create_module('assign', $record);
79949c1b
MN
1373
1374 // Check the forum was correctly created.
1375 $this->assertEquals(1, $DB->count_records('forum', array('id' => $module1->id)));
1376
1377 // Check the assignment was correctly created.
40cb4879 1378 $this->assertEquals(1, $DB->count_records('assign', array('id' => $module2->id)));
79949c1b
MN
1379
1380 // Check data exists in the course modules table.
1381 $this->assertEquals(2, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
1382 array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
1383
1384 // Enrol the user in the course.
1385 $enrol = enrol_get_plugin('manual');
1386 $enrolinstances = enrol_get_instances($course->id, true);
1387 foreach ($enrolinstances as $courseenrolinstance) {
1388 if ($courseenrolinstance->enrol == "manual") {
1389 $instance = $courseenrolinstance;
1390 break;
1391 }
1392 }
1393 $enrol->enrol_user($instance, $user->id);
1394
1395 // Assign capabilities to delete module 1.
1396 $modcontext = context_module::instance($module1->cmid);
1397 $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id);
1398
1399 // Assign capabilities to delete module 2.
1400 $modcontext = context_module::instance($module2->cmid);
1401 $newrole = create_role('Role 2', 'role2', 'Role 2 description');
1402 $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id, $newrole);
1403
1404 // Deleting these module instances.
1405 core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1406
1407 // Check the forum was deleted.
1408 $this->assertEquals(0, $DB->count_records('forum', array('id' => $module1->id)));
1409
1410 // Check the assignment was deleted.
40cb4879 1411 $this->assertEquals(0, $DB->count_records('assign', array('id' => $module2->id)));
79949c1b
MN
1412
1413 // Check we retrieve no data in the course modules table.
1414 $this->assertEquals(0, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
1415 array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
1416
1417 // Call with non-existent course module id and ensure exception thrown.
1418 try {
1419 core_course_external::delete_modules(array('1337'));
1420 $this->fail('Exception expected due to missing course module.');
1421 } catch (dml_missing_record_exception $e) {
affdc3b7 1422 $this->assertEquals('invalidcoursemodule', $e->errorcode);
79949c1b
MN
1423 }
1424
1425 // Create two modules.
1426 $module1 = self::getDataGenerator()->create_module('forum', $record);
40cb4879 1427 $module2 = self::getDataGenerator()->create_module('assign', $record);
79949c1b
MN
1428
1429 // Since these modules were recreated the user will not have capabilities
1430 // to delete them, ensure exception is thrown if they try.
1431 try {
1432 core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1433 $this->fail('Exception expected due to missing capability.');
1434 } catch (moodle_exception $e) {
1435 $this->assertEquals('nopermissions', $e->errorcode);
1436 }
1437
1438 // Unenrol user from the course.
1439 $enrol->unenrol_user($instance, $user->id);
1440
1441 // Try and delete modules from the course the user was unenrolled in, make sure exception thrown.
1442 try {
1443 core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
1444 $this->fail('Exception expected due to being unenrolled from the course.');
1445 } catch (moodle_exception $e) {
1446 $this->assertEquals('requireloginerror', $e->errorcode);
1447 }
1448 }
fce10644
DP
1449
1450 /**
1451 * Test import_course into an empty course
1452 */
1453 public function test_import_course_empty() {
1454 global $USER;
1455
1456 $this->resetAfterTest(true);
1457
1458 $course1 = self::getDataGenerator()->create_course();
1459 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course1->id, 'name' => 'Forum test'));
1460 $page = $this->getDataGenerator()->create_module('page', array('course' => $course1->id, 'name' => 'Page test'));
1461
1462 $course2 = self::getDataGenerator()->create_course();
1463
1464 $course1cms = get_fast_modinfo($course1->id)->get_cms();
1465 $course2cms = get_fast_modinfo($course2->id)->get_cms();
1466
1467 // Verify the state of the courses before we do the import.
1468 $this->assertCount(2, $course1cms);
1469 $this->assertEmpty($course2cms);
1470
1471 // Setup the user to run the operation (ugly hack because validate_context() will
1472 // fail as the email is not set by $this->setAdminUser()).
1473 $this->setAdminUser();
0fe86bbd 1474 $USER->email = 'emailtopass@example.com';
fce10644
DP
1475
1476 // Import from course1 to course2.
1477 core_course_external::import_course($course1->id, $course2->id, 0);
1478
1479 // Verify that now we have two modules in both courses.
1480 $course1cms = get_fast_modinfo($course1->id)->get_cms();
1481 $course2cms = get_fast_modinfo($course2->id)->get_cms();
1482 $this->assertCount(2, $course1cms);
1483 $this->assertCount(2, $course2cms);
1484
1485 // Verify that the names transfered across correctly.
1486 foreach ($course2cms as $cm) {
1487 if ($cm->modname === 'page') {
1488 $this->assertEquals($cm->name, $page->name);
1489 } else if ($cm->modname === 'forum') {
1490 $this->assertEquals($cm->name, $forum->name);
1491 } else {
1492 $this->fail('Unknown CM found.');
1493 }
1494 }
fce10644
DP
1495 }
1496
1497 /**
1498 * Test import_course into an filled course
1499 */
1500 public function test_import_course_filled() {
1501 global $USER;
1502
1503 $this->resetAfterTest(true);
1504
1505 // Add forum and page to course1.
1506 $course1 = self::getDataGenerator()->create_course();
1507 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1508 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
1509
1510 // Add quiz to course 2.
1511 $course2 = self::getDataGenerator()->create_course();
1512 $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
1513
1514 $course1cms = get_fast_modinfo($course1->id)->get_cms();
1515 $course2cms = get_fast_modinfo($course2->id)->get_cms();
1516
1517 // Verify the state of the courses before we do the import.
1518 $this->assertCount(2, $course1cms);
1519 $this->assertCount(1, $course2cms);
1520
1521 // Setup the user to run the operation (ugly hack because validate_context() will
1522 // fail as the email is not set by $this->setAdminUser()).
1523 $this->setAdminUser();
0fe86bbd 1524 $USER->email = 'emailtopass@example.com';
fce10644
DP
1525
1526 // Import from course1 to course2 without deleting content.
1527 core_course_external::import_course($course1->id, $course2->id, 0);
1528
1529 $course2cms = get_fast_modinfo($course2->id)->get_cms();
1530
1531 // Verify that now we have three modules in course2.
1532 $this->assertCount(3, $course2cms);
1533
1534 // Verify that the names transfered across correctly.
1535 foreach ($course2cms as $cm) {
1536 if ($cm->modname === 'page') {
1537 $this->assertEquals($cm->name, $page->name);
1538 } else if ($cm->modname === 'forum') {
1539 $this->assertEquals($cm->name, $forum->name);
1540 } else if ($cm->modname === 'quiz') {
1541 $this->assertEquals($cm->name, $quiz->name);
1542 } else {
1543 $this->fail('Unknown CM found.');
1544 }
1545 }
fce10644
DP
1546 }
1547
1548 /**
1549 * Test import_course with only blocks set to backup
1550 */
1551 public function test_import_course_blocksonly() {
1552 global $USER, $DB;
1553
1554 $this->resetAfterTest(true);
1555
1556 // Add forum and page to course1.
1557 $course1 = self::getDataGenerator()->create_course();
1558 $course1ctx = context_course::instance($course1->id);
1559 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1560 $block = $this->getDataGenerator()->create_block('online_users', array('parentcontextid' => $course1ctx->id));
1561
1562 $course2 = self::getDataGenerator()->create_course();
1563 $course2ctx = context_course::instance($course2->id);
1564 $initialblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
1565 $initialcmcount = count(get_fast_modinfo($course2->id)->get_cms());
1566
1567 // Setup the user to run the operation (ugly hack because validate_context() will
1568 // fail as the email is not set by $this->setAdminUser()).
1569 $this->setAdminUser();
0fe86bbd 1570 $USER->email = 'emailtopass@example.com';
fce10644
DP
1571
1572 // Import from course1 to course2 without deleting content, but excluding
1573 // activities.
1574 $options = array(
1575 array('name' => 'activities', 'value' => 0),
1576 array('name' => 'blocks', 'value' => 1),
1577 array('name' => 'filters', 'value' => 0),
1578 );
1579
1580 core_course_external::import_course($course1->id, $course2->id, 0, $options);
1581
1582 $newcmcount = count(get_fast_modinfo($course2->id)->get_cms());
1583 $newblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
1584 // Check that course modules haven't changed, but that blocks have.
1585 $this->assertEquals($initialcmcount, $newcmcount);
1586 $this->assertEquals(($initialblockcount + 1), $newblockcount);
fce10644
DP
1587 }
1588
1589 /**
1590 * Test import_course into an filled course, deleting content.
1591 */
1592 public function test_import_course_deletecontent() {
1593 global $USER;
1594 $this->resetAfterTest(true);
1595
1596 // Add forum and page to course1.
1597 $course1 = self::getDataGenerator()->create_course();
1598 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1599 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
1600
1601 // Add quiz to course 2.
1602 $course2 = self::getDataGenerator()->create_course();
1603 $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
1604
1605 $course1cms = get_fast_modinfo($course1->id)->get_cms();
1606 $course2cms = get_fast_modinfo($course2->id)->get_cms();
1607
1608 // Verify the state of the courses before we do the import.
1609 $this->assertCount(2, $course1cms);
1610 $this->assertCount(1, $course2cms);
1611
1612 // Setup the user to run the operation (ugly hack because validate_context() will
1613 // fail as the email is not set by $this->setAdminUser()).
1614 $this->setAdminUser();
0fe86bbd 1615 $USER->email = 'emailtopass@example.com';
fce10644
DP
1616
1617 // Import from course1 to course2, deleting content.
1618 core_course_external::import_course($course1->id, $course2->id, 1);
1619
1620 $course2cms = get_fast_modinfo($course2->id)->get_cms();
1621
1622 // Verify that now we have two modules in course2.
1623 $this->assertCount(2, $course2cms);
1624
1625 // Verify that the course only contains the imported modules.
1626 foreach ($course2cms as $cm) {
1627 if ($cm->modname === 'page') {
1628 $this->assertEquals($cm->name, $page->name);
1629 } else if ($cm->modname === 'forum') {
1630 $this->assertEquals($cm->name, $forum->name);
1631 } else {
1632 $this->fail('Unknown CM found: '.$cm->name);
1633 }
1634 }
fce10644
DP
1635 }
1636
1637 /**
1638 * Ensure import_course handles incorrect deletecontent option correctly.
1639 */
1640 public function test_import_course_invalid_deletecontent_option() {
1641 $this->resetAfterTest(true);
1642
1643 $course1 = self::getDataGenerator()->create_course();
1644 $course2 = self::getDataGenerator()->create_course();
1645
52f3e060
RT
1646 $this->expectException('moodle_exception');
1647 $this->expectExceptionMessage(get_string('invalidextparam', 'webservice', -1));
fce10644
DP
1648 // Import from course1 to course2, with invalid option
1649 core_course_external::import_course($course1->id, $course2->id, -1);;
1650 }
e81f67ca
JL
1651
1652 /**
1653 * Test view_course function
1654 */
1655 public function test_view_course() {
1656
1657 $this->resetAfterTest();
1658
1659 // Course without sections.
1660 $course = $this->getDataGenerator()->create_course(array('numsections' => 5), array('createsections' => true));
1661 $this->setAdminUser();
1662
1663 // Redirect events to the sink, so we can recover them later.
1664 $sink = $this->redirectEvents();
1665
bdf9f4d4
JL
1666 $result = core_course_external::view_course($course->id, 1);
1667 $result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result);
e81f67ca
JL
1668 $events = $sink->get_events();
1669 $event = reset($events);
1670
1671 // Check the event details are correct.
1672 $this->assertInstanceOf('\core\event\course_viewed', $event);
1673 $this->assertEquals(context_course::instance($course->id), $event->get_context());
1674 $this->assertEquals(1, $event->other['coursesectionnumber']);
1675
bdf9f4d4
JL
1676 $result = core_course_external::view_course($course->id);
1677 $result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result);
e81f67ca
JL
1678 $events = $sink->get_events();
1679 $event = array_pop($events);
1680 $sink->close();
1681
1682 // Check the event details are correct.
1683 $this->assertInstanceOf('\core\event\course_viewed', $event);
1684 $this->assertEquals(context_course::instance($course->id), $event->get_context());
1685 $this->assertEmpty($event->other);
1686
1687 }
c5158499
JL
1688
1689 /**
1690 * Test get_course_module
1691 */
1692 public function test_get_course_module() {
1693 global $DB;
1694
1695 $this->resetAfterTest(true);
1696
1697 $this->setAdminUser();
1698 $course = self::getDataGenerator()->create_course();
1699 $record = array(
1700 'course' => $course->id,
796876b0 1701 'name' => 'First Assignment'
c5158499
JL
1702 );
1703 $options = array(
1704 'idnumber' => 'ABC',
1705 'visible' => 0
1706 );
1707 // Hidden activity.
796876b0 1708 $assign = self::getDataGenerator()->create_module('assign', $record, $options);
c5158499 1709
28ff87be
PFO
1710 $outcomescale = 'Distinction, Very Good, Good, Pass, Fail';
1711
1712 // Insert a custom grade scale to be used by an outcome.
1713 $gradescale = new grade_scale();
1714 $gradescale->name = 'gettcoursemodulescale';
1715 $gradescale->courseid = $course->id;
1716 $gradescale->userid = 0;
1717 $gradescale->scale = $outcomescale;
1718 $gradescale->description = 'This scale is used to mark standard assignments.';
1719 $gradescale->insert();
1720
1721 // Insert an outcome.
1722 $data = new stdClass();
1723 $data->courseid = $course->id;
1724 $data->fullname = 'Team work';
1725 $data->shortname = 'Team work';
1726 $data->scaleid = $gradescale->id;
1727 $outcome = new grade_outcome($data, false);
1728 $outcome->insert();
1729
1730 $outcomegradeitem = new grade_item();
1731 $outcomegradeitem->itemname = $outcome->shortname;
1732 $outcomegradeitem->itemtype = 'mod';
1733 $outcomegradeitem->itemmodule = 'assign';
1734 $outcomegradeitem->iteminstance = $assign->id;
1735 $outcomegradeitem->outcomeid = $outcome->id;
1736 $outcomegradeitem->cmid = 0;
1737 $outcomegradeitem->courseid = $course->id;
1738 $outcomegradeitem->aggregationcoef = 0;
1739 $outcomegradeitem->itemnumber = 1; // The activity's original grade item will be 0.
1740 $outcomegradeitem->gradetype = GRADE_TYPE_SCALE;
1741 $outcomegradeitem->scaleid = $outcome->scaleid;
1742 $outcomegradeitem->insert();
1743
1744 $assignmentgradeitem = grade_item::fetch(
1745 array(
1746 'itemtype' => 'mod',
1747 'itemmodule' => 'assign',
1748 'iteminstance' => $assign->id,
1749 'itemnumber' => 0,
1750 'courseid' => $course->id
1751 )
1752 );
1753 $outcomegradeitem->set_parent($assignmentgradeitem->categoryid);
1754 $outcomegradeitem->move_after_sortorder($assignmentgradeitem->sortorder);
1755
c5158499 1756 // Test admin user can see the complete hidden activity.
796876b0 1757 $result = core_course_external::get_course_module($assign->cmid);
c5158499
JL
1758 $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result);
1759
1760 $this->assertCount(0, $result['warnings']);
1761 // Test we retrieve all the fields.
8341055e 1762 $this->assertCount(28, $result['cm']);
c5158499
JL
1763 $this->assertEquals($record['name'], $result['cm']['name']);
1764 $this->assertEquals($options['idnumber'], $result['cm']['idnumber']);
796876b0
JL
1765 $this->assertEquals(100, $result['cm']['grade']);
1766 $this->assertEquals(0.0, $result['cm']['gradepass']);
1767 $this->assertEquals('submissions', $result['cm']['advancedgrading'][0]['area']);
1768 $this->assertEmpty($result['cm']['advancedgrading'][0]['method']);
28ff87be 1769 $this->assertEquals($outcomescale, $result['cm']['outcomes'][0]['scale']);
c5158499
JL
1770
1771 $student = $this->getDataGenerator()->create_user();
1772 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1773
1774 self::getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
1775 $this->setUser($student);
1776
1777 // The user shouldn't be able to see the activity.
1778 try {
796876b0 1779 core_course_external::get_course_module($assign->cmid);
c5158499
JL
1780 $this->fail('Exception expected due to invalid permissions.');
1781 } catch (moodle_exception $e) {
1782 $this->assertEquals('requireloginerror', $e->errorcode);
1783 }
1784
1785 // Make module visible.
796876b0 1786 set_coursemodule_visible($assign->cmid, 1);
c5158499
JL
1787
1788 // Test student user.
796876b0 1789 $result = core_course_external::get_course_module($assign->cmid);
c5158499
JL
1790 $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result);
1791
1792 $this->assertCount(0, $result['warnings']);
1793 // Test we retrieve only the few files we can see.
1794 $this->assertCount(11, $result['cm']);
796876b0 1795 $this->assertEquals($assign->cmid, $result['cm']['id']);
c5158499 1796 $this->assertEquals($course->id, $result['cm']['course']);
796876b0
JL
1797 $this->assertEquals('assign', $result['cm']['modname']);
1798 $this->assertEquals($assign->id, $result['cm']['instance']);
c5158499
JL
1799
1800 }
13bb6819
JL
1801
1802 /**
1803 * Test get_course_module_by_instance
1804 */
1805 public function test_get_course_module_by_instance() {
1806 global $DB;
1807
1808 $this->resetAfterTest(true);
1809
1810 $this->setAdminUser();
1811 $course = self::getDataGenerator()->create_course();
1812 $record = array(
1813 'course' => $course->id,
7ddb5f25
JL
1814 'name' => 'First quiz',
1815 'grade' => 90.00
13bb6819
JL
1816 );
1817 $options = array(
1818 'idnumber' => 'ABC',
1819 'visible' => 0
1820 );
1821 // Hidden activity.
7ddb5f25 1822 $quiz = self::getDataGenerator()->create_module('quiz', $record, $options);
13bb6819
JL
1823
1824 // Test admin user can see the complete hidden activity.
7ddb5f25 1825 $result = core_course_external::get_course_module_by_instance('quiz', $quiz->id);
13bb6819
JL
1826 $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result);
1827
1828 $this->assertCount(0, $result['warnings']);
1829 // Test we retrieve all the fields.
7ddb5f25 1830 $this->assertCount(26, $result['cm']);
13bb6819 1831 $this->assertEquals($record['name'], $result['cm']['name']);
7ddb5f25 1832 $this->assertEquals($record['grade'], $result['cm']['grade']);
13bb6819
JL
1833 $this->assertEquals($options['idnumber'], $result['cm']['idnumber']);
1834
1835 $student = $this->getDataGenerator()->create_user();
1836 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1837
1838 self::getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
1839 $this->setUser($student);
1840
1841 // The user shouldn't be able to see the activity.
1842 try {
7ddb5f25 1843 core_course_external::get_course_module_by_instance('quiz', $quiz->id);
13bb6819
JL
1844 $this->fail('Exception expected due to invalid permissions.');
1845 } catch (moodle_exception $e) {
1846 $this->assertEquals('requireloginerror', $e->errorcode);
1847 }
1848
1849 // Make module visible.
7ddb5f25 1850 set_coursemodule_visible($quiz->cmid, 1);
13bb6819
JL
1851
1852 // Test student user.
7ddb5f25 1853 $result = core_course_external::get_course_module_by_instance('quiz', $quiz->id);
13bb6819
JL
1854 $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result);
1855
1856 $this->assertCount(0, $result['warnings']);
1857 // Test we retrieve only the few files we can see.
1858 $this->assertCount(11, $result['cm']);
7ddb5f25 1859 $this->assertEquals($quiz->cmid, $result['cm']['id']);
13bb6819 1860 $this->assertEquals($course->id, $result['cm']['course']);
7ddb5f25
JL
1861 $this->assertEquals('quiz', $result['cm']['modname']);
1862 $this->assertEquals($quiz->id, $result['cm']['instance']);
13bb6819
JL
1863
1864 // Try with an invalid module name.
1865 try {
7ddb5f25 1866 core_course_external::get_course_module_by_instance('abc', $quiz->id);
13bb6819
JL
1867 $this->fail('Exception expected due to invalid module name.');
1868 } catch (dml_read_exception $e) {
1869 $this->assertEquals('dmlreadexception', $e->errorcode);
1870 }
1871
1872 }
7c4e686f
JL
1873
1874 /**
1875 * Test get_activities_overview
1876 */
1877 public function test_get_activities_overview() {
1878 global $USER;
1879
1880 $this->resetAfterTest();
1881 $course1 = self::getDataGenerator()->create_course();
1882 $course2 = self::getDataGenerator()->create_course();
1883
1884 // Create a viewer user.
1885 $viewer = self::getDataGenerator()->create_user((object) array('trackforums' => 1));
1886 $this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
1887 $this->getDataGenerator()->enrol_user($viewer->id, $course2->id);
1888
1889 // Create two forums - one in each course.
1890 $record = new stdClass();
1891 $record->course = $course1->id;
1892 $forum1 = self::getDataGenerator()->create_module('forum', (object) array('course' => $course1->id));
1893 $forum2 = self::getDataGenerator()->create_module('forum', (object) array('course' => $course2->id));
1894
1895 $this->setAdminUser();
1896 // A standard post in the forum.
1897 $record = new stdClass();
1898 $record->course = $course1->id;
1899 $record->userid = $USER->id;
1900 $record->forum = $forum1->id;
1901 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1902
1903 $this->setUser($viewer->id);
1904 $courses = array($course1->id , $course2->id);
1905
1906 $result = core_course_external::get_activities_overview($courses);
e9dfeec9 1907 $this->assertDebuggingCalledCount(8);
7c4e686f
JL
1908 $result = external_api::clean_returnvalue(core_course_external::get_activities_overview_returns(), $result);
1909
1910 // There should be one entry for course1, and no others.
1911 $this->assertCount(1, $result['courses']);
1912 $this->assertEquals($course1->id, $result['courses'][0]['id']);
1913 // Check expected overview data for the module.
1914 $this->assertEquals('forum', $result['courses'][0]['overviews'][0]['module']);
1915 $this->assertContains('1 total unread', $result['courses'][0]['overviews'][0]['overviewtext']);
1916 }
c115ff6a
JL
1917
1918 /**
1919 * Test get_user_navigation_options
1920 */
1921 public function test_get_user_navigation_options() {
1922 global $USER;
1923
1924 $this->resetAfterTest();
1925 $course1 = self::getDataGenerator()->create_course();
1926 $course2 = self::getDataGenerator()->create_course();
1927
1928 // Create a viewer user.
1929 $viewer = self::getDataGenerator()->create_user();
1930 $this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
1931 $this->getDataGenerator()->enrol_user($viewer->id, $course2->id);
1932
1933 $this->setUser($viewer->id);
1934 $courses = array($course1->id , $course2->id, SITEID);
1935
1936 $result = core_course_external::get_user_navigation_options($courses);
1937 $result = external_api::clean_returnvalue(core_course_external::get_user_navigation_options_returns(), $result);
1938
1939 $this->assertCount(0, $result['warnings']);
1940 $this->assertCount(3, $result['courses']);
1941
1942 foreach ($result['courses'] as $course) {
1943 $navoptions = new stdClass;
1944 foreach ($course['options'] as $option) {
1945 $navoptions->{$option['name']} = $option['available'];
1946 }
203f51d6 1947 $this->assertCount(9, $course['options']);
c115ff6a 1948 if ($course['id'] == SITEID) {
c115ff6a
JL
1949 $this->assertTrue($navoptions->blogs);
1950 $this->assertFalse($navoptions->notes);
1951 $this->assertFalse($navoptions->participants);
1952 $this->assertTrue($navoptions->badges);
1953 $this->assertTrue($navoptions->tags);
203f51d6 1954 $this->assertFalse($navoptions->grades);
c115ff6a
JL
1955 $this->assertFalse($navoptions->search);
1956 $this->assertTrue($navoptions->calendar);
99061152 1957 $this->assertTrue($navoptions->competencies);
c115ff6a 1958 } else {
c115ff6a
JL
1959 $this->assertTrue($navoptions->blogs);
1960 $this->assertFalse($navoptions->notes);
1961 $this->assertTrue($navoptions->participants);
1962 $this->assertTrue($navoptions->badges);
203f51d6 1963 $this->assertFalse($navoptions->tags);
99061152 1964 $this->assertTrue($navoptions->grades);
203f51d6
DP
1965 $this->assertFalse($navoptions->search);
1966 $this->assertFalse($navoptions->calendar);
99061152 1967 $this->assertTrue($navoptions->competencies);
c115ff6a
JL
1968 }
1969 }
1970 }
b9050b10
JL
1971
1972 /**
1973 * Test get_user_administration_options
1974 */
1975 public function test_get_user_administration_options() {
1976 global $USER;
1977
1978 $this->resetAfterTest();
1979 $course1 = self::getDataGenerator()->create_course();
1980 $course2 = self::getDataGenerator()->create_course();
1981
1982 // Create a viewer user.
1983 $viewer = self::getDataGenerator()->create_user();
1984 $this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
1985 $this->getDataGenerator()->enrol_user($viewer->id, $course2->id);
1986
1987 $this->setUser($viewer->id);
1988 $courses = array($course1->id , $course2->id, SITEID);
1989
1990 $result = core_course_external::get_user_administration_options($courses);
1991 $result = external_api::clean_returnvalue(core_course_external::get_user_administration_options_returns(), $result);
1992
1993 $this->assertCount(0, $result['warnings']);
1994 $this->assertCount(3, $result['courses']);
1995
1996 foreach ($result['courses'] as $course) {
1997 $adminoptions = new stdClass;
1998 foreach ($course['options'] as $option) {
1999 $adminoptions->{$option['name']} = $option['available'];
2000 }
2001 if ($course['id'] == SITEID) {
0cbc248d 2002 $this->assertCount(16, $course['options']);
b9050b10
JL
2003 $this->assertFalse($adminoptions->update);
2004 $this->assertFalse($adminoptions->filters);
2005 $this->assertFalse($adminoptions->reports);
2006 $this->assertFalse($adminoptions->backup);
2007 $this->assertFalse($adminoptions->restore);
2008 $this->assertFalse($adminoptions->files);
c874d9aa
JL
2009 $this->assertFalse(!isset($adminoptions->tags));
2010 $this->assertFalse($adminoptions->gradebook);
2011 $this->assertFalse($adminoptions->outcomes);
2012 $this->assertFalse($adminoptions->badges);
2013 $this->assertFalse($adminoptions->import);
2014 $this->assertFalse($adminoptions->publish);
2015 $this->assertFalse($adminoptions->reset);
2016 $this->assertFalse($adminoptions->roles);
0cbc248d 2017 $this->assertFalse($adminoptions->editcompletion);
b9050b10 2018 } else {
0cbc248d 2019 $this->assertCount(15, $course['options']);
b9050b10
JL
2020 $this->assertFalse($adminoptions->update);
2021 $this->assertFalse($adminoptions->filters);
2022 $this->assertFalse($adminoptions->reports);
2023 $this->assertFalse($adminoptions->backup);
2024 $this->assertFalse($adminoptions->restore);
2025 $this->assertFalse($adminoptions->files);
2026 $this->assertFalse($adminoptions->tags);
2027 $this->assertFalse($adminoptions->gradebook);
2028 $this->assertFalse($adminoptions->outcomes);
2029 $this->assertTrue($adminoptions->badges);
2030 $this->assertFalse($adminoptions->import);
2031 $this->assertFalse($adminoptions->publish);
2032 $this->assertFalse($adminoptions->reset);
2033 $this->assertFalse($adminoptions->roles);
0cbc248d 2034 $this->assertFalse($adminoptions->editcompletion);
b9050b10
JL
2035 }
2036 }
2037 }
80adabef
JL
2038
2039 /**
2040 * Test get_courses_by_fields
2041 */
2042 public function test_get_courses_by_field() {
2043 global $DB;
2044 $this->resetAfterTest(true);
2045
2046 $category1 = self::getDataGenerator()->create_category();
2047 $category2 = self::getDataGenerator()->create_category(array('parent' => $category1->id));
2048 $course1 = self::getDataGenerator()->create_course(array('category' => $category1->id, 'shortname' => 'c1'));
2049 $course2 = self::getDataGenerator()->create_course(array('visible' => 0, 'category' => $category2->id, 'idnumber' => 'i2'));
2050
2051 $student1 = self::getDataGenerator()->create_user();
2052 $user1 = self::getDataGenerator()->create_user();
2053 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
2054 self::getDataGenerator()->enrol_user($student1->id, $course1->id, $studentrole->id);
2055 self::getDataGenerator()->enrol_user($student1->id, $course2->id, $studentrole->id);
2056
2057 self::setAdminUser();
2058 // As admins, we should be able to retrieve everything.
2059 $result = core_course_external::get_courses_by_field();
2060 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2061 $this->assertCount(3, $result['courses']);
2062 // Expect to receive all the fields.
ef83fc2a
JL
2063 $this->assertCount(37, $result['courses'][0]);
2064 $this->assertCount(37, $result['courses'][1]);
2065 $this->assertCount(37, $result['courses'][2]);
80adabef
JL
2066
2067 $result = core_course_external::get_courses_by_field('id', $course1->id);
2068 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2069 $this->assertCount(1, $result['courses']);
2070 $this->assertEquals($course1->id, $result['courses'][0]['id']);
2071 // Expect to receive all the fields.
ef83fc2a 2072 $this->assertCount(37, $result['courses'][0]);
80adabef
JL
2073
2074 $result = core_course_external::get_courses_by_field('id', $course2->id);
2075 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2076 $this->assertCount(1, $result['courses']);
2077 $this->assertEquals($course2->id, $result['courses'][0]['id']);
2078
2079 $result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id");
2080 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2081 $this->assertCount(2, $result['courses']);
2082
e45fc71e
JL
2083 // Check default filters.
2084 $this->assertCount(3, $result['courses'][0]['filters']);
2085 $this->assertCount(3, $result['courses'][1]['filters']);
2086
80adabef
JL
2087 $result = core_course_external::get_courses_by_field('category', $category1->id);
2088 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2089 $this->assertCount(1, $result['courses']);
2090 $this->assertEquals($course1->id, $result['courses'][0]['id']);
2091
2092 $result = core_course_external::get_courses_by_field('shortname', 'c1');
2093 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2094 $this->assertCount(1, $result['courses']);
2095 $this->assertEquals($course1->id, $result['courses'][0]['id']);
2096
2097 $result = core_course_external::get_courses_by_field('idnumber', 'i2');
2098 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2099 $this->assertCount(1, $result['courses']);
2100 $this->assertEquals($course2->id, $result['courses'][0]['id']);
2101
2102 $result = core_course_external::get_courses_by_field('idnumber', 'x');
2103 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2104 $this->assertCount(0, $result['courses']);
2105
e45fc71e
JL
2106 // Change filter value.
2107 filter_set_local_state('mediaplugin', context_course::instance($course1->id)->id, TEXTFILTER_OFF);
2108
80adabef
JL
2109 self::setUser($student1);
2110 // All visible courses (including front page) for normal student.
2111 $result = core_course_external::get_courses_by_field();
2112 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2113 $this->assertCount(2, $result['courses']);
ef83fc2a
JL
2114 $this->assertCount(30, $result['courses'][0]);
2115 $this->assertCount(30, $result['courses'][1]);
80adabef
JL
2116
2117 $result = core_course_external::get_courses_by_field('id', $course1->id);
2118 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2119 $this->assertCount(1, $result['courses']);
2120 $this->assertEquals($course1->id, $result['courses'][0]['id']);
2121 // Expect to receive all the files that a student can see.
ef83fc2a 2122 $this->assertCount(30, $result['courses'][0]);
e45fc71e
JL
2123
2124 // Check default filters.
2125 $filters = $result['courses'][0]['filters'];
2126 $this->assertCount(3, $filters);
2127 $found = false;
2128 foreach ($filters as $filter) {
2129 if ($filter['filter'] == 'mediaplugin' and $filter['localstate'] == TEXTFILTER_OFF) {
2130 $found = true;
2131 }
2132 }
2133 $this->assertTrue($found);
80adabef
JL
2134
2135 // Course 2 is not visible.
2136 $result = core_course_external::get_courses_by_field('id', $course2->id);
2137 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2138 $this->assertCount(0, $result['courses']);
2139
2140 $result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id");
2141 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2142 $this->assertCount(1, $result['courses']);
2143
2144 $result = core_course_external::get_courses_by_field('category', $category1->id);
2145 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2146 $this->assertCount(1, $result['courses']);
2147 $this->assertEquals($course1->id, $result['courses'][0]['id']);
2148
2149 $result = core_course_external::get_courses_by_field('shortname', 'c1');
2150 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2151 $this->assertCount(1, $result['courses']);
2152 $this->assertEquals($course1->id, $result['courses'][0]['id']);
2153
2154 $result = core_course_external::get_courses_by_field('idnumber', 'i2');
2155 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2156 $this->assertCount(0, $result['courses']);
2157
2158 $result = core_course_external::get_courses_by_field('idnumber', 'x');
2159 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2160 $this->assertCount(0, $result['courses']);
2161
2162 self::setUser($user1);
2163 // All visible courses (including front page) for authenticated user.
2164 $result = core_course_external::get_courses_by_field();
2165 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2166 $this->assertCount(2, $result['courses']);
ef83fc2a 2167 $this->assertCount(30, $result['courses'][0]); // Site course.
fb41d2f0 2168 $this->assertCount(13, $result['courses'][1]); // Only public information, not enrolled.
80adabef
JL
2169
2170 $result = core_course_external::get_courses_by_field('id', $course1->id);
2171 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2172 $this->assertCount(1, $result['courses']);
2173 $this->assertEquals($course1->id, $result['courses'][0]['id']);
2174 // Expect to receive all the files that a authenticated can see.
fb41d2f0 2175 $this->assertCount(13, $result['courses'][0]);
80adabef
JL
2176
2177 // Course 2 is not visible.
2178 $result = core_course_external::get_courses_by_field('id', $course2->id);
2179 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2180 $this->assertCount(0, $result['courses']);
2181
2182 $result = core_course_external::get_courses_by_field('ids', "$course1->id,$course2->id");
2183 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2184 $this->assertCount(1, $result['courses']);
2185
2186 $result = core_course_external::get_courses_by_field('category', $category1->id);
2187 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2188 $this->assertCount(1, $result['courses']);
2189 $this->assertEquals($course1->id, $result['courses'][0]['id']);
2190
2191 $result = core_course_external::get_courses_by_field('shortname', 'c1');
2192 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2193 $this->assertCount(1, $result['courses']);
2194 $this->assertEquals($course1->id, $result['courses'][0]['id']);
2195
2196 $result = core_course_external::get_courses_by_field('idnumber', 'i2');
2197 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2198 $this->assertCount(0, $result['courses']);
2199
2200 $result = core_course_external::get_courses_by_field('idnumber', 'x');
2201 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2202 $this->assertCount(0, $result['courses']);
2203 }
2204
2205 public function test_get_courses_by_field_invalid_field() {
2206 $this->expectException('invalid_parameter_exception');
2207 $result = core_course_external::get_courses_by_field('zyx', 'x');
2208 }
2209
2210 public function test_get_courses_by_field_invalid_courses() {
2211 $result = core_course_external::get_courses_by_field('id', '-1');
2212 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2213 $this->assertCount(0, $result['courses']);
2214 }
26659f62 2215
6db24235
JL
2216 /**
2217 * Test get_courses_by_field_invalid_theme_and_lang
2218 */
2219 public function test_get_courses_by_field_invalid_theme_and_lang() {
2220 $this->resetAfterTest(true);
2221 $this->setAdminUser();
2222
2223 $course = self::getDataGenerator()->create_course(array('theme' => 'kkt', 'lang' => 'kkl'));
2224 $result = core_course_external::get_courses_by_field('id', $course->id);
2225 $result = external_api::clean_returnvalue(core_course_external::get_courses_by_field_returns(), $result);
2226 $this->assertEmpty($result['courses']['0']['theme']);
2227 $this->assertEmpty($result['courses']['0']['lang']);
2228 }
2229
2230
26659f62
JL
2231 public function test_check_updates() {
2232 global $DB;
2233 $this->resetAfterTest(true);
2234 $this->setAdminUser();
2235
2236 // Create different types of activities.
2237 $course = self::getDataGenerator()->create_course();
2238 $tocreate = array('assign', 'book', 'choice', 'folder', 'forum', 'glossary', 'imscp', 'label', 'lti', 'page', 'quiz',
2239 'resource', 'scorm', 'survey', 'url', 'wiki');
2240
2241 $modules = array();
2242 foreach ($tocreate as $modname) {
2243 $modules[$modname]['instance'] = $this->getDataGenerator()->create_module($modname, array('course' => $course->id));
2244 $modules[$modname]['cm'] = get_coursemodule_from_id(false, $modules[$modname]['instance']->cmid);
2245 $modules[$modname]['context'] = context_module::instance($modules[$modname]['instance']->cmid);
2246 }
2247
2248 $student = self::getDataGenerator()->create_user();
2249 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
2250 self::getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
2251 $this->setUser($student);
2252
2253 $since = time();
2254 $this->waitForSecond();
2255 $params = array();
2256 foreach ($modules as $modname => $data) {
2257 $params[$data['cm']->id] = array(
2258 'contextlevel' => 'module',
2259 'id' => $data['cm']->id,
2260 'since' => $since
2261 );
2262 }
2263
2264 // Check there is nothing updated because modules are fresh new.
2265 $result = core_course_external::check_updates($course->id, $params);
2266 $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result);
25adfbaa 2267 $this->assertCount(0, $result['instances']);
26659f62 2268 $this->assertCount(0, $result['warnings']);
26659f62 2269
879a8f56
JL
2270 // Test with get_updates_since the same data.
2271 $result = core_course_external::get_updates_since($course->id, $since);
2272 $result = external_api::clean_returnvalue(core_course_external::get_updates_since_returns(), $result);
2273 $this->assertCount(0, $result['instances']);
2274 $this->assertCount(0, $result['warnings']);
2275
26659f62
JL
2276 // Update a module after a second.
2277 $this->waitForSecond();
2278 set_coursemodule_name($modules['forum']['cm']->id, 'New forum name');
2279
2280 $found = false;
2281 $result = core_course_external::check_updates($course->id, $params);
2282 $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result);
25adfbaa 2283 $this->assertCount(1, $result['instances']);
26659f62
JL
2284 $this->assertCount(0, $result['warnings']);
2285 foreach ($result['instances'] as $module) {
2286 foreach ($module['updates'] as $update) {
2287 if ($module['id'] == $modules['forum']['cm']->id and $update['name'] == 'configuration') {
26659f62 2288 $found = true;
879a8f56
JL
2289 }
2290 }
2291 }
2292 $this->assertTrue($found);
2293
2294 // Test with get_updates_since the same data.
2295 $result = core_course_external::get_updates_since($course->id, $since);
2296 $result = external_api::clean_returnvalue(core_course_external::get_updates_since_returns(), $result);
2297 $this->assertCount(1, $result['instances']);
2298 $this->assertCount(0, $result['warnings']);
2299 $found = false;
2300 $this->assertCount(1, $result['instances']);
2301 $this->assertCount(0, $result['warnings']);
2302 foreach ($result['instances'] as $module) {
2303 foreach ($module['updates'] as $update) {
2304 if ($module['id'] == $modules['forum']['cm']->id and $update['name'] == 'configuration') {
2305 $found = true;
26659f62
JL
2306 }
2307 }
2308 }
2309 $this->assertTrue($found);
2310
2311 // Do not retrieve the configuration field.
2312 $filter = array('files');
2313 $found = false;
2314 $result = core_course_external::check_updates($course->id, $params, $filter);
2315 $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result);
25adfbaa 2316 $this->assertCount(0, $result['instances']);
26659f62 2317 $this->assertCount(0, $result['warnings']);
26659f62
JL
2318 $this->assertFalse($found);
2319
2320 // Add invalid cmid.
2321 $params[] = array(
2322 'contextlevel' => 'module',
2323 'id' => -2,
2324 'since' => $since
2325 );
2326 $result = core_course_external::check_updates($course->id, $params);
2327 $result = external_api::clean_returnvalue(core_course_external::check_updates_returns(), $result);
2328 $this->assertCount(1, $result['warnings']);
2329 $this->assertEquals(-2, $result['warnings'][0]['itemid']);
2330 }
2a7a0216 2331}