]> git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/I18n.php
039a3e34d8804270b79b9282832aa169d67a7b69
[galette.git] / tests / Galette / Core / tests / units / I18n.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * i18n tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2013-2023 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
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.
18 *
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.
23 *
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/>.
26 *
27 * @category Core
28 * @package GaletteTests
29 *
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 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since 2013-01-13
36 */
37
38 namespace Galette\Core\test\units;
39
40 use atoum;
41
42 /**
43 * I18n tests class
44 *
45 * @category Core
46 * @name i18n
47 * @package GaletteTests
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2013-2023 The Galette Team
50 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
51 * @link http://galette.tuxfamily.org
52 * @since 2013-01-13
53 */
54 class I18n extends atoum
55 {
56 private \Galette\Core\Db $zdb;
57 private ?\Galette\Core\I18n $i18n = null;
58
59 /**
60 * Set up tests
61 *
62 * @param string $method Tested method name
63 *
64 * @return void
65 */
66 public function beforeTestMethod($method)
67 {
68 $this->zdb = new \Galette\Core\Db();
69 $this->i18n = new \Galette\Core\I18n(
70 \Galette\Core\I18n::DEFAULT_LANG
71 );
72 }
73
74 /**
75 * Tear down tests
76 *
77 * @param string $method Calling method
78 *
79 * @return void
80 */
81 public function afterTestMethod($method)
82 {
83 if (TYPE_DB === 'mysql') {
84 $this->array($this->zdb->getWarnings())->isIdenticalTo([]);
85 }
86 }
87
88 /**
89 * Test lang autodetect
90 *
91 * @return void
92 */
93 public function testAutoLang()
94 {
95 $this->i18n = new \Galette\Core\I18n();
96
97 $this->variable($this->i18n->getID())->isIdenticalTo(\Galette\Core\I18n::DEFAULT_LANG);
98
99 //simulate fr from browser
100 $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr_BE';
101 $this->i18n = new \Galette\Core\I18n();
102
103 $this->variable($this->i18n->getID())
104 ->isIdenticalTo('fr_FR');
105
106 //simulate en from browser
107 $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en_GB';
108 $this->i18n = new \Galette\Core\I18n();
109
110 $this->variable($this->i18n->getID())->isIdenticalTo('en_US');
111
112 //simulate unknown lang from browser
113 $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'un_KN';
114 $this->i18n = new \Galette\Core\I18n();
115
116 $this->variable($this->i18n->getID())
117 ->isIdenticalTo(\Galette\Core\I18n::DEFAULT_LANG);
118 }
119
120 /**
121 * Test languages list
122 *
123 * @return void
124 */
125 public function testGetList()
126 {
127 $list = $this->i18n->getList();
128
129 $this->array($list)
130 ->size->isGreaterThan(3);
131
132 foreach ($list as $elt) {
133 $this->object($elt)
134 ->isInstanceOf('\Galette\Core\I18n');
135 }
136 }
137
138 /**
139 * Test languages list as array
140 *
141 * @return void
142 */
143 public function testGetArrayList()
144 {
145 $list = $this->i18n->getArrayList();
146
147 $this->array($list)
148 ->size->isGreaterThan(3);
149 }
150
151 /**
152 * Test getting language name from its ID
153 *
154 * @return void
155 */
156 public function testGetNameFromid()
157 {
158 $lang = $this->i18n->getNameFromId('en_US');
159 $this->variable($lang)->isIdenticalTo('English');
160
161 $lang = $this->i18n->getNameFromId('fr_FR');
162 $this->variable($lang)->isIdenticalTo('Français');
163 }
164
165 /**
166 * Test retrieving language information
167 *
168 * @return void
169 */
170 public function testGetLangInfos()
171 {
172 $id = $this->i18n->getID();
173 $longid = $this->i18n->getLongID();
174 $name = $this->i18n->getName();
175 $abbrev = $this->i18n->getAbbrev();
176
177 $this->variable($id)->isIdenticalTo('en_US');
178 $this->variable($longid)->isIdenticalTo('en_US');
179 $this->variable($name)->isIdenticalTo('English');
180 $this->variable($abbrev)->isIdenticalTo('en');
181
182 $this->i18n->changeLanguage('fr_FR');
183 $id = $this->i18n->getID();
184 $longid = $this->i18n->getLongID();
185 $name = $this->i18n->getName();
186 $abbrev = $this->i18n->getAbbrev();
187
188 $this->variable($id)->isIdenticalTo('fr_FR');
189 $this->variable($longid)->isIdenticalTo('fr_FR.utf8');
190 $this->variable($name)->isIdenticalTo('Français');
191 $this->variable($abbrev)->isIdenticalTo('fr');
192 }
193
194 /**
195 * Change to an unknown language
196 *
197 * @return void
198 */
199 public function testChangeUnknownLanguage()
200 {
201 $this->i18n->changeLanguage('un_KN');
202 $id = $this->i18n->getID();
203
204 $this->variable($id)->isIdenticalTo(\Galette\Core\I18n::DEFAULT_LANG);
205 }
206
207 /**
208 * Check (non) UTF strings
209 *
210 * @return void
211 */
212 public function testSeemUtf8()
213 {
214 $is_utf = $this->i18n->seemUtf8('HéhéHÉHÉâ-ô߬- ©»«<ëßßä€êþÿûîœô');
215 $is_iso = $this->i18n->seemUtf8(mb_convert_encoding('Héhé', 'ISO-8859-1'));
216
217 $this->boolean($is_utf)->isTrue();
218 $this->boolean($is_iso)->isFalse();
219 }
220 }