/** * 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']); }
/** * 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']); }
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 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'); }
/** * 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); } }
/** * 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); } }
/** * 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); }
/** * 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(''); }
/** * 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); }
/** * 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 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); } }
/** * 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); } }
/** * 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); }
public function test_update_notes() { global $DB, $USER; $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'; $note2['userid'] = $USER->id; $note2['publishstate'] = 'course'; $note2['courseid'] = $course->id; $note2['text'] = 'the text'; $note3['userid'] = $USER->id; $note3['publishstate'] = 'site'; $note3['courseid'] = $course->id; $note3['text'] = 'the text'; $notes1 = array($note1, $note2, $note3); $creatednotes = core_notes_external::create_notes($notes1); $creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes); $note2 = array(); $note2["id"] = $creatednotes[0]['noteid']; $note2['publishstate'] = 'personal'; $note2['text'] = 'the new text'; $note2['format'] = FORMAT_HTML; $notes2 = array($note2); $updatednotes = core_notes_external::update_notes($notes2); $updatednotes = external_api::clean_returnvalue(core_notes_external::update_notes_returns(), $updatednotes); $thenote = $DB->get_record('post', array('id' => $creatednotes[0]['noteid'])); // Confirm that base note data was updated correctly. $this->assertEquals($thenote->publishstate, NOTES_STATE_DRAFT); $this->assertEquals($note2['text'], $thenote->content); // Call without required capability. $creatednotes = core_notes_external::create_notes($notes1); $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid); $this->setExpectedException('required_capability_exception'); $note2 = array(); $note2["id"] = $creatednotes[0]['noteid']; $note2['publishstate'] = 'personal'; $note2['text'] = 'the new text'; $note2['format'] = FORMAT_HTML; $notes2 = array($note2); $updatednotes = core_notes_external::update_notes($notes2); }
public function test_get_entry_by_id() { $this->resetAfterTest(true); // Generate all the things. $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary'); $c1 = $this->getDataGenerator()->create_course(); $c2 = $this->getDataGenerator()->create_course(); $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id)); $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'visible' => 0)); $u1 = $this->getDataGenerator()->create_user(); $u2 = $this->getDataGenerator()->create_user(); $ctx = context_module::instance($g1->cmid); $this->getDataGenerator()->enrol_user($u1->id, $c1->id); $e1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id)); $e2 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u1->id)); $e3 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u2->id)); $e4 = $gg->create_content($g2, array('approved' => 1)); $this->setUser($u1); $return = mod_glossary_external::get_entry_by_id($e1->id); $return = external_api::clean_returnvalue(mod_glossary_external::get_entry_by_id_returns(), $return); $this->assertEquals($e1->id, $return['entry']['id']); $return = mod_glossary_external::get_entry_by_id($e2->id); $return = external_api::clean_returnvalue(mod_glossary_external::get_entry_by_id_returns(), $return); $this->assertEquals($e2->id, $return['entry']['id']); try { $return = mod_glossary_external::get_entry_by_id($e3->id); $this->fail('Cannot view unapproved entries of others.'); } catch (invalid_parameter_exception $e) { // All good. } try { $return = mod_glossary_external::get_entry_by_id($e4->id); $this->fail('Cannot view entries from another course.'); } catch (require_login_exception $e) { // All good. } // An admin can be other's entries to be approved. $this->setAdminUser(); $return = mod_glossary_external::get_entry_by_id($e3->id); $return = external_api::clean_returnvalue(mod_glossary_external::get_entry_by_id_returns(), $return); $this->assertEquals($e3->id, $return['entry']['id']); }
/** * Test get_comments */ public function test_get_comments() { global $DB, $CFG; $this->resetAfterTest(true); $CFG->usecomments = true; $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(array('enablecomment' => 1)); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id); $record = new stdClass(); $record->course = $course->id; $record->name = "Mod data test"; $record->intro = "Some intro of some sort"; $record->comments = 1; $module = $this->getDataGenerator()->create_module('data', $record); $field = data_get_field_new('text', $module); $fielddetail = new stdClass(); $fielddetail->name = 'Name'; $fielddetail->description = 'Some name'; $field->define_field($fielddetail); $field->insert_field(); $recordid = data_add_record($module); $datacontent = array(); $datacontent['fieldid'] = $field->field->id; $datacontent['recordid'] = $recordid; $datacontent['content'] = 'Asterix'; $contentid = $DB->insert_record('data_content', $datacontent); $cm = get_coursemodule_from_instance('data', $module->id, $course->id); $context = context_module::instance($module->cmid); $this->setUser($user); // We need to add the comments manually, the comment API uses the global OUTPUT and this is going to make the WS to fail. $newcmt = new stdClass(); $newcmt->contextid = $context->id; $newcmt->commentarea = 'database_entry'; $newcmt->itemid = $recordid; $newcmt->content = 'New comment'; $newcmt->format = 0; $newcmt->userid = $user->id; $newcmt->timecreated = time(); $cmtid1 = $DB->insert_record('comments', $newcmt); $newcmt->content = 'New comment 2'; $newcmt->timecreated = time() + 1; $cmtid2 = $DB->insert_record('comments', $newcmt); $contextlevel = 'module'; $instanceid = $cm->id; $component = 'mod_data'; $itemid = $recordid; $area = 'database_entry'; $page = 0; $result = core_comment_external::get_comments($contextlevel, $instanceid, $component, $itemid, $area, $page); // We need to execute the return values cleaning process to simulate the web service server. $result = external_api::clean_returnvalue(core_comment_external::get_comments_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(2, $result['comments']); $this->assertEquals($user->id, $result['comments'][0]['userid']); $this->assertEquals($user->id, $result['comments'][1]['userid']); $this->assertEquals($cmtid2, $result['comments'][0]['id']); $this->assertEquals($cmtid1, $result['comments'][1]['id']); }
/** * 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); } }
/** * Test update_inplace_editable() */ public function test_update_module_name_inplace() { global $CFG, $DB, $PAGE; require_once $CFG->dirroot . '/lib/external/externallib.php'; $this->setUser($this->getDataGenerator()->create_user()); $this->resetAfterTest(true); $course = $this->getDataGenerator()->create_course(); $forum = self::getDataGenerator()->create_module('forum', array('course' => $course->id, 'name' => 'forum name')); // Call service for core_course component without necessary permissions. try { core_external::update_inplace_editable('core_course', 'activityname', $forum->cmid, 'New forum name'); $this->fail('Exception expected'); } catch (moodle_exception $e) { $this->assertEquals('Course or activity not accessible. (Not enrolled)', $e->getMessage()); } // Change to admin user and make sure that cm name can be updated using web service update_inplace_editable(). $this->setAdminUser(); $res = core_external::update_inplace_editable('core_course', 'activityname', $forum->cmid, 'New forum name'); $res = external_api::clean_returnvalue(core_external::update_inplace_editable_returns(), $res); $this->assertEquals('New forum name', $res['value']); $this->assertEquals('New forum name', $DB->get_field('forum', 'name', array('id' => $forum->id))); }
/** * 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_enrolled_users_with_capability */ public function test_get_enrolled_users_with_capability_with_parameters() { $capability = 'moodle/course:viewparticipants'; $data = $this->get_enrolled_users_with_capability_setup($capability); $result = core_enrol_external::get_enrolled_users_with_capability(array('coursecapabilities' => array('courseid' => $data->course->id, 'capabilities' => array($capability))), array(array('name' => 'limitfrom', 'value' => 1), array('name' => 'limitnumber', 'value' => 1), array('name' => 'userfields', 'value' => 'id'))); // 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]['users']; $expecteduser = reset($expecteduserlist); $this->assertEquals(1, count($expecteduserlist)); $this->assertEquals($data->student1->id, $expecteduser['id']); }
/** * 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']); }
/** * Test view_book */ public function test_view_book() { global $DB; $this->resetAfterTest(true); $this->setAdminUser(); // Setup test data. $course = $this->getDataGenerator()->create_course(); $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id)); $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book'); $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id)); $chapterhidden = $bookgenerator->create_chapter(array('bookid' => $book->id, 'hidden' => 1)); $context = context_module::instance($book->cmid); $cm = get_coursemodule_from_instance('book', $book->id); // Test invalid instance id. try { mod_book_external::view_book(0); $this->fail('Exception expected due to invalid mod_book 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_book_external::view_book($book->id, 0); $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_book_external::view_book($book->id, 0); $result = external_api::clean_returnvalue(mod_book_external::view_book_returns(), $result); $events = $sink->get_events(); $this->assertCount(2, $events); $event = array_shift($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\mod_book\event\course_module_viewed', $event); $this->assertEquals($context, $event->get_context()); $moodleurl = new \moodle_url('/mod/book/view.php', array('id' => $cm->id)); $this->assertEquals($moodleurl, $event->get_url()); $this->assertEventContextNotUsed($event); $this->assertNotEmpty($event->get_name()); $event = array_shift($events); $this->assertInstanceOf('\mod_book\event\chapter_viewed', $event); $this->assertEquals($chapter->id, $event->objectid); $result = mod_book_external::view_book($book->id, $chapter->id); $result = external_api::clean_returnvalue(mod_book_external::view_book_returns(), $result); $events = $sink->get_events(); // We expect a total of 3 events. $this->assertCount(3, $events); // Try to view a hidden chapter. try { mod_book_external::view_book($book->id, $chapterhidden->id); $this->fail('Exception expected due to missing capability.'); } catch (moodle_exception $e) { $this->assertEquals('errorchapter', $e->errorcode); } // 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/book:read', CAP_PROHIBIT, $studentrole->id, $context->id); accesslib_clear_all_caches_for_unit_testing(); try { mod_book_external::view_book($book->id, 0); $this->fail('Exception expected due to missing capability.'); } catch (moodle_exception $e) { $this->assertEquals('nopermissions', $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 get databases by courses */ public function test_mod_data_get_databases_by_courses() { global $DB; $this->resetAfterTest(true); // Create users. $student = self::getDataGenerator()->create_user(); $teacher = self::getDataGenerator()->create_user(); // Set to the student user. self::setUser($student); // Create courses to add the modules. $course1 = self::getDataGenerator()->create_course(); $course2 = self::getDataGenerator()->create_course(); // First database. $record = new stdClass(); $record->introformat = FORMAT_HTML; $record->course = $course1->id; $database1 = self::getDataGenerator()->create_module('data', $record); // Second database. $record = new stdClass(); $record->introformat = FORMAT_HTML; $record->course = $course2->id; $database2 = self::getDataGenerator()->create_module('data', $record); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); // Users enrolments. $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id, 'manual'); $this->getDataGenerator()->enrol_user($teacher->id, $course1->id, $teacherrole->id, 'manual'); // Execute real Moodle enrolment as we'll call unenrol() method on the instance later. $enrol = enrol_get_plugin('manual'); $enrolinstances = enrol_get_instances($course2->id, true); foreach ($enrolinstances as $courseenrolinstance) { if ($courseenrolinstance->enrol == "manual") { $instance2 = $courseenrolinstance; break; } } $enrol->enrol_user($instance2, $student->id, $studentrole->id); // Create what we expect to be returned when querying the two courses. // First for the student user. $expectedfields = array('id', 'coursemodule', 'course', 'name', 'comments', 'timeavailablefrom', 'timeavailableto', 'timeviewfrom', 'timeviewto', 'requiredentries', 'requiredentriestoview', 'intro', 'introformat'); // Add expected coursemodule. $database1->coursemodule = $database1->cmid; $database2->coursemodule = $database2->cmid; $expected1 = array(); $expected2 = array(); foreach ($expectedfields as $field) { $expected1[$field] = $database1->{$field}; $expected2[$field] = $database2->{$field}; } $expecteddatabases = array(); $expecteddatabases[] = $expected2; $expecteddatabases[] = $expected1; // Call the external function passing course ids. $result = mod_data_external::get_databases_by_courses(array($course2->id, $course1->id)); external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result); $this->assertEquals($expecteddatabases, $result['databases']); // Call the external function without passing course id. $result = mod_data_external::get_databases_by_courses(); external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result); $this->assertEquals($expecteddatabases, $result['databases']); // Unenrol user from second course and alter expected databases. $enrol->unenrol_user($instance2, $student->id); array_shift($expecteddatabases); // Call the external function without passing course id. $result = mod_data_external::get_databases_by_courses(); external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result); $this->assertEquals($expecteddatabases, $result['databases']); // Call for the second course we unenrolled the user from, expected warning. $result = mod_data_external::get_databases_by_courses(array($course2->id)); $this->assertCount(1, $result['warnings']); $this->assertEquals('2', $result['warnings'][0]['warningcode']); $this->assertEquals($course2->id, $result['warnings'][0]['itemid']); // Now, try as a teacher for getting all the additional fields. self::setUser($teacher); $additionalfields = array('maxentries', 'rssarticles', 'singletemplate', 'listtemplate', 'listtemplateheader', 'listtemplatefooter', 'addtemplate', 'rsstemplate', 'rsstitletemplate', 'csstemplate', 'jstemplate', 'asearchtemplate', 'approval', 'scale', 'assessed', 'assesstimestart', 'assesstimefinish', 'defaultsort', 'defaultsortdir', 'editany', 'notification'); foreach ($additionalfields as $field) { $expecteddatabases[0][$field] = $database1->{$field}; } $result = mod_data_external::get_databases_by_courses(); external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result); $this->assertEquals($expecteddatabases, $result['databases']); }
/** * 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); } }
/** * Test core_calendar_external::create_calendar_events */ public function test_core_create_calendar_events() { global $DB, $USER, $SITE; $this->resetAfterTest(true); $this->setAdminUser(); // Create a few stuff to test with. $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(); $record = new stdClass(); $record->courseid = $course->id; $group = $this->getDataGenerator()->create_group($record); $prevcount = $DB->count_records('event'); // Let's create a few events. $events = array(array('name' => 'site', 'courseid' => $SITE->id, 'eventtype' => 'site'), array('name' => 'course', 'courseid' => $course->id, 'eventtype' => 'course', 'repeats' => 2), array('name' => 'group', 'courseid' => $course->id, 'groupid' => $group->id, 'eventtype' => 'group'), array('name' => 'user')); $eventsret = core_calendar_external::create_calendar_events($events); $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret); // Check to see if things were created properly. $aftercount = $DB->count_records('event'); $this->assertEquals($prevcount + 5, $aftercount); $this->assertEquals(5, count($eventsret['events'])); $this->assertEquals(0, count($eventsret['warnings'])); $sitecontext = context_system::instance(); $coursecontext = context_course::instance($course->id); $this->setUser($user); $prevcount = $aftercount; $events = array(array('name' => 'course', 'courseid' => $course->id, 'eventtype' => 'course', 'repeats' => 2), array('name' => 'group', 'courseid' => $course->id, 'groupid' => $group->id, 'eventtype' => 'group'), array('name' => 'user')); $role = $DB->get_record('role', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id); groups_add_member($group, $user); $this->assignUserCapability('moodle/calendar:manageentries', $coursecontext->id, $role->id); $this->assignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id); $eventsret = core_calendar_external::create_calendar_events($events); $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret); // Check to see if things were created properly. $aftercount = $DB->count_records('event'); $this->assertEquals($prevcount + 4, $aftercount); $this->assertEquals(4, count($eventsret['events'])); $this->assertEquals(0, count($eventsret['warnings'])); // Check to see nothing was created without proper permission. $this->setGuestUser(); $prevcount = $DB->count_records('event'); $eventsret = core_calendar_external::create_calendar_events($events); $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret); $aftercount = $DB->count_records('event'); $this->assertEquals($prevcount, $aftercount); $this->assertEquals(0, count($eventsret['events'])); $this->assertEquals(3, count($eventsret['warnings'])); $this->setUser($user); $this->unassignUserCapability('moodle/calendar:manageentries', $coursecontext->id, $role->id); $this->unassignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id); $prevcount = $DB->count_records('event'); $eventsret = core_calendar_external::create_calendar_events($events); $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret); $aftercount = $DB->count_records('event'); $this->assertEquals($prevcount + 1, $aftercount); // User event. $this->assertEquals(1, count($eventsret['events'])); $this->assertEquals(2, count($eventsret['warnings'])); }