HelperTest.php 2.9 KB

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