summaryrefslogtreecommitdiffstats
path: root/tests/lib/naturalsort.php
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2014-02-18 12:29:05 +0100
committerVincent Petry <pvince81@owncloud.com>2014-08-11 13:28:53 +0200
commit173059f6d00faa06dab9188efb2d7536f15861e4 (patch)
tree2d3efee8b0e682b16d33fb8a37e7f5567b696800 /tests/lib/naturalsort.php
parent86ae3bd1e7d20e4f28ea9d7b9f71f1fdef0087aa (diff)
downloadnextcloud-server-173059f6d00faa06dab9188efb2d7536f15861e4.tar.gz
nextcloud-server-173059f6d00faa06dab9188efb2d7536f15861e4.zip
Fixed file list sorting
Now using a natural sort algorithm that is more consistent between JS and PHP (although not perfect in some corner cases) - added OC.Util.naturalSortComparator that uses the same algo that was used for the user list - changed user list and files list to use OC.Util.naturalSortComparator - removed toLowerCase() and changed the comparator to use String.localeCompare() - added unit tests - added OC_NaturalSort that is used by OCP\Util::naturalSortCompare()
Diffstat (limited to 'tests/lib/naturalsort.php')
-rw-r--r--tests/lib/naturalsort.php178
1 files changed, 178 insertions, 0 deletions
diff --git a/tests/lib/naturalsort.php b/tests/lib/naturalsort.php
new file mode 100644
index 00000000000..a880acaeec4
--- /dev/null
+++ b/tests/lib/naturalsort.php
@@ -0,0 +1,178 @@
+<?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.
+ */
+
+class Test_NaturalSort extends PHPUnit_Framework_TestCase {
+
+ public function setUp() {
+ if(!class_exists('Collator')) {
+ $this->markTestSkipped('The intl module is not available, natural sorting will not work as expected.');
+ return;
+ }
+ }
+
+ /**
+ * @dataProvider naturalSortDataProvider
+ */
+ public function testNaturalSortCompare($array, $sorted)
+ {
+ $comparator = \OC\NaturalSort::getInstance();
+ usort($array, array($comparator, 'compare'));
+ $this->assertEquals($sorted, $array);
+ }
+
+ /**
+ * Data provider for natural sort.
+ * Must provide the same result as in core/js/tests/specs/coreSpec.js
+ * @return array test cases
+ */
+ public function naturalSortDataProvider()
+ {
+ return array(
+ // different casing
+ array(
+ // unsorted
+ array(
+ 'aaa',
+ 'bbb',
+ 'BBB',
+ 'AAA'
+ ),
+ // sorted
+ array(
+ 'aaa',
+ 'AAA',
+ 'bbb',
+ 'BBB'
+ )
+ ),
+ // numbers
+ array(
+ // unsorted
+ array(
+ '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',
+ ),
+ // sorted
+ array(
+ '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',
+ )
+ ),
+ // chinese characters
+ array(
+ // unsorted
+ array(
+ '十.txt',
+ '一.txt',
+ '二.txt',
+ '十 2.txt',
+ '三.txt',
+ '四.txt',
+ 'abc.txt',
+ '五.txt',
+ '七.txt',
+ '八.txt',
+ '九.txt',
+ '六.txt',
+ '十一.txt',
+ '波.txt',
+ '破.txt',
+ '莫.txt',
+ '啊.txt',
+ '123.txt',
+ ),
+ // sorted
+ array(
+ '123.txt',
+ 'abc.txt',
+ '一.txt',
+ '七.txt',
+ '三.txt',
+ '九.txt',
+ '二.txt',
+ '五.txt',
+ '八.txt',
+ '六.txt',
+ '十.txt',
+ '十 2.txt',
+ '十一.txt',
+ '啊.txt',
+ '四.txt',
+ '波.txt',
+ '破.txt',
+ '莫.txt',
+ )
+ ),
+ // with umlauts
+ array(
+ // unsorted
+ array(
+ 'öh.txt',
+ 'Äh.txt',
+ 'oh.txt',
+ 'Üh 2.txt',
+ 'Üh.txt',
+ 'ah.txt',
+ 'Öh.txt',
+ 'uh.txt',
+ 'üh.txt',
+ 'äh.txt',
+ ),
+ // sorted
+ array(
+ 'ah.txt',
+ 'äh.txt',
+ 'Äh.txt',
+ 'oh.txt',
+ 'öh.txt',
+ 'Öh.txt',
+ 'uh.txt',
+ 'üh.txt',
+ 'Üh.txt',
+ 'Üh 2.txt',
+ )
+ ),
+ );
+ }
+}