2014-04-11 15:00:39 +00:00
|
|
|
<?php
|
2024-05-28 14:42:42 +00:00
|
|
|
|
2024-10-10 10:40:31 +00:00
|
|
|
use OC\Files\FileInfo;
|
|
|
|
use OCA\Files\Helper;
|
|
|
|
use OCP\ITagManager;
|
|
|
|
use OCP\ITags;
|
|
|
|
|
2014-04-11 15:00:39 +00:00
|
|
|
/**
|
2024-05-28 14:42:42 +00:00
|
|
|
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-04-11 15:00:39 +00:00
|
|
|
*/
|
2016-05-12 10:07:06 +00:00
|
|
|
class HelperTest extends \Test\TestCase {
|
2014-04-11 15:00:39 +00:00
|
|
|
private function makeFileInfo($name, $size, $mtime, $isDir = false) {
|
2024-10-10 10:40:31 +00:00
|
|
|
return new FileInfo(
|
2014-06-03 08:48:03 +00:00
|
|
|
'/' . $name,
|
2014-04-11 15:00:39 +00:00
|
|
|
null,
|
|
|
|
'/',
|
2020-03-26 08:30:18 +00:00
|
|
|
[
|
2014-04-11 15:00:39 +00:00
|
|
|
'name' => $name,
|
|
|
|
'size' => $size,
|
|
|
|
'mtime' => $mtime,
|
|
|
|
'type' => $isDir ? 'dir' : 'file',
|
|
|
|
'mimetype' => $isDir ? 'httpd/unix-directory' : 'application/octet-stream'
|
2020-03-26 08:30:18 +00:00
|
|
|
],
|
2014-12-16 13:24:48 +00:00
|
|
|
null
|
2014-04-11 15:00:39 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a file list for testing
|
|
|
|
*/
|
|
|
|
private function getTestFileList() {
|
2020-03-26 08:30:18 +00:00
|
|
|
return [
|
2014-12-03 22:45:57 +00:00
|
|
|
self::makeFileInfo('a.txt', 4, 2.3 * pow(10, 9)),
|
2014-04-11 15:00:39 +00:00
|
|
|
self::makeFileInfo('q.txt', 5, 150),
|
|
|
|
self::makeFileInfo('subdir2', 87, 128, true),
|
2014-12-03 22:45:57 +00:00
|
|
|
self::makeFileInfo('b.txt', 2.2 * pow(10, 9), 800),
|
2014-04-11 15:00:39 +00:00
|
|
|
self::makeFileInfo('o.txt', 12, 100),
|
|
|
|
self::makeFileInfo('subdir', 88, 125, true),
|
2020-03-26 08:30:18 +00:00
|
|
|
];
|
2014-04-11 15:00:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 14:51:06 +00:00
|
|
|
public function sortDataProvider() {
|
2020-03-26 08:30:18 +00:00
|
|
|
return [
|
|
|
|
[
|
2014-04-11 15:00:39 +00:00
|
|
|
'name',
|
|
|
|
false,
|
2020-03-26 08:30:18 +00:00
|
|
|
['subdir', 'subdir2', 'a.txt', 'b.txt', 'o.txt', 'q.txt'],
|
|
|
|
],
|
|
|
|
[
|
2014-04-11 15:00:39 +00:00
|
|
|
'name',
|
|
|
|
true,
|
2020-03-26 08:30:18 +00:00
|
|
|
['q.txt', 'o.txt', 'b.txt', 'a.txt', 'subdir2', 'subdir'],
|
|
|
|
],
|
|
|
|
[
|
2014-04-11 15:00:39 +00:00
|
|
|
'size',
|
|
|
|
false,
|
2020-03-26 08:30:18 +00:00
|
|
|
['a.txt', 'q.txt', 'o.txt', 'subdir2', 'subdir', 'b.txt'],
|
|
|
|
],
|
|
|
|
[
|
2014-04-11 15:00:39 +00:00
|
|
|
'size',
|
|
|
|
true,
|
2020-03-26 08:30:18 +00:00
|
|
|
['b.txt', 'subdir', 'subdir2', 'o.txt', 'q.txt', 'a.txt'],
|
|
|
|
],
|
|
|
|
[
|
2014-04-11 15:00:39 +00:00
|
|
|
'mtime',
|
|
|
|
false,
|
2020-03-26 08:30:18 +00:00
|
|
|
['o.txt', 'subdir', 'subdir2', 'q.txt', 'b.txt', 'a.txt'],
|
|
|
|
],
|
|
|
|
[
|
2014-04-11 15:00:39 +00:00
|
|
|
'mtime',
|
|
|
|
true,
|
2020-03-26 08:30:18 +00:00
|
|
|
['a.txt', 'b.txt', 'q.txt', 'subdir2', 'subdir', 'o.txt'],
|
|
|
|
],
|
|
|
|
];
|
2014-04-11 15:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider sortDataProvider
|
|
|
|
*/
|
2023-01-23 17:23:52 +00:00
|
|
|
public function testSortByName(string $sort, bool $sortDescending, array $expectedOrder): void {
|
|
|
|
if (($sort === 'mtime') && (PHP_INT_SIZE < 8)) {
|
2023-01-24 10:40:33 +00:00
|
|
|
$this->markTestSkipped('Skip mtime sorting on 32bit');
|
2023-01-23 17:23:52 +00:00
|
|
|
}
|
2014-04-11 15:00:39 +00:00
|
|
|
$files = self::getTestFileList();
|
2024-10-10 10:40:31 +00:00
|
|
|
$files = Helper::sortFiles($files, $sort, $sortDescending);
|
2020-03-26 08:30:18 +00:00
|
|
|
$fileNames = [];
|
2014-04-11 15:00:39 +00:00
|
|
|
foreach ($files as $fileInfo) {
|
|
|
|
$fileNames[] = $fileInfo->getName();
|
|
|
|
}
|
|
|
|
$this->assertEquals(
|
|
|
|
$expectedOrder,
|
|
|
|
$fileNames
|
2014-11-07 14:23:15 +00:00
|
|
|
);
|
2014-04-11 15:00:39 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 16:46:47 +00:00
|
|
|
public function testPopulateTags(): void {
|
2024-10-10 10:40:31 +00:00
|
|
|
$tagManager = $this->createMock(ITagManager::class);
|
|
|
|
$tagger = $this->createMock(ITags::class);
|
2017-08-22 16:46:47 +00:00
|
|
|
|
|
|
|
$tagManager->method('load')
|
|
|
|
->with('files')
|
|
|
|
->willReturn($tagger);
|
|
|
|
|
|
|
|
$data = [
|
2024-09-24 13:53:13 +00:00
|
|
|
['file_source' => 10],
|
|
|
|
['file_source' => 22, 'foo' => 'bar'],
|
|
|
|
['file_source' => 42, 'x' => 'y'],
|
2017-08-22 16:46:47 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
$tags = [
|
|
|
|
10 => ['tag3'],
|
|
|
|
42 => ['tag1', 'tag2'],
|
|
|
|
];
|
|
|
|
|
|
|
|
$tagger->method('getTagsForObjects')
|
|
|
|
->with([10, 22, 42])
|
|
|
|
->willReturn($tags);
|
|
|
|
|
2024-09-24 13:53:13 +00:00
|
|
|
$result = Helper::populateTags($data, $tagManager);
|
2017-08-22 16:46:47 +00:00
|
|
|
|
|
|
|
$this->assertSame([
|
2024-09-24 13:53:13 +00:00
|
|
|
['file_source' => 10, 'tags' => ['tag3']],
|
|
|
|
['file_source' => 22, 'foo' => 'bar', 'tags' => []],
|
|
|
|
['file_source' => 42, 'x' => 'y', 'tags' => ['tag1', 'tag2']],
|
2017-08-22 16:46:47 +00:00
|
|
|
], $result);
|
|
|
|
}
|
2014-04-11 15:00:39 +00:00
|
|
|
}
|