本文整理汇总了PHP中external_api::clean_returnvalue方法的典型用法代码示例。如果您正苦于以下问题:PHP external_api::clean_returnvalue方法的具体用法?PHP external_api::clean_returnvalue怎么用?PHP external_api::clean_returnvalue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类external_api
的用法示例。
在下文中一共展示了external_api::clean_returnvalue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: send_response
/**
* Send the result of function call to the WS client
* formatted as XML document.
*/
protected function send_response()
{
//Check that the returned values are valid
try {
if ($this->function->returns_desc != null) {
$validatedvalues = external_api::clean_returnvalue($this->function->returns_desc, $this->returns);
} else {
$validatedvalues = null;
}
} catch (Exception $ex) {
$exception = $ex;
}
if (!empty($exception)) {
$response = $this->generate_error($exception);
} else {
//We can now convert the response to the requested REST format
if ($this->restformat == 'json') {
$response = json_encode($validatedvalues);
} else {
$response = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
$response .= '<RESPONSE>' . "\n";
$response .= self::xmlize_result($this->returns, $this->function->returns_desc);
$response .= '</RESPONSE>' . "\n";
}
}
$this->send_headers();
echo $response;
}
示例2: test_update_activity_completion_status_manually
/**
* Test update_activity_completion_status_manually
*/
public function test_update_activity_completion_status_manually()
{
global $DB, $CFG;
$this->resetAfterTest(true);
$CFG->enablecompletion = true;
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
$data = $this->getDataGenerator()->create_module('data', array('course' => $course->id), array('completion' => 1));
$cm = get_coursemodule_from_id('data', $data->cmid);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
$this->setUser($user);
$result = core_completion_external::update_activity_completion_status_manually($data->cmid, true);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(core_completion_external::update_activity_completion_status_manually_returns(), $result);
// Check in DB.
$this->assertEquals(1, $DB->get_field('course_modules_completion', 'completionstate', array('coursemoduleid' => $data->cmid)));
// Check using the API.
$completion = new completion_info($course);
$completiondata = $completion->get_data($cm);
$this->assertEquals(1, $completiondata->completionstate);
$this->assertTrue($result['status']);
$result = core_completion_external::update_activity_completion_status_manually($data->cmid, false);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(core_completion_external::update_activity_completion_status_manually_returns(), $result);
$this->assertEquals(0, $DB->get_field('course_modules_completion', 'completionstate', array('coursemoduleid' => $data->cmid)));
$completiondata = $completion->get_data($cm);
$this->assertEquals(0, $completiondata->completionstate);
$this->assertTrue($result['status']);
}
示例3: test_get_plugins_supporting_mobile
/**
* Test get_plugins_supporting_mobile.
* This is a very basic test because currently there aren't plugins supporting Mobile in core.
*/
public function test_get_plugins_supporting_mobile()
{
$result = external::get_plugins_supporting_mobile();
$result = external_api::clean_returnvalue(external::get_plugins_supporting_mobile_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertCount(0, $result['plugins']);
}
示例4: test_get_site_info
public function test_get_site_info() {
global $DB, $USER, $CFG;
$this->resetAfterTest(true);
// This is the info we are going to check
set_config('release', '2.4dev (Build: 20120823)');
set_config('version', '2012083100.00');
// Set current user
$user = array();
$user['username'] = 'johnd';
$user['firstname'] = 'John';
$user['lastname'] = 'Doe';
self::setUser(self::getDataGenerator()->create_user($user));
// Add a web service and token.
$webservice = new stdClass();
$webservice->name = 'Test web service';
$webservice->enabled = true;
$webservice->restrictedusers = false;
$webservice->component = 'moodle';
$webservice->timecreated = time();
$webservice->downloadfiles = true;
$externalserviceid = $DB->insert_record('external_services', $webservice);
// Add a function to the service
$DB->insert_record('external_services_functions', array('externalserviceid' => $externalserviceid,
'functionname' => 'core_course_get_contents'));
$_POST['wstoken'] = 'testtoken';
$externaltoken = new stdClass();
$externaltoken->token = 'testtoken';
$externaltoken->tokentype = 0;
$externaltoken->userid = $USER->id;
$externaltoken->externalserviceid = $externalserviceid;
$externaltoken->contextid = 1;
$externaltoken->creatorid = $USER->id;
$externaltoken->timecreated = time();
$DB->insert_record('external_tokens', $externaltoken);
$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']);
$this->assertEquals(current_language(), $siteinfo['lang']);
$this->assertEquals($USER->id, $siteinfo['userid']);
$this->assertEquals(true, $siteinfo['downloadfiles']);
$this->assertEquals($CFG->release, $siteinfo['release']);
$this->assertEquals($CFG->version, $siteinfo['version']);
$this->assertEquals($CFG->mobilecssurl, $siteinfo['mobilecssurl']);
$this->assertEquals(count($siteinfo['functions']), 1);
$function = array_pop($siteinfo['functions']);
$this->assertEquals($function['name'], 'core_course_get_contents');
$this->assertEquals($function['version'], $siteinfo['version']);
}
示例5: test_get_glossaries_by_courses
/**
* Test get_glossaries_by_courses
*/
public function test_get_glossaries_by_courses()
{
$this->resetAfterTest(true);
// As admin.
$this->setAdminUser();
$c1 = self::getDataGenerator()->create_course();
$c2 = self::getDataGenerator()->create_course();
$g1 = self::getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'name' => 'First Glossary'));
$g2 = self::getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'name' => 'Second Glossary'));
$g3 = self::getDataGenerator()->create_module('glossary', array('course' => $c2->id, 'name' => 'Third Glossary'));
$s1 = $this->getDataGenerator()->create_user();
self::getDataGenerator()->enrol_user($s1->id, $c1->id);
// Check results where student is enrolled.
$this->setUser($s1);
$glossaries = mod_glossary_external::get_glossaries_by_courses(array());
$glossaries = external_api::clean_returnvalue(mod_glossary_external::get_glossaries_by_courses_returns(), $glossaries);
$this->assertCount(2, $glossaries['glossaries']);
$this->assertEquals('First Glossary', $glossaries['glossaries'][0]['name']);
$this->assertEquals('Second Glossary', $glossaries['glossaries'][1]['name']);
// Check results with specific course IDs.
$glossaries = mod_glossary_external::get_glossaries_by_courses(array($c1->id, $c2->id));
$glossaries = external_api::clean_returnvalue(mod_glossary_external::get_glossaries_by_courses_returns(), $glossaries);
$this->assertCount(2, $glossaries['glossaries']);
$this->assertEquals('First Glossary', $glossaries['glossaries'][0]['name']);
$this->assertEquals('Second Glossary', $glossaries['glossaries'][1]['name']);
$this->assertEquals('course', $glossaries['warnings'][0]['item']);
$this->assertEquals($c2->id, $glossaries['warnings'][0]['itemid']);
$this->assertEquals('1', $glossaries['warnings'][0]['warningcode']);
// Now as admin.
$this->setAdminUser();
$glossaries = mod_glossary_external::get_glossaries_by_courses(array($c2->id));
$glossaries = external_api::clean_returnvalue(mod_glossary_external::get_glossaries_by_courses_returns(), $glossaries);
$this->assertCount(1, $glossaries['glossaries']);
$this->assertEquals('Third Glossary', $glossaries['glossaries'][0]['name']);
}
示例6: test_confirm_user
/**
* Test confirm_user
*/
public function test_confirm_user()
{
global $DB;
$username = 'pepe';
$password = 'abcdefAª.ªª!!3';
$firstname = 'Pepe';
$lastname = 'Pérez';
$email = 'myemail@no.zbc';
// Create new user.
$result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email);
$result = external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
$this->assertTrue($result['success']);
$this->assertEmpty($result['warnings']);
$secret = $DB->get_field('user', 'secret', array('username' => $username));
// Confirm the user.
$result = core_auth_external::confirm_user($username, $secret);
$result = external_api::clean_returnvalue(core_auth_external::confirm_user_returns(), $result);
$this->assertTrue($result['success']);
$this->assertEmpty($result['warnings']);
$confirmed = $DB->get_field('user', 'confirmed', array('username' => $username));
$this->assertEquals(1, $confirmed);
// Try to confirm the user again.
$result = core_auth_external::confirm_user($username, $secret);
$result = external_api::clean_returnvalue(core_auth_external::confirm_user_returns(), $result);
$this->assertFalse($result['success']);
$this->assertCount(1, $result['warnings']);
$this->assertEquals('alreadyconfirmed', $result['warnings'][0]['warningcode']);
// Try to use an invalid secret.
$this->expectException('moodle_exception');
$this->expectExceptionMessage(get_string('invalidconfirmdata', 'error'));
$result = core_auth_external::confirm_user($username, 'zzZZzz');
}
示例7: test_view_folder
/**
* Test view_folder
*/
public function test_view_folder()
{
global $DB;
$this->resetAfterTest(true);
$this->setAdminUser();
// Setup test data.
$course = $this->getDataGenerator()->create_course();
$folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id));
$context = context_module::instance($folder->cmid);
$cm = get_coursemodule_from_instance('folder', $folder->id);
// Test invalid instance id.
try {
mod_folder_external::view_folder(0);
$this->fail('Exception expected due to invalid mod_folder instance id.');
} catch (moodle_exception $e) {
$this->assertEquals('invalidrecord', $e->errorcode);
}
// Test not-enrolled user.
$user = self::getDataGenerator()->create_user();
$this->setUser($user);
try {
mod_folder_external::view_folder($folder->id);
$this->fail('Exception expected due to not enrolled user.');
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}
// Test user with full capabilities.
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$result = mod_folder_external::view_folder($folder->id);
$result = external_api::clean_returnvalue(mod_folder_external::view_folder_returns(), $result);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = array_shift($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\\mod_folder\\event\\course_module_viewed', $event);
$this->assertEquals($context, $event->get_context());
$moodlefolder = new \moodle_url('/mod/folder/view.php', array('id' => $cm->id));
$this->assertEquals($moodlefolder, $event->get_url());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
// Test user with no capabilities.
// We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
assign_capability('mod/folder:view', CAP_PROHIBIT, $studentrole->id, $context->id);
// Empty all the caches that may be affected by this change.
accesslib_clear_all_caches_for_unit_testing();
course_modinfo::clear_instance_cache();
try {
mod_folder_external::view_folder($folder->id);
$this->fail('Exception expected due to missing capability.');
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}
}
示例8: test_get_instance_info
/**
* Test get_instance_info
*/
public function test_get_instance_info()
{
global $DB;
$this->resetAfterTest(true);
// Check if self enrolment plugin is enabled.
$selfplugin = enrol_get_plugin('self');
$this->assertNotEmpty($selfplugin);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->assertNotEmpty($studentrole);
$coursedata = new stdClass();
$coursedata->visible = 0;
$course = self::getDataGenerator()->create_course($coursedata);
// Add enrolment methods for course.
$instanceid1 = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED, 'name' => 'Test instance 1', 'customint6' => 1, 'roleid' => $studentrole->id));
$instanceid2 = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_DISABLED, 'customint6' => 1, 'name' => 'Test instance 2', 'roleid' => $studentrole->id));
$instanceid3 = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED, 'roleid' => $studentrole->id, 'customint6' => 1, 'name' => 'Test instance 3', 'password' => 'test'));
$enrolmentmethods = $DB->get_records('enrol', array('courseid' => $course->id, 'status' => ENROL_INSTANCE_ENABLED));
$this->assertCount(3, $enrolmentmethods);
$this->setAdminUser();
$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']);
$this->assertEquals('self', $instanceinfo1['type']);
$this->assertEquals('Test instance 1', $instanceinfo1['name']);
$this->assertTrue($instanceinfo1['status']);
$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']);
$this->assertEquals('Test instance 2', $instanceinfo2['name']);
$this->assertEquals(get_string('canntenrol', 'enrol_self'), $instanceinfo2['status']);
$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']);
$this->assertEquals('Test instance 3', $instanceinfo3['name']);
$this->assertTrue($instanceinfo3['status']);
$this->assertEquals(get_string('password', 'enrol_self'), $instanceinfo3['enrolpassword']);
// Try to retrieve information using a normal user for a hidden course.
$user = self::getDataGenerator()->create_user();
$this->setUser($user);
try {
enrol_self_external::get_instance_info($instanceid3);
} catch (moodle_exception $e) {
$this->assertEquals('coursehidden', $e->errorcode);
}
}
示例9: test_view_scorm
/**
* Test view_scorm
*/
public function test_view_scorm()
{
global $DB;
$this->resetAfterTest(true);
$this->setAdminUser();
// Setup test data.
$course = $this->getDataGenerator()->create_course();
$scorm = $this->getDataGenerator()->create_module('scorm', array('course' => $course->id));
$context = context_module::instance($scorm->cmid);
$cm = get_coursemodule_from_instance('scorm', $scorm->id);
// Test invalid instance id.
try {
mod_scorm_external::view_scorm(0);
$this->fail('Exception expected due to invalid mod_scorm instance id.');
} catch (moodle_exception $e) {
$this->assertEquals('invalidrecord', $e->errorcode);
}
// Test not-enrolled user.
$user = self::getDataGenerator()->create_user();
$this->setUser($user);
try {
mod_scorm_external::view_scorm($scorm->id);
$this->fail('Exception expected due to not enrolled user.');
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}
// Test user with full capabilities.
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$result = mod_scorm_external::view_scorm($scorm->id);
$result = external_api::clean_returnvalue(mod_scorm_external::view_scorm_returns(), $result);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = array_shift($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\\mod_scorm\\event\\course_module_viewed', $event);
$this->assertEquals($context, $event->get_context());
$moodleurl = new \moodle_url('/mod/scorm/view.php', array('id' => $cm->id));
$this->assertEquals($moodleurl, $event->get_url());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
}
示例10: test_create_notes
/**
* Test create_notes
*/
public function test_create_notes() {
global $DB, $USER, $DB;
$this->resetAfterTest(true);
$course = self::getDataGenerator()->create_course();
// Set the required capabilities by the external function
$contextid = context_course::instance($course->id)->id;
$roleid = $this->assignUserCapability('moodle/notes:manage', $contextid);
$this->assignUserCapability('moodle/course:view', $contextid, $roleid);
// Create test note data.
$note1 = array();
$note1['userid'] = $USER->id;
$note1['publishstate'] = 'personal';
$note1['courseid'] = $course->id;
$note1['text'] = 'the text';
$note1['clientnoteid'] = 4;
$notes = array($note1);
$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.
$this->assertEquals($thenote->userid, $note1['userid']);
$this->assertEquals($thenote->courseid, $note1['courseid']);
$this->assertEquals($thenote->publishstate, NOTES_STATE_DRAFT);
$this->assertEquals($thenote->content, $note1['text']);
$this->assertEquals($creatednotes[0]['clientnoteid'], $note1['clientnoteid']);
// Call without required capability
$this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid);
$this->setExpectedException('required_capability_exception');
$creatednotes = core_notes_external::create_notes($notes);
}
示例11: test_get_site_public_settings
public function test_get_site_public_settings()
{
global $CFG, $SITE;
$this->resetAfterTest(true);
$result = external::get_site_public_settings();
$result = external_api::clean_returnvalue(external::get_site_public_settings_returns(), $result);
// Test default values.
$context = context_system::instance();
$expected = array('wwwroot' => $CFG->wwwroot, 'httpswwwroot' => $CFG->httpswwwroot, 'sitename' => external_format_string($SITE->fullname, $context->id, true), 'guestlogin' => $CFG->guestloginbutton, 'rememberusername' => $CFG->rememberusername, 'authloginviaemail' => $CFG->authloginviaemail, 'registerauth' => $CFG->registerauth, 'forgottenpasswordurl' => $CFG->forgottenpasswordurl, 'authinstructions' => format_text($CFG->auth_instructions), 'authnoneenabled' => (int) is_enabled_auth('none'), 'enablewebservices' => $CFG->enablewebservices, 'enablemobilewebservice' => $CFG->enablemobilewebservice, 'maintenanceenabled' => $CFG->maintenance_enabled, 'maintenancemessage' => format_text($CFG->maintenance_message), 'warnings' => array());
$this->assertEquals($expected, $result);
// Change a value.
set_config('registerauth', 'email');
$authinstructions = 'Something with <b>html tags</b>';
set_config('auth_instructions', $authinstructions);
$expected['registerauth'] = 'email';
$expected['authinstructions'] = format_text($authinstructions);
$result = external::get_site_public_settings();
$result = external_api::clean_returnvalue(external::get_site_public_settings_returns(), $result);
$this->assertEquals($expected, $result);
}
示例12: test_search_contacts
/**
* Test search_contacts.
*/
public function test_search_contacts()
{
global $DB;
$this->resetAfterTest(true);
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$user1 = new stdClass();
$user1->firstname = 'X';
$user1->lastname = 'X';
$user1 = $this->getDataGenerator()->create_user($user1);
$this->getDataGenerator()->enrol_user($user1->id, $course1->id);
$this->getDataGenerator()->enrol_user($user1->id, $course2->id);
$user2 = new stdClass();
$user2->firstname = 'Eric';
$user2->lastname = 'Cartman';
$user2 = self::getDataGenerator()->create_user($user2);
$user3 = new stdClass();
$user3->firstname = 'Stan';
$user3->lastname = 'Marsh';
$user3 = self::getDataGenerator()->create_user($user3);
self::getDataGenerator()->enrol_user($user3->id, $course1->id);
$user4 = new stdClass();
$user4->firstname = 'Kyle';
$user4->lastname = 'Broflovski';
$user4 = self::getDataGenerator()->create_user($user4);
$user5 = new stdClass();
$user5->firstname = 'Kenny';
$user5->lastname = 'McCormick';
$user5 = self::getDataGenerator()->create_user($user5);
self::getDataGenerator()->enrol_user($user5->id, $course2->id);
$this->setUser($user1);
$results = core_message_external::search_contacts('r');
$results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
$this->assertCount(5, $results);
// Users 2 through 5 + admin
$results = core_message_external::search_contacts('r', true);
$results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
$this->assertCount(2, $results);
$results = core_message_external::search_contacts('Kyle', false);
$results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
$this->assertCount(1, $results);
$result = reset($results);
$this->assertEquals($user4->id, $result['id']);
$results = core_message_external::search_contacts('y', false);
$results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
$this->assertCount(2, $results);
$results = core_message_external::search_contacts('y', true);
$results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
$this->assertCount(1, $results);
$result = reset($results);
$this->assertEquals($user5->id, $result['id']);
// Empty query, will throw an exception.
$this->setExpectedException('moodle_exception');
$results = core_message_external::search_contacts('');
}
示例13: test_clean_returnvalue
/**
* Test for clean_returnvalue().
*/
public function test_clean_returnvalue()
{
// Build some return value decription.
$returndesc = new external_multiple_structure(new external_single_structure(array('object' => new external_single_structure(array('value1' => new external_value(PARAM_INT, 'this is a int'))), 'value2' => new external_value(PARAM_TEXT, 'some text', VALUE_OPTIONAL))));
// Clean an object (it should be cast into an array).
$object = new stdClass();
$object->value1 = 1;
$singlestructure['object'] = $object;
$singlestructure['value2'] = 'Some text';
$testdata = array($singlestructure);
$cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata);
$cleanedsinglestructure = array_pop($cleanedvalue);
$this->assertSame($object->value1, $cleanedsinglestructure['object']['value1']);
$this->assertSame($singlestructure['value2'], $cleanedsinglestructure['value2']);
// Missing VALUE_OPTIONAL.
$object = new stdClass();
$object->value1 = 1;
$singlestructure = new stdClass();
$singlestructure->object = $object;
$testdata = array($singlestructure);
$cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata);
$cleanedsinglestructure = array_pop($cleanedvalue);
$this->assertSame($object->value1, $cleanedsinglestructure['object']['value1']);
$this->assertArrayNotHasKey('value2', $cleanedsinglestructure);
// Unknown attribute (the value should be ignored).
$object = array();
$object['value1'] = 1;
$singlestructure = array();
$singlestructure['object'] = $object;
$singlestructure['value2'] = 'Some text';
$singlestructure['unknownvalue'] = 'Some text to ignore';
$testdata = array($singlestructure);
$cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata);
$cleanedsinglestructure = array_pop($cleanedvalue);
$this->assertSame($object['value1'], $cleanedsinglestructure['object']['value1']);
$this->assertSame($singlestructure['value2'], $cleanedsinglestructure['value2']);
$this->assertArrayNotHasKey('unknownvalue', $cleanedsinglestructure);
// Missing required value (an exception is thrown).
$object = array();
$singlestructure = array();
$singlestructure['object'] = $object;
$singlestructure['value2'] = 'Some text';
$testdata = array($singlestructure);
$this->setExpectedException('invalid_response_exception');
$cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata);
}
示例14: test_mod_anonforum_get_anonforum_discussions
//.........这里部分代码省略.........
$record = new stdClass();
$record->course = $course1->id;
$record->userid = $user1->id;
$record->anonforum = $anonforum1->id;
$discussion1 = self::getDataGenerator()->get_plugin_generator('mod_anonforum')->create_discussion($record);
$record = new stdClass();
$record->course = $course2->id;
$record->userid = $user2->id;
$record->anonforum = $anonforum2->id;
$discussion2 = self::getDataGenerator()->get_plugin_generator('mod_anonforum')->create_discussion($record);
$record = new stdClass();
$record->course = $course2->id;
$record->userid = $user2->id;
$record->anonforum = $anonforum3->id;
$discussion3 = self::getDataGenerator()->get_plugin_generator('mod_anonforum')->create_discussion($record);
// Add three replies to the discussion 1 from different users.
$record = new stdClass();
$record->discussion = $discussion1->id;
$record->parent = $discussion1->firstpost;
$record->userid = $user2->id;
$discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_anonforum')->create_post($record);
$record->parent = $discussion1reply1->id;
$record->userid = $user3->id;
$discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_anonforum')->create_post($record);
$record->userid = $user4->id;
$discussion1reply3 = self::getDataGenerator()->get_plugin_generator('mod_anonforum')->create_post($record);
// Add two replies to discussion 2 from different users.
$record = new stdClass();
$record->discussion = $discussion2->id;
$record->parent = $discussion2->firstpost;
$record->userid = $user1->id;
$discussion2reply1 = self::getDataGenerator()->get_plugin_generator('mod_anonforum')->create_post($record);
$record->parent = $discussion2reply1->id;
$record->userid = $user3->id;
$discussion2reply2 = self::getDataGenerator()->get_plugin_generator('mod_anonforum')->create_post($record);
// Check the forums were correctly created.
$this->assertEquals(3, $DB->count_records_select('anonforum', 'id = :anonforum1 OR id = :anonforum2 OR id = :anonforum3', array('anonforum1' => $anonforum1->id, 'anonforum2' => $anonforum2->id, 'anonforum3' => $anonforum3->id)));
// Check the discussions were correctly created.
$this->assertEquals(3, $DB->count_records_select('anonforum_discussions', 'anonforum = :anonforum1 OR anonforum = :anonforum2 OR anonforum = :anonforum3', array('anonforum1' => $anonforum1->id, 'anonforum2' => $anonforum2->id, 'anonforum3' => $anonforum3->id)));
// Check the posts were correctly created, don't forget each discussion created also creates a post.
$this->assertEquals(7, $DB->count_records_select('anonforum_posts', 'discussion = :discussion1 OR discussion = :discussion2', array('discussion1' => $discussion1->id, 'discussion2' => $discussion2->id)));
// Enrol the user in the first course.
$enrol = enrol_get_plugin('manual');
// Following line enrol and assign default role id to the user.
// So the user automatically gets mod/anonforum:viewdiscussion on all forums of the course.
$this->getDataGenerator()->enrol_user($user1->id, $course1->id);
// Now enrol into the second course.
// We don't use the dataGenerator as we need to get the $instance2 to unenrol later.
$enrolinstances = enrol_get_instances($course2->id, true);
foreach ($enrolinstances as $courseenrolinstance) {
if ($courseenrolinstance->enrol == "manual") {
$instance2 = $courseenrolinstance;
break;
}
}
$enrol->enrol_user($instance2, $user1->id);
// Assign capabilities to view discussions for forum 2.
$cm = get_coursemodule_from_id('anonforum', $anonforum2->cmid, 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
$newrole = create_role('Role 2', 'role2', 'Role 2 description');
$this->assignUserCapability('mod/anonforum:viewdiscussion', $context->id, $newrole);
// Assign capabilities to view discussions for forum 3.
$cm = get_coursemodule_from_id('anonforum', $anonforum3->cmid, 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
$this->assignUserCapability('mod/anonforum:viewdiscussion', $context->id, $newrole);
// Create what we expect to be returned when querying the forums.
$expecteddiscussions = array();
$expecteddiscussions[$discussion1->id] = array('id' => $discussion1->id, 'course' => $discussion1->course, 'anonforum' => $discussion1->anonforum, 'name' => $discussion1->name, 'firstpost' => $discussion1->firstpost, 'userid' => $discussion1->userid, 'groupid' => $discussion1->groupid, 'assessed' => $discussion1->assessed, 'timemodified' => $discussion1reply3->created, 'usermodified' => $discussion1reply3->userid, 'timestart' => $discussion1->timestart, 'timeend' => $discussion1->timeend, 'firstuserfullname' => fullname($user1), 'firstuserimagealt' => $user1->imagealt, 'firstuserpicture' => $user1->picture, 'firstuseremail' => $user1->email, 'subject' => $discussion1->name, 'numreplies' => 3, 'numunread' => '', 'lastpost' => $discussion1reply3->id, 'lastuserid' => $user4->id, 'lastuserfullname' => fullname($user4), 'lastuserimagealt' => $user4->imagealt, 'lastuserpicture' => $user4->picture, 'lastuseremail' => $user4->email);
$expecteddiscussions[$discussion2->id] = array('id' => $discussion2->id, 'course' => $discussion2->course, 'anonforum' => $discussion2->anonforum, 'name' => $discussion2->name, 'firstpost' => $discussion2->firstpost, 'userid' => $discussion2->userid, 'groupid' => $discussion2->groupid, 'assessed' => $discussion2->assessed, 'timemodified' => $discussion2reply2->created, 'usermodified' => $discussion2reply2->userid, 'timestart' => $discussion2->timestart, 'timeend' => $discussion2->timeend, 'firstuserfullname' => fullname($user2), 'firstuserimagealt' => $user2->imagealt, 'firstuserpicture' => $user2->picture, 'firstuseremail' => $user2->email, 'subject' => $discussion2->name, 'numreplies' => 2, 'numunread' => 3, 'lastpost' => $discussion2reply2->id, 'lastuserid' => $user3->id, 'lastuserfullname' => fullname($user3), 'lastuserimagealt' => $user3->imagealt, 'lastuserpicture' => $user3->picture, 'lastuseremail' => $user3->email);
$expecteddiscussions[$discussion3->id] = array('id' => $discussion3->id, 'course' => $discussion3->course, 'anonforum' => $discussion3->anonforum, 'name' => $discussion3->name, 'firstpost' => $discussion3->firstpost, 'userid' => $discussion3->userid, 'groupid' => $discussion3->groupid, 'assessed' => $discussion3->assessed, 'timemodified' => $discussion3->timemodified, 'usermodified' => $discussion3->usermodified, 'timestart' => $discussion3->timestart, 'timeend' => $discussion3->timeend, 'firstuserfullname' => fullname($user2), 'firstuserimagealt' => $user2->imagealt, 'firstuserpicture' => $user2->picture, 'firstuseremail' => $user2->email, 'subject' => $discussion3->name, 'numreplies' => 0, 'numunread' => 1, 'lastpost' => $discussion3->firstpost, 'lastuserid' => $user2->id, 'lastuserfullname' => fullname($user2), 'lastuserimagealt' => $user2->imagealt, 'lastuserpicture' => $user2->picture, 'lastuseremail' => $user2->email);
// Call the external function passing forum ids.
$discussions = mod_anonforum_external::get_anonforum_discussions(array($anonforum1->id, $anonforum2->id, $anonforum3->id));
external_api::clean_returnvalue(mod_anonforum_external::get_anonforum_discussions_returns(), $discussions);
$this->assertEquals($expecteddiscussions, $discussions);
// Remove the users post from the qanda forum and ensure they can not return the discussion.
$DB->delete_records('anonforum_posts', array('id' => $discussion2reply1->id));
try {
mod_anonforum_external::get_anonforum_discussions(array($anonforum2->id));
$this->fail('Exception expected due to attempting to access qanda forum without posting.');
} catch (moodle_exception $e) {
$this->assertEquals('nopermissions', $e->errorcode);
}
// Call without required view discussion capability.
$this->unassignUserCapability('mod/anonforum:viewdiscussion', null, null, $course1->id);
try {
mod_anonforum_external::get_anonforum_discussions(array($anonforum1->id));
$this->fail('Exception expected due to missing capability.');
} catch (moodle_exception $e) {
$this->assertEquals('nopermissions', $e->errorcode);
}
// Unenrol user from second course.
$enrol->unenrol_user($instance2, $user1->id);
// Call for the second course we unenrolled the user from, make sure exception thrown.
try {
mod_anonforum_external::get_anonforum_discussions(array($anonforum2->id));
$this->fail('Exception expected due to being unenrolled from the course.');
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}
}
示例15: test_edit_page
/**
* Test edit_page. We won't test all the possible cases because that's already
* done in the tests for wiki_save_section / wiki_save_page.
*/
public function test_edit_page()
{
$this->create_individual_wikis_with_groups();
// Test user with full capabilities.
$this->setUser($this->student);
$newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page($this->wikisepind, array('group' => $this->group1->id, 'content' => 'Test'));
// Test edit whole page.
$sectioncontent = '<h1>Title1</h1>Text inside section';
$newpagecontent = $sectioncontent . '<h1>Title2</h1>Text inside section';
$result = mod_wiki_external::edit_page($newpage->id, $newpagecontent);
$result = external_api::clean_returnvalue(mod_wiki_external::edit_page_returns(), $result);
$this->assertInternalType('int', $result['pageid']);
$version = wiki_get_current_version($result['pageid']);
$this->assertEquals($newpagecontent, $version->content);
// Test edit section.
$newsectioncontent = '<h1>Title2</h1>New test2';
$section = 'Title2';
$result = mod_wiki_external::edit_page($newpage->id, $newsectioncontent, $section);
$result = external_api::clean_returnvalue(mod_wiki_external::edit_page_returns(), $result);
$this->assertInternalType('int', $result['pageid']);
$expected = $sectioncontent . $newsectioncontent;
$version = wiki_get_current_version($result['pageid']);
$this->assertEquals($expected, $version->content);
// Test locked section.
$newsectioncontent = '<h1>Title2</h1>New test2';
$section = 'Title2';
try {
// Using user 1 to avoid other users to edit.
wiki_set_lock($newpage->id, 1, $section, true);
mod_wiki_external::edit_page($newpage->id, $newsectioncontent, $section);
$this->fail('Exception expected due to locked section');
} catch (moodle_exception $e) {
$this->assertEquals('pageislocked', $e->errorcode);
}
// Test edit non existing section.
$newsectioncontent = '<h1>Title3</h1>New test3';
$section = 'Title3';
try {
mod_wiki_external::edit_page($newpage->id, $newsectioncontent, $section);
$this->fail('Exception expected due to non existing section in the page.');
} catch (moodle_exception $e) {
$this->assertEquals('invalidsection', $e->errorcode);
}
}