Skip to content

Commit

Permalink
MDL-69788 admin: validate language menu configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Oct 21, 2020
1 parent f743468 commit 9f4b10f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
34 changes: 34 additions & 0 deletions admin/tests/behat/language_settings.feature
@@ -0,0 +1,34 @@
@core @core_admin
Feature: Configure language settings for the site
In order to configure language settings for the site
As an admin
I want to set language settings relevant to my site users

Scenario: Set languages on language menu
Given I log in as "admin"
And I navigate to "Language > Language settings" in site administration
When I set the field "Languages on language menu" to "en"
And I press "Save changes"
Then I should not see "Invalid language code"

Scenario: Reset languages on language menu
Given I log in as "admin"
And I navigate to "Language > Language settings" in site administration
When I set the field "Languages on language menu" to ""
And I press "Save changes"
Then I should not see "Invalid language code"

Scenario Outline: Set languages on language menu with invalid language
Given I log in as "admin"
And I navigate to "Language > Language settings" in site administration
When I set the field "Languages on language menu" to "<fieldvalue>"
And I press "Save changes"
Then I should see "Invalid language code: <invalidlang>"
Examples:
| fieldvalue | invalidlang |
| xx | xx |
| xx\|Bad | xx |
| en,qq | qq |
| en,qq\|Bad | qq |
| en$$ | en$$ |
| en$$\|Bad | en$$ |
1 change: 1 addition & 0 deletions lang/en/error.php
Expand Up @@ -339,6 +339,7 @@
$string['invalidipformat'] = 'Invalid IP address format';
$string['invaliditemid'] = 'Incorrect item ID';
$string['invalidkey'] = 'Incorrect key';
$string['invalidlanguagecode'] = 'Invalid language code: {$a}';
$string['invalidlegacy'] = 'Incorrect legacy role definition for type: {$a}';
$string['invalidmd5'] = 'The check variable was wrong - try again';
$string['invalidmode'] = 'Invalid mode ({$a})';
Expand Down
30 changes: 30 additions & 0 deletions lib/adminlib.php
Expand Up @@ -4864,6 +4864,36 @@ public function __construct() {
parent::__construct('langlist', get_string('langlist', 'admin'), get_string('configlanglist', 'admin'), '', PARAM_NOTAGS);
}

/**
* Validate that each language identifier exists on the site
*
* @param string $data
* @return bool|string True if validation successful, otherwise error string
*/
public function validate($data) {
$parentcheck = parent::validate($data);
if ($parentcheck !== true) {
return $parentcheck;
}

if ($data === '') {
return true;
}

// Normalize language identifiers.
$langcodes = array_map('trim', explode(',', $data));
foreach ($langcodes as $langcode) {
// If the langcode contains optional alias, split it out.
[$langcode, ] = preg_split('/\s*\|\s*/', $langcode, 2);

if (!get_string_manager()->translation_exists($langcode)) {
return get_string('invalidlanguagecode', 'error', $langcode);
}
}

return true;
}

/**
* Save the new setting
*
Expand Down

0 comments on commit 9f4b10f

Please sign in to comment.