From 5e9b618604cbe36ecde488fad665e60607ef61a0 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Fri, 15 Apr 2016 14:05:36 +0200 Subject: [PATCH] Translate OCS Share API error messages For https://github.com/owncloud/core/issues/22209 --- apps/files_sharing/api/ocssharewrapper.php | 4 +- apps/files_sharing/api/share20ocs.php | 63 +++++++++------- apps/files_sharing/tests/api.php | 15 +++- .../tests/api/share20ocstest.php | 75 ++++++++++++------- 4 files changed, 94 insertions(+), 63 deletions(-) diff --git a/apps/files_sharing/api/ocssharewrapper.php b/apps/files_sharing/api/ocssharewrapper.php index a51ad1eb2c..8ce9271a70 100644 --- a/apps/files_sharing/api/ocssharewrapper.php +++ b/apps/files_sharing/api/ocssharewrapper.php @@ -33,7 +33,9 @@ class OCSShareWrapper { \OC::$server->getRequest(), \OC::$server->getRootFolder(), \OC::$server->getURLGenerator(), - \OC::$server->getUserSession()->getUser()); + \OC::$server->getUserSession()->getUser(), + \OC::$server->getL10N('files_sharing') + ); } public function getAllShares() { diff --git a/apps/files_sharing/api/share20ocs.php b/apps/files_sharing/api/share20ocs.php index 61d5044cf8..6809853001 100644 --- a/apps/files_sharing/api/share20ocs.php +++ b/apps/files_sharing/api/share20ocs.php @@ -22,6 +22,7 @@ namespace OCA\Files_Sharing\API; use OCP\Files\NotFoundException; use OCP\IGroupManager; +use OCP\IL10N; use OCP\IUserManager; use OCP\IRequest; use OCP\IURLGenerator; @@ -54,6 +55,8 @@ class Share20OCS { private $urlGenerator; /** @var IUser */ private $currentUser; + /** @var IL10N */ + private $l; /** * Share20OCS constructor. @@ -73,7 +76,8 @@ class Share20OCS { IRequest $request, IRootFolder $rootFolder, IURLGenerator $urlGenerator, - IUser $currentUser + IUser $currentUser, + IL10N $l10n ) { $this->shareManager = $shareManager; $this->userManager = $userManager; @@ -82,6 +86,7 @@ class Share20OCS { $this->rootFolder = $rootFolder; $this->urlGenerator = $urlGenerator; $this->currentUser = $currentUser; + $this->l = $l10n; } /** @@ -162,13 +167,13 @@ class Share20OCS { */ public function getShare($id) { if (!$this->shareManager->shareApiEnabled()) { - return new \OC_OCS_Result(null, 404, 'Share API is disabled'); + return new \OC_OCS_Result(null, 404, $this->l->t('Share API is disabled')); } try { $share = $this->getShareById($id); } catch (ShareNotFound $e) { - return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); + return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist')); } if ($this->canAccessShare($share)) { @@ -180,7 +185,7 @@ class Share20OCS { } } - return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); + return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist')); } /** @@ -191,17 +196,17 @@ class Share20OCS { */ public function deleteShare($id) { if (!$this->shareManager->shareApiEnabled()) { - return new \OC_OCS_Result(null, 404, 'Share API is disabled'); + return new \OC_OCS_Result(null, 404, $this->l->t('Share API is disabled')); } try { $share = $this->getShareById($id); } catch (ShareNotFound $e) { - return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); + return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist')); } if (!$this->canAccessShare($share)) { - return new \OC_OCS_Result(null, 404, 'could not delete share'); + return new \OC_OCS_Result(null, 404, $this->l->t('Could not delete share')); } $this->shareManager->deleteShare($share); @@ -216,20 +221,20 @@ class Share20OCS { $share = $this->shareManager->newShare(); if (!$this->shareManager->shareApiEnabled()) { - return new \OC_OCS_Result(null, 404, 'Share API is disabled'); + return new \OC_OCS_Result(null, 404, $this->l->t('Share API is disabled')); } // Verify path $path = $this->request->getParam('path', null); if ($path === null) { - return new \OC_OCS_Result(null, 404, 'please specify a file or folder path'); + return new \OC_OCS_Result(null, 404, $this->l->t('Please specify a file or folder path')); } $userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID()); try { $path = $userFolder->get($path); } catch (\OCP\Files\NotFoundException $e) { - return new \OC_OCS_Result(null, 404, 'wrong path, file/folder doesn\'t exist'); + return new \OC_OCS_Result(null, 404, $this->l->t('Wrong path, file/folder doesn\'t exist')); } $share->setNode($path); @@ -270,25 +275,25 @@ class Share20OCS { if ($shareType === \OCP\Share::SHARE_TYPE_USER) { // Valid user is required to share if ($shareWith === null || !$this->userManager->userExists($shareWith)) { - return new \OC_OCS_Result(null, 404, 'please specify a valid user'); + return new \OC_OCS_Result(null, 404, $this->l->t('Please specify a valid user')); } $share->setSharedWith($shareWith); $share->setPermissions($permissions); } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { if (!$this->shareManager->allowGroupSharing()) { - return new \OC_OCS_Result(null, 404, 'group sharing is disabled by the administrator'); + return new \OC_OCS_Result(null, 404, $this->l->t('Group sharing is disabled by the administrator')); } // Valid group is required to share if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) { - return new \OC_OCS_Result(null, 404, 'please specify a valid group'); + return new \OC_OCS_Result(null, 404, $this->l->t('Please specify a valid group')); } $share->setSharedWith($shareWith); $share->setPermissions($permissions); } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) { //Can we even share links? if (!$this->shareManager->shareApiAllowLinks()) { - return new \OC_OCS_Result(null, 404, 'public link sharing is disabled by the administrator'); + return new \OC_OCS_Result(null, 404, $this->l->t('Public link sharing is disabled by the administrator')); } /* @@ -304,12 +309,12 @@ class Share20OCS { if ($publicUpload === 'true') { // Check if public upload is allowed if (!$this->shareManager->shareApiLinkAllowPublicUpload()) { - return new \OC_OCS_Result(null, 403, 'public upload disabled by the administrator'); + return new \OC_OCS_Result(null, 403, $this->l->t('Public upload disabled by the administrator')); } // Public upload can only be set for folders if ($path instanceof \OCP\Files\File) { - return new \OC_OCS_Result(null, 404, 'public upload is only possible for public shared folders'); + return new \OC_OCS_Result(null, 404, $this->l->t('Public upload is only possible for publicly shared folders')); } $share->setPermissions( @@ -336,19 +341,19 @@ class Share20OCS { $expireDate = $this->parseDate($expireDate); $share->setExpirationDate($expireDate); } catch (\Exception $e) { - return new \OC_OCS_Result(null, 404, 'Invalid Date. Format must be YYYY-MM-DD.'); + return new \OC_OCS_Result(null, 404, $this->l->t('Invalid date, date format must be YYYY-MM-DD')); } } } else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) { if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { - return new \OC_OCS_Result(null, 403, 'Sharing '.$path->getPath().' failed, because the backend does not allow shares from type '.$shareType); + return new \OC_OCS_Result(null, 403, $this->l->t('Sharing %s failed because the back end does not allow shares from type %s', [$path->getPath(), $shareType])); } $share->setSharedWith($shareWith); $share->setPermissions($permissions); } else { - return new \OC_OCS_Result(null, 400, "unknown share type"); + return new \OC_OCS_Result(null, 400, $this->l->t('Unknown share type')); } $share->setShareType($shareType); @@ -397,7 +402,7 @@ class Share20OCS { */ private function getSharesInDir($folder) { if (!($folder instanceof \OCP\Files\Folder)) { - return new \OC_OCS_Result(null, 400, "not a directory"); + return new \OC_OCS_Result(null, 400, $this->l->t('Not a directory')); } $nodes = $folder->getDirectoryListing(); @@ -450,7 +455,7 @@ class Share20OCS { try { $path = $userFolder->get($path); } catch (\OCP\Files\NotFoundException $e) { - return new \OC_OCS_Result(null, 404, 'wrong path, file/folder doesn\'t exist'); + return new \OC_OCS_Result(null, 404, $this->l->t('Wrong path, file/folder doesn\'t exist')); } } @@ -498,17 +503,17 @@ class Share20OCS { */ public function updateShare($id) { if (!$this->shareManager->shareApiEnabled()) { - return new \OC_OCS_Result(null, 404, 'Share API is disabled'); + return new \OC_OCS_Result(null, 404, $this->l->t('Share API is disabled')); } try { $share = $this->getShareById($id); } catch (ShareNotFound $e) { - return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); + return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist')); } if (!$this->canAccessShare($share)) { - return new \OC_OCS_Result(null, 404, 'wrong share Id, share doesn\'t exist.'); + return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist')); } $permissions = $this->request->getParam('permissions', null); @@ -538,16 +543,16 @@ class Share20OCS { if ($newPermissions !== null && $newPermissions !== \OCP\Constants::PERMISSION_READ && $newPermissions !== (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)) { - return new \OC_OCS_Result(null, 400, 'can\'t change permission for public link share'); + return new \OC_OCS_Result(null, 400, $this->l->t('Can\'t change permissions for public share links')); } if ($newPermissions === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)) { if (!$this->shareManager->shareApiLinkAllowPublicUpload()) { - return new \OC_OCS_Result(null, 403, 'public upload disabled by the administrator'); + return new \OC_OCS_Result(null, 403, $this->l->t('Public upload disabled by the administrator')); } if (!($share->getNode() instanceof \OCP\Files\Folder)) { - return new \OC_OCS_Result(null, 400, "public upload is only possible for public shared folders"); + return new \OC_OCS_Result(null, 400, $this->l->t('Public upload is only possible for publicly shared folders')); } } @@ -575,7 +580,7 @@ class Share20OCS { } else { // For other shares only permissions is valid. if ($permissions === null) { - return new \OC_OCS_Result(null, 400, 'Wrong or no update parameter given'); + return new \OC_OCS_Result(null, 400, $this->l->t('Wrong or no update parameter given')); } else { $permissions = (int)$permissions; $share->setPermissions($permissions); @@ -594,7 +599,7 @@ class Share20OCS { } if ($share->getPermissions() & ~$maxPermissions) { - return new \OC_OCS_Result(null, 404, 'Cannot increase permissions'); + return new \OC_OCS_Result(null, 404, $this->l->t('Cannot increase permissions')); } } } diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php index 0a61e9a9fa..3ded1bfc5d 100644 --- a/apps/files_sharing/tests/api.php +++ b/apps/files_sharing/tests/api.php @@ -101,6 +101,12 @@ class Test_Files_Sharing_Api extends TestCase { private function createOCS($request, $userId) { $currentUser = \OC::$server->getUserManager()->get($userId); + $l = $this->getMock('\OCP\IL10N'); + $l->method('t') + ->will($this->returnCallback(function($text, $parameters = []) { + return vsprintf($text, $parameters); + })); + return new \OCA\Files_Sharing\API\Share20OCS( $this->shareManager, \OC::$server->getGroupManager(), @@ -108,7 +114,8 @@ class Test_Files_Sharing_Api extends TestCase { $request, \OC::$server->getRootFolder(), \OC::$server->getURLGenerator(), - $currentUser + $currentUser, + $l ); } @@ -665,7 +672,7 @@ class Test_Files_Sharing_Api extends TestCase { $this->assertFalse($result->succeeded()); $this->assertEquals(400, $result->getStatusCode()); - $this->assertEquals('not a directory', $result->getMeta()['message']); + $this->assertEquals('Not a directory', $result->getMeta()['message']); $this->shareManager->deleteShare($share1); } @@ -935,7 +942,7 @@ class Test_Files_Sharing_Api extends TestCase { $this->assertEquals(404, $result->getStatusCode()); $meta = $result->getMeta(); - $this->assertEquals('wrong share ID, share doesn\'t exist.', $meta['message']); + $this->assertEquals('Wrong share ID, share doesn\'t exist', $meta['message']); } /** @@ -1404,7 +1411,7 @@ class Test_Files_Sharing_Api extends TestCase { if ($valid === false) { $this->assertFalse($result->succeeded()); $this->assertEquals(404, $result->getStatusCode()); - $this->assertEquals('Invalid Date. Format must be YYYY-MM-DD.', $result->getMeta()['message']); + $this->assertEquals('Invalid date, date format must be YYYY-MM-DD', $result->getMeta()['message']); return; } diff --git a/apps/files_sharing/tests/api/share20ocstest.php b/apps/files_sharing/tests/api/share20ocstest.php index 9a30b8720e..56c350aa99 100644 --- a/apps/files_sharing/tests/api/share20ocstest.php +++ b/apps/files_sharing/tests/api/share20ocstest.php @@ -20,6 +20,7 @@ */ namespace OCA\Files_Sharing\Tests\API; +use OCP\IL10N; use OCA\Files_Sharing\API\Share20OCS; use OCP\Files\NotFoundException; use OCP\IGroupManager; @@ -61,6 +62,9 @@ class Share20OCSTest extends \Test\TestCase { /** @var Share20OCS */ private $ocs; + /** @var IL10N */ + private $l; + protected function setUp() { $this->shareManager = $this->getMockBuilder('OCP\Share\IManager') ->disableOriginalConstructor() @@ -77,14 +81,21 @@ class Share20OCSTest extends \Test\TestCase { $this->currentUser = $this->getMock('OCP\IUser'); $this->currentUser->method('getUID')->willReturn('currentUser'); + $this->l = $this->getMock('\OCP\IL10N'); + $this->l->method('t') + ->will($this->returnCallback(function($text, $parameters = []) { + return vsprintf($text, $parameters); + })); + $this->ocs = new Share20OCS( - $this->shareManager, - $this->groupManager, - $this->userManager, - $this->request, - $this->rootFolder, - $this->urlGenerator, - $this->currentUser + $this->shareManager, + $this->groupManager, + $this->userManager, + $this->request, + $this->rootFolder, + $this->urlGenerator, + $this->currentUser, + $this->l ); } @@ -97,7 +108,8 @@ class Share20OCSTest extends \Test\TestCase { $this->request, $this->rootFolder, $this->urlGenerator, - $this->currentUser + $this->currentUser, + $this->l, ])->setMethods(['formatShare']) ->getMock(); } @@ -120,7 +132,7 @@ class Share20OCSTest extends \Test\TestCase { $this->shareManager->method('outgoingServer2ServerSharesAllowed')->willReturn(true); - $expected = new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); + $expected = new \OC_OCS_Result(null, 404, 'Wrong share ID, share doesn\'t exist'); $this->assertEquals($expected, $this->ocs->deleteShare(42)); } @@ -361,7 +373,8 @@ class Share20OCSTest extends \Test\TestCase { $this->request, $this->rootFolder, $this->urlGenerator, - $this->currentUser + $this->currentUser, + $this->l, ])->setMethods(['canAccessShare']) ->getMock(); @@ -426,7 +439,7 @@ class Share20OCSTest extends \Test\TestCase { ->with('ocinternal:42') ->willReturn($share); - $expected = new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.'); + $expected = new \OC_OCS_Result(null, 404, 'Wrong share ID, share doesn\'t exist'); $this->assertEquals($expected->getMeta(), $this->ocs->getShare(42)->getMeta()); } @@ -478,7 +491,7 @@ class Share20OCSTest extends \Test\TestCase { } public function testCreateShareNoPath() { - $expected = new \OC_OCS_Result(null, 404, 'please specify a file or folder path'); + $expected = new \OC_OCS_Result(null, 404, 'Please specify a file or folder path'); $result = $this->ocs->createShare(); @@ -504,7 +517,7 @@ class Share20OCSTest extends \Test\TestCase { ->with('invalid-path') ->will($this->throwException(new \OCP\Files\NotFoundException())); - $expected = new \OC_OCS_Result(null, 404, 'wrong path, file/folder doesn\'t exist'); + $expected = new \OC_OCS_Result(null, 404, 'Wrong path, file/folder doesn\'t exist'); $result = $this->ocs->createShare(); @@ -572,7 +585,7 @@ class Share20OCSTest extends \Test\TestCase { ->with('valid-path') ->willReturn($path); - $expected = new \OC_OCS_Result(null, 404, 'please specify a valid user'); + $expected = new \OC_OCS_Result(null, 404, 'Please specify a valid user'); $result = $this->ocs->createShare(); @@ -610,7 +623,7 @@ class Share20OCSTest extends \Test\TestCase { ->with('valid-path') ->willReturn($path); - $expected = new \OC_OCS_Result(null, 404, 'please specify a valid user'); + $expected = new \OC_OCS_Result(null, 404, 'Please specify a valid user'); $result = $this->ocs->createShare(); @@ -631,7 +644,8 @@ class Share20OCSTest extends \Test\TestCase { $this->request, $this->rootFolder, $this->urlGenerator, - $this->currentUser + $this->currentUser, + $this->l, ])->setMethods(['formatShare']) ->getMock(); @@ -711,7 +725,7 @@ class Share20OCSTest extends \Test\TestCase { ->with('valid-path') ->willReturn($path); - $expected = new \OC_OCS_Result(null, 404, 'please specify a valid user'); + $expected = new \OC_OCS_Result(null, 404, 'Please specify a valid user'); $result = $this->ocs->createShare(); @@ -732,7 +746,8 @@ class Share20OCSTest extends \Test\TestCase { $this->request, $this->rootFolder, $this->urlGenerator, - $this->currentUser + $this->currentUser, + $this->l, ])->setMethods(['formatShare']) ->getMock(); @@ -819,7 +834,7 @@ class Share20OCSTest extends \Test\TestCase { $share->method('setNode')->with($path); - $expected = new \OC_OCS_Result(null, 404, 'group sharing is disabled by the administrator'); + $expected = new \OC_OCS_Result(null, 404, 'Group sharing is disabled by the administrator'); $result = $this->ocs->createShare(); $this->assertEquals($expected->getMeta(), $result->getMeta()); @@ -845,7 +860,7 @@ class Share20OCSTest extends \Test\TestCase { $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare()); - $expected = new \OC_OCS_Result(null, 404, 'public link sharing is disabled by the administrator'); + $expected = new \OC_OCS_Result(null, 404, 'Public link sharing is disabled by the administrator'); $result = $this->ocs->createShare(); $this->assertEquals($expected->getMeta(), $result->getMeta()); @@ -873,7 +888,7 @@ class Share20OCSTest extends \Test\TestCase { $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare()); $this->shareManager->method('shareApiAllowLinks')->willReturn(true); - $expected = new \OC_OCS_Result(null, 403, 'public upload disabled by the administrator'); + $expected = new \OC_OCS_Result(null, 403, 'Public upload disabled by the administrator'); $result = $this->ocs->createShare(); $this->assertEquals($expected->getMeta(), $result->getMeta()); @@ -902,7 +917,7 @@ class Share20OCSTest extends \Test\TestCase { $this->shareManager->method('shareApiAllowLinks')->willReturn(true); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); - $expected = new \OC_OCS_Result(null, 404, 'public upload is only possible for public shared folders'); + $expected = new \OC_OCS_Result(null, 404, 'Public upload is only possible for publicly shared folders'); $result = $this->ocs->createShare(); $this->assertEquals($expected->getMeta(), $result->getMeta()); @@ -1070,7 +1085,7 @@ class Share20OCSTest extends \Test\TestCase { $this->shareManager->method('shareApiAllowLinks')->willReturn(true); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); - $expected = new \OC_OCS_Result(null, 404, 'Invalid Date. Format must be YYYY-MM-DD.'); + $expected = new \OC_OCS_Result(null, 404, 'Invalid date, date format must be YYYY-MM-DD'); $result = $ocs->createShare(); $this->assertEquals($expected->getMeta(), $result->getMeta()); @@ -1093,7 +1108,8 @@ class Share20OCSTest extends \Test\TestCase { $this->request, $this->rootFolder, $this->urlGenerator, - $this->currentUser + $this->currentUser, + $this->l, ])->setMethods(['formatShare']) ->getMock(); @@ -1142,7 +1158,7 @@ class Share20OCSTest extends \Test\TestCase { $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); - $expected = new \OC_OCS_Result(null, 404, 'wrong share Id, share doesn\'t exist.'); + $expected = new \OC_OCS_Result(null, 404, 'Wrong share ID, share doesn\'t exist'); $result = $this->ocs->updateShare(42); $this->assertEquals($expected->getMeta(), $result->getMeta()); @@ -1306,7 +1322,7 @@ class Share20OCSTest extends \Test\TestCase { $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(false); - $expected = new \OC_OCS_Result(null, 403, 'public upload disabled by the administrator'); + $expected = new \OC_OCS_Result(null, 403, 'Public upload disabled by the administrator'); $result = $ocs->updateShare(42); $this->assertEquals($expected->getMeta(), $result->getMeta()); @@ -1335,7 +1351,7 @@ class Share20OCSTest extends \Test\TestCase { $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); - $expected = new \OC_OCS_Result(null, 400, 'public upload is only possible for public shared folders'); + $expected = new \OC_OCS_Result(null, 400, 'Public upload is only possible for publicly shared folders'); $result = $ocs->updateShare(42); $this->assertEquals($expected->getMeta(), $result->getMeta()); @@ -1523,7 +1539,7 @@ class Share20OCSTest extends \Test\TestCase { $this->shareManager->method('getShareById')->with('ocinternal:42')->willReturn($share); $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true); - $expected = new \OC_OCS_Result(null, 400, 'can\'t change permission for public link share'); + $expected = new \OC_OCS_Result(null, 400, 'Can\'t change permissions for public share links'); $result = $ocs->updateShare(42); $this->assertEquals($expected->getMeta(), $result->getMeta()); @@ -1899,7 +1915,8 @@ class Share20OCSTest extends \Test\TestCase { $this->request, $this->rootFolder, $this->urlGenerator, - $this->currentUser + $this->currentUser, + $this->l ); }