NaturalSortTest.php 4.4 KB

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