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'] = '******'; $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']); }
/** * 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']); }
/** * 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']); }
/** * Test confirm_user */ public function test_confirm_user() { global $DB; $username = '******'; $password = '******'; $firstname = 'Pepe'; $lastname = 'Pérez'; $email = '*****@*****.**'; // 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'); }
/** * Manage inplace editable saves. * * @param string $itemtype The type of item. * @param int $itemid The ID of the item. * @param mixed $newvalue The new value * @return string */ function tool_usertours_inplace_editable($itemtype, $itemid, $newvalue) { $context = \context_system::instance(); external_api::validate_context($context); require_capability('tool/usertours:managetours', $context); if ($itemtype === 'tourname') { $tour = helper::get_tour($itemid); $tour->set_name($newvalue)->persist(); return helper::render_tourname_inplace_editable($tour); } else { if ($itemtype === 'tourdescription') { $tour = helper::get_tour($itemid); $tour->set_description($newvalue)->persist(); return helper::render_tourdescription_inplace_editable($tour); } else { if ($itemtype === 'tourenabled') { $tour = helper::get_tour($itemid); $tour->set_enabled(!!$newvalue)->persist(); return helper::render_tourenabled_inplace_editable($tour); } else { if ($itemtype === 'stepname') { $step = helper::get_step($itemid); $step->set_title($newvalue)->persist(); return helper::render_stepname_inplace_editable($step); } } } } }
/** * 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; }
/** * 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']); }
public function test_validate_params() { $params = array('text' => 'aaa', 'someid' => '6'); $description = new external_function_parameters(array('someid' => new external_value(PARAM_INT, 'Some int value'), 'text' => new external_value(PARAM_ALPHA, 'Some text value'))); $result = external_api::validate_parameters($description, $params); $this->assertEqual(count($result), 2); reset($result); $this->assertTrue(key($result) === 'someid'); $this->assertTrue($result['someid'] === 6); $this->assertTrue($result['text'] === 'aaa'); $params = array('someids' => array('1', 2, 'a' => '3'), 'scalar' => 666); $description = new external_function_parameters(array('someids' => new external_multiple_structure(new external_value(PARAM_INT, 'Some ID')), 'scalar' => new external_value(PARAM_ALPHANUM, 'Some text value'))); $result = external_api::validate_parameters($description, $params); $this->assertEqual(count($result), 2); reset($result); $this->assertTrue(key($result) === 'someids'); $this->assertTrue($result['someids'] == array(0 => 1, 1 => 2, 2 => 3)); $this->assertTrue($result['scalar'] === '666'); $params = array('text' => 'aaa'); $description = new external_function_parameters(array('someid' => new external_value(PARAM_INT, 'Some int value', false), 'text' => new external_value(PARAM_ALPHA, 'Some text value'))); $result = external_api::validate_parameters($description, $params); $this->assertEqual(count($result), 2); reset($result); $this->assertTrue(key($result) === 'someid'); $this->assertTrue($result['someid'] === null); $this->assertTrue($result['text'] === 'aaa'); $params = array('text' => 'aaa'); $description = new external_function_parameters(array('someid' => new external_value(PARAM_INT, 'Some int value', false, 6), 'text' => new external_value(PARAM_ALPHA, 'Some text value'))); $result = external_api::validate_parameters($description, $params); $this->assertEqual(count($result), 2); reset($result); $this->assertTrue(key($result) === 'someid'); $this->assertTrue($result['someid'] === 6); $this->assertTrue($result['text'] === 'aaa'); }
/** * Implements callback inplace_editable() allowing to edit values in-place * * @param string $itemtype * @param int $itemid * @param mixed $newvalue * @return \core\output\inplace_editable */ function core_tag_inplace_editable($itemtype, $itemid, $newvalue) { \external_api::validate_context(context_system::instance()); if ($itemtype === 'tagname') { return \core_tag\output\tagname::update($itemid, $newvalue); } else { if ($itemtype === 'tagareaenable') { return \core_tag\output\tagareaenabled::update($itemid, $newvalue); } else { if ($itemtype === 'tagareacollection') { return \core_tag\output\tagareacollection::update($itemid, $newvalue); } else { if ($itemtype === 'tagareashowstandard') { return \core_tag\output\tagareashowstandard::update($itemid, $newvalue); } else { if ($itemtype === 'tagcollname') { return \core_tag\output\tagcollname::update($itemid, $newvalue); } else { if ($itemtype === 'tagcollsearchable') { return \core_tag\output\tagcollsearchable::update($itemid, $newvalue); } else { if ($itemtype === 'tagflag') { return \core_tag\output\tagflag::update($itemid, $newvalue); } else { if ($itemtype === 'tagisstandard') { return \core_tag\output\tagisstandard::update($itemid, $newvalue); } } } } } } } } }
/** * 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); } }
/** * Updates cohort name and returns instance of this object * * @param int $cohortid * @param string $newvalue * @return static */ public static function update($cohortid, $newvalue) { global $DB; $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST); $cohortcontext = \context::instance_by_id($cohort->contextid); \external_api::validate_context($cohortcontext); require_capability('moodle/cohort:manage', $cohortcontext); $record = (object) array('id' => $cohort->id, 'idnumber' => $newvalue, 'contextid' => $cohort->contextid); cohort_update_cohort($record); $cohort->idnumber = $newvalue; return new static($cohort); }
/** * 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); } }
/** * Updates course module name * * @param int $itemid course module id * @param string $newvalue new name * @return static */ public static function update($itemid, $newvalue) { $context = context_module::instance($itemid); // Check access. \external_api::validate_context($context); require_capability('moodle/course:manageactivities', $context); // Update value. set_coursemodule_name($itemid, $newvalue); $coursemodulerecord = get_coursemodule_from_id('', $itemid, 0, false, MUST_EXIST); // Return instance. $cm = get_fast_modinfo($coursemodulerecord->course)->get_cm($itemid); return new static($cm, true); }
/** * Updates cohort name and returns instance of this object * * @param int $cohortid * @param string $newvalue * @return static */ public static function update($cohortid, $newvalue) { global $DB; $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST); $cohortcontext = \context::instance_by_id($cohort->contextid); \external_api::validate_context($cohortcontext); require_capability('moodle/cohort:manage', $cohortcontext); $newvalue = clean_param($newvalue, PARAM_TEXT); if (strval($newvalue) !== '') { $record = (object) array('id' => $cohort->id, 'name' => $newvalue, 'contextid' => $cohort->contextid); cohort_update_cohort($record); $cohort->name = $newvalue; } return new static($cohort); }
/** * 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()); }
/** * 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); }
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); }
$testclient = new $testclientclass(); $serverurl = "{$CFG->wwwroot}/webservice/{$protocol}/"; if ($authmethod == 'simple') { $serverurl .= 'simpleserver.php'; $serverurl .= '?wsusername='******'&wspassword='******'token') { $serverurl .= 'server.php'; $serverurl .= '?wstoken=' . urlencode($data->token); } } // now get the function parameters $params = $mform->get_params(); // now test the parameters, this also fixes PHP data types $params = external_api::validate_parameters($functioninfo->parameters_desc, $params); echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('pluginname', 'webservice_' . $protocol) . ': ' . $function); echo 'URL: ' . s($serverurl); echo $OUTPUT->box_start(); try { $response = $testclient->simpletest($serverurl, $function, $params); echo str_replace("\n", '<br />', s(var_export($response, true))); } catch (Exception $ex) { //TODO: handle exceptions and faults without exposing of the sensitive information such as debug traces! echo str_replace("\n", '<br />', s($ex)); } echo $OUTPUT->box_end(); $mform->display(); echo $OUTPUT->footer(); die;
/** * Test get forum discussions */ public function test_mod_anonforum_get_anonforum_discussions() { global $USER, $CFG, $DB; $this->resetAfterTest(true); // Set the CFG variable to allow track forums. $CFG->anonforum_trackreadposts = true; // Create a user who can track forums. $record = new stdClass(); $record->trackforums = true; $user1 = self::getDataGenerator()->create_user($record); // Create a bunch of other users to post. $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); $user4 = self::getDataGenerator()->create_user(); // Set the first created user to the test user. self::setUser($user1); // Create courses to add the modules. $course1 = self::getDataGenerator()->create_course(); $course2 = self::getDataGenerator()->create_course(); // First forum with tracking off. $record = new stdClass(); $record->course = $course1->id; $record->trackingtype = FORUM_TRACKING_OFF; $anonforum1 = self::getDataGenerator()->create_module('anonforum', $record); // Second forum of type 'qanda' with tracking enabled. $record = new stdClass(); $record->course = $course2->id; $record->type = 'qanda'; $record->trackingtype = FORUM_TRACKING_FORCED; $anonforum2 = self::getDataGenerator()->create_module('anonforum', $record); // Third forum where we will only have one discussion with no replies. $record = new stdClass(); $record->course = $course2->id; $anonforum3 = self::getDataGenerator()->create_module('anonforum', $record); // Add discussions to the forums. $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); } }
/** * Test delete_cohort_members */ public function test_delete_cohort_members() { global $DB; $this->resetAfterTest(true); // Reset all changes automatically after this test. $cohort1 = self::getDataGenerator()->create_cohort(); $user1 = self::getDataGenerator()->create_user(); $cohort2 = self::getDataGenerator()->create_cohort(); $user2 = self::getDataGenerator()->create_user(); $context = context_system::instance(); $roleid = $this->assignUserCapability('moodle/cohort:assign', $context->id); $cohortaddmember1 = array('cohorttype' => array('type' => 'id', 'value' => $cohort1->id), '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) OR (cohortid = :idcohort2 AND userid = :iduser2))', array('idcohort1' => $cohort1->id, 'iduser1' => $user1->id, 'idcohort2' => $cohort2->id, 'iduser2' => $user2->id))); // Call the external function. $cohortdel1 = array('cohortid' => $cohort1->id, 'userid' => $user1->id); $cohortdel2 = array('cohortid' => $cohort2->id, 'userid' => $user2->id); core_cohort_external::delete_cohort_members(array($cohortdel1, $cohortdel2)); // Check we retrieve no cohorts + no error on capability. $this->assertEquals(0, $DB->count_records_select('cohort_members', ' ((cohortid = :idcohort1 AND userid = :iduser1) OR (cohortid = :idcohort2 AND userid = :iduser2))', array('idcohort1' => $cohort1->id, 'iduser1' => $user1->id, 'idcohort2' => $cohort2->id, 'iduser2' => $user2->id))); // Call without required capability. $this->unassignUserCapability('moodle/cohort:assign', $context->id, $roleid); $this->setExpectedException('required_capability_exception'); core_cohort_external::delete_cohort_members(array($cohortdel1, $cohortdel2)); }
/** * 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); } }
* Web services API documentation * * @package webservice * @copyright 2011 Moodle Pty Ltd (http://moodle.com) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @author Jerome Mouneyrac */ require_once '../../config.php'; require_once $CFG->libdir . '/adminlib.php'; require $CFG->dirroot . '/webservice/lib.php'; admin_externalpage_setup('webservicedocumentation'); // get all the function descriptions $functions = $DB->get_records('external_functions', array(), 'name'); $functiondescs = array(); foreach ($functions as $function) { $functiondescs[$function->name] = external_api::external_function_info($function); } //display the documentation for all documented protocols, //regardless if they are activated or not $protocols = array(); $protocols['rest'] = true; $protocols['xmlrpc'] = true; /// Check if we are in printable mode $printableformat = optional_param('print', false, PARAM_BOOL); /// OUTPUT echo $OUTPUT->header(); $renderer = $PAGE->get_renderer('core', 'webservice'); echo $renderer->documentation_html($functiondescs, $printableformat, $protocols, array(), $PAGE->url); /// trigger browser print operation if (!empty($printableformat)) { $PAGE->requires->js_function_call('window.print', array());
/** * Test duplicate_course */ public function test_duplicate_course() { $this->resetAfterTest(true); // Create one course with three modules. $course = self::getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id)); $forumcm = get_coursemodule_from_id('forum', $forum->cmid); $forumcontext = context_module::instance($forum->cmid); $data = $this->getDataGenerator()->create_module('data', array('assessed'=>1, 'scale'=>100, 'course'=>$course->id)); $datacontext = context_module::instance($data->cmid); $datacm = get_coursemodule_from_instance('page', $data->id); $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id)); $pagecontext = context_module::instance($page->cmid); $pagecm = get_coursemodule_from_instance('page', $page->id); // Set the required capabilities by the external function. $coursecontext = context_course::instance($course->id); $categorycontext = context_coursecat::instance($course->category); $roleid = $this->assignUserCapability('moodle/course:create', $categorycontext->id); $this->assignUserCapability('moodle/course:view', $categorycontext->id, $roleid); $this->assignUserCapability('moodle/restore:restorecourse', $categorycontext->id, $roleid); $this->assignUserCapability('moodle/backup:backupcourse', $coursecontext->id, $roleid); $this->assignUserCapability('moodle/backup:configure', $coursecontext->id, $roleid); // Optional capabilities to copy user data. $this->assignUserCapability('moodle/backup:userinfo', $coursecontext->id, $roleid); $this->assignUserCapability('moodle/restore:userinfo', $categorycontext->id, $roleid); $newcourse['fullname'] = 'Course duplicate'; $newcourse['shortname'] = 'courseduplicate'; $newcourse['categoryid'] = $course->category; $newcourse['visible'] = true; $newcourse['options'][] = array('name' => 'users', 'value' => true); $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']); }
/** * Prepares the response. */ protected function prepare_response() { try { if (!empty($this->function->returns_desc)) { $validatedvalues = external_api::clean_returnvalue($this->function->returns_desc, $this->returns); $encodingoptions = array("encoding" => "utf-8", "verbosity" => "no_white_space"); // We can now convert the response to the requested XML-RPC format. $this->response = xmlrpc_encode_request(null, $validatedvalues, $encodingoptions); } } catch (invalid_response_exception $ex) { $this->response = $this->generate_error($ex); } }
/** * Test get_quiz_required_qtypes */ public function test_get_quiz_required_qtypes() { global $DB; // Create a new quiz. $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); $data = array('course' => $this->course->id); $quiz = $quizgenerator->create_instance($data); // Create some questions. $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $questiongenerator->create_question_category(); $question = $questiongenerator->create_question('numerical', null, array('category' => $cat->id)); quiz_add_quiz_question($question->id, $quiz); $question = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id)); quiz_add_quiz_question($question->id, $quiz); // Add new question types in the category (for the random one). $question = $questiongenerator->create_question('truefalse', null, array('category' => $cat->id)); $question = $questiongenerator->create_question('essay', null, array('category' => $cat->id)); $question = $questiongenerator->create_question('random', null, array('category' => $cat->id)); quiz_add_quiz_question($question->id, $quiz); $this->setUser($this->student); $result = mod_quiz_external::get_quiz_required_qtypes($quiz->id); $result = external_api::clean_returnvalue(mod_quiz_external::get_quiz_required_qtypes_returns(), $result); $expected = array('questiontypes' => ['essay', 'numerical', 'random', 'shortanswer', 'truefalse'], 'warnings' => []); $this->assertEquals($expected, $result); }
/** * Test get_course_module_by_instance */ public function test_get_course_module_by_instance() { global $DB; $this->resetAfterTest(true); $this->setAdminUser(); $course = self::getDataGenerator()->create_course(); $record = array('course' => $course->id, 'name' => 'First Chat'); $options = array('idnumber' => 'ABC', 'visible' => 0); // Hidden activity. $chat = self::getDataGenerator()->create_module('chat', $record, $options); // Test admin user can see the complete hidden activity. $result = core_course_external::get_course_module_by_instance('chat', $chat->id); $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result); $this->assertCount(0, $result['warnings']); // Test we retrieve all the fields. $this->assertCount(22, $result['cm']); $this->assertEquals($record['name'], $result['cm']['name']); $this->assertEquals($options['idnumber'], $result['cm']['idnumber']); $student = $this->getDataGenerator()->create_user(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); self::getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id); $this->setUser($student); // The user shouldn't be able to see the activity. try { core_course_external::get_course_module_by_instance('chat', $chat->id); $this->fail('Exception expected due to invalid permissions.'); } catch (moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } // Make module visible. set_coursemodule_visible($chat->cmid, 1); // Test student user. $result = core_course_external::get_course_module_by_instance('chat', $chat->id); $result = external_api::clean_returnvalue(core_course_external::get_course_module_by_instance_returns(), $result); $this->assertCount(0, $result['warnings']); // Test we retrieve only the few files we can see. $this->assertCount(11, $result['cm']); $this->assertEquals($chat->cmid, $result['cm']['id']); $this->assertEquals($course->id, $result['cm']['course']); $this->assertEquals('chat', $result['cm']['modname']); $this->assertEquals($chat->id, $result['cm']['instance']); // Try with an invalid module name. try { core_course_external::get_course_module_by_instance('abc', $chat->id); $this->fail('Exception expected due to invalid module name.'); } catch (dml_read_exception $e) { $this->assertEquals('dmlreadexception', $e->errorcode); } }
/** * Returns detailed function information * * @deprecated since Moodle 3.1 * @param string|object $function name of external function or record from external_function * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found; * MUST_EXIST means throw exception if no record or multiple records found * @return stdClass description or false if not found or exception thrown * @since Moodle 2.0 */ function external_function_info($function, $strictness = MUST_EXIST) { debugging('external_function_info() is deprecated. Please use external_api::external_function_info() instead.', DEBUG_DEVELOPER); return external_api::external_function_info($function, $strictness); }
/** * Test launch_sco */ public function test_launch_sco() { global $DB; // Test invalid instance id. try { mod_scorm_external::launch_sco(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::launch_sco($this->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. $this->setUser($this->student); // Trigger and capture the event. $sink = $this->redirectEvents(); $scoes = scorm_get_scoes($this->scorm->id); foreach ($scoes as $sco) { // Find launchable SCO. if ($sco->launch != '') { break; } } $result = mod_scorm_external::launch_sco($this->scorm->id, $sco->id); $result = external_api::clean_returnvalue(mod_scorm_external::launch_sco_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\\sco_launched', $event); $this->assertEquals($this->context, $event->get_context()); $moodleurl = new \moodle_url('/mod/scorm/player.php', array('id' => $this->cm->id, 'scoid' => $sco->id)); $this->assertEquals($moodleurl, $event->get_url()); $this->assertEventContextNotUsed($event); $this->assertNotEmpty($event->get_name()); // Invalid SCO. try { mod_scorm_external::launch_sco($this->scorm->id, -1); $this->fail('Exception expected due to invalid SCO id.'); } catch (moodle_exception $e) { $this->assertEquals('cannotfindsco', $e->errorcode); } }
/** * Updates the value in the database and modifies this object respectively. * * ALWAYS check user permissions before performing an update! Throw exceptions if permissions are not sufficient * or value is not legit. * * @param stdClass $section * @param string $itemtype * @param mixed $newvalue * @return \core\output\inplace_editable */ public function inplace_editable_update_section_name($section, $itemtype, $newvalue) { if ($itemtype === 'sectionname' || $itemtype === 'sectionnamenl') { $context = context_course::instance($section->course); external_api::validate_context($context); require_capability('moodle/course:update', $context); $newtitle = clean_param($newvalue, PARAM_TEXT); if (strval($section->name) !== strval($newtitle)) { course_update_section($section->course, $section, array('name' => $newtitle)); } return $this->inplace_editable_render_section_name($section, $itemtype === 'sectionname', true); } }
/** * Test mark_course_self_completed */ public function test_mark_course_self_completed() { global $DB, $CFG; require_once $CFG->dirroot . '/completion/criteria/completion_criteria_self.php'; $this->resetAfterTest(true); $CFG->enablecompletion = true; $student = $this->getDataGenerator()->create_user(); $teacher = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id); // Set completion rules. $completion = new completion_info($course); $criteriadata = new stdClass(); $criteriadata->id = $course->id; $criteriadata->criteria_activity = array(); // Self completion. $criteriadata->criteria_self = COMPLETION_CRITERIA_TYPE_SELF; $class = 'completion_criteria_self'; $criterion = new $class(); $criterion->update_config($criteriadata); // Handle overall aggregation. $aggdata = array('course' => $course->id, 'criteriatype' => null); $aggregation = new completion_aggregation($aggdata); $aggregation->setMethod(COMPLETION_AGGREGATION_ALL); $aggregation->save(); $this->setUser($student); $result = core_completion_external::mark_course_self_completed($course->id); // We need to execute the return values cleaning process to simulate the web service server. $result = external_api::clean_returnvalue(core_completion_external::mark_course_self_completed_returns(), $result); // We expect a valid result. $this->assertEquals(true, $result['status']); $result = core_completion_external::get_course_completion_status($course->id, $student->id); // We need to execute the return values cleaning process to simulate the web service server. $result = external_api::clean_returnvalue(core_completion_external::get_course_completion_status_returns(), $result); // Course must be completed. $this->assertEquals(COMPLETION_COMPLETE, $result['completionstatus']['completions'][0]['complete']); try { $result = core_completion_external::mark_course_self_completed($course->id); $this->fail('Exception expected due course already self completed.'); } catch (moodle_exception $e) { $this->assertEquals('useralreadymarkedcomplete', $e->errorcode); } }