Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | 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
/**
18
 * Unit tests for localization support in lib/moodlelib.php
19
 *
20
 * @package     core
21
 * @category    phpunit
22
 * @copyright   2013 David Mudrak <david@moodle.com>
23
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace core;
27
 
28
use core_string_manager_standard;
29
 
30
defined('MOODLE_INTERNAL') || die();
31
 
32
global $CFG;
33
require_once($CFG->libdir.'/moodlelib.php');
34
 
35
/**
36
 * Tests for the API of the string_manager.
37
 *
38
 * Unit tests for localization support in lib/moodlelib.php
39
 *
40
 * @package   core
41
 * @category  test
42
 * @copyright 2013 David Mudrak <david@moodle.com>
43
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44
 */
1441 ariadna 45
final class string_manager_standard_test extends \advanced_testcase {
1 efrain 46
 
11 efrain 47
    public function test_string_manager_instance(): void {
1 efrain 48
        $this->resetAfterTest();
49
 
50
        $otherroot = __DIR__.'/fixtures/langtest';
51
        $stringman = testable_core_string_manager::instance($otherroot);
52
        $this->assertInstanceOf('core_string_manager', $stringman);
53
    }
54
 
11 efrain 55
    public function test_get_language_dependencies(): void {
1 efrain 56
        $this->resetAfterTest();
57
 
58
        $otherroot = __DIR__.'/fixtures/langtest';
59
        $stringman = testable_core_string_manager::instance($otherroot);
60
 
61
        // There is no parent language for 'en'.
62
        $this->assertSame(array(), $stringman->get_language_dependencies('en'));
63
        // Language with no parent language declared.
64
        $this->assertSame(array('aa'), $stringman->get_language_dependencies('aa'));
65
        // Language with parent language explicitly set to English (en < de).
66
        $this->assertSame(array('de'), $stringman->get_language_dependencies('de'));
67
        // Language dependency hierarchy (de < de_du < de_kids).
68
        $this->assertSame(array('de', 'de_du', 'de_kids'), $stringman->get_language_dependencies('de_kids'));
69
        // Language with the parent language misconfigured to itself (sd < sd).
70
        $this->assertSame(array('sd'), $stringman->get_language_dependencies('sd'));
71
        // Language with circular dependency (cda < cdb < cdc < cda).
72
        $this->assertSame(array('cda', 'cdb', 'cdc'), $stringman->get_language_dependencies('cdc'));
73
        // Orphaned language (N/A < bb).
74
        $this->assertSame(array('bb'), $stringman->get_language_dependencies('bb'));
75
        // Descendant of an orphaned language (N/A < bb < bc).
76
        $this->assertSame(array('bb', 'bc'), $stringman->get_language_dependencies('bc'));
77
    }
78
 
11 efrain 79
    public function test_deprecated_strings(): void {
1 efrain 80
        $stringman = get_string_manager();
81
 
82
        // Check non-deprecated string.
83
        $this->assertFalse($stringman->string_deprecated('hidden', 'grades'));
84
 
85
        // Check deprecated string, make sure to update once that chosen below is finally removed.
1441 ariadna 86
        $this->assertTrue($stringman->string_deprecated('importantupdates_title', 'core_admin'));
87
        $this->assertTrue($stringman->string_exists('importantupdates_title', 'core_admin'));
1 efrain 88
        $this->assertDebuggingNotCalled();
1441 ariadna 89
        $this->assertEquals(
90
            'Important update about Chat and Survey activities',
91
            get_string('importantupdates_title', 'core_admin')
92
        );
93
        $this->assertDebuggingCalled('String [importantupdates_title,core_admin] is deprecated. '.
94
            'Either you should no longer be using that string, or the string has been incorrectly deprecated, '.
95
            'in which case you should report this as a bug. '.
1 efrain 96
            'Please refer to https://moodledev.io/general/projects/api/string-deprecation');
97
    }
98
 
99
    /**
100
     * Return all deprecated strings.
101
     *
102
     * @return array
103
     */
1441 ariadna 104
    public static function get_deprecated_strings_provider(): array {
1 efrain 105
        global $CFG;
106
 
1441 ariadna 107
        $teststringman = testable_core_string_manager::instance($CFG->langotherroot, $CFG->langlocalroot, []);
1 efrain 108
        $allstrings = $teststringman->get_all_deprecated_strings();
1441 ariadna 109
        return array_map(fn ($string): array => [$string], $allstrings);
1 efrain 110
    }
111
 
112
    /**
113
     * This test is a built-in validation of deprecated.txt files in lang locations.
114
     *
115
     * It will fail if the string in the wrong format or non-existing (mistyped) string was deprecated.
116
     *
117
     * @dataProvider get_deprecated_strings_provider
118
     * @param   string      $string     The string to be tested
119
     */
11 efrain 120
    public function test_validate_deprecated_strings_files($string): void {
1 efrain 121
        $stringman = get_string_manager();
122
 
123
        $result = preg_match('/^(.*),(.*)$/', $string, $matches);
124
        $this->assertEquals(1, $result);
125
        $this->assertCount(3, $matches);
126
        $this->assertEquals($matches[2], clean_param($matches[2], PARAM_COMPONENT),
127
            "Component name {$string} appearing in one of the lang/en/deprecated.txt files does not have correct syntax");
128
 
129
        list($pluginttype, $pluginname) = \core_component::normalize_component($matches[2]);
130
        $normcomponent = $pluginname ? ($pluginttype . '_' . $pluginname) : $pluginttype;
131
        $this->assertEquals($normcomponent, $matches[2],
132
            'String "'.$string.'" appearing in one of the lang/en/deprecated.txt files does not have normalised component name');
133
 
134
        $this->assertTrue($stringman->string_exists($matches[1], $matches[2]),
135
            "String {$string} appearing in one of the lang/en/deprecated.txt files does not exist");
136
    }
137
 
138
    /**
139
     * Test for $CFG->langlist (without installation of additional languages)
140
     */
11 efrain 141
    public function test_get_list_of_translations(): void {
1 efrain 142
        $this->resetAfterTest();
143
        $stringman = get_string_manager();
144
 
145
        $this->assertEquals(['en' => 'English ‎(en)‎'], $stringman->get_list_of_translations());
146
 
147
        set_config('langlist', 'en|En');
148
        get_string_manager(true);
149
        $stringman = get_string_manager();
150
 
151
        $this->assertEquals(['en' => 'En'], $stringman->get_list_of_translations());
152
 
153
        // Set invalid config, ensure original list is returned.
154
        set_config('langlist', 'xx');
155
        $this->assertEquals(['en' => 'English ‎(en)‎'], get_string_manager(true)->get_list_of_translations());
156
 
157
        set_config('langlist', 'xx,en|En');
158
        $this->assertEquals(['en' => 'En'], get_string_manager(true)->get_list_of_translations());
159
 
160
        set_config('langlist', '');
161
        get_string_manager(true);
162
    }
163
 
164
    /**
165
     * Test {@see core_string_manager_standard::get_list_of_countries()} under different conditions.
166
     */
11 efrain 167
    public function test_get_list_of_countries(): void {
1 efrain 168
 
169
        $this->resetAfterTest();
170
        $stringman = get_string_manager();
171
 
172
        $countries = $stringman->get_list_of_countries(true);
173
        $this->assertIsArray($countries);
174
        $this->assertArrayHasKey('AU', $countries);
175
        $this->assertArrayHasKey('BE', $countries);
176
        $this->assertArrayHasKey('CZ', $countries);
177
        $this->assertArrayHasKey('ES', $countries);
178
        $this->assertGreaterThan(4, count($countries));
179
 
180
        set_config('allcountrycodes', '');
181
        $countries = $stringman->get_list_of_countries(false);
182
        $this->assertArrayHasKey('AU', $countries);
183
        $this->assertArrayHasKey('BE', $countries);
184
        $this->assertArrayHasKey('CZ', $countries);
185
        $this->assertArrayHasKey('ES', $countries);
186
        $this->assertGreaterThan(4, count($countries));
187
 
188
        set_config('allcountrycodes', 'CZ,BE');
189
        $countries = $stringman->get_list_of_countries(true);
190
        $this->assertArrayHasKey('AU', $countries);
191
        $this->assertArrayHasKey('BE', $countries);
192
        $this->assertArrayHasKey('CZ', $countries);
193
        $this->assertArrayHasKey('ES', $countries);
194
        $this->assertGreaterThan(4, count($countries));
195
 
196
        $countries = $stringman->get_list_of_countries(false);
197
        $this->assertEquals(2, count($countries));
198
        $this->assertArrayHasKey('BE', $countries);
199
        $this->assertArrayHasKey('CZ', $countries);
200
 
201
        set_config('allcountrycodes', 'CZ,UVWXYZ');
202
        $countries = $stringman->get_list_of_countries();
203
        $this->assertArrayHasKey('CZ', $countries);
204
        $this->assertEquals(1, count($countries));
205
 
206
        set_config('allcountrycodes', 'UVWXYZ');
207
        $countries = $stringman->get_list_of_countries();
208
        $this->assertArrayHasKey('AU', $countries);
209
        $this->assertArrayHasKey('BE', $countries);
210
        $this->assertArrayHasKey('CZ', $countries);
211
        $this->assertArrayHasKey('ES', $countries);
212
        $this->assertGreaterThan(4, count($countries));
213
    }
214
}
215
 
216
/**
217
 * Helper class providing testable string_manager
218
 *
219
 * @copyright 2013 David Mudrak <david@moodle.com>
220
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
221
 */
222
class testable_core_string_manager extends core_string_manager_standard {
223
 
224
    /**
225
     * Factory method
226
     *
227
     * @param string $otherroot full path to the location of installed upstream language packs
228
     * @param string $localroot full path to the location of locally customized language packs, defaults to $otherroot
229
     * @param bool $usecache use application permanent cache
230
     * @param array $translist explicit list of visible translations
231
     * @param string $menucache the location of a file that caches the list of available translations
232
     * @return testable_core_string_manager
233
     */
234
    public static function instance($otherroot, $localroot = null, $usecache = false, array $translist = array(), $menucache = null) {
235
        global $CFG;
236
 
237
        if (is_null($localroot)) {
238
            $localroot = $otherroot;
239
        }
240
 
241
        if (is_null($menucache)) {
242
            $menucache = $CFG->cachedir.'/languages';
243
        }
244
 
245
        return new testable_core_string_manager($otherroot, $localroot, $usecache, $translist, $menucache);
246
    }
247
 
248
    public function get_all_deprecated_strings() {
249
        return array_flip($this->load_deprecated_strings());
250
    }
251
}