xref: /webtrees/tests/app/I18NTest.php (revision 1062a1429914c995339f502856821457aa975a5a)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2018 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18use Fisharebest\Webtrees\I18N;
19
20/**
21 * Test harness for the class I18N
22 */
23class I18NTest extends \PHPUnit\Framework\TestCase {
24	/**
25	 * Prepare the environment for these tests
26	 */
27	public function setUp() {
28		defined('WT_BASE_URL') || define('WT_BASE_URL', 'http://localhost/');
29		defined('WT_DATA_DIR') || define('WT_DATA_DIR', 'data/');
30		defined('WT_MODULES_DIR') || define('WT_MODULES_DIR', 'modules_v3/');
31		I18N::init('en-US');
32	}
33
34	/**
35	 * Test I18N::strtoupper()
36	 *
37	 * @todo test all locales
38	 */
39	public function testStrtoupper() {
40		$this->assertSame(I18N::strtoupper(''), '');
41		$this->assertSame(I18N::strtoupper('Abc'), 'ABC');
42	}
43
44	/**
45	 * Test I18N::strtolower()
46	 *
47	 * @todo test all locales
48	 */
49	public function testStrtolower() {
50		$this->assertSame(I18N::strtolower(''), '');
51		$this->assertSame(I18N::strtolower('Abc'), 'abc');
52	}
53
54	/**
55	 * Test I18N::strcasecmp()
56	 *
57	 * @todo test all locales
58	 */
59	public function testStrcasecmp() {
60		$this->assertSame(I18N::strcasecmp('', ''), 0);
61		$this->assertSame(I18N::strcasecmp('Abc', 'abc'), 0);
62		$this->assertTrue(I18N::strcasecmp('Abc', 'bcd') < 0);
63		$this->assertTrue(I18N::strcasecmp('bcd', 'ABC') > 0);
64		$this->assertTrue(I18N::strcasecmp('Abc', 'abcd') < 0);
65		$this->assertTrue(I18N::strcasecmp('Abcd', 'abc') > 0);
66	}
67
68	/**
69	 * Test I18N::reverseText()
70	 */
71	public function testReverseText() {
72		// Create these strings carefully, as text editors can display them in confusing ways.
73		$rtl_abc = 'א' . 'ב' . 'ג';
74		$rtl_cba = 'ג' . 'ב' . 'א';
75		$rtl_123 = '١' . '٢' . '٣';
76
77		$this->assertSame(I18N::reverseText(''), '');
78		$this->assertSame(I18N::reverseText('abc123'), 'abc123');
79		$this->assertSame(I18N::reverseText('<b>abc</b>123'), 'abc123');
80		$this->assertSame(I18N::reverseText('&lt;abc&gt;'), '<abc>');
81		$this->assertSame(I18N::reverseText('abc[123]'), 'abc[123]');
82		$this->assertSame(I18N::reverseText($rtl_123), $rtl_123);
83		$this->assertSame(I18N::reverseText($rtl_abc), $rtl_cba);
84		$this->assertSame(I18N::reverseText($rtl_abc . '123'), '123' . $rtl_cba);
85		$this->assertSame(I18N::reverseText($rtl_abc . '[123]'), '[123]' . $rtl_cba);
86		$this->assertSame(I18N::reverseText('123' . $rtl_abc . '456'), '456' . $rtl_cba . '123');
87		$this->assertSame(I18N::reverseText($rtl_abc . '&lt;'), '>' . $rtl_cba);
88	}
89
90	/**
91	 * Test I18N::languageName()
92	 */
93	public function testKnownLanguageName() {
94		$this->assertSame('العربية', I18N::languageName('ar'));
95		$this->assertSame('Deutsch', I18N::languageName('de'));
96		$this->assertSame('Ελληνικά', I18N::languageName('el'));
97		$this->assertSame('British English', I18N::languageName('en-GB'));
98		$this->assertSame('français', I18N::languageName('fr'));
99	}
100
101	/**
102	 * Test I18N::languageScript()
103	 */
104	public function testLanguageScript() {
105		$this->assertSame('Arab', I18N::languageScript('ar'));
106		$this->assertSame('Latn', I18N::languageScript('de'));
107		$this->assertSame('Grek', I18N::languageScript('el'));
108	}
109}
110