multibyte_string_functions.lib.test.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. <?php
  2. /**
  3. * This is a test of multibyte_string_functions.lib which is
  4. * a common purpose library for supporting multibyte string
  5. * aware functions. Only the public API is tested here.
  6. * @author Ricardo Rodriguez Salazar, 2009.
  7. * @author Ivan Tcholakov, August 2009.
  8. * For licensing terms, see /dokeos_license.txt
  9. *
  10. * Notes:
  11. * 1. While saving this file, please, preserve its UTF-8 encoding.
  12. * Othewise this tes would be broken.
  13. * 2. While running this test, send a header declaring UTF-8 encoding.
  14. * Then you would see variable dumps correctly.
  15. * 3. Tests about string comparison and sorting might give false results
  16. * if the intl extension has not been installed.
  17. */
  18. class TestMultibyte_String_Functions extends UnitTestCase {
  19. function TestMultibyte_String_Functions() {
  20. $this->UnitTestCase('Multibyte String Functions Tests');
  21. }
  22. /**
  23. * ----------------------------------------------------------------------------
  24. * A safe way to calculate binary lenght of a string (as number of bytes)
  25. * ----------------------------------------------------------------------------
  26. */
  27. public function test_api_byte_count() {
  28. $string = 'xxxáéíóú?'; // UTF-8
  29. $res = api_byte_count($string);
  30. $this->assertTrue($res == 14);
  31. $this->assertTrue(is_numeric($res));
  32. //var_dump($res);
  33. }
  34. /**
  35. * ----------------------------------------------------------------------------
  36. * Multibyte string conversion functions
  37. * ----------------------------------------------------------------------------
  38. */
  39. public function test_api_convert_encoding() {
  40. $string = 'xxxáéíóú?€'; // UTF-8
  41. $from_encoding = 'UTF-8';
  42. $to_encoding = 'ISO-8859-15';
  43. $res = api_convert_encoding($string, $to_encoding, $from_encoding);
  44. $this->assertTrue(is_string($res));
  45. $this->assertTrue(api_convert_encoding($res, $from_encoding, $to_encoding) == $string);
  46. //var_dump($res);
  47. //var_dump(api_convert_encoding($res, $from_encoding, $to_encoding));
  48. }
  49. public function test_api_utf8_encode() {
  50. $string = 'xxxáéíóú?€'; // UTF-8
  51. $from_encoding = 'ISO-8859-15';
  52. $string1 = api_utf8_decode($string, $from_encoding);
  53. $res = api_utf8_encode($string1, $from_encoding);
  54. $this->assertTrue(is_string($res));
  55. $this->assertTrue($res == $string);
  56. //var_dump($res);
  57. }
  58. public function test_api_utf8_decode() {
  59. $string = 'xxxx1ws?!áéíóú@€'; // UTF-8
  60. $to_encoding = 'ISO-8859-15';
  61. $res = api_utf8_decode($string, $to_encoding);
  62. $this->assertTrue(is_string($res));
  63. $this->assertTrue(api_utf8_encode($res, $to_encoding) == $string);
  64. //var_dump($res);
  65. }
  66. public function test_api_to_system_encoding() {
  67. $string = '!?/\áéíóú@€'; // UTF-8
  68. $from_encoding = 'UTF-8';
  69. $check_utf8_validity = false;
  70. $res = api_to_system_encoding($string, $from_encoding, $check_utf8_validity);
  71. $this->assertTrue(is_string($res));
  72. $this->assertTrue(api_convert_encoding($res, $from_encoding, api_get_system_encoding()) == $string);
  73. //var_dump($res);
  74. }
  75. public function test_api_htmlentities() {
  76. $string = 'áéíóú@!?/\-_`*ç´`'; // UTF-8
  77. $quote_style = ENT_QUOTES;
  78. $encoding = 'UTF-8';
  79. $res = api_htmlentities($string, $quote_style, $encoding);
  80. $this->assertTrue(is_string($res));
  81. $this->assertTrue(api_convert_encoding($res, $encoding, 'HTML-ENTITIES') == $string);
  82. //var_dump($res);
  83. }
  84. public function test_api_html_entity_decode() {
  85. $string = 'áéíóú@/\!?Ç´`+*?-_ '; // UTF-8
  86. $quote_style = ENT_QUOTES;
  87. $encoding = 'UTF-8';
  88. $res = api_html_entity_decode(api_convert_encoding($string, 'HTML-ENTITIES', $encoding), $quote_style, $encoding);
  89. $this->assertTrue(is_string($res));
  90. $this->assertTrue($res == $string);
  91. //var_dump($res);
  92. }
  93. public function test_api_xml_http_response_encode() {
  94. $string='áéíóú@/\!?Ç´`+*?-_'; // UTF-8
  95. $from_encoding = 'UTF-8';
  96. $res = api_xml_http_response_encode($string, $from_encoding);
  97. $this->assertTrue(is_string($res));
  98. //var_dump($res);
  99. }
  100. public function test_api_file_system_encode() {
  101. $string = 'áéíóú@/\!?Ç´`+*?-_'; // UTF-8
  102. $from_encoding = 'UTF-8';
  103. $res = api_file_system_encode($string, $from_encoding);
  104. $this->assertTrue(is_string($res));
  105. //var_dump($res);
  106. }
  107. public function test_api_file_system_decode() {
  108. $string='áéíóú@/\!?Ç´`+*?-_'; // UTF-8
  109. $to_encoding = 'UTF-8';
  110. $res = api_file_system_decode($string, $to_encoding);
  111. $this->assertTrue(is_string($res));
  112. //var_dump($res);
  113. }
  114. public function test_api_transliterate() {
  115. $string = 'Фёдор Михайлович Достоевкий'; // UTF-8
  116. /*
  117. // If you have broken by mistake UTF-8 encoding of this source, try the following equivalent:
  118. $string = api_html_entity_decode(
  119. '&#1060;&#1105;&#1076;&#1086;&#1088; '.
  120. '&#1052;&#1080;&#1093;&#1072;&#1081;&#1083;&#1086;&#1074;&#1080;&#1095; '.
  121. '&#1044;&#1086;&#1089;&#1090;&#1086;&#1077;&#1074;&#1082;&#1080;&#1081;',
  122. ENT_QUOTES, 'UTF-8');
  123. */
  124. $unknown = 'X';
  125. $from_encoding = 'UTF-8';
  126. $res = api_transliterate($string, $unknown, $from_encoding);
  127. $this->assertTrue($res);
  128. $this->assertTrue(is_string($res));
  129. $this->assertTrue($res == 'Fyodor Mihaylovich Dostoevkiy');
  130. //var_dump($string);
  131. //var_dump($res);
  132. }
  133. /**
  134. * ----------------------------------------------------------------------------
  135. * Common multibyte string functions
  136. * ----------------------------------------------------------------------------
  137. */
  138. public function test_api_str_ireplace() {
  139. $search = 'Á'; // UTF-8
  140. $replace = 'a';
  141. $subject = 'bájando'; // UTF-8
  142. $count = null;
  143. $encoding = 'UTF-8';
  144. $res = api_str_ireplace($search, $replace, $subject, & $count, $encoding);
  145. $this->assertTrue($res);
  146. $this->assertTrue(is_string($res));
  147. $this->assertTrue($res == 'bajando');
  148. //var_dump($res);
  149. }
  150. public function test_api_str_split() {
  151. $string = 'áéíóúº|\/?Ç][ç]'; // UTF-8
  152. $split_length = 1;
  153. $encoding = 'UTF-8';
  154. $res = api_str_split($string, $split_length, $encoding);
  155. $this->assertTrue(is_array($res));
  156. $this->assertTrue(count($res) == 15);
  157. //var_dump($res);
  158. }
  159. public function test_api_stripos() {
  160. $haystack = 'bájando'; // UTF-8
  161. $needle = 'Á';
  162. $offset = 0;
  163. $encoding = 'UTF-8';
  164. $res = api_stripos($haystack, $needle, $offset, $encoding);
  165. $this->assertTrue(is_numeric($res)|| is_bool($res));
  166. $this->assertTrue($res == 1);
  167. //var_dump($res);
  168. }
  169. public function test_api_stristr() {
  170. $haystack = 'bájando'; // UTF-8
  171. $needle = 'Á';
  172. $part = false;
  173. $encoding = 'UTF-8';
  174. $res = api_stristr($haystack, $needle, $part, $encoding);
  175. $this->assertTrue(is_bool($res) || is_string($res));
  176. $this->assertTrue($res == 'ájando');
  177. //var_dump($res);
  178. }
  179. public function test_api_strlen() {
  180. $string='áéíóúº|\/?Ç][ç]'; // UTF-8
  181. $encoding = 'UTF-8';
  182. $res = api_strlen($string, $encoding);
  183. $this->assertTrue(is_numeric($res));
  184. $this->assertTrue($res == 15);
  185. //var_dump($res);
  186. }
  187. public function test_api_strpos() {
  188. $haystack = 'bájando'; // UTF-8
  189. $needle = 'á';
  190. $offset = 0;
  191. $encoding = 'UTF-8';
  192. $res = api_strpos($haystack, $needle, $offset, $encoding);
  193. $this->assertTrue(is_numeric($res)|| is_bool($res));
  194. $this->assertTrue($res == 1);
  195. //var_dump($res);
  196. }
  197. public function test_api_strrchr() {
  198. $haystack = 'aviación aviación'; // UTF-8
  199. $needle = 'ó';
  200. $part = false;
  201. $encoding = 'UTF-8';
  202. $res = api_strrchr($haystack, $needle, $part, $encoding);
  203. $this->assertTrue(is_string($res)|| is_bool($res));
  204. $this->assertTrue($res == 'ón');
  205. //var_dump($res);
  206. }
  207. public function test_api_strrev() {
  208. $string = 'áéíóúº|\/?Ç][ç]'; // UTF-8
  209. $encoding = 'UTF-8';
  210. $res = api_strrev($string, $encoding);
  211. $this->assertTrue(is_string($res));
  212. $this->assertTrue($res == ']ç[]Ç?/\|ºúóíéá');
  213. //var_dump($res);
  214. }
  215. public function test_api_strrpos() {
  216. $haystack = 'aviación aviación'; // UTF-8
  217. $needle = 'ó';
  218. $offset = 0;
  219. $encoding = 'UTF-8';
  220. $res = api_strrpos($haystack, $needle, $offset, $encoding);
  221. $this->assertTrue(is_numeric($res) || is_bool($res));
  222. $this->assertTrue($res == 15);
  223. //var_dump($res);
  224. }
  225. public function test_api_strstr() {
  226. $haystack = 'aviación'; // UTF-8
  227. $needle = 'ó';
  228. $part = false;
  229. $encoding = 'UTF-8';
  230. $res = api_strstr($haystack, $needle, $part, $encoding);
  231. $this->assertTrue(is_bool($res)|| is_string($res));
  232. $this->assertTrue($res == 'ón');
  233. //var_dump($res);
  234. }
  235. public function test_api_strtolower() {
  236. $string = 'áéíóúº|\/?Ç][ç]'; // UTF-8
  237. $encoding = 'UTF-8';
  238. $res = api_strtolower($string, $encoding);
  239. $this->assertTrue(is_string($res));
  240. $this->assertTrue($res == 'áéíóúº|\/?ç][ç]');
  241. //var_dump($res);
  242. }
  243. public function test_api_strtoupper() {
  244. $string='áéíóúº|\/?Ç][ç]'; // UTF-8
  245. $encoding = 'UTF-8';
  246. $res = api_strtoupper($string, $encoding);
  247. $this->assertTrue(is_string($res));
  248. $this->assertTrue($res =='ÁÉÍÓÚº|\/?Ç][Ç]');
  249. //var_dump($res);
  250. }
  251. public function test_api_substr() {
  252. $string = 'áéíóúº|\/?Ç][ç]'; // UTF-8
  253. $start = 10;
  254. $length = 4;
  255. $encoding = 'UTF-8';
  256. $res = api_substr($string, $start, $length, $encoding);
  257. $this->assertTrue(is_string($res));
  258. $this->assertTrue($res == 'Ç][ç');
  259. //var_dump($res);
  260. }
  261. public function test_api_substr_replace() {
  262. $string = 'áéíóúº|\/?Ç][ç]'; // UTF-8
  263. $replacement = 'eiou';
  264. $start= 1;
  265. $length = 4;
  266. $encoding = 'UTF-8';
  267. $res = api_substr_replace($string, $replacement, $start, $length, $encoding);
  268. $this->assertTrue(is_string($res));
  269. $this->assertTrue($res == 'áeiouº|\/?Ç][ç]');
  270. //var_dump($res);
  271. }
  272. public function test_api_ucfirst() {
  273. $string = 'áéíóúº|\/? xx ][ xx ]'; // UTF-8
  274. $encoding = 'UTF-8';
  275. $res = api_ucfirst($string, $encoding);
  276. $this->assertTrue($res);
  277. $this->assertTrue(is_string($res));
  278. $this->assertTrue($res == 'Áéíóúº|\/? xx ][ xx ]');
  279. //var_dump($res);
  280. }
  281. public function test_api_ucwords() {
  282. $string = 'áéíóúº|\/? xx ][ xx ]'; // UTF-8
  283. $encoding = 'UTF-8';
  284. $res = api_ucwords($string, $encoding);
  285. $this->assertTrue($res);
  286. $this->assertTrue(is_string($res));
  287. $this->assertTrue($res == 'Áéíóúº|\/? Xx ][ Xx ]');
  288. //var_dump($res);
  289. }
  290. /**
  291. * ----------------------------------------------------------------------------
  292. * String operations using regular expressions
  293. * ----------------------------------------------------------------------------
  294. */
  295. public function test_api_preg_match() {
  296. $pattern = '/иван/i'; // UTF-8
  297. $subject = '-- Ivan (en) -- Иван (bg) --'; // UTF-8
  298. $matches = null;
  299. $flags = 0;
  300. $offset = 0;
  301. $encoding = 'UTF-8';
  302. $res = api_preg_match($pattern, $subject, $matches, $flags, $offset, $encoding);
  303. $this->assertTrue($res == 1);
  304. //var_dump($res);
  305. //var_dump($matches);
  306. }
  307. public function test_api_preg_match_all() {
  308. $pattern = '/иван/i'; // UTF-8
  309. $subject = '-- Ivan (en) -- Иван (bg) -- иван --'; // UTF-8
  310. $matches = null;
  311. $flags = PREG_PATTERN_ORDER;
  312. $offset = 0;
  313. $encoding = 'UTF-8';
  314. $res = api_preg_match_all($pattern, $subject, $matches, $flags, $offset, $encoding);
  315. $this->assertTrue($res == 2);
  316. //var_dump($res);
  317. //var_dump($matches);
  318. }
  319. public function test_api_preg_replace() {
  320. $pattern = '/иван/i'; // UTF-8
  321. $replacement = 'ИВАН'; // UTF-8
  322. $subject = '-- Ivan (en) -- Иван (bg) -- иван --'; // UTF-8
  323. $limit = -1;
  324. $count = null;
  325. $encoding = 'UTF-8';
  326. $res = api_preg_replace($pattern, $replacement, $subject, $limit, $count, $encoding);
  327. $this->assertTrue($res == '-- Ivan (en) -- ИВАН (bg) -- ИВАН --'); // UTF-8
  328. //var_dump($res);
  329. }
  330. public function test_api_preg_replace_callback() {
  331. $pattern = '/иван/i'; // UTF-8
  332. $subject = '-- Ivan (en) -- Иван (bg) -- иван --'; // UTF-8
  333. $limit = -1;
  334. $count = null;
  335. $encoding = 'UTF-8';
  336. $res = api_preg_replace_callback($pattern, create_function('$matches', 'return api_ucfirst($matches[0], \'UTF-8\');'), $subject, $limit, $count, $encoding);
  337. $this->assertTrue($res == '-- Ivan (en) -- Иван (bg) -- Иван --'); // UTF-8
  338. //var_dump($res);
  339. }
  340. public function test_api_preg_split() {
  341. $pattern = '/иван/i'; // UTF-8
  342. $subject = '-- Ivan (en) -- Иван (bg) -- иван --'; // UTF-8
  343. $limit = -1;
  344. $count = null;
  345. $encoding = 'UTF-8';
  346. $res = api_preg_split($pattern, $subject, $limit, $count, $encoding);
  347. $this->assertTrue($res[0] == '-- Ivan (en) -- '); // UTF-8
  348. //var_dump($res);
  349. }
  350. /**
  351. * ----------------------------------------------------------------------------
  352. * Obsolete string operations using regular expressions, to be deprecated
  353. * ----------------------------------------------------------------------------
  354. */
  355. public function test_api_ereg() {
  356. $pattern = 'scorm/showinframes.php([^"\'&]*)(&|&amp;)file=([^"\'&]*)$';
  357. $string = 'http://localhost/dokeos/main/scorm/showinframes.php?id=5&amp;file=test.php';
  358. $res = api_ereg($pattern, $string, $regs);
  359. $this->assertTrue(is_numeric($res));
  360. $this->assertTrue($res == 45);
  361. //var_dump($res);
  362. }
  363. public function test_api_ereg_replace() {
  364. $pattern = 'file=([^"\'&]*)$';
  365. $string = 'http://localhost/dokeos/main/scorm/showinframes.php?id=5&amp;file=test.php';
  366. $replacement = 'file=my_test.php';
  367. $option = null;
  368. $res = api_ereg_replace($pattern, $replacement, $string, $option);
  369. $this->assertTrue(is_string($res));
  370. $this->assertTrue(strlen($res) == 77);
  371. //var_dump($res);
  372. }
  373. public function testapi_eregi() {
  374. $pattern = 'scorm/showinframes.php([^"\'&]*)(&|&amp;)file=([^"\'&]*)$';
  375. $string = 'http://localhost/dokeos/main/scorm/showinframes.php?id=5&amp;file=test.php';
  376. $res = api_eregi($pattern, $string, $regs);
  377. $this->assertTrue(is_numeric($res));
  378. $this->assertTrue($res == 45);
  379. //var_dump($res);
  380. }
  381. public function test_api_eregi_replace() {
  382. $pattern = 'file=([^"\'&]*)$';
  383. $string = 'http://localhost/dokeos/main/scorm/showinframes.php?id=5&amp;file=test.php';
  384. $replacement = 'file=my_test.php';
  385. $option = null;
  386. $res = api_eregi_replace($pattern, $replacement, $string, $option);
  387. $this->assertTrue(is_string($res));
  388. $this->assertTrue(strlen($res) == 77);
  389. //var_dump($res);
  390. }
  391. public function test_api_split() {
  392. $pattern = '[/.-]';
  393. $string = '08/22/2009';
  394. $limit = null;
  395. $res = api_split($pattern, $string, $limit);
  396. $this->assertTrue(is_array($res));
  397. $this->assertTrue(count($res) == 3);
  398. //var_dump($res);
  399. }
  400. /**
  401. * ----------------------------------------------------------------------------
  402. * String comparison
  403. * ----------------------------------------------------------------------------
  404. */
  405. public function test_api_strcasecmp() {
  406. $string1 = 'áéíóu'; // UTF-8
  407. $string2 = 'Áéíóu'; // UTF-8
  408. $language = 'english';
  409. $encoding = 'UTF-8';
  410. $res = api_strcasecmp($string1, $string2, $language, $encoding);
  411. $this->assertTrue(is_numeric($res));
  412. $this->assertTrue($res == 0);
  413. //var_dump($res);
  414. }
  415. public function test_api_strcmp() {
  416. $string1 = 'áéíóu'; // UTF-8
  417. $string2 = 'Áéíóu'; // UTF-8
  418. $language = 'english';
  419. $encoding = 'UTF-8';
  420. $res = api_strcmp($string1, $string2, $language, $encoding);
  421. $this->assertTrue(is_numeric($res));
  422. $this->assertTrue($res == 1);
  423. //var_dump($res);
  424. }
  425. public function test_api_strnatcasecmp() {
  426. $string1 = '201áéíóu.txt'; // UTF-8
  427. $string2 = '30Áéíóu.TXT'; // UTF-8
  428. $language = 'english';
  429. $encoding = 'UTF-8';
  430. $res = api_strnatcasecmp($string1, $string2, $language, $encoding);
  431. $this->assertTrue(is_numeric($res));
  432. $this->assertTrue($res == 1);
  433. //var_dump($res);
  434. }
  435. public function test_api_strnatcmp() {
  436. $string1 = '201áéíóu.txt'; // UTF-8
  437. $string2 = '30áéíóu.TXT'; // UTF-8
  438. $language = 'english';
  439. $encoding = 'UTF-8';
  440. $res = api_strnatcmp($string1, $string2, $language, $encoding);
  441. $this->assertTrue(is_numeric($res));
  442. $this->assertTrue($res == 1);
  443. //var_dump($res);
  444. }
  445. /**
  446. * ----------------------------------------------------------------------------
  447. * Sorting arrays
  448. * ----------------------------------------------------------------------------
  449. */
  450. public function test_api_asort() {
  451. $array = array('úéo', 'aíó', 'áed'); // UTF-8
  452. $sort_flag = SORT_REGULAR;
  453. $language = 'english';
  454. $encoding = 'UTF-8';
  455. $res = api_asort($array, $sort_flag, $language, $encoding);
  456. $keys = array_keys($array);
  457. $this->assertTrue(is_bool($res));
  458. $this->assertTrue($array[$keys[0]] == 'aíó');
  459. //var_dump($array);
  460. //var_dump($res);
  461. }
  462. public function test_api_arsort() {
  463. $array = array('aíó', 'úéo', 'áed'); // UTF-8
  464. $sort_flag = SORT_REGULAR;
  465. $language = 'english';
  466. $encoding = 'UTF-8';
  467. $res = api_arsort($array, $sort_flag, $language, $encoding);
  468. $keys = array_keys($array);
  469. $this->assertTrue(is_bool($res));
  470. $this->assertTrue($array[$keys[0]] == 'úéo');
  471. //var_dump($array);
  472. //var_dump($res);
  473. }
  474. public function test_api_natsort() {
  475. $array = array('img12.png', 'img10.png', 'img2.png', 'img1.png'); // UTF-8
  476. $language = 'english';
  477. $encoding = 'UTF-8';
  478. $res = api_natsort($array, $language, $encoding);
  479. $keys = array_keys($array);
  480. $this->assertTrue(is_bool($res));
  481. $this->assertTrue($array[$keys[0]] == 'img1.png');
  482. //var_dump($array);
  483. //var_dump($res);
  484. }
  485. public function test_api_natrsort() {
  486. $array = array('img2.png', 'img10.png', 'img12.png', 'img1.png'); // UTF-8
  487. $language = 'english';
  488. $encoding = 'UTF-8';
  489. $res = api_natrsort($array, $language, $encoding);
  490. $keys = array_keys($array);
  491. $this->assertTrue(is_bool($res));
  492. $this->assertTrue($array[$keys[0]] == 'img12.png');
  493. //var_dump($array);
  494. //var_dump($res);
  495. }
  496. public function test_api_natcasesort() {
  497. $array = array('img2.png', 'img10.png', 'Img12.png', 'img1.png'); // UTF-8
  498. $language = 'english';
  499. $encoding = 'UTF-8';
  500. $res = api_natcasesort($array, $language, $encoding);
  501. $keys = array_keys($array);
  502. $this->assertTrue(is_bool($res));
  503. $this->assertTrue($array[$keys[0]] == 'img1.png');
  504. //var_dump($array);
  505. //var_dump($res);
  506. }
  507. public function test_api_natcasersort() {
  508. $array = array('img2.png', 'img10.png', 'Img12.png', 'img1.png'); // UTF-8
  509. $language = 'english';
  510. $encoding = 'UTF-8';
  511. $res = api_natcasersort($array, $language, $encoding);
  512. $keys = array_keys($array);
  513. $this->assertTrue(is_bool($res));
  514. $this->assertTrue($array[$keys[0]] == 'Img12.png');
  515. //var_dump($array);
  516. //var_dump($res);
  517. }
  518. public function test_api_ksort() {
  519. $array = array('aíó' => 'img2.png', 'úéo' => 'img10.png', 'áed' => 'img12.png', 'áedc' => 'img1.png'); // UTF-8
  520. $sort_flag = SORT_REGULAR;
  521. $language = 'english';
  522. $encoding = 'UTF-8';
  523. $res = api_ksort($array, $sort_flag, $language, $encoding);
  524. $keys = array_keys($array);
  525. $this->assertTrue(is_bool($res));
  526. $this->assertTrue($array[$keys[0]] == 'img2.png');
  527. //var_dump($array);
  528. //var_dump($res);
  529. }
  530. public function test_api_krsort() {
  531. $array = array('aíó' => 'img2.png', 'úéo' => 'img10.png', 'áed' => 'img12.png', 'áedc' => 'img1.png'); // UTF-8
  532. $sort_flag = SORT_REGULAR;
  533. $language = 'english';
  534. $encoding = 'UTF-8';
  535. $res = api_krsort($array, $sort_flag, $language, $encoding);
  536. $keys = array_keys($array);
  537. $this->assertTrue(is_bool($res));
  538. $this->assertTrue($array[$keys[0]] == 'img10.png');
  539. //var_dump($array);
  540. //var_dump($res);
  541. }
  542. public function test_api_knatsort() {
  543. $array = array('img2.png' => 'aíó', 'img10.png' => 'úéo', 'img12.png' => 'áed', 'img1.png' => 'áedc'); // UTF-8
  544. $language = 'english';
  545. $encoding = 'UTF-8';
  546. $res = api_knatsort($array, $language, $encoding);
  547. $keys = array_keys($array);
  548. $this->assertTrue(is_bool($res));
  549. $this->assertTrue($array[$keys[0]] == 'áedc');
  550. //var_dump($array);
  551. //var_dump($res);
  552. }
  553. public function test_api_knatrsort() {
  554. $array = array('img2.png' => 'aíó', 'img10.png' => 'úéo', 'IMG12.PNG' => 'áed', 'img1.png' => 'áedc'); // UTF-8
  555. $language = 'english';
  556. $encoding = 'UTF-8';
  557. $res = api_knatrsort($array, $language, $encoding);
  558. $keys = array_keys($array);
  559. $this->assertTrue(is_bool($res));
  560. $this->assertTrue($array[$keys[0]] == 'úéo');
  561. //var_dump($array);
  562. //var_dump($res);
  563. }
  564. public function test_api_knatcasesort() {
  565. $array = array('img2.png' => 'aíó', 'img10.png' => 'úéo', 'IMG12.PNG' => 'áed', 'img1.png' => 'áedc'); // UTF-8
  566. $language = 'english';
  567. $encoding = 'UTF-8';
  568. $res = api_knatcasesort($array, $language, $encoding);
  569. $keys = array_keys($array);
  570. $this->assertTrue(is_bool($res));
  571. $this->assertTrue($array[$keys[0]] == 'áedc');
  572. //var_dump($array);
  573. //var_dump($res);
  574. }
  575. public function test_api_knatcasersort() {
  576. $array = array('img2.png' => 'aíó', 'img10.png' => 'úéo', 'IMG12.PNG' => 'áed', 'IMG1.PNG' => 'áedc'); // UTF-8
  577. $language = 'english';
  578. $encoding = 'UTF-8';
  579. $res = api_knatcasersort($array, $language, $encoding);
  580. $keys = array_keys($array);
  581. $this->assertTrue(is_bool($res));
  582. $this->assertTrue($array[$keys[0]] == 'áed');
  583. //var_dump($array);
  584. //var_dump($res);
  585. }
  586. public function test_api_sort() {
  587. $array = array('úéo', 'aíó', 'áed', 'áedc'); // UTF-8
  588. $sort_flag = SORT_REGULAR;
  589. $language = 'english';
  590. $encoding = 'UTF-8';
  591. $res = api_sort($array, $sort_flag, $language, $encoding);
  592. $this->assertTrue(is_bool($res));
  593. $this->assertTrue($array[0] == 'aíó');
  594. //var_dump($array);
  595. //var_dump($res);
  596. }
  597. public function testapi_rsort() {
  598. $array = array('aíó', 'úéo', 'áed', 'áedc'); // UTF-8
  599. $sort_flag = SORT_REGULAR;
  600. $language = 'english';
  601. $encoding = 'UTF-8';
  602. $res = api_rsort($array, $sort_flag, $language, $encoding);
  603. $this->assertTrue(is_bool($res));
  604. $this->assertTrue($array[0] == 'úéo');
  605. //var_dump($array);
  606. //var_dump($res);
  607. }
  608. /**
  609. * ----------------------------------------------------------------------------
  610. * Common sting operations with arrays
  611. * ----------------------------------------------------------------------------
  612. */
  613. public function test_api_in_array_nocase() {
  614. $needle = 'áéíó'; // UTF-8
  615. $haystack = array('Áéíó', 'uáé', 'íóú'); // UTF-8
  616. $strict = false;
  617. $encoding = 'UTF-8';
  618. $res = api_in_array_nocase($needle, $haystack, $strict, $encoding);
  619. $this->assertTrue(is_bool($res));
  620. $this->assertTrue($res === true);
  621. //var_dump($res);
  622. }
  623. /**
  624. * ----------------------------------------------------------------------------
  625. * Encoding management functions
  626. * ----------------------------------------------------------------------------
  627. */
  628. public function test_api_refine_encoding_id() {
  629. $encoding = 'koI8-r';
  630. $res = api_refine_encoding_id($encoding);
  631. $this->assertTrue($res);
  632. $this->assertTrue(is_string($res));
  633. $this->assertTrue($res == 'KOI8-R');
  634. //var_dump($res);
  635. }
  636. public function test_api_equal_encodings() {
  637. $encoding1 = 'cp65001';
  638. $encoding2 = 'utf-8';
  639. $res1 = api_equal_encodings($encoding1, $encoding2);
  640. $encoding3 = 'WINDOWS-1251';
  641. $encoding4 = 'WINDOWS-1252';
  642. $res2 = api_equal_encodings($encoding3, $encoding4);
  643. $this->assertTrue(is_bool($res1));
  644. $this->assertTrue(is_bool($res2));
  645. $this->assertTrue($res1 && !$res2);
  646. //var_dump($res1);
  647. //var_dump($res2);
  648. }
  649. public function test_api_is_utf8() {
  650. $encoding = 'cp65001'; // This an alias of UTF-8.
  651. $res = api_is_utf8($encoding);
  652. $this->assertTrue(is_bool($res));
  653. $this->assertTrue($res);
  654. //var_dump($res);
  655. }
  656. public function test_api_is_latin1() {
  657. $encoding = 'ISO-8859-15';
  658. $strict = false;
  659. $res = api_is_latin1($encoding, false);
  660. $this->assertTrue(is_bool($res));
  661. $this->assertTrue($res);
  662. //var_dump($res);
  663. }
  664. public function test_api_get_system_encoding() {
  665. $res = api_get_system_encoding();
  666. $this->assertTrue(is_string($res));
  667. $this->assertTrue($res);
  668. //var_dump($res);
  669. }
  670. public function test_api_get_file_system_encoding() {
  671. $res = api_get_file_system_encoding();
  672. $this->assertTrue(is_string($res));
  673. $this->assertTrue($res);
  674. //var_dump($res);
  675. }
  676. public function test_api_is_encoding_supported() {
  677. $encoding1 = 'UTF-8';
  678. $encoding2 = 'XXXX#%#%VR^%BBDNdjlrsg;d';
  679. $res1 = api_is_encoding_supported($encoding1);
  680. $res2 = api_is_encoding_supported($encoding2);
  681. $this->assertTrue(is_bool($res1) && is_bool($res2));
  682. $this->assertTrue($res1 && !$res2);
  683. //var_dump($res1);
  684. //var_dump($res2);
  685. }
  686. public function test_api_get_non_utf8_encoding() {
  687. $language = 'bulgarian';
  688. $res = api_get_non_utf8_encoding($language);
  689. $this->assertTrue($res);
  690. $this->assertTrue(is_string($res));
  691. $this->assertTrue($res == 'WINDOWS-1251');
  692. //var_dump($res);
  693. }
  694. /**
  695. * ----------------------------------------------------------------------------
  696. * String validation functions concerning certain encodings
  697. * ----------------------------------------------------------------------------
  698. */
  699. public function test_api_is_valid_utf8() {
  700. $string = 'áéíóú1@\/-ḉ`´';
  701. $res = api_is_valid_utf8($string);
  702. $this->assertTrue(is_bool($res));
  703. $this->assertTrue($res);
  704. //var_dump($res);
  705. }
  706. public function test_api_is_valid_ascii() {
  707. $string = 'áéíóú'; // UTF-8
  708. $res = api_is_valid_ascii($string);
  709. $this->assertTrue(is_bool($res));
  710. $this->assertTrue(!$res);
  711. //var_dump($res);
  712. }
  713. /**
  714. * ----------------------------------------------------------------------------
  715. * Language management functions
  716. * ----------------------------------------------------------------------------
  717. */
  718. public function test_api_refine_language_id() {
  719. $language = 'english_org';
  720. $res = api_refine_language_id($language);
  721. $this->assertTrue(is_string($res));
  722. $this->assertTrue($res == 'english');
  723. //var_dump($res);
  724. }
  725. public function test_api_is_latin1_compatible() {
  726. $language = 'portuguese';
  727. $res = api_is_latin1_compatible($language);
  728. $this->assertTrue(is_bool($res));
  729. $this->assertTrue($res);
  730. //var_dump($res);
  731. }
  732. }
  733. ?>