2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Unit tests for current_language() in moodlelib.php.
22 * @copyright 2022 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 defined('MOODLE_INTERNAL') ||
die();
33 * Unit tests for current_language() in moodlelib.php.
35 * @copyright 2022 The Open University
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 * @covers ::current_language
39 class moodlelib_current_language_test
extends \advanced_testcase
{
41 public function test_current_language_site_default(): void
{
42 $this->resetAfterTest();
43 testable_string_manager_for_current_language_tests
::set_fake_list_of_installed_languages(
44 ['en' => 'English', 'en_ar' => 'English (pirate)']);
46 set_config('lang', 'en_ar');
48 $this->assertEquals('en_ar', current_language());
50 testable_string_manager_for_current_language_tests
::reset_installed_languages_override();
53 public function test_current_language_user_pref(): void
{
54 $this->resetAfterTest();
55 testable_string_manager_for_current_language_tests
::set_fake_list_of_installed_languages(
56 ['en' => 'English', 'en_ar' => 'English (pirate)', 'fr' => 'French']);
58 set_config('lang', 'en_ar');
59 $this->setUser($this->getDataGenerator()->create_user(['lang' => 'fr']));
61 $this->assertEquals('fr', current_language());
63 testable_string_manager_for_current_language_tests
::reset_installed_languages_override();
66 public function test_current_language_forced(): void
{
67 $this->resetAfterTest();
68 testable_string_manager_for_current_language_tests
::set_fake_list_of_installed_languages(
69 ['en' => 'English', 'en_ar' => 'English (pirate)', 'fr' => 'French', 'de' => 'German']);
71 set_config('lang', 'en_ar');
72 $this->setUser($this->getDataGenerator()->create_user(['lang' => 'fr']));
73 force_current_language('en');
75 $this->assertEquals('en', current_language());
78 public function test_current_language_course_setting(): void
{
80 $this->resetAfterTest();
81 testable_string_manager_for_current_language_tests
::set_fake_list_of_installed_languages(
82 ['en' => 'English', 'en_ar' => 'English (pirate)', 'fr' => 'French']);
84 set_config('lang', 'en_ar');
85 $this->setUser($this->getDataGenerator()->create_user(['lang' => 'fr']));
86 $PAGE = new moodle_page();
87 $PAGE->set_course($this->getDataGenerator()->create_course(['lang' => 'de']));
89 $this->assertEquals('de', current_language());
91 testable_string_manager_for_current_language_tests
::reset_installed_languages_override();
94 public function test_current_language_in_course_no_lang_set(): void
{
96 $this->resetAfterTest();
97 testable_string_manager_for_current_language_tests
::set_fake_list_of_installed_languages(
98 ['en' => 'English', 'en_ar' => 'English (pirate)', 'fr' => 'French']);
100 set_config('lang', 'en_ar');
101 $PAGE = new moodle_page();
102 $PAGE->set_course($this->getDataGenerator()->create_course());
104 $this->assertEquals('en_ar', current_language());
106 testable_string_manager_for_current_language_tests
::reset_installed_languages_override();
109 public function test_current_language_activity_setting(): void
{
111 $this->resetAfterTest();
112 testable_string_manager_for_current_language_tests
::set_fake_list_of_installed_languages(
113 ['en' => 'English', 'en_ar' => 'English (pirate)', 'fr' => 'French']);
115 $this->setAdminUser();
116 $course = $this->getDataGenerator()->create_course(['lang' => 'de']);
117 $pageactivity = $this->getDataGenerator()->create_module('page', ['course' => $course->id
, 'lang' => 'en']);
118 $cm = get_fast_modinfo($course)->get_cm($pageactivity->cmid
);
120 set_config('lang', 'en_ar');
121 $this->setUser($this->getDataGenerator()->create_user(['lang' => 'fr']));
122 $PAGE = new moodle_page();
123 $PAGE->set_cm($cm, $course, $pageactivity);
125 $this->assertEquals('en', current_language());
127 testable_string_manager_for_current_language_tests
::reset_installed_languages_override();
130 public function test_current_language_activity_setting_not_set(): void
{
132 $this->resetAfterTest();
133 testable_string_manager_for_current_language_tests
::set_fake_list_of_installed_languages(
134 ['en' => 'English', 'en_ar' => 'English (pirate)', 'fr' => 'French']);
136 $this->setAdminUser();
137 $course = $this->getDataGenerator()->create_course(['lang' => 'de']);
138 $pageactivity = $this->getDataGenerator()->create_module('page', ['course' => $course->id
]);
139 $cm = get_fast_modinfo($course)->get_cm($pageactivity->cmid
);
141 set_config('lang', 'en_ar');
142 $this->setUser($this->getDataGenerator()->create_user(['lang' => 'fr']));
143 $PAGE = new moodle_page();
144 $PAGE->set_cm($cm, $course, $pageactivity);
146 $this->assertEquals('de', current_language());
148 testable_string_manager_for_current_language_tests
::reset_installed_languages_override();
154 * Test helper class for test which need Moodle to think there are other languages installed.
156 * @copyright 2022 The Open University
157 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
159 class testable_string_manager_for_current_language_tests
extends \core_string_manager_standard
{
161 /** @var array $installedlanguages list of languages which we want to pretend are installed. */
162 protected $installedlanguages;
165 * Start pretending that the list of installed languages is other than what it is.
167 * You need to pass in an array like ['en' => 'English', 'fr' => 'French'].
169 * @param array $installedlanguages the list of languages to assume are installed.
171 public static function set_fake_list_of_installed_languages(array $installedlanguages): void
{
174 // Re-create the custom string-manager instance using this class, and force the thing we are overriding.
175 $oldsetting = $CFG->config_php_settings
['customstringmanager'] ??
null;
176 $CFG->config_php_settings
['customstringmanager'] = self
::class;
177 get_string_manager(true)->installedlanguages
= $installedlanguages;
179 // Reset the setting we overrode.
180 unset($CFG->config_php_settings
['customstringmanager']);
182 $CFG->config_php_settings
['customstringmanager'] = $oldsetting;
187 * Must be called at the end of any test which called set_fake_list_of_installed_languages to reset things.
189 public static function reset_installed_languages_override(): void
{
190 get_string_manager(true);
193 public function get_list_of_translations($returnall = false) {
194 return $this->installedlanguages
;