1
0

NaturalSortTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test;
  8. class NaturalSortTest extends \Test\TestCase {
  9. /**
  10. * @dataProvider naturalSortDataProvider
  11. */
  12. public function testNaturalSortCompare($array, $sorted): void {
  13. if (!class_exists('Collator')) {
  14. $this->markTestSkipped('The intl module is not available, natural sorting might not work as expected.');
  15. return;
  16. }
  17. $comparator = \OC\NaturalSort::getInstance();
  18. usort($array, [$comparator, 'compare']);
  19. $this->assertEquals($sorted, $array);
  20. }
  21. /**
  22. * @dataProvider defaultCollatorDataProvider
  23. */
  24. public function testDefaultCollatorCompare($array, $sorted): void {
  25. $comparator = new \OC\NaturalSort(new \OC\NaturalSort_DefaultCollator());
  26. usort($array, [$comparator, 'compare']);
  27. $this->assertEquals($sorted, $array);
  28. }
  29. /**
  30. * Data provider for natural sorting with php5-intl's Collator.
  31. * Must provide the same result as in core/js/tests/specs/coreSpec.js
  32. * @return array test cases
  33. */
  34. public function naturalSortDataProvider() {
  35. return [
  36. // different casing
  37. [
  38. // unsorted
  39. [
  40. 'aaa',
  41. 'bbb',
  42. 'BBB',
  43. 'AAA'
  44. ],
  45. // sorted
  46. [
  47. 'aaa',
  48. 'AAA',
  49. 'bbb',
  50. 'BBB'
  51. ]
  52. ],
  53. // numbers
  54. [
  55. // unsorted
  56. [
  57. '124.txt',
  58. 'abc1',
  59. '123.txt',
  60. 'abc',
  61. 'abc2',
  62. 'def (2).txt',
  63. 'ghi 10.txt',
  64. 'abc12',
  65. 'def.txt',
  66. 'def (1).txt',
  67. 'ghi 2.txt',
  68. 'def (10).txt',
  69. 'abc10',
  70. 'def (12).txt',
  71. 'z',
  72. 'ghi.txt',
  73. 'za',
  74. 'ghi 1.txt',
  75. 'ghi 12.txt',
  76. 'zz',
  77. '15.txt',
  78. '15b.txt',
  79. ],
  80. // sorted
  81. [
  82. '15.txt',
  83. '15b.txt',
  84. '123.txt',
  85. '124.txt',
  86. 'abc',
  87. 'abc1',
  88. 'abc2',
  89. 'abc10',
  90. 'abc12',
  91. 'def.txt',
  92. 'def (1).txt',
  93. 'def (2).txt',
  94. 'def (10).txt',
  95. 'def (12).txt',
  96. 'ghi.txt',
  97. 'ghi 1.txt',
  98. 'ghi 2.txt',
  99. 'ghi 10.txt',
  100. 'ghi 12.txt',
  101. 'z',
  102. 'za',
  103. 'zz',
  104. ]
  105. ],
  106. // chinese characters
  107. [
  108. // unsorted
  109. [
  110. '十.txt',
  111. '一.txt',
  112. '二.txt',
  113. '十 2.txt',
  114. '三.txt',
  115. '四.txt',
  116. 'abc.txt',
  117. '五.txt',
  118. '七.txt',
  119. '八.txt',
  120. '九.txt',
  121. '六.txt',
  122. '十一.txt',
  123. '波.txt',
  124. '破.txt',
  125. '莫.txt',
  126. '啊.txt',
  127. '123.txt',
  128. ],
  129. // sorted
  130. [
  131. '123.txt',
  132. 'abc.txt',
  133. '一.txt',
  134. '七.txt',
  135. '三.txt',
  136. '九.txt',
  137. '二.txt',
  138. '五.txt',
  139. '八.txt',
  140. '六.txt',
  141. '十.txt',
  142. '十 2.txt',
  143. '十一.txt',
  144. '啊.txt',
  145. '四.txt',
  146. '波.txt',
  147. '破.txt',
  148. '莫.txt',
  149. ]
  150. ],
  151. // with umlauts
  152. [
  153. // unsorted
  154. [
  155. 'öh.txt',
  156. 'Äh.txt',
  157. 'oh.txt',
  158. 'Üh 2.txt',
  159. 'Üh.txt',
  160. 'ah.txt',
  161. 'Öh.txt',
  162. 'uh.txt',
  163. 'üh.txt',
  164. 'äh.txt',
  165. ],
  166. // sorted
  167. [
  168. 'ah.txt',
  169. 'äh.txt',
  170. 'Äh.txt',
  171. 'oh.txt',
  172. 'öh.txt',
  173. 'Öh.txt',
  174. 'uh.txt',
  175. 'üh.txt',
  176. 'Üh.txt',
  177. 'Üh 2.txt',
  178. ]
  179. ],
  180. ];
  181. }
  182. /**
  183. * Data provider for natural sorting with \OC\NaturalSort_DefaultCollator.
  184. * Must provide the same result as in core/js/tests/specs/coreSpec.js
  185. * @return array test cases
  186. */
  187. public function defaultCollatorDataProvider() {
  188. return [
  189. // different casing
  190. [
  191. // unsorted
  192. [
  193. 'aaa',
  194. 'bbb',
  195. 'BBB',
  196. 'AAA'
  197. ],
  198. // sorted
  199. [
  200. 'aaa',
  201. 'AAA',
  202. 'bbb',
  203. 'BBB'
  204. ]
  205. ],
  206. // numbers
  207. [
  208. // unsorted
  209. [
  210. '124.txt',
  211. 'abc1',
  212. '123.txt',
  213. 'abc',
  214. 'abc2',
  215. 'def (2).txt',
  216. 'ghi 10.txt',
  217. 'abc12',
  218. 'def.txt',
  219. 'def (1).txt',
  220. 'ghi 2.txt',
  221. 'def (10).txt',
  222. 'abc10',
  223. 'def (12).txt',
  224. 'z',
  225. 'ghi.txt',
  226. 'za',
  227. 'ghi 1.txt',
  228. 'ghi 12.txt',
  229. 'zz',
  230. '15.txt',
  231. '15b.txt',
  232. ],
  233. // sorted
  234. [
  235. '15.txt',
  236. '15b.txt',
  237. '123.txt',
  238. '124.txt',
  239. 'abc',
  240. 'abc1',
  241. 'abc2',
  242. 'abc10',
  243. 'abc12',
  244. 'def.txt',
  245. 'def (1).txt',
  246. 'def (2).txt',
  247. 'def (10).txt',
  248. 'def (12).txt',
  249. 'ghi.txt',
  250. 'ghi 1.txt',
  251. 'ghi 2.txt',
  252. 'ghi 10.txt',
  253. 'ghi 12.txt',
  254. 'z',
  255. 'za',
  256. 'zz',
  257. ]
  258. ],
  259. ];
  260. }
  261. }