2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
30 * Unit tests for our utf-8 aware text processing
34 * @copyright 2010 Petr Skoda (http://skodak.org)
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class core_textlib_testcase extends basic_testcase {
40 * Tests the static parse charset method
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');
50 * Tests the static convert method
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);
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);
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);
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);
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);
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);
91 * Tests the static sub string method
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');
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'));
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'));
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);
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);
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);
125 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
126 $s = pack("H*", "cce5"); //GB2312
127 $this->assertSame(textlib::substr($str, 1, 1, 'GB2312'), $s);
129 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
130 $s = pack("H*", "cce5"); //GB18030
131 $this->assertSame(textlib::substr($str, 1, 1, 'GB18030'), $s);
135 * Tests the static string length method
138 public function test_strlen() {
139 $str = "Žluťoučký koníček";
140 $this->assertSame(textlib::strlen($str), 17);
142 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
143 $this->assertSame(textlib::strlen($iso2, 'iso-8859-2'), 17);
145 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
146 $this->assertSame(textlib::strlen($win, 'cp1250'), 17);
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);
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);
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);
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);
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);
176 * Tests the static strtolower method
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);
184 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
185 $this->assertSame(textlib::strtolower($iso2, 'iso-8859-2'), textlib::convert($low, 'utf-8', 'iso-8859-2'));
187 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
188 $this->assertSame(textlib::strtolower($win, 'cp1250'), textlib::convert($low, 'utf-8', 'cp1250'));
192 $this->assertSame(textlib::strtolower($str), $str);
195 $this->assertSame(textlib::strtolower($str), $str);
197 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
198 $this->assertSame(textlib::strtolower($str, 'ISO-2022-JP'), $str);
200 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
201 $this->assertSame(textlib::strtolower($str, 'SHIFT-JIS'), $str);
203 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
204 $this->assertSame(textlib::strtolower($str, 'GB2312'), $str);
206 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
207 $this->assertSame(textlib::strtolower($str, 'GB18030'), $str);
209 // typo3 has problems with integers
211 $this->assertSame((string)$str, textlib::strtolower($str));
215 * Tests the static strtoupper
218 public function test_strtoupper() {
219 $str = "Žluťoučký koníček";
220 $up = 'ŽLUŤOUČKÝ KONÍČEK';
221 $this->assertSame(textlib::strtoupper($str), $up);
223 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
224 $this->assertSame(textlib::strtoupper($iso2, 'iso-8859-2'), textlib::convert($up, 'utf-8', 'iso-8859-2'));
226 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
227 $this->assertSame(textlib::strtoupper($win, 'cp1250'), textlib::convert($up, 'utf-8', 'cp1250'));
231 $this->assertSame(textlib::strtoupper($str), $str);
234 $this->assertSame(textlib::strtoupper($str), $str);
236 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
237 $this->assertSame(textlib::strtoupper($str, 'ISO-2022-JP'), $str);
239 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
240 $this->assertSame(textlib::strtoupper($str, 'SHIFT-JIS'), $str);
242 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
243 $this->assertSame(textlib::strtoupper($str, 'GB2312'), $str);
245 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
246 $this->assertSame(textlib::strtoupper($str, 'GB18030'), $str);
250 * Tests the static strpos method
253 public function test_strpos() {
254 $str = "Žluťoučký koníček";
255 $this->assertSame(textlib::strpos($str, 'koníč'), 10);
259 * Tests the static strrpos
262 public function test_strrpos() {
263 $str = "Žluťoučký koníček";
264 $this->assertSame(textlib::strrpos($str, 'o'), 11);
268 * Tests the static specialtoascii method
271 public function test_specialtoascii() {
272 $str = "Žluťoučký koníček";
273 $this->assertSame(textlib::specialtoascii($str), 'Zlutoucky konicek');
277 * Tests the static encode_mimeheader method
280 public function test_encode_mimeheader() {
281 $str = "Žluťoučký koníček";
282 $this->assertSame(textlib::encode_mimeheader($str), '=?utf-8?B?xb1sdcWlb3XEjWvDvSBrb27DrcSNZWs=?=');
286 * Tests the static entities_to_utf8 method
289 public function test_entities_to_utf8() {
290 $str = "Žluťoučký koníček";
291 $this->assertSame(textlib::entities_to_utf8($str), "Žluťoučký koníček");
295 * Tests the static utf8_to_entities method
298 public function test_utf8_to_entities() {
299 $str = "Žluťoučký koníček";
300 $this->assertSame(textlib::utf8_to_entities($str), "Žluťoučký koníček");
301 $this->assertSame(textlib::utf8_to_entities($str, true), "Žluťoučký koníček");
306 * Tests the static trim_utf8_bom method
309 public function test_trim_utf8_bom() {
310 $bom = "\xef\xbb\xbf";
311 $str = "Žluťoučký koníček";
312 $this->assertSame(textlib::trim_utf8_bom($bom.$str.$bom), $str.$bom);
316 * Tests the static get_encodings method
319 public function test_get_encodings() {
320 $encodings = textlib::get_encodings();
321 $this->assertTrue(is_array($encodings));
322 $this->assertTrue(count($encodings) > 1);
323 $this->assertTrue(isset($encodings['UTF-8']));
327 * Tests the static code2utf8 method
330 public function test_code2utf8() {
331 $this->assertSame(textlib::code2utf8(381), 'Ž');
335 * Tests the static strtotitle method
338 public function test_strtotitle() {
339 $str = "žluťoučký koníček";
340 $this->assertSame(textlib::strtotitle($str), "Žluťoučký Koníček");
344 * Tests the deprecated method of textlib that still require an instance.
347 public function test_deprecated_textlib_get_instance() {
349 $textlib = textlib_get_instance();
350 $output = ob_get_contents();
351 $this->assertNotEmpty($output);
353 $this->assertSame($textlib->substr('abc', 1, 1), 'b');
354 $this->assertSame($textlib->strlen('abc'), 3);
355 $this->assertSame($textlib->strtoupper('Abc'), 'ABC');
356 $this->assertSame($textlib->strtolower('Abc'), 'abc');
357 $this->assertSame($textlib->strpos('abc', 'a'), 0);
358 $this->assertSame($textlib->strpos('abc', 'd'), false);
359 $this->assertSame($textlib->strrpos('abcabc', 'a'), 3);
360 $this->assertSame($textlib->specialtoascii('ábc'), 'abc');
361 $this->assertSame($textlib->strtotitle('abc ABC'), 'Abc Abc');
367 * Unit tests for our utf-8 aware collator.
373 * @copyright 2011 Sam Hemelryk
374 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
376 class collatorlib_testcase extends basic_testcase {
379 * @var string The initial lang, stored because we change it during testing
381 protected $initiallang = null;
384 * @var string The last error that has occured
386 protected $error = null;
389 * Prepares things for this test case
392 protected function setUp() {
394 if (isset($SESSION->lang)) {
395 $this->initiallang = $SESSION->lang;
397 $SESSION->lang = 'en'; // make sure we test en language to get consistent results, hopefully all systems have this locale
398 if (extension_loaded('intl')) {
399 $this->error = 'Collation aware sorting not supported';
401 $this->error = 'Collation aware sorting not supported, PHP extension "intl" is not available.';
407 * Cleans things up after this test case has run
410 protected function tearDown() {
413 if ($this->initiallang !== null) {
414 $SESSION->lang = $this->initiallang;
415 $this->initiallang = null;
417 unset($SESSION->lang);
422 * Tests the static asort method
425 public function test_asort() {
426 $arr = array('b' => 'ab', 1 => 'aa', 0 => 'cc');
427 collatorlib::asort($arr);
428 $this->assertSame(array_keys($arr), array(1, 'b', 0));
429 $this->assertSame(array_values($arr), array('aa', 'ab', 'cc'));
431 $arr = array('a' => 'áb', 'b' => 'ab', 1 => 'aa', 0=>'cc');
432 collatorlib::asort($arr);
433 $this->assertSame(array_keys($arr), array(1, 'b', 'a', 0), $this->error);
434 $this->assertSame(array_values($arr), array('aa', 'ab', 'áb', 'cc'), $this->error);
438 * Tests the static asort_objects_by_method method
441 public function test_asort_objects_by_method() {
443 'b' => new string_test_class('ab'),
444 1 => new string_test_class('aa'),
445 0 => new string_test_class('cc')
447 collatorlib::asort_objects_by_method($objects, 'get_protected_name');
448 $this->assertSame(array_keys($objects), array(1, 'b', 0));
449 $this->assertSame($this->get_ordered_names($objects, 'get_protected_name'), array('aa', 'ab', 'cc'));
452 'a' => new string_test_class('áb'),
453 'b' => new string_test_class('ab'),
454 1 => new string_test_class('aa'),
455 0 => new string_test_class('cc')
457 collatorlib::asort_objects_by_method($objects, 'get_private_name');
458 $this->assertSame(array_keys($objects), array(1, 'b', 'a', 0), $this->error);
459 $this->assertSame($this->get_ordered_names($objects, 'get_private_name'), array('aa', 'ab', 'áb', 'cc'), $this->error);
463 * Tests the static asort_objects_by_method method
466 public function asort_objects_by_property() {
468 'b' => new string_test_class('ab'),
469 1 => new string_test_class('aa'),
470 0 => new string_test_class('cc')
472 collatorlib::asort_objects_by_property($objects, 'publicname');
473 $this->assertSame(array_keys($objects), array(1, 'b', 0));
474 $this->assertSame($this->get_ordered_names($objects, 'publicname'), array('aa', 'ab', 'cc'));
477 'a' => new string_test_class('áb'),
478 'b' => new string_test_class('ab'),
479 1 => new string_test_class('aa'),
480 0 => new string_test_class('cc')
482 collatorlib::asort_objects_by_property($objects, 'publicname');
483 $this->assertSame(array_keys($objects), array(1, 'b', 'a', 0), $this->error);
484 $this->assertSame($this->get_ordered_names($objects, 'publicname'), array('aa', 'ab', 'áb', 'cc'), $this->error);
488 * Returns an array of sorted names
489 * @param array $objects
490 * @param string $methodproperty
493 protected function get_ordered_names($objects, $methodproperty = 'get_protected_name') {
495 foreach ($objects as $object) {
496 if ($methodproperty == 'publicname') {
497 $return[] = $object->publicname;
499 $return[] = $object->$methodproperty();
508 * Simple class used to work with the unit test.
512 * @copyright 2011 Sam Hemelryk
513 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
515 class string_test_class extends stdClass {
517 * @var string A public property
521 * @var string A protected property
523 protected $protectedname;
525 * @var string A private property
527 private $privatename;
529 * Constructs the test instance
530 * @param string $name
532 public function __construct($name) {
533 $this->publicname = $name;
534 $this->protectedname = $name;
535 $this->privatename = $name;
538 * Returns the protected property
541 public function get_protected_name() {
542 return $this->protectedname;
545 * Returns the protected property
548 public function get_private_name() {
549 return $this->publicname;