diff options
Diffstat (limited to 'apps/encryption/tests/Controller')
3 files changed, 55 insertions, 82 deletions
diff --git a/apps/encryption/tests/Controller/RecoveryControllerTest.php b/apps/encryption/tests/Controller/RecoveryControllerTest.php index 955e9c7c964..0fec3f4d6a9 100644 --- a/apps/encryption/tests/Controller/RecoveryControllerTest.php +++ b/apps/encryption/tests/Controller/RecoveryControllerTest.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + /** * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -13,39 +15,36 @@ 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); @@ -63,7 +62,7 @@ class RecoveryControllerTest extends TestCase { $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], @@ -74,14 +73,14 @@ 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) @@ -98,7 +97,7 @@ class RecoveryControllerTest extends TestCase { $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] @@ -106,12 +105,12 @@ 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) diff --git a/apps/encryption/tests/Controller/SettingsControllerTest.php b/apps/encryption/tests/Controller/SettingsControllerTest.php index ee49972001c..bee20f67cec 100644 --- a/apps/encryption/tests/Controller/SettingsControllerTest.php +++ b/apps/encryption/tests/Controller/SettingsControllerTest.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -24,37 +26,18 @@ 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; - - /** @var \OCA\Encryption\KeyManager|\PHPUnit\Framework\MockObject\MockObject */ - private $keyManagerMock; + protected SettingsController $controller; - /** @var \OCA\Encryption\Crypto\Crypt|\PHPUnit\Framework\MockObject\MockObject */ - private $cryptMock; - - /** @var \OCA\Encryption\Session|\PHPUnit\Framework\MockObject\MockObject */ - private $sessionMock; - /** @var MockObject|IUser */ - private $user; - - /** @var \OCP\ISession|\PHPUnit\Framework\MockObject\MockObject */ - private $ocSessionMock; - - /** @var \OCA\Encryption\Util|\PHPUnit\Framework\MockObject\MockObject */ - private $utilMock; + 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(); @@ -115,7 +98,7 @@ class SettingsControllerTest extends TestCase { /** * test updatePrivateKeyPassword() if wrong new password was entered */ - public function testUpdatePrivateKeyPasswordWrongNewPassword() { + public function testUpdatePrivateKeyPasswordWrongNewPassword(): void { $oldPassword = 'old'; $newPassword = 'new'; @@ -140,7 +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'; @@ -166,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->exactly(2)) ->method('checkPassword') - ->withConsecutive( - ['testUserUid', 'new'], - ['testUser', 'new'], - ) - ->willReturnOnConsecutiveCalls( - false, - true, - ); - - + ->willReturnMap([ + ['testUserUid', 'new', false], + ['testUser', 'new', true], + ]); $this->cryptMock ->expects($this->once()) @@ -227,7 +206,7 @@ class SettingsControllerTest extends TestCase { $data['message']); } - public 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 3c59a80fe15..1bbcad77411 100644 --- a/apps/encryption/tests/Controller/StatusControllerTest.php +++ b/apps/encryption/tests/Controller/StatusControllerTest.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -12,24 +14,17 @@ 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; - - /** @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; + protected IRequest&MockObject $requestMock; + protected IL10N&MockObject $l10nMock; + protected Session&MockObject $sessionMock; + protected IManager&MockObject $encryptionManagerMock; - /** @var StatusController */ - protected $controller; + protected StatusController $controller; protected function setUp(): void { parent::setUp(); @@ -55,12 +50,12 @@ class StatusControllerTest extends TestCase { } /** - * @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(); @@ -68,7 +63,7 @@ class StatusControllerTest extends TestCase { $this->assertSame($expectedStatus, $data['status']); } - public function dataTestGetStatus() { + public static function dataTestGetStatus(): array { return [ [Session::INIT_EXECUTED, 'interactionNeeded'], [Session::INIT_SUCCESSFUL, 'success'], |