MDL-36212 rework html entity conversions
[moodle.git] / lib / tests / textlib_test.php
CommitLineData
5bd40408
PS
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/>.
16
17/**
7aea08e1 18 * textlib unit tests
5bd40408 19 *
7aea08e1 20 * @package core
5bd40408
PS
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 */
25
26defined('MOODLE_INTERNAL') || die();
27
28
29/**
30 * Unit tests for our utf-8 aware text processing
31 *
32 * @package core
7aea08e1 33 * @category phpunit
5bd40408
PS
34 * @copyright 2010 Petr Skoda (http://skodak.org)
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class core_textlib_testcase extends basic_testcase {
38
7aea08e1
SH
39 /**
40 * Tests the static parse charset method
41 * @return void
42 */
5bd40408
PS
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 }
48
7aea08e1
SH
49 /**
50 * Tests the static convert method
51 * @return void
52 */
5bd40408
PS
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);
91438d78 65 $this->assertSame(textlib::convert($utf8, 'utf-8', 'utf-8'), $utf8);
5bd40408
PS
66
67
68 $utf8 = '言語設定';
69 $str = pack("H*", "b8c0b8ecc0dfc4ea"); //EUC-JP
70 $this->assertSame(textlib::convert($utf8, 'utf-8', 'EUC-JP'), $str);
71 $this->assertSame(textlib::convert($str, 'EUC-JP', 'utf-8'), $utf8);
91438d78 72 $this->assertSame(textlib::convert($utf8, 'utf-8', 'utf-8'), $utf8);
5bd40408
PS
73
74 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
75 $this->assertSame(textlib::convert($utf8, 'utf-8', 'ISO-2022-JP'), $str);
76 $this->assertSame(textlib::convert($str, 'ISO-2022-JP', 'utf-8'), $utf8);
91438d78 77 $this->assertSame(textlib::convert($utf8, 'utf-8', 'utf-8'), $utf8);
5bd40408
PS
78
79 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
80 $this->assertSame(textlib::convert($utf8, 'utf-8', 'SHIFT-JIS'), $str);
81 $this->assertSame(textlib::convert($str, 'SHIFT-JIS', 'utf-8'), $utf8);
91438d78 82 $this->assertSame(textlib::convert($utf8, 'utf-8', 'utf-8'), $utf8);
5bd40408
PS
83
84 $utf8 = '简体中文';
85 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
86 $this->assertSame(textlib::convert($utf8, 'utf-8', 'GB2312'), $str);
87 $this->assertSame(textlib::convert($str, 'GB2312', 'utf-8'), $utf8);
91438d78 88 $this->assertSame(textlib::convert($utf8, 'utf-8', 'utf-8'), $utf8);
5bd40408
PS
89
90 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
91 $this->assertSame(textlib::convert($utf8, 'utf-8', 'GB18030'), $str);
92 $this->assertSame(textlib::convert($str, 'GB18030', 'utf-8'), $utf8);
91438d78 93 $this->assertSame(textlib::convert($utf8, 'utf-8', 'utf-8'), $utf8);
5bd40408
PS
94 }
95
7aea08e1
SH
96 /**
97 * Tests the static sub string method
98 * @return void
99 */
5bd40408
PS
100 public function test_substr() {
101 $str = "Žluťoučký koníček";
102 $this->assertSame(textlib::substr($str, 0), $str);
103 $this->assertSame(textlib::substr($str, 1), 'luťoučký koníček');
104 $this->assertSame(textlib::substr($str, 1, 3), 'luť');
105 $this->assertSame(textlib::substr($str, 0, 100), $str);
106 $this->assertSame(textlib::substr($str, -3, 2), 'če');
107
108 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
109 $this->assertSame(textlib::substr($iso2, 1, 3, 'iso-8859-2'), textlib::convert('luť', 'utf-8', 'iso-8859-2'));
110 $this->assertSame(textlib::substr($iso2, 0, 100, 'iso-8859-2'), textlib::convert($str, 'utf-8', 'iso-8859-2'));
111 $this->assertSame(textlib::substr($iso2, -3, 2, 'iso-8859-2'), textlib::convert('če', 'utf-8', 'iso-8859-2'));
112
113 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
114 $this->assertSame(textlib::substr($win, 1, 3, 'cp1250'), textlib::convert('luť', 'utf-8', 'cp1250'));
115 $this->assertSame(textlib::substr($win, 0, 100, 'cp1250'), textlib::convert($str, 'utf-8', 'cp1250'));
116 $this->assertSame(textlib::substr($win, -3, 2, 'cp1250'), textlib::convert('če', 'utf-8', 'cp1250'));
117
118
119 $str = pack("H*", "b8c0b8ecc0dfc4ea"); //EUC-JP
120 $s = pack("H*", "b8ec"); //EUC-JP
121 $this->assertSame(textlib::substr($str, 1, 1, 'EUC-JP'), $s);
122
123 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
124 $s = pack("H*", "1b2442386c1b2842"); //ISO-2022-JP
125 $this->assertSame(textlib::substr($str, 1, 1, 'ISO-2022-JP'), $s);
126
127 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
128 $s = pack("H*", "8cea"); //SHIFT-JIS
129 $this->assertSame(textlib::substr($str, 1, 1, 'SHIFT-JIS'), $s);
130
131 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
132 $s = pack("H*", "cce5"); //GB2312
133 $this->assertSame(textlib::substr($str, 1, 1, 'GB2312'), $s);
134
135 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
136 $s = pack("H*", "cce5"); //GB18030
137 $this->assertSame(textlib::substr($str, 1, 1, 'GB18030'), $s);
138 }
139
7aea08e1
SH
140 /**
141 * Tests the static string length method
142 * @return void
143 */
5bd40408
PS
144 public function test_strlen() {
145 $str = "Žluťoučký koníček";
146 $this->assertSame(textlib::strlen($str), 17);
147
148 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
149 $this->assertSame(textlib::strlen($iso2, 'iso-8859-2'), 17);
150
151 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
152 $this->assertSame(textlib::strlen($win, 'cp1250'), 17);
153
154
155 $str = pack("H*", "b8ec"); //EUC-JP
156 $this->assertSame(textlib::strlen($str, 'EUC-JP'), 1);
157 $str = pack("H*", "b8c0b8ecc0dfc4ea"); //EUC-JP
158 $this->assertSame(textlib::strlen($str, 'EUC-JP'), 4);
159
160 $str = pack("H*", "1b2442386c1b2842"); //ISO-2022-JP
161 $this->assertSame(textlib::strlen($str, 'ISO-2022-JP'), 1);
162 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
163 $this->assertSame(textlib::strlen($str, 'ISO-2022-JP'), 4);
164
165 $str = pack("H*", "8cea"); //SHIFT-JIS
166 $this->assertSame(textlib::strlen($str, 'SHIFT-JIS'), 1);
167 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
168 $this->assertSame(textlib::strlen($str, 'SHIFT-JIS'), 4);
169
170 $str = pack("H*", "cce5"); //GB2312
171 $this->assertSame(textlib::strlen($str, 'GB2312'), 1);
172 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
173 $this->assertSame(textlib::strlen($str, 'GB2312'), 4);
174
175 $str = pack("H*", "cce5"); //GB18030
176 $this->assertSame(textlib::strlen($str, 'GB18030'), 1);
177 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
178 $this->assertSame(textlib::strlen($str, 'GB18030'), 4);
179 }
180
7aea08e1
SH
181 /**
182 * Tests the static strtolower method
183 * @return void
184 */
5bd40408
PS
185 public function test_strtolower() {
186 $str = "Žluťoučký koníček";
187 $low = 'žluťoučký koníček';
188 $this->assertSame(textlib::strtolower($str), $low);
189
190 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
191 $this->assertSame(textlib::strtolower($iso2, 'iso-8859-2'), textlib::convert($low, 'utf-8', 'iso-8859-2'));
192
193 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
194 $this->assertSame(textlib::strtolower($win, 'cp1250'), textlib::convert($low, 'utf-8', 'cp1250'));
195
196
197 $str = '言語設定';
198 $this->assertSame(textlib::strtolower($str), $str);
199
200 $str = '简体中文';
201 $this->assertSame(textlib::strtolower($str), $str);
202
203 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
204 $this->assertSame(textlib::strtolower($str, 'ISO-2022-JP'), $str);
205
206 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
207 $this->assertSame(textlib::strtolower($str, 'SHIFT-JIS'), $str);
208
209 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
210 $this->assertSame(textlib::strtolower($str, 'GB2312'), $str);
211
212 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
213 $this->assertSame(textlib::strtolower($str, 'GB18030'), $str);
bc5c10f6
PS
214
215 // typo3 has problems with integers
216 $str = 1309528800;
217 $this->assertSame((string)$str, textlib::strtolower($str));
5bd40408
PS
218 }
219
7aea08e1
SH
220 /**
221 * Tests the static strtoupper
222 * @return void
223 */
5bd40408
PS
224 public function test_strtoupper() {
225 $str = "Žluťoučký koníček";
226 $up = 'ŽLUŤOUČKÝ KONÍČEK';
227 $this->assertSame(textlib::strtoupper($str), $up);
228
229 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
230 $this->assertSame(textlib::strtoupper($iso2, 'iso-8859-2'), textlib::convert($up, 'utf-8', 'iso-8859-2'));
231
232 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
233 $this->assertSame(textlib::strtoupper($win, 'cp1250'), textlib::convert($up, 'utf-8', 'cp1250'));
234
235
236 $str = '言語設定';
237 $this->assertSame(textlib::strtoupper($str), $str);
238
239 $str = '简体中文';
240 $this->assertSame(textlib::strtoupper($str), $str);
241
242 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
243 $this->assertSame(textlib::strtoupper($str, 'ISO-2022-JP'), $str);
244
245 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
246 $this->assertSame(textlib::strtoupper($str, 'SHIFT-JIS'), $str);
247
248 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
249 $this->assertSame(textlib::strtoupper($str, 'GB2312'), $str);
250
251 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
252 $this->assertSame(textlib::strtoupper($str, 'GB18030'), $str);
253 }
254
7aea08e1
SH
255 /**
256 * Tests the static strpos method
257 * @return void
258 */
5bd40408
PS
259 public function test_strpos() {
260 $str = "Žluťoučký koníček";
261 $this->assertSame(textlib::strpos($str, 'koníč'), 10);
262 }
263
7aea08e1
SH
264 /**
265 * Tests the static strrpos
266 * @return void
267 */
5bd40408
PS
268 public function test_strrpos() {
269 $str = "Žluťoučký koníček";
270 $this->assertSame(textlib::strrpos($str, 'o'), 11);
271 }
272
7aea08e1
SH
273 /**
274 * Tests the static specialtoascii method
275 * @return void
276 */
5bd40408
PS
277 public function test_specialtoascii() {
278 $str = "Žluťoučký koníček";
279 $this->assertSame(textlib::specialtoascii($str), 'Zlutoucky konicek');
280 }
281
7aea08e1
SH
282 /**
283 * Tests the static encode_mimeheader method
284 * @return void
285 */
5bd40408
PS
286 public function test_encode_mimeheader() {
287 $str = "Žluťoučký koníček";
288 $this->assertSame(textlib::encode_mimeheader($str), '=?utf-8?B?xb1sdcWlb3XEjWvDvSBrb27DrcSNZWs=?=');
289 }
290
7aea08e1
SH
291 /**
292 * Tests the static entities_to_utf8 method
293 * @return void
294 */
5bd40408 295 public function test_entities_to_utf8() {
ea0f9a27
PS
296 $str = "&#x17d;lu&#x165;ou&#x10d;k&#xfd; kon&iacute;&#269;ek&copy;&quot;&amp;&lt;&gt;&sect;&laquo;";
297 $this->assertSame("Žluťoučký koníček©\"&<>§«", textlib::entities_to_utf8($str));
5bd40408
PS
298 }
299
7aea08e1
SH
300 /**
301 * Tests the static utf8_to_entities method
302 * @return void
303 */
5bd40408 304 public function test_utf8_to_entities() {
ea0f9a27
PS
305 $str = "&#x17d;luťoučký kon&iacute;ček&copy;&quot;&amp;&lt;&gt;&sect;&laquo;";
306 $this->assertSame("&#x17d;lu&#x165;ou&#x10d;k&#xfd; kon&iacute;&#x10d;ek&copy;&quot;&amp;&lt;&gt;&sect;&laquo;", textlib::utf8_to_entities($str));
307 $this->assertSame("&#381;lu&#357;ou&#269;k&#253; kon&iacute;&#269;ek&copy;&quot;&amp;&lt;&gt;&sect;&laquo;", textlib::utf8_to_entities($str, true));
5bd40408 308
ea0f9a27
PS
309 $str = "&#381;luťoučký kon&iacute;ček&copy;&quot;&amp;&lt;&gt;&sect;&laquo;";
310 $this->assertSame("&#x17d;lu&#x165;ou&#x10d;k&#xfd; kon&#xed;&#x10d;ek&#xa9;\"&<>&#xa7;&#xab;", textlib::utf8_to_entities($str, false, true));
311 $this->assertSame("&#381;lu&#357;ou&#269;k&#253; kon&#237;&#269;ek&#169;\"&<>&#167;&#171;", textlib::utf8_to_entities($str, true, true));
5bd40408
PS
312 }
313
7aea08e1
SH
314 /**
315 * Tests the static trim_utf8_bom method
316 * @return void
317 */
5bd40408
PS
318 public function test_trim_utf8_bom() {
319 $bom = "\xef\xbb\xbf";
320 $str = "Žluťoučký koníček";
321 $this->assertSame(textlib::trim_utf8_bom($bom.$str.$bom), $str.$bom);
322 }
323
7aea08e1
SH
324 /**
325 * Tests the static get_encodings method
326 * @return void
327 */
5bd40408
PS
328 public function test_get_encodings() {
329 $encodings = textlib::get_encodings();
330 $this->assertTrue(is_array($encodings));
331 $this->assertTrue(count($encodings) > 1);
332 $this->assertTrue(isset($encodings['UTF-8']));
333 }
334
7aea08e1
SH
335 /**
336 * Tests the static code2utf8 method
337 * @return void
338 */
5bd40408
PS
339 public function test_code2utf8() {
340 $this->assertSame(textlib::code2utf8(381), 'Ž');
341 }
342
7aea08e1
SH
343 /**
344 * Tests the static strtotitle method
345 * @return void
346 */
5bd40408
PS
347 public function test_strtotitle() {
348 $str = "žluťoučký koníček";
349 $this->assertSame(textlib::strtotitle($str), "Žluťoučký Koníček");
350 }
351
7aea08e1
SH
352 /**
353 * Tests the deprecated method of textlib that still require an instance.
354 * @return void
355 */
5bd40408
PS
356 public function test_deprecated_textlib_get_instance() {
357 ob_start();
358 $textlib = textlib_get_instance();
359 $output = ob_get_contents();
360 $this->assertNotEmpty($output);
361 ob_end_clean();
362 $this->assertSame($textlib->substr('abc', 1, 1), 'b');
363 $this->assertSame($textlib->strlen('abc'), 3);
364 $this->assertSame($textlib->strtoupper('Abc'), 'ABC');
365 $this->assertSame($textlib->strtolower('Abc'), 'abc');
366 $this->assertSame($textlib->strpos('abc', 'a'), 0);
367 $this->assertSame($textlib->strpos('abc', 'd'), false);
368 $this->assertSame($textlib->strrpos('abcabc', 'a'), 3);
369 $this->assertSame($textlib->specialtoascii('ábc'), 'abc');
370 $this->assertSame($textlib->strtotitle('abc ABC'), 'Abc Abc');
371 }
372}
373
374
375/**
376 * Unit tests for our utf-8 aware collator.
377 *
378 * Used for sorting.
379 *
380 * @package core
7aea08e1 381 * @category phpunit
5bd40408
PS
382 * @copyright 2011 Sam Hemelryk
383 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
384 */
385class collatorlib_testcase extends basic_testcase {
386
7aea08e1
SH
387 /**
388 * @var string The initial lang, stored because we change it during testing
389 */
5bd40408 390 protected $initiallang = null;
7aea08e1
SH
391
392 /**
393 * @var string The last error that has occured
394 */
5bd40408
PS
395 protected $error = null;
396
7aea08e1
SH
397 /**
398 * Prepares things for this test case
399 * @return void
400 */
1cbf2a20 401 protected function setUp() {
5bd40408
PS
402 global $SESSION;
403 if (isset($SESSION->lang)) {
404 $this->initiallang = $SESSION->lang;
405 }
406 $SESSION->lang = 'en'; // make sure we test en language to get consistent results, hopefully all systems have this locale
407 if (extension_loaded('intl')) {
408 $this->error = 'Collation aware sorting not supported';
409 } else {
410 $this->error = 'Collation aware sorting not supported, PHP extension "intl" is not available.';
411 }
412 parent::setUp();
413 }
7aea08e1
SH
414
415 /**
416 * Cleans things up after this test case has run
417 * @return void
418 */
1cbf2a20 419 protected function tearDown() {
5bd40408
PS
420 global $SESSION;
421 parent::tearDown();
422 if ($this->initiallang !== null) {
423 $SESSION->lang = $this->initiallang;
424 $this->initiallang = null;
425 } else {
426 unset($SESSION->lang);
427 }
428 }
7aea08e1
SH
429
430 /**
431 * Tests the static asort method
432 * @return void
433 */
434 public function test_asort() {
5bd40408 435 $arr = array('b' => 'ab', 1 => 'aa', 0 => 'cc');
21282685
PS
436 $result = collatorlib::asort($arr);
437 $this->assertSame(array_values($arr), array('aa', 'ab', 'cc'));
5bd40408 438 $this->assertSame(array_keys($arr), array(1, 'b', 0));
21282685
PS
439 $this->assertTrue($result);
440
441 $arr = array('b' => 'ab', 1 => 'aa', 0 => 'cc');
442 $result = collatorlib::asort($arr, collatorlib::SORT_STRING);
5bd40408 443 $this->assertSame(array_values($arr), array('aa', 'ab', 'cc'));
21282685
PS
444 $this->assertSame(array_keys($arr), array(1, 'b', 0));
445 $this->assertTrue($result);
5bd40408 446
21282685
PS
447 $arr = array('b' => 'aac', 1 => 'Aac', 0 => 'cc');
448 $result = collatorlib::asort($arr, (collatorlib::SORT_STRING | collatorlib::CASE_SENSITIVE));
449 $this->assertSame(array_values($arr), array('Aac', 'aac', 'cc'));
450 $this->assertSame(array_keys($arr), array(1, 'b', 0));
451 $this->assertTrue($result);
452
453 $arr = array('b' => 'a1', 1 => 'a10', 0 => 'a3b');
454 $result = collatorlib::asort($arr);
455 $this->assertSame(array_values($arr), array('a1', 'a10', 'a3b'));
456 $this->assertSame(array_keys($arr), array('b', 1, 0));
457 $this->assertTrue($result);
458
459 $arr = array('b' => 'a1', 1 => 'a10', 0 => 'a3b');
460 $result = collatorlib::asort($arr, collatorlib::SORT_NATURAL);
461 $this->assertSame(array_values($arr), array('a1', 'a3b', 'a10'));
462 $this->assertSame(array_keys($arr), array('b', 0, 1));
463 $this->assertTrue($result);
464
465 $arr = array('b' => '1.1.1', 1 => '1.2', 0 => '1.20.2');
466 $result = collatorlib::asort($arr, collatorlib::SORT_NATURAL);
467 $this->assertSame(array_values($arr), array('1.1.1', '1.2', '1.20.2'));
468 $this->assertSame(array_keys($arr), array('b', 1, 0));
469 $this->assertTrue($result);
470
471 $arr = array('b' => '-1', 1 => 1000, 0 => -1.2, 3 => 1, 4 => false);
472 $result = collatorlib::asort($arr, collatorlib::SORT_NUMERIC);
473 $this->assertSame(array_values($arr), array(-1.2, '-1', false, 1, 1000));
474 $this->assertSame(array_keys($arr), array(0, 'b', 4, 3, 1));
475 $this->assertTrue($result);
476
477 $arr = array('b' => array(1), 1 => array(2, 3), 0 => 1);
478 $result = collatorlib::asort($arr, collatorlib::SORT_REGULAR);
479 $this->assertSame(array_values($arr), array(1, array(1), array(2, 3)));
480 $this->assertSame(array_keys($arr), array(0, 'b', 1));
481 $this->assertTrue($result);
482
b273b20a
483 // test sorting of array of arrays - first element should be used for actual comparison
484 $arr = array(0=>array('bb', 'z'), 1=>array('ab', 'a'), 2=>array('zz', 'x'));
485 $result = collatorlib::asort($arr, collatorlib::SORT_REGULAR);
486 $this->assertSame(array_keys($arr), array(1, 0, 2));
487 $this->assertTrue($result);
488
21282685
PS
489 $arr = array('a' => 'áb', 'b' => 'ab', 1 => 'aa', 0=>'cc', 'x' => 'Áb',);
490 $result = collatorlib::asort($arr);
491 $this->assertSame(array_values($arr), array('aa', 'ab', 'áb', 'Áb', 'cc'), $this->error);
492 $this->assertSame(array_keys($arr), array(1, 'b', 'a', 'x', 0), $this->error);
493 $this->assertTrue($result);
bc53b4e9
PS
494
495 $a = array(2=>'b', 1=>'c');
496 $c =& $a;
497 $b =& $a;
498 collatorlib::asort($b);
499 $this->assertSame($a, $b);
500 $this->assertSame($c, $b);
5bd40408 501 }
7aea08e1
SH
502
503 /**
504 * Tests the static asort_objects_by_method method
505 * @return void
506 */
507 public function test_asort_objects_by_method() {
5bd40408
PS
508 $objects = array(
509 'b' => new string_test_class('ab'),
510 1 => new string_test_class('aa'),
511 0 => new string_test_class('cc')
512 );
21282685 513 $result = collatorlib::asort_objects_by_method($objects, 'get_protected_name');
5bd40408
PS
514 $this->assertSame(array_keys($objects), array(1, 'b', 0));
515 $this->assertSame($this->get_ordered_names($objects, 'get_protected_name'), array('aa', 'ab', 'cc'));
21282685 516 $this->assertTrue($result);
5bd40408
PS
517
518 $objects = array(
21282685
PS
519 'b' => new string_test_class('a20'),
520 1 => new string_test_class('a1'),
521 0 => new string_test_class('a100')
5bd40408 522 );
21282685
PS
523 $result = collatorlib::asort_objects_by_method($objects, 'get_protected_name', collatorlib::SORT_NATURAL);
524 $this->assertSame(array_keys($objects), array(1, 'b', 0));
525 $this->assertSame($this->get_ordered_names($objects, 'get_protected_name'), array('a1', 'a20', 'a100'));
526 $this->assertTrue($result);
5bd40408 527 }
7aea08e1
SH
528
529 /**
530 * Tests the static asort_objects_by_method method
531 * @return void
532 */
21282685 533 public function test_asort_objects_by_property() {
5bd40408
PS
534 $objects = array(
535 'b' => new string_test_class('ab'),
536 1 => new string_test_class('aa'),
537 0 => new string_test_class('cc')
538 );
21282685 539 $result = collatorlib::asort_objects_by_property($objects, 'publicname');
5bd40408
PS
540 $this->assertSame(array_keys($objects), array(1, 'b', 0));
541 $this->assertSame($this->get_ordered_names($objects, 'publicname'), array('aa', 'ab', 'cc'));
21282685 542 $this->assertTrue($result);
5bd40408
PS
543
544 $objects = array(
21282685
PS
545 'b' => new string_test_class('a20'),
546 1 => new string_test_class('a1'),
547 0 => new string_test_class('a100')
5bd40408 548 );
21282685
PS
549 $result = collatorlib::asort_objects_by_property($objects, 'publicname', collatorlib::SORT_NATURAL);
550 $this->assertSame(array_keys($objects), array(1, 'b', 0));
551 $this->assertSame($this->get_ordered_names($objects, 'publicname'), array('a1', 'a20', 'a100'));
552 $this->assertTrue($result);
5bd40408 553 }
7aea08e1
SH
554
555 /**
556 * Returns an array of sorted names
557 * @param array $objects
558 * @param string $methodproperty
559 * @return type
560 */
5bd40408
PS
561 protected function get_ordered_names($objects, $methodproperty = 'get_protected_name') {
562 $return = array();
563 foreach ($objects as $object) {
564 if ($methodproperty == 'publicname') {
565 $return[] = $object->publicname;
566 } else {
567 $return[] = $object->$methodproperty();
568 }
569 }
570 return $return;
571 }
b71d0b43
PS
572
573 /**
574 * Tests the static ksort method
575 * @return void
576 */
577 public function test_ksort() {
578 $arr = array('b' => 'ab', 1 => 'aa', 0 => 'cc');
579 $result = collatorlib::ksort($arr);
580 $this->assertSame(array_keys($arr), array(0, 1, 'b'));
581 $this->assertSame(array_values($arr), array('cc', 'aa', 'ab'));
582 $this->assertTrue($result);
583
584 $obj = new stdClass();
585 $arr = array('1.1.1'=>array(), '1.2'=>$obj, '1.20.2'=>null);
586 $result = collatorlib::ksort($arr, collatorlib::SORT_NATURAL);
587 $this->assertSame(array_keys($arr), array('1.1.1', '1.2', '1.20.2'));
588 $this->assertSame(array_values($arr), array(array(), $obj, null));
589 $this->assertTrue($result);
bc53b4e9
PS
590
591 $a = array(2=>'b', 1=>'c');
592 $c =& $a;
593 $b =& $a;
594 collatorlib::ksort($b);
595 $this->assertSame($a, $b);
596 $this->assertSame($c, $b);
b71d0b43 597 }
5bd40408
PS
598}
599
600
601/**
602 * Simple class used to work with the unit test.
603 *
604 * @package core
7aea08e1 605 * @category phpunit
5bd40408
PS
606 * @copyright 2011 Sam Hemelryk
607 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
608 */
609class string_test_class extends stdClass {
7aea08e1
SH
610 /**
611 * @var string A public property
612 */
5bd40408 613 public $publicname;
7aea08e1
SH
614 /**
615 * @var string A protected property
616 */
5bd40408 617 protected $protectedname;
7aea08e1
SH
618 /**
619 * @var string A private property
620 */
5bd40408 621 private $privatename;
7aea08e1
SH
622 /**
623 * Constructs the test instance
624 * @param string $name
625 */
5bd40408
PS
626 public function __construct($name) {
627 $this->publicname = $name;
628 $this->protectedname = $name;
629 $this->privatename = $name;
630 }
7aea08e1
SH
631 /**
632 * Returns the protected property
633 * @return string
634 */
5bd40408
PS
635 public function get_protected_name() {
636 return $this->protectedname;
637 }
7aea08e1
SH
638 /**
639 * Returns the protected property
640 * @return string
641 */
5bd40408
PS
642 public function get_private_name() {
643 return $this->publicname;
644 }
7aea08e1 645}