mirror of
https://github.com/moodle/moodle.git
synced 2025-01-31 04:33:13 +01:00
MDL-49240 webservices: core_get_string and friends bad lang handling
The core_get_string webservice and friends have never passed lang values properly, (so never worked) and when I fixed that I found they were passing lang parameter into the lazyload parameter.
This commit is contained in:
parent
7357af2b0c
commit
bef63c520c
12
lib/external/externallib.php
vendored
12
lib/external/externallib.php
vendored
@ -108,11 +108,12 @@ class core_external extends external_api {
|
||||
* @return string
|
||||
* @since Moodle 2.4
|
||||
*/
|
||||
public static function get_string($stringid, $component = 'moodle', $stringparams = array()) {
|
||||
public static function get_string($stringid, $component = 'moodle', $lang = null, $stringparams = array()) {
|
||||
$params = self::validate_parameters(self::get_string_parameters(),
|
||||
array('stringid'=>$stringid, 'component' => $component, 'stringparams' => $stringparams));
|
||||
array('stringid'=>$stringid, 'component' => $component, 'lang' => $lang, 'stringparams' => $stringparams));
|
||||
|
||||
return get_string($params['stringid'], $params['component'],
|
||||
$stringmanager = get_string_manager();
|
||||
return $stringmanager->get_string($params['stringid'], $params['component'],
|
||||
core_external::format_string_parameters($params['stringparams']), $params['lang']);
|
||||
}
|
||||
|
||||
@ -163,11 +164,12 @@ class core_external extends external_api {
|
||||
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'])) {
|
||||
if (!empty($string['lang'])) {
|
||||
$lang = $string['lang'];
|
||||
} else {
|
||||
$lang = current_language();
|
||||
@ -177,7 +179,7 @@ class core_external extends external_api {
|
||||
'stringid' => $string['stringid'],
|
||||
'component' => $string['component'],
|
||||
'lang' => $lang,
|
||||
'string' => get_string($string['stringid'], $string['component'],
|
||||
'string' => $stringmanager->get_string($string['stringid'], $string['component'],
|
||||
core_external::format_string_parameters($string['stringparams']), $lang));
|
||||
}
|
||||
|
||||
|
18
lib/external/tests/external_test.php
vendored
18
lib/external/tests/external_test.php
vendored
@ -41,7 +41,7 @@ class core_external_testcase extends externallib_advanced_testcase {
|
||||
$service->id = 12;
|
||||
|
||||
// String with two parameters.
|
||||
$returnedstring = core_external::get_string('addservice', 'webservice',
|
||||
$returnedstring = core_external::get_string('addservice', 'webservice', null,
|
||||
array(array('name' => 'name', 'value' => $service->name),
|
||||
array('name' => 'id', 'value' => $service->id)));
|
||||
|
||||
@ -53,7 +53,7 @@ class core_external_testcase extends externallib_advanced_testcase {
|
||||
|
||||
// String with one parameter.
|
||||
$acapname = 'A capability name';
|
||||
$returnedstring = core_external::get_string('missingrequiredcapability', 'webservice',
|
||||
$returnedstring = core_external::get_string('missingrequiredcapability', 'webservice', null,
|
||||
array(array('value' => $acapname)));
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
@ -73,7 +73,7 @@ class core_external_testcase extends externallib_advanced_testcase {
|
||||
|
||||
// String with two parameter but one is invalid (not named).
|
||||
$this->setExpectedException('moodle_exception');
|
||||
$returnedstring = core_external::get_string('addservice', 'webservice',
|
||||
$returnedstring = core_external::get_string('addservice', 'webservice', null,
|
||||
array(array('value' => $service->name),
|
||||
array('name' => 'id', 'value' => $service->id)));
|
||||
}
|
||||
@ -84,6 +84,8 @@ class core_external_testcase extends externallib_advanced_testcase {
|
||||
public function test_get_strings() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$stringmanager = get_string_manager();
|
||||
|
||||
$service = new stdClass();
|
||||
$service->name = 'Dummy Service';
|
||||
$service->id = 12;
|
||||
@ -94,16 +96,20 @@ class core_external_testcase extends externallib_advanced_testcase {
|
||||
'stringid' => 'addservice', 'component' => 'webservice',
|
||||
'stringparams' => array(array('name' => 'name', 'value' => $service->name),
|
||||
array('name' => 'id', 'value' => $service->id)
|
||||
)
|
||||
),
|
||||
'lang' => 'en'
|
||||
),
|
||||
array('stringid' => 'addaservice', 'component' => 'webservice')
|
||||
array('stringid' => 'addaservice', 'component' => 'webservice', 'lang' => 'en')
|
||||
));
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$returnedstrings = external_api::clean_returnvalue(core_external::get_strings_returns(), $returnedstrings);
|
||||
|
||||
foreach($returnedstrings as $returnedstring) {
|
||||
$corestring = get_string($returnedstring['stringid'], $returnedstring['component'], $service);
|
||||
$corestring = $stringmanager->get_string($returnedstring['stringid'],
|
||||
$returnedstring['component'],
|
||||
$service,
|
||||
'en');
|
||||
$this->assertSame($corestring, $returnedstring['string']);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user