Skip to content

Commit

Permalink
MDL-53779 external: get_string and get_strings use PARAM_RAW
Browse files Browse the repository at this point in the history
For the string parameters and translated strings.
  • Loading branch information
Frederic Massart committed Apr 12, 2016
1 parent 98eb681 commit fefe867
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/external/externallib.php
Expand Up @@ -102,7 +102,7 @@ public static function get_string_parameters() {
new external_single_structure(array(
'name' => new external_value(PARAM_ALPHANUMEXT, 'param name
- if the string expect only one $a parameter then don\'t send this field, just send the value.', VALUE_OPTIONAL),
'value' => new external_value(PARAM_TEXT,'param value'))),
'value' => new external_value(PARAM_RAW,'param value'))),
'the definition of a string param (i.e. {$a->name})', VALUE_DEFAULT, array()
)
)
Expand Down Expand Up @@ -134,7 +134,7 @@ public static function get_string($stringid, $component = 'moodle', $lang = null
* @since Moodle 2.4
*/
public static function get_string_returns() {
return new external_value(PARAM_TEXT, 'translated string');
return new external_value(PARAM_RAW, 'translated string');
}

/**
Expand All @@ -154,7 +154,7 @@ public static function get_strings_parameters() {
new external_single_structure(array(
'name' => new external_value(PARAM_ALPHANUMEXT, 'param name
- if the string expect only one $a parameter then don\'t send this field, just send the value.', VALUE_OPTIONAL),
'value' => new external_value(PARAM_TEXT, 'param value'))),
'value' => new external_value(PARAM_RAW, 'param value'))),
'the definition of a string param (i.e. {$a->name})', VALUE_DEFAULT, array()
))
)
Expand Down Expand Up @@ -219,7 +219,7 @@ public static function get_strings_returns() {
'stringid' => new external_value(PARAM_STRINGID, 'string id'),
'component' => new external_value(PARAM_COMPONENT, 'string component'),
'lang' => new external_value(PARAM_LANG, 'lang'),
'string' => new external_value(PARAM_TEXT, 'translated string'))
'string' => new external_value(PARAM_RAW, 'translated string'))
));
}

Expand Down
43 changes: 43 additions & 0 deletions lib/external/tests/external_test.php
Expand Up @@ -78,6 +78,26 @@ public function test_get_string() {
array('name' => 'id', 'value' => $service->id)));
}

/**
* Test get_string with HTML.
*/
public function test_get_string_containing_html() {
$result = core_external::get_string('registrationinfo');
$actual = external_api::clean_returnvalue(core_external::get_string_returns(), $result);
$expected = get_string('registrationinfo', 'moodle');
$this->assertSame($expected, $actual);
}

/**
* Test get_string with arguments containing HTML.
*/
public function test_get_string_with_args_containing_html() {
$result = core_external::get_string('added', 'moodle', null, [['value' => '<strong>Test</strong>']]);
$actual = external_api::clean_returnvalue(core_external::get_string_returns(), $result);
$expected = get_string('added', 'moodle', '<strong>Test</strong>');
$this->assertSame($expected, $actual);
}

/**
* Test get_strings
*/
Expand Down Expand Up @@ -114,6 +134,29 @@ public function test_get_strings() {
}
}

/**
* Test get_strings with HTML.
*/
public function test_get_strings_containing_html() {
$result = core_external::get_strings([['stringid' => 'registrationinfo'], ['stringid' => 'loginaspasswordexplain']]);
$actual = external_api::clean_returnvalue(core_external::get_strings_returns(), $result);
$this->assertSame(get_string('registrationinfo', 'moodle'), $actual[0]['string']);
$this->assertSame(get_string('loginaspasswordexplain', 'moodle'), $actual[1]['string']);
}

/**
* Test get_strings with arguments containing HTML.
*/
public function test_get_strings_with_args_containing_html() {
$result = core_external::get_strings([
['stringid' => 'added', 'stringparams' => [['value' => '<strong>Test</strong>']]],
['stringid' => 'loggedinas', 'stringparams' => [['value' => '<strong>Test</strong>']]]]
);
$actual = external_api::clean_returnvalue(core_external::get_strings_returns(), $result);
$this->assertSame(get_string('added', 'moodle', '<strong>Test</strong>'), $actual[0]['string']);
$this->assertSame(get_string('loggedinas', 'moodle', '<strong>Test</strong>'), $actual[1]['string']);
}

/**
* Test get_component_strings
*/
Expand Down

0 comments on commit fefe867

Please sign in to comment.