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.

helper.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @author brumsel <brumsel@losecatcher.de>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Vincent Petry <pvince81@owncloud.com>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. use OCA\Files;
  26. /**
  27. * Class Test_Files_Helper
  28. */
  29. class Test_Files_Helper extends \Test\TestCase {
  30. private function makeFileInfo($name, $size, $mtime, $isDir = false) {
  31. return new \OC\Files\FileInfo(
  32. '/' . $name,
  33. null,
  34. '/',
  35. array(
  36. 'name' => $name,
  37. 'size' => $size,
  38. 'mtime' => $mtime,
  39. 'type' => $isDir ? 'dir' : 'file',
  40. 'mimetype' => $isDir ? 'httpd/unix-directory' : 'application/octet-stream'
  41. ),
  42. null
  43. );
  44. }
  45. /**
  46. * Returns a file list for testing
  47. */
  48. private function getTestFileList() {
  49. return array(
  50. self::makeFileInfo('a.txt', 4, 2.3 * pow(10, 9)),
  51. self::makeFileInfo('q.txt', 5, 150),
  52. self::makeFileInfo('subdir2', 87, 128, true),
  53. self::makeFileInfo('b.txt', 2.2 * pow(10, 9), 800),
  54. self::makeFileInfo('o.txt', 12, 100),
  55. self::makeFileInfo('subdir', 88, 125, true),
  56. );
  57. }
  58. function sortDataProvider() {
  59. return array(
  60. array(
  61. 'name',
  62. false,
  63. array('subdir', 'subdir2', 'a.txt', 'b.txt', 'o.txt', 'q.txt'),
  64. ),
  65. array(
  66. 'name',
  67. true,
  68. array('q.txt', 'o.txt', 'b.txt', 'a.txt', 'subdir2', 'subdir'),
  69. ),
  70. array(
  71. 'size',
  72. false,
  73. array('a.txt', 'q.txt', 'o.txt', 'subdir2', 'subdir', 'b.txt'),
  74. ),
  75. array(
  76. 'size',
  77. true,
  78. array('b.txt', 'subdir', 'subdir2', 'o.txt', 'q.txt', 'a.txt'),
  79. ),
  80. array(
  81. 'mtime',
  82. false,
  83. array('o.txt', 'subdir', 'subdir2', 'q.txt', 'b.txt', 'a.txt'),
  84. ),
  85. array(
  86. 'mtime',
  87. true,
  88. array('a.txt', 'b.txt', 'q.txt', 'subdir2', 'subdir', 'o.txt'),
  89. ),
  90. );
  91. }
  92. /**
  93. * @dataProvider sortDataProvider
  94. */
  95. public function testSortByName($sort, $sortDescending, $expectedOrder) {
  96. $files = self::getTestFileList();
  97. $files = \OCA\Files\Helper::sortFiles($files, $sort, $sortDescending);
  98. $fileNames = array();
  99. foreach ($files as $fileInfo) {
  100. $fileNames[] = $fileInfo->getName();
  101. }
  102. $this->assertEquals(
  103. $expectedOrder,
  104. $fileNames
  105. );
  106. }
  107. }