Skip to content

Commit

Permalink
Merge branch 'MDL-45131-master' of git://github.com/jleyva/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao committed Apr 16, 2015
2 parents 960c0ec + 80cb20a commit 89a6290
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 6 deletions.
2 changes: 2 additions & 0 deletions calendar/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ public function test_get_calendar_events() {

$paramevents = array ('courseids' => array($course->id), 'groupids' => array($group->id));
$events = core_calendar_external::get_calendar_events($paramevents, $options);
$events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
$this->assertEquals(4, count($events['events'])); // site, user, group, one course event.
$this->assertEquals(0, count($events['warnings']));

Expand Down Expand Up @@ -390,6 +391,7 @@ public function test_core_create_calendar_events() {
array('name' => 'user')
);
$eventsret = core_calendar_external::create_calendar_events($events);
$eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);

// Check to see if things were created properly.
$aftercount = $DB->count_records('event');
Expand Down
7 changes: 7 additions & 0 deletions cohort/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function test_create_cohorts() {
// Call the external function.
$this->setCurrentTimeStart();
$createdcohorts = core_cohort_external::create_cohorts(array($cohort1, $cohort2));
$createdcohorts = external_api::clean_returnvalue(core_cohort_external::create_cohorts_returns(), $createdcohorts);

// Check we retrieve the good total number of created cohorts + no error on capability.
$this->assertEquals(2, count($createdcohorts));
Expand Down Expand Up @@ -155,6 +156,7 @@ public function test_get_cohorts() {
// Call the external function.
$returnedcohorts = core_cohort_external::get_cohorts(array(
$cohort1->id, $cohort2->id));
$returnedcohorts = external_api::clean_returnvalue(core_cohort_external::get_cohorts_returns(), $returnedcohorts);

// Check we retrieve the good total number of enrolled cohorts + no error on capability.
$this->assertEquals(2, count($returnedcohorts));
Expand All @@ -173,6 +175,7 @@ public function test_get_cohorts() {
// Call the external function.
$returnedcohorts = core_cohort_external::get_cohorts(array(
$cohort1->id, $cohort2->id));
$returnedcohorts = external_api::clean_returnvalue(core_cohort_external::get_cohorts_returns(), $returnedcohorts);

// Check we retrieve the good total number of enrolled cohorts + no error on capability.
$this->assertEquals(2, count($returnedcohorts));
Expand Down Expand Up @@ -367,6 +370,7 @@ public function test_add_cohort_members() {

// Call the external function.
$addcohortmembers = core_cohort_external::add_cohort_members(array($cohort1));
$addcohortmembers = external_api::clean_returnvalue(core_cohort_external::add_cohort_members_returns(), $addcohortmembers);

// Check we retrieve the good total number of created cohorts + no error on capability.
$this->assertEquals(1, count($addcohortmembers));
Expand Down Expand Up @@ -408,11 +412,14 @@ public function test_delete_cohort_members() {
'usertype' => array('type' => 'id', 'value' => $user1->id)
);
$cohortmembers1 = core_cohort_external::add_cohort_members(array($cohortaddmember1));
$cohortmembers1 = external_api::clean_returnvalue(core_cohort_external::add_cohort_members_returns(), $cohortmembers1);

$cohortaddmember2 = array(
'cohorttype' => array('type' => 'id', 'value' => $cohort2->id),
'usertype' => array('type' => 'id', 'value' => $user2->id)
);
$cohortmembers2 = core_cohort_external::add_cohort_members(array($cohortaddmember2));
$cohortmembers2 = external_api::clean_returnvalue(core_cohort_external::add_cohort_members_returns(), $cohortmembers2);

// Check we retrieve no cohorts + no error on capability.
$this->assertEquals(2, $DB->count_records_select('cohort_members', ' ((cohortid = :idcohort1 AND userid = :iduser1)
Expand Down
38 changes: 36 additions & 2 deletions course/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ public function test_get_categories() {
// Retrieve categories from parent.
$categories = core_course_external::get_categories(array(
array('key' => 'parent', 'value' => $category3->id)), 1);
$categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);

$this->assertEquals(2, count($categories));

// Retrieve all categories.
Expand Down Expand Up @@ -937,6 +939,8 @@ public function test_update_courses() {
$courses = array($course1, $course2);

$updatedcoursewarnings = core_course_external::update_courses($courses);
$updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
$updatedcoursewarnings);
$COURSE = $origcourse; // Restore $COURSE. Instead of using the OLD one set by the previous line.

// Check that right number of courses were created.
Expand Down Expand Up @@ -994,6 +998,8 @@ public function test_update_courses() {
$this->unassignUserCapability('moodle/course:update', $contextid, $roleid);
self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
$updatedcoursewarnings = core_course_external::update_courses($courses);
$updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
$updatedcoursewarnings);
$this->assertEquals(1, count($updatedcoursewarnings['warnings']));

// Try update course category without capability.
Expand All @@ -1005,6 +1011,8 @@ public function test_update_courses() {
$course1['categoryid'] = $category2->id;
$courses = array($course1);
$updatedcoursewarnings = core_course_external::update_courses($courses);
$updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
$updatedcoursewarnings);
$this->assertEquals(1, count($updatedcoursewarnings['warnings']));

// Try update course fullname without capability.
Expand All @@ -1014,10 +1022,14 @@ public function test_update_courses() {
$this->setUser($user);
self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
$updatedcoursewarnings = core_course_external::update_courses($courses);
$updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
$updatedcoursewarnings);
$this->assertEquals(0, count($updatedcoursewarnings['warnings']));
$course1['fullname'] = 'Testing fullname without permission';
$courses = array($course1);
$updatedcoursewarnings = core_course_external::update_courses($courses);
$updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
$updatedcoursewarnings);
$this->assertEquals(1, count($updatedcoursewarnings['warnings']));

// Try update course shortname without capability.
Expand All @@ -1027,10 +1039,14 @@ public function test_update_courses() {
$this->setUser($user);
self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
$updatedcoursewarnings = core_course_external::update_courses($courses);
$updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
$updatedcoursewarnings);
$this->assertEquals(0, count($updatedcoursewarnings['warnings']));
$course1['shortname'] = 'Testing shortname without permission';
$courses = array($course1);
$updatedcoursewarnings = core_course_external::update_courses($courses);
$updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
$updatedcoursewarnings);
$this->assertEquals(1, count($updatedcoursewarnings['warnings']));

// Try update course idnumber without capability.
Expand All @@ -1040,10 +1056,14 @@ public function test_update_courses() {
$this->setUser($user);
self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
$updatedcoursewarnings = core_course_external::update_courses($courses);
$updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
$updatedcoursewarnings);
$this->assertEquals(0, count($updatedcoursewarnings['warnings']));
$course1['idnumber'] = 'NEWIDNUMBER';
$courses = array($course1);
$updatedcoursewarnings = core_course_external::update_courses($courses);
$updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
$updatedcoursewarnings);
$this->assertEquals(1, count($updatedcoursewarnings['warnings']));

// Try update course summary without capability.
Expand All @@ -1053,10 +1073,14 @@ public function test_update_courses() {
$this->setUser($user);
self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
$updatedcoursewarnings = core_course_external::update_courses($courses);
$updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
$updatedcoursewarnings);
$this->assertEquals(0, count($updatedcoursewarnings['warnings']));
$course1['summary'] = 'New summary';
$courses = array($course1);
$updatedcoursewarnings = core_course_external::update_courses($courses);
$updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
$updatedcoursewarnings);
$this->assertEquals(1, count($updatedcoursewarnings['warnings']));

// Try update course with invalid summary format.
Expand All @@ -1065,10 +1089,14 @@ public function test_update_courses() {
$this->setUser($user);
self::getDataGenerator()->enrol_user($user->id, $course1['id'], $roleid);
$updatedcoursewarnings = core_course_external::update_courses($courses);
$updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
$updatedcoursewarnings);
$this->assertEquals(0, count($updatedcoursewarnings['warnings']));
$course1['summaryformat'] = 10;
$courses = array($course1);
$updatedcoursewarnings = core_course_external::update_courses($courses);
$updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
$updatedcoursewarnings);
$this->assertEquals(1, count($updatedcoursewarnings['warnings']));

// Try update course visibility without capability.
Expand All @@ -1079,10 +1107,14 @@ public function test_update_courses() {
$course1['summaryformat'] = FORMAT_MOODLE;
$courses = array($course1);
$updatedcoursewarnings = core_course_external::update_courses($courses);
$updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
$updatedcoursewarnings);
$this->assertEquals(0, count($updatedcoursewarnings['warnings']));
$course1['visible'] = 0;
$courses = array($course1);
$updatedcoursewarnings = core_course_external::update_courses($courses);
$updatedcoursewarnings = external_api::clean_returnvalue(core_course_external::update_courses_returns(),
$updatedcoursewarnings);
$this->assertEquals(1, count($updatedcoursewarnings['warnings']));
}

