Merge branch 'MDL-33611' of git://github.com/timhunt/moodle
[moodle.git] / lib / tests / textlib_test.php
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/>.
17 /**
18  * textlib 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  */
26 defined('MOODLE_INTERNAL') || die();
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  */
37 class core_textlib_testcase extends basic_testcase {
39     /**
40      * Tests the static parse charset method
41      * @return void
42      */
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     }
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(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);
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);
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);
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);
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     }
90     /**
91      * Tests the static sub string method
92      * @return void
93      */
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);
132     }
134     /**
135      * Tests the static string length method
136      * @return void
137      */
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);
173     }
175     /**
176      * Tests the static strtolower method
177      * @return void
178      */
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'));
191         $str = '言語設定';
192         $this->assertSame(textlib::strtolower($str), $str);
194         $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
210         $str = 1309528800;
211         $this->assertSame((string)$str, textlib::strtolower($str));
212     }
214     /**
215      * Tests the static strtoupper
216      * @return void
217      */
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'));
230         $str = '言語設定';
231         $this->assertSame(textlib::strtoupper($str), $str);
233         $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);
247     }
249     /**
250      * Tests the static strpos method
251      * @return void
252      */
253     public function test_strpos() {
254         $str = "Žluťoučký koníček";
255         $this->assertSame(textlib::strpos($str, 'koníč'), 10);
256     }
258     /**
259      * Tests the static strrpos
260      * @return void
261      */
262     public function test_strrpos() {
263         $str = "Žluťoučký koníček";
264         $this->assertSame(textlib::strrpos($str, 'o'), 11);
265     }
267     /**
268      * Tests the static specialtoascii method
269      * @return void
270      */
271     public function test_specialtoascii() {
272         $str = "Žluťoučký koníček";
273         $this->assertSame(textlib::specialtoascii($str), 'Zlutoucky konicek');
274     }
276     /**
277      * Tests the static encode_mimeheader method
278      * @return void
279      */
280     public function test_encode_mimeheader() {
281         $str = "Žluťoučký koníček";
282         $this->assertSame(textlib::encode_mimeheader($str), '=?utf-8?B?xb1sdcWlb3XEjWvDvSBrb27DrcSNZWs=?=');
283     }
285     /**
286      * Tests the static entities_to_utf8 method
287      * @return void
288      */
289     public function test_entities_to_utf8() {
290         $str = "&#x17d;lu&#x165;ou&#x10d;k&#xfd; kon&#237;&#269;ek";
291         $this->assertSame(textlib::entities_to_utf8($str), "Žluťoučký koníček");
292     }
294     /**
295      * Tests the static utf8_to_entities method
296      * @return void
297      */
298     public function test_utf8_to_entities() {
299         $str = "Žluťoučký koníček";
300         $this->assertSame(textlib::utf8_to_entities($str), "&#x17d;lu&#x165;ou&#x10d;k&#xfd; kon&#xed;&#x10d;ek");
301         $this->assertSame(textlib::utf8_to_entities($str, true), "&#381;lu&#357;ou&#269;k&#253; kon&#237;&#269;ek");
303     }
305     /**
306      * Tests the static trim_utf8_bom method
307      * @return void
308      */
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);
313     }
315     /**
316      * Tests the static get_encodings method
317      * @return void
318      */
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']));
324     }
326     /**
327      * Tests the static code2utf8 method
328      * @return void
329      */
330     public function test_code2utf8() {
331         $this->assertSame(textlib::code2utf8(381), 'Ž');
332     }
334     /**
335      * Tests the static strtotitle method
336      * @return void
337      */
338     public function test_strtotitle() {
339         $str = "žluťoučký koníček";
340         $this->assertSame(textlib::strtotitle($str), "Žluťoučký Koníček");
341     }
343     /**
344      * Tests the deprecated method of textlib that still require an instance.
345      * @return void
346      */
347     public function test_deprecated_textlib_get_instance() {
348         ob_start();
349         $textlib = textlib_get_instance();
350         $output = ob_get_contents();
351         $this->assertNotEmpty($output);
352         ob_end_clean();
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');
362     }
366 /**
367  * Unit tests for our utf-8 aware collator.
368  *
369  * Used for sorting.
370  *
371  * @package    core
372  * @category   phpunit
373  * @copyright  2011 Sam Hemelryk
374  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
375  */
376 class collatorlib_testcase extends basic_testcase {
378     /**
379      * @var string The initial lang, stored because we change it during testing
380      */
381     protected $initiallang = null;
383     /**
384      * @var string The last error that has occured
385      */
386     protected $error = null;
388     /**
389      * Prepares things for this test case
390      * @return void
391      */
392     protected function setUp() {
393         global $SESSION;
394         if (isset($SESSION->lang)) {
395             $this->initiallang = $SESSION->lang;
396         }
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';
400         } else {
401             $this->error = 'Collation aware sorting not supported, PHP extension "intl" is not available.';
402         }
403         parent::setUp();
404     }
406     /**
407      * Cleans things up after this test case has run
408      * @return void
409      */
410     protected function tearDown() {
411         global $SESSION;
412         parent::tearDown();
413         if ($this->initiallang !== null) {
414             $SESSION->lang = $this->initiallang;
415             $this->initiallang = null;
416         } else {
417             unset($SESSION->lang);
418         }
419     }
421     /**
422      * Tests the static asort method
423      * @return void
424      */
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);
435     }
437     /**
438      * Tests the static asort_objects_by_method method
439      * @return void
440      */
441     public function test_asort_objects_by_method() {
442         $objects = array(
443             'b' => new string_test_class('ab'),
444             1 => new string_test_class('aa'),
445             0 => new string_test_class('cc')
446         );
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'));
451         $objects = array(
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')
456         );
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);
460     }
462     /**
463      * Tests the static asort_objects_by_method method
464      * @return void
465      */
466     public function asort_objects_by_property() {
467         $objects = array(
468             'b' => new string_test_class('ab'),
469             1 => new string_test_class('aa'),
470             0 => new string_test_class('cc')
471         );
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'));
476         $objects = array(
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')
481         );
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);
485     }
487     /**
488      * Returns an array of sorted names
489      * @param array $objects
490      * @param string $methodproperty
491      * @return type
492      */
493     protected function get_ordered_names($objects, $methodproperty = 'get_protected_name') {
494         $return = array();
495         foreach ($objects as $object) {
496             if ($methodproperty == 'publicname') {
497                 $return[] = $object->publicname;
498             } else {
499                 $return[] = $object->$methodproperty();
500             }
501         }
502         return $return;
503     }
507 /**
508  * Simple class used to work with the unit test.
509  *
510  * @package    core
511  * @category   phpunit
512  * @copyright  2011 Sam Hemelryk
513  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
514  */
515 class string_test_class extends stdClass {
516     /**
517      * @var string A public property
518      */
519     public $publicname;
520     /**
521      * @var string A protected property
522      */
523     protected $protectedname;
524     /**
525      * @var string A private property
526      */
527     private $privatename;
528     /**
529      * Constructs the test instance
530      * @param string $name
531      */
532     public function __construct($name) {
533         $this->publicname = $name;
534         $this->protectedname = $name;
535         $this->privatename = $name;
536     }
537     /**
538      * Returns the protected property
539      * @return string
540      */
541     public function get_protected_name() {
542         return $this->protectedname;
543     }
544     /**
545      * Returns the protected property
546      * @return string
547      */
548     public function get_private_name() {
549         return $this->publicname;
550     }