MDL-19270 core_text: added unit tests for strrev
[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/**
2af73e22 18 * core_text unit tests.
d534708f
PS
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/**
2af73e22 30 * Unit tests for our utf-8 aware text processing.
d534708f
PS
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 /**
2af73e22 40 * Tests the static parse charset method.
d534708f
PS
41 */
42 public function test_parse_charset() {
2af73e22
PS
43 $this->assertSame('windows-1250', core_text::parse_charset('Cp1250'));
44 // Does typo3 work? Some encoding moodle does not use.
45 $this->assertSame('windows-1252', core_text::parse_charset('ms-ansi'));
d534708f
PS
46 }
47
48 /**
2af73e22 49 * Tests the static convert method.
d534708f
PS
50 */
51 public function test_convert() {
52 $utf8 = "Žluťoučký koníček";
53 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
54 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
2af73e22
PS
55 $this->assertSame($iso2, core_text::convert($utf8, 'utf-8', 'iso-8859-2'));
56 $this->assertSame($utf8, core_text::convert($iso2, 'iso-8859-2', 'utf-8'));
57 $this->assertSame($win, core_text::convert($utf8, 'utf-8', 'win-1250'));
58 $this->assertSame($utf8, core_text::convert($win, 'win-1250', 'utf-8'));
59 $this->assertSame($iso2, core_text::convert($win, 'win-1250', 'iso-8859-2'));
60 $this->assertSame($win, core_text::convert($iso2, 'iso-8859-2', 'win-1250'));
61 $this->assertSame($iso2, core_text::convert($iso2, 'iso-8859-2', 'iso-8859-2'));
62 $this->assertSame($win, core_text::convert($win, 'win-1250', 'cp1250'));
63 $this->assertSame($utf8, core_text::convert($utf8, 'utf-8', 'utf-8'));
d534708f
PS
64
65 $utf8 = '言語設定';
2af73e22
PS
66 $str = pack("H*", "b8c0b8ecc0dfc4ea"); // EUC-JP
67 $this->assertSame($str, core_text::convert($utf8, 'utf-8', 'EUC-JP'));
68 $this->assertSame($utf8, core_text::convert($str, 'EUC-JP', 'utf-8'));
69 $this->assertSame($utf8, core_text::convert($utf8, 'utf-8', 'utf-8'));
d534708f 70
2af73e22
PS
71 $str = pack("H*", "1b24423840386c405f446a1b2842"); // ISO-2022-JP
72 $this->assertSame($str, core_text::convert($utf8, 'utf-8', 'ISO-2022-JP'));
73 $this->assertSame($utf8, core_text::convert($str, 'ISO-2022-JP', 'utf-8'));
74 $this->assertSame($utf8, core_text::convert($utf8, 'utf-8', 'utf-8'));
d534708f 75
2af73e22
PS
76 $str = pack("H*", "8cbe8cea90dd92e8"); // SHIFT-JIS
77 $this->assertSame($str, core_text::convert($utf8, 'utf-8', 'SHIFT-JIS'));
78 $this->assertSame($utf8, core_text::convert($str, 'SHIFT-JIS', 'utf-8'));
79 $this->assertSame($utf8, core_text::convert($utf8, 'utf-8', 'utf-8'));
d534708f
PS
80
81 $utf8 = '简体中文';
2af73e22
PS
82 $str = pack("H*", "bcf2cce5d6d0cec4"); // GB2312
83 $this->assertSame($str, core_text::convert($utf8, 'utf-8', 'GB2312'));
84 $this->assertSame($utf8, core_text::convert($str, 'GB2312', 'utf-8'));
85 $this->assertSame($utf8, core_text::convert($utf8, 'utf-8', 'utf-8'));
86
87 $str = pack("H*", "bcf2cce5d6d0cec4"); // GB18030
88 $this->assertSame($str, core_text::convert($utf8, 'utf-8', 'GB18030'));
89 $this->assertSame($utf8, core_text::convert($str, 'GB18030', 'utf-8'));
90 $this->assertSame($utf8, core_text::convert($utf8, 'utf-8', 'utf-8'));
ef5e1151
PS
91
92 $utf8 = "Žluťoučký koníček";
e734d885 93 $this->assertSame('Zlutoucky konicek', core_text::convert($utf8, 'utf-8', 'ascii'));
ef5e1151 94 $this->assertSame($utf8, core_text::convert($utf8.chr(130), 'utf-8', 'utf-8'));
e734d885
PS
95 $utf8 = "Der eine stößt den Speer zum Mann";
96 $this->assertSame('Der eine stoesst den Speer zum Mann', core_text::convert($utf8, 'utf-8', 'ascii'));
97 $iso1 = core_text::convert($utf8, 'utf-8', 'iso-8859-1');
98 $this->assertSame('Der eine stoesst den Speer zum Mann', core_text::convert($iso1, 'iso-8859-1', 'ascii'));
d534708f
PS
99 }
100
101 /**
2af73e22 102 * Tests the static sub string method.
d534708f
PS
103 */
104 public function test_substr() {
105 $str = "Žluťoučký koníček";
2af73e22
PS
106 $this->assertSame($str, core_text::substr($str, 0));
107 $this->assertSame('luťoučký koníček', core_text::substr($str, 1));
108 $this->assertSame('luť', core_text::substr($str, 1, 3));
109 $this->assertSame($str, core_text::substr($str, 0, 100));
110 $this->assertSame('če', core_text::substr($str, -3, 2));
d534708f
PS
111
112 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
2af73e22
PS
113 $this->assertSame(core_text::convert('luť', 'utf-8', 'iso-8859-2'), core_text::substr($iso2, 1, 3, 'iso-8859-2'));
114 $this->assertSame(core_text::convert($str, 'utf-8', 'iso-8859-2'), core_text::substr($iso2, 0, 100, 'iso-8859-2'));
115 $this->assertSame(core_text::convert('če', 'utf-8', 'iso-8859-2'), core_text::substr($iso2, -3, 2, 'iso-8859-2'));
d534708f
PS
116
117 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
2af73e22
PS
118 $this->assertSame(core_text::convert('luť', 'utf-8', 'cp1250'), core_text::substr($win, 1, 3, 'cp1250'));
119 $this->assertSame(core_text::convert($str, 'utf-8', 'cp1250'), core_text::substr($win, 0, 100, 'cp1250'));
120 $this->assertSame(core_text::convert('če', 'utf-8', 'cp1250'), core_text::substr($win, -3, 2, 'cp1250'));
d534708f 121
2af73e22
PS
122 $str = pack("H*", "b8c0b8ecc0dfc4ea"); // EUC-JP
123 $s = pack("H*", "b8ec"); // EUC-JP
124 $this->assertSame($s, core_text::substr($str, 1, 1, 'EUC-JP'));
d534708f 125
2af73e22
PS
126 $str = pack("H*", "1b24423840386c405f446a1b2842"); // ISO-2022-JP
127 $s = pack("H*", "1b2442386c1b2842"); // ISO-2022-JP
128 $this->assertSame($s, core_text::substr($str, 1, 1, 'ISO-2022-JP'));
d534708f 129
2af73e22
PS
130 $str = pack("H*", "8cbe8cea90dd92e8"); // SHIFT-JIS
131 $s = pack("H*", "8cea"); // SHIFT-JIS
132 $this->assertSame($s, core_text::substr($str, 1, 1, 'SHIFT-JIS'));
d534708f 133
2af73e22
PS
134 $str = pack("H*", "bcf2cce5d6d0cec4"); // GB2312
135 $s = pack("H*", "cce5"); // GB2312
136 $this->assertSame($s, core_text::substr($str, 1, 1, 'GB2312'));
d534708f 137
2af73e22
PS
138 $str = pack("H*", "bcf2cce5d6d0cec4"); // GB18030
139 $s = pack("H*", "cce5"); // GB18030
140 $this->assertSame($s, core_text::substr($str, 1, 1, 'GB18030'));
d534708f
PS
141 }
142
143 /**
2af73e22 144 * Tests the static string length method.
d534708f
PS
145 */
146 public function test_strlen() {
147 $str = "Žluťoučký koníček";
2af73e22 148 $this->assertSame(17, core_text::strlen($str));
d534708f
PS
149
150 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
2af73e22 151 $this->assertSame(17, core_text::strlen($iso2, 'iso-8859-2'));
d534708f
PS
152
153 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
2af73e22
PS
154 $this->assertSame(17, core_text::strlen($win, 'cp1250'));
155
156 $str = pack("H*", "b8ec"); // EUC-JP
157 $this->assertSame(1, core_text::strlen($str, 'EUC-JP'));
158 $str = pack("H*", "b8c0b8ecc0dfc4ea"); // EUC-JP
159 $this->assertSame(4, core_text::strlen($str, 'EUC-JP'));
160
161 $str = pack("H*", "1b2442386c1b2842"); // ISO-2022-JP
162 $this->assertSame(1, core_text::strlen($str, 'ISO-2022-JP'));
163 $str = pack("H*", "1b24423840386c405f446a1b2842"); // ISO-2022-JP
164 $this->assertSame(4, core_text::strlen($str, 'ISO-2022-JP'));
165
166 $str = pack("H*", "8cea"); // SHIFT-JIS
167 $this->assertSame(1, core_text::strlen($str, 'SHIFT-JIS'));
168 $str = pack("H*", "8cbe8cea90dd92e8"); // SHIFT-JIS
169 $this->assertSame(4, core_text::strlen($str, 'SHIFT-JIS'));
170
171 $str = pack("H*", "cce5"); // GB2312
172 $this->assertSame(1, core_text::strlen($str, 'GB2312'));
173 $str = pack("H*", "bcf2cce5d6d0cec4"); // GB2312
174 $this->assertSame(4, core_text::strlen($str, 'GB2312'));
175
176 $str = pack("H*", "cce5"); // GB18030
177 $this->assertSame(1, core_text::strlen($str, 'GB18030'));
178 $str = pack("H*", "bcf2cce5d6d0cec4"); // GB18030
179 $this->assertSame(4, core_text::strlen($str, 'GB18030'));
d534708f
PS
180 }
181
182 /**
2af73e22 183 * Tests the static strtolower method.
d534708f
PS
184 */
185 public function test_strtolower() {
186 $str = "Žluťoučký koníček";
187 $low = 'žluťoučký koníček';
2af73e22 188 $this->assertSame($low, core_text::strtolower($str));
d534708f
PS
189
190 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
2af73e22 191 $this->assertSame(core_text::convert($low, 'utf-8', 'iso-8859-2'), core_text::strtolower($iso2, 'iso-8859-2'));
d534708f
PS
192
193 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
2af73e22 194 $this->assertSame(core_text::convert($low, 'utf-8', 'cp1250'), core_text::strtolower($win, 'cp1250'));
d534708f
PS
195
196 $str = '言語設定';
2af73e22 197 $this->assertSame($str, core_text::strtolower($str));
d534708f
PS
198
199 $str = '简体中文';
2af73e22 200 $this->assertSame($str, core_text::strtolower($str));
d534708f 201
2af73e22
PS
202 $str = pack("H*", "1b24423840386c405f446a1b2842"); // ISO-2022-JP
203 $this->assertSame($str, core_text::strtolower($str, 'ISO-2022-JP'));
d534708f 204
2af73e22
PS
205 $str = pack("H*", "8cbe8cea90dd92e8"); // SHIFT-JIS
206 $this->assertSame($str, core_text::strtolower($str, 'SHIFT-JIS'));
d534708f 207
2af73e22
PS
208 $str = pack("H*", "bcf2cce5d6d0cec4"); // GB2312
209 $this->assertSame($str, core_text::strtolower($str, 'GB2312'));
d534708f 210
2af73e22
PS
211 $str = pack("H*", "bcf2cce5d6d0cec4"); // GB18030
212 $this->assertSame($str, core_text::strtolower($str, 'GB18030'));
d534708f 213
2af73e22 214 // Typo3 has problems with integers.
d534708f
PS
215 $str = 1309528800;
216 $this->assertSame((string)$str, core_text::strtolower($str));
217 }
218
219 /**
2af73e22 220 * Tests the static strtoupper.
d534708f
PS
221 */
222 public function test_strtoupper() {
223 $str = "Žluťoučký koníček";
224 $up = 'ŽLUŤOUČKÝ KONÍČEK';
2af73e22 225 $this->assertSame($up, core_text::strtoupper($str));
d534708f
PS
226
227 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
2af73e22 228 $this->assertSame(core_text::convert($up, 'utf-8', 'iso-8859-2'), core_text::strtoupper($iso2, 'iso-8859-2'));
d534708f
PS
229
230 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
2af73e22 231 $this->assertSame(core_text::convert($up, 'utf-8', 'cp1250'), core_text::strtoupper($win, 'cp1250'));
d534708f
PS
232
233 $str = '言語設定';
2af73e22 234 $this->assertSame($str, core_text::strtoupper($str));
d534708f
PS
235
236 $str = '简体中文';
2af73e22 237 $this->assertSame($str, core_text::strtoupper($str));
d534708f 238
2af73e22
PS
239 $str = pack("H*", "1b24423840386c405f446a1b2842"); // ISO-2022-JP
240 $this->assertSame($str, core_text::strtoupper($str, 'ISO-2022-JP'));
d534708f 241
2af73e22
PS
242 $str = pack("H*", "8cbe8cea90dd92e8"); // SHIFT-JIS
243 $this->assertSame($str, core_text::strtoupper($str, 'SHIFT-JIS'));
d534708f 244
2af73e22
PS
245 $str = pack("H*", "bcf2cce5d6d0cec4"); // GB2312
246 $this->assertSame($str, core_text::strtoupper($str, 'GB2312'));
d534708f 247
2af73e22
PS
248 $str = pack("H*", "bcf2cce5d6d0cec4"); // GB18030
249 $this->assertSame($str, core_text::strtoupper($str, 'GB18030'));
d534708f
PS
250 }
251
1c194014
SH
252 /**
253 * Test the strrev method.
254 */
255 public function test_strrev() {
256 $strings = array(
257 "Žluťoučký koníček" => "kečínok ýkčuoťulŽ",
258 'ŽLUŤOUČKÝ KONÍČEK' => "KEČÍNOK ÝKČUOŤULŽ",
259 '言語設定' => '定設語言',
260 '简体中文' => '文中体简',
261 "Der eine stößt den Speer zum Mann" => "nnaM muz reepS ned tßöts enie reD"
262 );
263 foreach ($strings as $before => $after) {
264 // Make sure we can reverse it both ways and that it comes out the same.
265 $this->assertSame($after, core_text::strrev($before));
266 $this->assertSame($before, core_text::strrev($after));
267 // Reverse it twice to be doubly sure.
268 $this->assertSame($after, core_text::strrev(core_text::strrev($after)));
269 }
270 }
271
d534708f 272 /**
2af73e22 273 * Tests the static strpos method.
d534708f
PS
274 */
275 public function test_strpos() {
276 $str = "Žluťoučký koníček";
2af73e22 277 $this->assertSame(10, core_text::strpos($str, 'koníč'));
d534708f
PS
278 }
279
280 /**
2af73e22 281 * Tests the static strrpos.
d534708f
PS
282 */
283 public function test_strrpos() {
284 $str = "Žluťoučký koníček";
2af73e22 285 $this->assertSame(11, core_text::strrpos($str, 'o'));
d534708f
PS
286 }
287
288 /**
2af73e22 289 * Tests the static specialtoascii method.
d534708f
PS
290 */
291 public function test_specialtoascii() {
292 $str = "Žluťoučký koníček";
2af73e22 293 $this->assertSame('Zlutoucky konicek', core_text::specialtoascii($str));
d534708f
PS
294 }
295
296 /**
2af73e22 297 * Tests the static encode_mimeheader method.
d534708f
PS
298 */
299 public function test_encode_mimeheader() {
300 $str = "Žluťoučký koníček";
2af73e22 301 $this->assertSame('=?utf-8?B?xb1sdcWlb3XEjWvDvSBrb27DrcSNZWs=?=', core_text::encode_mimeheader($str));
d534708f
PS
302 }
303
304 /**
2af73e22 305 * Tests the static entities_to_utf8 method.
d534708f
PS
306 */
307 public function test_entities_to_utf8() {
308 $str = "&#x17d;lu&#x165;ou&#x10d;k&#xfd; kon&iacute;&#269;ek&copy;&quot;&amp;&lt;&gt;&sect;&laquo;";
309 $this->assertSame("Žluťoučký koníček©\"&<>§«", core_text::entities_to_utf8($str));
310 }
311
312 /**
2af73e22 313 * Tests the static utf8_to_entities method.
d534708f
PS
314 */
315 public function test_utf8_to_entities() {
316 $str = "&#x17d;luťoučký kon&iacute;ček&copy;&quot;&amp;&lt;&gt;&sect;&laquo;";
317 $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));
318 $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));
319
320 $str = "&#381;luťoučký kon&iacute;ček&copy;&quot;&amp;&lt;&gt;&sect;&laquo;";
321 $this->assertSame("&#x17d;lu&#x165;ou&#x10d;k&#xfd; kon&#xed;&#x10d;ek&#xa9;\"&<>&#xa7;&#xab;", core_text::utf8_to_entities($str, false, true));
322 $this->assertSame("&#381;lu&#357;ou&#269;k&#253; kon&#237;&#269;ek&#169;\"&<>&#167;&#171;", core_text::utf8_to_entities($str, true, true));
323 }
324
325 /**
2af73e22 326 * Tests the static trim_utf8_bom method.
d534708f
PS
327 */
328 public function test_trim_utf8_bom() {
329 $bom = "\xef\xbb\xbf";
330 $str = "Žluťoučký koníček";
2af73e22 331 $this->assertSame($str.$bom, core_text::trim_utf8_bom($bom.$str.$bom));
d534708f
PS
332 }
333
334 /**
2af73e22 335 * Tests the static get_encodings method.
d534708f
PS
336 */
337 public function test_get_encodings() {
338 $encodings = core_text::get_encodings();
339 $this->assertTrue(is_array($encodings));
340 $this->assertTrue(count($encodings) > 1);
341 $this->assertTrue(isset($encodings['UTF-8']));
342 }
343
344 /**
2af73e22 345 * Tests the static code2utf8 method.
d534708f
PS
346 */
347 public function test_code2utf8() {
2af73e22 348 $this->assertSame('Ž', core_text::code2utf8(381));
d534708f
PS
349 }
350
351 /**
2af73e22 352 * Tests the static utf8ord method.
d534708f
PS
353 */
354 public function test_utf8ord() {
2af73e22
PS
355 $this->assertSame(ord(''), core_text::utf8ord(''));
356 $this->assertSame(ord('f'), core_text::utf8ord('f'));
357 $this->assertSame(0x03B1, core_text::utf8ord('α'));
358 $this->assertSame(0x0439, core_text::utf8ord('й'));
359 $this->assertSame(0x2FA1F, core_text::utf8ord('𯨟'));
360 $this->assertSame(381, core_text::utf8ord('Ž'));
d534708f
PS
361 }
362
363 /**
2af73e22 364 * Tests the static strtotitle method.
d534708f
PS
365 */
366 public function test_strtotitle() {
367 $str = "žluťoučký koníček";
2af73e22 368 $this->assertSame("Žluťoučký Koníček", core_text::strtotitle($str));
d534708f
PS
369 }
370
371 public function test_deprecated_textlib() {
2af73e22 372 $this->assertSame(textlib::strtolower('HUH'), core_text::strtolower('HUH'));
78c12cdb 373 $this->assertDebuggingCalled(null, null, 'This fails if any other test uses the deprecated textlib class.');
d534708f
PS
374 }
375
376 /**
377 * Tests the deprecated method of textlib that still require an instance.
d534708f
PS
378 */
379 public function test_deprecated_textlib_get_instance() {
380 $textlib = textlib_get_instance();
381 $this->assertDebuggingCalled();
382 $this->assertSame($textlib->substr('abc', 1, 1), 'b');
383 $this->assertSame($textlib->strlen('abc'), 3);
384 $this->assertSame($textlib->strtoupper('Abc'), 'ABC');
385 $this->assertSame($textlib->strtolower('Abc'), 'abc');
386 $this->assertSame($textlib->strpos('abc', 'a'), 0);
387 $this->assertSame($textlib->strpos('abc', 'd'), false);
388 $this->assertSame($textlib->strrpos('abcabc', 'a'), 3);
389 $this->assertSame($textlib->specialtoascii('ábc'), 'abc');
390 $this->assertSame($textlib->strtotitle('abc ABC'), 'Abc Abc');
391 }
aa87fe80
FM
392
393 /**
394 * Test strrchr.
395 */
396 public function test_strrchr() {
397 $str = "Žluťoučký koníček";
398 $this->assertSame('koníček', core_text::strrchr($str, 'koní'));
399 $this->assertSame('Žluťoučký ', core_text::strrchr($str, 'koní', true));
400 $this->assertFalse(core_text::strrchr($str, 'A'));
401 $this->assertFalse(core_text::strrchr($str, 'ç', true));
402 }
d534708f
PS
403}
404