Skip to content

Commit

Permalink
MDL-37354 clean return value for each externallib PHPUnit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mouneyrac committed Jan 9, 2013
1 parent 9da506c commit fb695f6
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 4 deletions.
33 changes: 33 additions & 0 deletions course/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public function test_create_categories() {

$createdcats = core_course_external::create_categories($categories);

// We need to execute the return values cleaning process to simulate the web service server.
$createdcats = external_api::clean_returnvalue(core_course_external::create_categories_returns(), $createdcats);

// Initially confirm that base data was inserted correctly.
$this->assertEquals($category1->name, $createdcats[0]['name']);
$this->assertEquals($category2->name, $createdcats[1]['name']);
Expand All @@ -93,6 +96,9 @@ public function test_create_categories() {

$createdsubcats = core_course_external::create_categories($subcategories);

// We need to execute the return values cleaning process to simulate the web service server.
$createdsubcats = external_api::clean_returnvalue(core_course_external::create_categories_returns(), $createdsubcats);

// Confirm that sub categories were inserted correctly.
$this->assertEquals($category3->name, $createdsubcats[0]['name']);

Expand Down Expand Up @@ -197,6 +203,9 @@ public function test_get_categories() {
array('key' => 'id', 'value' => $category1->id),
array('key' => 'visible', 'value' => 1)), 1);

// We need to execute the return values cleaning process to simulate the web service server.
$categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);

// Check we retrieve the good total number of categories.
$this->assertEquals(2, count($categories));

Expand All @@ -214,6 +223,10 @@ public function test_get_categories() {
array('key' => 'id', 'value' => $category1->id),
array('key' => 'idnumber', 'value' => $category1->idnumber),
array('key' => 'visible', 'value' => 1)), 0);

// We need to execute the return values cleaning process to simulate the web service server.
$categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);

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

// Retrieve categories from parent.
Expand All @@ -223,6 +236,10 @@ public function test_get_categories() {

// Retrieve all categories.
$categories = core_course_external::get_categories();

// We need to execute the return values cleaning process to simulate the web service server.
$categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories);

$this->assertEquals($DB->count_records('course_categories'), count($categories));

// Call without required capability (it will fail cause of the search on idnumber).
Expand Down Expand Up @@ -354,6 +371,9 @@ public function test_create_courses() {

$createdcourses = core_course_external::create_courses($courses);

// We need to execute the return values cleaning process to simulate the web service server.
$createdcourses = external_api::clean_returnvalue(core_course_external::create_courses_returns(), $createdcourses);

// Check that right number of courses were created.
$this->assertEquals(2, count($createdcourses));

Expand Down Expand Up @@ -492,6 +512,9 @@ public function test_get_courses () {
$courses = core_course_external::get_courses(array('ids' =>
array($course1->id, $course2->id)));

// We need to execute the return values cleaning process to simulate the web service server.
$courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);

// Check we retrieve the good total number of categories.
$this->assertEquals(2, count($courses));

Expand Down Expand Up @@ -532,6 +555,10 @@ public function test_get_courses () {

// Get all courses in the DB
$courses = core_course_external::get_courses(array());

// We need to execute the return values cleaning process to simulate the web service server.
$courses = external_api::clean_returnvalue(core_course_external::get_courses_returns(), $courses);

$this->assertEquals($DB->count_records('course'), count($courses));
}

Expand Down Expand Up @@ -559,6 +586,9 @@ public function test_get_course_contents() {

$courses = core_course_external::get_course_contents($course->id, array());

// We need to execute the return values cleaning process to simulate the web service server.
$courses = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $courses);

// Check that the course has the 3 created modules
$this->assertEquals(3, count($courses[0]['modules']));
}
Expand Down Expand Up @@ -602,6 +632,9 @@ public function test_duplicate_course() {
$duplicate = core_course_external::duplicate_course($course->id, $newcourse['fullname'],
$newcourse['shortname'], $newcourse['categoryid'], $newcourse['visible'], $newcourse['options']);

// We need to execute the return values cleaning process to simulate the web service server.
$duplicate = external_api::clean_returnvalue(core_course_external::duplicate_course_returns(), $duplicate);

// Check that the course has been duplicated.
$this->assertEquals($newcourse['shortname'], $duplicate['shortname']);
}
Expand Down
9 changes: 9 additions & 0 deletions enrol/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public function test_get_enrolled_users() {
// Call the external function.
$enrolledusers = core_enrol_external::get_enrolled_users($course->id);

// We need to execute the return values cleaning process to simulate the web service server.
$enrolledusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers);

// Check we retrieve the good total number of enrolled users.
$this->assertEquals(3, count($enrolledusers));

Expand Down Expand Up @@ -109,6 +112,9 @@ public function test_get_users_courses() {
// Call the external function.
$enrolledincourses = core_enrol_external::get_users_courses($USER->id);

// We need to execute the return values cleaning process to simulate the web service server.
$enrolledincourses = external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses);

