chore(encryption): Adapt tests to code changes

Also pulled tests refactors from master to avoid conflicts

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
pull/54263/head
Côme Chilliet 2025-08-04 11:53:19 +07:00
parent c022a1a1b1
commit a79ced0f98
No known key found for this signature in database
GPG Key ID: A3E2F658B28C760A
3 changed files with 93 additions and 129 deletions

@ -247,7 +247,7 @@ class EncryptAll {
while ($root = array_pop($directories)) { while ($root = array_pop($directories)) {
$content = $this->rootView->getDirectoryContent($root); $content = $this->rootView->getDirectoryContent($root);
foreach ($content as $file) { foreach ($content as $file) {
$path = $root . '/' . $file['name']; $path = $root . '/' . $file->getName();
if ($file->isShared()) { if ($file->isShared()) {
$progress->setMessage("Skip shared file/folder $path"); $progress->setMessage("Skip shared file/folder $path");
$progress->advance(); $progress->advance();

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
/** /**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@ -31,52 +33,23 @@ use Test\TestCase;
class EncryptAllTest extends TestCase { class EncryptAllTest extends TestCase {
/** @var \PHPUnit\Framework\MockObject\MockObject | \OCA\Encryption\KeyManager */ protected KeyManager&MockObject $keyManager;
protected $keyManager; protected Util&MockObject $util;
protected IUserManager&MockObject $userManager;
/** @var \PHPUnit\Framework\MockObject\MockObject | \OCA\Encryption\Util */ protected Setup&MockObject $setupUser;
protected $util; protected View&MockObject $view;
protected IConfig&MockObject $config;
/** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IUserManager */ protected IMailer&MockObject $mailer;
protected $userManager; protected IL10N&MockObject $l;
protected IFactory&MockObject $l10nFactory;
/** @var \PHPUnit\Framework\MockObject\MockObject | \OCA\Encryption\Users\Setup */ protected \Symfony\Component\Console\Helper\QuestionHelper&MockObject $questionHelper;
protected $setupUser; protected \Symfony\Component\Console\Input\InputInterface&MockObject $inputInterface;
protected \Symfony\Component\Console\Output\OutputInterface&MockObject $outputInterface;
/** @var \PHPUnit\Framework\MockObject\MockObject | \OC\Files\View */ protected UserInterface&MockObject $userInterface;
protected $view; protected ISecureRandom&MockObject $secureRandom;
/** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IConfig */
protected $config;
/** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Mail\IMailer */
protected $mailer;
/** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IL10N */
protected $l;
/** @var \PHPUnit\Framework\MockObject\MockObject | IFactory */
protected $l10nFactory;
/** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Helper\QuestionHelper */
protected $questionHelper;
/** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Input\InputInterface */
protected $inputInterface;
/** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Output\OutputInterface */
protected $outputInterface;
/** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\UserInterface */
protected $userInterface;
/** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Security\ISecureRandom */
protected $secureRandom;
protected LoggerInterface&MockObject $logger; protected LoggerInterface&MockObject $logger;
/** @var EncryptAll */ protected EncryptAll $encryptAll;
protected $encryptAll;
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
@ -109,7 +82,7 @@ class EncryptAllTest extends TestCase {
/** /**
* We need format method to return a string * We need format method to return a string
* @var OutputFormatterInterface|\PHPUnit\Framework\MockObject\MockObject * @var OutputFormatterInterface&MockObject
*/ */
$outputFormatter = $this->createMock(OutputFormatterInterface::class); $outputFormatter = $this->createMock(OutputFormatterInterface::class);
$outputFormatter->method('isDecorated')->willReturn(false); $outputFormatter->method('isDecorated')->willReturn(false);
@ -141,6 +114,13 @@ class EncryptAllTest extends TestCase {
); );
} }
protected function createFileInfoMock($type, string $name): FileInfo&MockObject {
$fileInfo = $this->createMock(FileInfo::class);
$fileInfo->method('getType')->willReturn($type);
$fileInfo->method('getName')->willReturn($name);
return $fileInfo;
}
public function testEncryptAll(): void { public function testEncryptAll(): void {
/** @var EncryptAll&MockObject $encryptAll */ /** @var EncryptAll&MockObject $encryptAll */
$encryptAll = $this->getMockBuilder(EncryptAll::class) $encryptAll = $this->getMockBuilder(EncryptAll::class)
@ -160,7 +140,7 @@ class EncryptAllTest extends TestCase {
$this->logger, $this->logger,
] ]
) )
->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords']) ->onlyMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
->getMock(); ->getMock();
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false); $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
@ -190,7 +170,7 @@ class EncryptAllTest extends TestCase {
$this->logger, $this->logger,
] ]
) )
->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords']) ->onlyMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
->getMock(); ->getMock();
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(true); $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(true);
@ -221,7 +201,7 @@ class EncryptAllTest extends TestCase {
$this->logger, $this->logger,
] ]
) )
->setMethods(['setupUserFS', 'generateOneTimePassword']) ->onlyMethods(['setupUserFS', 'generateOneTimePassword'])
->getMock(); ->getMock();
@ -253,8 +233,8 @@ class EncryptAllTest extends TestCase {
$this->assertSame('', $userPasswords['user2']); $this->assertSame('', $userPasswords['user2']);
} }
public function testEncryptAllUsersFiles() { public function testEncryptAllUsersFiles(): void {
/** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */ /** @var EncryptAll&MockObject $encryptAll */
$encryptAll = $this->getMockBuilder(EncryptAll::class) $encryptAll = $this->getMockBuilder(EncryptAll::class)
->setConstructorArgs( ->setConstructorArgs(
[ [
@ -272,7 +252,7 @@ class EncryptAllTest extends TestCase {
$this->logger, $this->logger,
] ]
) )
->setMethods(['encryptUsersFiles']) ->onlyMethods(['encryptUsersFiles'])
->getMock(); ->getMock();
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false); $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
@ -281,17 +261,22 @@ class EncryptAllTest extends TestCase {
$this->invokePrivate($encryptAll, 'output', [$this->outputInterface]); $this->invokePrivate($encryptAll, 'output', [$this->outputInterface]);
$this->invokePrivate($encryptAll, 'userPasswords', [['user1' => 'pwd1', 'user2' => 'pwd2']]); $this->invokePrivate($encryptAll, 'userPasswords', [['user1' => 'pwd1', 'user2' => 'pwd2']]);
$encryptAll->expects($this->exactly(2))->method('encryptUsersFiles') $encryptAllCalls = [];
->withConsecutive( $encryptAll->expects($this->exactly(2))
['user1'], ->method('encryptUsersFiles')
['user2'], ->willReturnCallback(function ($uid) use (&$encryptAllCalls): void {
); $encryptAllCalls[] = $uid;
});
$this->invokePrivate($encryptAll, 'encryptAllUsersFiles'); $this->invokePrivate($encryptAll, 'encryptAllUsersFiles');
self::assertEquals([
'user1',
'user2',
], $encryptAllCalls);
} }
public function testEncryptUsersFiles() { public function testEncryptUsersFiles(): void {
/** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */ /** @var EncryptAll&MockObject $encryptAll */
$encryptAll = $this->getMockBuilder(EncryptAll::class) $encryptAll = $this->getMockBuilder(EncryptAll::class)
->setConstructorArgs( ->setConstructorArgs(
[ [
@ -309,40 +294,39 @@ class EncryptAllTest extends TestCase {
$this->logger, $this->logger,
] ]
) )
->setMethods(['encryptFile', 'setupUserFS']) ->onlyMethods(['encryptFile', 'setupUserFS'])
->getMock(); ->getMock();
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false); $this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
$this->view->expects($this->exactly(2))->method('getDirectoryContent') $this->view->expects($this->exactly(2))->method('getDirectoryContent')
->withConsecutive( ->willReturnMap([
['/user1/files'],
['/user1/files/foo'],
)->willReturnOnConsecutiveCalls(
[ [
['name' => 'foo', 'type' => 'dir'], '/user1/files',
['name' => 'bar', 'type' => 'file'], '',
null,
[
$this->createFileInfoMock(FileInfo::TYPE_FOLDER, 'foo'),
$this->createFileInfoMock(FileInfo::TYPE_FILE, 'bar'),
],
], ],
[ [
['name' => 'subfile', 'type' => 'file'] '/user1/files/foo',
] '',
); null,
[
$this->view->expects($this->any())->method('is_dir') $this->createFileInfoMock(FileInfo::TYPE_FILE, 'subfile'),
->willReturnCallback( ],
function ($path) { ],
if ($path === '/user1/files/foo') { ]);
return true;
}
return false;
}
);
$encryptAll->expects($this->exactly(2))->method('encryptFile') $encryptAllCalls = [];
->withConsecutive( $encryptAll->expects($this->exactly(2))
['/user1/files/bar'], ->method('encryptFile')
['/user1/files/foo/subfile'], ->willReturnCallback(function (FileInfo $file, string $path) use (&$encryptAllCalls): bool {
); $encryptAllCalls[] = $path;
return true;
});
$outputFormatter = $this->createMock(OutputFormatterInterface::class); $outputFormatter = $this->createMock(OutputFormatterInterface::class);
$outputFormatter->method('isDecorated')->willReturn(false); $outputFormatter->method('isDecorated')->willReturn(false);
@ -352,9 +336,13 @@ class EncryptAllTest extends TestCase {
$progressBar = new ProgressBar($this->outputInterface); $progressBar = new ProgressBar($this->outputInterface);
$this->invokePrivate($encryptAll, 'encryptUsersFiles', ['user1', $progressBar, '']); $this->invokePrivate($encryptAll, 'encryptUsersFiles', ['user1', $progressBar, '']);
self::assertEquals([
'/user1/files/bar',
'/user1/files/foo/subfile',
], $encryptAllCalls);
} }
public function testGenerateOneTimePassword() { public function testGenerateOneTimePassword(): void {
$password = $this->invokePrivate($this->encryptAll, 'generateOneTimePassword', ['user1']); $password = $this->invokePrivate($this->encryptAll, 'generateOneTimePassword', ['user1']);
$this->assertTrue(is_string($password)); $this->assertTrue(is_string($password));
$this->assertSame(8, strlen($password)); $this->assertSame(8, strlen($password));
@ -368,12 +356,11 @@ class EncryptAllTest extends TestCase {
* @dataProvider dataTestEncryptFile * @dataProvider dataTestEncryptFile
* @param $isEncrypted * @param $isEncrypted
*/ */
public function testEncryptFile($isEncrypted) { public function testEncryptFile($isEncrypted): void {
$fileInfo = $this->createMock(FileInfo::class); $fileInfo = $this->createMock(FileInfo::class);
$fileInfo->expects($this->any())->method('isEncrypted') $fileInfo->expects($this->any())->method('isEncrypted')
->willReturn($isEncrypted); ->willReturn($isEncrypted);
$this->view->expects($this->any())->method('getFileInfo') $this->view->expects($this->never())->method('getFileInfo');
->willReturn($fileInfo);
if ($isEncrypted) { if ($isEncrypted) {
@ -385,11 +372,11 @@ class EncryptAllTest extends TestCase {
} }
$this->assertTrue( $this->assertTrue(
$this->invokePrivate($this->encryptAll, 'encryptFile', ['foo.txt']) $this->invokePrivate($this->encryptAll, 'encryptFile', [$fileInfo, 'foo.txt'])
); );
} }
public function dataTestEncryptFile() { public static function dataTestEncryptFile(): array {
return [ return [
[true], [true],
[false], [false],

@ -13,59 +13,36 @@ use OCP\App\IAppManager;
use OCP\Encryption\IEncryptionModule; use OCP\Encryption\IEncryptionModule;
use OCP\Encryption\IManager; use OCP\Encryption\IManager;
use OCP\IConfig; use OCP\IConfig;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Test\TestCase; use Test\TestCase;
class EncryptAllTest extends TestCase { class EncryptAllTest extends TestCase {
/** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IConfig */ private IConfig&MockObject $config;
protected $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 \PHPUnit\Framework\MockObject\MockObject | \OCP\Encryption\IManager */ private EncryptAll $command;
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;
/** @var EncryptAll */
protected $command;
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
$this->config = $this->getMockBuilder(IConfig::class) $this->config = $this->createMock(IConfig::class);
->disableOriginalConstructor() $this->encryptionManager = $this->createMock(IManager::class);
->getMock(); $this->appManager = $this->createMock(IAppManager::class);
$this->encryptionManager = $this->getMockBuilder(IManager::class) $this->encryptionModule = $this->createMock(IEncryptionModule::class);
->disableOriginalConstructor() $this->questionHelper = $this->createMock(QuestionHelper::class);
->getMock(); $this->consoleInput = $this->createMock(InputInterface::class);
$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->consoleInput->expects($this->any()) $this->consoleInput->expects($this->any())
->method('isInteractive') ->method('isInteractive')
->willReturn(true); ->willReturn(true);
$this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); $this->consoleOutput = $this->createMock(OutputInterface::class);
} }
public function testEncryptAll() { public function testEncryptAll() {