MDL-69772 lang: Ignore misconfigured allcountrycodes filter

If the allcountrycodes filter contains only invalid values, ignore the
whole filter setting and make get_list_of_countries() return the full
list of all known countries, rather than empty list.
This commit is contained in:
David Mudrák
2020-09-24 18:14:12 +02:00
parent ccd4ef8ddd
commit 11e5003bd6
2 changed files with 56 additions and 1 deletions
+5 -1
View File
@@ -427,6 +427,7 @@ class core_string_manager_standard implements core_string_manager {
$countries = $this->load_component_strings('core_countries', $lang);
core_collator::asort($countries);
if (!$returnall and !empty($CFG->allcountrycodes)) {
$enabled = explode(',', $CFG->allcountrycodes);
$return = array();
@@ -435,7 +436,10 @@ class core_string_manager_standard implements core_string_manager {
$return[$c] = $countries[$c];
}
}
return $return;
if (!empty($return)) {
return $return;
}
}
return $countries;
@@ -143,6 +143,57 @@ class core_string_manager_standard_testcase extends advanced_testcase {
set_config('langlist', '');
get_string_manager(true);
}
/**
* Test {@see core_string_manager_standard::get_list_of_countries()} under different conditions.
*/
public function test_get_list_of_countries() {
$this->resetAfterTest();
$stringman = get_string_manager();
$countries = $stringman->get_list_of_countries(true);
$this->assertIsArray($countries);
$this->assertArrayHasKey('AU', $countries);
$this->assertArrayHasKey('BE', $countries);
$this->assertArrayHasKey('CZ', $countries);
$this->assertArrayHasKey('ES', $countries);
$this->assertGreaterThan(4, count($countries));
set_config('allcountrycodes', '');
$countries = $stringman->get_list_of_countries(false);
$this->assertArrayHasKey('AU', $countries);
$this->assertArrayHasKey('BE', $countries);
$this->assertArrayHasKey('CZ', $countries);
$this->assertArrayHasKey('ES', $countries);
$this->assertGreaterThan(4, count($countries));
set_config('allcountrycodes', 'CZ,BE');
$countries = $stringman->get_list_of_countries(true);
$this->assertArrayHasKey('AU', $countries);
$this->assertArrayHasKey('BE', $countries);
$this->assertArrayHasKey('CZ', $countries);
$this->assertArrayHasKey('ES', $countries);
$this->assertGreaterThan(4, count($countries));
$countries = $stringman->get_list_of_countries(false);
$this->assertEquals(2, count($countries));
$this->assertArrayHasKey('BE', $countries);
$this->assertArrayHasKey('CZ', $countries);
set_config('allcountrycodes', 'CZ,UVWXYZ');
$countries = $stringman->get_list_of_countries();
$this->assertArrayHasKey('CZ', $countries);
$this->assertEquals(1, count($countries));
set_config('allcountrycodes', 'UVWXYZ');
$countries = $stringman->get_list_of_countries();
$this->assertArrayHasKey('AU', $countries);
$this->assertArrayHasKey('BE', $countries);
$this->assertArrayHasKey('CZ', $countries);
$this->assertArrayHasKey('ES', $countries);
$this->assertGreaterThan(4, count($countries));
}
}
/**