1
0

HelperTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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($sort, $sortDescending, $expectedOrder) {
  94. $files = self::getTestFileList();
  95. $files = \OCA\Files\Helper::sortFiles($files, $sort, $sortDescending);
  96. $fileNames = [];
  97. foreach ($files as $fileInfo) {
  98. $fileNames[] = $fileInfo->getName();
  99. }
  100. $this->assertEquals(
  101. $expectedOrder,
  102. $fileNames
  103. );
  104. }
  105. public function testPopulateTags() {
  106. $tagManager = $this->createMock(\OCP\ITagManager::class);
  107. $tagger = $this->createMock(\OCP\ITags::class);
  108. $tagManager->method('load')
  109. ->with('files')
  110. ->willReturn($tagger);
  111. $data = [
  112. ['id' => 10],
  113. ['id' => 22, 'foo' => 'bar'],
  114. ['id' => 42, 'x' => 'y'],
  115. ];
  116. $tags = [
  117. 10 => ['tag3'],
  118. 42 => ['tag1', 'tag2'],
  119. ];
  120. $tagger->method('getTagsForObjects')
  121. ->with([10, 22, 42])
  122. ->willReturn($tags);
  123. $result = \OCA\Files\Helper::populateTags($data, 'id', $tagManager);
  124. $this->assertSame([
  125. ['id' => 10, 'tags' => ['tag3']],
  126. ['id' => 22, 'foo' => 'bar', 'tags' => []],
  127. ['id' => 42, 'x' => 'y', 'tags' => ['tag1', 'tag2']],
  128. ], $result);
  129. }
  130. }