Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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  namespace tool_langimport;
  18  
  19  /**
  20   * Tests for \tool_langimport\locale class.
  21   *
  22   * @package    tool_langimport
  23   * @category   test
  24   * @coversDefaultClass \tool_langimport\locale
  25   * @copyright  2018 Université Rennes 2 {@link https://www.univ-rennes2.fr}
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  class locale_test extends \advanced_testcase {
  29      /**
  30       * Test that \tool_langimport\locale::check_locale_availability() works as expected.
  31       *
  32       * @covers ::check_locale_availability
  33       * @return void
  34       */
  35      public function test_check_locale_availability() {
  36          // Create a mock of set_locale() method to simulate :
  37          // - first setlocale() call which backup current locale
  38          // - second setlocale() call which try to set new 'es' locale
  39          // - third setlocale() call which restore locale.
  40          $mock = $this->getMockBuilder(locale::class)
  41              ->onlyMethods(['set_locale'])
  42              ->getMock();
  43          $mock->method('set_locale')->will($this->onConsecutiveCalls('en', 'es', 'en'));
  44  
  45          // Test what happen when locale is available on system.
  46          $result = $mock->check_locale_availability('en');
  47          $this->assertTrue($result);
  48  
  49          // Create a mock of set_locale() method to simulate :
  50          // - first setlocale() call which backup current locale
  51          // - second setlocale() call which fail to set new locale
  52          // - third setlocale() call which restore locale.
  53          $mock = $this->getMockBuilder(locale::class)
  54              ->onlyMethods(['set_locale'])
  55              ->getMock();
  56          $mock->method('set_locale')->will($this->onConsecutiveCalls('en', false, 'en'));
  57  
  58          // Test what happen when locale is not available on system.
  59          $result = $mock->check_locale_availability('en');
  60          $this->assertFalse($result);
  61  
  62          // Test an invalid parameter.
  63          $locale = new locale();
  64          $this->expectException(\coding_exception::class);
  65          $locale->check_locale_availability('');
  66      }
  67  
  68      /**
  69       * Test \tool_langimport\locale::set_locale() own logic.
  70       *
  71       * We have to explicitly test set_locale() own logic and results,
  72       * that effectively sets the current locale, so we need to restore
  73       * the original locale after every test (ugly, from a purist unit test
  74       * point of view, but needed).
  75       *
  76       * @dataProvider set_locale_provider
  77       * @covers ::set_locale
  78       *
  79       * @param string $set locale string to be set.
  80       * @param string $ret expected results returned after setting the locale.
  81       */
  82      public function test_set_locale(string $set, string $ret) {
  83          // Make set_locale() public.
  84          $loc = new locale();
  85          $rc = new \ReflectionClass(locale::class);
  86          $rm = $rc->getMethod('set_locale');
  87          $rm->setAccessible(true);
  88  
  89          // Capture current locale for later restore (funnily, using the set_locale() method itself.
  90          $originallocale = $rm->invokeArgs($loc, [LC_ALL, 0]);
  91  
  92          // Assert we get the locale defined as expected.
  93          $this->assertEquals($ret, $rm->invokeArgs($loc, [LC_ALL, $set]));
  94  
  95          // We have finished, restore the original locale, so this doesn't affect other tests at distance.
  96          // (again, funnily, using the very same set_locale() method).
  97          $rm->invokeArgs($loc, [LC_ALL, $originallocale]);
  98  
  99      }
 100  
 101      /**
 102       * Data provider for test_set_locale().
 103       *
 104       * Provides a locale to be set (as 'set') and a expected return value (as 'ret'). Note that
 105       * some of the locales are OS dependent, so only the ones matching the OS will be provided.
 106       *
 107       * We make extensive use of the en_AU.UTF-8/English_Australia.1252 locale that is mandatory to
 108       * be installed in any system running PHPUnit tests.
 109       */
 110      public function set_locale_provider(): array {
 111          // Let's list the allowed categories by OS.
 112          $bsdallowed = ['LC_COLLATE', 'LC_CTYPE', 'LC_MESSAGES', 'LC_MONETARY', 'LC_NUMERIC', 'LC_TIME'];
 113          $winallowed = ['LC_COLLATE', 'LC_CTYPE', 'LC_MONETARY', 'LC_NUMERIC', 'LC_TIME'];
 114          $linuxallowed = [
 115              'LC_COLLATE', 'LC_CTYPE', 'LC_MESSAGES', 'LC_MONETARY', 'LC_NUMERIC', 'LC_TIME',
 116              'LC_PAPER', 'LC_NAME', 'LC_ADDRESS', 'LC_TELEPHONE', 'LC_MEASUREMENT', 'LC_IDENTIFICATION'
 117          ];
 118  
 119          // The base locale name is also OS dependent.
 120          $baselocale = get_string('locale', 'langconfig');
 121          if (PHP_OS_FAMILY === 'Windows') {
 122              $baselocale = get_string('localewin', 'langconfig');
 123          }
 124  
 125          // Here we'll go accumulating cases to be provided.
 126          $cases = [];
 127  
 128          // First, the simplest case, just pass a locale name, without categories.
 129          $cases['rawlocale'] = [
 130              'set' => $baselocale,
 131              'ret' => $baselocale,
 132          ];
 133  
 134          // Now, let's fill ALL LC categories, we should get back the locale name if all them are set with same value.
 135          // Note that this case is the one that, under Linux only, covers the changes performed to the set_locale() method.
 136          // Pick the correct categories depending on the OS.
 137          $oscategories = $bsdallowed; // Default to BSD/Dawrwin ones because they are the standard 6 supported by PHP.
 138          if (PHP_OS_FAMILY === 'Windows') {
 139              $oscategories = $winallowed;
 140          } else if (PHP_OS_FAMILY === 'Linux') {
 141              $oscategories = $linuxallowed;
 142          }
 143  
 144          $localestr = '';
 145          foreach ($oscategories as $category) {
 146              // Format is different by OS too, so let build the string conditionally.
 147              if (PHP_OS_FAMILY === 'BSD' || PHP_OS_FAMILY === 'Darwin') {
 148                  // BSD uses slashes (/) separated list of the 6 values in exact order.
 149                  $localestr .= '/' . $baselocale;
 150              } else {
 151                  // Linux/Windows use semicolon (;) separated list of category=value pairs.
 152                  $localestr .= ';' . $category . '=' . $baselocale;
 153              }
 154          }
 155          $cases['allcategories'] = [
 156              'set' => trim($localestr, ';/'),
 157              'ret' => $baselocale,
 158          ];
 159  
 160          // Return all the built cases.
 161          return $cases;
 162      }
 163  }