Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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