Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   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.
   8  //
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Unit tests for current_language() in moodlelib.php.
  19   *
  20   * @package   core
  21   * @category  test
  22   * @copyright 2022 The Open University
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core;
  27  
  28  use moodle_page;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  /**
  33   * Unit tests for current_language() in moodlelib.php.
  34   *
  35   * @copyright 2022 The Open University
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   * @covers    ::current_language
  38   */
  39  class moodlelib_current_language_test extends \advanced_testcase {
  40  
  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)']);
  45  
  46          set_config('lang', 'en_ar');
  47  
  48          $this->assertEquals('en_ar', current_language());
  49  
  50          testable_string_manager_for_current_language_tests::reset_installed_languages_override();
  51      }
  52  
  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']);
  57  
  58          set_config('lang', 'en_ar');
  59          $this->setUser($this->getDataGenerator()->create_user(['lang' => 'fr']));
  60  
  61          $this->assertEquals('fr', current_language());
  62  
  63          testable_string_manager_for_current_language_tests::reset_installed_languages_override();
  64      }
  65  
  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']);
  70  
  71          set_config('lang', 'en_ar');
  72          $this->setUser($this->getDataGenerator()->create_user(['lang' => 'fr']));
  73          force_current_language('en');
  74  
  75          $this->assertEquals('en', current_language());
  76      }
  77  
  78      public function test_current_language_course_setting(): void {
  79          global $PAGE;
  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']);
  83  
  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']));
  88  
  89          $this->assertEquals('de', current_language());
  90  
  91          testable_string_manager_for_current_language_tests::reset_installed_languages_override();
  92      }
  93  
  94      public function test_current_language_in_course_no_lang_set(): void {
  95          global $PAGE;
  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']);
  99  
 100          set_config('lang', 'en_ar');
 101          $PAGE = new moodle_page();
 102          $PAGE->set_course($this->getDataGenerator()->create_course());
 103  
 104          $this->assertEquals('en_ar', current_language());
 105  
 106          testable_string_manager_for_current_language_tests::reset_installed_languages_override();
 107      }
 108  
 109      public function test_current_language_activity_setting(): void {
 110          global $PAGE;
 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']);
 114  
 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);
 119  
 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);
 124  
 125          $this->assertEquals('en', current_language());
 126  
 127          testable_string_manager_for_current_language_tests::reset_installed_languages_override();
 128      }
 129  
 130      public function test_current_language_activity_setting_not_set(): void {
 131          global $PAGE;
 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']);
 135  
 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);
 140  
 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);
 145  
 146          $this->assertEquals('de', current_language());
 147  
 148          testable_string_manager_for_current_language_tests::reset_installed_languages_override();
 149      }
 150  }
 151  
 152  
 153  /**
 154   * Test helper class for test which need Moodle to think there are other languages installed.
 155   *
 156   * @copyright 2022 The Open University
 157   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 158   */
 159  class testable_string_manager_for_current_language_tests extends \core_string_manager_standard {
 160  
 161      /** @var array $installedlanguages list of languages which we want to pretend are installed. */
 162      protected $installedlanguages;
 163  
 164      /**
 165       * Start pretending that the list of installed languages is other than what it is.
 166       *
 167       * You need to pass in an array like ['en' => 'English', 'fr' => 'French'].
 168       *
 169       * @param array $installedlanguages the list of languages to assume are installed.
 170       */
 171      public static function set_fake_list_of_installed_languages(array $installedlanguages): void {
 172          global $CFG;
 173  
 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;
 178  
 179          // Reset the setting we overrode.
 180          unset($CFG->config_php_settings['customstringmanager']);
 181          if ($oldsetting) {
 182              $CFG->config_php_settings['customstringmanager'] = $oldsetting;
 183          }
 184      }
 185  
 186      /**
 187       * Must be called at the end of any test which called set_fake_list_of_installed_languages to reset things.
 188       */
 189      public static function reset_installed_languages_override(): void {
 190          get_string_manager(true);
 191      }
 192  
 193      public function get_list_of_translations($returnall = false) {
 194          return $this->installedlanguages;
 195      }
 196  }