You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

naturalsort.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Vincent Petry <PVince81@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class Test_NaturalSort extends \Test\TestCase {
  9. public function setUp() {
  10. parent::setUp();
  11. if(!class_exists('Collator')) {
  12. $this->markTestSkipped('The intl module is not available, natural sorting will not work as expected.');
  13. return;
  14. }
  15. }
  16. /**
  17. * @dataProvider naturalSortDataProvider
  18. */
  19. public function testNaturalSortCompare($array, $sorted)
  20. {
  21. $comparator = \OC\NaturalSort::getInstance();
  22. usort($array, array($comparator, 'compare'));
  23. $this->assertEquals($sorted, $array);
  24. }
  25. /**
  26. * Data provider for natural sort.
  27. * Must provide the same result as in core/js/tests/specs/coreSpec.js
  28. * @return array test cases
  29. */
  30. public function naturalSortDataProvider()
  31. {
  32. return array(
  33. // different casing
  34. array(
  35. // unsorted
  36. array(
  37. 'aaa',
  38. 'bbb',
  39. 'BBB',
  40. 'AAA'
  41. ),
  42. // sorted
  43. array(
  44. 'aaa',
  45. 'AAA',
  46. 'bbb',
  47. 'BBB'
  48. )
  49. ),
  50. // numbers
  51. array(
  52. // unsorted
  53. array(
  54. '124.txt',
  55. 'abc1',
  56. '123.txt',
  57. 'abc',
  58. 'abc2',
  59. 'def (2).txt',
  60. 'ghi 10.txt',
  61. 'abc12',
  62. 'def.txt',
  63. 'def (1).txt',
  64. 'ghi 2.txt',
  65. 'def (10).txt',
  66. 'abc10',
  67. 'def (12).txt',
  68. 'z',
  69. 'ghi.txt',
  70. 'za',
  71. 'ghi 1.txt',
  72. 'ghi 12.txt',
  73. 'zz',
  74. '15.txt',
  75. '15b.txt',
  76. ),
  77. // sorted
  78. array(
  79. '15.txt',
  80. '15b.txt',
  81. '123.txt',
  82. '124.txt',
  83. 'abc',
  84. 'abc1',
  85. 'abc2',
  86. 'abc10',
  87. 'abc12',
  88. 'def.txt',
  89. 'def (1).txt',
  90. 'def (2).txt',
  91. 'def (10).txt',
  92. 'def (12).txt',
  93. 'ghi.txt',
  94. 'ghi 1.txt',
  95. 'ghi 2.txt',
  96. 'ghi 10.txt',
  97. 'ghi 12.txt',
  98. 'z',
  99. 'za',
  100. 'zz',
  101. )
  102. ),
  103. // chinese characters
  104. array(
  105. // unsorted
  106. array(
  107. '十.txt',
  108. '一.txt',
  109. '二.txt',
  110. '十 2.txt',
  111. '三.txt',
  112. '四.txt',
  113. 'abc.txt',
  114. '五.txt',
  115. '七.txt',
  116. '八.txt',
  117. '九.txt',
  118. '六.txt',
  119. '十一.txt',
  120. '波.txt',
  121. '破.txt',
  122. '莫.txt',
  123. '啊.txt',
  124. '123.txt',
  125. ),
  126. // sorted
  127. array(
  128. '123.txt',
  129. 'abc.txt',
  130. '一.txt',
  131. '七.txt',
  132. '三.txt',
  133. '九.txt',
  134. '二.txt',
  135. '五.txt',
  136. '八.txt',
  137. '六.txt',
  138. '十.txt',
  139. '十 2.txt',
  140. '十一.txt',
  141. '啊.txt',
  142. '四.txt',
  143. '波.txt',
  144. '破.txt',
  145. '莫.txt',
  146. )
  147. ),
  148. // with umlauts
  149. array(
  150. // unsorted
  151. array(
  152. 'öh.txt',
  153. 'Äh.txt',
  154. 'oh.txt',
  155. 'Üh 2.txt',
  156. 'Üh.txt',
  157. 'ah.txt',
  158. 'Öh.txt',
  159. 'uh.txt',
  160. 'üh.txt',
  161. 'äh.txt',
  162. ),
  163. // sorted
  164. array(
  165. 'ah.txt',
  166. 'äh.txt',
  167. 'Äh.txt',
  168. 'oh.txt',
  169. 'öh.txt',
  170. 'Öh.txt',
  171. 'uh.txt',
  172. 'üh.txt',
  173. 'Üh.txt',
  174. 'Üh 2.txt',
  175. )
  176. ),
  177. );
  178. }
  179. }