Merge branch 'w24_MDL-33635_m23_sort' of git://github.com/skodak/moodle
[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);
65
66
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);
71
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);
75
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);
79
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);
84
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 }
89
7aea08e1
SH
90 /**
91 * Tests the static sub string method
92 * @return void
93 */
5bd40408
PS
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');
101
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'));
106
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'));
111
112
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);
116
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);
120
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);
124
125 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
126 $s = pack("H*", "cce5"); //GB2312
127 $this->assertSame(textlib::substr($str, 1, 1, 'GB2312'), $s);
128
129 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
130 $s = pack("H*", "cce5"); //GB18030
131 $this->assertSame(textlib::substr($str, 1, 1, 'GB18030'), $s);
132 }
133
7aea08e1
SH
134 /**
135 * Tests the static string length method
136 * @return void
137 */
5bd40408
PS
138 public function test_strlen() {
139 $str = "Žluťoučký koníček";
140 $this->assertSame(textlib::strlen($str), 17);
141
142 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
143 $this->assertSame(textlib::strlen($iso2, 'iso-8859-2'), 17);
144
145 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
146 $this->assertSame(textlib::strlen($win, 'cp1250'), 17);
147
148
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);
153
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);
158
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);
163
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);
168
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 }
174
7aea08e1
SH
175 /**
176 * Tests the static strtolower method
177 * @return void
178 */
5bd40408
PS
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);
183
184 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
185 $this->assertSame(textlib::strtolower($iso2, 'iso-8859-2'), textlib::convert($low, 'utf-8', 'iso-8859-2'));
186
187 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
188 $this->assertSame(textlib::strtolower($win, 'cp1250'), textlib::convert($low, 'utf-8', 'cp1250'));
189
190
191 $str = '言語設定';
192 $this->assertSame(textlib::strtolower($str), $str);
193
194 $str = '简体中文';
195 $this->assertSame(textlib::strtolower($str), $str);
196
197 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
198 $this->assertSame(textlib::strtolower($str, 'ISO-2022-JP'), $str);
199
200 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
201 $this->assertSame(textlib::strtolower($str, 'SHIFT-JIS'), $str);
202
203 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
204 $this->assertSame(textlib::strtolower($str, 'GB2312'), $str);
205
206 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
207 $this->assertSame(textlib::strtolower($str, 'GB18030'), $str);
bc5c10f6
PS
208
209 // typo3 has problems with integers
210 $str = 1309528800;
211 $this->assertSame((string)$str, textlib::strtolower($str));
5bd40408
PS
212 }
213
7aea08e1
SH
214 /**
215 * Tests the static strtoupper
216 * @return void
217 */
5bd40408
PS
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);
222
223 $iso2 = pack("H*", "ae6c75bb6f75e86bfd206b6f6eede8656b");
224 $this->assertSame(textlib::strtoupper($iso2, 'iso-8859-2'), textlib::convert($up, 'utf-8', 'iso-8859-2'));
225
226 $win = pack("H*", "8e6c759d6f75e86bfd206b6f6eede8656b");
227 $this->assertSame(textlib::strtoupper($win, 'cp1250'), textlib::convert($up, 'utf-8', 'cp1250'));
228
229
230 $str = '言語設定';
231 $this->assertSame(textlib::strtoupper($str), $str);
232
233 $str = '简体中文';
234 $this->assertSame(textlib::strtoupper($str), $str);
235
236 $str = pack("H*", "1b24423840386c405f446a1b2842"); //ISO-2022-JP
237 $this->assertSame(textlib::strtoupper($str, 'ISO-2022-JP'), $str);
238
239 $str = pack("H*", "8cbe8cea90dd92e8"); //SHIFT-JIS
240 $this->assertSame(textlib::strtoupper($str, 'SHIFT-JIS'), $str);
241
242 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB2312
243 $this->assertSame(textlib::strtoupper($str, 'GB2312'), $str);
244
245 $str = pack("H*", "bcf2cce5d6d0cec4"); //GB18030
246 $this->assertSame(textlib::strtoupper($str, 'GB18030'), $str);
247 }
248
7aea08e1
SH
249 /**
250 * Tests the static strpos method
251 * @return void
252 */
5bd40408
PS
253 public function test_strpos() {
254 $str = "Žluťoučký koníček";
255 $this->assertSame(textlib::strpos($str, 'koníč'), 10);
256 }
257
7aea08e1
SH
258 /**
259 * Tests the static strrpos
260 * @return void
261 */
5bd40408
PS
262 public function test_strrpos() {
263 $str = "Žluťoučký koníček";
264 $this->assertSame(textlib::strrpos($str, 'o'), 11);
265 }
266
7aea08e1
SH
267 /**
268 * Tests the static specialtoascii method
269 * @return void
270 */
5bd40408
PS
271 public function test_specialtoascii() {
272 $str = "Žluťoučký koníček";
273 $this->assertSame(textlib::specialtoascii($str), 'Zlutoucky konicek');
274 }
275
7aea08e1
SH
276 /**
277 * Tests the static encode_mimeheader method
278 * @return void
279 */
5bd40408
PS
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 }
284
7aea08e1
SH
285 /**
286 * Tests the static entities_to_utf8 method
287 * @return void
288 */
5bd40408
PS
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 }
293
7aea08e1
SH
294 /**
295 * Tests the static utf8_to_entities method
296 * @return void
297 */
5bd40408
PS
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");
302
303 }
304
7aea08e1
SH
305 /**
306 * Tests the static trim_utf8_bom method
307 * @return void
308 */
5bd40408
PS
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 }
314
7aea08e1
SH
315 /**
316 * Tests the static get_encodings method
317 * @return void
318 */
5bd40408
PS
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 }
325
7aea08e1
SH
326 /**
327 * Tests the static code2utf8 method
328 * @return void
329 */
5bd40408
PS
330 public function test_code2utf8() {
331 $this->assertSame(textlib::code2utf8(381), 'Ž');
332 }
333
7aea08e1
SH
334 /**
335 * Tests the static strtotitle method
336 * @return void
337 */
5bd40408
PS
338 public function test_strtotitle() {
339 $str = "žluťoučký koníček";
340 $this->assertSame(textlib::strtotitle($str), "Žluťoučký Koníček");
341 }
342
7aea08e1
SH
343 /**
344 * Tests the deprecated method of textlib that still require an instance.
345 * @return void
346 */
5bd40408
PS
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 }
363}
364
365
366/**
367 * Unit tests for our utf-8 aware collator.
368 *
369 * Used for sorting.
370 *
371 * @package core
7aea08e1 372 * @category phpunit
5bd40408
PS
373 * @copyright 2011 Sam Hemelryk
374 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
375 */
376class collatorlib_testcase extends basic_testcase {
377
7aea08e1
SH
378 /**
379 * @var string The initial lang, stored because we change it during testing
380 */
5bd40408 381 protected $initiallang = null;
7aea08e1
SH
382
383 /**
384 * @var string The last error that has occured
385 */
5bd40408
PS
386 protected $error = null;
387
7aea08e1
SH
388 /**
389 * Prepares things for this test case
390 * @return void
391 */
1cbf2a20 392 protected function setUp() {
5bd40408
PS
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 }
7aea08e1
SH
405
406 /**
407 * Cleans things up after this test case has run
408 * @return void
409 */
1cbf2a20 410 protected function tearDown() {
5bd40408
PS
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 }
7aea08e1
SH
420
421 /**
422 * Tests the static asort method
423 * @return void
424 */
425 public function test_asort() {
5bd40408 426 $arr = array('b' => 'ab', 1 => 'aa', 0 => 'cc');
21282685
PS
427 $result = collatorlib::asort($arr);
428 $this->assertSame(array_values($arr), array('aa', 'ab', 'cc'));
5bd40408 429 $this->assertSame(array_keys($arr), array(1, 'b', 0));
21282685
PS
430 $this->assertTrue($result);
431
432 $arr = array('b' => 'ab', 1 => 'aa', 0 => 'cc');
433 $result = collatorlib::asort($arr, collatorlib::SORT_STRING);
5bd40408 434 $this->assertSame(array_values($arr), array('aa', 'ab', 'cc'));
21282685
PS
435 $this->assertSame(array_keys($arr), array(1, 'b', 0));
436 $this->assertTrue($result);
5bd40408 437
21282685
PS
438 $arr = array('b' => 'aac', 1 => 'Aac', 0 => 'cc');
439 $result = collatorlib::asort($arr, (collatorlib::SORT_STRING | collatorlib::CASE_SENSITIVE));
440 $this->assertSame(array_values($arr), array('Aac', 'aac', 'cc'));
441 $this->assertSame(array_keys($arr), array(1, 'b', 0));
442 $this->assertTrue($result);
443
444 $arr = array('b' => 'a1', 1 => 'a10', 0 => 'a3b');
445 $result = collatorlib::asort($arr);
446 $this->assertSame(array_values($arr), array('a1', 'a10', 'a3b'));
447 $this->assertSame(array_keys($arr), array('b', 1, 0));
448 $this->assertTrue($result);
449
450 $arr = array('b' => 'a1', 1 => 'a10', 0 => 'a3b');
451 $result = collatorlib::asort($arr, collatorlib::SORT_NATURAL);
452 $this->assertSame(array_values($arr), array('a1', 'a3b', 'a10'));
453 $this->assertSame(array_keys($arr), array('b', 0, 1));
454 $this->assertTrue($result);
455
456 $arr = array('b' => '1.1.1', 1 => '1.2', 0 => '1.20.2');
457 $result = collatorlib::asort($arr, collatorlib::SORT_NATURAL);
458 $this->assertSame(array_values($arr), array('1.1.1', '1.2', '1.20.2'));
459 $this->assertSame(array_keys($arr), array('b', 1, 0));
460 $this->assertTrue($result);
461
462 $arr = array('b' => '-1', 1 => 1000, 0 => -1.2, 3 => 1, 4 => false);
463 $result = collatorlib::asort($arr, collatorlib::SORT_NUMERIC);
464 $this->assertSame(array_values($arr), array(-1.2, '-1', false, 1, 1000));
465 $this->assertSame(array_keys($arr), array(0, 'b', 4, 3, 1));
466 $this->assertTrue($result);
467
468 $arr = array('b' => array(1), 1 => array(2, 3), 0 => 1);
469 $result = collatorlib::asort($arr, collatorlib::SORT_REGULAR);
470 $this->assertSame(array_values($arr), array(1, array(1), array(2, 3)));
471 $this->assertSame(array_keys($arr), array(0, 'b', 1));
472 $this->assertTrue($result);
473
474 $arr = array('a' => 'áb', 'b' => 'ab', 1 => 'aa', 0=>'cc', 'x' => 'Áb',);
475 $result = collatorlib::asort($arr);
476 $this->assertSame(array_values($arr), array('aa', 'ab', 'áb', 'Áb', 'cc'), $this->error);
477 $this->assertSame(array_keys($arr), array(1, 'b', 'a', 'x', 0), $this->error);
478 $this->assertTrue($result);
bc53b4e9
PS
479
480 $a = array(2=>'b', 1=>'c');
481 $c =& $a;
482 $b =& $a;
483 collatorlib::asort($b);
484 $this->assertSame($a, $b);
485 $this->assertSame($c, $b);
5bd40408 486 }
7aea08e1
SH
487
488 /**
489 * Tests the static asort_objects_by_method method
490 * @return void
491 */
492 public function test_asort_objects_by_method() {
5bd40408
PS
493 $objects = array(
494 'b' => new string_test_class('ab'),
495 1 => new string_test_class('aa'),
496 0 => new string_test_class('cc')
497 );
21282685 498 $result = collatorlib::asort_objects_by_method($objects, 'get_protected_name');
5bd40408
PS
499 $this->assertSame(array_keys($objects), array(1, 'b', 0));
500 $this->assertSame($this->get_ordered_names($objects, 'get_protected_name'), array('aa', 'ab', 'cc'));
21282685 501 $this->assertTrue($result);
5bd40408
PS
502
503 $objects = array(
21282685
PS
504 'b' => new string_test_class('a20'),
505 1 => new string_test_class('a1'),
506 0 => new string_test_class('a100')
5bd40408 507 );
21282685
PS
508 $result = collatorlib::asort_objects_by_method($objects, 'get_protected_name', collatorlib::SORT_NATURAL);
509 $this->assertSame(array_keys($objects), array(1, 'b', 0));
510 $this->assertSame($this->get_ordered_names($objects, 'get_protected_name'), array('a1', 'a20', 'a100'));
511 $this->assertTrue($result);
5bd40408 512 }
7aea08e1
SH
513
514 /**
515 * Tests the static asort_objects_by_method method
516 * @return void
517 */
21282685 518 public function test_asort_objects_by_property() {
5bd40408
PS
519 $objects = array(
520 'b' => new string_test_class('ab'),
521 1 => new string_test_class('aa'),
522 0 => new string_test_class('cc')
523 );
21282685 524 $result = collatorlib::asort_objects_by_property($objects, 'publicname');
5bd40408
PS
525 $this->assertSame(array_keys($objects), array(1, 'b', 0));
526 $this->assertSame($this->get_ordered_names($objects, 'publicname'), array('aa', 'ab', 'cc'));
21282685 527 $this->assertTrue($result);
5bd40408
PS
528
529 $objects = array(
21282685
PS
530 'b' => new string_test_class('a20'),
531 1 => new string_test_class('a1'),
532 0 => new string_test_class('a100')
5bd40408 533 );
21282685
PS
534 $result = collatorlib::asort_objects_by_property($objects, 'publicname', collatorlib::SORT_NATURAL);
535 $this->assertSame(array_keys($objects), array(1, 'b', 0));
536 $this->assertSame($this->get_ordered_names($objects, 'publicname'), array('a1', 'a20', 'a100'));
537 $this->assertTrue($result);
5bd40408 538 }
7aea08e1
SH
539
540 /**
541 * Returns an array of sorted names
542 * @param array $objects
543 * @param string $methodproperty
544 * @return type
545 */
5bd40408
PS
546 protected function get_ordered_names($objects, $methodproperty = 'get_protected_name') {
547 $return = array();
548 foreach ($objects as $object) {
549 if ($methodproperty == 'publicname') {
550 $return[] = $object->publicname;
551 } else {
552 $return[] = $object->$methodproperty();
553 }
554 }
555 return $return;
556 }
b71d0b43
PS
557
558 /**
559 * Tests the static ksort method
560 * @return void
561 */
562 public function test_ksort() {
563 $arr = array('b' => 'ab', 1 => 'aa', 0 => 'cc');
564 $result = collatorlib::ksort($arr);
565 $this->assertSame(array_keys($arr), array(0, 1, 'b'));
566 $this->assertSame(array_values($arr), array('cc', 'aa', 'ab'));
567 $this->assertTrue($result);
568
569 $obj = new stdClass();
570 $arr = array('1.1.1'=>array(), '1.2'=>$obj, '1.20.2'=>null);
571 $result = collatorlib::ksort($arr, collatorlib::SORT_NATURAL);
572 $this->assertSame(array_keys($arr), array('1.1.1', '1.2', '1.20.2'));
573 $this->assertSame(array_values($arr), array(array(), $obj, null));
574 $this->assertTrue($result);
bc53b4e9
PS
575
576 $a = array(2=>'b', 1=>'c');
577 $c =& $a;
578 $b =& $a;
579 collatorlib::ksort($b);
580 $this->assertSame($a, $b);
581 $this->assertSame($c, $b);
b71d0b43 582 }
5bd40408
PS
583}
584
585
586/**
587 * Simple class used to work with the unit test.
588 *
589 * @package core
7aea08e1 590 * @category phpunit
5bd40408
PS
591 * @copyright 2011 Sam Hemelryk
592 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
593 */
594class string_test_class extends stdClass {
7aea08e1
SH
595 /**
596 * @var string A public property
597 */
5bd40408 598 public $publicname;
7aea08e1
SH
599 /**
600 * @var string A protected property
601 */
5bd40408 602 protected $protectedname;
7aea08e1
SH
603 /**
604 * @var string A private property
605 */
5bd40408 606 private $privatename;
7aea08e1
SH
607 /**
608 * Constructs the test instance
609 * @param string $name
610 */
5bd40408
PS
611 public function __construct($name) {
612 $this->publicname = $name;
613 $this->protectedname = $name;
614 $this->privatename = $name;
615 }
7aea08e1
SH
616 /**
617 * Returns the protected property
618 * @return string
619 */
5bd40408
PS
620 public function get_protected_name() {
621 return $this->protectedname;
622 }
7aea08e1
SH
623 /**
624 * Returns the protected property
625 * @return string
626 */
5bd40408
PS
627 public function get_private_name() {
628 return $this->publicname;
629 }
7aea08e1 630}