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
325 // Set the required capabilities by the external function
326 $contextid = context_system::instance()->id;
327 $roleid = $this->assignUserCapability('moodle/course:create', $contextid);
328 $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
329
330 $category = self::getDataGenerator()->create_category();
331
332 // Create base categories.
333 $course1['fullname'] = 'Test course 1';
334 $course1['shortname'] = 'Testcourse1';
335 $course1['categoryid'] = $category->id;
336 $course2['fullname'] = 'Test course 2';
337 $course2['shortname'] = 'Testcourse2';
338 $course2['categoryid'] = $category->id;
339 $course2['idnumber'] = 'testcourse2idnumber';
340 $course2['summary'] = 'Description for course 2';
341 $course2['summaryformat'] = FORMAT_MOODLE;
342 $course2['format'] = 'weeks';
343 $course2['showgrades'] = 1;
344 $course2['newsitems'] = 3;
4491273b 345 $course2['startdate'] = 1420092000; // 01/01/2015
2a7a0216
JM
346 $course2['numsections'] = 4;
347 $course2['maxbytes'] = 100000;
348 $course2['showreports'] = 1;
349 $course2['visible'] = 0;
350 $course2['hiddensections'] = 0;
351 $course2['groupmode'] = 0;
352 $course2['groupmodeforce'] = 0;
353 $course2['defaultgroupingid'] = 0;
354 $course2['enablecompletion'] = 1;
355 $course2['completionstartonenrol'] = 1;
356 $course2['completionnotify'] = 1;
357 $course2['lang'] = 'en';
358 $course2['forcetheme'] = 'base';
0e984d98
MG
359 $course3['fullname'] = 'Test course 3';
360 $course3['shortname'] = 'Testcourse3';
361 $course3['categoryid'] = $category->id;
362 $course3['format'] = 'topics';
363 $course3options = array('numsections' => 8,
364 'hiddensections' => 1,
365 'coursedisplay' => 1);
8d8d4da4 366 $course3['courseformatoptions'] = array();
0e984d98 367 foreach ($course3options as $key => $value) {
8d8d4da4 368 $course3['courseformatoptions'][] = array('name' => $key, 'value' => $value);
0e984d98 369 }
2a7a0216
JM
370 $courses = array($course1, $course2);
371
372 $createdcourses = core_course_external::create_courses($courses);
373
fb695f6e
JM
374 // We need to execute the return values cleaning process to simulate the web service server.
375 $createdcourses = external_api::clean_returnvalue(core_course_external::create_courses_returns(), $createdcourses);
376
2a7a0216
JM
377 // Check that right number of courses were created.
378 $this->assertEquals(2, count($createdcourses));
379
380 // Check that the courses were correctly created.
381 foreach ($createdcourses as $createdcourse) {
850acb35 382 $courseinfo = course_get_format($createdcourse['id'])->get_course();
2a7a0216
JM
383
384 if ($createdcourse['shortname'] == $course2['shortname']) {
850acb35
MG
385 $this->assertEquals($courseinfo->fullname, $course2['fullname']);
386 $this->assertEquals($courseinfo->shortname, $course2['shortname']);
387 $this->assertEquals($courseinfo->category, $course2['categoryid']);
388 $this->assertEquals($courseinfo->idnumber, $course2['idnumber']);
389 $this->assertEquals($courseinfo->summary, $course2['summary']);
390 $this->assertEquals($courseinfo->summaryformat, $course2['summaryformat']);
391 $this->assertEquals($courseinfo->format, $course2['format']);
392 $this->assertEquals($courseinfo->showgrades, $course2['showgrades']);
393 $this->assertEquals($courseinfo->newsitems, $course2['newsitems']);
394 $this->assertEquals($courseinfo->startdate, $course2['startdate']);
395 $this->assertEquals($courseinfo->numsections, $course2['numsections']);
396 $this->assertEquals($courseinfo->maxbytes, $course2['maxbytes']);
397 $this->assertEquals($courseinfo->showreports, $course2['showreports']);
398 $this->assertEquals($courseinfo->visible, $course2['visible']);
399 $this->assertEquals($courseinfo->hiddensections, $course2['hiddensections']);
400 $this->assertEquals($courseinfo->groupmode, $course2['groupmode']);
401 $this->assertEquals($courseinfo->groupmodeforce, $course2['groupmodeforce']);
402 $this->assertEquals($courseinfo->defaultgroupingid, $course2['defaultgroupingid']);
403 $this->assertEquals($courseinfo->completionnotify, $course2['completionnotify']);
404 $this->assertEquals($courseinfo->lang, $course2['lang']);
2a7a0216
JM
405
406 if (!empty($CFG->allowcoursethemes)) {
850acb35 407 $this->assertEquals($courseinfo->theme, $course2['forcetheme']);
2a7a0216
JM
408 }
409
410 if (completion_info::is_enabled_for_site()) {
850acb35
MG
411 $this->assertEquals($courseinfo->enablecompletion, $course2['enabledcompletion']);
412 $this->assertEquals($courseinfo->completionstartonenrol, $course2['completionstartonenrol']);
2a7a0216 413 } else {
850acb35
MG
414 $this->assertEquals($courseinfo->enablecompletion, 0);
415 $this->assertEquals($courseinfo->completionstartonenrol, 0);
2a7a0216
JM
416 }
417
418 } else if ($createdcourse['shortname'] == $course1['shortname']) {
419 $courseconfig = get_config('moodlecourse');
850acb35
MG
420 $this->assertEquals($courseinfo->fullname, $course1['fullname']);
421 $this->assertEquals($courseinfo->shortname, $course1['shortname']);
422 $this->assertEquals($courseinfo->category, $course1['categoryid']);
423 $this->assertEquals($courseinfo->summaryformat, FORMAT_HTML);
424 $this->assertEquals($courseinfo->format, $courseconfig->format);
425 $this->assertEquals($courseinfo->showgrades, $courseconfig->showgrades);
426 $this->assertEquals($courseinfo->newsitems, $courseconfig->newsitems);
427 $this->assertEquals($courseinfo->maxbytes, $courseconfig->maxbytes);
428 $this->assertEquals($courseinfo->showreports, $courseconfig->showreports);
429 $this->assertEquals($courseinfo->groupmode, $courseconfig->groupmode);
430 $this->assertEquals($courseinfo->groupmodeforce, $courseconfig->groupmodeforce);
431 $this->assertEquals($courseinfo->defaultgroupingid, 0);
0e984d98 432 } else if ($createdcourse['shortname'] == $course3['shortname']) {
850acb35
MG
433 $this->assertEquals($courseinfo->fullname, $course3['fullname']);
434 $this->assertEquals($courseinfo->shortname, $course3['shortname']);
435 $this->assertEquals($courseinfo->category, $course3['categoryid']);
436 $this->assertEquals($courseinfo->format, $course3['format']);
437 $this->assertEquals($courseinfo->hiddensections, $course3options['hiddensections']);
438 $this->assertEquals($courseinfo->numsections, $course3options['numsections']);
439 $this->assertEquals($courseinfo->coursedisplay, $course3options['coursedisplay']);
2a7a0216
JM
440 } else {
441 throw moodle_exception('Unexpected shortname');
442 }
443 }
444
445 // Call without required capability
446 $this->unassignUserCapability('moodle/course:create', $contextid, $roleid);
447 $this->setExpectedException('required_capability_exception');
448 $createdsubcats = core_course_external::create_courses($courses);
449 }
450
451 /**
452 * Test delete_courses
453 */
454 public function test_delete_courses() {
455 global $DB, $USER;
456
457 $this->resetAfterTest(true);
458
459 // Admin can delete a course.
460 $this->setAdminUser();
461 // Validate_context() will fail as the email is not set by $this->setAdminUser().
462 $USER->email = 'emailtopass@contextvalidation.me';
463
464 $course1 = self::getDataGenerator()->create_course();
465 $course2 = self::getDataGenerator()->create_course();
466 $course3 = self::getDataGenerator()->create_course();
467
468 // Delete courses.
469 core_course_external::delete_courses(array($course1->id, $course2->id));
470
471 // Check $course 1 and 2 are deleted.
472 $notdeletedcount = $DB->count_records_select('course',
473 'id IN ( ' . $course1->id . ',' . $course2->id . ')');
474 $this->assertEquals(0, $notdeletedcount);
475
476 // Fail when the user is not allow to access the course (enrolled) or is not admin.
477 $this->setGuestUser();
478 $this->setExpectedException('require_login_exception');
479 $createdsubcats = core_course_external::delete_courses(array($course3->id));
480 }
481
482 /**
483 * Test get_courses
484 */
485 public function test_get_courses () {
486 global $DB;
487
488 $this->resetAfterTest(true);
489
7d6c58bc 490 $generatedcourses = array();
2a7a0216
JM
491 $coursedata['idnumber'] = 'idnumbercourse1';
492 $coursedata['fullname'] = 'Course 1 for PHPunit test';
493 $coursedata['summary'] = 'Course 1 description';
494 $coursedata['summaryformat'] = FORMAT_MOODLE;
495 $course1 = self::getDataGenerator()->create_course($coursedata);
7d6c58bc 496 $generatedcourses[$course1->id] = $course1;
2a7a0216 497 $course2 = self::getDataGenerator()->create_course();
7d6c58bc 498 $generatedcourses[$course2->id] = $course2;
0e984d98 499 $course3 = self::getDataGenerator()->create_course(array('format' => 'topics'));
7d6c58bc 500 $generatedcourses[$course3->id] = $course3;
2a7a0216
JM
501
502 // Set the required capabilities by the external function.
503 $context = context_system::instance();
504 $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
505 $this->assignUserCapability('moodle/course:update',
506 context_course::instance($course1->id)->id, $roleid);
507 $this->assignUserCapability('moodle/course:update',
508 context_course::instance($course2->id)->id, $roleid);
509 $this->assignUserCapability('moodle/course:update',
510 context_course::instance($course3->id)->id, $roleid);
511
512 $courses = core_course_external::get_courses(array('ids' =>
513 array($course1->id, $course2->id)));
514
fb695f6e
JM
515 // We need to execute the return values cleaning process to simulate the web service server.
516 $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
517
2a7a0216
JM
518 // Check we retrieve the good total number of categories.
519 $this->assertEquals(2, count($courses));
520
7d6c58bc
JM
521 foreach ($courses as $course) {
522 $dbcourse = $generatedcourses[$course['id']];
523 $this->assertEquals($course['idnumber'], $dbcourse->idnumber);
524 $this->assertEquals($course['fullname'], $dbcourse->fullname);
525 $this->assertEquals($course['summary'], $dbcourse->summary);
526 $this->assertEquals($course['summaryformat'], FORMAT_HTML);
527 $this->assertEquals($course['shortname'], $dbcourse->shortname);
528 $this->assertEquals($course['categoryid'], $dbcourse->category);
529 $this->assertEquals($course['format'], $dbcourse->format);
530 $this->assertEquals($course['showgrades'], $dbcourse->showgrades);
531 $this->assertEquals($course['newsitems'], $dbcourse->newsitems);
532 $this->assertEquals($course['startdate'], $dbcourse->startdate);
533 $this->assertEquals($course['numsections'], $dbcourse->numsections);
534 $this->assertEquals($course['maxbytes'], $dbcourse->maxbytes);
535 $this->assertEquals($course['showreports'], $dbcourse->showreports);
536 $this->assertEquals($course['visible'], $dbcourse->visible);
537 $this->assertEquals($course['hiddensections'], $dbcourse->hiddensections);
538 $this->assertEquals($course['groupmode'], $dbcourse->groupmode);
539 $this->assertEquals($course['groupmodeforce'], $dbcourse->groupmodeforce);
540 $this->assertEquals($course['defaultgroupingid'], $dbcourse->defaultgroupingid);
541 $this->assertEquals($course['completionnotify'], $dbcourse->completionnotify);
542 $this->assertEquals($course['lang'], $dbcourse->lang);
543 $this->assertEquals($course['forcetheme'], $dbcourse->theme);
544 $this->assertEquals($course['completionstartonenrol'], $dbcourse->completionstartonenrol);
545 $this->assertEquals($course['enablecompletion'], $dbcourse->enablecompletion);
546 $this->assertEquals($course['completionstartonenrol'], $dbcourse->completionstartonenrol);
0e984d98 547 if ($dbcourse->format === 'topics') {
8d8d4da4
MG
548 $this->assertEquals($course['courseformatoptions'], array(
549 array('name' => 'numsections', 'value' => $dbcourse->numsections),
550 array('name' => 'hiddensections', 'value' => $dbcourse->hiddensections),
551 array('name' => 'coursedisplay', 'value' => $dbcourse->coursedisplay),
0e984d98
MG
552 ));
553 }
7d6c58bc 554 }
2a7a0216
JM
555
556 // Get all courses in the DB
557 $courses = core_course_external::get_courses(array());
fb695f6e
JM
558
559 // We need to execute the return values cleaning process to simulate the web service server.
560 $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
561
2a7a0216
JM
562 $this->assertEquals($DB->count_records('course'), count($courses));
563 }
564
565 /**
566 * Test get_course_contents
567 */
568 public function test_get_course_contents() {
569 $this->resetAfterTest(true);
570
571 $course = self::getDataGenerator()->create_course();
572 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
573 $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
574 $forumcontext = context_module::instance($forum->cmid);
575 $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
576 $datacontext = context_module::instance($data->cmid);
577 $datacm = get_coursemodule_from_instance('page', $data->id);
578 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
579 $pagecontext = context_module::instance($page->cmid);
580 $pagecm = get_coursemodule_from_instance('page', $page->id);
581
582 // Set the required capabilities by the external function.
583 $context = context_course::instance($course->id);
584 $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
585 $this->assignUserCapability('moodle/course:update', $context->id, $roleid);
586
587 $courses = core_course_external::get_course_contents($course->id, array());
588
fb695f6e
JM
589 // We need to execute the return values cleaning process to simulate the web service server.
590 $courses = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $courses);
591
2a7a0216
JM
592 // Check that the course has the 3 created modules
593 $this->assertEquals(3, count($courses[0]['modules']));
594 }
595
596 /**
597 * Test duplicate_course
598 */
599 public function test_duplicate_course() {
600 $this->resetAfterTest(true);
601
602 // Create one course with three modules.
603 $course = self::getDataGenerator()->create_course();
604 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
605 $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
606 $forumcontext = context_module::instance($forum->cmid);
607 $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
608 $datacontext = context_module::instance($data->cmid);
609 $datacm = get_coursemodule_from_instance('page', $data->id);
610 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
611 $pagecontext = context_module::instance($page->cmid);
612 $pagecm = get_coursemodule_from_instance('page', $page->id);
613
614 // Set the required capabilities by the external function.
615 $coursecontext = context_course::instance($course->id);
616 $categorycontext = context_coursecat::instance($course->category);
617 $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id);
618 $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid);
619 $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid);
620 $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid);
621 $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid);
622 // Optional capabilities to copy user data.
623 $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid);
624 $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid);
625
626 $newcourse['fullname'] = 'Course duplicate';
627 $newcourse['shortname'] = 'courseduplicate';
628 $newcourse['categoryid'] = $course->category;
629 $newcourse['visible'] = true;
630 $newcourse['options'][] = array('name' => 'users', 'value' => true);
631
632 $duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'],
633 $newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']);
634
fb695f6e
JM
635 // We need to execute the return values cleaning process to simulate the web service server.
636 $duplicate = external_api::clean_returnvalue(core_course_external::duplicate_course_returns(), $duplicate);
637
2a7a0216
JM
638 // Check that the course has been duplicated.
639 $this->assertEquals($newcourse['shortname'], $duplicate['shortname']);
640 }
641}