MDL-31857 phpunit: PHPDoc and typo fixes during integration
[moodle.git] / lib / tests / textlib_test.php
CommitLineData
5bd40408
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/**
7aea08e1 18 * textlib unit tests
5bd40408 19 *
7aea08e1 20 * @package core
5bd40408
PS
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
7aea08e1 33 * @category phpunit
5bd40408
PS
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_textlib_testcase extends basic_testcase {
38
7aea08e1
SH
39 /**
40 * Tests the static parse charset method
41 * @return void
42 */
5bd40408
PS
43 public function test_parse_charset() {
44 $this->assertSame(textlib::parse_charset('Cp1250'), 'windows-1250');
45 // does typo3 work? some encoding moodle does not use
46 $this->assertSame(textlib::parse_charset('ms-ansi'), 'windows-1252');
47 }
48
7aea08e1
SH
49 /**
50 * Tests the static convert method
51 * @return void
52 */
5bd40408
PS
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(textlib::convert($utf8, 'utf-8', 'iso-8859-2'), $iso2);
58 $this->assertSame(textlib::convert($iso2, 'iso-8859-2', 'utf-8'), $utf8);
59 $this->assertSame(textlib::convert($utf8, 'utf-8', 'win-1250'), $win);
60 $this->assertSame(textlib::convert($win, 'win-1250', 'utf-8'), $utf8);
61 $this->assertSame(textlib::convert($win, 'win-1250', 'iso-8859-2'), $iso2);
62 $this->assertSame(textlib::convert($iso2, 'iso-8859-2', 'win-1250'), $win);
63 $this->assertSame(textlib::convert($iso2, 'iso-8859-2', 'iso-8859-2'), $iso2);
64 $this->assertSame(textlib::convert($win, 'win-1250', 'cp1250'), $win);
65
66
67 $utf8 = '言語設定';
68 $str = pack("H*", "b8c0b8ecc0dfc4ea"); //EUC-JP
69 $this->assertSame(textlib::convert($utf8, 'utf-8', 'EUC-JP'), $str);
70 $this->assertSame(textlib::convert($str, 'EUC-JP', 'utf-8'), $utf8);
71
72 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
73 $this->assertSame(textlib::convert($utf8, 'utf-8', 'ISO-2022-JP'), $str);
74 $this->assertSame(textlib::convert($str, 'ISO-2022-JP', 'utf-8'), $utf8);
75
76 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
77 $this->assertSame(textlib::convert($utf8, 'utf-8', 'SHIFT-JIS'), $str);
78 $this->assertSame(textlib::convert($str, 'SHIFT-JIS', 'utf-8'), $utf8);
79
80 $utf8 = '简体中文';
81 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
82 $this->assertSame(textlib::convert($utf8, 'utf-8', 'GB2312'), $str);
83 $this->assertSame(textlib::convert($str, 'GB2312', 'utf-8'), $utf8);
84
85 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
86 $this->assertSame(textlib::convert($utf8, 'utf-8', 'GB18030'), $str);
87 $this->assertSame(textlib::convert($str, 'GB18030', 'utf-8'), $utf8);
88 }
89
7aea08e1
SH
90 /**
91 * Tests the static sub string method
92 * @return void
93 */
5bd40408
PS
94 public function test_substr() {
95 $str = "Žluťoučký koníček";
96 $this->assertSame(textlib::substr($str, 0), $str);
97 $this->assertSame(textlib::substr($str, 1), 'luťoučký koníček');
98 $this->assertSame(textlib::substr($str, 1, 3), 'luť');
99 $this->assertSame(textlib::substr($str, 0, 100), $str);
100 $this->assertSame(textlib::substr($str, -3, 2), 'če');
101
102 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
103 $this->assertSame(textlib::substr($iso2, 1, 3, 'iso-8859-2'), textlib::convert('luť', 'utf-8', 'iso-8859-2'));
104 $this->assertSame(textlib::substr($iso2, 0, 100, 'iso-8859-2'), textlib::convert($str, 'utf-8', 'iso-8859-2'));
105 $this->assertSame(textlib::substr($iso2, -3, 2, 'iso-8859-2'), textlib::convert('če', 'utf-8', 'iso-8859-2'));
106
107 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
108 $this->assertSame(textlib::substr($win, 1, 3, 'cp1250'), textlib::convert('luť', 'utf-8', 'cp1250'));
109 $this->assertSame(textlib::substr($win, 0, 100, 'cp1250'), textlib::convert($str, 'utf-8', 'cp1250'));
110 $this->assertSame(textlib::substr($win, -3, 2, 'cp1250'), textlib::convert('če', 'utf-8', 'cp1250'));
111
112
113 $str = pack("H*", "b8c0b8ecc0dfc4ea"); //EUC-JP
114 $s = pack("H*", "b8ec"); //EUC-JP
115 $this->assertSame(textlib::substr($str, 1, 1, 'EUC-JP'), $s);
116
117 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
118 $s = pack("H*", "1b2442386c1b2842"); //ISO-2022-JP
119 $this->assertSame(textlib::substr($str, 1, 1, 'ISO-2022-JP'), $s);
120
121 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
122 $s = pack("H*", "8cea"); //SHIFT-JIS
123 $this->assertSame(textlib::substr($str, 1, 1, 'SHIFT-JIS'), $s);
124
125 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
126 $s = pack("H*", "cce5"); //GB2312
127 $this->assertSame(textlib::substr($str, 1, 1, 'GB2312'), $s);
128
129 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
130 $s = pack("H*", "cce5"); //GB18030
131 $this->assertSame(textlib::substr($str, 1, 1, 'GB18030'), $s);
132 }
133
7aea08e1
SH
134 /**
135 * Tests the static string length method
136 * @return void
137 */
5bd40408
PS
138 public function test_strlen() {
139 $str = "Žluťoučký koníček";
140 $this->assertSame(textlib::strlen($str), 17);
141
142 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
143 $this->assertSame(textlib::strlen($iso2, 'iso-8859-2'), 17);
144
145 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
146 $this->assertSame(textlib::strlen($win, 'cp1250'), 17);
147
148
149 $str = pack("H*", "b8ec"); //EUC-JP
150 $this->assertSame(textlib::strlen($str, 'EUC-JP'), 1);
151 $str = pack("H*", "b8c0b8ecc0dfc4ea"); //EUC-JP
152 $this->assertSame(textlib::strlen($str, 'EUC-JP'), 4);
153
154 $str = pack("H*", "1b2442386c1b2842"); //ISO-2022-JP
155 $this->assertSame(textlib::strlen($str, 'ISO-2022-JP'), 1);
156 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
157 $this->assertSame(textlib::strlen($str, 'ISO-2022-JP'), 4);
158
159 $str = pack("H*", "8cea"); //SHIFT-JIS
160 $this->assertSame(textlib::strlen($str, 'SHIFT-JIS'), 1);
161 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
162 $this->assertSame(textlib::strlen($str, 'SHIFT-JIS'), 4);
163
164 $str = pack("H*", "cce5"); //GB2312
165 $this->assertSame(textlib::strlen($str, 'GB2312'), 1);
166 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
167 $this->assertSame(textlib::strlen($str, 'GB2312'), 4);
168
169 $str = pack("H*", "cce5"); //GB18030
170 $this->assertSame(textlib::strlen($str, 'GB18030'), 1);
171 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
172 $this->assertSame(textlib::strlen($str, 'GB18030'), 4);
173 }
174
7aea08e1
SH
175 /**
176 * Tests the static strtolower method
177 * @return void
178 */
5bd40408
PS
179 public function test_strtolower() {
180 $str = "Žluťoučký koníček";
181 $low = 'žluťoučký koníček';
182 $this->assertSame(textlib::strtolower($str), $low);
183
184 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
185 $this->assertSame(textlib::strtolower($iso2, 'iso-8859-2'), textlib::convert($low, 'utf-8', 'iso-8859-2'));
186
187 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
188 $this->assertSame(textlib::strtolower($win, 'cp1250'), textlib::convert($low, 'utf-8', 'cp1250'));
189
190
191 $str = '言語設定';
192 $this->assertSame(textlib::strtolower($str), $str);
193
194 $str = '简体中文';
195 $this->assertSame(textlib::strtolower($str), $str);
196
197 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
198 $this->assertSame(textlib::strtolower($str, 'ISO-2022-JP'), $str);
199
200 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
201 $this->assertSame(textlib::strtolower($str, 'SHIFT-JIS'), $str);
202
203 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
204 $this->assertSame(textlib::strtolower($str, 'GB2312'), $str);
205
206 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
207 $this->assertSame(textlib::strtolower($str, 'GB18030'), $str);
208 }
209
7aea08e1
SH
210 /**
211 * Tests the static strtoupper
212 * @return void
213 */
5bd40408
PS
214 public function test_strtoupper() {
215 $str = "Žluťoučký koníček";
216 $up = 'ŽLUŤOUČKÝ KONÍČEK';
217 $this->assertSame(textlib::strtoupper($str), $up);
218
219 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
220 $this->assertSame(textlib::strtoupper($iso2, 'iso-8859-2'), textlib::convert($up, 'utf-8', 'iso-8859-2'));
221
222 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
223 $this->assertSame(textlib::strtoupper($win, 'cp1250'), textlib::convert($up, 'utf-8', 'cp1250'));
224
225
226 $str = '言語設定';
227 $this->assertSame(textlib::strtoupper($str), $str);
228
229 $str = '简体中文';
230 $this->assertSame(textlib::strtoupper($str), $str);
231
232 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
233 $this->assertSame(textlib::strtoupper($str, 'ISO-2022-JP'), $str);
234
235 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
236 $this->assertSame(textlib::strtoupper($str, 'SHIFT-JIS'), $str);
237
238 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
239 $this->assertSame(textlib::strtoupper($str, 'GB2312'), $str);
240
241 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
242 $this->assertSame(textlib::strtoupper($str, 'GB18030'), $str);
243 }
244
7aea08e1
SH
245 /**
246 * Tests the static strpos method
247 * @return void
248 */
5bd40408
PS
249 public function test_strpos() {
250 $str = "Žluťoučký koníček";
251 $this->assertSame(textlib::strpos($str, 'koníč'), 10);
252 }
253
7aea08e1
SH
254 /**
255 * Tests the static strrpos
256 * @return void
257 */
5bd40408
PS
258 public function test_strrpos() {
259 $str = "Žluťoučký koníček";
260 $this->assertSame(textlib::strrpos($str, 'o'), 11);
261 }
262
7aea08e1
SH
263 /**
264 * Tests the static specialtoascii method
265 * @return void
266 */
5bd40408
PS
267 public function test_specialtoascii() {
268 $str = "Žluťoučký koníček";
269 $this->assertSame(textlib::specialtoascii($str), 'Zlutoucky konicek');
270 }
271
7aea08e1
SH
272 /**
273 * Tests the static encode_mimeheader method
274 * @return void
275 */
5bd40408
PS
276 public function test_encode_mimeheader() {
277 $str = "Žluťoučký koníček";
278 $this->assertSame(textlib::encode_mimeheader($str), '=?utf-8?B?xb1sdcWlb3XEjWvDvSBrb27DrcSNZWs=?=');
279 }
280
7aea08e1
SH
281 /**
282 * Tests the static entities_to_utf8 method
283 * @return void
284 */
5bd40408
PS
285 public function test_entities_to_utf8() {
286 $str = "&#x17d;lu&#x165;ou&#x10d;k&#xfd; kon&#237;&#269;ek";
287 $this->assertSame(textlib::entities_to_utf8($str), "Žluťoučký koníček");
288 }
289
7aea08e1
SH
290 /**
291 * Tests the static utf8_to_entities method
292 * @return void
293 */
5bd40408
PS
294 public function test_utf8_to_entities() {
295 $str = "Žluťoučký koníček";
296 $this->assertSame(textlib::utf8_to_entities($str), "&#x17d;lu&#x165;ou&#x10d;k&#xfd; kon&#xed;&#x10d;ek");
297 $this->assertSame(textlib::utf8_to_entities($str, true), "&#381;lu&#357;ou&#269;k&#253; kon&#237;&#269;ek");
298
299 }
300
7aea08e1
SH
301 /**
302 * Tests the static trim_utf8_bom method
303 * @return void
304 */
5bd40408
PS
305 public function test_trim_utf8_bom() {
306 $bom = "\xef\xbb\xbf";
307 $str = "Žluťoučký koníček";
308 $this->assertSame(textlib::trim_utf8_bom($bom.$str.$bom), $str.$bom);
309 }
310
7aea08e1
SH
311 /**
312 * Tests the static get_encodings method
313 * @return void
314 */
5bd40408
PS
315 public function test_get_encodings() {
316 $encodings = textlib::get_encodings();
317 $this->assertTrue(is_array($encodings));
318 $this->assertTrue(count($encodings) > 1);
319 $this->assertTrue(isset($encodings['UTF-8']));
320 }
321
7aea08e1
SH
322 /**
323 * Tests the static code2utf8 method
324 * @return void
325 */
5bd40408
PS
326 public function test_code2utf8() {
327 $this->assertSame(textlib::code2utf8(381), 'Ž');
328 }
329
7aea08e1
SH
330 /**
331 * Tests the static strtotitle method
332 * @return void
333 */
5bd40408
PS
334 public function test_strtotitle() {
335 $str = "žluťoučký koníček";
336 $this->assertSame(textlib::strtotitle($str), "Žluťoučký Koníček");
337 }
338
7aea08e1
SH
339 /**
340 * Tests the deprecated method of textlib that still require an instance.
341 * @return void
342 */
5bd40408
PS
343 public function test_deprecated_textlib_get_instance() {
344 ob_start();
345 $textlib = textlib_get_instance();
346 $output = ob_get_contents();
347 $this->assertNotEmpty($output);
348 ob_end_clean();
349 $this->assertSame($textlib->substr('abc', 1, 1), 'b');
350 $this->assertSame($textlib->strlen('abc'), 3);
351 $this->assertSame($textlib->strtoupper('Abc'), 'ABC');
352 $this->assertSame($textlib->strtolower('Abc'), 'abc');
353 $this->assertSame($textlib->strpos('abc', 'a'), 0);
354 $this->assertSame($textlib->strpos('abc', 'd'), false);
355 $this->assertSame($textlib->strrpos('abcabc', 'a'), 3);
356 $this->assertSame($textlib->specialtoascii('ábc'), 'abc');
357 $this->assertSame($textlib->strtotitle('abc ABC'), 'Abc Abc');
358 }
359}
360
361
362/**
363 * Unit tests for our utf-8 aware collator.
364 *
365 * Used for sorting.
366 *
367 * @package core
7aea08e1 368 * @category phpunit
5bd40408
PS
369 * @copyright 2011 Sam Hemelryk
370 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
371 */
372class collatorlib_testcase extends basic_testcase {
373
7aea08e1
SH
374 /**
375 * @var string The initial lang, stored because we change it during testing
376 */
5bd40408 377 protected $initiallang = null;
7aea08e1
SH
378
379 /**
380 * @var string The last error that has occured
381 */
5bd40408
PS
382 protected $error = null;
383
7aea08e1
SH
384 /**
385 * Prepares things for this test case
386 * @return void
387 */
5bd40408
PS
388 public function setUp() {
389 global $SESSION;
390 if (isset($SESSION->lang)) {
391 $this->initiallang = $SESSION->lang;
392 }
393 $SESSION->lang = 'en'; // make sure we test en language to get consistent results, hopefully all systems have this locale
394 if (extension_loaded('intl')) {
395 $this->error = 'Collation aware sorting not supported';
396 } else {
397 $this->error = 'Collation aware sorting not supported, PHP extension "intl" is not available.';
398 }
399 parent::setUp();
400 }
7aea08e1
SH
401
402 /**
403 * Cleans things up after this test case has run
404 * @return void
405 */
5bd40408
PS
406 public function tearDown() {
407 global $SESSION;
408 parent::tearDown();
409 if ($this->initiallang !== null) {
410 $SESSION->lang = $this->initiallang;
411 $this->initiallang = null;
412 } else {
413 unset($SESSION->lang);
414 }
415 }
7aea08e1
SH
416
417 /**
418 * Tests the static asort method
419 * @return void
420 */
421 public function test_asort() {
5bd40408
PS
422 $arr = array('b' => 'ab', 1 => 'aa', 0 => 'cc');
423 collatorlib::asort($arr);
424 $this->assertSame(array_keys($arr), array(1, 'b', 0));
425 $this->assertSame(array_values($arr), array('aa', 'ab', 'cc'));
426
427 $arr = array('a' => 'áb', 'b' => 'ab', 1 => 'aa', 0=>'cc');
428 collatorlib::asort($arr);
429 $this->assertSame(array_keys($arr), array(1, 'b', 'a', 0), $this->error);
430 $this->assertSame(array_values($arr), array('aa', 'ab', 'áb', 'cc'), $this->error);
431 }
7aea08e1
SH
432
433 /**
434 * Tests the static asort_objects_by_method method
435 * @return void
436 */
437 public function test_asort_objects_by_method() {
5bd40408
PS
438 $objects = array(
439 'b' => new string_test_class('ab'),
440 1 => new string_test_class('aa'),
441 0 => new string_test_class('cc')
442 );
443 collatorlib::asort_objects_by_method($objects, 'get_protected_name');
444 $this->assertSame(array_keys($objects), array(1, 'b', 0));
445 $this->assertSame($this->get_ordered_names($objects, 'get_protected_name'), array('aa', 'ab', 'cc'));
446
447 $objects = array(
448 'a' => new string_test_class('áb'),
449 'b' => new string_test_class('ab'),
450 1 => new string_test_class('aa'),
451 0 => new string_test_class('cc')
452 );
453 collatorlib::asort_objects_by_method($objects, 'get_private_name');
454 $this->assertSame(array_keys($objects), array(1, 'b', 'a', 0), $this->error);
455 $this->assertSame($this->get_ordered_names($objects, 'get_private_name'), array('aa', 'ab', 'áb', 'cc'), $this->error);
456 }
7aea08e1
SH
457
458 /**
459 * Tests the static asort_objects_by_method method
460 * @return void
461 */
462 public function asort_objects_by_property() {
5bd40408
PS
463 $objects = array(
464 'b' => new string_test_class('ab'),
465 1 => new string_test_class('aa'),
466 0 => new string_test_class('cc')
467 );
468 collatorlib::asort_objects_by_property($objects, 'publicname');
469 $this->assertSame(array_keys($objects), array(1, 'b', 0));
470 $this->assertSame($this->get_ordered_names($objects, 'publicname'), array('aa', 'ab', 'cc'));
471
472 $objects = array(
473 'a' => new string_test_class('áb'),
474 'b' => new string_test_class('ab'),
475 1 => new string_test_class('aa'),
476 0 => new string_test_class('cc')
477 );
478 collatorlib::asort_objects_by_property($objects, 'publicname');
479 $this->assertSame(array_keys($objects), array(1, 'b', 'a', 0), $this->error);
480 $this->assertSame($this->get_ordered_names($objects, 'publicname'), array('aa', 'ab', 'áb', 'cc'), $this->error);
481 }
7aea08e1
SH
482
483 /**
484 * Returns an array of sorted names
485 * @param array $objects
486 * @param string $methodproperty
487 * @return type
488 */
5bd40408
PS
489 protected function get_ordered_names($objects, $methodproperty = 'get_protected_name') {
490 $return = array();
491 foreach ($objects as $object) {
492 if ($methodproperty == 'publicname') {
493 $return[] = $object->publicname;
494 } else {
495 $return[] = $object->$methodproperty();
496 }
497 }
498 return $return;
499 }
500}
501
502
503/**
504 * Simple class used to work with the unit test.
505 *
506 * @package core
7aea08e1 507 * @category phpunit
5bd40408
PS
508 * @copyright 2011 Sam Hemelryk
509 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
510 */
511class string_test_class extends stdClass {
7aea08e1
SH
512 /**
513 * @var string A public property
514 */
5bd40408 515 public $publicname;
7aea08e1
SH
516 /**
517 * @var string A protected property
518 */
5bd40408 519 protected $protectedname;
7aea08e1
SH
520 /**
521 * @var string A private property
522 */
5bd40408 523 private $privatename;
7aea08e1
SH
524 /**
525 * Constructs the test instance
526 * @param string $name
527 */
5bd40408
PS
528 public function __construct($name) {
529 $this->publicname = $name;
530 $this->protectedname = $name;
531 $this->privatename = $name;
532 }
7aea08e1
SH
533 /**
534 * Returns the protected property
535 * @return string
536 */
5bd40408
PS
537 public function get_protected_name() {
538 return $this->protectedname;
539 }
7aea08e1
SH
540 /**
541 * Returns the protected property
542 * @return string
543 */
5bd40408
PS
544 public function get_private_name() {
545 return $this->publicname;
546 }
7aea08e1 547}