MDL-42084 Unit tests: Remove unnecessary 'clear time limit change' lines
[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';
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
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);
123 $this->setExpectedException('required_capability_exception');
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);
166 $this->setExpectedException('required_capability_exception');
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);
221 $this->assertEquals($category['description'], $generatedcat->description);
222 $this->assertEquals($category['descriptionformat'], FORMAT_HTML);
223 }
2a7a0216
JM
224
225 // Check different params.
226 $categories = core_course_external::get_categories(array(
227 array('key' => 'id', 'value' => $category1->id),
228 array('key' => 'idnumber', 'value' => $category1->idnumber),
229 array('key' => 'visible', 'value' => 1)), 0);
fb695f6e
JM
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
2a7a0216
JM
234 $this->assertEquals(1, count($categories));
235
236 // Retrieve categories from parent.
237 $categories = core_course_external::get_categories(array(
238 array('key' => 'parent', 'value' => $category3->id)), 1);
239 $this->assertEquals(2, count($categories));
240
241 // Retrieve all categories.
242 $categories = core_course_external::get_categories();
fb695f6e
JM
243
244 // We need to execute the return values cleaning process to simulate the web service server.
245 $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);
246
2a7a0216
JM
247 $this->assertEquals($DB->count_records('course_categories'), count($categories));
248
249 // Call without required capability (it will fail cause of the search on idnumber).
250 $this->unassignUserCapability('moodle/category:manage', $context->id, $roleid);
251 $this->setExpectedException('moodle_exception');
252 $categories = core_course_external::get_categories(array(
253 array('key' => 'id', 'value' => $category1->id),
254 array('key' => 'idnumber', 'value' => $category1->idnumber),
255 array('key' => 'visible', 'value' => 1)), 0);
256 }
257
258 /**
259 * Test update_categories
260 */
261 public function test_update_categories() {
262 global $DB;
263
264 $this->resetAfterTest(true);
265
266 // Set the required capabilities by the external function
267 $contextid = context_system::instance()->id;
268 $roleid = $this->assignUserCapability('moodle/category:manage', $contextid);
269
270 // Create base categories.
271 $category1data['idnumber'] = 'idnumbercat1';
272 $category1data['name'] = 'Category 1 for PHPunit test';
273 $category1data['description'] = 'Category 1 description';
274 $category1data['descriptionformat'] = FORMAT_MOODLE;
275 $category1 = self::getDataGenerator()->create_category($category1data);
276 $category2 = self::getDataGenerator()->create_category(
277 array('parent' => $category1->id));
278 $category3 = self::getDataGenerator()->create_category();
279 $category4 = self::getDataGenerator()->create_category(
280 array('parent' => $category3->id));
281 $category5 = self::getDataGenerator()->create_category(
282 array('parent' => $category4->id));
283
284 // We update all category1 attribut.
285 // Then we move cat4 and cat5 parent: cat3 => cat1
286 $categories = array(
287 array('id' => $category1->id,
288 'name' => $category1->name . '_updated',
289 'idnumber' => $category1->idnumber . '_updated',
290 'description' => $category1->description . '_updated',
291 'descriptionformat' => FORMAT_HTML,
292 'theme' => $category1->theme),
293 array('id' => $category4->id, 'parent' => $category1->id));
294
295 core_course_external::update_categories($categories);
296
297 // Check the values were updated.
298 $dbcategories = $DB->get_records_select('course_categories',
299 'id IN (' . $category1->id . ',' . $category2->id . ',' . $category2->id
300 . ',' . $category3->id . ',' . $category4->id . ',' . $category5->id .')');
301 $this->assertEquals($category1->name . '_updated',
302 $dbcategories[$category1->id]->name);
303 $this->assertEquals($category1->idnumber . '_updated',
304 $dbcategories[$category1->id]->idnumber);
305 $this->assertEquals($category1->description . '_updated',
306 $dbcategories[$category1->id]->description);
307 $this->assertEquals(FORMAT_HTML, $dbcategories[$category1->id]->descriptionformat);
308
309 // Check that category4 and category5 have been properly moved.
310 $this->assertEquals('/' . $category1->id . '/' . $category4->id,
311 $dbcategories[$category4->id]->path);
312 $this->assertEquals('/' . $category1->id . '/' . $category4->id . '/' . $category5->id,
313 $dbcategories[$category5->id]->path);
314
315 // Call without required capability.
316 $this->unassignUserCapability('moodle/category:manage', $contextid, $roleid);
317 $this->setExpectedException('required_capability_exception');
318 core_course_external::update_categories($categories);
319 }
320
321 /**
322 * Test create_courses
323 */
324 public function test_create_courses() {
325 global $DB;
326
327 $this->resetAfterTest(true);
328
821676f5
JM
329 // Enable course completion.
330 set_config('enablecompletion', 1);
331
2a7a0216
JM
332 // Set the required capabilities by the external function
333 $contextid = context_system::instance()->id;
334 $roleid = $this->assignUserCapability('moodle/course:create', $contextid);
335 $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
336
337 $category = self::getDataGenerator()->create_category();
338
339 // Create base categories.
340 $course1['fullname'] = 'Test course 1';
341 $course1['shortname'] = 'Testcourse1';
342 $course1['categoryid'] = $category->id;
343 $course2['fullname'] = 'Test course 2';
344 $course2['shortname'] = 'Testcourse2';
345 $course2['categoryid'] = $category->id;
346 $course2['idnumber'] = 'testcourse2idnumber';
347 $course2['summary'] = 'Description for course 2';
348 $course2['summaryformat'] = FORMAT_MOODLE;
349 $course2['format'] = 'weeks';
350 $course2['showgrades'] = 1;
351 $course2['newsitems'] = 3;
4491273b 352 $course2['startdate'] = 1420092000; // 01/01/2015
2a7a0216
JM
353 $course2['numsections'] = 4;
354 $course2['maxbytes'] = 100000;
355 $course2['showreports'] = 1;
356 $course2['visible'] = 0;
357 $course2['hiddensections'] = 0;
358 $course2['groupmode'] = 0;
359 $course2['groupmodeforce'] = 0;
360 $course2['defaultgroupingid'] = 0;
361 $course2['enablecompletion'] = 1;
2a7a0216
JM
362 $course2['completionnotify'] = 1;
363 $course2['lang'] = 'en';
364 $course2['forcetheme'] = 'base';
0e984d98
MG
365 $course3['fullname'] = 'Test course 3';
366 $course3['shortname'] = 'Testcourse3';
367 $course3['categoryid'] = $category->id;
368 $course3['format'] = 'topics';
369 $course3options = array('numsections' => 8,
370 'hiddensections' => 1,
371 'coursedisplay' => 1);
8d8d4da4 372 $course3['courseformatoptions'] = array();
0e984d98 373 foreach ($course3options as $key => $value) {
8d8d4da4 374 $course3['courseformatoptions'][] = array('name' => $key, 'value' => $value);
0e984d98 375 }
821676f5 376 $courses = array($course1, $course2, $course3);
2a7a0216
JM
377
378 $createdcourses = core_course_external::create_courses($courses);
379
fb695f6e
JM
380 // We need to execute the return values cleaning process to simulate the web service server.
381 $createdcourses = external_api::clean_returnvalue(core_course_external::create_courses_returns(), $createdcourses);
382
2a7a0216 383 // Check that right number of courses were created.
821676f5 384 $this->assertEquals(3, count($createdcourses));
2a7a0216
JM
385
386 // Check that the courses were correctly created.
387 foreach ($createdcourses as $createdcourse) {
850acb35 388 $courseinfo = course_get_format($createdcourse['id'])->get_course();
2a7a0216
JM
389
390 if ($createdcourse['shortname'] == $course2['shortname']) {
850acb35
MG
391 $this->assertEquals($courseinfo->fullname, $course2['fullname']);
392 $this->assertEquals($courseinfo->shortname, $course2['shortname']);
393 $this->assertEquals($courseinfo->category, $course2['categoryid']);
394 $this->assertEquals($courseinfo->idnumber, $course2['idnumber']);
395 $this->assertEquals($courseinfo->summary, $course2['summary']);
396 $this->assertEquals($courseinfo->summaryformat, $course2['summaryformat']);
397 $this->assertEquals($courseinfo->format, $course2['format']);
398 $this->assertEquals($courseinfo->showgrades, $course2['showgrades']);
399 $this->assertEquals($courseinfo->newsitems, $course2['newsitems']);
400 $this->assertEquals($courseinfo->startdate, $course2['startdate']);
401 $this->assertEquals($courseinfo->numsections, $course2['numsections']);
402 $this->assertEquals($courseinfo->maxbytes, $course2['maxbytes']);
403 $this->assertEquals($courseinfo->showreports, $course2['showreports']);
404 $this->assertEquals($courseinfo->visible, $course2['visible']);
405 $this->assertEquals($courseinfo->hiddensections, $course2['hiddensections']);
406 $this->assertEquals($courseinfo->groupmode, $course2['groupmode']);
407 $this->assertEquals($courseinfo->groupmodeforce, $course2['groupmodeforce']);
408 $this->assertEquals($courseinfo->defaultgroupingid, $course2['defaultgroupingid']);
409 $this->assertEquals($courseinfo->completionnotify, $course2['completionnotify']);
410 $this->assertEquals($courseinfo->lang, $course2['lang']);
2a7a0216
JM
411
412 if (!empty($CFG->allowcoursethemes)) {
850acb35 413 $this->assertEquals($courseinfo->theme, $course2['forcetheme']);
2a7a0216
JM
414 }
415
821676f5
JM
416 // We enabled completion at the beginning of the test.
417 $this->assertEquals($courseinfo->enablecompletion, $course2['enablecompletion']);
2a7a0216
JM
418
419 } else if ($createdcourse['shortname'] == $course1['shortname']) {
420 $courseconfig = get_config('moodlecourse');
850acb35
MG
421 $this->assertEquals($courseinfo->fullname, $course1['fullname']);
422 $this->assertEquals($courseinfo->shortname, $course1['shortname']);
423 $this->assertEquals($courseinfo->category, $course1['categoryid']);
424 $this->assertEquals($courseinfo->summaryformat, FORMAT_HTML);
425 $this->assertEquals($courseinfo->format, $courseconfig->format);
426 $this->assertEquals($courseinfo->showgrades, $courseconfig->showgrades);
427 $this->assertEquals($courseinfo->newsitems, $courseconfig->newsitems);
428 $this->assertEquals($courseinfo->maxbytes, $courseconfig->maxbytes);
429 $this->assertEquals($courseinfo->showreports, $courseconfig->showreports);
430 $this->assertEquals($courseinfo->groupmode, $courseconfig->groupmode);
431 $this->assertEquals($courseinfo->groupmodeforce, $courseconfig->groupmodeforce);
432 $this->assertEquals($courseinfo->defaultgroupingid, 0);
0e984d98 433 } else if ($createdcourse['shortname'] == $course3['shortname']) {
850acb35
MG
434 $this->assertEquals($courseinfo->fullname, $course3['fullname']);
435 $this->assertEquals($courseinfo->shortname, $course3['shortname']);
436 $this->assertEquals($courseinfo->category, $course3['categoryid']);
437 $this->assertEquals($courseinfo->format, $course3['format']);
438 $this->assertEquals($courseinfo->hiddensections, $course3options['hiddensections']);
439 $this->assertEquals($courseinfo->numsections, $course3options['numsections']);
440 $this->assertEquals($courseinfo->coursedisplay, $course3options['coursedisplay']);
2a7a0216
JM
441 } else {
442 throw moodle_exception('Unexpected shortname');
443 }
444 }
445
446 // Call without required capability
447 $this->unassignUserCapability('moodle/course:create', $contextid, $roleid);
448 $this->setExpectedException('required_capability_exception');
449 $createdsubcats = core_course_external::create_courses($courses);
450 }
451
452 /**
453 * Test delete_courses
454 */
455 public function test_delete_courses() {
456 global $DB, $USER;
457
458 $this->resetAfterTest(true);
459
460 // Admin can delete a course.
461 $this->setAdminUser();
462 // Validate_context() will fail as the email is not set by $this->setAdminUser().
463 $USER->email = 'emailtopass@contextvalidation.me';
464
465 $course1 = self::getDataGenerator()->create_course();
466 $course2 = self::getDataGenerator()->create_course();
467 $course3 = self::getDataGenerator()->create_course();
468
469 // Delete courses.
470 core_course_external::delete_courses(array($course1->id, $course2->id));
471
472 // Check $course 1 and 2 are deleted.
473 $notdeletedcount = $DB->count_records_select('course',
474 'id IN ( ' . $course1->id . ',' . $course2->id . ')');
475 $this->assertEquals(0, $notdeletedcount);
476
477 // Fail when the user is not allow to access the course (enrolled) or is not admin.
478 $this->setGuestUser();
479 $this->setExpectedException('require_login_exception');
480 $createdsubcats = core_course_external::delete_courses(array($course3->id));
481 }
482
483 /**
484 * Test get_courses
485 */
486 public function test_get_courses () {
487 global $DB;
488
489 $this->resetAfterTest(true);
490
7d6c58bc 491 $generatedcourses = array();
2a7a0216
JM
492 $coursedata['idnumber'] = 'idnumbercourse1';
493 $coursedata['fullname'] = 'Course 1 for PHPunit test';
494 $coursedata['summary'] = 'Course 1 description';
495 $coursedata['summaryformat'] = FORMAT_MOODLE;
496 $course1 = self::getDataGenerator()->create_course($coursedata);
7d6c58bc 497 $generatedcourses[$course1->id] = $course1;
2a7a0216 498 $course2 = self::getDataGenerator()->create_course();
7d6c58bc 499 $generatedcourses[$course2->id] = $course2;
0e984d98 500 $course3 = self::getDataGenerator()->create_course(array('format' => 'topics'));
7d6c58bc 501 $generatedcourses[$course3->id] = $course3;
2a7a0216
JM
502
503 // Set the required capabilities by the external function.
504 $context = context_system::instance();
505 $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
506 $this->assignUserCapability('moodle/course:update',
507 context_course::instance($course1->id)->id, $roleid);
508 $this->assignUserCapability('moodle/course:update',
509 context_course::instance($course2->id)->id, $roleid);
510 $this->assignUserCapability('moodle/course:update',
511 context_course::instance($course3->id)->id, $roleid);
512
513 $courses = core_course_external::get_courses(array('ids' =>
514 array($course1->id, $course2->id)));
515
fb695f6e
JM
516 // We need to execute the return values cleaning process to simulate the web service server.
517 $courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);
518
2a7a0216
JM
519 // Check we retrieve the good total number of categories.
520 $this->assertEquals(2, count($courses));
521
7d6c58bc
JM
522 foreach ($courses as $course) {
523 $dbcourse = $generatedcourses[$course['id']];
524 $this->assertEquals($course['idnumber'], $dbcourse->idnumber);
525 $this->assertEquals($course['fullname'], $dbcourse->fullname);
526 $this->assertEquals($course['summary'], $dbcourse->summary);
527 $this->assertEquals($course['summaryformat'], FORMAT_HTML);
528 $this->assertEquals($course['shortname'], $dbcourse->shortname);
529 $this->assertEquals($course['categoryid'], $dbcourse->category);
530 $this->assertEquals($course['format'], $dbcourse->format);
531 $this->assertEquals($course['showgrades'], $dbcourse->showgrades);
532 $this->assertEquals($course['newsitems'], $dbcourse->newsitems);
533 $this->assertEquals($course['startdate'], $dbcourse->startdate);
534 $this->assertEquals($course['numsections'], $dbcourse->numsections);
535 $this->assertEquals($course['maxbytes'], $dbcourse->maxbytes);
536 $this->assertEquals($course['showreports'], $dbcourse->showreports);
537 $this->assertEquals($course['visible'], $dbcourse->visible);
538 $this->assertEquals($course['hiddensections'], $dbcourse->hiddensections);
539 $this->assertEquals($course['groupmode'], $dbcourse->groupmode);
540 $this->assertEquals($course['groupmodeforce'], $dbcourse->groupmodeforce);
541 $this->assertEquals($course['defaultgroupingid'], $dbcourse->defaultgroupingid);
542 $this->assertEquals($course['completionnotify'], $dbcourse->completionnotify);
543 $this->assertEquals($course['lang'], $dbcourse->lang);
544 $this->assertEquals($course['forcetheme'], $dbcourse->theme);
7d6c58bc 545 $this->assertEquals($course['enablecompletion'], $dbcourse->enablecompletion);
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();
487bc1b6
JM
571 $forumdescription = 'This is the forum description';
572 $forum = $this->getDataGenerator()->create_module('forum',
573 array('course'=>$course->id, 'intro' => $forumdescription),
574 array('showdescription' => true));
2a7a0216 575 $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
2a7a0216 576 $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
2a7a0216
JM
577 $datacm = get_coursemodule_from_instance('page', $data->id);
578 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
2a7a0216 579 $pagecm = get_coursemodule_from_instance('page', $page->id);
487bc1b6
JM
580 $labeldescription = 'This is a very long label to test if more than 50 characters are returned.
581 So bla bla bla bla <b>bold bold bold</b> bla bla bla bla.';
582 $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id,
583 'intro' => $labeldescription));
584 $labelcm = get_coursemodule_from_instance('label', $label->id);
2a7a0216
JM
585
586 // Set the required capabilities by the external function.
587 $context = context_course::instance($course->id);
588 $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
589 $this->assignUserCapability('moodle/course:update', $context->id, $roleid);
590
487bc1b6 591 $sections = core_course_external::get_course_contents($course->id, array());
2a7a0216 592
fb695f6e 593 // We need to execute the return values cleaning process to simulate the web service server.
487bc1b6
JM
594 $sections = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $sections);
595
596 // Check that forum and label descriptions are correctly returned.
597 $firstsection = array_pop($sections);
598 $modinfo = get_fast_modinfo($course);
599 $testexecuted = 0;
600 foreach($firstsection['modules'] as $module) {
601 if ($module['id'] == $forumcm->id and $module['modname'] == 'forum') {
602 $cm = $modinfo->cms[$forumcm->id];
603 $formattedtext = format_text($cm->get_content(), FORMAT_HTML,
604 array('noclean' => true, 'para' => false, 'filter' => false));
605 $this->assertEquals($formattedtext, $module['description']);
606 $testexecuted = $testexecuted + 1;
607 } else if ($module['id'] == $labelcm->id and $module['modname'] == 'label') {
608 $cm = $modinfo->cms[$labelcm->id];
609 $formattedtext = format_text($cm->get_content(), FORMAT_HTML,
610 array('noclean' => true, 'para' => false, 'filter' => false));
611 $this->assertEquals($formattedtext, $module['description']);
612 $testexecuted = $testexecuted + 1;
613 }
614 }
615 $this->assertEquals(2, $testexecuted);
fb695f6e 616
487bc1b6
JM
617 // Check that the only return section has the 4 created modules
618 $this->assertEquals(4, count($firstsection['modules']));
2a7a0216
JM
619 }
620
621 /**
622 * Test duplicate_course
623 */
624 public function test_duplicate_course() {
625 $this->resetAfterTest(true);
626
627 // Create one course with three modules.
628 $course = self::getDataGenerator()->create_course();
629 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
630 $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
631 $forumcontext = context_module::instance($forum->cmid);
632 $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id));
633 $datacontext = context_module::instance($data->cmid);
634 $datacm = get_coursemodule_from_instance('page', $data->id);
635 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
636 $pagecontext = context_module::instance($page->cmid);
637 $pagecm = get_coursemodule_from_instance('page', $page->id);
638
639 // Set the required capabilities by the external function.
640 $coursecontext = context_course::instance($course->id);
641 $categorycontext = context_coursecat::instance($course->category);
642 $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id);
643 $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid);
644 $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid);
645 $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid);
646 $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid);
647 // Optional capabilities to copy user data.
648 $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid);
649 $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid);
650
651 $newcourse['fullname'] = 'Course duplicate';
652 $newcourse['shortname'] = 'courseduplicate';
653 $newcourse['categoryid'] = $course->category;
654 $newcourse['visible'] = true;
655 $newcourse['options'][] = array('name' => 'users', 'value' => true);
656
657 $duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'],
658 $newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']);
659
fb695f6e
JM
660 // We need to execute the return values cleaning process to simulate the web service server.
661 $duplicate = external_api::clean_returnvalue(core_course_external::duplicate_course_returns(), $duplicate);
662
2a7a0216
JM
663 // Check that the course has been duplicated.
664 $this->assertEquals($newcourse['shortname'], $duplicate['shortname']);
665 }
791723c3
RT
666
667 /**
668 * Test update_courses
669 */
670 public function test_update_courses() {
a182f88f
EL
671 global $DB, $CFG, $USER, $COURSE;
672
673 // Get current $COURSE to be able to restore it later (defaults to $SITE). We need this
674 // trick because we are both updating and getting (for testing) course information
675 // in the same request and core_course_external::update_courses()
676 // is overwriting $COURSE all over the time with OLD values, so later
677 // use of get_course() fetches those OLD values instead of the updated ones.
678 // See MDL-39723 for more info.
679 $origcourse = clone($COURSE);
791723c3
RT
680
681 $this->resetAfterTest(true);
682
683 // Set the required capabilities by the external function.
684 $contextid = context_system::instance()->id;
685 $roleid = $this->assignUserCapability('moodle/course:update', $contextid);
686 $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
687 $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
688 $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
689 $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
690 $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
691 $this->assignUserCapability('moodle/course:visibility', $contextid, $roleid);
692 $this->assignUserCapability('moodle/course:viewhiddencourses', $contextid, $roleid);
693
694 // Create category and course.
695 $category1 = self::getDataGenerator()->create_category();
696 $category2 = self::getDataGenerator()->create_category();
697 $originalcourse1 = self::getDataGenerator()->create_course();
698 self::getDataGenerator()->enrol_user($USER->id, $originalcourse1->id, $roleid);
699 $originalcourse2 = self::getDataGenerator()->create_course();
700 self::getDataGenerator()->enrol_user($USER->id, $originalcourse2->id, $roleid);
701
702 // Course values to be updated.
703 $course1['id'] = $originalcourse1->id;
704 $course1['fullname'] = 'Updated test course 1';
705 $course1['shortname'] = 'Udestedtestcourse1';
706 $course1['categoryid'] = $category1->id;
707 $course2['id'] = $originalcourse2->id;
708 $course2['fullname'] = 'Updated test course 2';
709 $course2['shortname'] = 'Updestedtestcourse2';
710 $course2['categoryid'] = $category2->id;
711 $course2['idnumber'] = 'Updatedidnumber2';
712 $course2['summary'] = 'Updaated description for course 2';
713 $course2['summaryformat'] = FORMAT_HTML;
714 $course2['format'] = 'topics';
715 $course2['showgrades'] = 1;
716 $course2['newsitems'] = 3;
717 $course2['startdate'] = 1420092000; // 01/01/2015.
718 $course2['numsections'] = 4;
719 $course2['maxbytes'] = 100000;
720 $course2['showreports'] = 1;
721 $course2['visible'] = 0;
722 $course2['hiddensections'] = 0;
723 $course2['groupmode'] = 0;
724 $course2['groupmodeforce'] = 0;
725 $course2['defaultgroupingid'] = 0;
726 $course2['enablecompletion'] = 1;
727 $course2['lang'] = 'en';
728 $course2['forcetheme'] = 'base';
729 $courses = array($course1, $course2);
730
731 $updatedcoursewarnings = core_course_external::update_courses($courses);
a182f88f 732 $COURSE = $origcourse; // Restore $COURSE. Instead of using the OLD one set by the previous line.
791723c3
RT
733
734 // Check that right number of courses were created.
735 $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
736
737 // Check that the courses were correctly created.
738 foreach ($courses as $course) {
739 $courseinfo = course_get_format($course['id'])->get_course();
740 if ($course['id'] == $course2['id']) {
741 $this->assertEquals($course2['fullname'], $courseinfo->fullname);
742 $this->assertEquals($course2['shortname'], $courseinfo->shortname);
743 $this->assertEquals($course2['categoryid'], $courseinfo->category);
744 $this->assertEquals($course2['idnumber'], $courseinfo->idnumber);
745 $this->assertEquals($course2['summary'], $courseinfo->summary);
746 $this->assertEquals($course2['summaryformat'], $courseinfo->summaryformat);
747 $this->assertEquals($course2['format'], $courseinfo->format);
748 $this->assertEquals($course2['showgrades'], $courseinfo->showgrades);
749 $this->assertEquals($course2['newsitems'], $courseinfo->newsitems);
750 $this->assertEquals($course2['startdate'], $courseinfo->startdate);
751 $this->assertEquals($course2['numsections'], $courseinfo->numsections);
752 $this->assertEquals($course2['maxbytes'], $courseinfo->maxbytes);
753 $this->assertEquals($course2['showreports'], $courseinfo->showreports);
754 $this->assertEquals($course2['visible'], $courseinfo->visible);
755 $this->assertEquals($course2['hiddensections'], $courseinfo->hiddensections);
756 $this->assertEquals($course2['groupmode'], $courseinfo->groupmode);
757 $this->assertEquals($course2['groupmodeforce'], $courseinfo->groupmodeforce);
758 $this->assertEquals($course2['defaultgroupingid'], $courseinfo->defaultgroupingid);
759 $this->assertEquals($course2['lang'], $courseinfo->lang);
760
761 if (!empty($CFG->allowcoursethemes)) {
762 $this->assertEquals($course2['forcetheme'], $courseinfo->theme);
763 }
764
765 if (completion_info::is_enabled_for_site()) {
766 $this->assertEquals($course2['enabledcompletion'], $courseinfo->enablecompletion);
791723c3
RT
767 }
768 } else if ($course['id'] == $course1['id']) {
769 $this->assertEquals($course1['fullname'], $courseinfo->fullname);
770 $this->assertEquals($course1['shortname'], $courseinfo->shortname);
771 $this->assertEquals($course1['categoryid'], $courseinfo->category);
772 $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
773 $this->assertEquals('topics', $courseinfo->format);
774 $this->assertEquals(5, $courseinfo->numsections);
775 $this->assertEquals(0, $courseinfo->newsitems);
776 $this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
777 } else {
778 throw moodle_exception('Unexpected shortname');
779 }
780 }
781
782 $courses = array($course1);
783 // Try update course without update capability.
784 $user = self::getDataGenerator()->create_user();
785 $this->setUser($user);
786 $this->unassignUserCapability('moodle/course:update', $contextid, $roleid);
787 self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
788 $updatedcoursewarnings = core_course_external::update_courses($courses);
789 $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
790
791 // Try update course category without capability.
792 $this->assignUserCapability('moodle/course:update', $contextid, $roleid);
793 $this->unassignUserCapability('moodle/course:changecategory', $contextid, $roleid);
794 $user = self::getDataGenerator()->create_user();
795 $this->setUser($user);
796 self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
797 $course1['categoryid'] = $category2->id;
798 $courses = array($course1);
799 $updatedcoursewarnings = core_course_external::update_courses($courses);
800 $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
801
802 // Try update course fullname without capability.
803 $this->assignUserCapability('moodle/course:changecategory', $contextid, $roleid);
804 $this->unassignUserCapability('moodle/course:changefullname', $contextid, $roleid);
805 $user = self::getDataGenerator()->create_user();
806 $this->setUser($user);
807 self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
808 $updatedcoursewarnings = core_course_external::update_courses($courses);
809 $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
810 $course1['fullname'] = 'Testing fullname without permission';
811 $courses = array($course1);
812 $updatedcoursewarnings = core_course_external::update_courses($courses);
813 $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
814
815 // Try update course shortname without capability.
816 $this->assignUserCapability('moodle/course:changefullname', $contextid, $roleid);
817 $this->unassignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
818 $user = self::getDataGenerator()->create_user();
819 $this->setUser($user);
820 self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
821 $updatedcoursewarnings = core_course_external::update_courses($courses);
822 $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
823 $course1['shortname'] = 'Testing shortname without permission';
824 $courses = array($course1);
825 $updatedcoursewarnings = core_course_external::update_courses($courses);
826 $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
827
828 // Try update course idnumber without capability.
829 $this->assignUserCapability('moodle/course:changeshortname', $contextid, $roleid);
830 $this->unassignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
831 $user = self::getDataGenerator()->create_user();
832 $this->setUser($user);
833 self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
834 $updatedcoursewarnings = core_course_external::update_courses($courses);
835 $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
836 $course1['idnumber'] = 'NEWIDNUMBER';
837 $courses = array($course1);
838 $updatedcoursewarnings = core_course_external::update_courses($courses);
839 $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
840
841 // Try update course summary without capability.
842 $this->assignUserCapability('moodle/course:changeidnumber', $contextid, $roleid);
843 $this->unassignUserCapability('moodle/course:changesummary', $contextid, $roleid);
844 $user = self::getDataGenerator()->create_user();
845 $this->setUser($user);
846 self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
847 $updatedcoursewarnings = core_course_external::update_courses($courses);
848 $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
849 $course1['summary'] = 'New summary';
850 $courses = array($course1);
851 $updatedcoursewarnings = core_course_external::update_courses($courses);
852 $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
853
854 // Try update course with invalid summary format.
855 $this->assignUserCapability('moodle/course:changesummary', $contextid, $roleid);
856 $user = self::getDataGenerator()->create_user();
857 $this->setUser($user);
858 self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
859 $updatedcoursewarnings = core_course_external::update_courses($courses);
860 $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
861 $course1['summaryformat'] = 10;
862 $courses = array($course1);
863 $updatedcoursewarnings = core_course_external::update_courses($courses);
864 $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
865
866 // Try update course visibility without capability.
867 $this->unassignUserCapability('moodle/course:visibility', $contextid, $roleid);
868 $user = self::getDataGenerator()->create_user();
869 $this->setUser($user);
870 self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
871 $course1['summaryformat'] = FORMAT_MOODLE;
872 $courses = array($course1);
873 $updatedcoursewarnings = core_course_external::update_courses($courses);
874 $this->assertEquals(0, count($updatedcoursewarnings['warnings']));
875 $course1['visible'] = 0;
876 $courses = array($course1);
877 $updatedcoursewarnings = core_course_external::update_courses($courses);
878 $this->assertEquals(1, count($updatedcoursewarnings['warnings']));
879 }
05fc7ccc 880
79949c1b
MN
881 /**
882 * Test delete course_module.
883 */
884 public function test_delete_modules() {
885 global $DB;
886
887 // Ensure we reset the data after this test.
888 $this->resetAfterTest(true);
889
890 // Create a user.
891 $user = self::getDataGenerator()->create_user();
892
893 // Set the tests to run as the user.
894 self::setUser($user);
895
896 // Create a course to add the modules.
897 $course = self::getDataGenerator()->create_course();
898
899 // Create two test modules.
900 $record = new stdClass();
901 $record->course = $course->id;
902 $module1 = self::getDataGenerator()->create_module('forum', $record);
903 $module2 = self::getDataGenerator()->create_module('assignment', $record);
904
905 // Check the forum was correctly created.
906 $this->assertEquals(1, $DB->count_records('forum', array('id' => $module1->id)));
907
908 // Check the assignment was correctly created.
909 $this->assertEquals(1, $DB->count_records('assignment', array('id' => $module2->id)));
910
911 // Check data exists in the course modules table.
912 $this->assertEquals(2, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
913 array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
914
915 // Enrol the user in the course.
916 $enrol = enrol_get_plugin('manual');
917 $enrolinstances = enrol_get_instances($course->id, true);
918 foreach ($enrolinstances as $courseenrolinstance) {
919 if ($courseenrolinstance->enrol == "manual") {
920 $instance = $courseenrolinstance;
921 break;
922 }
923 }
924 $enrol->enrol_user($instance, $user->id);
925
926 // Assign capabilities to delete module 1.
927 $modcontext = context_module::instance($module1->cmid);
928 $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id);
929
930 // Assign capabilities to delete module 2.
931 $modcontext = context_module::instance($module2->cmid);
932 $newrole = create_role('Role 2', 'role2', 'Role 2 description');
933 $this->assignUserCapability('moodle/course:manageactivities', $modcontext->id, $newrole);
934
935 // Deleting these module instances.
936 core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
937
938 // Check the forum was deleted.
939 $this->assertEquals(0, $DB->count_records('forum', array('id' => $module1->id)));
940
941 // Check the assignment was deleted.
942 $this->assertEquals(0, $DB->count_records('assignment', array('id' => $module2->id)));
943
944 // Check we retrieve no data in the course modules table.
945 $this->assertEquals(0, $DB->count_records_select('course_modules', 'id = :module1 OR id = :module2',
946 array('module1' => $module1->cmid, 'module2' => $module2->cmid)));
947
948 // Call with non-existent course module id and ensure exception thrown.
949 try {
950 core_course_external::delete_modules(array('1337'));
951 $this->fail('Exception expected due to missing course module.');
952 } catch (dml_missing_record_exception $e) {
953 $this->assertEquals('invalidrecord', $e->errorcode);
954 }
955
956 // Create two modules.
957 $module1 = self::getDataGenerator()->create_module('forum', $record);
958 $module2 = self::getDataGenerator()->create_module('assignment', $record);
959
960 // Since these modules were recreated the user will not have capabilities
961 // to delete them, ensure exception is thrown if they try.
962 try {
963 core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
964 $this->fail('Exception expected due to missing capability.');
965 } catch (moodle_exception $e) {
966 $this->assertEquals('nopermissions', $e->errorcode);
967 }
968
969 // Unenrol user from the course.
970 $enrol->unenrol_user($instance, $user->id);
971
972 // Try and delete modules from the course the user was unenrolled in, make sure exception thrown.
973 try {
974 core_course_external::delete_modules(array($module1->cmid, $module2->cmid));
975 $this->fail('Exception expected due to being unenrolled from the course.');
976 } catch (moodle_exception $e) {
977 $this->assertEquals('requireloginerror', $e->errorcode);
978 }
979 }
fce10644
DP
980
981 /**
982 * Test import_course into an empty course
983 */
984 public function test_import_course_empty() {
985 global $USER;
986
987 $this->resetAfterTest(true);
988
989 $course1 = self::getDataGenerator()->create_course();
990 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course1->id, 'name' => 'Forum test'));
991 $page = $this->getDataGenerator()->create_module('page', array('course' => $course1->id, 'name' => 'Page test'));
992
993 $course2 = self::getDataGenerator()->create_course();
994
995 $course1cms = get_fast_modinfo($course1->id)->get_cms();
996 $course2cms = get_fast_modinfo($course2->id)->get_cms();
997
998 // Verify the state of the courses before we do the import.
999 $this->assertCount(2, $course1cms);
1000 $this->assertEmpty($course2cms);
1001
1002 // Setup the user to run the operation (ugly hack because validate_context() will
1003 // fail as the email is not set by $this->setAdminUser()).
1004 $this->setAdminUser();
1005 $USER->email = 'emailtopass@contextvalidation.me';
1006
1007 // Import from course1 to course2.
1008 core_course_external::import_course($course1->id, $course2->id, 0);
1009
1010 // Verify that now we have two modules in both courses.
1011 $course1cms = get_fast_modinfo($course1->id)->get_cms();
1012 $course2cms = get_fast_modinfo($course2->id)->get_cms();
1013 $this->assertCount(2, $course1cms);
1014 $this->assertCount(2, $course2cms);
1015
1016 // Verify that the names transfered across correctly.
1017 foreach ($course2cms as $cm) {
1018 if ($cm->modname === 'page') {
1019 $this->assertEquals($cm->name, $page->name);
1020 } else if ($cm->modname === 'forum') {
1021 $this->assertEquals($cm->name, $forum->name);
1022 } else {
1023 $this->fail('Unknown CM found.');
1024 }
1025 }
fce10644
DP
1026 }
1027
1028 /**
1029 * Test import_course into an filled course
1030 */
1031 public function test_import_course_filled() {
1032 global $USER;
1033
1034 $this->resetAfterTest(true);
1035
1036 // Add forum and page to course1.
1037 $course1 = self::getDataGenerator()->create_course();
1038 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1039 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
1040
1041 // Add quiz to course 2.
1042 $course2 = self::getDataGenerator()->create_course();
1043 $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
1044
1045 $course1cms = get_fast_modinfo($course1->id)->get_cms();
1046 $course2cms = get_fast_modinfo($course2->id)->get_cms();
1047
1048 // Verify the state of the courses before we do the import.
1049 $this->assertCount(2, $course1cms);
1050 $this->assertCount(1, $course2cms);
1051
1052 // Setup the user to run the operation (ugly hack because validate_context() will
1053 // fail as the email is not set by $this->setAdminUser()).
1054 $this->setAdminUser();
1055 $USER->email = 'emailtopass@contextvalidation.me';
1056
1057 // Import from course1 to course2 without deleting content.
1058 core_course_external::import_course($course1->id, $course2->id, 0);
1059
1060 $course2cms = get_fast_modinfo($course2->id)->get_cms();
1061
1062 // Verify that now we have three modules in course2.
1063 $this->assertCount(3, $course2cms);
1064
1065 // Verify that the names transfered across correctly.
1066 foreach ($course2cms as $cm) {
1067 if ($cm->modname === 'page') {
1068 $this->assertEquals($cm->name, $page->name);
1069 } else if ($cm->modname === 'forum') {
1070 $this->assertEquals($cm->name, $forum->name);
1071 } else if ($cm->modname === 'quiz') {
1072 $this->assertEquals($cm->name, $quiz->name);
1073 } else {
1074 $this->fail('Unknown CM found.');
1075 }
1076 }
fce10644
DP
1077 }
1078
1079 /**
1080 * Test import_course with only blocks set to backup
1081 */
1082 public function test_import_course_blocksonly() {
1083 global $USER, $DB;
1084
1085 $this->resetAfterTest(true);
1086
1087 // Add forum and page to course1.
1088 $course1 = self::getDataGenerator()->create_course();
1089 $course1ctx = context_course::instance($course1->id);
1090 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1091 $block = $this->getDataGenerator()->create_block('online_users', array('parentcontextid' => $course1ctx->id));
1092
1093 $course2 = self::getDataGenerator()->create_course();
1094 $course2ctx = context_course::instance($course2->id);
1095 $initialblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
1096 $initialcmcount = count(get_fast_modinfo($course2->id)->get_cms());
1097
1098 // Setup the user to run the operation (ugly hack because validate_context() will
1099 // fail as the email is not set by $this->setAdminUser()).
1100 $this->setAdminUser();
1101 $USER->email = 'emailtopass@contextvalidation.me';
1102
1103 // Import from course1 to course2 without deleting content, but excluding
1104 // activities.
1105 $options = array(
1106 array('name' => 'activities', 'value' => 0),
1107 array('name' => 'blocks', 'value' => 1),
1108 array('name' => 'filters', 'value' => 0),
1109 );
1110
1111 core_course_external::import_course($course1->id, $course2->id, 0, $options);
1112
1113 $newcmcount = count(get_fast_modinfo($course2->id)->get_cms());
1114 $newblockcount = $DB->count_records('block_instances', array('parentcontextid' => $course2ctx->id));
1115 // Check that course modules haven't changed, but that blocks have.
1116 $this->assertEquals($initialcmcount, $newcmcount);
1117 $this->assertEquals(($initialblockcount + 1), $newblockcount);
fce10644
DP
1118 }
1119
1120 /**
1121 * Test import_course into an filled course, deleting content.
1122 */
1123 public function test_import_course_deletecontent() {
1124 global $USER;
1125 $this->resetAfterTest(true);
1126
1127 // Add forum and page to course1.
1128 $course1 = self::getDataGenerator()->create_course();
1129 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course1->id, 'name' => 'Forum test'));
1130 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course1->id, 'name' => 'Page test'));
1131
1132 // Add quiz to course 2.
1133 $course2 = self::getDataGenerator()->create_course();
1134 $quiz = $this->getDataGenerator()->create_module('quiz', array('course'=>$course2->id, 'name' => 'Page test'));
1135
1136 $course1cms = get_fast_modinfo($course1->id)->get_cms();
1137 $course2cms = get_fast_modinfo($course2->id)->get_cms();
1138
1139 // Verify the state of the courses before we do the import.
1140 $this->assertCount(2, $course1cms);
1141 $this->assertCount(1, $course2cms);
1142
1143 // Setup the user to run the operation (ugly hack because validate_context() will
1144 // fail as the email is not set by $this->setAdminUser()).
1145 $this->setAdminUser();
1146 $USER->email = 'emailtopass@contextvalidation.me';
1147
1148 // Import from course1 to course2, deleting content.
1149 core_course_external::import_course($course1->id, $course2->id, 1);
1150
1151 $course2cms = get_fast_modinfo($course2->id)->get_cms();
1152
1153 // Verify that now we have two modules in course2.
1154 $this->assertCount(2, $course2cms);
1155
1156 // Verify that the course only contains the imported modules.
1157 foreach ($course2cms as $cm) {
1158 if ($cm->modname === 'page') {
1159 $this->assertEquals($cm->name, $page->name);
1160 } else if ($cm->modname === 'forum') {
1161 $this->assertEquals($cm->name, $forum->name);
1162 } else {
1163 $this->fail('Unknown CM found: '.$cm->name);
1164 }
1165 }
fce10644
DP
1166 }
1167
1168 /**
1169 * Ensure import_course handles incorrect deletecontent option correctly.
1170 */
1171 public function test_import_course_invalid_deletecontent_option() {
1172 $this->resetAfterTest(true);
1173
1174 $course1 = self::getDataGenerator()->create_course();
1175 $course2 = self::getDataGenerator()->create_course();
1176
1177 $this->setExpectedException('moodle_exception', get_string('invalidextparam', 'webservice', -1));
1178 // Import from course1 to course2, with invalid option
1179 core_course_external::import_course($course1->id, $course2->id, -1);;
1180 }
2a7a0216 1181}