Expand Down Expand Up @@ -1401,7 +1433,8 @@ public function test_view_course() {
// Redirect events to the sink, so we can recover them later.
$sink = $this->redirectEvents();

core_course_external::view_course($course->id, 1);
$result = core_course_external::view_course($course->id, 1);
$result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result);
$events = $sink->get_events();
$event = reset($events);

Expand All @@ -1410,7 +1443,8 @@ public function test_view_course() {
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$this->assertEquals(1, $event->other['coursesectionnumber']);

core_course_external::view_course($course->id);
$result = core_course_external::view_course($course->id);
$result = external_api::clean_returnvalue(core_course_external::view_course_returns(), $result);
$events = $sink->get_events();
$event = array_pop($events);
$sink->close();
Expand Down
3 changes: 3 additions & 0 deletions enrol/self/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function test_get_instance_info() {
$this->assertCount(3, $enrolmentmethods);

$instanceinfo1 = enrol_self_external::get_instance_info($instanceid1);
$instanceinfo1 = external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo1);

$this->assertEquals($instanceid1, $instanceinfo1['id']);
$this->assertEquals($course->id, $instanceinfo1['courseid']);
Expand All @@ -78,6 +79,7 @@ public function test_get_instance_info() {
$this->assertFalse(isset($instanceinfo1['enrolpassword']));

$instanceinfo2 = enrol_self_external::get_instance_info($instanceid2);
$instanceinfo2 = external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo2);
$this->assertEquals($instanceid2, $instanceinfo2['id']);
$this->assertEquals($course->id, $instanceinfo2['courseid']);
$this->assertEquals('self', $instanceinfo2['type']);
Expand All @@ -86,6 +88,7 @@ public function test_get_instance_info() {
$this->assertFalse(isset($instanceinfo2['enrolpassword']));

$instanceinfo3 = enrol_self_external::get_instance_info($instanceid3);
$instanceinfo3 = external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo3);
$this->assertEquals($instanceid3, $instanceinfo3['id']);
$this->assertEquals($course->id, $instanceinfo3['courseid']);
$this->assertEquals('self', $instanceinfo3['type']);
Expand Down
4 changes: 4 additions & 0 deletions enrol/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ public function test_get_course_enrolment_methods() {

// Check if information is returned.
$enrolmentmethods = core_enrol_external::get_course_enrolment_methods($course1->id);
$enrolmentmethods = external_api::clean_returnvalue(core_enrol_external::get_course_enrolment_methods_returns(),
$enrolmentmethods);
// Enrolment information is currently returned by self enrolment plugin, so count == 1.
// This should be changed as we implement get_enrol_info() for other enrolment plugins.
$this->assertCount(1, $enrolmentmethods);
Expand All @@ -287,6 +289,8 @@ public function test_get_course_enrolment_methods() {
'customint6' => 1,
'password' => 'test'));
$enrolmentmethods = core_enrol_external::get_course_enrolment_methods($course2->id);
$enrolmentmethods = external_api::clean_returnvalue(core_enrol_external::get_course_enrolment_methods_returns(),
$enrolmentmethods);
$this->assertCount(1, $enrolmentmethods);

$enrolmentmethod = $enrolmentmethods[0];
Expand Down
12 changes: 10 additions & 2 deletions files/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function test_upload() {
// Call the api to create a file.
$fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
$filename, $filecontent, $contextlevel, $instanceid);
$fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo);
// Get the created draft item id.
$itemid = $fileinfo['itemid'];

