1
0

HelperTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author brumsel <brumsel@losecatcher.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Vincent Petry <vincent@nextcloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. class HelperTest extends \Test\TestCase {
  28. private function makeFileInfo($name, $size, $mtime, $isDir = false) {
  29. return new \OC\Files\FileInfo(
  30. '/' . $name,
  31. null,
  32. '/',
  33. [
  34. 'name' => $name,
  35. 'size' => $size,
  36. 'mtime' => $mtime,
  37. 'type' => $isDir ? 'dir' : 'file',
  38. 'mimetype' => $isDir ? 'httpd/unix-directory' : 'application/octet-stream'
  39. ],
  40. null
  41. );
  42. }
  43. /**
  44. * Returns a file list for testing
  45. */
  46. private function getTestFileList() {
  47. return [
  48. self::makeFileInfo('a.txt', 4, 2.3 * pow(10, 9)),
  49. self::makeFileInfo('q.txt', 5, 150),
  50. self::makeFileInfo('subdir2', 87, 128, true),
  51. self::makeFileInfo('b.txt', 2.2 * pow(10, 9), 800),
  52. self::makeFileInfo('o.txt', 12, 100),
  53. self::makeFileInfo('subdir', 88, 125, true),
  54. ];
  55. }
  56. public function sortDataProvider() {
  57. return [
  58. [
  59. 'name',
  60. false,
  61. ['subdir', 'subdir2', 'a.txt', 'b.txt', 'o.txt', 'q.txt'],
  62. ],
  63. [
  64. 'name',
  65. true,
  66. ['q.txt', 'o.txt', 'b.txt', 'a.txt', 'subdir2', 'subdir'],
  67. ],
  68. [
  69. 'size',
  70. false,
  71. ['a.txt', 'q.txt', 'o.txt', 'subdir2', 'subdir', 'b.txt'],
  72. ],
  73. [
  74. 'size',
  75. true,
  76. ['b.txt', 'subdir', 'subdir2', 'o.txt', 'q.txt', 'a.txt'],
  77. ],
  78. [
  79. 'mtime',
  80. false,
  81. ['o.txt', 'subdir', 'subdir2', 'q.txt', 'b.txt', 'a.txt'],
  82. ],
  83. [
  84. 'mtime',
  85. true,
  86. ['a.txt', 'b.txt', 'q.txt', 'subdir2', 'subdir', 'o.txt'],
  87. ],
  88. ];
  89. }
  90. /**
  91. * @dataProvider sortDataProvider
  92. */
  93. public function testSortByName(string $sort, bool $sortDescending, array $expectedOrder) {
  94. if (($sort === 'mtime') && (PHP_INT_SIZE < 8)) {
  95. $this->markTestSkipped('Skip mtime sorting on 32bit');
  96. }
  97. $files = self::getTestFileList();
  98. $files = \OCA\Files\Helper::sortFiles($files, $sort, $sortDescending);
  99. $fileNames = [];
  100. foreach ($files as $fileInfo) {
  101. $fileNames[] = $fileInfo->getName();
  102. }
  103. $this->assertEquals(
  104. $expectedOrder,
  105. $fileNames
  106. );
  107. }
  108. public function testPopulateTags() {
  109. $tagManager = $this->createMock(\OCP\ITagManager::class);
  110. $tagger = $this->createMock(\OCP\ITags::class);
  111. $tagManager->method('load')
  112. ->with('files')
  113. ->willReturn($tagger);
  114. $data = [
  115. ['id' => 10],
  116. ['id' => 22, 'foo' => 'bar'],
  117. ['id' => 42, 'x' => 'y'],
  118. ];
  119. $tags = [
  120. 10 => ['tag3'],
  121. 42 => ['tag1', 'tag2'],
  122. ];
  123. $tagger->method('getTagsForObjects')
  124. ->with([10, 22, 42])
  125. ->willReturn($tags);
  126. $result = \OCA\Files\Helper::populateTags($data, 'id', $tagManager);
  127. $this->assertSame([
  128. ['id' => 10, 'tags' => ['tag3']],
  129. ['id' => 22, 'foo' => 'bar', 'tags' => []],
  130. ['id' => 42, 'x' => 'y', 'tags' => ['tag1', 'tag2']],
  131. ], $result);
  132. }
  133. }