Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/simpletest/testtextlib.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00026 defined('MOODLE_INTERNAL') || die();
00027 
00028 
00037 class textlib_test extends UnitTestCase {
00038 
00039     public static $includecoverage = array('lib/textlib.class.php');
00040 
00041     public function test_parse_charset() {
00042         $this->assertIdentical(textlib::parse_charset('Cp1250'), 'windows-1250');
00043         // does typo3 work? some encoding moodle does not use
00044         $this->assertIdentical(textlib::parse_charset('ms-ansi'), 'windows-1252');
00045     }
00046 
00047     public function test_convert() {
00048         $utf8 = "Žluťoučký koníček";
00049         $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
00050         $win  = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
00051         $this->assertIdentical(textlib::convert($utf8, 'utf-8', 'iso-8859-2'), $iso2);
00052         $this->assertIdentical(textlib::convert($iso2, 'iso-8859-2', 'utf-8'), $utf8);
00053         $this->assertIdentical(textlib::convert($utf8, 'utf-8', 'win-1250'), $win);
00054         $this->assertIdentical(textlib::convert($win, 'win-1250', 'utf-8'), $utf8);
00055         $this->assertIdentical(textlib::convert($win, 'win-1250', 'iso-8859-2'), $iso2);
00056         $this->assertIdentical(textlib::convert($iso2, 'iso-8859-2', 'win-1250'), $win);
00057         $this->assertIdentical(textlib::convert($iso2, 'iso-8859-2', 'iso-8859-2'), $iso2);
00058         $this->assertIdentical(textlib::convert($win, 'win-1250', 'cp1250'), $win);
00059 
00060 
00061         $utf8 = '言語設定';
00062         $str = pack("H*", "b8c0b8ecc0dfc4ea"); //EUC-JP
00063         $this->assertIdentical(textlib::convert($utf8, 'utf-8', 'EUC-JP'), $str);
00064         $this->assertIdentical(textlib::convert($str, 'EUC-JP', 'utf-8'), $utf8);
00065 
00066         $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
00067         $this->assertIdentical(textlib::convert($utf8, 'utf-8', 'ISO-2022-JP'), $str);
00068         $this->assertIdentical(textlib::convert($str, 'ISO-2022-JP', 'utf-8'), $utf8);
00069 
00070         $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
00071         $this->assertIdentical(textlib::convert($utf8, 'utf-8', 'SHIFT-JIS'), $str);
00072         $this->assertIdentical(textlib::convert($str, 'SHIFT-JIS', 'utf-8'), $utf8);
00073 
00074         $utf8 = '简体中文';
00075         $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
00076         $this->assertIdentical(textlib::convert($utf8, 'utf-8', 'GB2312'), $str);
00077         $this->assertIdentical(textlib::convert($str, 'GB2312', 'utf-8'), $utf8);
00078 
00079         $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
00080         $this->assertIdentical(textlib::convert($utf8, 'utf-8', 'GB18030'), $str);
00081         $this->assertIdentical(textlib::convert($str, 'GB18030', 'utf-8'), $utf8);
00082     }
00083 
00084     public function test_substr() {
00085         $str = "Žluťoučký koníček";
00086         $this->assertIdentical(textlib::substr($str, 0), $str);
00087         $this->assertIdentical(textlib::substr($str, 1), 'luťoučký koníček');
00088         $this->assertIdentical(textlib::substr($str, 1, 3), 'luť');
00089         $this->assertIdentical(textlib::substr($str, 0, 100), $str);
00090         $this->assertIdentical(textlib::substr($str, -3, 2), 'če');
00091 
00092         $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
00093         $this->assertIdentical(textlib::substr($iso2, 1, 3, 'iso-8859-2'), textlib::convert('luť', 'utf-8', 'iso-8859-2'));
00094         $this->assertIdentical(textlib::substr($iso2, 0, 100, 'iso-8859-2'), textlib::convert($str, 'utf-8', 'iso-8859-2'));
00095         $this->assertIdentical(textlib::substr($iso2, -3, 2, 'iso-8859-2'), textlib::convert('če', 'utf-8', 'iso-8859-2'));
00096 
00097         $win  = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
00098         $this->assertIdentical(textlib::substr($win, 1, 3, 'cp1250'), textlib::convert('luť', 'utf-8', 'cp1250'));
00099         $this->assertIdentical(textlib::substr($win, 0, 100, 'cp1250'), textlib::convert($str, 'utf-8', 'cp1250'));
00100         $this->assertIdentical(textlib::substr($win, -3, 2, 'cp1250'), textlib::convert('če', 'utf-8', 'cp1250'));
00101 
00102 
00103         $str = pack("H*", "b8c0b8ecc0dfc4ea"); //EUC-JP
00104         $s = pack("H*", "b8ec"); //EUC-JP
00105         $this->assertIdentical(textlib::substr($str, 1, 1, 'EUC-JP'), $s);
00106 
00107         $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
00108         $s = pack("H*", "1b2442386c1b2842"); //ISO-2022-JP
00109         $this->assertIdentical(textlib::substr($str, 1, 1, 'ISO-2022-JP'), $s);
00110 
00111         $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
00112         $s = pack("H*", "8cea"); //SHIFT-JIS
00113         $this->assertIdentical(textlib::substr($str, 1, 1, 'SHIFT-JIS'), $s);
00114 
00115         $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
00116         $s = pack("H*", "cce5"); //GB2312
00117         $this->assertIdentical(textlib::substr($str, 1, 1, 'GB2312'), $s);
00118 
00119         $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
00120         $s = pack("H*", "cce5"); //GB18030
00121         $this->assertIdentical(textlib::substr($str, 1, 1, 'GB18030'), $s);
00122     }
00123 
00124     public function test_strlen() {
00125         $str = "Žluťoučký koníček";
00126         $this->assertIdentical(textlib::strlen($str), 17);
00127 
00128         $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
00129         $this->assertIdentical(textlib::strlen($iso2, 'iso-8859-2'), 17);
00130 
00131         $win  = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
00132         $this->assertIdentical(textlib::strlen($win, 'cp1250'), 17);
00133 
00134 
00135         $str = pack("H*", "b8ec"); //EUC-JP
00136         $this->assertIdentical(textlib::strlen($str, 'EUC-JP'), 1);
00137         $str = pack("H*", "b8c0b8ecc0dfc4ea"); //EUC-JP
00138         $this->assertIdentical(textlib::strlen($str, 'EUC-JP'), 4);
00139 
00140         $str = pack("H*", "1b2442386c1b2842"); //ISO-2022-JP
00141         $this->assertIdentical(textlib::strlen($str, 'ISO-2022-JP'), 1);
00142         $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
00143         $this->assertIdentical(textlib::strlen($str, 'ISO-2022-JP'), 4);
00144 
00145         $str = pack("H*", "8cea"); //SHIFT-JIS
00146         $this->assertIdentical(textlib::strlen($str, 'SHIFT-JIS'), 1);
00147         $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
00148         $this->assertIdentical(textlib::strlen($str, 'SHIFT-JIS'), 4);
00149 
00150         $str = pack("H*", "cce5"); //GB2312
00151         $this->assertIdentical(textlib::strlen($str, 'GB2312'), 1);
00152         $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
00153         $this->assertIdentical(textlib::strlen($str, 'GB2312'), 4);
00154 
00155         $str = pack("H*", "cce5"); //GB18030
00156         $this->assertIdentical(textlib::strlen($str, 'GB18030'), 1);
00157         $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
00158         $this->assertIdentical(textlib::strlen($str, 'GB18030'), 4);
00159     }
00160 
00161     public function test_strtolower() {
00162         $str = "Žluťoučký koníček";
00163         $low = 'žluťoučký koníček';
00164         $this->assertIdentical(textlib::strtolower($str), $low);
00165 
00166         $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
00167         $this->assertIdentical(textlib::strtolower($iso2, 'iso-8859-2'), textlib::convert($low, 'utf-8', 'iso-8859-2'));
00168 
00169         $win  = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
00170         $this->assertIdentical(textlib::strtolower($win, 'cp1250'), textlib::convert($low, 'utf-8', 'cp1250'));
00171 
00172 
00173         $str = '言語設定';
00174         $this->assertIdentical(textlib::strtolower($str), $str);
00175 
00176         $str = '简体中文';
00177         $this->assertIdentical(textlib::strtolower($str), $str);
00178 
00179         $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
00180         $this->assertIdentical(textlib::strtolower($str, 'ISO-2022-JP'), $str);
00181 
00182         $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
00183         $this->assertIdentical(textlib::strtolower($str, 'SHIFT-JIS'), $str);
00184 
00185         $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
00186         $this->assertIdentical(textlib::strtolower($str, 'GB2312'), $str);
00187 
00188         $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
00189         $this->assertIdentical(textlib::strtolower($str, 'GB18030'), $str);
00190     }
00191 
00192     public function test_strtoupper() {
00193         $str = "Žluťoučký koníček";
00194         $up  = 'ŽLUŤOUČKÝ KONÍČEK';
00195         $this->assertIdentical(textlib::strtoupper($str), $up);
00196 
00197         $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
00198         $this->assertIdentical(textlib::strtoupper($iso2, 'iso-8859-2'), textlib::convert($up, 'utf-8', 'iso-8859-2'));
00199 
00200         $win  = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
00201         $this->assertIdentical(textlib::strtoupper($win, 'cp1250'), textlib::convert($up, 'utf-8', 'cp1250'));
00202 
00203 
00204         $str = '言語設定';
00205         $this->assertIdentical(textlib::strtoupper($str), $str);
00206 
00207         $str = '简体中文';
00208         $this->assertIdentical(textlib::strtoupper($str), $str);
00209 
00210         $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
00211         $this->assertIdentical(textlib::strtoupper($str, 'ISO-2022-JP'), $str);
00212 
00213         $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
00214         $this->assertIdentical(textlib::strtoupper($str, 'SHIFT-JIS'), $str);
00215 
00216         $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
00217         $this->assertIdentical(textlib::strtoupper($str, 'GB2312'), $str);
00218 
00219         $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
00220         $this->assertIdentical(textlib::strtoupper($str, 'GB18030'), $str);
00221     }
00222 
00223     public function test_strpos() {
00224         $str = "Žluťoučký koníček";
00225         $this->assertIdentical(textlib::strpos($str, 'koníč'), 10);
00226     }
00227 
00228     public function test_strrpos() {
00229         $str = "Žluťoučký koníček";
00230         $this->assertIdentical(textlib::strrpos($str, 'o'), 11);
00231     }
00232 
00233     public function test_specialtoascii() {
00234         $str = "Žluťoučký koníček";
00235         $this->assertIdentical(textlib::specialtoascii($str), 'Zlutoucky konicek');
00236     }
00237 
00238     public function test_encode_mimeheader() {
00239         $str = "Žluťoučký koníček";
00240         $this->assertIdentical(textlib::encode_mimeheader($str), '=?utf-8?B?xb1sdcWlb3XEjWvDvSBrb27DrcSNZWs=?=');
00241     }
00242 
00243     public function test_entities_to_utf8() {
00244         $str = "&#x17d;lu&#x165;ou&#x10d;k&#xfd; kon&#237;&#269;ek";
00245         $this->assertIdentical(textlib::entities_to_utf8($str), "Žluťoučký koníček");
00246     }
00247 
00248     public function test_utf8_to_entities() {
00249         $str = "Žluťoučký koníček";
00250         $this->assertIdentical(textlib::utf8_to_entities($str), "&#x17d;lu&#x165;ou&#x10d;k&#xfd; kon&#xed;&#x10d;ek");
00251         $this->assertIdentical(textlib::utf8_to_entities($str, true), "&#381;lu&#357;ou&#269;k&#253; kon&#237;&#269;ek");
00252 
00253     }
00254 
00255     public function test_trim_utf8_bom() {
00256         $bom = "\xef\xbb\xbf";
00257         $str = "Žluťoučký koníček";
00258         $this->assertIdentical(textlib::trim_utf8_bom($bom.$str.$bom), $str.$bom);
00259     }
00260 
00261     public function test_get_encodings() {
00262         $encodings = textlib::get_encodings();
00263         $this->assertTrue(is_array($encodings));
00264         $this->assertTrue(count($encodings) > 1);
00265         $this->assertTrue(isset($encodings['UTF-8']));
00266     }
00267 
00268     public function test_code2utf8() {
00269         $this->assertIdentical(textlib::code2utf8(381), 'Ž');
00270     }
00271 
00272     public function test_strtotitle() {
00273         $str = "žluťoučký koníček";
00274         $this->assertIdentical(textlib::strtotitle($str), "Žluťoučký Koníček");
00275     }
00276 
00277     public function test_deprecated_textlib_get_instance() {
00278         $textlib = textlib_get_instance();
00279         $this->assertIdentical($textlib->substr('abc', 1, 1), 'b');
00280         $this->assertIdentical($textlib->strlen('abc'), 3);
00281         $this->assertIdentical($textlib->strtoupper('Abc'), 'ABC');
00282         $this->assertIdentical($textlib->strtolower('Abc'), 'abc');
00283         $this->assertIdentical($textlib->strpos('abc', 'a'), 0);
00284         $this->assertIdentical($textlib->strpos('abc', 'd'), false);
00285         $this->assertIdentical($textlib->strrpos('abcabc', 'a'), 3);
00286         $this->assertIdentical($textlib->specialtoascii('ábc'), 'abc');
00287         $this->assertIdentical($textlib->strtotitle('abc ABC'), 'Abc Abc');
00288     }
00289 }
00290 
00301 class collatorlib_test extends UnitTestCase {
00302 
00303     protected $initiallang = null;
00304     protected $error = null;
00305 
00306     public function setUp() {
00307         global $SESSION;
00308         if (isset($SESSION->lang)) {
00309             $this->initiallang = $SESSION->lang;
00310         }
00311         $SESSION->lang = 'en'; // make sure we test en language to get consistent results, hopefully all systems have this locale
00312         if (extension_loaded('intl')) {
00313             $this->error = 'Collation aware sorting not supported';
00314         } else {
00315             $this->error = 'Collation aware sorting not supported, PHP extension "intl" is not available.';
00316         }
00317         parent::setUp();
00318     }
00319     public function tearDown() {
00320         global $SESSION;
00321         parent::tearDown();
00322         if ($this->initiallang !== null) {
00323             $SESSION->lang = $this->initiallang;
00324             $this->initiallang = null;
00325         } else {
00326             unset($SESSION->lang);
00327         }
00328     }
00329     function test_asort() {
00330         $arr = array('b' => 'ab', 1 => 'aa', 0 => 'cc');
00331         collatorlib::asort($arr);
00332         $this->assertIdentical(array_keys($arr), array(1, 'b', 0));
00333         $this->assertIdentical(array_values($arr), array('aa', 'ab', 'cc'));
00334 
00335         $arr = array('a' => 'áb', 'b' => 'ab', 1 => 'aa', 0=>'cc');
00336         collatorlib::asort($arr);
00337         $this->assertIdentical(array_keys($arr), array(1, 'b', 'a', 0), $this->error);
00338         $this->assertIdentical(array_values($arr), array('aa', 'ab', 'áb', 'cc'), $this->error);
00339     }
00340     function test_asort_objects_by_method() {
00341         $objects = array(
00342             'b' => new string_test_class('ab'),
00343             1 => new string_test_class('aa'),
00344             0 => new string_test_class('cc')
00345         );
00346         collatorlib::asort_objects_by_method($objects, 'get_protected_name');
00347         $this->assertIdentical(array_keys($objects), array(1, 'b', 0));
00348         $this->assertIdentical($this->get_ordered_names($objects, 'get_protected_name'), array('aa', 'ab', 'cc'));
00349 
00350         $objects = array(
00351             'a' => new string_test_class('áb'),
00352             'b' => new string_test_class('ab'),
00353             1 => new string_test_class('aa'),
00354             0 => new string_test_class('cc')
00355         );
00356         collatorlib::asort_objects_by_method($objects, 'get_private_name');
00357         $this->assertIdentical(array_keys($objects), array(1, 'b', 'a', 0), $this->error);
00358         $this->assertIdentical($this->get_ordered_names($objects, 'get_private_name'), array('aa', 'ab', 'áb', 'cc'), $this->error);
00359     }
00360     function test_asort_objects_by_property() {
00361         $objects = array(
00362             'b' => new string_test_class('ab'),
00363             1 => new string_test_class('aa'),
00364             0 => new string_test_class('cc')
00365         );
00366         collatorlib::asort_objects_by_property($objects, 'publicname');
00367         $this->assertIdentical(array_keys($objects), array(1, 'b', 0));
00368         $this->assertIdentical($this->get_ordered_names($objects, 'publicname'), array('aa', 'ab', 'cc'));
00369 
00370         $objects = array(
00371             'a' => new string_test_class('áb'),
00372             'b' => new string_test_class('ab'),
00373             1 => new string_test_class('aa'),
00374             0 => new string_test_class('cc')
00375         );
00376         collatorlib::asort_objects_by_property($objects, 'publicname');
00377         $this->assertIdentical(array_keys($objects), array(1, 'b', 'a', 0), $this->error);
00378         $this->assertIdentical($this->get_ordered_names($objects, 'publicname'), array('aa', 'ab', 'áb', 'cc'), $this->error);
00379     }
00380     protected function get_ordered_names($objects, $methodproperty = 'get_protected_name') {
00381         $return = array();
00382         foreach ($objects as $object) {
00383             if ($methodproperty == 'publicname') {
00384                 $return[] = $object->publicname;
00385             } else {
00386                 $return[] = $object->$methodproperty();
00387             }
00388         }
00389         return $return;
00390     }
00391 }
00400 class string_test_class extends stdClass {
00401     public $publicname;
00402     protected $protectedname;
00403     private $privatename;
00404     public function __construct($name) {
00405         $this->publicname = $name;
00406         $this->protectedname = $name;
00407         $this->privatename = $name;
00408     }
00409     public function get_protected_name() {
00410         return $this->protectedname;
00411     }
00412     public function get_private_name() {
00413         return $this->publicname;
00414     }
00415 }
 All Data Structures Namespaces Files Functions Variables Enumerations