diff options
author | Morris Jobke <hey@morrisjobke.de> | 2017-09-04 08:46:09 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-04 08:46:09 +0200 |
commit | d5cba5eeaa54e208a0e62b0aa460033717c35568 (patch) | |
tree | ab00b26f7d3207f622b25341aad7c79481209805 /apps/files | |
parent | ad5a2985701d77f9fa2c69711c830733e3f8bf73 (diff) | |
parent | eea7de4c9fc57e1a5c3e8c2f3b94200e7415cd10 (diff) | |
download | nextcloud-server-d5cba5eeaa54e208a0e62b0aa460033717c35568.tar.gz nextcloud-server-d5cba5eeaa54e208a0e62b0aa460033717c35568.zip |
Merge pull request #6221 from nextcloud/fav_share_api_fix
Correctly format OCS response with favorites
Diffstat (limited to 'apps/files')
-rw-r--r-- | apps/files/lib/Helper.php | 34 | ||||
-rw-r--r-- | apps/files/tests/HelperTest.php | 31 |
2 files changed, 48 insertions, 17 deletions
diff --git a/apps/files/lib/Helper.php b/apps/files/lib/Helper.php index 4a5595999d2..3eddbbaebf8 100644 --- a/apps/files/lib/Helper.php +++ b/apps/files/lib/Helper.php @@ -32,6 +32,7 @@ namespace OCA\Files; use OCP\Files\FileInfo; +use OCP\ITagManager; /** * Helper class for manipulating file information @@ -206,40 +207,39 @@ class Helper { * * @param array $fileList * @param string $fileIdentifier identifier attribute name for values in $fileList + * @param ITagManager $tagManager * @return array file list populated with tags */ - public static function populateTags(array $fileList, $fileIdentifier = 'fileid') { - $filesById = []; + public static function populateTags(array $fileList, $fileIdentifier = 'fileid', ITagManager $tagManager) { + $ids = []; foreach ($fileList as $fileData) { - $filesById[$fileData[$fileIdentifier]] = $fileData; + $ids[] = $fileData[$fileIdentifier]; } - $tagger = \OC::$server->getTagManager()->load('files'); - $tags = $tagger->getTagsForObjects(array_keys($filesById)); + $tagger = $tagManager->load('files'); + $tags = $tagger->getTagsForObjects($ids); if (!is_array($tags)) { throw new \UnexpectedValueException('$tags must be an array'); } + // Set empty tag array + foreach ($fileList as $key => $fileData) { + $fileList[$key]['tags'] = []; + } + if (!empty($tags)) { foreach ($tags as $fileId => $fileTags) { - $filesById[$fileId]['tags'] = $fileTags; - } - foreach ($filesById as $key => $fileWithTags) { - foreach($fileList as $key2 => $file){ - if( $file[$fileIdentifier] === $key){ - $fileList[$key2] = $fileWithTags; + foreach ($fileList as $key => $fileData) { + if ($fileId !== $fileData[$fileIdentifier]) { + continue; } - } - } - foreach ($fileList as $key => $file) { - if (!array_key_exists('tags', $file)) { - $fileList[$key]['tags'] = []; + $fileList[$key]['tags'] = $fileTags; } } - } + return $fileList; } diff --git a/apps/files/tests/HelperTest.php b/apps/files/tests/HelperTest.php index 8afd52abd95..901e86ddb20 100644 --- a/apps/files/tests/HelperTest.php +++ b/apps/files/tests/HelperTest.php @@ -112,4 +112,35 @@ class HelperTest extends \Test\TestCase { ); } + public function testPopulateTags() { + $tagManager = $this->createMock(\OCP\ITagManager::class); + $tagger = $this->createMock(\OCP\ITags::class); + + $tagManager->method('load') + ->with('files') + ->willReturn($tagger); + + $data = [ + ['id' => 10], + ['id' => 22, 'foo' => 'bar'], + ['id' => 42, 'x' => 'y'], + ]; + + $tags = [ + 10 => ['tag3'], + 42 => ['tag1', 'tag2'], + ]; + + $tagger->method('getTagsForObjects') + ->with([10, 22, 42]) + ->willReturn($tags); + + $result = \OCA\Files\Helper::populateTags($data, 'id', $tagManager); + + $this->assertSame([ + ['id' => 10, 'tags' => ['tag3']], + ['id' => 22, 'foo' => 'bar', 'tags' => []], + ['id' => 42, 'x' => 'y', 'tags' => ['tag1', 'tag2']], + ], $result); + } } |