Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LanguagesManagerTest.php « Plugins « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fb0ca7012c3aa226922e73dedcb18c8b04d775b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link    http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
use Piwik\Common;
use Piwik\TranslationWriter;

require_once 'LanguagesManager/API.php';

class Test_LanguagesManager extends PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        parent::setUp();
        include PIWIK_INCLUDE_PATH . '/core/DataFiles/Languages.php';
    }

    static $errors;
    static $englishStringsIndexed = array();
    static $englishStringsWithParameters = array();
    static $allLanguages = array();
    static $allCountries = array();
    static $expectedLanguageKeys = array();

    function getTestDataForLanguageFiles()
    {
        self::$allLanguages = Common::getLanguagesList();
        self::$allCountries = Common::getCountriesList();
        self::$englishStringsWithParameters = array();
        self::$englishStringsIndexed = array();
        self::$expectedLanguageKeys = array();
        $englishStrings = Piwik_LanguagesManager_API::getInstance()->getTranslationsForLanguage('en');
        foreach ($englishStrings as $englishString) {
            $stringLabel = $englishString['label'];
            $stringValue = $englishString['value'];
            $count = $this->getCountParametersToReplace($stringValue);
            if ($count > 0) {
                self::$englishStringsWithParameters[$stringLabel] = $count;
            }
            self::$englishStringsIndexed[$stringLabel] = $stringValue;
            self::$expectedLanguageKeys[] = $stringLabel;
        }

        // we also test that none of the language php files outputs any character on the screen (eg. space before the <?php)
        $languages = Piwik_LanguagesManager_API::getInstance()->getAvailableLanguages();

        $return = array();
        foreach ($languages AS $language) {
            if ($language != 'en') {
                $return[] = array($language);
            }
        }
        return $return;
    }

    /**
     * test all languages
     *
     * @group Plugins
     * @group LanguagesManager
     * @dataProvider getTestDataForLanguageFiles
     */
    function testGetTranslationsForLanguages($language)
    {
        self::$errors = array();
        ob_start();
        $writeCleanedFile = false;
        $strings = Piwik_LanguagesManager_API::getInstance()->getTranslationsForLanguage($language);
        $content = ob_get_flush();
        $serializedStrings = serialize($strings);
        $invalids = array("<script", 'document.', 'javascript:', 'src=', 'BACKGROUND=', 'onload=');
        foreach ($invalids as $invalid) {
            $this->assertTrue(stripos($serializedStrings, $invalid) === false, "$language: language file containing javascript");
        }
        $this->assertTrue(count($strings) > 250, "$language: expecting at least 250 translations in the language file");
        $this->assertTrue(strlen($content) == 0, "$language: buffer was " . strlen($content) . " long but should be zero. Translation file for '$language' must be buggy.");

        $cleanedStrings = array();
        foreach ($strings as $string) {
            $stringLabel = $string['label'];
            $stringValue = $string['value'];

            $plugin = substr($stringLabel, 0, strpos($stringLabel, '_'));
            $plugins[$plugin] = true;
            // Testing that the translated string is not empty => '',
            if (empty($stringValue) || trim($stringValue) === '') {
                $writeCleanedFile = true;
                self::$errors[] = "$language: The string $stringLabel is empty in the translation file, removing the line.";
                $cleanedStrings[$stringLabel] = false;
            } elseif (!in_array($stringLabel, self::$expectedLanguageKeys)
                // translation files should not contain 3rd plugin translations, but if they are there, we shall not delete them
                // since translators have spent time working on it... at least for now we shall leave them in (until V2 and plugin repository is done)
                && !in_array($plugin, array('GeoIP', 'Forecast', 'EntryPage', 'UserLanguage'))
            ) {
                $writeCleanedFile = true;
                self::$errors[] = "$language: The string $stringLabel was not found in the English language file, removing the line.";
                $cleanedStrings[$stringLabel] = false;
            } else {
                // checking that translated strings have the same number of %s as the english source strings
                if (isset(self::$englishStringsWithParameters[$stringLabel])) {
                    $englishParametersCount = self::$englishStringsWithParameters[$stringLabel];
                    $countTranslation = $this->getCountParametersToReplace($stringValue);
                    if ($englishParametersCount != $countTranslation) {
                        // Write fixed file in given location
                        // Will trigger a ->fail()
                        $writeCleanedFile = true;
                        self::$errors[] = "$language: The string $stringLabel has $englishParametersCount parameters in English, but $countTranslation in this translation.";
                    } else {
                        $cleanedStrings[$stringLabel] = $stringValue;
                    }
                } // No %s found
                else {
                    $cleanedStrings[$stringLabel] = $stringValue;
                }
            }

            // If the translation is the same as in English, we remove it from the translation file (as it might have been copied by
            // the translator but this would skew translation stats
            if (isset($englishStringsIndexed[$stringLabel])
                && $englishStringsIndexed[$stringLabel] == $stringValue
                // Do not however remove the General_ since there are definiely legit translations that are same as in english (eg. short days)
                && strpos($stringLabel, 'General_') === false
                && strpos($stringLabel, 'CoreHome_') === false
                && strpos($stringLabel, 'UserCountry_') === false
                && $language != 'de'
            ) {
                $writeCleanedFile = true;
                self::$errors[] = "$language: The string $stringLabel is the same as in English, removing...";
                $cleanedStrings[$stringLabel] = false;
            }
            // remove excessive line breaks (and leading/trailing whitespace) from translations
            if (!empty($cleanedStrings[$stringLabel])) {
                $stringNoLineBreak = trim($cleanedStrings[$stringLabel]);
                if ($stringLabel != 'Login_MailPasswordChangeBody') {
                    $stringNoLineBreak = str_replace(array("\n", "\r"), " ", $stringNoLineBreak);
                }
                if ($cleanedStrings[$stringLabel] !== $stringNoLineBreak) {
                    self::$errors[] = "$language: found unnecessary whitespace in some strings in $stringLabel";
                    $writeCleanedFile = true;
                    $cleanedStrings[$stringLabel] = $stringNoLineBreak;
                }
            }
            // Test locale
            if ($stringLabel == 'General_Locale'
                && !empty($cleanedStrings[$stringLabel])
            ) {
                if (!preg_match('/^([a-z]{2})_([A-Z]{2})\.UTF-8$/', $cleanedStrings[$stringLabel], $matches)) {
                    $this->fail("$language: invalid locale in $stringLabel");
                } else if (!array_key_exists($matches[1], self::$allLanguages)) {
                    $this->fail("$language: invalid language code in $stringLabel");
                } else if (!array_key_exists(strtolower($matches[2]), self::$allCountries)) {
                    $this->fail("$language: invalid region (country code) in $stringLabel");
                }
            }
            if (isset($cleanedStrings[$stringLabel])) {
                $currentString = $cleanedStrings[$stringLabel];
                $decoded = TranslationWriter::clean($currentString);
                if ($currentString != $decoded) {
                    self::$errors[] = "$language: found encoded entities in $stringLabel, converting entities to characters";
                    $writeCleanedFile = true;
                    $cleanedStrings[$stringLabel] = $decoded;
                }
            }
        }
        $this->assertTrue(!empty($cleanedStrings['General_TranslatorName']), "$language: translator info not specified");
        $this->assertTrue(!empty($cleanedStrings['General_TranslatorEmail']), "$language: translator email not specified");
        if (!empty($cleanedStrings['General_LayoutDirection'])
            && !in_array($cleanedStrings['General_LayoutDirection'], array('rtl', 'ltr'))
        ) {
            $writeCleanedFile = true;
            $cleanedStrings['General_LayoutDirection'] = false;
            self::$errors[] = "$language: General_LayoutDirection must be rtl or ltr";
        }
        if ($writeCleanedFile) {
            $path = TranslationWriter::getTranslationPath($language, 'tmp');

            // Reorder cleaned up translations as the same order as en.php
            uksort($cleanedStrings, array($this, 'sortTranslationsKeys'));

            TranslationWriter::saveTranslation($cleanedStrings, $path);
            $output[] = (implode("\n", self::$errors) . "\n" . 'Translation file errors detected in ' . $language . '...
                    Wrote cleaned translation file in: ' . $path . ".
                    You can copy the cleaned files to /lang/\n");
        }
        if (!empty($output)) {
            $this->fail(implode(",", $output));
        }
    }

    /**
     * Keep strings in the order of the english file
     *
     * @param $k1
     * @param $k2
     *
     * @return int
     */
    protected function sortTranslationsKeys($k1, $k2)
    {
        static $order = array();
        if (empty($order)) {
            $i = 0;
            foreach (self::$englishStringsIndexed as $key => $value) {
                $order[$key] = $i;
                $i++;
            }
        }
        if (empty($order[$k1])) {
            $order[$k1] = 5000;
        }
        if (empty($order[$k2])) {
            $order[$k2] = 5000;
        }

        return $order[$k1] < $order[$k2] ? -1 : ($order[$k1] == $order[$k2] ? strcmp($k1, $k2) : 1);
    }

    private function getCountParametersToReplace($string)
    {
        $sprintfParameters = array('%s', '%1$s', '%2$s', '%3$s', '%4$s', '%5$s', '%6$s');
        $count = 0;
        foreach ($sprintfParameters as $parameter) {
            $count += substr_count($string, $parameter);
        }
        return $count;
    }

    /**
     * test language when it's not defined
     *
     * @group Plugins
     * @group LanguagesManager
     */
    function testGetTranslationsForLanguagesNot()
    {
        $this->assertFalse(Piwik_LanguagesManager_API::getInstance()->getTranslationsForLanguage("../no-language"));
    }

    /**
     * test English short name for language
     *
     * @group Plugins
     * @group LanguagesManager
     */
    function testGetLanguageNamesInEnglish()
    {
        $languages = Piwik_LanguagesManager_API::getInstance()->getAvailableLanguages();
        foreach ($languages as $language) {
            require PIWIK_INCLUDE_PATH . "/lang/$language.php";
            $name = $translations['General_EnglishLanguageName'];

            if ($language != 'en') {
                $this->assertFalse($name == 'English', "for $language");
            }

            $languageCode = substr($language, 0, 2);
            $this->assertTrue(isset($GLOBALS['Piwik_LanguageList'][$languageCode]));
            $names = $GLOBALS['Piwik_LanguageList'][$languageCode];

            if (isset($GLOBALS['Piwik_LanguageList'][$language])) {
                if (is_array($names)) {
                    $this->assertTrue(in_array($name, $names), "$language: failed because $name not a known language name");
                } else {
                    $this->assertTrue($name == $names, "$language: failed because $name == $names");
                }
            } else {
                if (is_array($names)) {
                    $this->assertTrue(strpos($name, $names[0]) !== false);
                } else {
                    $this->fail("$language: expected an array of language names");
                }
            }
        }
    }

    /**
     * test format of DataFile/Languages.php
     *
     * @group Plugins
     * @group LanguagesManager
     */
    function testGetLanguagesList()
    {
        $languages = Common::getLanguagesList();
        $this->assertTrue(count($languages) > 0);
        foreach ($languages as $langCode => $langs) {
            $this->assertTrue(strlen($langCode) == 2, "$langCode length = 2");
            $this->assertTrue(is_array($langs) && count($langs) >= 1, "$langCode array(names) >= 1");
        }
    }
}