123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <?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 \Test\TestCase {
- /**
- * @dataProvider naturalSortDataProvider
- */
- public function testNaturalSortCompare($array, $sorted)
- {
- if(!class_exists('Collator')) {
- $this->markTestSkipped('The intl module is not available, natural sorting might not work as expected.');
- return;
- }
- $comparator = \OC\NaturalSort::getInstance();
- usort($array, array($comparator, 'compare'));
- $this->assertEquals($sorted, $array);
- }
- /**
- * @dataProvider defaultCollatorDataProvider
- */
- public function testDefaultCollatorCompare($array, $sorted)
- {
- $comparator = new \OC\NaturalSort(new \OC\NaturalSort_DefaultCollator());
- usort($array, array($comparator, 'compare'));
- $this->assertEquals($sorted, $array);
- }
- /**
- * Data provider for natural sorting with php5-intl's Collator.
- * 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',
- '15.txt',
- '15b.txt',
- ),
- // sorted
- array(
- '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',
- )
- ),
- // 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',
- )
- ),
- );
- }
- /**
- * 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
- */
- public function defaultCollatorDataProvider()
- {
- 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',
- '15.txt',
- '15b.txt',
- ),
- // sorted
- array(
- '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',
- )
- ),
- );
- }
- }
|