Merge branch 'MDL-37476-stable23' of git://github.com/mouneyrac/moodle into MOODLE_23...
[moodle.git] / course / tests / externallib_test.php
CommitLineData
08641906
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 */
c3f59e04 40class core_course_external_testcase extends externallib_advanced_testcase {
08641906
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
96ace848
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
08641906
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
96ace848
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
08641906
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);
a66863c3
JM
174
175 $generatedcats = array();
08641906
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);
a66863c3 181 $generatedcats[$category1->id] = $category1;
08641906
JM
182 $category2 = self::getDataGenerator()->create_category(
183 array('parent' => $category1->id));
a66863c3 184 $generatedcats[$category2->id] = $category2;
08641906
JM
185 $category6 = self::getDataGenerator()->create_category(
186 array('parent' => $category1->id, 'visible' => 0));
a66863c3 187 $generatedcats[$category6->id] = $category6;
08641906 188 $category3 = self::getDataGenerator()->create_category();
a66863c3 189 $generatedcats[$category3->id] = $category3;
08641906
JM
190 $category4 = self::getDataGenerator()->create_category(
191 array('parent' => $category3->id));
a66863c3 192 $generatedcats[$category4->id] = $category4;
08641906
JM
193 $category5 = self::getDataGenerator()->create_category(
194 array('parent' => $category4->id));
a66863c3 195 $generatedcats[$category5->id] = $category5;
08641906
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
96ace848
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
08641906
JM
209 // Check we retrieve the good total number of categories.
210 $this->assertEquals(2, count($categories));
211
212 // Check the return values
a66863c3
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 }
08641906
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);
96ace848
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
08641906
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();
96ace848
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
08641906
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
30e2ad8b
JM
325 // Enable course completion.
326 set_config('enablecompletion', 1);
327
08641906
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;
7a4f9ba8 348 $course2['startdate'] = 1420092000; // 01/01/2015
08641906
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';
362 $courses = array($course1, $course2);
363
364 $createdcourses = core_course_external::create_courses($courses);
365
96ace848
JM
366 // We need to execute the return values cleaning process to simulate the web service server.
367 $createdcourses = external_api::clean_returnvalue(core_course_external::create_courses_returns(), $createdcourses);
368
08641906
JM
369 // Check that right number of courses were created.
370 $this->assertEquals(2, count($createdcourses));
371
372 // Check that the courses were correctly created.
373 foreach ($createdcourses as $createdcourse) {
374 $dbcourse = $DB->get_record('course', array('id' => $createdcourse['id']));
375
376 if ($createdcourse['shortname'] == $course2['shortname']) {
377 $this->assertEquals($dbcourse->fullname, $course2['fullname']);
378 $this->assertEquals($dbcourse->shortname, $course2['shortname']);
379 $this->assertEquals($dbcourse->category, $course2['categoryid']);
380 $this->assertEquals($dbcourse->idnumber, $course2['idnumber']);
381 $this->assertEquals($dbcourse->summary, $course2['summary']);
382 $this->assertEquals($dbcourse->summaryformat, $course2['summaryformat']);
383 $this->assertEquals($dbcourse->format, $course2['format']);
384 $this->assertEquals($dbcourse->showgrades, $course2['showgrades']);
385 $this->assertEquals($dbcourse->newsitems, $course2['newsitems']);
386 $this->assertEquals($dbcourse->startdate, $course2['startdate']);
387 $this->assertEquals($dbcourse->numsections, $course2['numsections']);
388 $this->assertEquals($dbcourse->maxbytes, $course2['maxbytes']);
389 $this->assertEquals($dbcourse->showreports, $course2['showreports']);
390 $this->assertEquals($dbcourse->visible, $course2['visible']);
391 $this->assertEquals($dbcourse->hiddensections, $course2['hiddensections']);
392 $this->assertEquals($dbcourse->groupmode, $course2['groupmode']);
393 $this->assertEquals($dbcourse->groupmodeforce, $course2['groupmodeforce']);
394 $this->assertEquals($dbcourse->defaultgroupingid, $course2['defaultgroupingid']);
395 $this->assertEquals($dbcourse->completionnotify, $course2['completionnotify']);
396 $this->assertEquals($dbcourse->lang, $course2['lang']);
397
398 if (!empty($CFG->allowcoursethemes)) {
399 $this->assertEquals($dbcourse->theme, $course2['forcetheme']);
400 }
401
30e2ad8b
JM
402 $this->assertEquals($dbcourse->enablecompletion, $course2['enablecompletion']);
403 $this->assertEquals($dbcourse->completionstartonenrol, $course2['completionstartonenrol']);
08641906
JM
404
405 } else if ($createdcourse['shortname'] == $course1['shortname']) {
406 $courseconfig = get_config('moodlecourse');
407 $this->assertEquals($dbcourse->fullname, $course1['fullname']);
408 $this->assertEquals($dbcourse->shortname, $course1['shortname']);
409 $this->assertEquals($dbcourse->category, $course1['categoryid']);
410 $this->assertEquals($dbcourse->summaryformat, FORMAT_HTML);
411 $this->assertEquals($dbcourse->format, $courseconfig->format);
412 $this->assertEquals($dbcourse->showgrades, $courseconfig->showgrades);
413 $this->assertEquals($dbcourse->newsitems, $courseconfig->newsitems);
414 $this->assertEquals($dbcourse->numsections, $courseconfig->numsections);
415 $this->assertEquals($dbcourse->maxbytes, $courseconfig->maxbytes);
416 $this->assertEquals($dbcourse->showreports, $courseconfig->showreports);
417 $this->assertEquals($dbcourse->hiddensections, $courseconfig->hiddensections);
418 $this->assertEquals($dbcourse->groupmode, $courseconfig->groupmode);
419 $this->assertEquals($dbcourse->groupmodeforce, $courseconfig->groupmodeforce);
420 $this->assertEquals($dbcourse->defaultgroupingid, 0);
421 } else {
422 throw moodle_exception('Unexpected shortname');
423 }
424 }
425
426 // Call without required capability
427 $this->unassignUserCapability('moodle/course:create', $contextid, $roleid);
428 $this->setExpectedException('required_capability_exception');
429 $createdsubcats = core_course_external::create_courses($courses);
430 }
431
432 /**
433 * Test delete_courses
434 */
435 public function test_delete_courses() {
436 global $DB, $USER;
437
438 $this->resetAfterTest(true);
439
440 // Admin can delete a course.
441 $this->setAdminUser();
442 // Validate_context() will fail as the email is not set by $this->setAdminUser().
443 $USER->email = 'emailtopass@contextvalidation.me';
444
445 $course1 = self::getDataGenerator()->create_course();
446 $course2 = self::getDataGenerator()->create_course();
447 $course3 = self::getDataGenerator()->create_course();
448
449 // Delete courses.
450 core_course_external::delete_courses(array($course1->id, $course2->id));
451
452 // Check $course 1 and 2 are deleted.
453 $notdeletedcount = $DB->count_records_select('course',
454 'id IN ( ' . $course1->id . ',' . $course2->id . ')');
455 $this->assertEquals(0, $notdeletedcount);
456
457 // Fail when the user is not allow to access the course (enrolled) or is not admin.
458 $this->setGuestUser();
459 $this->setExpectedException('require_login_exception');
460 $createdsubcats = core_course_external::delete_courses(array($course3->id));
461 }
462
463 /**
464 * Test get_courses
465 */
466 public function test_get_courses () {
467 global $DB;
468
469 $this->resetAfterTest(true);
470
a66863c3 471 $generatedcourses = array();
08641906
JM
472 $coursedata['idnumber'] = 'idnumbercourse1';
473 $coursedata['fullname'] = 'Course 1 for PHPunit test';
474 $coursedata['summary'] = 'Course 1 description';
475 $coursedata['summaryformat'] = FORMAT_MOODLE;
476 $course1 = self::getDataGenerator()->create_course($coursedata);
a66863c3 477 $generatedcourses[$course1->id] = $course1;
08641906 478 $course2 = self::getDataGenerator()->create_course();
a66863c3 479 $generatedcourses[$course2->id] = $course2;
08641906 480 $course3 = self::getDataGenerator()->create_course();
a66863c3 481 $generatedcourses[$course3->id] = $course3;
08641906
JM
482
483 // Set the required capabilities by the external function.
484 $context = context_system::instance();
485 $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
486 $this->assignUserCapability('moodle/course:update',
487 context_course::instance($course1->id)->id, $roleid);
488 $this->assignUserCapability('moodle/course:update',
489 context_course::instance($course2->id)->id, $roleid);
490 $this->assignUserCapability('moodle/course:update',
491 context_course::instance($course3->id)->id, $roleid);
492
493 $courses = core_course_external::get_courses(array('ids' =>
494 array($course1->id, $course2->id)));
495
96ace848
JM
496 // We need to execute the return values cleaning process to simulate the web service server.
497 $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
498
08641906
JM
499 // Check we retrieve the good total number of categories.
500 $this->assertEquals(2, count($courses));
501
a66863c3
JM
502 foreach ($courses as $course) {
503 $dbcourse = $generatedcourses[$course['id']];
504 $this->assertEquals($course['idnumber'], $dbcourse->idnumber);
505 $this->assertEquals($course['fullname'], $dbcourse->fullname);
506 $this->assertEquals($course['summary'], $dbcourse->summary);
507 $this->assertEquals($course['summaryformat'], FORMAT_HTML);
508 $this->assertEquals($course['shortname'], $dbcourse->shortname);
509 $this->assertEquals($course['categoryid'], $dbcourse->category);
510 $this->assertEquals($course['format'], $dbcourse->format);
511 $this->assertEquals($course['showgrades'], $dbcourse->showgrades);
512 $this->assertEquals($course['newsitems'], $dbcourse->newsitems);
513 $this->assertEquals($course['startdate'], $dbcourse->startdate);
514 $this->assertEquals($course['numsections'], $dbcourse->numsections);
515 $this->assertEquals($course['maxbytes'], $dbcourse->maxbytes);
516 $this->assertEquals($course['showreports'], $dbcourse->showreports);
517 $this->assertEquals($course['visible'], $dbcourse->visible);
518 $this->assertEquals($course['hiddensections'], $dbcourse->hiddensections);
519 $this->assertEquals($course['groupmode'], $dbcourse->groupmode);
520 $this->assertEquals($course['groupmodeforce'], $dbcourse->groupmodeforce);
521 $this->assertEquals($course['defaultgroupingid'], $dbcourse->defaultgroupingid);
522 $this->assertEquals($course['completionnotify'], $dbcourse->completionnotify);
523 $this->assertEquals($course['lang'], $dbcourse->lang);
524 $this->assertEquals($course['forcetheme'], $dbcourse->theme);
525 $this->assertEquals($course['completionstartonenrol'], $dbcourse->completionstartonenrol);
526 $this->assertEquals($course['enablecompletion'], $dbcourse->enablecompletion);
527 $this->assertEquals($course['completionstartonenrol'], $dbcourse->completionstartonenrol);
528 }
08641906
JM
529
530 // Get all courses in the DB
531 $courses = core_course_external::get_courses(array());
96ace848
JM
532
533 // We need to execute the return values cleaning process to simulate the web service server.
534 $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
535
08641906
JM
536 $this->assertEquals($DB->count_records('course'), count($courses));
537 }
538
539 /**
540 * Test get_course_contents
541 */
542 public function test_get_course_contents() {
543 $this->resetAfterTest(true);
544
545 $course = self::getDataGenerator()->create_course();
546 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
547 $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
548 $forumcontext = context_module::instance($forum->cmid);
549 $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
550 $datacontext = context_module::instance($data->cmid);
551 $datacm = get_coursemodule_from_instance('page', $data->id);
552 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
553 $pagecontext = context_module::instance($page->cmid);
554 $pagecm = get_coursemodule_from_instance('page', $page->id);
555
556 // Set the required capabilities by the external function.
557 $context = context_course::instance($course->id);
558 $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
559 $this->assignUserCapability('moodle/course:update', $context->id, $roleid);
560
561 $courses = core_course_external::get_course_contents($course->id, array());
562
96ace848
JM
563 // We need to execute the return values cleaning process to simulate the web service server.
564 $courses = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $courses);
565
08641906
JM
566 // Check that the course has the 3 created modules
567 $this->assertEquals(3, count($courses[0]['modules']));
568 }
569
570 /**
571 * Test duplicate_course
572 */
573 public function test_duplicate_course() {
574 $this->resetAfterTest(true);
575
576 // Create one course with three modules.
577 $course = self::getDataGenerator()->create_course();
578 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
579 $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
580 $forumcontext = context_module::instance($forum->cmid);
581 $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
582 $datacontext = context_module::instance($data->cmid);
583 $datacm = get_coursemodule_from_instance('page', $data->id);
584 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
585 $pagecontext = context_module::instance($page->cmid);
586 $pagecm = get_coursemodule_from_instance('page', $page->id);
587
588 // Set the required capabilities by the external function.
589 $coursecontext = context_course::instance($course->id);
590 $categorycontext = context_coursecat::instance($course->category);
591 $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id);
592 $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid);
593 $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid);
594 $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid);
595 $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid);
596 // Optional capabilities to copy user data.
597 $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid);
598 $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid);
599
600 $newcourse['fullname'] = 'Course duplicate';
601 $newcourse['shortname'] = 'courseduplicate';
602 $newcourse['categoryid'] = $course->category;
603 $newcourse['visible'] = true;
604 $newcourse['options'][] = array('name' => 'users', 'value' => true);
605
606 $duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'],
607 $newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']);
608
96ace848
JM
609 // We need to execute the return values cleaning process to simulate the web service server.
610 $duplicate = external_api::clean_returnvalue(core_course_external::duplicate_course_returns(), $duplicate);
611
08641906
JM
612 // Check that the course has been duplicated.
613 $this->assertEquals($newcourse['shortname'], $duplicate['shortname']);
614 }
615}