// Check we retrieve the good total number of enrolled users.
$this->assertEquals(2, count($enrolledincourses));
}
Expand Down Expand Up @@ -156,6 +162,9 @@ public function test_get_enrolled_users_with_capability () {
$options = array();
$result = core_enrol_external::get_enrolled_users_with_capability($params, $options);

// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_with_capability_returns(), $result);

// Check an array containing the expected user for the course capability is returned.
$expecteduserlist = $result[0];
$this->assertEquals($course1->id, $expecteduserlist['courseid']);
Expand Down
10 changes: 8 additions & 2 deletions group/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public function test_create_groups() {
// Call the external function.
$groups = core_group_external::create_groups(array($group1, $group2));

// We need to execute the return values cleaning process to simulate the web service server.
$groups = external_api::clean_returnvalue(core_group_external::create_groups_returns(), $groups);

// Checks against DB values
$this->assertEquals(2, count($groups));
foreach ($groups as $group) {
Expand Down Expand Up @@ -125,6 +128,9 @@ public function test_get_groups() {
// Call the external function.
$groups = core_group_external::get_groups(array($group1->id, $group2->id));

// We need to execute the return values cleaning process to simulate the web service server.
$groups = external_api::clean_returnvalue(core_group_external::get_groups_returns(), $groups);

// Checks against DB values
$this->assertEquals(2, count($groups));
foreach ($groups as $group) {
Expand All @@ -151,7 +157,7 @@ public function test_get_groups() {
// Call without required capability
$this->unassignUserCapability('moodle/course:managegroups', $context->id, $roleid);
$this->setExpectedException('required_capability_exception');
$froups = core_group_external::get_groups(array($group1->id, $group2->id));
$groups = core_group_external::get_groups(array($group1->id, $group2->id));
}

/**
Expand Down Expand Up @@ -201,4 +207,4 @@ public function test_delete_groups() {
$this->setExpectedException('required_capability_exception');
$froups = core_group_external::delete_groups(array($group3->id));
}
}
}
15 changes: 15 additions & 0 deletions lib/external/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,30 @@ public function test_get_string() {
$returnedstring = core_external::get_string('addservice', 'webservice',
array(array('name' => 'name', 'value' => $service->name),
array('name' => 'id', 'value' => $service->id)));

// We need to execute the return values cleaning process to simulate the web service server.
$returnedstring = external_api::clean_returnvalue(core_external::get_string_returns(), $returnedstring);

$corestring = get_string('addservice', 'webservice', $service);
$this->assertEquals($corestring, $returnedstring);

// String with one parameter.
$acapname = 'A capability name';
$returnedstring = core_external::get_string('missingrequiredcapability', 'webservice',
array(array('value' => $acapname)));

// We need to execute the return values cleaning process to simulate the web service server.
$returnedstring = external_api::clean_returnvalue(core_external::get_string_returns(), $returnedstring);

$corestring = get_string('missingrequiredcapability', 'webservice', $acapname);
$this->assertEquals($corestring, $returnedstring);

// String without parameters.
$returnedstring = core_external::get_string('missingpassword', 'webservice');

// We need to execute the return values cleaning process to simulate the web service server.
$returnedstring = external_api::clean_returnvalue(core_external::get_string_returns(), $returnedstring);

$corestring = get_string('missingpassword', 'webservice');
$this->assertEquals($corestring, $returnedstring);

Expand Down Expand Up @@ -87,6 +99,9 @@ public function test_get_strings() {
array('stringid' => 'addaservice', 'component' => 'webservice')
));

// We need to execute the return values cleaning process to simulate the web service server.
$returnedstrings = external_api::clean_returnvalue(core_external::get_strings_returns(), $returnedstrings);

foreach($returnedstrings as $returnedstring) {
$corestring = get_string($returnedstring['stringid'], $returnedstring['component'], $service);
$this->assertEquals($corestring, $returnedstring['string']);
Expand Down
5 changes: 4 additions & 1 deletion message/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public function test_send_instant_messages() {

$sentmessages = core_message_external::send_instant_messages($messages);

// We need to execute the return values cleaning process to simulate the web service server.
$sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages);

$themessage = $DB->get_record('message', array('id' => $sentmessages[0]['msgid']));

// Confirm that the message was inserted correctly.
Expand All @@ -72,4 +75,4 @@ public function test_send_instant_messages() {
$this->assertEquals($themessage->smallmessage, $message1['text']);
$this->assertEquals($sentmessages[0]['clientmsgid'], $message1['clientmsgid']);
}
}
}
15 changes: 15 additions & 0 deletions mod/assign/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ public function test_get_grades () {
$assignmentids[] = $assign->id;
$result = mod_assign_external::get_grades($assignmentids);

// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(mod_assign_external::get_grades_returns(), $result);

// Check that the correct grade information for the student is returned.
$this->assertEquals(1, count($result['assignments']));
$assignment = $result['assignments'][0];
Expand Down Expand Up @@ -158,6 +161,10 @@ public function test_get_assignments () {
));

$result = mod_assign_external::get_assignments();

// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);

// Check the course and assignment are returned.
$this->assertEquals(1, count($result['courses']));
$course = $result['courses'][0];
Expand All @@ -169,6 +176,10 @@ public function test_get_assignments () {
$this->assertEquals('lightwork assignment', $assignment['name']);

$result = mod_assign_external::get_assignments(array($course1->id));

// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);

$this->assertEquals(1, count($result['courses']));
$course = $result['courses'][0];
$this->assertEquals('Lightwork Course 1', $course['fullname']);
Expand All @@ -179,6 +190,10 @@ public function test_get_assignments () {
$this->assertEquals('lightwork assignment', $assignment['name']);

$result = mod_assign_external::get_assignments(array($course2->id));

// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);

$this->assertEquals(0, count($result['courses']));
$this->assertEquals(1, count($result['warnings']));
}
Expand Down
3 changes: 3 additions & 0 deletions notes/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public function test_create_notes() {

$creatednotes = core_notes_external::create_notes($notes);

// We need to execute the return values cleaning process to simulate the web service server.
$creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes);

$thenote = $DB->get_record('post', array('id' => $creatednotes[0]['noteid']));

// Confirm that base note data was inserted correctly.
Expand Down
17 changes: 16 additions & 1 deletion user/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public function test_get_course_user_profiles() {
array('userid' => $user1->id, 'courseid' => $course->id),
array('userid' => $user2->id, 'courseid' => $course->id)));

// We need to execute the return values cleaning process to simulate the web service server.
$enrolledusers = external_api::clean_returnvalue(core_user_external::get_course_user_profiles_returns(), $enrolledusers);

// Check we retrieve the good total number of enrolled users + no error on capability.
$this->assertEquals(3, count($enrolledusers));

Expand All @@ -109,6 +112,9 @@ public function test_get_course_user_profiles() {
array('userid' => $user1->id, 'courseid' => $course->id),
array('userid' => $user2->id, 'courseid' => $course->id)));

// We need to execute the return values cleaning process to simulate the web service server.
$enrolledusers = external_api::clean_returnvalue(core_user_external::get_course_user_profiles_returns(), $enrolledusers);

foreach($enrolledusers as $enrolleduser) {
if ($enrolleduser['username'] == $user1->username) {
$this->assertEquals($user1->idnumber, $enrolleduser['idnumber']);
Expand Down Expand Up @@ -163,6 +169,9 @@ public function test_create_users() {
// Call the external function.
$createdusers = core_user_external::create_users(array($user1));

// We need to execute the return values cleaning process to simulate the web service server.
$createdusers = external_api::clean_returnvalue(core_user_external::create_users_returns(), $createdusers);

// Check we retrieve the good total number of created users + no error on capability.
$this->assertEquals(1, count($createdusers));

Expand Down Expand Up @@ -261,6 +270,9 @@ public function test_get_users_by_id() {
$returnedusers = core_user_external::get_users_by_id(array(
$USER->id, $user1->id, $user2->id));

// We need to execute the return values cleaning process to simulate the web service server.
$returnedusers = external_api::clean_returnvalue(core_user_external::get_users_by_id_returns(), $returnedusers);

// Check we retrieve the good total number of enrolled users + no error on capability.
$this->assertEquals(3, count($returnedusers));

Expand All @@ -272,6 +284,9 @@ public function test_get_users_by_id() {
$returnedusers = core_user_external::get_users_by_id(array(
$USER->id, $user1->id, $user2->id));

// We need to execute the return values cleaning process to simulate the web service server.
$returnedusers = external_api::clean_returnvalue(core_user_external::get_users_by_id_returns(), $returnedusers);

foreach($returnedusers as $enrolleduser) {
if ($enrolleduser['username'] == $user1->username) {
$this->assertEquals($user1->idnumber, $enrolleduser['idnumber']);
Expand Down Expand Up @@ -344,4 +359,4 @@ public function test_update_users() {
$this->setExpectedException('required_capability_exception');
core_user_external::update_users(array($user1));
}
}
}
3 changes: 3 additions & 0 deletions webservice/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public function test_get_site_info() {

$siteinfo = core_webservice_external::get_site_info();

// We need to execute the return values cleaning process to simulate the web service server.
$siteinfo = external_api::clean_returnvalue(core_webservice_external::get_site_info_returns(), $siteinfo);

$this->assertEquals('johnd', $siteinfo['username']);
$this->assertEquals('John', $siteinfo['firstname']);
$this->assertEquals('Doe', $siteinfo['lastname']);
Expand Down

0 comments on commit fb695f6

Please sign in to comment.