weekly release 2.5dev
[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 */
1b1ad55b 40class core_course_external_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';
70 $category2->theme = 'base';
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
114 $this->assertGreaterThanOrEqual($category1->sortorder, $category3->sortorder);
115 $this->assertGreaterThanOrEqual($category2->sortorder, $category3->sortorder);
116
117 // Call without required capability
118 $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
119 $this->setExpectedException('required_capability_exception');
120 $createdsubcats = core_course_external::create_categories($subcategories);
121
122 }
123
124 /**
125 * Test delete categories
126 */
127 public function test_delete_categories() {
128 global $DB;
129
130 $this->resetAfterTest(true);
131
132 // Set the required capabilities by the external function
133 $contextid = context_system::instance()->id;
134 $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
135
136 $category1 = self::getDataGenerator()->create_category();
137 $category2 = self::getDataGenerator()->create_category(
138 array('parent' => $category1->id));
139 $category3 = self::getDataGenerator()->create_category();
140 $category4 = self::getDataGenerator()->create_category(
141 array('parent' => $category3->id));
142 $category5 = self::getDataGenerator()->create_category(
143 array('parent' => $category4->id));
144
145 //delete category 1 and 2 + delete category 4, category 5 moved under category 3
146 core_course_external::delete_categories(array(
147 array('id' => $category1->id, 'recursive' => 1),
148 array('id' => $category4->id)
149 ));
150
151 //check $category 1 and 2 are deleted
152 $notdeletedcount = $DB->count_records_select('course_categories',
153 'id IN ( ' . $category1->id . ',' . $category2->id . ',' . $category4->id . ')');
154 $this->assertEquals(0, $notdeletedcount);
155
156 //check that $category5 as $category3 for parent
157 $dbcategory5 = $DB->get_record('course_categories', array('id' => $category5->id));
158 $this->assertEquals($dbcategory5->path, $category3->path . '/' . $category5->id);
159
160 // Call without required capability
161 $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
162 $this->setExpectedException('required_capability_exception');
163 $createdsubcats = core_course_external::delete_categories(
164 array(array('id' => $category3->id)));
165 }
166
167 /**
168 * Test get categories
169 */
170 public function test_get_categories() {
171 global $DB;
172
173 $this->resetAfterTest(true);
7d6c58bc
JM
174
175 $generatedcats = array();
2a7a0216
JM
176 $category1data['idnumber'] = 'idnumbercat1';
177 $category1data['name'] = 'Category 1 for PHPunit test';
178 $category1data['description'] = 'Category 1 description';
179 $category1data['descriptionformat'] = FORMAT_MOODLE;
180 $category1 = self::getDataGenerator()->create_category($category1data);
7d6c58bc 181 $generatedcats[$category1->id] = $category1;
2a7a0216
JM
182 $category2 = self::getDataGenerator()->create_category(
183 array('parent' => $category1->id));
7d6c58bc 184 $generatedcats[$category2->id] = $category2;
2a7a0216
JM
185 $category6 = self::getDataGenerator()->create_category(
186 array('parent' => $category1->id, 'visible' => 0));
7d6c58bc 187 $generatedcats[$category6->id] = $category6;
2a7a0216 188 $category3 = self::getDataGenerator()->create_category();
7d6c58bc 189 $generatedcats[$category3->id] = $category3;
2a7a0216
JM
190 $category4 = self::getDataGenerator()->create_category(
191 array('parent' => $category3->id));
7d6c58bc 192 $generatedcats[$category4->id] = $category4;
2a7a0216
JM
193 $category5 = self::getDataGenerator()->create_category(
194 array('parent' => $category4->id));
7d6c58bc 195 $generatedcats[$category5->id] = $category5;
2a7a0216
JM
196
197 // Set the required capabilities by the external function.
198 $context = context_system::instance();
199 $roleid = $this->assignUserCapability('moodle/category:manage', $context->id);
200
201 // Retrieve category1 + sub-categories except not visible ones
202 $categories = core_course_external::get_categories(array(
203 array('key' => 'id', 'value' => $category1->id),
204 array('key' => 'visible', 'value' => 1)), 1);
205
fb695f6e
JM
206 // We need to execute the return values cleaning process to simulate the web service server.
207 $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
208
2a7a0216
JM
209 // Check we retrieve the good total number of categories.
210 $this->assertEquals(2, count($categories));
211
212 // Check the return values
7d6c58bc
JM
213 foreach ($categories as $category) {
214 $generatedcat = $generatedcats[$category['id']];
215 $this->assertEquals($category['idnumber'], $generatedcat->idnumber);
216 $this->assertEquals($category['name'], $generatedcat->name);
217 $this->assertEquals($category['description'], $generatedcat->description);
218 $this->assertEquals($category['descriptionformat'], FORMAT_HTML);
219 }
2a7a0216
JM
220
221 // Check different params.
222 $categories = core_course_external::get_categories(array(
223 array('key' => 'id', 'value' => $category1->id),
224 array('key' => 'idnumber', 'value' => $category1->idnumber),
225 array('key' => 'visible', 'value' => 1)), 0);
fb695f6e
JM
226
227 // We need to execute the return values cleaning process to simulate the web service server.
228 $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
229
2a7a0216
JM
230 $this->assertEquals(1, count($categories));
231
232 // Retrieve categories from parent.
233 $categories = core_course_external::get_categories(array(
234 array('key' => 'parent', 'value' => $category3->id)), 1);
235 $this->assertEquals(2, count($categories));
236
237 // Retrieve all categories.
238 $categories = core_course_external::get_categories();
fb695f6e
JM
239
240 // We need to execute the return values cleaning process to simulate the web service server.
241 $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
242
2a7a0216
JM
243 $this->assertEquals($DB->count_records('course_categories'), count($categories));
244
245 // Call without required capability (it will fail cause of the search on idnumber).
246 $this->unassignUserCapability('moodle/category:manage', $context->id, $roleid);
247 $this->setExpectedException('moodle_exception');
248 $categories = core_course_external::get_categories(array(
249 array('key' => 'id', 'value' => $category1->id),
250 array('key' => 'idnumber', 'value' => $category1->idnumber),
251 array('key' => 'visible', 'value' => 1)), 0);
252 }
253
254 /**
255 * Test update_categories
256 */
257 public function test_update_categories() {
258 global $DB;
259
260 $this->resetAfterTest(true);
261
262 // Set the required capabilities by the external function
263 $contextid = context_system::instance()->id;
264 $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
265
266 // Create base categories.
267 $category1data['idnumber'] = 'idnumbercat1';
268 $category1data['name'] = 'Category 1 for PHPunit test';
269 $category1data['description'] = 'Category 1 description';
270 $category1data['descriptionformat'] = FORMAT_MOODLE;
271 $category1 = self::getDataGenerator()->create_category($category1data);
272 $category2 = self::getDataGenerator()->create_category(
273 array('parent' => $category1->id));
274 $category3 = self::getDataGenerator()->create_category();
275 $category4 = self::getDataGenerator()->create_category(
276 array('parent' => $category3->id));
277 $category5 = self::getDataGenerator()->create_category(
278 array('parent' => $category4->id));
279
280 // We update all category1 attribut.
281 // Then we move cat4 and cat5 parent: cat3 => cat1
282 $categories = array(
283 array('id' => $category1->id,
284 'name' => $category1->name . '_updated',
285 'idnumber' => $category1->idnumber . '_updated',
286 'description' => $category1->description . '_updated',
287 'descriptionformat' => FORMAT_HTML,
288 'theme' => $category1->theme),
289 array('id' => $category4->id, 'parent' => $category1->id));
290
291 core_course_external::update_categories($categories);
292
293 // Check the values were updated.
294 $dbcategories = $DB->get_records_select('course_categories',
295 'id IN (' . $category1->id . ',' . $category2->id . ',' . $category2->id
296 . ',' . $category3->id . ',' . $category4->id . ',' . $category5->id .')');
297 $this->assertEquals($category1->name . '_updated',
298 $dbcategories[$category1->id]->name);
299 $this->assertEquals($category1->idnumber . '_updated',
300 $dbcategories[$category1->id]->idnumber);
301 $this->assertEquals($category1->description . '_updated',
302 $dbcategories[$category1->id]->description);
303 $this->assertEquals(FORMAT_HTML, $dbcategories[$category1->id]->descriptionformat);
304
305 // Check that category4 and category5 have been properly moved.
306 $this->assertEquals('/' . $category1->id . '/' . $category4->id,
307 $dbcategories[$category4->id]->path);
308 $this->assertEquals('/' . $category1->id . '/' . $category4->id . '/' . $category5->id,
309 $dbcategories[$category5->id]->path);
310
311 // Call without required capability.
312 $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
313 $this->setExpectedException('required_capability_exception');
314 core_course_external::update_categories($categories);
315 }
316
317 /**
318 * Test create_courses
319 */
320 public function test_create_courses() {
321 global $DB;
322
323 $this->resetAfterTest(true);
324
821676f5
JM
325 // Enable course completion.
326 set_config('enablecompletion', 1);
327
2a7a0216
JM
328 // Set the required capabilities by the external function
329 $contextid = context_system::instance()->id;
330 $roleid = $this->assignUserCapability('moodle/course:create', $contextid);
331 $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
332
333 $category = self::getDataGenerator()->create_category();
334
335 // Create base categories.
336 $course1['fullname'] = 'Test course 1';
337 $course1['shortname'] = 'Testcourse1';
338 $course1['categoryid'] = $category->id;
339 $course2['fullname'] = 'Test course 2';
340 $course2['shortname'] = 'Testcourse2';
341 $course2['categoryid'] = $category->id;
342 $course2['idnumber'] = 'testcourse2idnumber';
343 $course2['summary'] = 'Description for course 2';
344 $course2['summaryformat'] = FORMAT_MOODLE;
345 $course2['format'] = 'weeks';
346 $course2['showgrades'] = 1;
347 $course2['newsitems'] = 3;
4491273b 348 $course2['startdate'] = 1420092000; // 01/01/2015
2a7a0216
JM
349 $course2['numsections'] = 4;
350 $course2['maxbytes'] = 100000;
351 $course2['showreports'] = 1;
352 $course2['visible'] = 0;
353 $course2['hiddensections'] = 0;
354 $course2['groupmode'] = 0;
355 $course2['groupmodeforce'] = 0;
356 $course2['defaultgroupingid'] = 0;
357 $course2['enablecompletion'] = 1;
358 $course2['completionstartonenrol'] = 1;
359 $course2['completionnotify'] = 1;
360 $course2['lang'] = 'en';
361 $course2['forcetheme'] = 'base';
0e984d98
MG
362 $course3['fullname'] = 'Test course 3';
363 $course3['shortname'] = 'Testcourse3';
364 $course3['categoryid'] = $category->id;
365 $course3['format'] = 'topics';
366 $course3options = array('numsections' => 8,
367 'hiddensections' => 1,
368 'coursedisplay' => 1);
8d8d4da4 369 $course3['courseformatoptions'] = array();
0e984d98 370 foreach ($course3options as $key => $value) {
8d8d4da4 371 $course3['courseformatoptions'][] = array('name' => $key, 'value' => $value);
0e984d98 372 }
821676f5 373 $courses = array($course1, $course2, $course3);
2a7a0216
JM
374
375 $createdcourses = core_course_external::create_courses($courses);
376
fb695f6e
JM
377 // We need to execute the return values cleaning process to simulate the web service server.
378 $createdcourses = external_api::clean_returnvalue(core_course_external::create_courses_returns(), $createdcourses);
379
2a7a0216 380 // Check that right number of courses were created.
821676f5 381 $this->assertEquals(3, count($createdcourses));
2a7a0216
JM
382
383 // Check that the courses were correctly created.
384 foreach ($createdcourses as $createdcourse) {
850acb35 385 $courseinfo = course_get_format($createdcourse['id'])->get_course();
2a7a0216
JM
386
387 if ($createdcourse['shortname'] == $course2['shortname']) {
850acb35
MG
388 $this->assertEquals($courseinfo->fullname, $course2['fullname']);
389 $this->assertEquals($courseinfo->shortname, $course2['shortname']);
390 $this->assertEquals($courseinfo->category, $course2['categoryid']);
391 $this->assertEquals($courseinfo->idnumber, $course2['idnumber']);
392 $this->assertEquals($courseinfo->summary, $course2['summary']);
393 $this->assertEquals($courseinfo->summaryformat, $course2['summaryformat']);
394 $this->assertEquals($courseinfo->format, $course2['format']);
395 $this->assertEquals($courseinfo->showgrades, $course2['showgrades']);
396 $this->assertEquals($courseinfo->newsitems, $course2['newsitems']);
397 $this->assertEquals($courseinfo->startdate, $course2['startdate']);
398 $this->assertEquals($courseinfo->numsections, $course2['numsections']);
399 $this->assertEquals($courseinfo->maxbytes, $course2['maxbytes']);
400 $this->assertEquals($courseinfo->showreports, $course2['showreports']);
401 $this->assertEquals($courseinfo->visible, $course2['visible']);
402 $this->assertEquals($courseinfo->hiddensections, $course2['hiddensections']);
403 $this->assertEquals($courseinfo->groupmode, $course2['groupmode']);
404 $this->assertEquals($courseinfo->groupmodeforce, $course2['groupmodeforce']);
405 $this->assertEquals($courseinfo->defaultgroupingid, $course2['defaultgroupingid']);
406 $this->assertEquals($courseinfo->completionnotify, $course2['completionnotify']);
407 $this->assertEquals($courseinfo->lang, $course2['lang']);
2a7a0216
JM
408
409 if (!empty($CFG->allowcoursethemes)) {
850acb35 410 $this->assertEquals($courseinfo->theme, $course2['forcetheme']);
2a7a0216
JM
411 }
412
821676f5
JM
413 // We enabled completion at the beginning of the test.
414 $this->assertEquals($courseinfo->enablecompletion, $course2['enablecompletion']);
415 $this->assertEquals($courseinfo->completionstartonenrol, $course2['completionstartonenrol']);
2a7a0216
JM
416
417 } else if ($createdcourse['shortname'] == $course1['shortname']) {
418 $courseconfig = get_config('moodlecourse');
850acb35
MG
419 $this->assertEquals($courseinfo->fullname, $course1['fullname']);
420 $this->assertEquals($courseinfo->shortname, $course1['shortname']);
421 $this->assertEquals($courseinfo->category, $course1['categoryid']);
422 $this->assertEquals($courseinfo->summaryformat, FORMAT_HTML);
423 $this->assertEquals($courseinfo->format, $courseconfig->format);
424 $this->assertEquals($courseinfo->showgrades, $courseconfig->showgrades);
425 $this->assertEquals($courseinfo->newsitems, $courseconfig->newsitems);
426 $this->assertEquals($courseinfo->maxbytes, $courseconfig->maxbytes);
427 $this->assertEquals($courseinfo->showreports, $courseconfig->showreports);
428 $this->assertEquals($courseinfo->groupmode, $courseconfig->groupmode);
429 $this->assertEquals($courseinfo->groupmodeforce, $courseconfig->groupmodeforce);
430 $this->assertEquals($courseinfo->defaultgroupingid, 0);
0e984d98 431 } else if ($createdcourse['shortname'] == $course3['shortname']) {
850acb35
MG
432 $this->assertEquals($courseinfo->fullname, $course3['fullname']);
433 $this->assertEquals($courseinfo->shortname, $course3['shortname']);
434 $this->assertEquals($courseinfo->category, $course3['categoryid']);
435 $this->assertEquals($courseinfo->format, $course3['format']);
436 $this->assertEquals($courseinfo->hiddensections, $course3options['hiddensections']);
437 $this->assertEquals($courseinfo->numsections, $course3options['numsections']);
438 $this->assertEquals($courseinfo->coursedisplay, $course3options['coursedisplay']);
2a7a0216
JM
439 } else {
440 throw moodle_exception('Unexpected shortname');
441 }
442 }
443
444 // Call without required capability
445 $this->unassignUserCapability('moodle/course:create', $contextid, $roleid);
446 $this->setExpectedException('required_capability_exception');
447 $createdsubcats = core_course_external::create_courses($courses);
448 }
449
450 /**
451 * Test delete_courses
452 */
453 public function test_delete_courses() {
454 global $DB, $USER;
455
456 $this->resetAfterTest(true);
457
458 // Admin can delete a course.
459 $this->setAdminUser();
460 // Validate_context() will fail as the email is not set by $this->setAdminUser().
461 $USER->email = 'emailtopass@contextvalidation.me';
462
463 $course1 = self::getDataGenerator()->create_course();
464 $course2 = self::getDataGenerator()->create_course();
465 $course3 = self::getDataGenerator()->create_course();
466
467 // Delete courses.
468 core_course_external::delete_courses(array($course1->id, $course2->id));
469
470 // Check $course 1 and 2 are deleted.
471 $notdeletedcount = $DB->count_records_select('course',
472 'id IN ( ' . $course1->id . ',' . $course2->id . ')');
473 $this->assertEquals(0, $notdeletedcount);
474
475 // Fail when the user is not allow to access the course (enrolled) or is not admin.
476 $this->setGuestUser();
477 $this->setExpectedException('require_login_exception');
478 $createdsubcats = core_course_external::delete_courses(array($course3->id));
479 }
480
481 /**
482 * Test get_courses
483 */
484 public function test_get_courses () {
485 global $DB;
486
487 $this->resetAfterTest(true);
488
7d6c58bc 489 $generatedcourses = array();
2a7a0216
JM
490 $coursedata['idnumber'] = 'idnumbercourse1';
491 $coursedata['fullname'] = 'Course 1 for PHPunit test';
492 $coursedata['summary'] = 'Course 1 description';
493 $coursedata['summaryformat'] = FORMAT_MOODLE;
494 $course1 = self::getDataGenerator()->create_course($coursedata);
7d6c58bc 495 $generatedcourses[$course1->id] = $course1;
2a7a0216 496 $course2 = self::getDataGenerator()->create_course();
7d6c58bc 497 $generatedcourses[$course2->id] = $course2;
0e984d98 498 $course3 = self::getDataGenerator()->create_course(array('format' => 'topics'));
7d6c58bc 499 $generatedcourses[$course3->id] = $course3;
2a7a0216
JM
500
501 // Set the required capabilities by the external function.
502 $context = context_system::instance();
503 $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
504 $this->assignUserCapability('moodle/course:update',
505 context_course::instance($course1->id)->id, $roleid);
506 $this->assignUserCapability('moodle/course:update',
507 context_course::instance($course2->id)->id, $roleid);
508 $this->assignUserCapability('moodle/course:update',
509 context_course::instance($course3->id)->id, $roleid);
510
511 $courses = core_course_external::get_courses(array('ids' =>
512 array($course1->id, $course2->id)));
513
fb695f6e
JM
514 // We need to execute the return values cleaning process to simulate the web service server.
515 $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
516
2a7a0216
JM
517 // Check we retrieve the good total number of categories.
518 $this->assertEquals(2, count($courses));
519
7d6c58bc
JM
520 foreach ($courses as $course) {
521 $dbcourse = $generatedcourses[$course['id']];
522 $this->assertEquals($course['idnumber'], $dbcourse->idnumber);
523 $this->assertEquals($course['fullname'], $dbcourse->fullname);
524 $this->assertEquals($course['summary'], $dbcourse->summary);
525 $this->assertEquals($course['summaryformat'], FORMAT_HTML);
526 $this->assertEquals($course['shortname'], $dbcourse->shortname);
527 $this->assertEquals($course['categoryid'], $dbcourse->category);
528 $this->assertEquals($course['format'], $dbcourse->format);
529 $this->assertEquals($course['showgrades'], $dbcourse->showgrades);
530 $this->assertEquals($course['newsitems'], $dbcourse->newsitems);
531 $this->assertEquals($course['startdate'], $dbcourse->startdate);
532 $this->assertEquals($course['numsections'], $dbcourse->numsections);
533 $this->assertEquals($course['maxbytes'], $dbcourse->maxbytes);
534 $this->assertEquals($course['showreports'], $dbcourse->showreports);
535 $this->assertEquals($course['visible'], $dbcourse->visible);
536 $this->assertEquals($course['hiddensections'], $dbcourse->hiddensections);
537 $this->assertEquals($course['groupmode'], $dbcourse->groupmode);
538 $this->assertEquals($course['groupmodeforce'], $dbcourse->groupmodeforce);
539 $this->assertEquals($course['defaultgroupingid'], $dbcourse->defaultgroupingid);
540 $this->assertEquals($course['completionnotify'], $dbcourse->completionnotify);
541 $this->assertEquals($course['lang'], $dbcourse->lang);
542 $this->assertEquals($course['forcetheme'], $dbcourse->theme);
543 $this->assertEquals($course['completionstartonenrol'], $dbcourse->completionstartonenrol);
544 $this->assertEquals($course['enablecompletion'], $dbcourse->enablecompletion);
545 $this->assertEquals($course['completionstartonenrol'], $dbcourse->completionstartonenrol);
0e984d98 546 if ($dbcourse->format === 'topics') {
8d8d4da4
MG
547 $this->assertEquals($course['courseformatoptions'], array(
548 array('name' => 'numsections', 'value' => $dbcourse->numsections),
549 array('name' => 'hiddensections', 'value' => $dbcourse->hiddensections),
550 array('name' => 'coursedisplay', 'value' => $dbcourse->coursedisplay),
0e984d98
MG
551 ));
552 }
7d6c58bc 553 }
2a7a0216
JM
554
555 // Get all courses in the DB
556 $courses = core_course_external::get_courses(array());
fb695f6e
JM
557
558 // We need to execute the return values cleaning process to simulate the web service server.
559 $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
560
2a7a0216
JM
561 $this->assertEquals($DB->count_records('course'), count($courses));
562 }
563
564 /**
565 * Test get_course_contents
566 */
567 public function test_get_course_contents() {
568 $this->resetAfterTest(true);
569
570 $course = self::getDataGenerator()->create_course();
571 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
572 $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
573 $forumcontext = context_module::instance($forum->cmid);
574 $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
575 $datacontext = context_module::instance($data->cmid);
576 $datacm = get_coursemodule_from_instance('page', $data->id);
577 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
578 $pagecontext = context_module::instance($page->cmid);
579 $pagecm = get_coursemodule_from_instance('page', $page->id);
580
581 // Set the required capabilities by the external function.
582 $context = context_course::instance($course->id);
583 $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
584 $this->assignUserCapability('moodle/course:update', $context->id, $roleid);
585
586 $courses = core_course_external::get_course_contents($course->id, array());
587
fb695f6e
JM
588 // We need to execute the return values cleaning process to simulate the web service server.
589 $courses = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $courses);
590
2a7a0216
JM
591 // Check that the course has the 3 created modules
592 $this->assertEquals(3, count($courses[0]['modules']));
593 }
594
595 /**
596 * Test duplicate_course
597 */
598 public function test_duplicate_course() {
599 $this->resetAfterTest(true);
600
601 // Create one course with three modules.
602 $course = self::getDataGenerator()->create_course();
603 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
604 $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
605 $forumcontext = context_module::instance($forum->cmid);
606 $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
607 $datacontext = context_module::instance($data->cmid);
608 $datacm = get_coursemodule_from_instance('page', $data->id);
609 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
610 $pagecontext = context_module::instance($page->cmid);
611 $pagecm = get_coursemodule_from_instance('page', $page->id);
612
613 // Set the required capabilities by the external function.
614 $coursecontext = context_course::instance($course->id);
615 $categorycontext = context_coursecat::instance($course->category);
616 $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id);
617 $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid);
618 $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid);
619 $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid);
620 $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid);
621 // Optional capabilities to copy user data.
622 $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid);
623 $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid);
624
625 $newcourse['fullname'] = 'Course duplicate';
626 $newcourse['shortname'] = 'courseduplicate';
627 $newcourse['categoryid'] = $course->category;
628 $newcourse['visible'] = true;
629 $newcourse['options'][] = array('name' => 'users', 'value' => true);
630
631 $duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'],
632 $newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']);
633
fb695f6e
JM
634 // We need to execute the return values cleaning process to simulate the web service server.
635 $duplicate = external_api::clean_returnvalue(core_course_external::duplicate_course_returns(), $duplicate);
636
2a7a0216
JM
637 // Check that the course has been duplicated.
638 $this->assertEquals($newcourse['shortname'], $duplicate['shortname']);
639 }
640}