11 |
efrain |
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 core;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Tests for \core\locale class.
|
|
|
21 |
*
|
|
|
22 |
* @package core
|
|
|
23 |
* @category test
|
|
|
24 |
* @copyright 2018 Université Rennes 2 {@link https://www.univ-rennes2.fr}
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
* @covers \core\locale
|
|
|
27 |
*/
|
|
|
28 |
final class locale_test extends \advanced_testcase {
|
|
|
29 |
/** @var string Locale */
|
|
|
30 |
protected string $locale;
|
|
|
31 |
|
|
|
32 |
#[\Override]
|
|
|
33 |
public function setUp(): void {
|
|
|
34 |
parent::setUp();
|
|
|
35 |
$this->locale = \core\locale::get_locale();
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
#[\Override]
|
|
|
39 |
public function tearDown(): void {
|
|
|
40 |
parent::tearDown();
|
|
|
41 |
\core\locale::set_locale(LC_ALL, $this->locale);
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Test \tool_langimport\locale::set_locale() own logic.
|
|
|
46 |
*
|
|
|
47 |
* We have to explicitly test set_locale() own logic and results,
|
|
|
48 |
* that effectively sets the current locale, so we need to restore
|
|
|
49 |
* the original locale after every test (ugly, from a purist unit test
|
|
|
50 |
* point of view, but needed).
|
|
|
51 |
*
|
|
|
52 |
* @dataProvider set_locale_provider
|
|
|
53 |
* @param string $set locale string to be set.
|
|
|
54 |
* @param string $ret expected results returned after setting the locale.
|
|
|
55 |
*/
|
|
|
56 |
public function test_set_locale(string $set, string $ret): void {
|
|
|
57 |
// Capture current locale for later restore (funnily, using the set_locale() method itself.
|
|
|
58 |
$originallocale = locale::set_locale(LC_ALL, 0);
|
|
|
59 |
|
|
|
60 |
// Assert we get the locale defined as expected.
|
|
|
61 |
$this->assertEquals($ret, locale::set_locale(LC_ALL, $set));
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Data provider for test_set_locale().
|
|
|
66 |
*
|
|
|
67 |
* Provides a locale to be set (as 'set') and a expected return value (as 'ret'). Note that
|
|
|
68 |
* some of the locales are OS dependent, so only the ones matching the OS will be provided.
|
|
|
69 |
*
|
|
|
70 |
* We make extensive use of the en_AU.UTF-8/English_Australia.1252 locale that is mandatory to
|
|
|
71 |
* be installed in any system running PHPUnit tests.
|
|
|
72 |
*/
|
|
|
73 |
public static function set_locale_provider(): array {
|
|
|
74 |
// Let's list the allowed categories by OS.
|
|
|
75 |
$bsdallowed = ['LC_COLLATE', 'LC_CTYPE', 'LC_MESSAGES', 'LC_MONETARY', 'LC_NUMERIC', 'LC_TIME'];
|
|
|
76 |
$winallowed = ['LC_COLLATE', 'LC_CTYPE', 'LC_MONETARY', 'LC_NUMERIC', 'LC_TIME'];
|
|
|
77 |
$linuxallowed = [
|
|
|
78 |
'LC_COLLATE', 'LC_CTYPE', 'LC_MESSAGES', 'LC_MONETARY', 'LC_NUMERIC', 'LC_TIME',
|
|
|
79 |
'LC_PAPER', 'LC_NAME', 'LC_ADDRESS', 'LC_TELEPHONE', 'LC_MEASUREMENT', 'LC_IDENTIFICATION',
|
|
|
80 |
];
|
|
|
81 |
|
|
|
82 |
// The base locale name is also OS dependent.
|
|
|
83 |
$baselocale = get_string('locale', 'langconfig');
|
|
|
84 |
if (PHP_OS_FAMILY === 'Windows') {
|
|
|
85 |
$baselocale = get_string('localewin', 'langconfig');
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
// Here we'll go accumulating cases to be provided.
|
|
|
89 |
$cases = [];
|
|
|
90 |
|
|
|
91 |
// First, the simplest case, just pass a locale name, without categories.
|
|
|
92 |
$cases['rawlocale'] = [
|
|
|
93 |
'set' => $baselocale,
|
|
|
94 |
'ret' => $baselocale,
|
|
|
95 |
];
|
|
|
96 |
|
|
|
97 |
// Now, let's fill ALL LC categories, we should get back the locale name if all them are set with same value.
|
|
|
98 |
// Note that this case is the one that, under Linux only, covers the changes performed to the set_locale() method.
|
|
|
99 |
// Pick the correct categories depending on the OS.
|
|
|
100 |
$oscategories = $bsdallowed; // Default to BSD/Dawrwin ones because they are the standard 6 supported by PHP.
|
|
|
101 |
if (PHP_OS_FAMILY === 'Windows') {
|
|
|
102 |
$oscategories = $winallowed;
|
|
|
103 |
} else if (PHP_OS_FAMILY === 'Linux') {
|
|
|
104 |
$oscategories = $linuxallowed;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
$localestr = '';
|
|
|
108 |
foreach ($oscategories as $category) {
|
|
|
109 |
// Format is different by OS too, so let build the string conditionally.
|
|
|
110 |
if (PHP_OS_FAMILY === 'BSD' || PHP_OS_FAMILY === 'Darwin') {
|
|
|
111 |
// BSD uses slashes (/) separated list of the 6 values in exact order.
|
|
|
112 |
$localestr .= '/' . $baselocale;
|
|
|
113 |
} else {
|
|
|
114 |
// Linux/Windows use semicolon (;) separated list of category=value pairs.
|
|
|
115 |
$localestr .= ';' . $category . '=' . $baselocale;
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
$cases['allcategories'] = [
|
|
|
119 |
'set' => trim($localestr, ';/'),
|
|
|
120 |
'ret' => $baselocale,
|
|
|
121 |
];
|
|
|
122 |
|
|
|
123 |
// Return all the built cases.
|
|
|
124 |
return $cases;
|
|
|
125 |
}
|
|
|
126 |
}
|