diff options
Diffstat (limited to 'tests/lib/Encryption')
-rw-r--r-- | tests/lib/Encryption/DecryptAllTest.php | 70 | ||||
-rw-r--r-- | tests/lib/Encryption/Keys/StorageTest.php | 38 |
2 files changed, 61 insertions, 47 deletions
diff --git a/tests/lib/Encryption/DecryptAllTest.php b/tests/lib/Encryption/DecryptAllTest.php index 6a8453bcaf8..7385ff03785 100644 --- a/tests/lib/Encryption/DecryptAllTest.php +++ b/tests/lib/Encryption/DecryptAllTest.php @@ -82,7 +82,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], @@ -113,7 +113,7 @@ class DecryptAllTest extends TestCase { $this->view ] ) - ->setMethods(['prepareEncryptionModules', 'decryptAllUsersFiles']) + ->onlyMethods(['prepareEncryptionModules', 'decryptAllUsersFiles']) ->getMock(); $instance->expects($this->once()) @@ -145,7 +145,7 @@ class DecryptAllTest extends TestCase { ); } - public function dataTrueFalse() { + public static function dataTrueFalse(): array { return [ [true], [false], @@ -198,7 +198,7 @@ class DecryptAllTest extends TestCase { $this->view ] ) - ->setMethods(['decryptUsersFiles']) + ->onlyMethods(['decryptUsersFiles']) ->getMock(); $this->invokePrivate($instance, 'input', [$this->inputInterface]); @@ -211,12 +211,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) { + $expected = array_shift($calls); + $this->assertEquals($expected, $user); + }); } else { $instance->expects($this->once()) ->method('decryptUsersFiles') @@ -226,7 +230,7 @@ class DecryptAllTest extends TestCase { $this->invokePrivate($instance, 'decryptAllUsersFiles', [$user]); } - public function dataTestDecryptAllUsersFiles() { + public static function dataTestDecryptAllUsersFiles(): array { return [ ['user1'], [''] @@ -243,7 +247,7 @@ class DecryptAllTest extends TestCase { $this->view ] ) - ->setMethods(['decryptFile']) + ->onlyMethods(['decryptFile']) ->getMock(); $storage = $this->getMockBuilder(IStorage::class) @@ -253,25 +257,29 @@ class DecryptAllTest extends TestCase { $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( @@ -283,12 +291,16 @@ 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) { + $expected = array_shift($calls); + $this->assertEquals($expected, $path); + }); /* We need format method to return a string */ @@ -320,7 +332,7 @@ class DecryptAllTest extends TestCase { $this->view ] ) - ->setMethods(['getTimestamp']) + ->onlyMethods(['getTimestamp']) ->getMock(); $fileInfo = $this->createMock(FileInfo::class); @@ -360,7 +372,7 @@ class DecryptAllTest extends TestCase { $this->view ] ) - ->setMethods(['getTimestamp']) + ->onlyMethods(['getTimestamp']) ->getMock(); diff --git a/tests/lib/Encryption/Keys/StorageTest.php b/tests/lib/Encryption/Keys/StorageTest.php index 2ce30105293..bc79c5771ca 100644 --- a/tests/lib/Encryption/Keys/StorageTest.php +++ b/tests/lib/Encryption/Keys/StorageTest.php @@ -9,6 +9,7 @@ namespace Test\Encryption\Keys; use OC\Encryption\Keys\Storage; +use OC\Encryption\Util; use OC\Files\View; use OCP\IConfig; use OCP\Security\ICrypto; @@ -36,9 +37,9 @@ class StorageTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->util = $this->getMockBuilder('OC\Encryption\Util') + $this->util = $this->getMockBuilder(Util::class) ->disableOriginalConstructor() - ->setMethodsExcept(['getFileKeyDir']) + ->onlyMethods(array_diff(get_class_methods(Util::class), ['getFileKeyDir'])) ->getMock(); $this->view = $this->getMockBuilder(View::class) @@ -114,7 +115,7 @@ class StorageTest extends TestCase { ); } - public function dataTestGetFileKey() { + public static function dataTestGetFileKey() { return [ ['/files/foo.txt', '/files/foo.txt', true, 'key'], ['/files/foo.txt.ocTransferId2111130212.part', '/files/foo.txt', true, 'key'], @@ -157,13 +158,10 @@ class StorageTest extends TestCase { if (!$originalKeyExists) { $this->view->expects($this->exactly(2)) ->method('file_exists') - ->withConsecutive( - [$this->equalTo('/user1/files_encryption/keys' . $strippedPartialName . '/encModule/fileKey')], - [$this->equalTo('/user1/files_encryption/keys' . $path . '/encModule/fileKey')], - )->willReturnOnConsecutiveCalls( - $originalKeyExists, - true, - ); + ->willReturnMap([ + ['/user1/files_encryption/keys' . $strippedPartialName . '/encModule/fileKey', $originalKeyExists], + ['/user1/files_encryption/keys' . $path . '/encModule/fileKey', true], + ]); $this->view->expects($this->once()) ->method('file_get_contents') @@ -481,7 +479,7 @@ class StorageTest extends TestCase { return [$parts[1], '/' . implode('/', array_slice($parts, 2))]; } - public function dataProviderCopyRename() { + public static function dataProviderCopyRename() { return [ ['/user1/files/source.txt', '/user1/files/target.txt', false, false, '/user1/files_encryption/keys/files/source.txt/', '/user1/files_encryption/keys/files/target.txt/'], @@ -534,7 +532,7 @@ class StorageTest extends TestCase { ); } - public function dataTestGetPathToKeys() { + public static function dataTestGetPathToKeys() { return [ ['/user1/files/source.txt', false, '', '/user1/files_encryption/keys/files/source.txt/'], ['/user1/files/source.txt', true, '', '/files_encryption/keys/files/source.txt/'], @@ -577,7 +575,7 @@ class StorageTest extends TestCase { public function testBackupUserKeys($createBackupDir): void { $storage = $this->getMockBuilder('OC\Encryption\Keys\Storage') ->setConstructorArgs([$this->view, $this->util, $this->crypto, $this->config]) - ->setMethods(['getTimestamp']) + ->onlyMethods(['getTimestamp']) ->getMock(); $storage->expects($this->any())->method('getTimestamp')->willReturn('1234567'); @@ -586,11 +584,15 @@ class StorageTest extends TestCase { ->with('user1/files_encryption/backup')->willReturn(!$createBackupDir); if ($createBackupDir) { + $calls = [ + 'user1/files_encryption/backup', + 'user1/files_encryption/backup/test.encryptionModule.1234567', + ]; $this->view->expects($this->exactly(2))->method('mkdir') - ->withConsecutive( - ['user1/files_encryption/backup'], - ['user1/files_encryption/backup/test.encryptionModule.1234567'], - ); + ->willReturnCallback(function ($path) use (&$calls) { + $expected = array_shift($calls); + $this->assertEquals($expected, $path); + }); } else { $this->view->expects($this->once())->method('mkdir') ->with('user1/files_encryption/backup/test.encryptionModule.1234567'); @@ -605,7 +607,7 @@ class StorageTest extends TestCase { $this->assertTrue($storage->backupUserKeys('encryptionModule', 'test', 'user1')); } - public function dataTestBackupUserKeys() { + public static function dataTestBackupUserKeys() { return [ [true], [false] ]; |