MDL-39915 cleanup core_string_manager_testcase
[moodle.git] / lib / tests / text_test.php
CommitLineData
d534708f
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle 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//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * core_text unit tests
19 *
20 * @package core
21 * @category phpunit
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28
29/**
30 * Unit tests for our utf-8 aware text processing
31 *
32 * @package core
33 * @category phpunit
34 * @copyright 2010 Petr Skoda (http://skodak.org)
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class core_text_testcase extends advanced_testcase {
38
39 /**
40 * Tests the static parse charset method
41 * @return void
42 */
43 public function test_parse_charset() {
44 $this->assertSame(core_text::parse_charset('Cp1250'), 'windows-1250');
45 // does typo3 work? some encoding moodle does not use
46 $this->assertSame(core_text::parse_charset('ms-ansi'), 'windows-1252');
47 }
48
49 /**
50 * Tests the static convert method
51 * @return void
52 */
53 public function test_convert() {
54 $utf8 = "Žluťoučký koníček";
55 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
56 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
57 $this->assertSame(core_text::convert($utf8, 'utf-8', 'iso-8859-2'), $iso2);
58 $this->assertSame(core_text::convert($iso2, 'iso-8859-2', 'utf-8'), $utf8);
59 $this->assertSame(core_text::convert($utf8, 'utf-8', 'win-1250'), $win);
60 $this->assertSame(core_text::convert($win, 'win-1250', 'utf-8'), $utf8);
61 $this->assertSame(core_text::convert($win, 'win-1250', 'iso-8859-2'), $iso2);
62 $this->assertSame(core_text::convert($iso2, 'iso-8859-2', 'win-1250'), $win);
63 $this->assertSame(core_text::convert($iso2, 'iso-8859-2', 'iso-8859-2'), $iso2);
64 $this->assertSame(core_text::convert($win, 'win-1250', 'cp1250'), $win);
65 $this->assertSame(core_text::convert($utf8, 'utf-8', 'utf-8'), $utf8);
66
67
68 $utf8 = '言語設定';
69 $str = pack("H*", "b8c0b8ecc0dfc4ea"); //EUC-JP
70 $this->assertSame(core_text::convert($utf8, 'utf-8', 'EUC-JP'), $str);
71 $this->assertSame(core_text::convert($str, 'EUC-JP', 'utf-8'), $utf8);
72 $this->assertSame(core_text::convert($utf8, 'utf-8', 'utf-8'), $utf8);
73
74 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
75 $this->assertSame(core_text::convert($utf8, 'utf-8', 'ISO-2022-JP'), $str);
76 $this->assertSame(core_text::convert($str, 'ISO-2022-JP', 'utf-8'), $utf8);
77 $this->assertSame(core_text::convert($utf8, 'utf-8', 'utf-8'), $utf8);
78
79 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
80 $this->assertSame(core_text::convert($utf8, 'utf-8', 'SHIFT-JIS'), $str);
81 $this->assertSame(core_text::convert($str, 'SHIFT-JIS', 'utf-8'), $utf8);
82 $this->assertSame(core_text::convert($utf8, 'utf-8', 'utf-8'), $utf8);
83
84 $utf8 = '简体中文';
85 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
86 $this->assertSame(core_text::convert($utf8, 'utf-8', 'GB2312'), $str);
87 $this->assertSame(core_text::convert($str, 'GB2312', 'utf-8'), $utf8);
88 $this->assertSame(core_text::convert($utf8, 'utf-8', 'utf-8'), $utf8);
89
90 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
91 $this->assertSame(core_text::convert($utf8, 'utf-8', 'GB18030'), $str);
92 $this->assertSame(core_text::convert($str, 'GB18030', 'utf-8'), $utf8);
93 $this->assertSame(core_text::convert($utf8, 'utf-8', 'utf-8'), $utf8);
94 }
95
96 /**
97 * Tests the static sub string method
98 * @return void
99 */
100 public function test_substr() {
101 $str = "Žluťoučký koníček";
102 $this->assertSame(core_text::substr($str, 0), $str);
103 $this->assertSame(core_text::substr($str, 1), 'luťoučký koníček');
104 $this->assertSame(core_text::substr($str, 1, 3), 'luť');
105 $this->assertSame(core_text::substr($str, 0, 100), $str);
106 $this->assertSame(core_text::substr($str, -3, 2), 'če');
107
108 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
109 $this->assertSame(core_text::substr($iso2, 1, 3, 'iso-8859-2'), core_text::convert('luť', 'utf-8', 'iso-8859-2'));
110 $this->assertSame(core_text::substr($iso2, 0, 100, 'iso-8859-2'), core_text::convert($str, 'utf-8', 'iso-8859-2'));
111 $this->assertSame(core_text::substr($iso2, -3, 2, 'iso-8859-2'), core_text::convert('če', 'utf-8', 'iso-8859-2'));
112
113 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
114 $this->assertSame(core_text::substr($win, 1, 3, 'cp1250'), core_text::convert('luť', 'utf-8', 'cp1250'));
115 $this->assertSame(core_text::substr($win, 0, 100, 'cp1250'), core_text::convert($str, 'utf-8', 'cp1250'));
116 $this->assertSame(core_text::substr($win, -3, 2, 'cp1250'), core_text::convert('če', 'utf-8', 'cp1250'));
117
118
119 $str = pack("H*", "b8c0b8ecc0dfc4ea"); //EUC-JP
120 $s = pack("H*", "b8ec"); //EUC-JP
121 $this->assertSame(core_text::substr($str, 1, 1, 'EUC-JP'), $s);
122
123 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
124 $s = pack("H*", "1b2442386c1b2842"); //ISO-2022-JP
125 $this->assertSame(core_text::substr($str, 1, 1, 'ISO-2022-JP'), $s);
126
127 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
128 $s = pack("H*", "8cea"); //SHIFT-JIS
129 $this->assertSame(core_text::substr($str, 1, 1, 'SHIFT-JIS'), $s);
130
131 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
132 $s = pack("H*", "cce5"); //GB2312
133 $this->assertSame(core_text::substr($str, 1, 1, 'GB2312'), $s);
134
135 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
136 $s = pack("H*", "cce5"); //GB18030
137 $this->assertSame(core_text::substr($str, 1, 1, 'GB18030'), $s);
138 }
139
140 /**
141 * Tests the static string length method
142 * @return void
143 */
144 public function test_strlen() {
145 $str = "Žluťoučký koníček";
146 $this->assertSame(core_text::strlen($str), 17);
147
148 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
149 $this->assertSame(core_text::strlen($iso2, 'iso-8859-2'), 17);
150
151 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
152 $this->assertSame(core_text::strlen($win, 'cp1250'), 17);
153
154
155 $str = pack("H*", "b8ec"); //EUC-JP
156 $this->assertSame(core_text::strlen($str, 'EUC-JP'), 1);
157 $str = pack("H*", "b8c0b8ecc0dfc4ea"); //EUC-JP
158 $this->assertSame(core_text::strlen($str, 'EUC-JP'), 4);
159
160 $str = pack("H*", "1b2442386c1b2842"); //ISO-2022-JP
161 $this->assertSame(core_text::strlen($str, 'ISO-2022-JP'), 1);
162 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
163 $this->assertSame(core_text::strlen($str, 'ISO-2022-JP'), 4);
164
165 $str = pack("H*", "8cea"); //SHIFT-JIS
166 $this->assertSame(core_text::strlen($str, 'SHIFT-JIS'), 1);
167 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
168 $this->assertSame(core_text::strlen($str, 'SHIFT-JIS'), 4);
169
170 $str = pack("H*", "cce5"); //GB2312
171 $this->assertSame(core_text::strlen($str, 'GB2312'), 1);
172 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
173 $this->assertSame(core_text::strlen($str, 'GB2312'), 4);
174
175 $str = pack("H*", "cce5"); //GB18030
176 $this->assertSame(core_text::strlen($str, 'GB18030'), 1);
177 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
178 $this->assertSame(core_text::strlen($str, 'GB18030'), 4);
179 }
180
181 /**
182 * Tests the static strtolower method
183 * @return void
184 */
185 public function test_strtolower() {
186 $str = "Žluťoučký koníček";
187 $low = 'žluťoučký koníček';
188 $this->assertSame(core_text::strtolower($str), $low);
189
190 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
191 $this->assertSame(core_text::strtolower($iso2, 'iso-8859-2'), core_text::convert($low, 'utf-8', 'iso-8859-2'));
192
193 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
194 $this->assertSame(core_text::strtolower($win, 'cp1250'), core_text::convert($low, 'utf-8', 'cp1250'));
195
196
197 $str = '言語設定';
198 $this->assertSame(core_text::strtolower($str), $str);
199
200 $str = '简体中文';
201 $this->assertSame(core_text::strtolower($str), $str);
202
203 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
204 $this->assertSame(core_text::strtolower($str, 'ISO-2022-JP'), $str);
205
206 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
207 $this->assertSame(core_text::strtolower($str, 'SHIFT-JIS'), $str);
208
209 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
210 $this->assertSame(core_text::strtolower($str, 'GB2312'), $str);
211
212 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
213 $this->assertSame(core_text::strtolower($str, 'GB18030'), $str);
214
215 // typo3 has problems with integers
216 $str = 1309528800;
217 $this->assertSame((string)$str, core_text::strtolower($str));
218 }
219
220 /**
221 * Tests the static strtoupper
222 * @return void
223 */
224 public function test_strtoupper() {
225 $str = "Žluťoučký koníček";
226 $up = 'ŽLUŤOUČKÝ KONÍČEK';
227 $this->assertSame(core_text::strtoupper($str), $up);
228
229 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
230 $this->assertSame(core_text::strtoupper($iso2, 'iso-8859-2'), core_text::convert($up, 'utf-8', 'iso-8859-2'));
231
232 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
233 $this->assertSame(core_text::strtoupper($win, 'cp1250'), core_text::convert($up, 'utf-8', 'cp1250'));
234
235
236 $str = '言語設定';
237 $this->assertSame(core_text::strtoupper($str), $str);
238
239 $str = '简体中文';
240 $this->assertSame(core_text::strtoupper($str), $str);
241
242 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
243 $this->assertSame(core_text::strtoupper($str, 'ISO-2022-JP'), $str);
244
245 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
246 $this->assertSame(core_text::strtoupper($str, 'SHIFT-JIS'), $str);
247
248 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
249 $this->assertSame(core_text::strtoupper($str, 'GB2312'), $str);
250
251 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
252 $this->assertSame(core_text::strtoupper($str, 'GB18030'), $str);
253 }
254
255 /**
256 * Tests the static strpos method
257 * @return void
258 */
259 public function test_strpos() {
260 $str = "Žluťoučký koníček";
261 $this->assertSame(core_text::strpos($str, 'koníč'), 10);
262 }
263
264 /**
265 * Tests the static strrpos
266 * @return void
267 */
268 public function test_strrpos() {
269 $str = "Žluťoučký koníček";
270 $this->assertSame(core_text::strrpos($str, 'o'), 11);
271 }
272
273 /**
274 * Tests the static specialtoascii method
275 * @return void
276 */
277 public function test_specialtoascii() {
278 $str = "Žluťoučký koníček";
279 $this->assertSame(core_text::specialtoascii($str), 'Zlutoucky konicek');
280 }
281
282 /**
283 * Tests the static encode_mimeheader method
284 * @return void
285 */
286 public function test_encode_mimeheader() {
287 $str = "Žluťoučký koníček";
288 $this->assertSame(core_text::encode_mimeheader($str), '=?utf-8?B?xb1sdcWlb3XEjWvDvSBrb27DrcSNZWs=?=');
289 }
290
291 /**
292 * Tests the static entities_to_utf8 method
293 * @return void
294 */
295 public function test_entities_to_utf8() {
296 $str = "&#x17d;lu&#x165;ou&#x10d;k&#xfd; kon&iacute;&#269;ek&copy;&quot;&amp;&lt;&gt;&sect;&laquo;";
297 $this->assertSame("Žluťoučký koníček©\"&<>§«", core_text::entities_to_utf8($str));
298 }
299
300 /**
301 * Tests the static utf8_to_entities method
302 * @return void
303 */
304 public function test_utf8_to_entities() {
305 $str = "&#x17d;luťoučký kon&iacute;ček&copy;&quot;&amp;&lt;&gt;&sect;&laquo;";
306 $this->assertSame("&#x17d;lu&#x165;ou&#x10d;k&#xfd; kon&iacute;&#x10d;ek&copy;&quot;&amp;&lt;&gt;&sect;&laquo;", core_text::utf8_to_entities($str));
307 $this->assertSame("&#381;lu&#357;ou&#269;k&#253; kon&iacute;&#269;ek&copy;&quot;&amp;&lt;&gt;&sect;&laquo;", core_text::utf8_to_entities($str, true));
308
309 $str = "&#381;luťoučký kon&iacute;ček&copy;&quot;&amp;&lt;&gt;&sect;&laquo;";
310 $this->assertSame("&#x17d;lu&#x165;ou&#x10d;k&#xfd; kon&#xed;&#x10d;ek&#xa9;\"&<>&#xa7;&#xab;", core_text::utf8_to_entities($str, false, true));
311 $this->assertSame("&#381;lu&#357;ou&#269;k&#253; kon&#237;&#269;ek&#169;\"&<>&#167;&#171;", core_text::utf8_to_entities($str, true, true));
312 }
313
314 /**
315 * Tests the static trim_utf8_bom method
316 * @return void
317 */
318 public function test_trim_utf8_bom() {
319 $bom = "\xef\xbb\xbf";
320 $str = "Žluťoučký koníček";
321 $this->assertSame(core_text::trim_utf8_bom($bom.$str.$bom), $str.$bom);
322 }
323
324 /**
325 * Tests the static get_encodings method
326 * @return void
327 */
328 public function test_get_encodings() {
329 $encodings = core_text::get_encodings();
330 $this->assertTrue(is_array($encodings));
331 $this->assertTrue(count($encodings) > 1);
332 $this->assertTrue(isset($encodings['UTF-8']));
333 }
334
335 /**
336 * Tests the static code2utf8 method
337 * @return void
338 */
339 public function test_code2utf8() {
340 $this->assertSame(core_text::code2utf8(381), 'Ž');
341 }
342
343 /**
344 * Tests the static utf8ord method
345 * @return void
346 */
347 public function test_utf8ord() {
348 $this->assertSame(core_text::utf8ord(''), ord(''));
349 $this->assertSame(core_text::utf8ord('f'), ord('f'));
350 $this->assertSame(core_text::utf8ord('α'), 0x03B1);
351 $this->assertSame(core_text::utf8ord('й'), 0x0439);
352 $this->assertSame(core_text::utf8ord('𯨟'), 0x2FA1F);
353 $this->assertSame(core_text::utf8ord('Ž'), 381);
354 }
355
356 /**
357 * Tests the static strtotitle method
358 * @return void
359 */
360 public function test_strtotitle() {
361 $str = "žluťoučký koníček";
362 $this->assertSame(core_text::strtotitle($str), "Žluťoučký Koníček");
363 }
364
365 public function test_deprecated_textlib() {
366 $this->assertSame(core_text::strtolower('HUH'), textlib::strtolower('HUH'));
367 }
368
369 /**
370 * Tests the deprecated method of textlib that still require an instance.
371 * @return void
372 */
373 public function test_deprecated_textlib_get_instance() {
374 $textlib = textlib_get_instance();
375 $this->assertDebuggingCalled();
376 $this->assertSame($textlib->substr('abc', 1, 1), 'b');
377 $this->assertSame($textlib->strlen('abc'), 3);
378 $this->assertSame($textlib->strtoupper('Abc'), 'ABC');
379 $this->assertSame($textlib->strtolower('Abc'), 'abc');
380 $this->assertSame($textlib->strpos('abc', 'a'), 0);
381 $this->assertSame($textlib->strpos('abc', 'd'), false);
382 $this->assertSame($textlib->strrpos('abcabc', 'a'), 3);
383 $this->assertSame($textlib->specialtoascii('ábc'), 'abc');
384 $this->assertSame($textlib->strtotitle('abc ABC'), 'Abc Abc');
385 }
386}
387