Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
127 / 127
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
phpbb_language_test
100.00% covered (success)
100.00%
127 / 127
100.00% covered (success)
100.00%
9 / 9
9
100.00% covered (success)
100.00%
1 / 1
 setUp
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 test_is_set
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 test_lang_raw
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 test_lang_array
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 test_lang
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
1
 test_lang_plural_rules
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 test_lang_bc
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
1
 test_lang_plural_rules_bc
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 get_test_data_set
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14class phpbb_language_test extends phpbb_test_case
15{
16    /** @var \phpbb\language\language */
17    protected $lang;
18
19    protected function setUp(): void
20    {
21        global $phpbb_root_path, $phpEx;
22
23        // Set up language service
24        $this->lang = new \phpbb\language\language(
25            new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)
26        );
27
28        // Set up language data for testing
29        $reflection_class = new ReflectionClass('\phpbb\language\language');
30
31        // Set default language files loaded flag to true
32        $loaded_flag = $reflection_class->getProperty('common_language_files_loaded');
33        $loaded_flag->setValue($this->lang, true);
34
35        // Set up test language data
36        $lang_array = $reflection_class->getProperty('lang');
37        $lang_array->setValue($this->lang, $this->get_test_data_set());
38    }
39
40    public function test_is_set()
41    {
42        // Check for non-existing key
43        $this->assertFalse($this->lang->is_set('VALUE'));
44        $this->assertFalse($this->lang->is_set(array('dateformat', 'MAYBE')));
45
46        // Check for existing key
47        $this->assertTrue($this->lang->is_set('FOO'));
48        $this->assertTrue($this->lang->is_set(array('dateformat', 'AGO')));
49
50        // Array doesn't exist at all...
51        $this->assertFalse($this->lang->is_set(array('PHPBB', 'PHP')));
52    }
53
54    public function test_lang_raw()
55    {
56        $this->assertEquals($this->lang->lang_raw('FOO'), 'BAR');
57        $this->assertEquals($this->lang->lang_raw('VOID'), 'VOID');
58        $this->assertEquals($this->lang->lang_raw('ARRY'), array(
59            0        => 'No posts',        // 0
60            1        => '1 post',        // 1
61            2        => '%d posts',        // 2+
62        ));
63    }
64
65    public function test_lang_array()
66    {
67        $this->assertEquals($this->lang->lang_array('FOO'), 'BAR');
68        $this->assertEquals($this->lang->lang_array('VOID'), 'VOID');
69        $this->assertEquals($this->lang->lang_array('ARRY', [0]), 'No posts');
70        $this->assertEquals($this->lang->lang_array('FOO', [2, 3, 'BARZ']), 'BAR');
71    }
72
73    public function test_lang()
74    {
75        // No param
76        $this->assertEquals($this->lang->lang('FOO'), 'BAR');
77        $this->assertEquals($this->lang->lang('EMPTY'), '');
78        $this->assertEquals($this->lang->lang('ZERO'), '0');
79
80        // Invalid index
81        $this->assertEquals($this->lang->lang('VOID'), 'VOID');
82
83        // Unnecessary param
84        $this->assertEquals($this->lang->lang('FOO', 2), 'BAR');
85        $this->assertEquals($this->lang->lang('FOO', 2, 3), 'BAR');
86        $this->assertEquals($this->lang->lang('FOO', 2, 3, 'BARZ'), 'BAR');
87
88        // String
89        $this->assertEquals($this->lang->lang('STR', 24, 'x', 42), '24 x, 42 topics');
90        $this->assertEquals($this->lang->lang('STR2', 64), '64 foos');
91
92        // Array
93        $this->assertEquals($this->lang->lang('ARRY', 0), 'No posts');
94        $this->assertEquals($this->lang->lang('ARRY', 1), '1 post');
95        $this->assertEquals($this->lang->lang('ARRY', 2), '2 posts');
96        $this->assertEquals($this->lang->lang('ARRY', 123), '123 posts');
97
98        // Empty array returns the language key
99        $this->assertEquals($this->lang->lang('ARRY_EMPTY', 123), 'ARRY_EMPTY');
100
101        // No 0 key defined
102        $this->assertEquals($this->lang->lang('ARRY_NO_ZERO', 0), '0 posts');
103        $this->assertEquals($this->lang->lang('ARRY_NO_ZERO', 1), '1 post');
104        $this->assertEquals($this->lang->lang('ARRY_NO_ZERO', 2), '2 posts');
105
106        // Array with missing keys
107        $this->assertEquals($this->lang->lang('ARRY_MISSING', 2), '2 post');
108
109        // Floats as array key
110        $this->assertEquals($this->lang->lang('ARRY_FLOAT', 1.3), '1 post');
111        $this->assertEquals($this->lang->lang('ARRY_FLOAT', 2.0), '2.0 posts');
112        $this->assertEquals($this->lang->lang('ARRY_FLOAT', 2.51), '2.5 posts');
113
114        // Use sub key, if first paramenter is an array
115        $this->assertEquals($this->lang->lang(array('dateformat', 'AGO'), 2), '2 seconds');
116
117        // ticket PHPBB3-9949 - use first int to determinate the plural-form to use
118        $this->assertEquals($this->lang->lang('ARRY', 1, 2), '1 post');
119        $this->assertEquals($this->lang->lang('ARRY', 1, 's', 2), '1 post');
120    }
121
122    public function test_lang_plural_rules()
123    {
124        $this->assertEquals($this->lang->lang('PLURAL_ARRY', 0), '0 is 0');
125        $this->assertEquals($this->lang->lang('PLURAL_ARRY', 1), '1 is 1');
126        $this->assertEquals($this->lang->lang('PLURAL_ARRY', 103), '103 ends with 01-10');
127        $this->assertEquals($this->lang->lang('PLURAL_ARRY', 15), '15 ends with 11-19');
128        $this->assertEquals($this->lang->lang('PLURAL_ARRY', 300), '300 is part of the last rule');
129    }
130
131    public function test_lang_bc()
132    {
133        $user = new \phpbb\user($this->lang, '\phpbb\datetime');
134
135        // Test lang array access
136        $this->assertEquals($user->lang['FOO'], 'BAR');
137
138        // No param
139        $this->assertEquals($user->lang('FOO'), 'BAR');
140        $this->assertEquals($user->lang('EMPTY'), '');
141        $this->assertEquals($user->lang('ZERO'), '0');
142
143        // Invalid index
144        $this->assertEquals($user->lang('VOID'), 'VOID');
145
146        // Unnecessary param
147        $this->assertEquals($user->lang('FOO', 2), 'BAR');
148        $this->assertEquals($user->lang('FOO', 2, 3), 'BAR');
149        $this->assertEquals($user->lang('FOO', 2, 3, 'BARZ'), 'BAR');
150
151        // String
152        $this->assertEquals($user->lang('STR', 24, 'x', 42), '24 x, 42 topics');
153        $this->assertEquals($user->lang('STR2', 64), '64 foos');
154
155        // Array
156        $this->assertEquals($user->lang('ARRY', 0), 'No posts');
157        $this->assertEquals($user->lang('ARRY', 1), '1 post');
158        $this->assertEquals($user->lang('ARRY', 2), '2 posts');
159        $this->assertEquals($user->lang('ARRY', 123), '123 posts');
160
161        // Empty array returns the language key
162        $this->assertEquals($user->lang('ARRY_EMPTY', 123), 'ARRY_EMPTY');
163
164        // No 0 key defined
165        $this->assertEquals($user->lang('ARRY_NO_ZERO', 0), '0 posts');
166        $this->assertEquals($user->lang('ARRY_NO_ZERO', 1), '1 post');
167        $this->assertEquals($user->lang('ARRY_NO_ZERO', 2), '2 posts');
168
169        // Array with missing keys
170        $this->assertEquals($user->lang('ARRY_MISSING', 2), '2 post');
171
172        // Floats as array key
173        $this->assertEquals($user->lang('ARRY_FLOAT', 1.3), '1 post');
174        $this->assertEquals($user->lang('ARRY_FLOAT', 2.0), '2.0 posts');
175        $this->assertEquals($user->lang('ARRY_FLOAT', 2.51), '2.5 posts');
176
177        // Use sub key, if first paramenter is an array
178        $this->assertEquals($user->lang(array('dateformat', 'AGO'), 2), '2 seconds');
179
180        // ticket PHPBB3-9949 - use first int to determinate the plural-form to use
181        $this->assertEquals($user->lang('ARRY', 1, 2), '1 post');
182        $this->assertEquals($user->lang('ARRY', 1, 's', 2), '1 post');
183    }
184
185    public function test_lang_plural_rules_bc()
186    {
187        $user = new \phpbb\user($this->lang, '\phpbb\datetime');
188
189        // ticket PHPBB3-10345 - different plural rules, not just 0/1/2+
190        $this->assertEquals($user->lang('PLURAL_ARRY', 0), '0 is 0');
191        $this->assertEquals($user->lang('PLURAL_ARRY', 1), '1 is 1');
192        $this->assertEquals($user->lang('PLURAL_ARRY', 103), '103 ends with 01-10');
193        $this->assertEquals($user->lang('PLURAL_ARRY', 15), '15 ends with 11-19');
194        $this->assertEquals($user->lang('PLURAL_ARRY', 300), '300 is part of the last rule');
195    }
196
197    protected function get_test_data_set()
198    {
199        return array(
200            'FOO'        => 'BAR',
201            'BARZ'        => 'PENG',
202            'EMPTY'        => '',
203            'ZERO'        => '0',
204            'STR'        => '%d %s, %d topics',
205            'STR2'        => '%d foos',
206            'ARRY'        => array(
207                0        => 'No posts',        // 0
208                1        => '1 post',        // 1
209                2        => '%d posts',        // 2+
210            ),
211            'ARRY_NO_ZERO'    => array(
212                1        => '1 post',        // 1
213                2        => '%d posts',        // 0, 2+
214            ),
215            'ARRY_MISSING'    => array(
216                1        => '%d post',        // 1
217                //Missing second plural
218            ),
219            'ARRY_FLOAT'    => array(
220                1        => '1 post',        // 1.x
221                2        => '%1$.1f posts',    // 0.x, 2+.x
222            ),
223            'ARRY_EMPTY'    => array(
224            ),
225            'dateformat'    => array(
226                'AGO'    => array(
227                    1    => '%d second',
228                    2    => '%d seconds',
229                ),
230            ),
231            'PLURAL_RULE' => 13,
232            'PLURAL_ARRY' => array(
233                0        => '%d is 0',                        // 0
234                1        => '%d is 1',                        // 1
235                2        => '%d ends with 01-10',            // ending with 01-10
236                3        => '%d ends with 11-19',            // ending with 11-19
237                4        => '%d is part of the last rule',    // everything else
238            ),
239        );
240    }
241}