diff options
Diffstat (limited to 'apps/encryption/tests/Controller')
3 files changed, 95 insertions, 200 deletions
diff --git a/apps/encryption/tests/Controller/RecoveryControllerTest.php b/apps/encryption/tests/Controller/RecoveryControllerTest.php index c38436e68e2..0fec3f4d6a9 100644 --- a/apps/encryption/tests/Controller/RecoveryControllerTest.php +++ b/apps/encryption/tests/Controller/RecoveryControllerTest.php @@ -1,72 +1,50 @@ <?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Clark Tomlinson <fallen013@gmail.com> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * - * @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/> - * - */ +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ namespace OCA\Encryption\Tests\Controller; - use OCA\Encryption\Controller\RecoveryController; use OCA\Encryption\Recovery; use OCP\AppFramework\Http; use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class RecoveryControllerTest extends TestCase { - /** @var RecoveryController */ - private $controller; - /** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */ - private $requestMock; - /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */ - private $configMock; - /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */ - private $l10nMock; - /** @var \OCA\Encryption\Recovery|\PHPUnit_Framework_MockObject_MockObject */ - private $recoveryMock; - - public function adminRecoveryProvider() { + protected RecoveryController $controller; + + protected IRequest&MockObject $requestMock; + protected IConfig&MockObject $configMock; + protected IL10N&MockObject $l10nMock; + protected Recovery&MockObject $recoveryMock; + + public static function adminRecoveryProvider(): array { return [ ['test', 'test', '1', 'Recovery key successfully enabled', Http::STATUS_OK], ['', 'test', '1', 'Missing recovery key password', Http::STATUS_BAD_REQUEST], ['test', '', '1', 'Please repeat the recovery key password', Http::STATUS_BAD_REQUEST], - ['test', 'soimething that doesn\'t match', '1', 'Repeated recovery key password does not match the provided recovery key password', Http::STATUS_BAD_REQUEST], + ['test', 'something that doesn\'t match', '1', 'Repeated recovery key password does not match the provided recovery key password', Http::STATUS_BAD_REQUEST], ['test', 'test', '0', 'Recovery key successfully disabled', Http::STATUS_OK], ]; } /** - * @dataProvider adminRecoveryProvider * @param $recoveryPassword * @param $passConfirm * @param $enableRecovery * @param $expectedMessage * @param $expectedStatus */ - public function testAdminRecovery($recoveryPassword, $passConfirm, $enableRecovery, $expectedMessage, $expectedStatus) { - - + #[\PHPUnit\Framework\Attributes\DataProvider('adminRecoveryProvider')] + public function testAdminRecovery($recoveryPassword, $passConfirm, $enableRecovery, $expectedMessage, $expectedStatus): void { $this->recoveryMock->expects($this->any()) ->method('enableAdminRecovery') ->willReturn(true); @@ -82,11 +60,9 @@ class RecoveryControllerTest extends TestCase { $this->assertEquals($expectedMessage, $response->getData()['data']['message']); $this->assertEquals($expectedStatus, $response->getStatus()); - - } - public function changeRecoveryPasswordProvider() { + public static function changeRecoveryPasswordProvider(): array { return [ ['test', 'test', 'oldtestFail', 'Could not change the password. Maybe the old password was not correct.', Http::STATUS_BAD_REQUEST], ['test', 'test', 'oldtest', 'Password successfully changed.', Http::STATUS_OK], @@ -97,21 +73,21 @@ class RecoveryControllerTest extends TestCase { } /** - * @dataProvider changeRecoveryPasswordProvider * @param $password * @param $confirmPassword * @param $oldPassword * @param $expectedMessage * @param $expectedStatus */ - public function testChangeRecoveryPassword($password, $confirmPassword, $oldPassword, $expectedMessage, $expectedStatus) { + #[\PHPUnit\Framework\Attributes\DataProvider('changeRecoveryPasswordProvider')] + public function testChangeRecoveryPassword($password, $confirmPassword, $oldPassword, $expectedMessage, $expectedStatus): void { $this->recoveryMock->expects($this->any()) ->method('changeRecoveryKeyPassword') ->with($password, $oldPassword) - ->will($this->returnValueMap([ + ->willReturnMap([ ['test', 'oldTestFail', false], ['test', 'oldtest', true] - ])); + ]); $response = $this->controller->changeRecoveryPassword($password, $oldPassword, @@ -119,11 +95,9 @@ class RecoveryControllerTest extends TestCase { $this->assertEquals($expectedMessage, $response->getData()['data']['message']); $this->assertEquals($expectedStatus, $response->getStatus()); - - } - public function userSetRecoveryProvider() { + public static function userSetRecoveryProvider(): array { return [ ['1', 'Recovery Key enabled', Http::STATUS_OK], ['0', 'Could not enable the recovery key, please try again or contact your administrator', Http::STATUS_BAD_REQUEST] @@ -131,29 +105,28 @@ class RecoveryControllerTest extends TestCase { } /** - * @dataProvider userSetRecoveryProvider * @param $enableRecovery * @param $expectedMessage * @param $expectedStatus */ - public function testUserSetRecovery($enableRecovery, $expectedMessage, $expectedStatus) { + #[\PHPUnit\Framework\Attributes\DataProvider('userSetRecoveryProvider')] + public function testUserSetRecovery($enableRecovery, $expectedMessage, $expectedStatus): void { $this->recoveryMock->expects($this->any()) ->method('setRecoveryForUser') ->with($enableRecovery) - ->will($this->returnValueMap([ + ->willReturnMap([ ['1', true], ['0', false] - ])); + ]); $response = $this->controller->userSetRecovery($enableRecovery); $this->assertEquals($expectedMessage, $response->getData()['data']['message']); $this->assertEquals($expectedStatus, $response->getStatus()); - } - protected function setUp() { + protected function setUp(): void { parent::setUp(); $this->requestMock = $this->getMockBuilder(IRequest::class) @@ -183,5 +156,4 @@ class RecoveryControllerTest extends TestCase { $this->l10nMock, $this->recoveryMock); } - } diff --git a/apps/encryption/tests/Controller/SettingsControllerTest.php b/apps/encryption/tests/Controller/SettingsControllerTest.php index b12652b51c9..bee20f67cec 100644 --- a/apps/encryption/tests/Controller/SettingsControllerTest.php +++ b/apps/encryption/tests/Controller/SettingsControllerTest.php @@ -1,28 +1,12 @@ <?php + +declare(strict_types=1); + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Björn Schießle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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\Encryption\Tests\Controller; use OCA\Encryption\Controller\SettingsController; @@ -34,44 +18,28 @@ use OCP\AppFramework\Http; use OCP\IL10N; use OCP\IRequest; use OCP\ISession; +use OCP\IUser; use OCP\IUserManager; use OCP\IUserSession; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class SettingsControllerTest extends TestCase { - /** @var SettingsController */ - private $controller; - - /** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */ - private $requestMock; - - /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */ - private $l10nMock; - - /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject */ - private $userManagerMock; - - /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */ - private $userSessionMock; + protected SettingsController $controller; - /** @var \OCA\Encryption\KeyManager|\PHPUnit_Framework_MockObject_MockObject */ - private $keyManagerMock; - - /** @var \OCA\Encryption\Crypto\Crypt|\PHPUnit_Framework_MockObject_MockObject */ - private $cryptMock; - - /** @var \OCA\Encryption\Session|\PHPUnit_Framework_MockObject_MockObject */ - private $sessionMock; - - /** @var \OCP\ISession|\PHPUnit_Framework_MockObject_MockObject */ - private $ocSessionMock; - - /** @var \OCA\Encryption\Util|\PHPUnit_Framework_MockObject_MockObject */ - private $utilMock; - - protected function setUp() { + protected IRequest&MockObject $requestMock; + protected IL10N&MockObject $l10nMock; + protected IUserManager&MockObject $userManagerMock; + protected IUserSession&MockObject $userSessionMock; + protected KeyManager&MockObject $keyManagerMock; + protected Crypt&MockObject $cryptMock; + protected Session&MockObject $sessionMock; + protected IUser&MockObject $user; + protected ISession&MockObject $ocSessionMock; + protected Util&MockObject $utilMock; + protected function setUp(): void { parent::setUp(); $this->requestMock = $this->createMock(IRequest::class); @@ -81,9 +49,9 @@ class SettingsControllerTest extends TestCase { $this->l10nMock->expects($this->any()) ->method('t') - ->will($this->returnCallback(function($message) { + ->willReturnCallback(function ($message) { return $message; - })); + }); $this->userManagerMock = $this->getMockBuilder(IUserManager::class) ->disableOriginalConstructor()->getMock(); @@ -94,28 +62,17 @@ class SettingsControllerTest extends TestCase { $this->cryptMock = $this->getMockBuilder(Crypt::class) ->disableOriginalConstructor()->getMock(); - $this->userSessionMock = $this->getMockBuilder(IUserSession::class) - ->disableOriginalConstructor() - ->setMethods([ - 'isLoggedIn', - 'getUID', - 'login', - 'logout', - 'setUser', - 'getUser', - 'canChangePassword', - ]) - ->getMock(); - $this->ocSessionMock = $this->getMockBuilder(ISession::class)->disableOriginalConstructor()->getMock(); - $this->userSessionMock->expects($this->any()) + $this->user = $this->createMock(IUser::class); + $this->user->expects($this->any()) ->method('getUID') ->willReturn('testUserUid'); + $this->userSessionMock = $this->createMock(IUserSession::class); $this->userSessionMock->expects($this->any()) - ->method($this->anything()) - ->will($this->returnSelf()); + ->method('getUser') + ->willReturn($this->user); $this->sessionMock = $this->getMockBuilder(Session::class) ->disableOriginalConstructor()->getMock(); @@ -141,12 +98,13 @@ class SettingsControllerTest extends TestCase { /** * test updatePrivateKeyPassword() if wrong new password was entered */ - public function testUpdatePrivateKeyPasswordWrongNewPassword() { - + public function testUpdatePrivateKeyPasswordWrongNewPassword(): void { $oldPassword = 'old'; $newPassword = 'new'; - $this->userSessionMock->expects($this->once())->method('getUID')->willReturn('uid'); + $this->user->expects($this->any()) + ->method('getUID') + ->willReturn('uid'); $this->userManagerMock ->expects($this->exactly(2)) @@ -165,8 +123,7 @@ class SettingsControllerTest extends TestCase { /** * test updatePrivateKeyPassword() if wrong old password was entered */ - public function testUpdatePrivateKeyPasswordWrongOldPassword() { - + public function testUpdatePrivateKeyPasswordWrongOldPassword(): void { $oldPassword = 'old'; $newPassword = 'new'; @@ -192,26 +149,22 @@ class SettingsControllerTest extends TestCase { /** * test updatePrivateKeyPassword() with the correct old and new password */ - public function testUpdatePrivateKeyPassword() { - + public function testUpdatePrivateKeyPassword(): void { $oldPassword = 'old'; $newPassword = 'new'; $this->ocSessionMock->expects($this->once()) - ->method('get')->with('loginname')->willReturn('testUser'); + ->method('get') + ->with('loginname') + ->willReturn('testUser'); $this->userManagerMock - ->expects($this->at(0)) - ->method('checkPassword') - ->with('testUserUid', 'new') - ->willReturn(false); - $this->userManagerMock - ->expects($this->at(1)) + ->expects($this->exactly(2)) ->method('checkPassword') - ->with('testUser', 'new') - ->willReturn(true); - - + ->willReturnMap([ + ['testUserUid', 'new', false], + ['testUser', 'new', true], + ]); $this->cryptMock ->expects($this->once()) @@ -253,10 +206,9 @@ class SettingsControllerTest extends TestCase { $data['message']); } - function testSetEncryptHomeStorage() { + public function testSetEncryptHomeStorage(): void { $value = true; $this->utilMock->expects($this->once())->method('setEncryptHomeStorage')->with($value); $this->controller->setEncryptHomeStorage($value); } - } diff --git a/apps/encryption/tests/Controller/StatusControllerTest.php b/apps/encryption/tests/Controller/StatusControllerTest.php index 0dc04b0ba1c..1bbcad77411 100644 --- a/apps/encryption/tests/Controller/StatusControllerTest.php +++ b/apps/encryption/tests/Controller/StatusControllerTest.php @@ -1,59 +1,32 @@ <?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Björn Schießle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/> - * - */ +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ namespace OCA\Encryption\Tests\Controller; - use OCA\Encryption\Controller\StatusController; use OCA\Encryption\Session; use OCP\Encryption\IManager; use OCP\IL10N; use OCP\IRequest; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class StatusControllerTest extends TestCase { - /** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */ - private $requestMock; + protected IRequest&MockObject $requestMock; + protected IL10N&MockObject $l10nMock; + protected Session&MockObject $sessionMock; + protected IManager&MockObject $encryptionManagerMock; - /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */ - private $l10nMock; - - /** @var \OCA\Encryption\Session | \PHPUnit_Framework_MockObject_MockObject */ - protected $sessionMock; - - /** @var IManager | \PHPUnit_Framework_MockObject_MockObject */ - protected $encryptionManagerMock; - - /** @var StatusController */ - protected $controller; - - protected function setUp() { + protected StatusController $controller; + protected function setUp(): void { parent::setUp(); $this->sessionMock = $this->getMockBuilder(Session::class) @@ -64,9 +37,9 @@ class StatusControllerTest extends TestCase { ->disableOriginalConstructor()->getMock(); $this->l10nMock->expects($this->any()) ->method('t') - ->will($this->returnCallback(function($message) { + ->willReturnCallback(function ($message) { return $message; - })); + }); $this->encryptionManagerMock = $this->createMock(IManager::class); $this->controller = new StatusController('encryptionTest', @@ -74,16 +47,15 @@ class StatusControllerTest extends TestCase { $this->l10nMock, $this->sessionMock, $this->encryptionManagerMock); - } /** - * @dataProvider dataTestGetStatus * * @param string $status * @param string $expectedStatus */ - public function testGetStatus($status, $expectedStatus) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetStatus')] + public function testGetStatus($status, $expectedStatus): void { $this->sessionMock->expects($this->once()) ->method('getStatus')->willReturn($status); $result = $this->controller->getStatus(); @@ -91,13 +63,12 @@ class StatusControllerTest extends TestCase { $this->assertSame($expectedStatus, $data['status']); } - public function dataTestGetStatus() { - return array( - array(Session::RUN_MIGRATION, 'interactionNeeded'), - array(Session::INIT_EXECUTED, 'interactionNeeded'), - array(Session::INIT_SUCCESSFUL, 'success'), - array(Session::NOT_INITIALIZED, 'interactionNeeded'), - array('unknown', 'error'), - ); + public static function dataTestGetStatus(): array { + return [ + [Session::INIT_EXECUTED, 'interactionNeeded'], + [Session::INIT_SUCCESSFUL, 'success'], + [Session::NOT_INITIALIZED, 'interactionNeeded'], + ['unknown', 'error'], + ]; } } |