diff options
Diffstat (limited to 'apps/files_external/tests/Controller')
-rw-r--r-- | apps/files_external/tests/Controller/AjaxControllerTest.php | 129 | ||||
-rw-r--r-- | apps/files_external/tests/Controller/GlobalStoragesControllerTest.php | 71 | ||||
-rw-r--r-- | apps/files_external/tests/Controller/StoragesControllerTestCase.php (renamed from apps/files_external/tests/Controller/StoragesControllerTest.php) | 240 | ||||
-rw-r--r-- | apps/files_external/tests/Controller/UserStoragesControllerTest.php | 90 |
4 files changed, 262 insertions, 268 deletions
diff --git a/apps/files_external/tests/Controller/AjaxControllerTest.php b/apps/files_external/tests/Controller/AjaxControllerTest.php index 1153036c414..b1ea7a2b1b1 100644 --- a/apps/files_external/tests/Controller/AjaxControllerTest.php +++ b/apps/files_external/tests/Controller/AjaxControllerTest.php @@ -1,25 +1,9 @@ <?php + +declare(strict_types=1); /** - * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch> - * - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Files_External\Tests\Controller; @@ -28,35 +12,29 @@ use OCA\Files_External\Lib\Auth\Password\GlobalAuth; use OCA\Files_External\Lib\Auth\PublicKey\RSA; use OCP\AppFramework\Http\JSONResponse; use OCP\IGroupManager; +use OCP\IL10N; use OCP\IRequest; use OCP\IUser; use OCP\IUserSession; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class AjaxControllerTest extends TestCase { - /** @var IRequest */ - private $request; - /** @var RSA */ - private $rsa; - /** @var GlobalAuth */ - private $globalAuth; - /** @var IUserSession */ - private $userSession; - /** @var IGroupManager */ - private $groupManager; - /** @var AjaxController */ - private $ajaxController; - - public function setUp() { + private IRequest&MockObject $request; + private RSA&MockObject $rsa; + private GlobalAuth&MockObject $globalAuth; + private IUserSession&MockObject $userSession; + private IGroupManager&MockObject $groupManager; + private IL10N&MockObject $l10n; + private AjaxController $ajaxController; + + protected function setUp(): void { $this->request = $this->createMock(IRequest::class); - $this->rsa = $this->getMockBuilder('\\OCA\\Files_External\\Lib\\Auth\\PublicKey\\RSA') - ->disableOriginalConstructor() - ->getMock(); - $this->globalAuth = $this->getMockBuilder('\\OCA\\Files_External\\Lib\\Auth\\Password\GlobalAuth') - ->disableOriginalConstructor() - ->getMock(); + $this->rsa = $this->createMock(RSA::class); + $this->globalAuth = $this->createMock(GlobalAuth::class); $this->userSession = $this->createMock(IUserSession::class); $this->groupManager = $this->createMock(IGroupManager::class); + $this->l10n = $this->createMock(IL10N::class); $this->ajaxController = new AjaxController( 'files_external', @@ -64,13 +42,23 @@ class AjaxControllerTest extends TestCase { $this->rsa, $this->globalAuth, $this->userSession, - $this->groupManager + $this->groupManager, + $this->l10n, ); + $this->l10n->expects($this->any()) + ->method('t') + ->willReturnCallback(function ($string, $args) { + if (!is_array($args)) { + $args = [$args]; + } + return vsprintf($string, $args); + }); + parent::setUp(); } - public function testGetSshKeys() { + public function testGetSshKeys(): void { $this->rsa ->expects($this->once()) ->method('createKey') @@ -79,7 +67,7 @@ class AjaxControllerTest extends TestCase { 'publickey' => 'MyPublicKey', ]); - $expected = new JSONResponse( + $expected = new JSONResponse( [ 'data' => [ 'private_key' => 'MyPrivateKey', @@ -91,7 +79,7 @@ class AjaxControllerTest extends TestCase { $this->assertEquals($expected, $this->ajaxController->getSshKeys()); } - public function testSaveGlobalCredentialsAsAdminForAnotherUser() { + public function testSaveGlobalCredentialsAsAdminForAnotherUser(): void { $user = $this->createMock(IUser::class); $user ->expects($this->once()) @@ -101,20 +89,16 @@ class AjaxControllerTest extends TestCase { ->expects($this->once()) ->method('getUser') ->willReturn($user); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('MyAdminUid') - ->willReturn(true); $this->globalAuth - ->expects($this->once()) - ->method('saveAuth') - ->with('UidOfTestUser', 'test', 'password'); + ->expects($this->never()) + ->method('saveAuth'); - $this->assertSame(true, $this->ajaxController->saveGlobalCredentials('UidOfTestUser', 'test', 'password')); + $response = $this->ajaxController->saveGlobalCredentials('UidOfTestUser', 'test', 'password'); + $this->assertSame($response->getStatus(), 403); + $this->assertSame('Permission denied', $response->getData()['message']); } - public function testSaveGlobalCredentialsAsAdminForSelf() { + public function testSaveGlobalCredentialsAsAdminForSelf(): void { $user = $this->createMock(IUser::class); $user ->expects($this->once()) @@ -124,58 +108,45 @@ class AjaxControllerTest extends TestCase { ->expects($this->once()) ->method('getUser') ->willReturn($user); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('MyAdminUid') - ->willReturn(true); $this->globalAuth ->expects($this->once()) ->method('saveAuth') ->with('MyAdminUid', 'test', 'password'); - $this->assertSame(true, $this->ajaxController->saveGlobalCredentials('MyAdminUid', 'test', 'password')); + $response = $this->ajaxController->saveGlobalCredentials('MyAdminUid', 'test', 'password'); + $this->assertSame($response->getStatus(), 200); } - public function testSaveGlobalCredentialsAsNormalUserForSelf() { + public function testSaveGlobalCredentialsAsNormalUserForSelf(): void { $user = $this->createMock(IUser::class); $user - ->expects($this->exactly(2)) ->method('getUID') ->willReturn('MyUserUid'); $this->userSession - ->expects($this->once()) ->method('getUser') ->willReturn($user); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('MyUserUid') - ->willReturn(false); $this->globalAuth - ->expects($this->once()) ->method('saveAuth') ->with('MyUserUid', 'test', 'password'); - $this->assertSame(true, $this->ajaxController->saveGlobalCredentials('MyUserUid', 'test', 'password')); + $response = $this->ajaxController->saveGlobalCredentials('MyUserUid', 'test', 'password'); + $this->assertSame($response->getStatus(), 200); } - public function testSaveGlobalCredentialsAsNormalUserForAnotherUser() { + public function testSaveGlobalCredentialsAsNormalUserForAnotherUser(): void { $user = $this->createMock(IUser::class); $user - ->expects($this->exactly(2)) ->method('getUID') ->willReturn('MyUserUid'); $this->userSession - ->expects($this->once()) ->method('getUser') ->willReturn($user); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('MyUserUid') - ->willReturn(false); + $this->globalAuth + ->expects($this->never()) + ->method('saveAuth'); - $this->assertSame(false, $this->ajaxController->saveGlobalCredentials('AnotherUserUid', 'test', 'password')); + $response = $this->ajaxController->saveGlobalCredentials('AnotherUserUid', 'test', 'password'); + $this->assertSame($response->getStatus(), 403); + $this->assertSame('Permission denied', $response->getData()['message']); } } diff --git a/apps/files_external/tests/Controller/GlobalStoragesControllerTest.php b/apps/files_external/tests/Controller/GlobalStoragesControllerTest.php index 2e0245e76fa..74a27eb15e4 100644 --- a/apps/files_external/tests/Controller/GlobalStoragesControllerTest.php +++ b/apps/files_external/tests/Controller/GlobalStoragesControllerTest.php @@ -1,53 +1,60 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * @author Robin Appelman <robin@icewind.nl> - * @author Robin McCorkell <robin@mccorkell.me.uk> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\Files_External\Tests\Controller; +use OC\User\User; use OCA\Files_External\Controller\GlobalStoragesController; -use \OCP\AppFramework\Http; -use \OCA\Files_External\Service\BackendService; +use OCA\Files_External\Service\BackendService; +use OCA\Files_External\Service\GlobalStoragesService; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\IConfig; +use OCP\IGroupManager; use OCP\IL10N; -use OCP\ILogger; use OCP\IRequest; +use OCP\IUserSession; +use Psr\Log\LoggerInterface; -class GlobalStoragesControllerTest extends StoragesControllerTest { - public function setUp() { +class GlobalStoragesControllerTest extends StoragesControllerTestCase { + protected function setUp(): void { parent::setUp(); - $this->service = $this->getMockBuilder('\OCA\Files_External\Service\GlobalStoragesService') - ->disableOriginalConstructor() - ->getMock(); + + $this->service = $this->createMock(GlobalStoragesService::class); $this->service->method('getVisibilityType') ->willReturn(BackendService::VISIBILITY_ADMIN); - $this->controller = new GlobalStoragesController( + $this->controller = $this->createController(true); + } + + private function createController(bool $allowCreateLocal = true): GlobalStoragesController { + $session = $this->createMock(IUserSession::class); + $session->method('getUser') + ->willReturn(new User('test', null, $this->createMock(IEventDispatcher::class))); + + $config = $this->createMock(IConfig::class); + $config->method('getSystemValue') + ->with('files_external_allow_create_new_local', true) + ->willReturn($allowCreateLocal); + + return new GlobalStoragesController( 'files_external', $this->createMock(IRequest::class), $this->createMock(IL10N::class), $this->service, - $this->createMock(ILogger::class) + $this->createMock(LoggerInterface::class), + $session, + $this->createMock(IGroupManager::class), + $config ); } + + public function testAddLocalStorageWhenDisabled(): void { + $this->controller = $this->createController(false); + parent::testAddLocalStorageWhenDisabled(); + } } diff --git a/apps/files_external/tests/Controller/StoragesControllerTest.php b/apps/files_external/tests/Controller/StoragesControllerTestCase.php index ad0a3401412..1eb52f9b459 100644 --- a/apps/files_external/tests/Controller/StoragesControllerTest.php +++ b/apps/files_external/tests/Controller/StoragesControllerTestCase.php @@ -1,88 +1,71 @@ <?php + +declare(strict_types=1); /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin McCorkell <robin@mccorkell.me.uk> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\Files_External\Tests\Controller; +use OCA\Files_External\Controller\GlobalStoragesController; +use OCA\Files_External\Controller\UserStoragesController; use OCA\Files_External\Lib\Auth\AuthMechanism; +use OCA\Files_External\Lib\Auth\NullMechanism; use OCA\Files_External\Lib\Backend\Backend; -use \OCP\AppFramework\Http; - -use \OCA\Files_External\Controller\GlobalStoragesController; -use \OCA\Files_External\Service\GlobalStoragesService; +use OCA\Files_External\Lib\Backend\SMB; use OCA\Files_External\Lib\StorageConfig; +use OCA\Files_External\MountConfig; use OCA\Files_External\NotFoundException; - -abstract class StoragesControllerTest extends \Test\TestCase { - - /** - * @var GlobalStoragesController - */ - protected $controller; - - /** - * @var GlobalStoragesService - */ - protected $service; - - public function setUp() { - \OC_Mount_Config::$skipTest = true; +use OCA\Files_External\Service\GlobalStoragesService; +use OCA\Files_External\Service\UserStoragesService; +use OCP\AppFramework\Http; +use PHPUnit\Framework\MockObject\MockObject; + +abstract class StoragesControllerTestCase extends \Test\TestCase { + protected GlobalStoragesController|UserStoragesController $controller; + protected GlobalStoragesService|UserStoragesService|MockObject $service; + + protected function setUp(): void { + parent::setUp(); + MountConfig::$skipTest = true; } - public function tearDown() { - \OC_Mount_Config::$skipTest = false; + protected function tearDown(): void { + MountConfig::$skipTest = false; + parent::tearDown(); } /** - * @return \OCA\Files_External\Lib\Backend\Backend + * @return \OCA\Files_External\Lib\Backend\Backend&MockObject */ - protected function getBackendMock($class = '\OCA\Files_External\Lib\Backend\SMB', $storageClass = '\OCA\Files_External\Lib\Storage\SMB') { - $backend = $this->getMockBuilder(Backend::class) - ->disableOriginalConstructor() - ->getMock(); + protected function getBackendMock($class = SMB::class, $storageClass = \OCA\Files_External\Lib\Storage\SMB::class) { + $backend = $this->createMock(Backend::class); $backend->method('getStorageClass') ->willReturn($storageClass); $backend->method('getIdentifier') - ->willReturn('identifier:'.$class); + ->willReturn('identifier:' . $class); + $backend->method('getParameters') + ->willReturn([]); return $backend; } /** - * @return \OCA\Files_External\Lib\Auth\AuthMechanism + * @return AuthMechanism|MockObject */ - protected function getAuthMechMock($scheme = 'null', $class = '\OCA\Files_External\Lib\Auth\NullMechanism') { - $authMech = $this->getMockBuilder(AuthMechanism::class) - ->disableOriginalConstructor() - ->getMock(); + protected function getAuthMechMock($scheme = 'null', $class = NullMechanism::class) { + $authMech = $this->createMock(AuthMechanism::class); $authMech->method('getScheme') ->willReturn($scheme); $authMech->method('getIdentifier') - ->willReturn('identifier:'.$class); + ->willReturn('identifier:' . $class); + $authMech->method('getParameters') + ->willReturn([]); return $authMech; } - public function testAddStorage() { + public function testAddStorage(): void { $authMech = $this->getAuthMechMock(); $authMech->method('validateStorage') ->willReturn(true); @@ -102,16 +85,16 @@ abstract class StoragesControllerTest extends \Test\TestCase { $this->service->expects($this->once()) ->method('createStorage') - ->will($this->returnValue($storageConfig)); + ->willReturn($storageConfig); $this->service->expects($this->once()) ->method('addStorage') - ->will($this->returnValue($storageConfig)); + ->willReturn($storageConfig); $response = $this->controller->create( 'mount', - '\OCA\Files_External\Lib\Storage\SMB', - '\OCA\Files_External\Lib\Auth\NullMechanism', - array(), + \OCA\Files_External\Lib\Storage\SMB::class, + NullMechanism::class, + [], [], [], [], @@ -120,10 +103,40 @@ abstract class StoragesControllerTest extends \Test\TestCase { $data = $response->getData(); $this->assertEquals(Http::STATUS_CREATED, $response->getStatus()); - $this->assertEquals($storageConfig, $data); + $this->assertEquals($storageConfig->jsonSerialize(), $data); } - public function testUpdateStorage() { + public function testAddLocalStorageWhenDisabled(): void { + $authMech = $this->getAuthMechMock(); + $backend = $this->getBackendMock(); + + $storageConfig = new StorageConfig(1); + $storageConfig->setMountPoint('mount'); + $storageConfig->setBackend($backend); + $storageConfig->setAuthMechanism($authMech); + $storageConfig->setBackendOptions([]); + + $this->service->expects($this->never()) + ->method('createStorage'); + $this->service->expects($this->never()) + ->method('addStorage'); + + $response = $this->controller->create( + 'mount', + 'local', + NullMechanism::class, + [], + [], + [], + [], + null + ); + + $data = $response->getData(); + $this->assertEquals(Http::STATUS_FORBIDDEN, $response->getStatus()); + } + + public function testUpdateStorage(): void { $authMech = $this->getAuthMechMock(); $authMech->method('validateStorage') ->willReturn(true); @@ -143,17 +156,17 @@ abstract class StoragesControllerTest extends \Test\TestCase { $this->service->expects($this->once()) ->method('createStorage') - ->will($this->returnValue($storageConfig)); + ->willReturn($storageConfig); $this->service->expects($this->once()) ->method('updateStorage') - ->will($this->returnValue($storageConfig)); + ->willReturn($storageConfig); $response = $this->controller->update( 1, 'mount', - '\OCA\Files_External\Lib\Storage\SMB', - '\OCA\Files_External\Lib\Auth\NullMechanism', - array(), + \OCA\Files_External\Lib\Storage\SMB::class, + NullMechanism::class, + [], [], [], [], @@ -162,21 +175,19 @@ abstract class StoragesControllerTest extends \Test\TestCase { $data = $response->getData(); $this->assertEquals(Http::STATUS_OK, $response->getStatus()); - $this->assertEquals($storageConfig, $data); + $this->assertEquals($storageConfig->jsonSerialize(), $data); } - function mountPointNamesProvider() { - return array( - array(''), - array('/'), - array('//'), - ); + public static function mountPointNamesProvider(): array { + return [ + [''], + ['/'], + ['//'], + ]; } - /** - * @dataProvider mountPointNamesProvider - */ - public function testAddOrUpdateStorageInvalidMountPoint($mountPoint) { + #[\PHPUnit\Framework\Attributes\DataProvider('mountPointNamesProvider')] + public function testAddOrUpdateStorageInvalidMountPoint($mountPoint): void { $storageConfig = new StorageConfig(1); $storageConfig->setMountPoint($mountPoint); $storageConfig->setBackend($this->getBackendMock()); @@ -185,7 +196,7 @@ abstract class StoragesControllerTest extends \Test\TestCase { $this->service->expects($this->exactly(2)) ->method('createStorage') - ->will($this->returnValue($storageConfig)); + ->willReturn($storageConfig); $this->service->expects($this->never()) ->method('addStorage'); $this->service->expects($this->never()) @@ -193,9 +204,9 @@ abstract class StoragesControllerTest extends \Test\TestCase { $response = $this->controller->create( $mountPoint, - '\OCA\Files_External\Lib\Storage\SMB', - '\OCA\Files_External\Lib\Auth\NullMechanism', - array(), + \OCA\Files_External\Lib\Storage\SMB::class, + NullMechanism::class, + [], [], [], [], @@ -207,9 +218,9 @@ abstract class StoragesControllerTest extends \Test\TestCase { $response = $this->controller->update( 1, $mountPoint, - '\OCA\Files_External\Lib\Storage\SMB', - '\OCA\Files_External\Lib\Auth\NullMechanism', - array(), + \OCA\Files_External\Lib\Storage\SMB::class, + NullMechanism::class, + [], [], [], [], @@ -219,10 +230,10 @@ abstract class StoragesControllerTest extends \Test\TestCase { $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); } - public function testAddOrUpdateStorageInvalidBackend() { + public function testAddOrUpdateStorageInvalidBackend(): void { $this->service->expects($this->exactly(2)) ->method('createStorage') - ->will($this->throwException(new \InvalidArgumentException())); + ->willThrowException(new \InvalidArgumentException()); $this->service->expects($this->never()) ->method('addStorage'); $this->service->expects($this->never()) @@ -231,8 +242,8 @@ abstract class StoragesControllerTest extends \Test\TestCase { $response = $this->controller->create( 'mount', '\OC\Files\Storage\InvalidStorage', - '\OCA\Files_External\Lib\Auth\NullMechanism', - array(), + NullMechanism::class, + [], [], [], [], @@ -245,8 +256,8 @@ abstract class StoragesControllerTest extends \Test\TestCase { 1, 'mount', '\OC\Files\Storage\InvalidStorage', - '\OCA\Files_External\Lib\Auth\NullMechanism', - array(), + NullMechanism::class, + [], [], [], [], @@ -256,7 +267,7 @@ abstract class StoragesControllerTest extends \Test\TestCase { $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); } - public function testUpdateStorageNonExisting() { + public function testUpdateStorageNonExisting(): void { $authMech = $this->getAuthMechMock(); $authMech->method('validateStorage') ->willReturn(true); @@ -276,17 +287,17 @@ abstract class StoragesControllerTest extends \Test\TestCase { $this->service->expects($this->once()) ->method('createStorage') - ->will($this->returnValue($storageConfig)); + ->willReturn($storageConfig); $this->service->expects($this->once()) ->method('updateStorage') - ->will($this->throwException(new NotFoundException())); + ->willThrowException(new NotFoundException()); $response = $this->controller->update( 255, 'mount', - '\OCA\Files_External\Lib\Storage\SMB', - '\OCA\Files_External\Lib\Auth\NullMechanism', - array(), + \OCA\Files_External\Lib\Storage\SMB::class, + NullMechanism::class, + [], [], [], [], @@ -296,7 +307,7 @@ abstract class StoragesControllerTest extends \Test\TestCase { $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); } - public function testDeleteStorage() { + public function testDeleteStorage(): void { $this->service->expects($this->once()) ->method('removeStorage'); @@ -304,16 +315,16 @@ abstract class StoragesControllerTest extends \Test\TestCase { $this->assertEquals(Http::STATUS_NO_CONTENT, $response->getStatus()); } - public function testDeleteStorageNonExisting() { + public function testDeleteStorageNonExisting(): void { $this->service->expects($this->once()) ->method('removeStorage') - ->will($this->throwException(new NotFoundException())); + ->willThrowException(new NotFoundException()); $response = $this->controller->destroy(255); $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); } - public function testGetStorage() { + public function testGetStorage(): void { $backend = $this->getBackendMock(); $authMech = $this->getAuthMechMock(); $storageConfig = new StorageConfig(1); @@ -326,14 +337,16 @@ abstract class StoragesControllerTest extends \Test\TestCase { $this->service->expects($this->once()) ->method('getStorage') ->with(1) - ->will($this->returnValue($storageConfig)); + ->willReturn($storageConfig); $response = $this->controller->show(1); $this->assertEquals(Http::STATUS_OK, $response->getStatus()); - $this->assertEquals($storageConfig, $response->getData()); + $expected = $storageConfig->jsonSerialize(); + $expected['can_edit'] = false; + $this->assertEquals($expected, $response->getData()); } - public function validateStorageProvider() { + public static function validateStorageProvider(): array { return [ [true, true, true], [false, true, false], @@ -342,10 +355,8 @@ abstract class StoragesControllerTest extends \Test\TestCase { ]; } - /** - * @dataProvider validateStorageProvider - */ - public function testValidateStorage($backendValidate, $authMechValidate, $expectSuccess) { + #[\PHPUnit\Framework\Attributes\DataProvider('validateStorageProvider')] + public function testValidateStorage(bool $backendValidate, bool $authMechValidate, bool $expectSuccess): void { $backend = $this->getBackendMock(); $backend->method('validateStorage') ->willReturn($backendValidate); @@ -354,7 +365,7 @@ abstract class StoragesControllerTest extends \Test\TestCase { $authMech = $this->getAuthMechMock(); $authMech->method('validateStorage') - ->will($this->returnValue($authMechValidate)); + ->willReturn($authMechValidate); $authMech->method('isVisibleFor') ->willReturn(true); @@ -366,13 +377,13 @@ abstract class StoragesControllerTest extends \Test\TestCase { $this->service->expects($this->once()) ->method('createStorage') - ->will($this->returnValue($storageConfig)); + ->willReturn($storageConfig); if ($expectSuccess) { $this->service->expects($this->once()) ->method('addStorage') ->with($storageConfig) - ->will($this->returnValue($storageConfig)); + ->willReturn($storageConfig); } else { $this->service->expects($this->never()) ->method('addStorage'); @@ -380,9 +391,9 @@ abstract class StoragesControllerTest extends \Test\TestCase { $response = $this->controller->create( 'mount', - '\OCA\Files_External\Lib\Storage\SMB', - '\OCA\Files_External\Lib\Auth\NullMechanism', - array(), + \OCA\Files_External\Lib\Storage\SMB::class, + NullMechanism::class, + [], [], [], [], @@ -395,5 +406,4 @@ abstract class StoragesControllerTest extends \Test\TestCase { $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); } } - } diff --git a/apps/files_external/tests/Controller/UserStoragesControllerTest.php b/apps/files_external/tests/Controller/UserStoragesControllerTest.php index 817c5b4e5c3..3e8d89ec060 100644 --- a/apps/files_external/tests/Controller/UserStoragesControllerTest.php +++ b/apps/files_external/tests/Controller/UserStoragesControllerTest.php @@ -1,66 +1,73 @@ <?php + +declare(strict_types=1); /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * @author Robin Appelman <robin@icewind.nl> - * @author Robin McCorkell <robin@mccorkell.me.uk> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\Files_External\Tests\Controller; -use \OCA\Files_External\Controller\UserStoragesController; +use OC\User\User; +use OCA\Files_External\Controller\UserStoragesController; +use OCA\Files_External\Lib\Storage\SMB; use OCA\Files_External\Lib\StorageConfig; -use \OCP\AppFramework\Http; -use \OCA\Files_External\Service\BackendService; +use OCA\Files_External\Service\BackendService; +use OCA\Files_External\Service\UserStoragesService; +use OCP\AppFramework\Http; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\IConfig; +use OCP\IGroupManager; use OCP\IL10N; -use OCP\ILogger; use OCP\IRequest; use OCP\IUserSession; +use Psr\Log\LoggerInterface; -class UserStoragesControllerTest extends StoragesControllerTest { +class UserStoragesControllerTest extends StoragesControllerTestCase { /** * @var array */ - private $oldAllowedBackends; + private array $oldAllowedBackends; - public function setUp() { + protected function setUp(): void { parent::setUp(); - $this->service = $this->getMockBuilder('\OCA\Files_External\Service\UserStoragesService') - ->disableOriginalConstructor() - ->getMock(); + $this->service = $this->createMock(UserStoragesService::class); $this->service->method('getVisibilityType') ->willReturn(BackendService::VISIBILITY_PERSONAL); - $this->controller = new UserStoragesController( + $this->controller = $this->createController(true); + } + + private function createController(bool $allowCreateLocal = true) { + $session = $this->createMock(IUserSession::class); + $session->method('getUser') + ->willReturn(new User('test', null, $this->createMock(IEventDispatcher::class))); + + $config = $this->createMock(IConfig::class); + $config->method('getSystemValue') + ->with('files_external_allow_create_new_local', true) + ->willReturn($allowCreateLocal); + + return new UserStoragesController( 'files_external', $this->createMock(IRequest::class), $this->createMock(IL10N::class), $this->service, - $this->createMock(IUserSession::class), - $this->createMock(ILogger::class) + $this->createMock(LoggerInterface::class), + $session, + $this->createMock(IGroupManager::class), + $config ); } - public function testAddOrUpdateStorageDisallowedBackend() { + public function testAddLocalStorageWhenDisabled(): void { + $this->controller = $this->createController(false); + parent::testAddLocalStorageWhenDisabled(); + } + + public function testAddOrUpdateStorageDisallowedBackend(): void { $backend = $this->getBackendMock(); $backend->method('isVisibleFor') ->with(BackendService::VISIBILITY_PERSONAL) @@ -75,7 +82,7 @@ class UserStoragesControllerTest extends StoragesControllerTest { $this->service->expects($this->exactly(2)) ->method('createStorage') - ->will($this->returnValue($storageConfig)); + ->willReturn($storageConfig); $this->service->expects($this->never()) ->method('addStorage'); $this->service->expects($this->never()) @@ -83,9 +90,9 @@ class UserStoragesControllerTest extends StoragesControllerTest { $response = $this->controller->create( 'mount', - '\OCA\Files_External\Lib\Storage\SMB', + SMB::class, '\Auth\Mechanism', - array(), + [], [], [], [], @@ -97,9 +104,9 @@ class UserStoragesControllerTest extends StoragesControllerTest { $response = $this->controller->update( 1, 'mount', - '\OCA\Files_External\Lib\Storage\SMB', + SMB::class, '\Auth\Mechanism', - array(), + [], [], [], [], @@ -108,5 +115,4 @@ class UserStoragesControllerTest extends StoragesControllerTest { $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); } - } |