Translated using Weblate (Lithuanian)
[phpmyadmin.git] / test / classes / LanguageTest.php
blob363dd452ee51934e009c98a73214a9b74252a484
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests;
7 use PhpMyAdmin\LanguageManager;
9 use function _ngettext;
10 use function count;
11 use function is_readable;
12 use function strtolower;
14 /**
15 * @covers \PhpMyAdmin\Language
16 * @covers \PhpMyAdmin\LanguageManager
18 class LanguageTest extends AbstractTestCase
20 /** @var LanguageManager */
21 private $manager;
23 /**
24 * Setup for Language tests.
26 protected function setUp(): void
28 parent::setUp();
29 $loc = LOCALE_PATH . '/cs/LC_MESSAGES/phpmyadmin.mo';
30 if (! is_readable($loc)) {
31 $this->markTestSkipped('Missing compiled locales.');
34 $this->manager = new LanguageManager();
37 protected function tearDown(): void
39 parent::tearDown();
40 // Ensure we have English locale after tests
41 $lang = $this->manager->getLanguage('en');
42 if ($lang === false) {
43 return;
46 $lang->activate();
49 /**
50 * Test language filtering
52 public function testAvailable(): void
54 $GLOBALS['config']->set('FilterLanguages', 'cs|en$');
56 $langs = $this->manager->availableLocales();
58 $this->assertCount(2, $langs);
59 $this->assertContains('cs', $langs);
60 $GLOBALS['config']->set('FilterLanguages', '');
63 /**
64 * Test no language filtering
66 public function testAllAvailable(): void
68 $GLOBALS['config']->set('FilterLanguages', '');
70 $langs = $this->manager->availableLocales();
72 $this->assertContains('cs', $langs);
73 $this->assertContains('en', $langs);
76 /**
77 * Test whether listing locales works
79 public function testList(): void
81 $langs = $this->manager->listLocaleDir();
82 $this->assertContains('cs', $langs);
83 $this->assertContains('en', $langs);
86 /**
87 * Test for getting available languages
89 public function testLanguages(): void
91 $langs = $this->manager->availableLanguages();
92 $this->assertGreaterThan(1, count($langs));
94 /* Ensure we have name for every language */
95 foreach ($langs as $lang) {
96 $this->assertNotEquals(
97 $lang->getCode(),
98 strtolower($lang->getEnglishName()),
99 'Maybe this language does not exist in LanguageManager class'
100 . ', see: https://github.com/phpmyadmin/phpmyadmin/issues/16300.'
106 * Test for MySQL locales
108 public function testMySQLLocale(): void
110 $GLOBALS['config']->set('FilterLanguages', '');
111 $czech = $this->manager->getLanguage('cs');
112 $this->assertNotFalse($czech);
113 $this->assertEquals('cs_CZ', $czech->getMySQLLocale());
115 $azerbaijani = $this->manager->getLanguage('az');
116 $this->assertNotFalse($azerbaijani);
117 $this->assertEquals('', $azerbaijani->getMySQLLocale());
121 * Test for getting available sorted languages
123 public function testSortedLanguages(): void
125 $langs = $this->manager->sortedLanguages();
126 $this->assertGreaterThan(1, count($langs));
130 * Test getting language by code
132 public function testGet(): void
134 $GLOBALS['config']->set('FilterLanguages', '');
135 $lang = $this->manager->getLanguage('cs');
136 $this->assertNotFalse($lang);
137 $this->assertEquals('Czech', $lang->getEnglishName());
138 $this->assertEquals('Čeština', $lang->getNativeName());
139 $lang = $this->manager->getLanguage('nonexisting');
140 $this->assertFalse($lang);
144 * Test language selection
146 * @param string $lang Value for forced language
147 * @param string $post Value for language in POST
148 * @param string $get Value for language in GET
149 * @param string $cookie Value for language in COOKIE
150 * @param string $accept Value for HTTP Accept-Language header
151 * @param string $agent Value for HTTP User-Agent header
152 * @param string $default Value for default language
153 * @param string $expect Expected language name
155 * @dataProvider selectDataProvider
157 public function testSelect(
158 string $lang,
159 string $post,
160 string $get,
161 string $cookie,
162 string $accept,
163 string $agent,
164 string $default,
165 string $expect
166 ): void {
167 $GLOBALS['config']->set('FilterLanguages', '');
168 $GLOBALS['config']->set('Lang', $lang);
169 $GLOBALS['config']->set('is_https', false);
170 $_POST['lang'] = $post;
171 $_GET['lang'] = $get;
172 $_COOKIE['pma_lang'] = $cookie;
173 $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $accept;
174 $_SERVER['HTTP_USER_AGENT'] = $agent;
175 $GLOBALS['config']->set('DefaultLang', $default);
177 $lang = $this->manager->selectLanguage();
179 $this->assertEquals($expect, $lang->getEnglishName());
181 $GLOBALS['config']->set('Lang', '');
182 $_POST['lang'] = '';
183 $_GET['lang'] = '';
184 $_COOKIE['pma_lang'] = '';
185 $_SERVER['HTTP_ACCEPT_LANGUAGE'] = '';
186 $_SERVER['HTTP_USER_AGENT'] = '';
187 $GLOBALS['config']->set('DefaultLang', 'en');
191 * Data provider for language selection test.
193 * @return array Test parameters.
195 public function selectDataProvider(): array
197 return [
199 'cs',
200 'en',
206 'Czech',
210 'cs',
216 'Czech',
220 'cs',
221 'en',
226 'Czech',
231 'cs',
236 'Czech',
242 'cs',
246 'Czech',
253 'cs,en-US;q=0.7,en;q=0.3',
256 'Czech',
264 'Mozilla/5.0 (Linux; U; Android 2.2.2; tr-tr; GM FOX)',
266 'Turkish',
275 'cs',
276 'Czech',
286 'English',
292 * Test for setting and parsing locales
294 * @param string $locale locale name
296 * @group large
297 * @dataProvider listLocales
299 public function testGettext(string $locale): void
301 $GLOBALS['config']->set('FilterLanguages', '');
302 /* We should be able to set the language */
303 $lang = $this->manager->getLanguage($locale);
304 $this->assertNotFalse($lang);
305 $lang->activate();
307 /* Grab some texts */
308 $this->assertStringContainsString('%s', _ngettext('%s table', '%s tables', 10));
309 $this->assertStringContainsString('%s', _ngettext('%s table', '%s tables', 1));
311 $this->assertEquals(
312 $locale,
313 $this->manager->getCurrentLanguage()->getCode()
318 * Data provider to generate list of available locales.
320 * @return array with arrays of available locales
322 public function listLocales(): array
324 $ret = [];
325 foreach (LanguageManager::getInstance()->availableLanguages() as $language) {
326 $ret[] = [$language->getCode()];
329 return $ret;