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