2014-02-18 15:29:05 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2014 Vincent Petry <PVince81@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2016-05-19 10:38:52 +03:00
|
|
|
namespace Test;
|
|
|
|
|
|
|
|
class NaturalSortTest extends \Test\TestCase {
|
2014-02-18 15:29:05 +04:00
|
|
|
|
2015-02-25 01:51:08 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider naturalSortDataProvider
|
|
|
|
*/
|
2020-04-10 15:19:56 +03:00
|
|
|
public function testNaturalSortCompare($array, $sorted) {
|
|
|
|
if (!class_exists('Collator')) {
|
2015-02-25 01:51:08 +03:00
|
|
|
$this->markTestSkipped('The intl module is not available, natural sorting might not work as expected.');
|
2014-02-18 15:29:05 +04:00
|
|
|
return;
|
|
|
|
}
|
2015-02-25 01:51:08 +03:00
|
|
|
$comparator = \OC\NaturalSort::getInstance();
|
2020-03-26 11:30:18 +03:00
|
|
|
usort($array, [$comparator, 'compare']);
|
2015-02-25 01:51:08 +03:00
|
|
|
$this->assertEquals($sorted, $array);
|
2014-02-18 15:29:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* @dataProvider defaultCollatorDataProvider
|
|
|
|
*/
|
2020-04-10 15:19:56 +03:00
|
|
|
public function testDefaultCollatorCompare($array, $sorted) {
|
2015-02-25 01:51:08 +03:00
|
|
|
$comparator = new \OC\NaturalSort(new \OC\NaturalSort_DefaultCollator());
|
2020-03-26 11:30:18 +03:00
|
|
|
usort($array, [$comparator, 'compare']);
|
2014-02-18 15:29:05 +04:00
|
|
|
$this->assertEquals($sorted, $array);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-25 01:51:08 +03:00
|
|
|
* Data provider for natural sorting with php5-intl's Collator.
|
2014-02-18 15:29:05 +04:00
|
|
|
* Must provide the same result as in core/js/tests/specs/coreSpec.js
|
|
|
|
* @return array test cases
|
|
|
|
*/
|
2020-04-10 15:19:56 +03:00
|
|
|
public function naturalSortDataProvider() {
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
2014-02-18 15:29:05 +04:00
|
|
|
// different casing
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2014-02-18 15:29:05 +04:00
|
|
|
// unsorted
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2014-02-18 15:29:05 +04:00
|
|
|
'aaa',
|
|
|
|
'bbb',
|
|
|
|
'BBB',
|
|
|
|
'AAA'
|
2020-03-26 11:30:18 +03:00
|
|
|
],
|
2014-02-18 15:29:05 +04:00
|
|
|
// sorted
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2014-02-18 15:29:05 +04:00
|
|
|
'aaa',
|
|
|
|
'AAA',
|
|
|
|
'bbb',
|
|
|
|
'BBB'
|
2020-03-26 11:30:18 +03:00
|
|
|
]
|
|
|
|
],
|
2014-02-18 15:29:05 +04:00
|
|
|
// numbers
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2014-02-18 15:29:05 +04:00
|
|
|
// unsorted
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2014-02-18 15:29:05 +04:00
|
|
|
'124.txt',
|
|
|
|
'abc1',
|
|
|
|
'123.txt',
|
|
|
|
'abc',
|
|
|
|
'abc2',
|
|
|
|
'def (2).txt',
|
|
|
|
'ghi 10.txt',
|
|
|
|
'abc12',
|
|
|
|
'def.txt',
|
|
|
|
'def (1).txt',
|
|
|
|
'ghi 2.txt',
|
|
|
|
'def (10).txt',
|
|
|
|
'abc10',
|
|
|
|
'def (12).txt',
|
|
|
|
'z',
|
|
|
|
'ghi.txt',
|
|
|
|
'za',
|
|
|
|
'ghi 1.txt',
|
|
|
|
'ghi 12.txt',
|
|
|
|
'zz',
|
2014-07-23 14:52:13 +04:00
|
|
|
'15.txt',
|
|
|
|
'15b.txt',
|
2020-03-26 11:30:18 +03:00
|
|
|
],
|
2014-02-18 15:29:05 +04:00
|
|
|
// sorted
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2014-07-23 14:52:13 +04:00
|
|
|
'15.txt',
|
|
|
|
'15b.txt',
|
2014-02-18 15:29:05 +04:00
|
|
|
'123.txt',
|
|
|
|
'124.txt',
|
|
|
|
'abc',
|
|
|
|
'abc1',
|
|
|
|
'abc2',
|
|
|
|
'abc10',
|
|
|
|
'abc12',
|
|
|
|
'def.txt',
|
|
|
|
'def (1).txt',
|
|
|
|
'def (2).txt',
|
|
|
|
'def (10).txt',
|
|
|
|
'def (12).txt',
|
|
|
|
'ghi.txt',
|
|
|
|
'ghi 1.txt',
|
|
|
|
'ghi 2.txt',
|
|
|
|
'ghi 10.txt',
|
|
|
|
'ghi 12.txt',
|
|
|
|
'z',
|
|
|
|
'za',
|
|
|
|
'zz',
|
2020-03-26 11:30:18 +03:00
|
|
|
]
|
|
|
|
],
|
2014-02-18 15:29:05 +04:00
|
|
|
// chinese characters
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2014-02-18 15:29:05 +04:00
|
|
|
// unsorted
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2014-02-18 15:29:05 +04:00
|
|
|
'十.txt',
|
|
|
|
'一.txt',
|
|
|
|
'二.txt',
|
|
|
|
'十 2.txt',
|
|
|
|
'三.txt',
|
|
|
|
'四.txt',
|
|
|
|
'abc.txt',
|
|
|
|
'五.txt',
|
|
|
|
'七.txt',
|
|
|
|
'八.txt',
|
|
|
|
'九.txt',
|
|
|
|
'六.txt',
|
|
|
|
'十一.txt',
|
|
|
|
'波.txt',
|
|
|
|
'破.txt',
|
|
|
|
'莫.txt',
|
|
|
|
'啊.txt',
|
|
|
|
'123.txt',
|
2020-03-26 11:30:18 +03:00
|
|
|
],
|
2014-02-18 15:29:05 +04:00
|
|
|
// sorted
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2014-02-18 15:29:05 +04:00
|
|
|
'123.txt',
|
|
|
|
'abc.txt',
|
|
|
|
'一.txt',
|
|
|
|
'七.txt',
|
|
|
|
'三.txt',
|
|
|
|
'九.txt',
|
|
|
|
'二.txt',
|
|
|
|
'五.txt',
|
|
|
|
'八.txt',
|
|
|
|
'六.txt',
|
|
|
|
'十.txt',
|
|
|
|
'十 2.txt',
|
|
|
|
'十一.txt',
|
|
|
|
'啊.txt',
|
|
|
|
'四.txt',
|
|
|
|
'波.txt',
|
|
|
|
'破.txt',
|
|
|
|
'莫.txt',
|
2020-03-26 11:30:18 +03:00
|
|
|
]
|
|
|
|
],
|
2014-02-18 15:29:05 +04:00
|
|
|
// with umlauts
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2014-02-18 15:29:05 +04:00
|
|
|
// unsorted
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2014-02-18 15:29:05 +04:00
|
|
|
'öh.txt',
|
|
|
|
'Äh.txt',
|
|
|
|
'oh.txt',
|
|
|
|
'Üh 2.txt',
|
|
|
|
'Üh.txt',
|
|
|
|
'ah.txt',
|
|
|
|
'Öh.txt',
|
|
|
|
'uh.txt',
|
|
|
|
'üh.txt',
|
|
|
|
'äh.txt',
|
2020-03-26 11:30:18 +03:00
|
|
|
],
|
2014-02-18 15:29:05 +04:00
|
|
|
// sorted
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2014-02-18 15:29:05 +04:00
|
|
|
'ah.txt',
|
|
|
|
'äh.txt',
|
|
|
|
'Äh.txt',
|
|
|
|
'oh.txt',
|
|
|
|
'öh.txt',
|
|
|
|
'Öh.txt',
|
|
|
|
'uh.txt',
|
|
|
|
'üh.txt',
|
|
|
|
'Üh.txt',
|
|
|
|
'Üh 2.txt',
|
2020-03-26 11:30:18 +03:00
|
|
|
]
|
|
|
|
],
|
|
|
|
];
|
2014-02-18 15:29:05 +04:00
|
|
|
}
|
2015-02-25 01:51:08 +03:00
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* Data provider for natural sorting with \OC\NaturalSort_DefaultCollator.
|
|
|
|
* Must provide the same result as in core/js/tests/specs/coreSpec.js
|
|
|
|
* @return array test cases
|
|
|
|
*/
|
2020-04-10 15:19:56 +03:00
|
|
|
public function defaultCollatorDataProvider() {
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
2015-02-25 01:51:08 +03:00
|
|
|
// different casing
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2015-02-25 01:51:08 +03:00
|
|
|
// unsorted
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2015-02-25 01:51:08 +03:00
|
|
|
'aaa',
|
|
|
|
'bbb',
|
|
|
|
'BBB',
|
|
|
|
'AAA'
|
2020-03-26 11:30:18 +03:00
|
|
|
],
|
2015-02-25 01:51:08 +03:00
|
|
|
// sorted
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2015-02-25 01:51:08 +03:00
|
|
|
'aaa',
|
|
|
|
'AAA',
|
|
|
|
'bbb',
|
|
|
|
'BBB'
|
2020-03-26 11:30:18 +03:00
|
|
|
]
|
|
|
|
],
|
2015-02-25 01:51:08 +03:00
|
|
|
// numbers
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2015-02-25 01:51:08 +03:00
|
|
|
// unsorted
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2015-02-25 01:51:08 +03:00
|
|
|
'124.txt',
|
|
|
|
'abc1',
|
|
|
|
'123.txt',
|
|
|
|
'abc',
|
|
|
|
'abc2',
|
|
|
|
'def (2).txt',
|
|
|
|
'ghi 10.txt',
|
|
|
|
'abc12',
|
|
|
|
'def.txt',
|
|
|
|
'def (1).txt',
|
|
|
|
'ghi 2.txt',
|
|
|
|
'def (10).txt',
|
|
|
|
'abc10',
|
|
|
|
'def (12).txt',
|
|
|
|
'z',
|
|
|
|
'ghi.txt',
|
|
|
|
'za',
|
|
|
|
'ghi 1.txt',
|
|
|
|
'ghi 12.txt',
|
|
|
|
'zz',
|
|
|
|
'15.txt',
|
|
|
|
'15b.txt',
|
2020-03-26 11:30:18 +03:00
|
|
|
],
|
2015-02-25 01:51:08 +03:00
|
|
|
// sorted
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2015-02-25 01:51:08 +03:00
|
|
|
'15.txt',
|
|
|
|
'15b.txt',
|
|
|
|
'123.txt',
|
|
|
|
'124.txt',
|
|
|
|
'abc',
|
|
|
|
'abc1',
|
|
|
|
'abc2',
|
|
|
|
'abc10',
|
|
|
|
'abc12',
|
|
|
|
'def.txt',
|
|
|
|
'def (1).txt',
|
|
|
|
'def (2).txt',
|
|
|
|
'def (10).txt',
|
|
|
|
'def (12).txt',
|
|
|
|
'ghi.txt',
|
|
|
|
'ghi 1.txt',
|
|
|
|
'ghi 2.txt',
|
|
|
|
'ghi 10.txt',
|
|
|
|
'ghi 12.txt',
|
|
|
|
'z',
|
|
|
|
'za',
|
|
|
|
'zz',
|
2020-03-26 11:30:18 +03:00
|
|
|
]
|
|
|
|
],
|
|
|
|
];
|
2015-02-25 01:51:08 +03:00
|
|
|
}
|
2014-02-18 15:29:05 +04:00
|
|
|
}
|