Expand All @@ -76,6 +77,7 @@ public function test_upload() {
// Call the api to create a file.
$fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid,
$filepath, $filename, $filecontent, $contextlevel, $instanceid);
$fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo);
$file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
$this->assertNotEmpty($file);

Expand All @@ -88,6 +90,7 @@ public function test_upload() {
$this->assertEmpty($file);
$fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
$filename, $filecontent, $contextlevel, $instanceid);
$fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo);
$file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
$this->assertNotEmpty($file);

Expand Down Expand Up @@ -142,7 +145,8 @@ public function test_upload_param_area() {
$instanceid = null;

// Make sure the file is created.
@core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
$fileinfo = @core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
$fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo);
$browser = get_file_browser();
$file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
$this->assertNotEmpty($file);
Expand All @@ -165,7 +169,8 @@ public function test_upload_without_new_param() {
$filename = "Simple4.txt";
$filecontent = base64_encode("Let us create a nice simple file");

@core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
$fileinfo = @core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
$fileinfo = external_api::clean_returnvalue(core_files_external::upload_returns(), $fileinfo);

// Assert debugging called (deprecation warning).
$this->assertDebuggingCalled();
Expand Down Expand Up @@ -251,6 +256,7 @@ public function test_get_files() {
// The first time is with a valid context ID.
$filename = '';
$testfilelisting = core_files_external::get_files($context->id, $component, $filearea, $itemid, '/', $filename);
$testfilelisting = external_api::clean_returnvalue(core_files_external::get_files_returns(), $testfilelisting);

// With the information that we have provided we should get an object exactly like the one below.
$coursecontext = context_course::instance($course->id);
Expand Down Expand Up @@ -311,6 +317,8 @@ public function test_get_files() {
$contextlevel = 'module';
$instanceid = $module->cmid;
$testfilelisting = core_files_external::get_files($nocontext, $component, $filearea, $itemid, '/', $filename, $modified, $contextlevel, $instanceid);
$testfilelisting = external_api::clean_returnvalue(core_files_external::get_files_returns(), $testfilelisting);

$this->assertEquals($testfilelisting, $testdata);
}
}
Loading

0 comments on commit 89a6290

Please sign in to comment.