Merge pull request #25003 from owncloud/fix_ocs_path_response

Fix OCS Share API path response
This commit is contained in:
Vincent Petry 2016-06-10 16:49:50 +02:00 committed by GitHub
commit 14fde6650c
3 changed files with 65 additions and 21 deletions

View File

@ -100,15 +100,8 @@ class Share20OCS {
*/ */
protected function formatShare(\OCP\Share\IShare $share) { protected function formatShare(\OCP\Share\IShare $share) {
$sharedBy = $this->userManager->get($share->getSharedBy()); $sharedBy = $this->userManager->get($share->getSharedBy());
// for federated shares the owner can be a remote user, in this $shareOwner = $this->userManager->get($share->getShareOwner());
// case we use the initiator
if ($this->userManager->userExists($share->getShareOwner())) {
$shareOwner = $this->userManager->get($share->getShareOwner());
$localUser = $share->getShareOwner();
} else {
$shareOwner = $this->userManager->get($share->getSharedBy());
$localUser = $share->getSharedBy();
}
$result = [ $result = [
'id' => $share->getId(), 'id' => $share->getId(),
'share_type' => $share->getShareType(), 'share_type' => $share->getShareType(),
@ -123,8 +116,16 @@ class Share20OCS {
'displayname_file_owner' => $shareOwner !== null ? $shareOwner->getDisplayName() : $share->getShareOwner(), 'displayname_file_owner' => $shareOwner !== null ? $shareOwner->getDisplayName() : $share->getShareOwner(),
]; ];
$node = $share->getNode(); $userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID());
$result['path'] = $this->rootFolder->getUserFolder($localUser)->getRelativePath($node->getPath()); $nodes = $userFolder->getById($share->getNodeId());
if (empty($nodes)) {
throw new NotFoundException();
}
$node = $nodes[0];
$result['path'] = $userFolder->getRelativePath($node->getPath());
if ($node instanceOf \OCP\Files\Folder) { if ($node instanceOf \OCP\Files\Folder) {
$result['item_type'] = 'folder'; $result['item_type'] = 'folder';
} else { } else {
@ -536,7 +537,6 @@ class Share20OCS {
$shares = array_merge($shares, $federatedShares); $shares = array_merge($shares, $federatedShares);
} }
$formatted = []; $formatted = [];
foreach ($shares as $share) { foreach ($shares as $share) {
try { try {

View File

@ -433,8 +433,12 @@ class Share20OCSTest extends \Test\TestCase {
->method('getRelativePath') ->method('getRelativePath')
->will($this->returnArgument(0)); ->will($this->returnArgument(0));
$userFolder->method('getById')
->with($share->getNodeId())
->willReturn([$share->getNode()]);
$this->rootFolder->method('getUserFolder') $this->rootFolder->method('getUserFolder')
->with($share->getShareOwner()) ->with($this->currentUser->getUID())
->willReturn($userFolder); ->willReturn($userFolder);
$this->urlGenerator $this->urlGenerator
@ -2006,8 +2010,19 @@ class Share20OCSTest extends \Test\TestCase {
->willReturn('myLink'); ->willReturn('myLink');
$this->rootFolder->method('getUserFolder')->with($share->getShareOwner())->will($this->returnSelf()); $this->rootFolder->method('getUserFolder')
$this->rootFolder->method('getRelativePath')->will($this->returnArgument(0)); ->with($this->currentUser->getUID())
->will($this->returnSelf());
if (!$exception) {
$this->rootFolder->method('getById')
->with($share->getNodeId())
->willReturn([$share->getNode()]);
$this->rootFolder->method('getRelativePath')
->with($share->getNode()->getPath())
->will($this->returnArgument(0));
}
try { try {
$result = $this->invokePrivate($this->ocs, 'formatShare', [$share]); $result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);

View File

@ -764,8 +764,7 @@ class ApiTest extends TestCase {
// we should get exactly one result // we should get exactly one result
$this->assertCount(1, $data); $this->assertCount(1, $data);
$expectedPath = $this->folder . $this->subfolder; $this->assertEquals($this->subfolder, $data[0]['path']);
$this->assertEquals($expectedPath, $data[0]['path']);
$this->shareManager->deleteShare($share2); $this->shareManager->deleteShare($share2);
$this->shareManager->deleteShare($share1); $this->shareManager->deleteShare($share1);
@ -801,6 +800,9 @@ class ApiTest extends TestCase {
->setPermissions(1); ->setPermissions(1);
$share3 = $this->shareManager->createShare($share3); $share3 = $this->shareManager->createShare($share3);
/*
* Test as recipient
*/
$request = $this->createRequest(['path' => '/', 'subfiles' => 'true']); $request = $this->createRequest(['path' => '/', 'subfiles' => 'true']);
$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER3); $ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER3);
$result = $ocs->getShares(); $result = $ocs->getShares();
@ -811,9 +813,37 @@ class ApiTest extends TestCase {
// we should get exactly one result // we should get exactly one result
$this->assertCount(1, $data); $this->assertCount(1, $data);
$this->assertEquals($this->subsubfolder, $data[0]['path']);
$expectedPath = $this->folder . $this->subfolder . $this->subsubfolder; /*
$this->assertEquals($expectedPath, $data[0]['path']); * Test for first owner/initiator
*/
$request = $this->createRequest([]);
$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
$result = $ocs->getShares();
$this->assertTrue($result->succeeded());
// test should return one share within $this->folder
$data = $result->getData();
// we should get exactly one result
$this->assertCount(1, $data);
$this->assertEquals($this->folder . $this->subfolder, $data[0]['path']);
/*
* Test for second initiator
*/
$request = $this->createRequest([]);
$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER2);
$result = $ocs->getShares();
$this->assertTrue($result->succeeded());
// test should return one share within $this->folder
$data = $result->getData();
// we should get exactly one result
$this->assertCount(1, $data);
$this->assertEquals($this->subfolder . $this->subsubfolder, $data[0]['path']);
$this->shareManager->deleteShare($share1); $this->shareManager->deleteShare($share1);
$this->shareManager->deleteShare($share2); $this->shareManager->deleteShare($share2);
@ -922,8 +952,7 @@ class ApiTest extends TestCase {
// we should get exactly one result // we should get exactly one result
$this->assertCount(1, $data); $this->assertCount(1, $data);
$expectedPath = $this->folder.$this->subfolder.$this->filename; $this->assertEquals($this->filename, $data[0]['path']);
$this->assertEquals($expectedPath, $data[0]['path']);
$this->shareManager->deleteShare($share1); $this->shareManager->deleteShare($share1);
$this->shareManager->deleteShare($share2); $this->shareManager->deleteShare($share2);