Skip to content

Commit 9b4797e

Browse files
authored
Merge pull request #54262 from nextcloud/backport/54233/stable31
2 parents b8419fd + efd541c commit 9b4797e

File tree

3 files changed

+86
-95
lines changed

3 files changed

+86
-95
lines changed

apps/encryption/lib/Crypto/EncryptAll.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OCA\Encryption\KeyManager;
1313
use OCA\Encryption\Users\Setup;
1414
use OCA\Encryption\Util;
15+
use OCP\Files\FileInfo;
1516
use OCP\IConfig;
1617
use OCP\IL10N;
1718
use OCP\IUser;
@@ -202,15 +203,19 @@ protected function encryptUsersFiles($uid, ProgressBar $progress, $userCount) {
202203
while ($root = array_pop($directories)) {
203204
$content = $this->rootView->getDirectoryContent($root);
204205
foreach ($content as $file) {
205-
$path = $root . '/' . $file['name'];
206-
if ($this->rootView->is_dir($path)) {
206+
$path = $root . '/' . $file->getName();
207+
if ($file->isShared()) {
208+
$progress->setMessage("Skip shared file/folder $path");
209+
$progress->advance();
210+
continue;
211+
} elseif ($file->getType() === FileInfo::TYPE_FOLDER) {
207212
$directories[] = $path;
208213
continue;
209214
} else {
210215
$progress->setMessage("encrypt files for user $userCount: $path");
211216
$progress->advance();
212217
try {
213-
if ($this->encryptFile($path) === false) {
218+
if ($this->encryptFile($file, $path) === false) {
214219
$progress->setMessage("encrypt files for user $userCount: $path (already encrypted)");
215220
$progress->advance();
216221
}
@@ -231,17 +236,9 @@ protected function encryptUsersFiles($uid, ProgressBar $progress, $userCount) {
231236
}
232237
}
233238

234-
/**
235-
* encrypt file
236-
*
237-
* @param string $path
238-
* @return bool
239-
*/
240-
protected function encryptFile($path) {
241-
239+
protected function encryptFile(FileInfo $fileInfo, string $path): bool {
242240
// skip already encrypted files
243-
$fileInfo = $this->rootView->getFileInfo($path);
244-
if ($fileInfo !== false && $fileInfo->isEncrypted()) {
241+
if ($fileInfo->isEncrypted()) {
245242
return true;
246243
}
247244

apps/encryption/tests/Crypto/EncryptAllTest.php

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -30,6 +32,7 @@
3032
use Test\TestCase;
3133

3234
class EncryptAllTest extends TestCase {
35+
3336
protected KeyManager&MockObject $keyManager;
3437
protected Util&MockObject $util;
3538
protected IUserManager&MockObject $userManager;
@@ -46,8 +49,7 @@ class EncryptAllTest extends TestCase {
4649
protected ISecureRandom&MockObject $secureRandom;
4750
protected LoggerInterface&MockObject $logger;
4851

49-
/** @var EncryptAll */
50-
protected $encryptAll;
52+
protected EncryptAll $encryptAll;
5153

5254
protected function setUp(): void {
5355
parent::setUp();
@@ -80,7 +82,7 @@ protected function setUp(): void {
8082

8183
/**
8284
* We need format method to return a string
83-
* @var OutputFormatterInterface|\PHPUnit\Framework\MockObject\MockObject
85+
* @var OutputFormatterInterface&MockObject
8486
*/
8587
$outputFormatter = $this->createMock(OutputFormatterInterface::class);
8688
$outputFormatter->method('isDecorated')->willReturn(false);
@@ -112,6 +114,13 @@ protected function setUp(): void {
112114
);
113115
}
114116

117+
protected function createFileInfoMock($type, string $name): FileInfo&MockObject {
118+
$fileInfo = $this->createMock(FileInfo::class);
119+
$fileInfo->method('getType')->willReturn($type);
120+
$fileInfo->method('getName')->willReturn($name);
121+
return $fileInfo;
122+
}
123+
115124
public function testEncryptAll(): void {
116125
/** @var EncryptAll&MockObject $encryptAll */
117126
$encryptAll = $this->getMockBuilder(EncryptAll::class)
@@ -131,7 +140,7 @@ public function testEncryptAll(): void {
131140
$this->logger,
132141
]
133142
)
134-
->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
143+
->onlyMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
135144
->getMock();
136145

137146
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
@@ -161,7 +170,7 @@ public function testEncryptAllWithMasterKey(): void {
161170
$this->logger,
162171
]
163172
)
164-
->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
173+
->onlyMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
165174
->getMock();
166175

167176
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(true);
@@ -192,7 +201,7 @@ public function testCreateKeyPairs(): void {
192201
$this->logger,
193202
]
194203
)
195-
->setMethods(['setupUserFS', 'generateOneTimePassword'])
204+
->onlyMethods(['setupUserFS', 'generateOneTimePassword'])
196205
->getMock();
197206

198207

@@ -225,7 +234,7 @@ function ($user) {
225234
}
226235

227236
public function testEncryptAllUsersFiles(): void {
228-
/** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
237+
/** @var EncryptAll&MockObject $encryptAll */
229238
$encryptAll = $this->getMockBuilder(EncryptAll::class)
230239
->setConstructorArgs(
231240
[
@@ -243,7 +252,7 @@ public function testEncryptAllUsersFiles(): void {
243252
$this->logger,
244253
]
245254
)
246-
->setMethods(['encryptUsersFiles'])
255+
->onlyMethods(['encryptUsersFiles'])
247256
->getMock();
248257

249258
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
@@ -252,17 +261,22 @@ public function testEncryptAllUsersFiles(): void {
252261
$this->invokePrivate($encryptAll, 'output', [$this->outputInterface]);
253262
$this->invokePrivate($encryptAll, 'userPasswords', [['user1' => 'pwd1', 'user2' => 'pwd2']]);
254263

255-
$encryptAll->expects($this->exactly(2))->method('encryptUsersFiles')
256-
->withConsecutive(
257-
['user1'],
258-
['user2'],
259-
);
264+
$encryptAllCalls = [];
265+
$encryptAll->expects($this->exactly(2))
266+
->method('encryptUsersFiles')
267+
->willReturnCallback(function ($uid) use (&$encryptAllCalls): void {
268+
$encryptAllCalls[] = $uid;
269+
});
260270

261271
$this->invokePrivate($encryptAll, 'encryptAllUsersFiles');
272+
self::assertEquals([
273+
'user1',
274+
'user2',
275+
], $encryptAllCalls);
262276
}
263277

264278
public function testEncryptUsersFiles(): void {
265-
/** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
279+
/** @var EncryptAll&MockObject $encryptAll */
266280
$encryptAll = $this->getMockBuilder(EncryptAll::class)
267281
->setConstructorArgs(
268282
[
@@ -280,40 +294,39 @@ public function testEncryptUsersFiles(): void {
280294
$this->logger,
281295
]
282296
)
283-
->setMethods(['encryptFile', 'setupUserFS'])
297+
->onlyMethods(['encryptFile', 'setupUserFS'])
284298
->getMock();
285299

286300
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
287301

288302
$this->view->expects($this->exactly(2))->method('getDirectoryContent')
289-
->withConsecutive(
290-
['/user1/files'],
291-
['/user1/files/foo'],
292-
)->willReturnOnConsecutiveCalls(
303+
->willReturnMap([
293304
[
294-
['name' => 'foo', 'type' => 'dir'],
295-
['name' => 'bar', 'type' => 'file'],
305+
'/user1/files',
306+
'',
307+
null,
308+
[
309+
$this->createFileInfoMock(FileInfo::TYPE_FOLDER, 'foo'),
310+
$this->createFileInfoMock(FileInfo::TYPE_FILE, 'bar'),
311+
],
296312
],
297313
[
298-
['name' => 'subfile', 'type' => 'file']
299-
]
300-
);
301-
302-
$this->view->expects($this->any())->method('is_dir')
303-
->willReturnCallback(
304-
function ($path) {
305-
if ($path === '/user1/files/foo') {
306-
return true;
307-
}
308-
return false;
309-
}
310-
);
314+
'/user1/files/foo',
315+
'',
316+
null,
317+
[
318+
$this->createFileInfoMock(FileInfo::TYPE_FILE, 'subfile'),
319+
],
320+
],
321+
]);
311322

312-
$encryptAll->expects($this->exactly(2))->method('encryptFile')
313-
->withConsecutive(
314-
['/user1/files/bar'],
315-
['/user1/files/foo/subfile'],
316-
);
323+
$encryptAllCalls = [];
324+
$encryptAll->expects($this->exactly(2))
325+
->method('encryptFile')
326+
->willReturnCallback(function (FileInfo $file, string $path) use (&$encryptAllCalls): bool {
327+
$encryptAllCalls[] = $path;
328+
return true;
329+
});
317330

318331
$outputFormatter = $this->createMock(OutputFormatterInterface::class);
319332
$outputFormatter->method('isDecorated')->willReturn(false);
@@ -323,6 +336,10 @@ function ($path) {
323336
$progressBar = new ProgressBar($this->outputInterface);
324337

325338
$this->invokePrivate($encryptAll, 'encryptUsersFiles', ['user1', $progressBar, '']);
339+
self::assertEquals([
340+
'/user1/files/bar',
341+
'/user1/files/foo/subfile',
342+
], $encryptAllCalls);
326343
}
327344

328345
public function testGenerateOneTimePassword(): void {
@@ -343,8 +360,7 @@ public function testEncryptFile($isEncrypted): void {
343360
$fileInfo = $this->createMock(FileInfo::class);
344361
$fileInfo->expects($this->any())->method('isEncrypted')
345362
->willReturn($isEncrypted);
346-
$this->view->expects($this->any())->method('getFileInfo')
347-
->willReturn($fileInfo);
363+
$this->view->expects($this->never())->method('getFileInfo');
348364

349365

350366
if ($isEncrypted) {
@@ -356,11 +372,11 @@ public function testEncryptFile($isEncrypted): void {
356372
}
357373

358374
$this->assertTrue(
359-
$this->invokePrivate($this->encryptAll, 'encryptFile', ['foo.txt'])
375+
$this->invokePrivate($this->encryptAll, 'encryptFile', [$fileInfo, 'foo.txt'])
360376
);
361377
}
362378

363-
public function dataTestEncryptFile() {
379+
public static function dataTestEncryptFile(): array {
364380
return [
365381
[true],
366382
[false],

tests/Core/Command/Encryption/EncryptAllTest.php

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
45
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -12,59 +13,36 @@
1213
use OCP\Encryption\IEncryptionModule;
1314
use OCP\Encryption\IManager;
1415
use OCP\IConfig;
16+
use PHPUnit\Framework\MockObject\MockObject;
1517
use Symfony\Component\Console\Helper\QuestionHelper;
1618
use Symfony\Component\Console\Input\InputInterface;
1719
use Symfony\Component\Console\Output\OutputInterface;
1820
use Test\TestCase;
1921

2022
class EncryptAllTest extends TestCase {
21-
/** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IConfig */
22-
protected $config;
23-
24-
/** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Encryption\IManager */
25-
protected $encryptionManager;
26-
27-
/** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\App\IAppManager */
28-
protected $appManager;
29-
30-
/** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Input\InputInterface */
31-
protected $consoleInput;
32-
33-
/** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Output\OutputInterface */
34-
protected $consoleOutput;
35-
36-
/** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Helper\QuestionHelper */
37-
protected $questionHelper;
38-
39-
/** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Encryption\IEncryptionModule */
40-
protected $encryptionModule;
23+
private IConfig&MockObject $config;
24+
private IManager&MockObject $encryptionManager;
25+
private IAppManager&MockObject $appManager;
26+
private InputInterface&MockObject $consoleInput;
27+
private OutputInterface&MockObject $consoleOutput;
28+
private QuestionHelper&MockObject $questionHelper;
29+
private IEncryptionModule&MockObject $encryptionModule;
4130

42-
/** @var EncryptAll */
43-
protected $command;
31+
private EncryptAll $command;
4432

4533
protected function setUp(): void {
4634
parent::setUp();
4735

48-
$this->config = $this->getMockBuilder(IConfig::class)
49-
->disableOriginalConstructor()
50-
->getMock();
51-
$this->encryptionManager = $this->getMockBuilder(IManager::class)
52-
->disableOriginalConstructor()
53-
->getMock();
54-
$this->appManager = $this->getMockBuilder(IAppManager::class)
55-
->disableOriginalConstructor()
56-
->getMock();
57-
$this->encryptionModule = $this->getMockBuilder(IEncryptionModule::class)
58-
->disableOriginalConstructor()
59-
->getMock();
60-
$this->questionHelper = $this->getMockBuilder(QuestionHelper::class)
61-
->disableOriginalConstructor()
62-
->getMock();
63-
$this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
36+
$this->config = $this->createMock(IConfig::class);
37+
$this->encryptionManager = $this->createMock(IManager::class);
38+
$this->appManager = $this->createMock(IAppManager::class);
39+
$this->encryptionModule = $this->createMock(IEncryptionModule::class);
40+
$this->questionHelper = $this->createMock(QuestionHelper::class);
41+
$this->consoleInput = $this->createMock(InputInterface::class);
6442
$this->consoleInput->expects($this->any())
6543
->method('isInteractive')
6644
->willReturn(true);
67-
$this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
45+
$this->consoleOutput = $this->createMock(OutputInterface::class);
6846
}
6947

7048
public function testEncryptAll(): void {

0 commit comments

Comments
 (0)