diff options
Diffstat (limited to 'tests/lib/Encryption/DecryptAllTest.php')
-rw-r--r-- | tests/lib/Encryption/DecryptAllTest.php | 137 |
1 files changed, 68 insertions, 69 deletions
diff --git a/tests/lib/Encryption/DecryptAllTest.php b/tests/lib/Encryption/DecryptAllTest.php index f33f88eb214..979e12e03b3 100644 --- a/tests/lib/Encryption/DecryptAllTest.php +++ b/tests/lib/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 Test\Encryption; @@ -26,7 +13,7 @@ use OC\Encryption\Exceptions\DecryptionFailedException; use OC\Encryption\Manager; use OC\Files\FileInfo; use OC\Files\View; -use OCP\Files\Storage; +use OCP\Files\Storage\IStorage; use OCP\IUserManager; use OCP\UserInterface; use Symfony\Component\Console\Formatter\OutputFormatterInterface; @@ -52,16 +39,16 @@ class DecryptAllTest extends TestCase { /** @var \PHPUnit\Framework\MockObject\MockObject | View */ protected $view; - /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Input\InputInterface */ + /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Input\InputInterface */ protected $inputInterface; - /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Output\OutputInterface */ + /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Output\OutputInterface */ protected $outputInterface; - /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\UserInterface */ + /** @var \PHPUnit\Framework\MockObject\MockObject|UserInterface */ protected $userInterface; - /** @var DecryptAll */ + /** @var DecryptAll */ protected $instance; protected function setUp(): void { @@ -77,12 +64,15 @@ class DecryptAllTest extends TestCase { ->disableOriginalConstructor()->getMock(); $this->outputInterface = $this->getMockBuilder(OutputInterface::class) ->disableOriginalConstructor()->getMock(); + $this->outputInterface->expects($this->any())->method('isDecorated') + ->willReturn(false); $this->userInterface = $this->getMockBuilder(UserInterface::class) ->disableOriginalConstructor()->getMock(); /* We need format method to return a string */ $outputFormatter = $this->createMock(OutputFormatterInterface::class); $outputFormatter->method('format')->willReturn('foo'); + $outputFormatter->method('isDecorated')->willReturn(false); $this->outputInterface->expects($this->any())->method('getFormatter') ->willReturn($outputFormatter); @@ -93,7 +83,7 @@ class DecryptAllTest extends TestCase { $this->invokePrivate($this->instance, 'output', [$this->outputInterface]); } - public function dataDecryptAll() { + public static function dataDecryptAll(): array { return [ [true, 'user1', true], [false, 'user1', true], @@ -104,12 +94,12 @@ class DecryptAllTest extends TestCase { } /** - * @dataProvider dataDecryptAll * @param bool $prepareResult * @param string $user * @param bool $userExistsChecked */ - public function testDecryptAll($prepareResult, $user, $userExistsChecked) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataDecryptAll')] + public function testDecryptAll($prepareResult, $user, $userExistsChecked): void { if ($userExistsChecked) { $this->userManager->expects($this->once())->method('userExists')->willReturn(true); } else { @@ -124,7 +114,7 @@ class DecryptAllTest extends TestCase { $this->view ] ) - ->setMethods(['prepareEncryptionModules', 'decryptAllUsersFiles']) + ->onlyMethods(['prepareEncryptionModules', 'decryptAllUsersFiles']) ->getMock(); $instance->expects($this->once()) @@ -146,7 +136,7 @@ class DecryptAllTest extends TestCase { /** * test decrypt all call with a user who doesn't exists */ - public function testDecryptAllWrongUser() { + public function testDecryptAllWrongUser(): void { $this->userManager->expects($this->once())->method('userExists')->willReturn(false); $this->outputInterface->expects($this->once())->method('writeln') ->with('User "user1" does not exist. Please check the username and try again'); @@ -156,7 +146,7 @@ class DecryptAllTest extends TestCase { ); } - public function dataTrueFalse() { + public static function dataTrueFalse(): array { return [ [true], [false], @@ -164,10 +154,10 @@ class DecryptAllTest extends TestCase { } /** - * @dataProvider dataTrueFalse * @param bool $success */ - public function testPrepareEncryptionModules($success) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTrueFalse')] + public function testPrepareEncryptionModules($success): void { $user = 'user1'; $dummyEncryptionModule = $this->getMockBuilder('OCP\Encryption\IEncryptionModule') @@ -196,10 +186,8 @@ class DecryptAllTest extends TestCase { ); } - /** - * @dataProvider dataTestDecryptAllUsersFiles - */ - public function testDecryptAllUsersFiles($user) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestDecryptAllUsersFiles')] + public function testDecryptAllUsersFiles($user): void { /** @var DecryptAll | \PHPUnit\Framework\MockObject\MockObject | $instance */ $instance = $this->getMockBuilder('OC\Encryption\DecryptAll') ->setConstructorArgs( @@ -209,7 +197,7 @@ class DecryptAllTest extends TestCase { $this->view ] ) - ->setMethods(['decryptUsersFiles']) + ->onlyMethods(['decryptUsersFiles']) ->getMock(); $this->invokePrivate($instance, 'input', [$this->inputInterface]); @@ -222,12 +210,16 @@ class DecryptAllTest extends TestCase { $this->userInterface->expects($this->any()) ->method('getUsers') ->willReturn(['user1', 'user2']); + $calls = [ + 'user1', + 'user2', + ]; $instance->expects($this->exactly(2)) ->method('decryptUsersFiles') - ->withConsecutive( - ['user1'], - ['user2'], - ); + ->willReturnCallback(function ($user) use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, $user); + }); } else { $instance->expects($this->once()) ->method('decryptUsersFiles') @@ -237,14 +229,14 @@ class DecryptAllTest extends TestCase { $this->invokePrivate($instance, 'decryptAllUsersFiles', [$user]); } - public function dataTestDecryptAllUsersFiles() { + public static function dataTestDecryptAllUsersFiles(): array { return [ ['user1'], [''] ]; } - public function testDecryptUsersFiles() { + public function testDecryptUsersFiles(): void { /** @var DecryptAll | \PHPUnit\Framework\MockObject\MockObject $instance */ $instance = $this->getMockBuilder('OC\Encryption\DecryptAll') ->setConstructorArgs( @@ -254,35 +246,39 @@ class DecryptAllTest extends TestCase { $this->view ] ) - ->setMethods(['decryptFile']) + ->onlyMethods(['decryptFile']) ->getMock(); - $storage = $this->getMockBuilder(Storage::class) + $storage = $this->getMockBuilder(IStorage::class) ->disableOriginalConstructor()->getMock(); - $sharedStorage = $this->getMockBuilder(Storage::class) + $sharedStorage = $this->getMockBuilder(IStorage::class) ->disableOriginalConstructor()->getMock(); - $sharedStorage->expects($this->once())->method('instanceOfStorage') - ->with('OCA\Files_Sharing\SharedStorage')->willReturn(true); + $sharedStorage->expects($this->once()) + ->method('instanceOfStorage') + ->with('OCA\Files_Sharing\SharedStorage') + ->willReturn(true); $this->view->expects($this->exactly(2)) ->method('getDirectoryContent') - ->withConsecutive( - ['/user1/files'], - ['/user1/files/foo'] - ) - ->willReturnOnConsecutiveCalls( + ->willReturnMap([ [ - new FileInfo('path', $storage, 'intPath', ['name' => 'foo', 'type' => 'dir'], null), - new FileInfo('path', $storage, 'intPath', ['name' => 'bar', 'type' => 'file', 'encrypted' => true], null), - new FileInfo('path', $sharedStorage, 'intPath', ['name' => 'shared', 'type' => 'file', 'encrypted' => true], null), + '/user1/files', '', null, + [ + new FileInfo('path', $storage, 'intPath', ['name' => 'foo', 'type' => 'dir'], null), + new FileInfo('path', $storage, 'intPath', ['name' => 'bar', 'type' => 'file', 'encrypted' => true], null), + new FileInfo('path', $sharedStorage, 'intPath', ['name' => 'shared', 'type' => 'file', 'encrypted' => true], null), + ], ], [ - new FileInfo('path', $storage, 'intPath', ['name' => 'subfile', 'type' => 'file', 'encrypted' => true], null) - ] - ); + '/user1/files/foo', '', null, + [ + new FileInfo('path', $storage, 'intPath', ['name' => 'subfile', 'type' => 'file', 'encrypted' => true], null) + ], + ], + ]); $this->view->expects($this->any())->method('is_dir') ->willReturnCallback( @@ -294,16 +290,21 @@ class DecryptAllTest extends TestCase { } ); + $calls = [ + '/user1/files/bar', + '/user1/files/foo/subfile', + ]; $instance->expects($this->exactly(2)) ->method('decryptFile') - ->withConsecutive( - ['/user1/files/bar'], - ['/user1/files/foo/subfile'], - ); + ->willReturnCallback(function ($path) use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, $path); + }); /* We need format method to return a string */ $outputFormatter = $this->createMock(OutputFormatterInterface::class); + $outputFormatter->method('isDecorated')->willReturn(false); $outputFormatter->method('format')->willReturn('foo'); $output = $this->createMock(OutputInterface::class); @@ -315,10 +316,8 @@ class DecryptAllTest extends TestCase { $this->invokePrivate($instance, 'decryptUsersFiles', ['user1', $progressBar, '']); } - /** - * @dataProvider dataTrueFalse - */ - public function testDecryptFile($isEncrypted) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTrueFalse')] + public function testDecryptFile($isEncrypted): void { $path = 'test.txt'; /** @var DecryptAll | \PHPUnit\Framework\MockObject\MockObject $instance */ @@ -330,7 +329,7 @@ class DecryptAllTest extends TestCase { $this->view ] ) - ->setMethods(['getTimestamp']) + ->onlyMethods(['getTimestamp']) ->getMock(); $fileInfo = $this->createMock(FileInfo::class); @@ -358,7 +357,7 @@ class DecryptAllTest extends TestCase { ); } - public function testDecryptFileFailure() { + public function testDecryptFileFailure(): void { $path = 'test.txt'; /** @var DecryptAll | \PHPUnit\Framework\MockObject\MockObject $instance */ @@ -370,7 +369,7 @@ class DecryptAllTest extends TestCase { $this->view ] ) - ->setMethods(['getTimestamp']) + ->onlyMethods(['getTimestamp']) ->getMock(); @@ -385,7 +384,7 @@ class DecryptAllTest extends TestCase { $this->view->expects($this->once()) ->method('copy') ->with($path, $path . '.decrypted.42') - ->willReturnCallback(function () { + ->willReturnCallback(function (): void { throw new DecryptionFailedException(); }); |