From f00ad20a6f75845b4e4182cf0f176a85d7c78d4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Wed, 16 Dec 2020 20:50:06 +0100 Subject: [PATCH] MDL-70486 output: Improve check for block region name string existence * The fact that non-existing strings are returned in certain format starting with the bracket, is kind of side debugging effect. The value is really undefined and should not be relied upon. * The string could actually exist and be worded so that its first character is also the square bracket. --- lib/outputlib.php | 20 ++++++++++---------- lib/tests/theme_config_test.php | 12 ++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/outputlib.php b/lib/outputlib.php index 9f533ca84d4..bfc93a0d6cc 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -2451,22 +2451,22 @@ class theme_config { * @return string */ protected function get_region_name($region, $theme) { - $regionstring = get_string('region-' . $region, 'theme_' . $theme); - // A name exists in this theme, so use it - if (substr($regionstring, 0, 1) != '[') { - return $regionstring; + + $stringman = get_string_manager(); + + // Check if the name is defined in the theme. + if ($stringman->string_exists('region-' . $region, 'theme_' . $theme)) { + return get_string('region-' . $region, 'theme_' . $theme); } - // Otherwise, try to find one elsewhere - // Check parents, if any + // Check the theme parents. foreach ($this->parents as $parentthemename) { - $regionstring = get_string('region-' . $region, 'theme_' . $parentthemename); - if (substr($regionstring, 0, 1) != '[') { - return $regionstring; + if ($stringman->string_exists('region-' . $region, 'theme_' . $parentthemename)) { + return get_string('region-' . $region, 'theme_' . $parentthemename); } } - // Last resort, try the boost theme for names + // Last resort, try the boost theme for names. return get_string('region-' . $region, 'theme_boost'); } diff --git a/lib/tests/theme_config_test.php b/lib/tests/theme_config_test.php index 35f3df73c63..3308d079cfd 100644 --- a/lib/tests/theme_config_test.php +++ b/lib/tests/theme_config_test.php @@ -213,4 +213,16 @@ class core_theme_config_testcase extends advanced_testcase { $this->assertEquals($cssexpected, $cssactual); } + + /** + * Test that {@see theme_config::get_all_block_regions()} returns localised list of region names. + */ + public function test_get_all_block_regions() { + $this->resetAfterTest(); + + $theme = theme_config::load(theme_config::DEFAULT_THEME); + $regions = $theme->get_all_block_regions(); + + $this->assertEquals('Right', $regions['side-pre']); + } }