NaturalSortTest.php 4.3 KB

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