/** * Test get_component_strings */ public function test_get_component_strings() { global $USER; $this->resetAfterTest(true); $stringmanager = get_string_manager(); $wsstrings = $stringmanager->load_component_strings('webservice', current_language()); $componentstrings = core_external::get_component_strings('webservice'); $this->assertEquals(count($componentstrings), count($wsstrings)); foreach ($wsstrings as $name => $string) { $this->assertEquals($string, $componentstrings[$name]); } }
/** * 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))); }
/** * Test web service updating section name */ public function test_update_inplace_editable() { global $CFG, $DB, $PAGE; require_once $CFG->dirroot . '/lib/external/externallib.php'; $this->resetAfterTest(); $user = $this->getDataGenerator()->create_user(); $this->setUser($user); $course = $this->getDataGenerator()->create_course(array('numsections' => 5, 'format' => 'weeks'), array('createsections' => true)); $section = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 2)); // Call webservice without necessary permissions. try { core_external::update_inplace_editable('format_weeks', 'sectionname', $section->id, 'New section name'); $this->fail('Exception expected'); } catch (moodle_exception $e) { $this->assertEquals('Course or activity not accessible. (Not enrolled)', $e->getMessage()); } // Change to teacher and make sure that section name can be updated using web service update_inplace_editable(). $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); $this->getDataGenerator()->enrol_user($user->id, $course->id, $teacherrole->id); $res = core_external::update_inplace_editable('format_weeks', 'sectionname', $section->id, 'New section name'); $res = external_api::clean_returnvalue(core_external::update_inplace_editable_returns(), $res); $this->assertEquals('New section name', $res['value']); $this->assertEquals('New section name', $DB->get_field('course_sections', 'name', array('id' => $section->id))); }
/** * Return multiple call to core get_string() * * @param array $strings strings to translate * @return array * * @since Moodle 2.4 */ public static function get_strings($strings) { $params = self::validate_parameters(self::get_strings_parameters(), array('strings' => $strings)); $stringmanager = get_string_manager(); $translatedstrings = array(); foreach ($params['strings'] as $string) { if (!empty($string['lang'])) { $lang = $string['lang']; } else { $lang = current_language(); } $translatedstrings[] = array('stringid' => $string['stringid'], 'component' => $string['component'], 'lang' => $lang, 'string' => $stringmanager->get_string($string['stringid'], $string['component'], core_external::format_string_parameters($string['stringparams']), $lang)); } return $translatedstrings; }
/** * Test get_component_strings */ public function test_get_component_strings() { global $USER; $this->resetAfterTest(true); $stringmanager = get_string_manager(); $wsstrings = $stringmanager->load_component_strings('webservice', current_language()); $componentstrings = core_external::get_component_strings('webservice'); // We need to execute the return values cleaning process to simulate the web service server. $componentstrings = external_api::clean_returnvalue(core_external::get_component_strings_returns(), $componentstrings); $this->assertEquals(count($componentstrings), count($wsstrings)); foreach ($componentstrings as $string) { $this->assertEquals($string['string'], $wsstrings[$string['stringid']]); } }
/** * Test update_inplace_editable() */ public function test_update_inplace_editable() { $this->resetAfterTest(true); // Call service for component that does not have inplace_editable callback. try { core_external::update_inplace_editable('tool_log', 'itemtype', 1, 'newvalue'); $this->fail('Exception expected'); } catch (moodle_exception $e) { $this->assertEquals('Error calling update processor', $e->getMessage()); } // This is a very basic test for the return value of the external function. // More detailed test for tag updating can be found in core_tag component. $this->setAdminUser(); $tag = $this->getDataGenerator()->create_tag(); $res = core_external::update_inplace_editable('core_tag', 'tagname', $tag->id, 'new tag name'); $res = external_api::clean_returnvalue(core_external::update_inplace_editable_returns(), $res); $this->assertEquals('new tag name', $res['value']); }
/** * Test update_inplace_editable() */ public function test_update_inplace_editable() { global $CFG, $DB, $PAGE; require_once $CFG->dirroot . '/lib/external/externallib.php'; $this->resetAfterTest(true); $tag = $this->getDataGenerator()->create_tag(); $this->setUser($this->getDataGenerator()->create_user()); // Call service for core_tag component without necessary permissions. try { core_external::update_inplace_editable('core_tag', 'tagname', $tag->id, 'new tag name'); $this->fail('Exception expected'); } catch (moodle_exception $e) { $this->assertEquals('Sorry, but you do not currently have permissions to do that (Manage all tags)', $e->getMessage()); } // Change to admin user and make sure that tag name can be updated using web service update_inplace_editable(). $this->setAdminUser(); $res = core_external::update_inplace_editable('core_tag', 'tagname', $tag->id, 'New tag name'); $res = external_api::clean_returnvalue(core_external::update_inplace_editable_returns(), $res); $this->assertEquals('New tag name', $res['value']); $this->assertEquals('New tag name', $DB->get_field('tag', 'rawname', array('id' => $tag->id))); // Call callback core_tag_inplace_editable() directly. $tmpl = component_callback('core_tag', 'inplace_editable', array('tagname', $tag->id, 'Rename me again')); $this->assertInstanceOf('core\\output\\inplace_editable', $tmpl); $res = $tmpl->export_for_template($PAGE->get_renderer('core')); $this->assertEquals('Rename me again', $res['value']); $this->assertEquals('Rename me again', $DB->get_field('tag', 'rawname', array('id' => $tag->id))); }