HelperTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. class HelperTest extends \Test\TestCase {
  8. private function makeFileInfo($name, $size, $mtime, $isDir = false) {
  9. return new \OC\Files\FileInfo(
  10. '/' . $name,
  11. null,
  12. '/',
  13. [
  14. 'name' => $name,
  15. 'size' => $size,
  16. 'mtime' => $mtime,
  17. 'type' => $isDir ? 'dir' : 'file',
  18. 'mimetype' => $isDir ? 'httpd/unix-directory' : 'application/octet-stream'
  19. ],
  20. null
  21. );
  22. }
  23. /**
  24. * Returns a file list for testing
  25. */
  26. private function getTestFileList() {
  27. return [
  28. self::makeFileInfo('a.txt', 4, 2.3 * pow(10, 9)),
  29. self::makeFileInfo('q.txt', 5, 150),
  30. self::makeFileInfo('subdir2', 87, 128, true),
  31. self::makeFileInfo('b.txt', 2.2 * pow(10, 9), 800),
  32. self::makeFileInfo('o.txt', 12, 100),
  33. self::makeFileInfo('subdir', 88, 125, true),
  34. ];
  35. }
  36. public function sortDataProvider() {
  37. return [
  38. [
  39. 'name',
  40. false,
  41. ['subdir', 'subdir2', 'a.txt', 'b.txt', 'o.txt', 'q.txt'],
  42. ],
  43. [
  44. 'name',
  45. true,
  46. ['q.txt', 'o.txt', 'b.txt', 'a.txt', 'subdir2', 'subdir'],
  47. ],
  48. [
  49. 'size',
  50. false,
  51. ['a.txt', 'q.txt', 'o.txt', 'subdir2', 'subdir', 'b.txt'],
  52. ],
  53. [
  54. 'size',
  55. true,
  56. ['b.txt', 'subdir', 'subdir2', 'o.txt', 'q.txt', 'a.txt'],
  57. ],
  58. [
  59. 'mtime',
  60. false,
  61. ['o.txt', 'subdir', 'subdir2', 'q.txt', 'b.txt', 'a.txt'],
  62. ],
  63. [
  64. 'mtime',
  65. true,
  66. ['a.txt', 'b.txt', 'q.txt', 'subdir2', 'subdir', 'o.txt'],
  67. ],
  68. ];
  69. }
  70. /**
  71. * @dataProvider sortDataProvider
  72. */
  73. public function testSortByName(string $sort, bool $sortDescending, array $expectedOrder): void {
  74. if (($sort === 'mtime') && (PHP_INT_SIZE < 8)) {
  75. $this->markTestSkipped('Skip mtime sorting on 32bit');
  76. }
  77. $files = self::getTestFileList();
  78. $files = \OCA\Files\Helper::sortFiles($files, $sort, $sortDescending);
  79. $fileNames = [];
  80. foreach ($files as $fileInfo) {
  81. $fileNames[] = $fileInfo->getName();
  82. }
  83. $this->assertEquals(
  84. $expectedOrder,
  85. $fileNames
  86. );
  87. }
  88. public function testPopulateTags(): void {
  89. $tagManager = $this->createMock(\OCP\ITagManager::class);
  90. $tagger = $this->createMock(\OCP\ITags::class);
  91. $tagManager->method('load')
  92. ->with('files')
  93. ->willReturn($tagger);
  94. $data = [
  95. ['id' => 10],
  96. ['id' => 22, 'foo' => 'bar'],
  97. ['id' => 42, 'x' => 'y'],
  98. ];
  99. $tags = [
  100. 10 => ['tag3'],
  101. 42 => ['tag1', 'tag2'],
  102. ];
  103. $tagger->method('getTagsForObjects')
  104. ->with([10, 22, 42])
  105. ->willReturn($tags);
  106. $result = \OCA\Files\Helper::populateTags($data, 'id', $tagManager);
  107. $this->assertSame([
  108. ['id' => 10, 'tags' => ['tag3']],
  109. ['id' => 22, 'foo' => 'bar', 'tags' => []],
  110. ['id' => 42, 'x' => 'y', 'tags' => ['tag1', 'tag2']],
  111. ], $result);
  112. }
  113. }