Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 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 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
 
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
 
159
        // Return all the built cases.
160
        return $cases;
161
    }
162
}