3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2013-2023 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
28 * @package GaletteTests
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2013-2023 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
37 namespace Galette\Core\test\units
;
39 use PHPUnit\Framework\TestCase
;
46 * @package GaletteTests
47 * @author Johan Cwiklinski <johan@x-tnd.be>
48 * @copyright 2013-2023 The Galette Team
49 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
50 * @link http://galette.tuxfamily.org
53 class I18n
extends TestCase
55 private \Galette\Core\Db
$zdb;
56 private ?\Galette\Core\I18n
$i18n = null;
63 public function setUp(): void
65 $this->zdb
= new \Galette\Core\
Db();
66 $this->i18n
= new \Galette\Core\
I18n(
67 \Galette\Core\I18n
::DEFAULT_LANG
76 public function tearDown(): void
78 if (TYPE_DB
=== 'mysql') {
79 $this->assertSame([], $this->zdb
->getWarnings());
84 * Test lang autodetect
88 public function testAutoLang()
90 $this->i18n
= new \Galette\Core\
I18n();
92 $this->assertSame(\Galette\Core\I18n
::DEFAULT_LANG
, $this->i18n
->getID());
94 //simulate fr from browser
95 $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr_BE';
96 $this->i18n
= new \Galette\Core\
I18n();
98 $this->assertSame('fr_FR', $this->i18n
->getID());
100 //simulate en from browser
101 $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en_GB';
102 $this->i18n
= new \Galette\Core\
I18n();
104 $this->assertSame('en_US', $this->i18n
->getID());
106 //simulate unknown lang from browser
107 $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'un_KN';
108 $this->i18n
= new \Galette\Core\
I18n();
110 $this->assertSame(\Galette\Core\I18n
::DEFAULT_LANG
, $this->i18n
->getID());
114 * Test languages list
118 public function testGetList()
120 $list = $this->i18n
->getList();
122 $this->assertGreaterThan(3, count($list));
124 foreach ($list as $elt) {
125 $this->assertInstanceOf('\Galette\Core\I18n', $elt);
130 * Test languages list as array
134 public function testGetArrayList()
136 $list = $this->i18n
->getArrayList();
138 $this->assertGreaterThan(3, count($list));
142 * Test getting language name from its ID
146 public function testGetNameFromid()
148 $lang = $this->i18n
->getNameFromId('en_US');
149 $this->assertSame('English', $lang);
151 $lang = $this->i18n
->getNameFromId('fr_FR');
152 $this->assertSame('Français', $lang);
156 * Test retrieving language information
160 public function testGetLangInfos()
162 $id = $this->i18n
->getID();
163 $longid = $this->i18n
->getLongID();
164 $name = $this->i18n
->getName();
165 $abbrev = $this->i18n
->getAbbrev();
167 $this->assertSame('en_US', $id);
168 $this->assertSame('en_US', $longid);
169 $this->assertSame('English', $name);
170 $this->assertSame('en', $abbrev);
172 $this->i18n
->changeLanguage('fr_FR');
173 $id = $this->i18n
->getID();
174 $longid = $this->i18n
->getLongID();
175 $name = $this->i18n
->getName();
176 $abbrev = $this->i18n
->getAbbrev();
178 $this->assertSame('fr_FR', $id);
179 $this->assertSame('fr_FR.utf8', $longid);
180 $this->assertSame('Français', $name);
181 $this->assertSame('fr', $abbrev);
185 * Change to an unknown language
189 public function testChangeUnknownLanguage()
191 $this->i18n
->changeLanguage('un_KN');
192 $id = $this->i18n
->getID();
194 $this->assertSame(\Galette\Core\I18n
::DEFAULT_LANG
, $id);
198 * Check (non) UTF strings
202 public function testSeemUtf8()
204 $is_utf = $this->i18n
->seemUtf8('HéhéHÉHÉâ-ô߬- ©»«<ëßßä€êþÿûîœô');
205 $is_iso = $this->i18n
->seemUtf8(mb_convert_encoding('Héhé', 'ISO-8859-1'));
207 $this->assertTrue($is_utf);
208 $this->assertFalse($is_iso);