1
0

HelperTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author brumsel <brumsel@losecatcher.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Vincent Petry <pvince81@owncloud.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. use OCA\Files\Tests;
  28. /**
  29. * Class Helper
  30. */
  31. class HelperTest extends \Test\TestCase {
  32. private function makeFileInfo($name, $size, $mtime, $isDir = false) {
  33. return new \OC\Files\FileInfo(
  34. '/' . $name,
  35. null,
  36. '/',
  37. array(
  38. 'name' => $name,
  39. 'size' => $size,
  40. 'mtime' => $mtime,
  41. 'type' => $isDir ? 'dir' : 'file',
  42. 'mimetype' => $isDir ? 'httpd/unix-directory' : 'application/octet-stream'
  43. ),
  44. null
  45. );
  46. }
  47. /**
  48. * Returns a file list for testing
  49. */
  50. private function getTestFileList() {
  51. return array(
  52. self::makeFileInfo('a.txt', 4, 2.3 * pow(10, 9)),
  53. self::makeFileInfo('q.txt', 5, 150),
  54. self::makeFileInfo('subdir2', 87, 128, true),
  55. self::makeFileInfo('b.txt', 2.2 * pow(10, 9), 800),
  56. self::makeFileInfo('o.txt', 12, 100),
  57. self::makeFileInfo('subdir', 88, 125, true),
  58. );
  59. }
  60. function sortDataProvider() {
  61. return array(
  62. array(
  63. 'name',
  64. false,
  65. array('subdir', 'subdir2', 'a.txt', 'b.txt', 'o.txt', 'q.txt'),
  66. ),
  67. array(
  68. 'name',
  69. true,
  70. array('q.txt', 'o.txt', 'b.txt', 'a.txt', 'subdir2', 'subdir'),
  71. ),
  72. array(
  73. 'size',
  74. false,
  75. array('a.txt', 'q.txt', 'o.txt', 'subdir2', 'subdir', 'b.txt'),
  76. ),
  77. array(
  78. 'size',
  79. true,
  80. array('b.txt', 'subdir', 'subdir2', 'o.txt', 'q.txt', 'a.txt'),
  81. ),
  82. array(
  83. 'mtime',
  84. false,
  85. array('o.txt', 'subdir', 'subdir2', 'q.txt', 'b.txt', 'a.txt'),
  86. ),
  87. array(
  88. 'mtime',
  89. true,
  90. array('a.txt', 'b.txt', 'q.txt', 'subdir2', 'subdir', 'o.txt'),
  91. ),
  92. );
  93. }
  94. /**
  95. * @dataProvider sortDataProvider
  96. */
  97. public function testSortByName($sort, $sortDescending, $expectedOrder) {
  98. $files = self::getTestFileList();
  99. $files = \OCA\Files\Helper::sortFiles($files, $sort, $sortDescending);
  100. $fileNames = array();
  101. foreach ($files as $fileInfo) {
  102. $fileNames[] = $fileInfo->getName();
  103. }
  104. $this->assertEquals(
  105. $expectedOrder,
  106. $fileNames
  107. );
  108. }
  109. public function testPopulateTags() {
  110. $tagManager = $this->createMock(\OCP\ITagManager::class);
  111. $tagger = $this->createMock(\OCP\ITags::class);
  112. $tagManager->method('load')
  113. ->with('files')
  114. ->willReturn($tagger);
  115. $data = [
  116. ['id' => 10],
  117. ['id' => 22, 'foo' => 'bar'],
  118. ['id' => 42, 'x' => 'y'],
  119. ];
  120. $tags = [
  121. 10 => ['tag3'],
  122. 42 => ['tag1', 'tag2'],
  123. ];
  124. $tagger->method('getTagsForObjects')
  125. ->with([10, 22, 42])
  126. ->willReturn($tags);
  127. $result = \OCA\Files\Helper::populateTags($data, 'id', $tagManager);
  128. $this->assertSame([
  129. ['id' => 10, 'tags' => ['tag3']],
  130. ['id' => 22, 'foo' => 'bar', 'tags' => []],
  131. ['id' => 42, 'x' => 'y', 'tags' => ['tag1', 'tag2']],
  132. ], $result);
  133. }
  134. }