diff options
Diffstat (limited to 'tests/Core/Command/Encryption')
-rw-r--r-- | tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php | 93 | ||||
-rw-r--r-- | tests/Core/Command/Encryption/DecryptAllTest.php | 90 | ||||
-rw-r--r-- | tests/Core/Command/Encryption/DisableTest.php | 29 | ||||
-rw-r--r-- | tests/Core/Command/Encryption/EnableTest.php | 81 | ||||
-rw-r--r-- | tests/Core/Command/Encryption/EncryptAllTest.php | 93 | ||||
-rw-r--r-- | tests/Core/Command/Encryption/SetDefaultModuleTest.php | 46 |
6 files changed, 162 insertions, 270 deletions
diff --git a/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php b/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php index 1d5e2ac420d..0bc6cbb64cf 100644 --- a/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php +++ b/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php @@ -1,27 +1,15 @@ <?php + /** - * @author Björn Schießle <schiessle@owncloud.com> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @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 Tests\Core\Command\Encryption; use OC\Core\Command\Encryption\ChangeKeyStorageRoot; +use OC\Encryption\Keys\Storage; use OC\Encryption\Util; use OC\Files\View; use OCP\IConfig; @@ -46,7 +34,7 @@ class ChangeKeyStorageRootTest extends TestCase { /** @var IConfig | \PHPUnit\Framework\MockObject\MockObject */ protected $config; - /** @var Util | \PHPUnit\Framework\MockObject\MockObject */ + /** @var Util | \PHPUnit\Framework\MockObject\MockObject */ protected $util; /** @var QuestionHelper | \PHPUnit\Framework\MockObject\MockObject */ @@ -58,7 +46,7 @@ class ChangeKeyStorageRootTest extends TestCase { /** @var OutputInterface | \PHPUnit\Framework\MockObject\MockObject */ protected $outputInterface; - /** @var \OCP\UserInterface | \PHPUnit\Framework\MockObject\MockObject */ + /** @var UserInterface|\PHPUnit\Framework\MockObject\MockObject */ protected $userInterface; protected function setUp(): void { @@ -75,6 +63,7 @@ class ChangeKeyStorageRootTest extends TestCase { /* We need format method to return a string */ $outputFormatter = $this->createMock(OutputFormatterInterface::class); + $outputFormatter->method('isDecorated')->willReturn(false); $outputFormatter->method('format')->willReturnArgument(0); $this->outputInterface->expects($this->any())->method('getFormatter') @@ -89,10 +78,8 @@ class ChangeKeyStorageRootTest extends TestCase { ); } - /** - * @dataProvider dataTestExecute - */ - public function testExecute($newRoot, $answer, $successMoveKey) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestExecute')] + public function testExecute($newRoot, $answer, $successMoveKey): void { $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') ->setConstructorArgs( [ @@ -102,7 +89,7 @@ class ChangeKeyStorageRootTest extends TestCase { $this->util, $this->questionHelper ] - )->setMethods(['moveAllKeys'])->getMock(); + )->onlyMethods(['moveAllKeys'])->getMock(); $this->util->expects($this->once())->method('getKeyStorageRoot') ->willReturn(''); @@ -135,7 +122,7 @@ class ChangeKeyStorageRootTest extends TestCase { ); } - public function dataTestExecute() { + public static function dataTestExecute(): array { return [ [null, true, true], [null, true, false], @@ -145,8 +132,8 @@ class ChangeKeyStorageRootTest extends TestCase { ]; } - public function testMoveAllKeys() { - /** @var \OC\Core\Command\Encryption\ChangeKeyStorageRoot $changeKeyStorageRoot */ + public function testMoveAllKeys(): void { + /** @var ChangeKeyStorageRoot $changeKeyStorageRoot */ $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') ->setConstructorArgs( [ @@ -156,33 +143,33 @@ class ChangeKeyStorageRootTest extends TestCase { $this->util, $this->questionHelper ] - )->setMethods(['prepareNewRoot', 'moveSystemKeys', 'moveUserKeys'])->getMock(); + )->onlyMethods(['prepareNewRoot', 'moveSystemKeys', 'moveUserKeys'])->getMock(); - $changeKeyStorageRoot->expects($this->at(0))->method('prepareNewRoot')->with('newRoot'); - $changeKeyStorageRoot->expects($this->at(1))->method('moveSystemKeys')->with('oldRoot', 'newRoot'); - $changeKeyStorageRoot->expects($this->at(2))->method('moveUserKeys')->with('oldRoot', 'newRoot', $this->outputInterface); + $changeKeyStorageRoot->expects($this->once())->method('prepareNewRoot')->with('newRoot'); + $changeKeyStorageRoot->expects($this->once())->method('moveSystemKeys')->with('oldRoot', 'newRoot'); + $changeKeyStorageRoot->expects($this->once())->method('moveUserKeys')->with('oldRoot', 'newRoot', $this->outputInterface); $this->invokePrivate($changeKeyStorageRoot, 'moveAllKeys', ['oldRoot', 'newRoot', $this->outputInterface]); } - public function testPrepareNewRoot() { + public function testPrepareNewRoot(): void { $this->view->expects($this->once())->method('is_dir')->with('newRoot') ->willReturn(true); $this->view->expects($this->once())->method('file_put_contents') - ->with('newRoot/' . \OC\Encryption\Keys\Storage::KEY_STORAGE_MARKER, + ->with('newRoot/' . Storage::KEY_STORAGE_MARKER, 'Nextcloud will detect this folder as key storage root only if this file exists')->willReturn(true); $this->invokePrivate($this->changeKeyStorageRoot, 'prepareNewRoot', ['newRoot']); } /** - * @dataProvider dataTestPrepareNewRootException * * @param bool $dirExists * @param bool $couldCreateFile */ - public function testPrepareNewRootException($dirExists, $couldCreateFile) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestPrepareNewRootException')] + public function testPrepareNewRootException($dirExists, $couldCreateFile): void { $this->expectException(\Exception::class); $this->view->expects($this->once())->method('is_dir')->with('newRoot') @@ -192,7 +179,7 @@ class ChangeKeyStorageRootTest extends TestCase { $this->invokePrivate($this->changeKeyStorageRoot, 'prepareNewRoot', ['newRoot']); } - public function dataTestPrepareNewRootException() { + public static function dataTestPrepareNewRootException(): array { return [ [true, false], [true, null], @@ -201,13 +188,13 @@ class ChangeKeyStorageRootTest extends TestCase { } /** - * @dataProvider dataTestMoveSystemKeys * * @param bool $dirExists * @param bool $targetExists * @param bool $executeRename */ - public function testMoveSystemKeys($dirExists, $targetExists, $executeRename) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestMoveSystemKeys')] + public function testMoveSystemKeys($dirExists, $targetExists, $executeRename): void { $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') ->setConstructorArgs( [ @@ -217,7 +204,7 @@ class ChangeKeyStorageRootTest extends TestCase { $this->util, $this->questionHelper ] - )->setMethods(['targetExists'])->getMock(); + )->onlyMethods(['targetExists'])->getMock(); $this->view->expects($this->once())->method('is_dir') ->with('oldRoot/files_encryption')->willReturn($dirExists); @@ -226,7 +213,7 @@ class ChangeKeyStorageRootTest extends TestCase { if ($executeRename) { $this->view->expects($this->once())->method('rename') - ->with('oldRoot/files_encryption', 'newRoot/files_encryption'); + ->with('oldRoot/files_encryption', 'newRoot/files_encryption'); } else { $this->view->expects($this->never())->method('rename'); } @@ -234,7 +221,7 @@ class ChangeKeyStorageRootTest extends TestCase { $this->invokePrivate($changeKeyStorageRoot, 'moveSystemKeys', ['oldRoot', 'newRoot']); } - public function dataTestMoveSystemKeys() { + public static function dataTestMoveSystemKeys(): array { return [ [true, false, true], [false, true, false], @@ -244,7 +231,7 @@ class ChangeKeyStorageRootTest extends TestCase { } - public function testMoveUserKeys() { + public function testMoveUserKeys(): void { $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') ->setConstructorArgs( [ @@ -254,7 +241,7 @@ class ChangeKeyStorageRootTest extends TestCase { $this->util, $this->questionHelper ] - )->setMethods(['setupUserFS', 'moveUserEncryptionFolder'])->getMock(); + )->onlyMethods(['setupUserFS', 'moveUserEncryptionFolder'])->getMock(); $this->userManager->expects($this->once())->method('getBackends') ->willReturn([$this->userInterface]); @@ -267,14 +254,14 @@ class ChangeKeyStorageRootTest extends TestCase { } /** - * @dataProvider dataTestMoveUserEncryptionFolder * * @param bool $userExists * @param bool $isDir * @param bool $targetExists * @param bool $shouldRename */ - public function testMoveUserEncryptionFolder($userExists, $isDir, $targetExists, $shouldRename) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestMoveUserEncryptionFolder')] + public function testMoveUserEncryptionFolder($userExists, $isDir, $targetExists, $shouldRename): void { $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') ->setConstructorArgs( [ @@ -284,7 +271,7 @@ class ChangeKeyStorageRootTest extends TestCase { $this->util, $this->questionHelper ] - )->setMethods(['targetExists', 'prepareParentFolder'])->getMock(); + )->onlyMethods(['targetExists', 'prepareParentFolder'])->getMock(); $this->userManager->expects($this->once())->method('userExists') ->willReturn($userExists); @@ -306,7 +293,7 @@ class ChangeKeyStorageRootTest extends TestCase { $this->invokePrivate($changeKeyStorageRoot, 'moveUserEncryptionFolder', ['user1', 'oldRoot', 'newRoot']); } - public function dataTestMoveUserEncryptionFolder() { + public static function dataTestMoveUserEncryptionFolder(): array { return [ [true, true, false, true], [true, false, true, false], @@ -319,10 +306,8 @@ class ChangeKeyStorageRootTest extends TestCase { } - /** - * @dataProvider dataTestPrepareParentFolder - */ - public function testPrepareParentFolder($path, $pathExists) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestPrepareParentFolder')] + public function testPrepareParentFolder($path, $pathExists): void { $this->view->expects($this->any())->method('file_exists') ->willReturnCallback( function ($fileExistsPath) use ($path, $pathExists) { @@ -347,14 +332,14 @@ class ChangeKeyStorageRootTest extends TestCase { ); } - public function dataTestPrepareParentFolder() { + public static function dataTestPrepareParentFolder(): array { return [ ['/user/folder/sub_folder/keystorage', true], ['/user/folder/sub_folder/keystorage', false] ]; } - public function testTargetExists() { + public function testTargetExists(): void { $this->view->expects($this->once())->method('file_exists')->with('path') ->willReturn(false); @@ -364,7 +349,7 @@ class ChangeKeyStorageRootTest extends TestCase { } - public function testTargetExistsException() { + public function testTargetExistsException(): void { $this->expectException(\Exception::class); $this->view->expects($this->once())->method('file_exists')->with('path') diff --git a/tests/Core/Command/Encryption/DecryptAllTest.php b/tests/Core/Command/Encryption/DecryptAllTest.php index c78500fd9d8..41d9e4c713f 100644 --- a/tests/Core/Command/Encryption/DecryptAllTest.php +++ b/tests/Core/Command/Encryption/DecryptAllTest.php @@ -1,22 +1,9 @@ <?php + /** - * @author Björn Schießle <schiessle@owncloud.com> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @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 Tests\Core\Command\Encryption; @@ -31,16 +18,16 @@ use Symfony\Component\Console\Output\OutputInterface; use Test\TestCase; class DecryptAllTest extends TestCase { - /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IConfig */ + /** @var \PHPUnit\Framework\MockObject\MockObject|IConfig */ protected $config; - /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Encryption\IManager */ + /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Encryption\IManager */ protected $encryptionManager; - /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\App\IAppManager */ + /** @var \PHPUnit\Framework\MockObject\MockObject|IAppManager */ protected $appManager; - /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Input\InputInterface */ + /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Input\InputInterface */ protected $consoleInput; /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Output\OutputInterface */ @@ -84,19 +71,22 @@ class DecryptAllTest extends TestCase { ->with('files_trashbin')->willReturn(true); } - public function testMaintenanceAndTrashbin() { + public function testMaintenanceAndTrashbin(): void { // on construct we enable single-user-mode and disable the trash bin - $this->config->expects($this->at(1)) + // on destruct we disable single-user-mode again and enable the trash bin + $calls = [ + ['maintenance', true], + ['maintenance', false], + ]; + $this->config->expects($this->exactly(2)) ->method('setSystemValue') - ->with('maintenance', true); + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->appManager->expects($this->once()) ->method('disableApp') ->with('files_trashbin'); - - // on destruct wi disable single-user-mode again and enable the trash bin - $this->config->expects($this->at(2)) - ->method('setSystemValue') - ->with('maintenance', false); $this->appManager->expects($this->once()) ->method('enableApp') ->with('files_trashbin'); @@ -120,10 +110,8 @@ class DecryptAllTest extends TestCase { $this->invokePrivate($instance, 'resetMaintenanceAndTrashbin'); } - /** - * @dataProvider dataTestExecute - */ - public function testExecute($encryptionEnabled, $continue) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestExecute')] + public function testExecute($encryptionEnabled, $continue): void { $instance = new DecryptAll( $this->encryptionManager, $this->appManager, @@ -142,9 +130,16 @@ class DecryptAllTest extends TestCase { ->willReturn('user1'); if ($encryptionEnabled) { - $this->config->expects($this->at(1)) + $calls = [ + ['core', 'encryption_enabled', 'no'], + ['core', 'encryption_enabled', 'yes'], + ]; + $this->config->expects($this->exactly(2)) ->method('setAppValue') - ->with('core', 'encryption_enabled', 'no'); + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->questionHelper->expects($this->once()) ->method('ask') ->willReturn($continue); @@ -154,9 +149,6 @@ class DecryptAllTest extends TestCase { ->with($this->consoleInput, $this->consoleOutput, 'user1'); } else { $this->decryptAll->expects($this->never())->method('decryptAll'); - $this->config->expects($this->at(2)) - ->method('setAppValue') - ->with('core', 'encryption_enabled', 'yes'); } } else { $this->config->expects($this->never())->method('setAppValue'); @@ -167,7 +159,7 @@ class DecryptAllTest extends TestCase { $this->invokePrivate($instance, 'execute', [$this->consoleInput, $this->consoleOutput]); } - public function dataTestExecute() { + public static function dataTestExecute(): array { return [ [true, true], [true, false], @@ -177,7 +169,7 @@ class DecryptAllTest extends TestCase { } - public function testExecuteFailure() { + public function testExecuteFailure(): void { $this->expectException(\Exception::class); $instance = new DecryptAll( @@ -188,15 +180,17 @@ class DecryptAllTest extends TestCase { $this->questionHelper ); - $this->config->expects($this->at(1)) - ->method('setAppValue') - ->with('core', 'encryption_enabled', 'no'); - // make sure that we enable encryption again after a exception was thrown - $this->config->expects($this->at(4)) + $calls = [ + ['core', 'encryption_enabled', 'no'], + ['core', 'encryption_enabled', 'yes'], + ]; + $this->config->expects($this->exactly(2)) ->method('setAppValue') - ->with('core', 'encryption_enabled', 'yes'); - + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->encryptionManager->expects($this->once()) ->method('isEnabled') ->willReturn(true); @@ -213,7 +207,7 @@ class DecryptAllTest extends TestCase { $this->decryptAll->expects($this->once()) ->method('decryptAll') ->with($this->consoleInput, $this->consoleOutput, 'user1') - ->willReturnCallback(function () { + ->willReturnCallback(function (): void { throw new \Exception(); }); diff --git a/tests/Core/Command/Encryption/DisableTest.php b/tests/Core/Command/Encryption/DisableTest.php index bd29caafec3..a89fd636e47 100644 --- a/tests/Core/Command/Encryption/DisableTest.php +++ b/tests/Core/Command/Encryption/DisableTest.php @@ -1,22 +1,9 @@ <?php + /** - * @author Joas Schilling <nickvergessen@owncloud.com> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @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 Tests\Core\Command\Encryption; @@ -47,12 +34,12 @@ class DisableTest extends TestCase { $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); - /** @var \OCP\IConfig $config */ + /** @var IConfig $config */ $this->command = new Disable($config); } - public function dataDisable() { + public static function dataDisable(): array { return [ ['yes', true, 'Encryption disabled'], ['no', false, 'Encryption is already disabled'], @@ -60,13 +47,13 @@ class DisableTest extends TestCase { } /** - * @dataProvider dataDisable * * @param string $oldStatus * @param bool $isUpdating * @param string $expectedString */ - public function testDisable($oldStatus, $isUpdating, $expectedString) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataDisable')] + public function testDisable($oldStatus, $isUpdating, $expectedString): void { $this->config->expects($this->once()) ->method('getAppValue') ->with('core', 'encryption_enabled', $this->anything()) diff --git a/tests/Core/Command/Encryption/EnableTest.php b/tests/Core/Command/Encryption/EnableTest.php index c1656054ecd..32d1a7576f5 100644 --- a/tests/Core/Command/Encryption/EnableTest.php +++ b/tests/Core/Command/Encryption/EnableTest.php @@ -1,22 +1,9 @@ <?php + /** - * @author Joas Schilling <nickvergessen@owncloud.com> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @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 Tests\Core\Command\Encryption; @@ -59,7 +46,7 @@ class EnableTest extends TestCase { } - public function dataEnable() { + public static function dataEnable(): array { return [ ['no', null, [], true, 'Encryption enabled', 'No encryption module is loaded'], ['yes', null, [], false, 'Encryption is already enabled', 'No encryption module is loaded'], @@ -69,53 +56,45 @@ class EnableTest extends TestCase { ]; } - /** - * @dataProvider dataEnable - * - * @param string $oldStatus - * @param string $defaultModule - * @param array $availableModules - * @param bool $isUpdating - * @param string $expectedString - * @param string $expectedDefaultModuleString - */ - public function testEnable($oldStatus, $defaultModule, $availableModules, $isUpdating, $expectedString, $expectedDefaultModuleString) { - $invokeCount = 0; - $this->config->expects($this->at($invokeCount)) - ->method('getAppValue') - ->with('core', 'encryption_enabled', $this->anything()) - ->willReturn($oldStatus); - $invokeCount++; - + #[\PHPUnit\Framework\Attributes\DataProvider('dataEnable')] + public function testEnable(string $oldStatus, ?string $defaultModule, array $availableModules, bool $isUpdating, string $expectedString, string $expectedDefaultModuleString): void { if ($isUpdating) { $this->config->expects($this->once()) ->method('setAppValue') ->with('core', 'encryption_enabled', 'yes'); - $invokeCount++; } $this->manager->expects($this->atLeastOnce()) ->method('getEncryptionModules') ->willReturn($availableModules); - if (!empty($availableModules)) { - $this->config->expects($this->at($invokeCount)) + if (empty($availableModules)) { + $this->config->expects($this->once()) ->method('getAppValue') - ->with('core', 'default_encryption_module', $this->anything()) - ->willReturn($defaultModule); + ->willReturnMap([ + ['core', 'encryption_enabled', 'no', $oldStatus], + ]); + } else { + $this->config->expects($this->exactly(2)) + ->method('getAppValue') + ->willReturnMap([ + ['core', 'encryption_enabled', 'no', $oldStatus], + ['core', 'default_encryption_module', null, $defaultModule], + ]); } - $this->consoleOutput->expects($this->at(0)) - ->method('writeln') - ->with($this->stringContains($expectedString)); - - $this->consoleOutput->expects($this->at(1)) - ->method('writeln') - ->with(''); - - $this->consoleOutput->expects($this->at(2)) + $calls = [ + [$expectedString, 0], + ['', 0], + [$expectedDefaultModuleString, 0], + ]; + $this->consoleOutput->expects($this->exactly(3)) ->method('writeln') - ->with($this->stringContains($expectedDefaultModuleString)); + ->willReturnCallback(function (string $message, int $level) use (&$calls): void { + $call = array_shift($calls); + $this->assertStringContainsString($call[0], $message); + $this->assertSame($call[1], $level); + }); self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); } diff --git a/tests/Core/Command/Encryption/EncryptAllTest.php b/tests/Core/Command/Encryption/EncryptAllTest.php index 1190a98843f..15cbe83737d 100644 --- a/tests/Core/Command/Encryption/EncryptAllTest.php +++ b/tests/Core/Command/Encryption/EncryptAllTest.php @@ -1,22 +1,9 @@ <?php + /** - * @author Björn Schießle <schiessle@owncloud.com> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @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 Tests\Core\Command\Encryption; @@ -26,80 +13,50 @@ use OCP\App\IAppManager; use OCP\Encryption\IEncryptionModule; use OCP\Encryption\IManager; use OCP\IConfig; +use PHPUnit\Framework\MockObject\MockObject; use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Test\TestCase; class EncryptAllTest extends TestCase { - /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IConfig */ - protected $config; - - /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Encryption\IManager */ - protected $encryptionManager; - - /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\App\IAppManager */ - protected $appManager; - - /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Input\InputInterface */ - protected $consoleInput; - - /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Output\OutputInterface */ - protected $consoleOutput; - - /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Helper\QuestionHelper */ - protected $questionHelper; - - /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Encryption\IEncryptionModule */ - protected $encryptionModule; + private IConfig&MockObject $config; + private IManager&MockObject $encryptionManager; + private IAppManager&MockObject $appManager; + private InputInterface&MockObject $consoleInput; + private OutputInterface&MockObject $consoleOutput; + private QuestionHelper&MockObject $questionHelper; + private IEncryptionModule&MockObject $encryptionModule; - /** @var EncryptAll */ - protected $command; + private EncryptAll $command; protected function setUp(): void { parent::setUp(); - $this->config = $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock(); - $this->encryptionManager = $this->getMockBuilder(IManager::class) - ->disableOriginalConstructor() - ->getMock(); - $this->appManager = $this->getMockBuilder(IAppManager::class) - ->disableOriginalConstructor() - ->getMock(); - $this->encryptionModule = $this->getMockBuilder(IEncryptionModule::class) - ->disableOriginalConstructor() - ->getMock(); - $this->questionHelper = $this->getMockBuilder(QuestionHelper::class) - ->disableOriginalConstructor() - ->getMock(); - $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); + $this->config = $this->createMock(IConfig::class); + $this->encryptionManager = $this->createMock(IManager::class); + $this->appManager = $this->createMock(IAppManager::class); + $this->encryptionModule = $this->createMock(IEncryptionModule::class); + $this->questionHelper = $this->createMock(QuestionHelper::class); + $this->consoleInput = $this->createMock(InputInterface::class); $this->consoleInput->expects($this->any()) ->method('isInteractive') ->willReturn(true); - $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); + $this->consoleOutput = $this->createMock(OutputInterface::class); } - public function testEncryptAll() { + public function testEncryptAll(): void { // trash bin needs to be disabled in order to avoid adding dummy files to the users // trash bin which gets deleted during the encryption process $this->appManager->expects($this->once())->method('disableApp')->with('files_trashbin'); - // enable single user mode to avoid that other user login during encryption - // destructor should disable the single user mode again - $this->config->expects($this->once())->method('getSystemValueBool')->with('maintenance', false)->willReturn(false); - $this->config->expects($this->at(1))->method('setSystemValue')->with('maintenance', true); - $this->config->expects($this->at(2))->method('setSystemValue')->with('maintenance', false); $instance = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper); $this->invokePrivate($instance, 'forceMaintenanceAndTrashbin'); $this->invokePrivate($instance, 'resetMaintenanceAndTrashbin'); } - /** - * @dataProvider dataTestExecute - */ - public function testExecute($answer, $askResult) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestExecute')] + public function testExecute($answer, $askResult): void { $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper); $this->encryptionManager->expects($this->once())->method('isEnabled')->willReturn(true); @@ -118,14 +75,14 @@ class EncryptAllTest extends TestCase { $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]); } - public function dataTestExecute() { + public static function dataTestExecute(): array { return [ ['y', true], ['Y', true], ['n', false], ['N', false], ['', false] ]; } - public function testExecuteException() { + public function testExecuteException(): void { $this->expectException(\Exception::class); $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper); diff --git a/tests/Core/Command/Encryption/SetDefaultModuleTest.php b/tests/Core/Command/Encryption/SetDefaultModuleTest.php index 015964e1357..df38d730db3 100644 --- a/tests/Core/Command/Encryption/SetDefaultModuleTest.php +++ b/tests/Core/Command/Encryption/SetDefaultModuleTest.php @@ -1,22 +1,9 @@ <?php + /** - * @author Joas Schilling <nickvergessen@owncloud.com> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @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 Tests\Core\Command\Encryption; @@ -57,7 +44,7 @@ class SetDefaultModuleTest extends TestCase { } - public function dataSetDefaultModule() { + public static function dataSetDefaultModule(): array { return [ ['ID0', 'ID0', null, null, 'already'], ['ID0', 'ID1', 'ID1', true, 'info'], @@ -66,7 +53,6 @@ class SetDefaultModuleTest extends TestCase { } /** - * @dataProvider dataSetDefaultModule * * @param string $oldModule * @param string $newModule @@ -74,7 +60,8 @@ class SetDefaultModuleTest extends TestCase { * @param bool $updateSuccess * @param string $expectedString */ - public function testSetDefaultModule($oldModule, $newModule, $updateModule, $updateSuccess, $expectedString) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataSetDefaultModule')] + public function testSetDefaultModule($oldModule, $newModule, $updateModule, $updateSuccess, $expectedString): void { $this->consoleInput->expects($this->once()) ->method('getArgument') ->with('module') @@ -104,7 +91,6 @@ class SetDefaultModuleTest extends TestCase { } /** - * @dataProvider dataSetDefaultModule * * @param string $oldModule * @param string $newModule @@ -112,7 +98,8 @@ class SetDefaultModuleTest extends TestCase { * @param bool $updateSuccess * @param string $expectedString */ - public function testMaintenanceMode($oldModule, $newModule, $updateModule, $updateSuccess, $expectedString) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataSetDefaultModule')] + public function testMaintenanceMode($oldModule, $newModule, $updateModule, $updateSuccess, $expectedString): void { $this->consoleInput->expects($this->never()) ->method('getArgument') ->with('module') @@ -127,13 +114,16 @@ class SetDefaultModuleTest extends TestCase { ->with('maintenance', false) ->willReturn(true); - $this->consoleOutput->expects($this->at(0)) - ->method('writeln') - ->with($this->stringContains('Maintenance mode must be disabled when setting default module,')); - - $this->consoleOutput->expects($this->at(1)) + $calls = [ + 'Maintenance mode must be disabled when setting default module,', + 'in order to load the relevant encryption modules correctly.', + ]; + $this->consoleOutput->expects($this->exactly(2)) ->method('writeln') - ->with($this->stringContains('in order to load the relevant encryption modules correctly.')); + ->willReturnCallback(function ($message) use (&$calls): void { + $expected = array_shift($calls); + $this->assertStringContainsString($expected, $message); + }); self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); } |