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/>.
18 * textlib unit teststest
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 {
39 public function test_parse_charset() {
40 $this->assertSame(textlib::parse_charset('Cp1250'), 'windows-1250');
41 // does typo3 work? some encoding moodle does not use
42 $this->assertSame(textlib::parse_charset('ms-ansi'), 'windows-1252');
45 public function test_convert() {
46 $utf8 = "Žluťoučký koníček";
47 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
48 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
49 $this->assertSame(textlib::convert($utf8, 'utf-8', 'iso-8859-2'), $iso2);
50 $this->assertSame(textlib::convert($iso2, 'iso-8859-2', 'utf-8'), $utf8);
51 $this->assertSame(textlib::convert($utf8, 'utf-8', 'win-1250'), $win);
52 $this->assertSame(textlib::convert($win, 'win-1250', 'utf-8'), $utf8);
53 $this->assertSame(textlib::convert($win, 'win-1250', 'iso-8859-2'), $iso2);
54 $this->assertSame(textlib::convert($iso2, 'iso-8859-2', 'win-1250'), $win);
55 $this->assertSame(textlib::convert($iso2, 'iso-8859-2', 'iso-8859-2'), $iso2);
56 $this->assertSame(textlib::convert($win, 'win-1250', 'cp1250'), $win);
60 $str = pack("H*", "b8c0b8ecc0dfc4ea"); //EUC-JP
61 $this->assertSame(textlib::convert($utf8, 'utf-8', 'EUC-JP'), $str);
62 $this->assertSame(textlib::convert($str, 'EUC-JP', 'utf-8'), $utf8);
64 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
65 $this->assertSame(textlib::convert($utf8, 'utf-8', 'ISO-2022-JP'), $str);
66 $this->assertSame(textlib::convert($str, 'ISO-2022-JP', 'utf-8'), $utf8);
68 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
69 $this->assertSame(textlib::convert($utf8, 'utf-8', 'SHIFT-JIS'), $str);
70 $this->assertSame(textlib::convert($str, 'SHIFT-JIS', 'utf-8'), $utf8);
73 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
74 $this->assertSame(textlib::convert($utf8, 'utf-8', 'GB2312'), $str);
75 $this->assertSame(textlib::convert($str, 'GB2312', 'utf-8'), $utf8);
77 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
78 $this->assertSame(textlib::convert($utf8, 'utf-8', 'GB18030'), $str);
79 $this->assertSame(textlib::convert($str, 'GB18030', 'utf-8'), $utf8);
82 public function test_substr() {
83 $str = "Žluťoučký koníček";
84 $this->assertSame(textlib::substr($str, 0), $str);
85 $this->assertSame(textlib::substr($str, 1), 'luťoučký koníček');
86 $this->assertSame(textlib::substr($str, 1, 3), 'luť');
87 $this->assertSame(textlib::substr($str, 0, 100), $str);
88 $this->assertSame(textlib::substr($str, -3, 2), 'če');
90 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
91 $this->assertSame(textlib::substr($iso2, 1, 3, 'iso-8859-2'), textlib::convert('luť', 'utf-8', 'iso-8859-2'));
92 $this->assertSame(textlib::substr($iso2, 0, 100, 'iso-8859-2'), textlib::convert($str, 'utf-8', 'iso-8859-2'));
93 $this->assertSame(textlib::substr($iso2, -3, 2, 'iso-8859-2'), textlib::convert('če', 'utf-8', 'iso-8859-2'));
95 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
96 $this->assertSame(textlib::substr($win, 1, 3, 'cp1250'), textlib::convert('luť', 'utf-8', 'cp1250'));
97 $this->assertSame(textlib::substr($win, 0, 100, 'cp1250'), textlib::convert($str, 'utf-8', 'cp1250'));
98 $this->assertSame(textlib::substr($win, -3, 2, 'cp1250'), textlib::convert('če', 'utf-8', 'cp1250'));
101 $str = pack("H*", "b8c0b8ecc0dfc4ea"); //EUC-JP
102 $s = pack("H*", "b8ec"); //EUC-JP
103 $this->assertSame(textlib::substr($str, 1, 1, 'EUC-JP'), $s);
105 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
106 $s = pack("H*", "1b2442386c1b2842"); //ISO-2022-JP
107 $this->assertSame(textlib::substr($str, 1, 1, 'ISO-2022-JP'), $s);
109 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
110 $s = pack("H*", "8cea"); //SHIFT-JIS
111 $this->assertSame(textlib::substr($str, 1, 1, 'SHIFT-JIS'), $s);
113 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
114 $s = pack("H*", "cce5"); //GB2312
115 $this->assertSame(textlib::substr($str, 1, 1, 'GB2312'), $s);
117 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
118 $s = pack("H*", "cce5"); //GB18030
119 $this->assertSame(textlib::substr($str, 1, 1, 'GB18030'), $s);
122 public function test_strlen() {
123 $str = "Žluťoučký koníček";
124 $this->assertSame(textlib::strlen($str), 17);
126 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
127 $this->assertSame(textlib::strlen($iso2, 'iso-8859-2'), 17);
129 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
130 $this->assertSame(textlib::strlen($win, 'cp1250'), 17);
133 $str = pack("H*", "b8ec"); //EUC-JP
134 $this->assertSame(textlib::strlen($str, 'EUC-JP'), 1);
135 $str = pack("H*", "b8c0b8ecc0dfc4ea"); //EUC-JP
136 $this->assertSame(textlib::strlen($str, 'EUC-JP'), 4);
138 $str = pack("H*", "1b2442386c1b2842"); //ISO-2022-JP
139 $this->assertSame(textlib::strlen($str, 'ISO-2022-JP'), 1);
140 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
141 $this->assertSame(textlib::strlen($str, 'ISO-2022-JP'), 4);
143 $str = pack("H*", "8cea"); //SHIFT-JIS
144 $this->assertSame(textlib::strlen($str, 'SHIFT-JIS'), 1);
145 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
146 $this->assertSame(textlib::strlen($str, 'SHIFT-JIS'), 4);
148 $str = pack("H*", "cce5"); //GB2312
149 $this->assertSame(textlib::strlen($str, 'GB2312'), 1);
150 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
151 $this->assertSame(textlib::strlen($str, 'GB2312'), 4);
153 $str = pack("H*", "cce5"); //GB18030
154 $this->assertSame(textlib::strlen($str, 'GB18030'), 1);
155 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
156 $this->assertSame(textlib::strlen($str, 'GB18030'), 4);
159 public function test_strtolower() {
160 $str = "Žluťoučký koníček";
161 $low = 'žluťoučký koníček';
162 $this->assertSame(textlib::strtolower($str), $low);
164 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
165 $this->assertSame(textlib::strtolower($iso2, 'iso-8859-2'), textlib::convert($low, 'utf-8', 'iso-8859-2'));
167 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
168 $this->assertSame(textlib::strtolower($win, 'cp1250'), textlib::convert($low, 'utf-8', 'cp1250'));
172 $this->assertSame(textlib::strtolower($str), $str);
175 $this->assertSame(textlib::strtolower($str), $str);
177 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
178 $this->assertSame(textlib::strtolower($str, 'ISO-2022-JP'), $str);
180 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
181 $this->assertSame(textlib::strtolower($str, 'SHIFT-JIS'), $str);
183 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
184 $this->assertSame(textlib::strtolower($str, 'GB2312'), $str);
186 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
187 $this->assertSame(textlib::strtolower($str, 'GB18030'), $str);
190 public function test_strtoupper() {
191 $str = "Žluťoučký koníček";
192 $up = 'ŽLUŤOUČKÝ KONÍČEK';
193 $this->assertSame(textlib::strtoupper($str), $up);
195 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
196 $this->assertSame(textlib::strtoupper($iso2, 'iso-8859-2'), textlib::convert($up, 'utf-8', 'iso-8859-2'));
198 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
199 $this->assertSame(textlib::strtoupper($win, 'cp1250'), textlib::convert($up, 'utf-8', 'cp1250'));
203 $this->assertSame(textlib::strtoupper($str), $str);
206 $this->assertSame(textlib::strtoupper($str), $str);
208 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
209 $this->assertSame(textlib::strtoupper($str, 'ISO-2022-JP'), $str);
211 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
212 $this->assertSame(textlib::strtoupper($str, 'SHIFT-JIS'), $str);
214 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
215 $this->assertSame(textlib::strtoupper($str, 'GB2312'), $str);
217 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
218 $this->assertSame(textlib::strtoupper($str, 'GB18030'), $str);
221 public function test_strpos() {
222 $str = "Žluťoučký koníček";
223 $this->assertSame(textlib::strpos($str, 'koníč'), 10);
226 public function test_strrpos() {
227 $str = "Žluťoučký koníček";
228 $this->assertSame(textlib::strrpos($str, 'o'), 11);
231 public function test_specialtoascii() {
232 $str = "Žluťoučký koníček";
233 $this->assertSame(textlib::specialtoascii($str), 'Zlutoucky konicek');
236 public function test_encode_mimeheader() {
237 $str = "Žluťoučký koníček";
238 $this->assertSame(textlib::encode_mimeheader($str), '=?utf-8?B?xb1sdcWlb3XEjWvDvSBrb27DrcSNZWs=?=');
241 public function test_entities_to_utf8() {
242 $str = "Žluťoučký koníček";
243 $this->assertSame(textlib::entities_to_utf8($str), "Žluťoučký koníček");
246 public function test_utf8_to_entities() {
247 $str = "Žluťoučký koníček";
248 $this->assertSame(textlib::utf8_to_entities($str), "Žluťoučký koníček");
249 $this->assertSame(textlib::utf8_to_entities($str, true), "Žluťoučký koníček");
253 public function test_trim_utf8_bom() {
254 $bom = "\xef\xbb\xbf";
255 $str = "Žluťoučký koníček";
256 $this->assertSame(textlib::trim_utf8_bom($bom.$str.$bom), $str.$bom);
259 public function test_get_encodings() {
260 $encodings = textlib::get_encodings();
261 $this->assertTrue(is_array($encodings));
262 $this->assertTrue(count($encodings) > 1);
263 $this->assertTrue(isset($encodings['UTF-8']));
266 public function test_code2utf8() {
267 $this->assertSame(textlib::code2utf8(381), 'Ž');
270 public function test_strtotitle() {
271 $str = "žluťoučký koníček";
272 $this->assertSame(textlib::strtotitle($str), "Žluťoučký Koníček");
275 public function test_deprecated_textlib_get_instance() {
277 $textlib = textlib_get_instance();
278 $output = ob_get_contents();
279 $this->assertNotEmpty($output);
281 $this->assertSame($textlib->substr('abc', 1, 1), 'b');
282 $this->assertSame($textlib->strlen('abc'), 3);
283 $this->assertSame($textlib->strtoupper('Abc'), 'ABC');
284 $this->assertSame($textlib->strtolower('Abc'), 'abc');
285 $this->assertSame($textlib->strpos('abc', 'a'), 0);
286 $this->assertSame($textlib->strpos('abc', 'd'), false);
287 $this->assertSame($textlib->strrpos('abcabc', 'a'), 3);
288 $this->assertSame($textlib->specialtoascii('ábc'), 'abc');
289 $this->assertSame($textlib->strtotitle('abc ABC'), 'Abc Abc');
295 * Unit tests for our utf-8 aware collator.
301 * @copyright 2011 Sam Hemelryk
302 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
304 class collatorlib_testcase extends basic_testcase {
306 protected $initiallang = null;
307 protected $error = null;
309 public function setUp() {
311 if (isset($SESSION->lang)) {
312 $this->initiallang = $SESSION->lang;
314 $SESSION->lang = 'en'; // make sure we test en language to get consistent results, hopefully all systems have this locale
315 if (extension_loaded('intl')) {
316 $this->error = 'Collation aware sorting not supported';
318 $this->error = 'Collation aware sorting not supported, PHP extension "intl" is not available.';
322 public function tearDown() {
325 if ($this->initiallang !== null) {
326 $SESSION->lang = $this->initiallang;
327 $this->initiallang = null;
329 unset($SESSION->lang);
332 function test_asort() {
333 $arr = array('b' => 'ab', 1 => 'aa', 0 => 'cc');
334 collatorlib::asort($arr);
335 $this->assertSame(array_keys($arr), array(1, 'b', 0));
336 $this->assertSame(array_values($arr), array('aa', 'ab', 'cc'));
338 $arr = array('a' => 'áb', 'b' => 'ab', 1 => 'aa', 0=>'cc');
339 collatorlib::asort($arr);
340 $this->assertSame(array_keys($arr), array(1, 'b', 'a', 0), $this->error);
341 $this->assertSame(array_values($arr), array('aa', 'ab', 'áb', 'cc'), $this->error);
343 function test_asort_objects_by_method() {
345 'b' => new string_test_class('ab'),
346 1 => new string_test_class('aa'),
347 0 => new string_test_class('cc')
349 collatorlib::asort_objects_by_method($objects, 'get_protected_name');
350 $this->assertSame(array_keys($objects), array(1, 'b', 0));
351 $this->assertSame($this->get_ordered_names($objects, 'get_protected_name'), array('aa', 'ab', 'cc'));
354 'a' => new string_test_class('áb'),
355 'b' => new string_test_class('ab'),
356 1 => new string_test_class('aa'),
357 0 => new string_test_class('cc')
359 collatorlib::asort_objects_by_method($objects, 'get_private_name');
360 $this->assertSame(array_keys($objects), array(1, 'b', 'a', 0), $this->error);
361 $this->assertSame($this->get_ordered_names($objects, 'get_private_name'), array('aa', 'ab', 'áb', 'cc'), $this->error);
363 function test_asort_objects_by_property() {
365 'b' => new string_test_class('ab'),
366 1 => new string_test_class('aa'),
367 0 => new string_test_class('cc')
369 collatorlib::asort_objects_by_property($objects, 'publicname');
370 $this->assertSame(array_keys($objects), array(1, 'b', 0));
371 $this->assertSame($this->get_ordered_names($objects, 'publicname'), array('aa', 'ab', 'cc'));
374 'a' => new string_test_class('áb'),
375 'b' => new string_test_class('ab'),
376 1 => new string_test_class('aa'),
377 0 => new string_test_class('cc')
379 collatorlib::asort_objects_by_property($objects, 'publicname');
380 $this->assertSame(array_keys($objects), array(1, 'b', 'a', 0), $this->error);
381 $this->assertSame($this->get_ordered_names($objects, 'publicname'), array('aa', 'ab', 'áb', 'cc'), $this->error);
383 protected function get_ordered_names($objects, $methodproperty = 'get_protected_name') {
385 foreach ($objects as $object) {
386 if ($methodproperty == 'publicname') {
387 $return[] = $object->publicname;
389 $return[] = $object->$methodproperty();
398 * Simple class used to work with the unit test.
402 * @copyright 2011 Sam Hemelryk
403 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
405 class string_test_class extends stdClass {
407 protected $protectedname;
408 private $privatename;
409 public function __construct($name) {
410 $this->publicname = $name;
411 $this->protectedname = $name;
412 $this->privatename = $name;
414 public function get_protected_name() {
415 return $this->protectedname;
417 public function get_private_name() {
418 return $this->publicname;