diff options
Diffstat (limited to 'tests/Core')
63 files changed, 1364 insertions, 715 deletions
diff --git a/tests/Core/Command/Apps/AppsDisableTest.php b/tests/Core/Command/Apps/AppsDisableTest.php index 8d23ecd1c61..117af958054 100644 --- a/tests/Core/Command/Apps/AppsDisableTest.php +++ b/tests/Core/Command/Apps/AppsDisableTest.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Tests\Core\Command\Config; use OC\Core\Command\App\Disable; +use OCP\App\IAppManager; +use OCP\Server; use Symfony\Component\Console\Tester\CommandTester; use Test\TestCase; @@ -25,22 +27,22 @@ class AppsDisableTest extends TestCase { parent::setUp(); $command = new Disable( - \OC::$server->getAppManager() + Server::get(IAppManager::class) ); $this->commandTester = new CommandTester($command); - \OC::$server->getAppManager()->enableApp('admin_audit'); - \OC::$server->getAppManager()->enableApp('comments'); + Server::get(IAppManager::class)->enableApp('admin_audit'); + Server::get(IAppManager::class)->enableApp('comments'); } /** - * @dataProvider dataCommandInput * @param $appId * @param $groups * @param $statusCode * @param $pattern */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataCommandInput')] public function testCommandInput($appId, $statusCode, $pattern): void { $input = ['app-id' => $appId]; @@ -50,7 +52,7 @@ class AppsDisableTest extends TestCase { $this->assertSame($statusCode, $this->commandTester->getStatusCode()); } - public function dataCommandInput(): array { + public static function dataCommandInput(): array { return [ [['admin_audit'], 0, 'admin_audit ([\d\.]*) disabled'], [['comments'], 0, 'comments ([\d\.]*) disabled'], diff --git a/tests/Core/Command/Apps/AppsEnableTest.php b/tests/Core/Command/Apps/AppsEnableTest.php index 228c09364bf..604c670ae15 100644 --- a/tests/Core/Command/Apps/AppsEnableTest.php +++ b/tests/Core/Command/Apps/AppsEnableTest.php @@ -9,6 +9,10 @@ declare(strict_types=1); namespace Tests\Core\Command\Config; use OC\Core\Command\App\Enable; +use OC\Installer; +use OCP\App\IAppManager; +use OCP\IGroupManager; +use OCP\Server; use Symfony\Component\Console\Tester\CommandTester; use Test\TestCase; @@ -25,23 +29,24 @@ class AppsEnableTest extends TestCase { parent::setUp(); $command = new Enable( - \OC::$server->getAppManager(), - \OC::$server->getGroupManager() + Server::get(IAppManager::class), + Server::get(IGroupManager::class), + Server::get(Installer::class), ); $this->commandTester = new CommandTester($command); - \OC::$server->getAppManager()->disableApp('admin_audit'); - \OC::$server->getAppManager()->disableApp('comments'); + Server::get(IAppManager::class)->disableApp('admin_audit'); + Server::get(IAppManager::class)->disableApp('comments'); } /** - * @dataProvider dataCommandInput * @param $appId * @param $groups * @param $statusCode * @param $pattern */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataCommandInput')] public function testCommandInput($appId, $groups, $statusCode, $pattern): void { $input = ['app-id' => $appId]; @@ -55,7 +60,7 @@ class AppsEnableTest extends TestCase { $this->assertSame($statusCode, $this->commandTester->getStatusCode()); } - public function dataCommandInput(): array { + public static function dataCommandInput(): array { return [ [['admin_audit'], null, 0, 'admin_audit ([\d\.]*) enabled'], [['comments'], null, 0, 'comments ([\d\.]*) enabled'], diff --git a/tests/Core/Command/Config/App/DeleteConfigTest.php b/tests/Core/Command/Config/App/DeleteConfigTest.php index ebbbb15e1b6..9e43f389214 100644 --- a/tests/Core/Command/Config/App/DeleteConfigTest.php +++ b/tests/Core/Command/Config/App/DeleteConfigTest.php @@ -1,4 +1,6 @@ <?php + +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -7,38 +9,35 @@ namespace Tests\Core\Command\Config\App; +use OC\Config\ConfigManager; use OC\Core\Command\Config\App\DeleteConfig; -use OCP\IConfig; +use OCP\IAppConfig; +use PHPUnit\Framework\MockObject\MockObject; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Test\TestCase; class DeleteConfigTest extends TestCase { - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ - protected $config; - - /** @var \PHPUnit\Framework\MockObject\MockObject */ - protected $consoleInput; - /** @var \PHPUnit\Framework\MockObject\MockObject */ - protected $consoleOutput; - - /** @var \Symfony\Component\Console\Command\Command */ - protected $command; + protected IAppConfig&MockObject $appConfig; + protected ConfigManager&MockObject $configManager; + protected InputInterface&MockObject $consoleInput; + protected OutputInterface&MockObject $consoleOutput; + protected Command $command; protected function setUp(): void { parent::setUp(); - $this->config = $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock(); - $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); - $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); + $this->appConfig = $this->createMock(IAppConfig::class); + $this->configManager = $this->createMock(ConfigManager::class); + $this->consoleInput = $this->createMock(InputInterface::class); + $this->consoleOutput = $this->createMock(OutputInterface::class); - $this->command = new DeleteConfig($this->config); + $this->command = new DeleteConfig($this->appConfig, $this->configManager); } - public function deleteData() { + public static function dataDelete(): array { return [ [ 'name', @@ -71,23 +70,15 @@ class DeleteConfigTest extends TestCase { ]; } - /** - * @dataProvider deleteData - * - * @param string $configName - * @param bool $configExists - * @param bool $checkIfExists - * @param int $expectedReturn - * @param string $expectedMessage - */ - public function testDelete($configName, $configExists, $checkIfExists, $expectedReturn, $expectedMessage): void { - $this->config->expects(($checkIfExists) ? $this->once() : $this->never()) - ->method('getAppKeys') + #[\PHPUnit\Framework\Attributes\DataProvider('dataDelete')] + public function testDelete(string $configName, bool $configExists, bool $checkIfExists, int $expectedReturn, string $expectedMessage): void { + $this->appConfig->expects(($checkIfExists) ? $this->once() : $this->never()) + ->method('getKeys') ->with('app-name') ->willReturn($configExists ? [$configName] : []); - $this->config->expects(($expectedReturn === 0) ? $this->once() : $this->never()) - ->method('deleteAppValue') + $this->appConfig->expects(($expectedReturn === 0) ? $this->once() : $this->never()) + ->method('deleteKey') ->with('app-name', $configName); $this->consoleInput->expects($this->exactly(2)) @@ -96,15 +87,13 @@ class DeleteConfigTest extends TestCase { ['app', 'app-name'], ['name', $configName], ]); - $this->consoleInput->expects($this->any()) - ->method('hasParameterOption') + $this->consoleInput->method('hasParameterOption') ->with('--error-if-not-exists') ->willReturn($checkIfExists); - $this->consoleOutput->expects($this->any()) - ->method('writeln') + $this->consoleOutput->method('writeln') ->with($this->stringContains($expectedMessage)); - $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); + $this->assertSame($expectedReturn, self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); } } diff --git a/tests/Core/Command/Config/App/GetConfigTest.php b/tests/Core/Command/Config/App/GetConfigTest.php index c1a6265db40..13392cddf55 100644 --- a/tests/Core/Command/Config/App/GetConfigTest.php +++ b/tests/Core/Command/Config/App/GetConfigTest.php @@ -1,4 +1,6 @@ <?php + +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -7,40 +9,36 @@ namespace Tests\Core\Command\Config\App; -use OC\AppConfig; +use OC\Config\ConfigManager; use OC\Core\Command\Config\App\GetConfig; use OCP\Exceptions\AppConfigUnknownKeyException; +use OCP\IAppConfig; +use PHPUnit\Framework\MockObject\MockObject; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Test\TestCase; class GetConfigTest extends TestCase { - /** @var \PHPUnit\Framework\MockObject\MockObject */ - protected $config; - - /** @var \PHPUnit\Framework\MockObject\MockObject */ - protected $consoleInput; - /** @var \PHPUnit\Framework\MockObject\MockObject */ - protected $consoleOutput; - - /** @var \Symfony\Component\Console\Command\Command */ - protected $command; + protected IAppConfig&MockObject $appConfig; + protected ConfigManager&MockObject $configManager; + protected InputInterface&MockObject $consoleInput; + protected OutputInterface&MockObject $consoleOutput; + protected Command $command; protected function setUp(): void { parent::setUp(); - $config = $this->config = $this->getMockBuilder(AppConfig::class) - ->disableOriginalConstructor() - ->getMock(); - $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); - $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); + $this->appConfig = $this->createMock(IAppConfig::class); + $this->configManager = $this->createMock(ConfigManager::class); + $this->consoleInput = $this->createMock(InputInterface::class); + $this->consoleOutput = $this->createMock(OutputInterface::class); - /** @var \OCP\IAppConfig $config */ - $this->command = new GetConfig($config); + $this->command = new GetConfig($this->appConfig, $this->configManager); } - public function getData() { + public static function dataGet(): array { return [ // String output as json ['name', 'newvalue', true, null, false, 'json', 0, json_encode('newvalue')], @@ -82,22 +80,11 @@ class GetConfigTest extends TestCase { ]; } - /** - * @dataProvider getData - * - * @param string $configName - * @param mixed $value - * @param bool $configExists - * @param mixed $defaultValue - * @param bool $hasDefault - * @param string $outputFormat - * @param int $expectedReturn - * @param string $expectedMessage - */ - public function testGet($configName, $value, $configExists, $defaultValue, $hasDefault, $outputFormat, $expectedReturn, $expectedMessage): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGet')] + public function testGet(string $configName, mixed $value, bool $configExists, mixed $defaultValue, bool $hasDefault, string $outputFormat, int $expectedReturn, ?string $expectedMessage): void { if (!$expectedReturn) { if ($configExists) { - $this->config->expects($this->once()) + $this->appConfig->expects($this->once()) ->method('getDetails') ->with('app-name', $configName) ->willReturn(['value' => $value]); @@ -105,7 +92,7 @@ class GetConfigTest extends TestCase { } if (!$configExists) { - $this->config->expects($this->once()) + $this->appConfig->expects($this->once()) ->method('getDetails') ->with('app-name', $configName) ->willThrowException(new AppConfigUnknownKeyException()); @@ -117,14 +104,12 @@ class GetConfigTest extends TestCase { ['app', 'app-name'], ['name', $configName], ]); - $this->consoleInput->expects($this->any()) - ->method('getOption') + $this->consoleInput->method('getOption') ->willReturnMap([ ['default-value', $defaultValue], ['output', $outputFormat], ]); - $this->consoleInput->expects($this->any()) - ->method('hasParameterOption') + $this->consoleInput->method('hasParameterOption') ->willReturnMap([ ['--output', false, true], ['--default-value', false, $hasDefault], @@ -134,8 +119,7 @@ class GetConfigTest extends TestCase { global $output; $output = ''; - $this->consoleOutput->expects($this->any()) - ->method('writeln') + $this->consoleOutput->method('writeln') ->willReturnCallback(function ($value) { global $output; $output .= $value . "\n"; @@ -143,7 +127,7 @@ class GetConfigTest extends TestCase { }); } - $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); + $this->assertSame($expectedReturn, self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); if ($expectedMessage !== null) { global $output; diff --git a/tests/Core/Command/Config/App/SetConfigTest.php b/tests/Core/Command/Config/App/SetConfigTest.php index 1d599bf3234..a5c62368163 100644 --- a/tests/Core/Command/Config/App/SetConfigTest.php +++ b/tests/Core/Command/Config/App/SetConfigTest.php @@ -1,4 +1,6 @@ <?php + +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -8,40 +10,36 @@ namespace Tests\Core\Command\Config\App; use OC\AppConfig; +use OC\Config\ConfigManager; use OC\Core\Command\Config\App\SetConfig; use OCP\Exceptions\AppConfigUnknownKeyException; use OCP\IAppConfig; +use PHPUnit\Framework\MockObject\MockObject; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Test\TestCase; class SetConfigTest extends TestCase { - /** @var \PHPUnit\Framework\MockObject\MockObject */ - protected $config; - - /** @var \PHPUnit\Framework\MockObject\MockObject */ - protected $consoleInput; - /** @var \PHPUnit\Framework\MockObject\MockObject */ - protected $consoleOutput; - - /** @var \Symfony\Component\Console\Command\Command */ - protected $command; + protected IAppConfig&MockObject $appConfig; + protected ConfigManager&MockObject $configManager; + protected InputInterface&MockObject $consoleInput; + protected OutputInterface&MockObject $consoleOutput; + protected Command $command; protected function setUp(): void { parent::setUp(); - $config = $this->config = $this->getMockBuilder(AppConfig::class) - ->disableOriginalConstructor() - ->getMock(); - $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); - $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); + $this->appConfig = $this->createMock(AppConfig::class); + $this->configManager = $this->createMock(ConfigManager::class); + $this->consoleInput = $this->createMock(InputInterface::class); + $this->consoleOutput = $this->createMock(OutputInterface::class); - /** @var \OCP\IAppConfig $config */ - $this->command = new SetConfig($config); + $this->command = new SetConfig($this->appConfig, $this->configManager); } - public function setData() { + public static function dataSet(): array { return [ [ 'name', @@ -62,34 +60,22 @@ class SetConfigTest extends TestCase { ]; } - /** - * @dataProvider setData - * - * @param string $configName - * @param mixed $newValue - * @param bool $configExists - * @param bool $updateOnly - * @param bool $updated - * @param string $expectedMessage - */ - public function testSet($configName, $newValue, $configExists, $updateOnly, $updated, $expectedMessage): void { - $this->config->expects($this->any()) - ->method('hasKey') + #[\PHPUnit\Framework\Attributes\DataProvider('dataSet')] + public function testSet(string $configName, mixed $newValue, bool $configExists, bool $updateOnly, bool $updated, string $expectedMessage): void { + $this->appConfig->method('hasKey') ->with('app-name', $configName) ->willReturn($configExists); if (!$configExists) { - $this->config->expects($this->any()) - ->method('getValueType') + $this->appConfig->method('getValueType') ->willThrowException(new AppConfigUnknownKeyException()); } else { - $this->config->expects($this->any()) - ->method('getValueType') + $this->appConfig->method('getValueType') ->willReturn(IAppConfig::VALUE_MIXED); } if ($updated) { - $this->config->expects($this->once()) + $this->appConfig->expects($this->once()) ->method('setValueMixed') ->with('app-name', $configName, $newValue); } @@ -100,25 +86,22 @@ class SetConfigTest extends TestCase { ['app', 'app-name'], ['name', $configName], ]); - $this->consoleInput->expects($this->any()) - ->method('getOption') + $this->consoleInput->method('getOption') ->willReturnMap([ ['value', $newValue], ['lazy', null], ['sensitive', null], ['no-interaction', true], ]); - $this->consoleInput->expects($this->any()) - ->method('hasParameterOption') + $this->consoleInput->method('hasParameterOption') ->willReturnMap([ ['--type', false, false], ['--value', false, true], ['--update-only', false, $updateOnly] ]); - $this->consoleOutput->expects($this->any()) - ->method('writeln') + $this->consoleOutput->method('writeln') ->with($this->stringContains($expectedMessage)); - $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); } } diff --git a/tests/Core/Command/Config/ImportTest.php b/tests/Core/Command/Config/ImportTest.php index 0077f84f82b..14cdd714d12 100644 --- a/tests/Core/Command/Config/ImportTest.php +++ b/tests/Core/Command/Config/ImportTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -34,11 +35,11 @@ class ImportTest 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 Import($config); } - public function validateAppsArrayData() { + public static function validateAppsArrayData(): array { return [ [0], [1], @@ -49,16 +50,15 @@ class ImportTest extends TestCase { } /** - * @dataProvider validateAppsArrayData - * * @param mixed $configValue */ + #[\PHPUnit\Framework\Attributes\DataProvider('validateAppsArrayData')] public function testValidateAppsArray($configValue): void { $this->invokePrivate($this->command, 'validateAppsArray', [['app' => ['name' => $configValue]]]); $this->assertTrue(true, 'Asserting that no exception is thrown'); } - public function validateAppsArrayThrowsData() { + public static function validateAppsArrayThrowsData(): array { return [ [false], [true], @@ -68,10 +68,9 @@ class ImportTest extends TestCase { } /** - * @dataProvider validateAppsArrayThrowsData - * * @param mixed $configValue */ + #[\PHPUnit\Framework\Attributes\DataProvider('validateAppsArrayThrowsData')] public function testValidateAppsArrayThrows($configValue): void { try { $this->invokePrivate($this->command, 'validateAppsArray', [['app' => ['name' => $configValue]]]); @@ -81,7 +80,7 @@ class ImportTest extends TestCase { } } - public function checkTypeRecursivelyData() { + public static function checkTypeRecursivelyData(): array { return [ [0], [1], @@ -98,16 +97,15 @@ class ImportTest extends TestCase { } /** - * @dataProvider checkTypeRecursivelyData - * * @param mixed $configValue */ + #[\PHPUnit\Framework\Attributes\DataProvider('checkTypeRecursivelyData')] public function testCheckTypeRecursively($configValue): void { $this->invokePrivate($this->command, 'checkTypeRecursively', [$configValue, 'name']); $this->assertTrue(true, 'Asserting that no exception is thrown'); } - public function checkTypeRecursivelyThrowsData() { + public static function checkTypeRecursivelyThrowsData(): array { return [ [new \Exception()], [[new \Exception()]], @@ -117,10 +115,9 @@ class ImportTest extends TestCase { } /** - * @dataProvider checkTypeRecursivelyThrowsData - * * @param mixed $configValue */ + #[\PHPUnit\Framework\Attributes\DataProvider('checkTypeRecursivelyThrowsData')] public function testCheckTypeRecursivelyThrows($configValue): void { try { $this->invokePrivate($this->command, 'checkTypeRecursively', [$configValue, 'name']); @@ -130,7 +127,7 @@ class ImportTest extends TestCase { } } - public function validateArrayData() { + public static function validateArrayData(): array { return [ [['system' => []]], [['apps' => []]], @@ -139,16 +136,15 @@ class ImportTest extends TestCase { } /** - * @dataProvider validateArrayData - * * @param array $configArray */ + #[\PHPUnit\Framework\Attributes\DataProvider('validateArrayData')] public function testValidateArray($configArray): void { $this->invokePrivate($this->command, 'validateArray', [$configArray]); $this->assertTrue(true, 'Asserting that no exception is thrown'); } - public function validateArrayThrowsData() { + public static function validateArrayThrowsData(): array { return [ [[], 'At least one key of the following is expected:'], [[0 => []], 'Found invalid entries in root'], @@ -157,11 +153,11 @@ class ImportTest extends TestCase { } /** - * @dataProvider validateArrayThrowsData * * @param mixed $configArray * @param string $expectedException */ + #[\PHPUnit\Framework\Attributes\DataProvider('validateArrayThrowsData')] public function testValidateArrayThrows($configArray, $expectedException): void { try { $this->invokePrivate($this->command, 'validateArray', [$configArray]); diff --git a/tests/Core/Command/Config/ListConfigsTest.php b/tests/Core/Command/Config/ListConfigsTest.php index 0cee870d45c..e4af55116c0 100644 --- a/tests/Core/Command/Config/ListConfigsTest.php +++ b/tests/Core/Command/Config/ListConfigsTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -7,6 +8,7 @@ namespace Tests\Core\Command\Config; +use OC\Config\ConfigManager; use OC\Core\Command\Config\ListConfigs; use OC\SystemConfig; use OCP\IAppConfig; @@ -20,6 +22,8 @@ class ListConfigsTest extends TestCase { protected $appConfig; /** @var \PHPUnit\Framework\MockObject\MockObject */ protected $systemConfig; + /** @var \PHPUnit\Framework\MockObject\MockObject */ + protected $configManager; /** @var \PHPUnit\Framework\MockObject\MockObject */ protected $consoleInput; @@ -38,15 +42,20 @@ class ListConfigsTest extends TestCase { $appConfig = $this->appConfig = $this->getMockBuilder(IAppConfig::class) ->disableOriginalConstructor() ->getMock(); + $configManager = $this->configManager = $this->getMockBuilder(ConfigManager::class) + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); /** @var \OC\SystemConfig $systemConfig */ /** @var \OCP\IAppConfig $appConfig */ - $this->command = new ListConfigs($systemConfig, $appConfig); + /** @var ConfigManager $configManager */ + $this->command = new ListConfigs($systemConfig, $appConfig, $configManager); } - public function listData() { + public static function listData(): array { return [ [ 'all', @@ -61,10 +70,10 @@ class ListConfigsTest extends TestCase { ], // app config [ - ['files', false, [ + ['files', [ 'enabled' => 'yes', ]], - ['core', false, [ + ['core', [ 'global_cache_gc_lastrun' => '1430388388', ]], ], @@ -141,10 +150,10 @@ class ListConfigsTest extends TestCase { ], // app config [ - ['files', false, [ + ['files', [ 'enabled' => 'yes', ]], - ['core', false, [ + ['core', [ 'global_cache_gc_lastrun' => '1430388388', ]], ], @@ -176,10 +185,10 @@ class ListConfigsTest extends TestCase { ], // app config [ - ['files', false, [ + ['files', [ 'enabled' => 'yes', ]], - ['core', false, [ + ['core', [ 'global_cache_gc_lastrun' => '1430388388', ]], ], @@ -204,10 +213,10 @@ class ListConfigsTest extends TestCase { ], // app config [ - ['files', false, [ + ['files', [ 'enabled' => 'yes', ]], - ['core', false, [ + ['core', [ 'global_cache_gc_lastrun' => '1430388388', ]], ], @@ -253,7 +262,6 @@ class ListConfigsTest extends TestCase { } /** - * @dataProvider listData * * @param string $app * @param array $systemConfigs @@ -262,6 +270,7 @@ class ListConfigsTest extends TestCase { * @param bool $private * @param string $expected */ + #[\PHPUnit\Framework\Attributes\DataProvider('listData')] public function testList($app, $systemConfigs, $systemConfigMap, $appConfig, $private, $expected): void { $this->systemConfig->expects($this->any()) ->method('getKeys') diff --git a/tests/Core/Command/Config/System/CastHelperTest.php b/tests/Core/Command/Config/System/CastHelperTest.php new file mode 100644 index 00000000000..924887daaf7 --- /dev/null +++ b/tests/Core/Command/Config/System/CastHelperTest.php @@ -0,0 +1,66 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-only + */ + +namespace Core\Command\Config\System; + +use OC\Core\Command\Config\System\CastHelper; +use Test\TestCase; + +class CastHelperTest extends TestCase { + private CastHelper $castHelper; + + protected function setUp(): void { + parent::setUp(); + $this->castHelper = new CastHelper(); + } + + public static function castValueProvider(): array { + return [ + [null, 'string', ['value' => '', 'readable-value' => 'empty string']], + + ['abc', 'string', ['value' => 'abc', 'readable-value' => 'string abc']], + + ['123', 'integer', ['value' => 123, 'readable-value' => 'integer 123']], + ['456', 'int', ['value' => 456, 'readable-value' => 'integer 456']], + + ['2.25', 'double', ['value' => 2.25, 'readable-value' => 'double 2.25']], + ['0.5', 'float', ['value' => 0.5, 'readable-value' => 'double 0.5']], + + ['', 'null', ['value' => null, 'readable-value' => 'null']], + + ['true', 'boolean', ['value' => true, 'readable-value' => 'boolean true']], + ['false', 'bool', ['value' => false, 'readable-value' => 'boolean false']], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('castValueProvider')] + public function testCastValue($value, $type, $expectedValue): void { + $this->assertSame( + $expectedValue, + $this->castHelper->castValue($value, $type) + ); + } + + public static function castValueInvalidProvider(): array { + return [ + ['123', 'foobar'], + + [null, 'integer'], + ['abc', 'integer'], + ['76ggg', 'double'], + ['true', 'float'], + ['foobar', 'boolean'], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('castValueInvalidProvider')] + public function testCastValueInvalid($value, $type): void { + $this->expectException(\InvalidArgumentException::class); + + $this->castHelper->castValue($value, $type); + } +} diff --git a/tests/Core/Command/Config/System/DeleteConfigTest.php b/tests/Core/Command/Config/System/DeleteConfigTest.php index 872079ef4c2..b0a3580e1cd 100644 --- a/tests/Core/Command/Config/System/DeleteConfigTest.php +++ b/tests/Core/Command/Config/System/DeleteConfigTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -34,11 +35,11 @@ class DeleteConfigTest extends TestCase { $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); - /** @var \OC\SystemConfig $systemConfig */ + /** @var SystemConfig $systemConfig */ $this->command = new DeleteConfig($systemConfig); } - public function deleteData() { + public static function deleteData(): array { return [ [ 'name1', @@ -72,7 +73,6 @@ class DeleteConfigTest extends TestCase { } /** - * @dataProvider deleteData * * @param string $configName * @param bool $configExists @@ -80,6 +80,7 @@ class DeleteConfigTest extends TestCase { * @param int $expectedReturn * @param string $expectedMessage */ + #[\PHPUnit\Framework\Attributes\DataProvider('deleteData')] public function testDelete($configName, $configExists, $checkIfExists, $expectedReturn, $expectedMessage): void { $this->systemConfig->expects(($checkIfExists) ? $this->once() : $this->never()) ->method('getKeys') @@ -105,7 +106,7 @@ class DeleteConfigTest extends TestCase { $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); } - public function deleteArrayData() { + public static function deleteArrayData(): array { return [ [ ['name', 'sub'], @@ -165,7 +166,6 @@ class DeleteConfigTest extends TestCase { } /** - * @dataProvider deleteArrayData * * @param string[] $configNames * @param bool $configKeyExists @@ -175,6 +175,7 @@ class DeleteConfigTest extends TestCase { * @param int $expectedReturn * @param string $expectedMessage */ + #[\PHPUnit\Framework\Attributes\DataProvider('deleteArrayData')] public function testArrayDelete(array $configNames, $configKeyExists, $checkIfKeyExists, $configValue, $updateValue, $expectedReturn, $expectedMessage): void { $this->systemConfig->expects(($checkIfKeyExists) ? $this->once() : $this->never()) ->method('getKeys') diff --git a/tests/Core/Command/Config/System/GetConfigTest.php b/tests/Core/Command/Config/System/GetConfigTest.php index f05d0e384ee..8b84fd14198 100644 --- a/tests/Core/Command/Config/System/GetConfigTest.php +++ b/tests/Core/Command/Config/System/GetConfigTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -34,12 +35,12 @@ class GetConfigTest extends TestCase { $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); - /** @var \OC\SystemConfig $systemConfig */ + /** @var SystemConfig $systemConfig */ $this->command = new GetConfig($systemConfig); } - public function getData() { + public static function getData(): array { return [ // String output as json ['name', 'newvalue', true, null, false, 'json', 0, json_encode('newvalue')], @@ -88,7 +89,6 @@ class GetConfigTest extends TestCase { } /** - * @dataProvider getData * * @param string[] $configNames * @param mixed $value @@ -99,6 +99,7 @@ class GetConfigTest extends TestCase { * @param int $expectedReturn * @param string $expectedMessage */ + #[\PHPUnit\Framework\Attributes\DataProvider('getData')] public function testGet($configNames, $value, $configExists, $defaultValue, $hasDefault, $outputFormat, $expectedReturn, $expectedMessage): void { if (is_array($configNames)) { $configName = $configNames[0]; diff --git a/tests/Core/Command/Config/System/SetConfigTest.php b/tests/Core/Command/Config/System/SetConfigTest.php index 2905af5c3d7..a99b832c160 100644 --- a/tests/Core/Command/Config/System/SetConfigTest.php +++ b/tests/Core/Command/Config/System/SetConfigTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -7,6 +8,7 @@ namespace Tests\Core\Command\Config\System; +use OC\Core\Command\Config\System\CastHelper; use OC\Core\Command\Config\System\SetConfig; use OC\SystemConfig; use Symfony\Component\Console\Input\InputInterface; @@ -34,12 +36,12 @@ class SetConfigTest extends TestCase { $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); - /** @var \OC\SystemConfig $systemConfig */ - $this->command = new SetConfig($systemConfig); + /** @var SystemConfig $systemConfig */ + $this->command = new SetConfig($systemConfig, new CastHelper()); } - public function setData() { + public static function dataTest() { return [ [['name'], 'newvalue', null, 'newvalue'], [['a', 'b', 'c'], 'foobar', null, ['b' => ['c' => 'foobar']]], @@ -48,13 +50,13 @@ class SetConfigTest extends TestCase { } /** - * @dataProvider setData * * @param array $configNames * @param string $newValue * @param mixed $existingData * @param mixed $expectedValue */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTest')] public function testSet($configNames, $newValue, $existingData, $expectedValue): void { $this->systemConfig->expects($this->once()) ->method('setValue') @@ -76,7 +78,7 @@ class SetConfigTest extends TestCase { $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); } - public function setUpdateOnlyProvider() { + public static function setUpdateOnlyProvider(): array { return [ [['name'], null], [['a', 'b', 'c'], null], @@ -85,9 +87,7 @@ class SetConfigTest extends TestCase { ]; } - /** - * @dataProvider setUpdateOnlyProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('setUpdateOnlyProvider')] public function testSetUpdateOnly($configNames, $existingData): void { $this->expectException(\UnexpectedValueException::class); @@ -112,53 +112,4 @@ class SetConfigTest extends TestCase { $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); } - - public function castValueProvider() { - return [ - [null, 'string', ['value' => '', 'readable-value' => 'empty string']], - - ['abc', 'string', ['value' => 'abc', 'readable-value' => 'string abc']], - - ['123', 'integer', ['value' => 123, 'readable-value' => 'integer 123']], - ['456', 'int', ['value' => 456, 'readable-value' => 'integer 456']], - - ['2.25', 'double', ['value' => 2.25, 'readable-value' => 'double 2.25']], - ['0.5', 'float', ['value' => 0.5, 'readable-value' => 'double 0.5']], - - ['', 'null', ['value' => null, 'readable-value' => 'null']], - - ['true', 'boolean', ['value' => true, 'readable-value' => 'boolean true']], - ['false', 'bool', ['value' => false, 'readable-value' => 'boolean false']], - ]; - } - - /** - * @dataProvider castValueProvider - */ - public function testCastValue($value, $type, $expectedValue): void { - $this->assertSame($expectedValue, - $this->invokePrivate($this->command, 'castValue', [$value, $type]) - ); - } - - public function castValueInvalidProvider() { - return [ - ['123', 'foobar'], - - [null, 'integer'], - ['abc', 'integer'], - ['76ggg', 'double'], - ['true', 'float'], - ['foobar', 'boolean'], - ]; - } - - /** - * @dataProvider castValueInvalidProvider - */ - public function testCastValueInvalid($value, $type): void { - $this->expectException(\InvalidArgumentException::class); - - $this->invokePrivate($this->command, 'castValue', [$value, $type]); - } } diff --git a/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php b/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php index b124a27f9ab..0bc6cbb64cf 100644 --- a/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php +++ b/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -8,6 +9,7 @@ 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; @@ -44,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 { @@ -76,9 +78,7 @@ class ChangeKeyStorageRootTest extends TestCase { ); } - /** - * @dataProvider dataTestExecute - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestExecute')] public function testExecute($newRoot, $answer, $successMoveKey): void { $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') ->setConstructorArgs( @@ -89,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(''); @@ -122,7 +122,7 @@ class ChangeKeyStorageRootTest extends TestCase { ); } - public function dataTestExecute() { + public static function dataTestExecute(): array { return [ [null, true, true], [null, true, false], @@ -133,7 +133,7 @@ class ChangeKeyStorageRootTest extends TestCase { } public function testMoveAllKeys(): void { - /** @var \OC\Core\Command\Encryption\ChangeKeyStorageRoot $changeKeyStorageRoot */ + /** @var ChangeKeyStorageRoot $changeKeyStorageRoot */ $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') ->setConstructorArgs( [ @@ -143,7 +143,7 @@ class ChangeKeyStorageRootTest extends TestCase { $this->util, $this->questionHelper ] - )->setMethods(['prepareNewRoot', 'moveSystemKeys', 'moveUserKeys'])->getMock(); + )->onlyMethods(['prepareNewRoot', 'moveSystemKeys', 'moveUserKeys'])->getMock(); $changeKeyStorageRoot->expects($this->once())->method('prepareNewRoot')->with('newRoot'); $changeKeyStorageRoot->expects($this->once())->method('moveSystemKeys')->with('oldRoot', 'newRoot'); @@ -157,18 +157,18 @@ class ChangeKeyStorageRootTest extends TestCase { ->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 */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestPrepareNewRootException')] public function testPrepareNewRootException($dirExists, $couldCreateFile): void { $this->expectException(\Exception::class); @@ -179,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], @@ -188,12 +188,12 @@ class ChangeKeyStorageRootTest extends TestCase { } /** - * @dataProvider dataTestMoveSystemKeys * * @param bool $dirExists * @param bool $targetExists * @param bool $executeRename */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestMoveSystemKeys')] public function testMoveSystemKeys($dirExists, $targetExists, $executeRename): void { $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') ->setConstructorArgs( @@ -204,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); @@ -221,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], @@ -241,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]); @@ -254,13 +254,13 @@ class ChangeKeyStorageRootTest extends TestCase { } /** - * @dataProvider dataTestMoveUserEncryptionFolder * * @param bool $userExists * @param bool $isDir * @param bool $targetExists * @param bool $shouldRename */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestMoveUserEncryptionFolder')] public function testMoveUserEncryptionFolder($userExists, $isDir, $targetExists, $shouldRename): void { $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') ->setConstructorArgs( @@ -271,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); @@ -293,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], @@ -306,9 +306,7 @@ class ChangeKeyStorageRootTest extends TestCase { } - /** - * @dataProvider dataTestPrepareParentFolder - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestPrepareParentFolder')] public function testPrepareParentFolder($path, $pathExists): void { $this->view->expects($this->any())->method('file_exists') ->willReturnCallback( @@ -334,7 +332,7 @@ 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] diff --git a/tests/Core/Command/Encryption/DecryptAllTest.php b/tests/Core/Command/Encryption/DecryptAllTest.php index 6ed4cbbea99..41d9e4c713f 100644 --- a/tests/Core/Command/Encryption/DecryptAllTest.php +++ b/tests/Core/Command/Encryption/DecryptAllTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -17,13 +18,13 @@ 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 */ 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 */ @@ -73,12 +74,16 @@ class DecryptAllTest extends TestCase { public function testMaintenanceAndTrashbin(): void { // on construct we enable single-user-mode and disable the trash bin // 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') - ->withConsecutive( - ['maintenance', true], - ['maintenance', false], - ); + ->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'); @@ -105,9 +110,7 @@ class DecryptAllTest extends TestCase { $this->invokePrivate($instance, 'resetMaintenanceAndTrashbin'); } - /** - * @dataProvider dataTestExecute - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestExecute')] public function testExecute($encryptionEnabled, $continue): void { $instance = new DecryptAll( $this->encryptionManager, @@ -127,12 +130,16 @@ class DecryptAllTest extends TestCase { ->willReturn('user1'); if ($encryptionEnabled) { + $calls = [ + ['core', 'encryption_enabled', 'no'], + ['core', 'encryption_enabled', 'yes'], + ]; $this->config->expects($this->exactly(2)) ->method('setAppValue') - ->withConsecutive( - ['core', 'encryption_enabled', 'no'], - ['core', 'encryption_enabled', 'yes'], - ); + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->questionHelper->expects($this->once()) ->method('ask') ->willReturn($continue); @@ -152,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], @@ -174,13 +181,16 @@ class DecryptAllTest extends TestCase { ); // make sure that we enable encryption again after a exception was thrown + $calls = [ + ['core', 'encryption_enabled', 'no'], + ['core', 'encryption_enabled', 'yes'], + ]; $this->config->expects($this->exactly(2)) ->method('setAppValue') - ->withConsecutive( - ['core', 'encryption_enabled', 'no'], - ['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); @@ -197,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 913d03a8850..a89fd636e47 100644 --- a/tests/Core/Command/Encryption/DisableTest.php +++ b/tests/Core/Command/Encryption/DisableTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -33,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'], @@ -46,12 +47,12 @@ class DisableTest extends TestCase { } /** - * @dataProvider dataDisable * * @param string $oldStatus * @param bool $isUpdating * @param string $expectedString */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataDisable')] public function testDisable($oldStatus, $isUpdating, $expectedString): void { $this->config->expects($this->once()) ->method('getAppValue') diff --git a/tests/Core/Command/Encryption/EnableTest.php b/tests/Core/Command/Encryption/EnableTest.php index 2d463452add..32d1a7576f5 100644 --- a/tests/Core/Command/Encryption/EnableTest.php +++ b/tests/Core/Command/Encryption/EnableTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -45,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'], @@ -55,17 +56,8 @@ 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): void { + #[\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') @@ -79,27 +71,30 @@ class EnableTest extends TestCase { if (empty($availableModules)) { $this->config->expects($this->once()) ->method('getAppValue') - ->with('core', 'encryption_enabled', $this->anything()) - ->willReturn($oldStatus); + ->willReturnMap([ + ['core', 'encryption_enabled', 'no', $oldStatus], + ]); } else { $this->config->expects($this->exactly(2)) ->method('getAppValue') - ->withConsecutive( - ['core', 'encryption_enabled', $this->anything()], - ['core', 'default_encryption_module', $this->anything()], - )->willReturnOnConsecutiveCalls( - $oldStatus, - $defaultModule, - ); + ->willReturnMap([ + ['core', 'encryption_enabled', 'no', $oldStatus], + ['core', 'default_encryption_module', null, $defaultModule], + ]); } + $calls = [ + [$expectedString, 0], + ['', 0], + [$expectedDefaultModuleString, 0], + ]; $this->consoleOutput->expects($this->exactly(3)) ->method('writeln') - ->withConsecutive( - [$this->stringContains($expectedString)], - [''], - [$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 6e72e87b973..15cbe83737d 100644 --- a/tests/Core/Command/Encryption/EncryptAllTest.php +++ b/tests/Core/Command/Encryption/EncryptAllTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -12,59 +13,36 @@ 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(): void { @@ -77,9 +55,7 @@ class EncryptAllTest extends TestCase { $this->invokePrivate($instance, 'resetMaintenanceAndTrashbin'); } - /** - * @dataProvider dataTestExecute - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestExecute')] public function testExecute($answer, $askResult): void { $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper); @@ -99,7 +75,7 @@ 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] ]; diff --git a/tests/Core/Command/Encryption/SetDefaultModuleTest.php b/tests/Core/Command/Encryption/SetDefaultModuleTest.php index 0a2c2a81c28..df38d730db3 100644 --- a/tests/Core/Command/Encryption/SetDefaultModuleTest.php +++ b/tests/Core/Command/Encryption/SetDefaultModuleTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -43,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'], @@ -52,7 +53,6 @@ class SetDefaultModuleTest extends TestCase { } /** - * @dataProvider dataSetDefaultModule * * @param string $oldModule * @param string $newModule @@ -60,6 +60,7 @@ class SetDefaultModuleTest extends TestCase { * @param bool $updateSuccess * @param string $expectedString */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataSetDefaultModule')] public function testSetDefaultModule($oldModule, $newModule, $updateModule, $updateSuccess, $expectedString): void { $this->consoleInput->expects($this->once()) ->method('getArgument') @@ -90,7 +91,6 @@ class SetDefaultModuleTest extends TestCase { } /** - * @dataProvider dataSetDefaultModule * * @param string $oldModule * @param string $newModule @@ -98,6 +98,7 @@ class SetDefaultModuleTest extends TestCase { * @param bool $updateSuccess * @param string $expectedString */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataSetDefaultModule')] public function testMaintenanceMode($oldModule, $newModule, $updateModule, $updateSuccess, $expectedString): void { $this->consoleInput->expects($this->never()) ->method('getArgument') @@ -113,12 +114,16 @@ class SetDefaultModuleTest extends TestCase { ->with('maintenance', false) ->willReturn(true); + $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') - ->withConsecutive( - [$this->stringContains('Maintenance mode must be disabled when setting default module,')], - [$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]); } diff --git a/tests/Core/Command/Group/AddTest.php b/tests/Core/Command/Group/AddTest.php index c953199766f..24f2d823292 100644 --- a/tests/Core/Command/Group/AddTest.php +++ b/tests/Core/Command/Group/AddTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/tests/Core/Command/Group/AddUserTest.php b/tests/Core/Command/Group/AddUserTest.php index 4bbf54f7b3f..68c8cecdba1 100644 --- a/tests/Core/Command/Group/AddUserTest.php +++ b/tests/Core/Command/Group/AddUserTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/tests/Core/Command/Group/DeleteTest.php b/tests/Core/Command/Group/DeleteTest.php index 4b4f16feb76..289c6a7c322 100644 --- a/tests/Core/Command/Group/DeleteTest.php +++ b/tests/Core/Command/Group/DeleteTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/tests/Core/Command/Group/InfoTest.php b/tests/Core/Command/Group/InfoTest.php index 869b6741866..87f59d2adc4 100644 --- a/tests/Core/Command/Group/InfoTest.php +++ b/tests/Core/Command/Group/InfoTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -32,7 +33,7 @@ class InfoTest extends TestCase { $this->groupManager = $this->createMock(IGroupManager::class); $this->command = $this->getMockBuilder(Info::class) ->setConstructorArgs([$this->groupManager]) - ->setMethods(['writeArrayInOutputFormat']) + ->onlyMethods(['writeArrayInOutputFormat']) ->getMock(); $this->input = $this->createMock(InputInterface::class); diff --git a/tests/Core/Command/Group/ListCommandTest.php b/tests/Core/Command/Group/ListCommandTest.php index 93d472af388..aaca772d714 100644 --- a/tests/Core/Command/Group/ListCommandTest.php +++ b/tests/Core/Command/Group/ListCommandTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -33,7 +34,7 @@ class ListCommandTest extends TestCase { $this->groupManager = $this->createMock(IGroupManager::class); $this->command = $this->getMockBuilder(ListCommand::class) ->setConstructorArgs([$this->groupManager]) - ->setMethods(['writeArrayInOutputFormat']) + ->onlyMethods(['writeArrayInOutputFormat']) ->getMock(); $this->input = $this->createMock(InputInterface::class); diff --git a/tests/Core/Command/Group/RemoveUserTest.php b/tests/Core/Command/Group/RemoveUserTest.php index 98fe41784f1..74343e77d3f 100644 --- a/tests/Core/Command/Group/RemoveUserTest.php +++ b/tests/Core/Command/Group/RemoveUserTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/tests/Core/Command/Log/FileTest.php b/tests/Core/Command/Log/FileTest.php index 9c7e0297d53..1aaf398b875 100644 --- a/tests/Core/Command/Log/FileTest.php +++ b/tests/Core/Command/Log/FileTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -62,7 +63,7 @@ class FileTest extends TestCase { self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); } - public function changeRotateSizeProvider() { + public static function changeRotateSizeProvider(): array { return [ ['42', 42], ['0', 0], @@ -71,9 +72,7 @@ class FileTest extends TestCase { ]; } - /** - * @dataProvider changeRotateSizeProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('changeRotateSizeProvider')] public function testChangeRotateSize($optionValue, $configValue): void { $this->config->method('getSystemValue')->willReturnArgument(1); $this->consoleInput->method('getOption') @@ -96,13 +95,17 @@ class FileTest extends TestCase { ['log_rotate_size', 100 * 1024 * 1024, 5 * 1024 * 1024], ]); + $calls = [ + ['Log backend file: disabled'], + ['Log file: /var/log/nextcloud.log'], + ['Rotate at: 5 MB'], + ]; $this->consoleOutput->expects($this->exactly(3)) ->method('writeln') - ->withConsecutive( - ['Log backend file: disabled'], - ['Log file: /var/log/nextcloud.log'], - ['Rotate at: 5 MB'], - ); + ->willReturnCallback(function (string $message) use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected[0], $message); + }); self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); } diff --git a/tests/Core/Command/Log/ManageTest.php b/tests/Core/Command/Log/ManageTest.php index b354bb17076..8b307048719 100644 --- a/tests/Core/Command/Log/ManageTest.php +++ b/tests/Core/Command/Log/ManageTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -87,7 +88,7 @@ class ManageTest extends TestCase { self::invokePrivate($this->command, 'validateTimezone', ['Mars/OlympusMons']); } - public function convertLevelStringProvider() { + public static function dataConvertLevelString(): array { return [ ['dEbug', 0], ['inFO', 1], @@ -99,10 +100,8 @@ class ManageTest extends TestCase { ]; } - /** - * @dataProvider convertLevelStringProvider - */ - public function testConvertLevelString($levelString, $expectedInt): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataConvertLevelString')] + public function testConvertLevelString(string $levelString, int $expectedInt): void { $this->assertEquals($expectedInt, self::invokePrivate($this->command, 'convertLevelString', [$levelString]) ); @@ -115,7 +114,7 @@ class ManageTest extends TestCase { self::invokePrivate($this->command, 'convertLevelString', ['abc']); } - public function convertLevelNumberProvider() { + public static function dataConvertLevelNumber(): array { return [ [0, 'Debug'], [1, 'Info'], @@ -125,10 +124,8 @@ class ManageTest extends TestCase { ]; } - /** - * @dataProvider convertLevelNumberProvider - */ - public function testConvertLevelNumber($levelNum, $expectedString): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataConvertLevelNumber')] + public function testConvertLevelNumber(int $levelNum, string $expectedString): void { $this->assertEquals($expectedString, self::invokePrivate($this->command, 'convertLevelNumber', [$levelNum]) ); @@ -144,23 +141,23 @@ class ManageTest extends TestCase { public function testGetConfiguration(): void { $this->config->expects($this->exactly(3)) ->method('getSystemValue') - ->withConsecutive( - ['log_type', 'file'], - ['loglevel', 2], - ['logtimezone', 'UTC'], - )->willReturnOnConsecutiveCalls( - 'log_type_value', - 0, - 'logtimezone_value' - ); + ->willReturnMap([ + ['log_type', 'file', 'log_type_value'], + ['loglevel', 2, 0], + ['logtimezone', 'UTC', 'logtimezone_value'], + ]); + $calls = [ + ['Enabled logging backend: log_type_value'], + ['Log level: Debug (0)'], + ['Log timezone: logtimezone_value'], + ]; $this->consoleOutput->expects($this->exactly(3)) ->method('writeln') - ->withConsecutive( - ['Enabled logging backend: log_type_value'], - ['Log level: Debug (0)'], - ['Log timezone: logtimezone_value'], - ); + ->willReturnCallback(function (string $message) use (&$calls): void { + $call = array_shift($calls); + $this->assertStringContainsString($call[0], $message); + }); self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); } diff --git a/tests/Core/Command/Maintenance/DataFingerprintTest.php b/tests/Core/Command/Maintenance/DataFingerprintTest.php index 3d56d891bef..99004a7a5f5 100644 --- a/tests/Core/Command/Maintenance/DataFingerprintTest.php +++ b/tests/Core/Command/Maintenance/DataFingerprintTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -35,7 +36,7 @@ class DataFingerprintTest 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 DataFingerprint($this->config, $this->timeFactory); } diff --git a/tests/Core/Command/Maintenance/Mimetype/UpdateDBTest.php b/tests/Core/Command/Maintenance/Mimetype/UpdateDBTest.php index d8c82de19ac..b85dcf87bbc 100644 --- a/tests/Core/Command/Maintenance/Mimetype/UpdateDBTest.php +++ b/tests/Core/Command/Maintenance/Mimetype/UpdateDBTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -33,15 +34,10 @@ class UpdateDBTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->detector = $this->getMockBuilder(Detection::class) - ->disableOriginalConstructor() - ->getMock(); - $this->loader = $this->getMockBuilder(Loader::class) - ->disableOriginalConstructor() - ->getMock(); - - $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); - $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); + $this->detector = $this->createMock(Detection::class); + $this->loader = $this->createMock(Loader::class); + $this->consoleInput = $this->createMock(InputInterface::class); + $this->consoleOutput = $this->createMock(OutputInterface::class); $this->command = new UpdateDB($this->detector, $this->loader); } @@ -64,12 +60,16 @@ class UpdateDBTest extends TestCase { $this->loader->expects($this->never()) ->method('updateFilecache'); + $calls = [ + 'Added 0 new mimetypes', + 'Updated 0 filecache rows', + ]; $this->consoleOutput->expects($this->exactly(2)) ->method('writeln') - ->withConsecutive( - ['Added 0 new mimetypes'], - ['Updated 0 filecache rows'], - ); + ->willReturnCallback(function ($message) use (&$calls): void { + $expected = array_shift($calls); + $this->assertStringContainsString($expected, $message); + }); self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); } @@ -103,14 +103,18 @@ class UpdateDBTest extends TestCase { ->with('new', 2) ->willReturn(3); + $calls = [ + 'Added mimetype "testing/newmimetype" to database', + 'Updated 3 filecache rows for mimetype "testing/newmimetype"', + 'Added 1 new mimetypes', + 'Updated 3 filecache rows', + ]; $this->consoleOutput->expects($this->exactly(4)) ->method('writeln') - ->withConsecutive( - ['Added mimetype "testing/newmimetype" to database'], - ['Updated 3 filecache rows for mimetype "testing/newmimetype"'], - ['Added 1 new mimetypes'], - ['Updated 3 filecache rows'], - ); + ->willReturnCallback(function ($message) use (&$calls): void { + $expected = array_shift($calls); + $this->assertStringContainsString($expected, $message); + }); self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); } @@ -153,13 +157,17 @@ class UpdateDBTest extends TestCase { ->with('ext', 1) ->willReturn(3); + $calls = [ + 'Updated 3 filecache rows for mimetype "testing/existingmimetype"', + 'Added 0 new mimetypes', + 'Updated 3 filecache rows', + ]; $this->consoleOutput->expects($this->exactly(3)) ->method('writeln') - ->withConsecutive( - ['Updated 3 filecache rows for mimetype "testing/existingmimetype"'], - ['Added 0 new mimetypes'], - ['Updated 3 filecache rows'], - ); + ->willReturnCallback(function ($message) use (&$calls): void { + $expected = array_shift($calls); + $this->assertStringContainsString($expected, $message); + }); self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); } diff --git a/tests/Core/Command/Maintenance/ModeTest.php b/tests/Core/Command/Maintenance/ModeTest.php index a4c33474745..5a9a90b0197 100644 --- a/tests/Core/Command/Maintenance/ModeTest.php +++ b/tests/Core/Command/Maintenance/ModeTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -67,7 +68,7 @@ class ModeTest extends TestCase { * * @return array */ - public function getExecuteTestData(): array { + public static function getExecuteTestData(): array { return [ 'off -> on' => [ 'on', // command option @@ -111,7 +112,6 @@ class ModeTest extends TestCase { /** * Asserts that execute works as expected. * - * @dataProvider getExecuteTestData * @param string $option The command option. * @param bool $currentMaintenanceState The current maintenance state. * @param null|bool $expectedMaintenanceState @@ -119,6 +119,7 @@ class ModeTest extends TestCase { * @param string $expectedOutput The expected command output. * @throws \Exception */ + #[\PHPUnit\Framework\Attributes\DataProvider('getExecuteTestData')] public function testExecute( string $option, bool $currentMaintenanceState, diff --git a/tests/Core/Command/Maintenance/UpdateTheme.php b/tests/Core/Command/Maintenance/UpdateTheme.php index 41b95d358d3..9c9a2b903a7 100644 --- a/tests/Core/Command/Maintenance/UpdateTheme.php +++ b/tests/Core/Command/Maintenance/UpdateTheme.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/tests/Core/Command/Preview/CleanupTest.php b/tests/Core/Command/Preview/CleanupTest.php index fcbb7582ca3..e4a83246e5b 100644 --- a/tests/Core/Command/Preview/CleanupTest.php +++ b/tests/Core/Command/Preview/CleanupTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -99,9 +100,7 @@ class CleanupTest extends TestCase { $this->assertEquals(1, $this->repair->run($this->input, $this->output)); } - /** - * @dataProvider dataForTestCleanupWithDeleteException - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataForTestCleanupWithDeleteException')] public function testCleanupWithDeleteException(string $exceptionClass, string $errorMessage): void { $previewFolder = $this->createMock(Folder::class); $previewFolder->expects($this->once()) diff --git a/tests/Core/Command/Preview/RepairTest.php b/tests/Core/Command/Preview/RepairTest.php index e54f4dde3ad..9b9cde6dd95 100644 --- a/tests/Core/Command/Preview/RepairTest.php +++ b/tests/Core/Command/Preview/RepairTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -16,6 +17,7 @@ use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Symfony\Component\Console\Formatter\OutputFormatterInterface; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\Console\Output\OutputInterface; use Test\TestCase; @@ -54,8 +56,7 @@ class RepairTest extends TestCase { $this->iniGetWrapper, $this->createMock(ILockingProvider::class) ); - $this->input = $this->getMockBuilder(InputInterface::class) - ->getMock(); + $this->input = $this->createMock(InputInterface::class); $this->input->expects($this->any()) ->method('getOption') ->willReturnCallback(function ($parameter) { @@ -64,13 +65,10 @@ class RepairTest extends TestCase { } return null; }); - $this->output = $this->getMockBuilder(OutputInterface::class) - ->setMethods(['section', 'writeln', 'write', 'setVerbosity', 'getVerbosity', 'isQuiet', 'isVerbose', 'isVeryVerbose', 'isDebug', 'setDecorated', 'isDecorated', 'setFormatter', 'getFormatter']) + $this->output = $this->getMockBuilder(ConsoleOutput::class) + ->onlyMethods(['section', 'writeln', 'getFormatter']) ->getMock(); $self = $this; - $this->output->expects($this->any()) - ->method('section') - ->willReturn($this->output); /* We need format method to return a string */ $outputFormatter = $this->createMock(OutputFormatterInterface::class); @@ -82,12 +80,12 @@ class RepairTest extends TestCase { ->willReturn($outputFormatter); $this->output->expects($this->any()) ->method('writeln') - ->willReturnCallback(function ($line) use ($self) { + ->willReturnCallback(function ($line) use ($self): void { $self->outputLines .= $line . "\n"; }); } - public function emptyTestDataProvider() { + public static function dataEmptyTest(): array { /** directoryNames, expectedOutput */ return [ [ @@ -113,9 +111,7 @@ class RepairTest extends TestCase { ]; } - /** - * @dataProvider emptyTestDataProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataEmptyTest')] public function testEmptyExecute($directoryNames, $expectedOutput): void { $previewFolder = $this->getMockBuilder(Folder::class) ->getMock(); diff --git a/tests/Core/Command/SystemTag/AddTest.php b/tests/Core/Command/SystemTag/AddTest.php index 7ae832e4079..5f3c7174758 100644 --- a/tests/Core/Command/SystemTag/AddTest.php +++ b/tests/Core/Command/SystemTag/AddTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -33,7 +34,7 @@ class AddTest extends TestCase { $this->systemTagManager = $this->createMock(ISystemTagManager::class); $this->command = $this->getMockBuilder(Add::class) ->setConstructorArgs([$this->systemTagManager]) - ->setMethods(['writeArrayInOutputFormat']) + ->onlyMethods(['writeArrayInOutputFormat']) ->getMock(); $this->input = $this->createMock(InputInterface::class); @@ -93,7 +94,7 @@ class AddTest extends TestCase { $tag->method('getAccessLevel')->willReturn(ISystemTag::ACCESS_LEVEL_PUBLIC); $this->systemTagManager->method('createTag') - ->willReturnCallback(function ($tagName, $userVisible, $userAssignable) { + ->willReturnCallback(function ($tagName, $userVisible, $userAssignable): void { throw new TagAlreadyExistsException( 'Tag ("' . $tagName . '", ' . $userVisible . ', ' . $userAssignable . ') already exists' ); diff --git a/tests/Core/Command/SystemTag/DeleteTest.php b/tests/Core/Command/SystemTag/DeleteTest.php index 78d073db6d6..bf756311000 100644 --- a/tests/Core/Command/SystemTag/DeleteTest.php +++ b/tests/Core/Command/SystemTag/DeleteTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -32,7 +33,7 @@ class DeleteTest extends TestCase { $this->systemTagManager = $this->createMock(ISystemTagManager::class); $this->command = $this->getMockBuilder(Delete::class) ->setConstructorArgs([$this->systemTagManager]) - ->setMethods(['writeArrayInOutputFormat']) + ->onlyMethods(['writeArrayInOutputFormat']) ->getMock(); $this->input = $this->createMock(InputInterface::class); @@ -69,7 +70,7 @@ class DeleteTest extends TestCase { }); $this->systemTagManager->method('deleteTags') - ->willReturnCallback(function ($tagId) { + ->willReturnCallback(function ($tagId): void { throw new TagNotFoundException(); }); diff --git a/tests/Core/Command/SystemTag/EditTest.php b/tests/Core/Command/SystemTag/EditTest.php index 0d2f6ba4fbc..b22151e3608 100644 --- a/tests/Core/Command/SystemTag/EditTest.php +++ b/tests/Core/Command/SystemTag/EditTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -33,7 +34,7 @@ class EditTest extends TestCase { $this->systemTagManager = $this->createMock(ISystemTagManager::class); $this->command = $this->getMockBuilder(Edit::class) ->setConstructorArgs([$this->systemTagManager]) - ->setMethods(['writeArrayInOutputFormat']) + ->onlyMethods(['writeArrayInOutputFormat']) ->getMock(); $this->input = $this->createMock(InputInterface::class); @@ -134,7 +135,7 @@ class EditTest extends TestCase { }); $this->systemTagManager->method('updateTag') - ->willReturnCallback(function ($tagId, $tagName, $userVisible, $userAssignable) { + ->willReturnCallback(function ($tagId, $tagName, $userVisible, $userAssignable): void { throw new TagAlreadyExistsException( 'Tag ("' . $tagName . '", ' . $userVisible . ', ' . $userAssignable . ') already exists' ); diff --git a/tests/Core/Command/SystemTag/ListCommandTest.php b/tests/Core/Command/SystemTag/ListCommandTest.php index 9f4c4072ffc..e1ff8290633 100644 --- a/tests/Core/Command/SystemTag/ListCommandTest.php +++ b/tests/Core/Command/SystemTag/ListCommandTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -32,7 +33,7 @@ class ListCommandTest extends TestCase { $this->systemTagManager = $this->createMock(ISystemTagManager::class); $this->command = $this->getMockBuilder(ListCommand::class) ->setConstructorArgs([$this->systemTagManager]) - ->setMethods(['writeArrayInOutputFormat']) + ->onlyMethods(['writeArrayInOutputFormat']) ->getMock(); $this->input = $this->createMock(InputInterface::class); diff --git a/tests/Core/Command/User/AddTest.php b/tests/Core/Command/User/AddTest.php index 7a956c173c1..5a8bc3abea1 100644 --- a/tests/Core/Command/User/AddTest.php +++ b/tests/Core/Command/User/AddTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -83,9 +84,7 @@ class AddTest extends TestCase { ); } - /** - * @dataProvider addEmailDataProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('addEmailDataProvider')] public function testAddEmail( ?string $email, bool $isEmailValid, @@ -111,11 +110,11 @@ class AddTest extends TestCase { ->method('sendMail'); $this->consoleInput->method('getOption') - ->will(static::returnValueMap([ + ->willReturnMap([ ['generate-password', 'true'], ['email', $email], ['group', []], - ])); + ]); $this->invokePrivate($this->addCommand, 'execute', [ $this->consoleInput, @@ -126,7 +125,7 @@ class AddTest extends TestCase { /** * @return array */ - public function addEmailDataProvider(): array { + public static function addEmailDataProvider(): array { return [ 'Valid E-Mail' => [ 'info@example.com', diff --git a/tests/Core/Command/User/AuthTokens/DeleteTest.php b/tests/Core/Command/User/AuthTokens/DeleteTest.php index 8588c1d5d54..6692473c240 100644 --- a/tests/Core/Command/User/AuthTokens/DeleteTest.php +++ b/tests/Core/Command/User/AuthTokens/DeleteTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -40,8 +41,10 @@ class DeleteTest extends TestCase { public function testDeleteTokenById(): void { $this->consoleInput->expects($this->exactly(2)) ->method('getArgument') - ->withConsecutive(['uid'], ['id']) - ->willReturnOnConsecutiveCalls('user', 42); + ->willReturnMap([ + ['uid', 'user'], + ['id', '42'] + ]); $this->consoleInput->expects($this->once()) ->method('getOption') @@ -59,8 +62,10 @@ class DeleteTest extends TestCase { public function testDeleteTokenByIdRequiresTokenId(): void { $this->consoleInput->expects($this->exactly(2)) ->method('getArgument') - ->withConsecutive(['uid'], ['id']) - ->willReturnOnConsecutiveCalls('user', null); + ->willReturnMap([ + ['uid', 'user'], + ['id', null] + ]); $this->consoleInput->expects($this->once()) ->method('getOption') @@ -78,8 +83,10 @@ class DeleteTest extends TestCase { public function testDeleteTokensLastUsedBefore(): void { $this->consoleInput->expects($this->exactly(2)) ->method('getArgument') - ->withConsecutive(['uid'], ['id']) - ->willReturnOnConsecutiveCalls('user', null); + ->willReturnMap([ + ['uid', 'user'], + ['id', null] + ]); $this->consoleInput->expects($this->once()) ->method('getOption') @@ -97,8 +104,10 @@ class DeleteTest extends TestCase { public function testLastUsedBeforeAcceptsIso8601Expanded(): void { $this->consoleInput->expects($this->exactly(2)) ->method('getArgument') - ->withConsecutive(['uid'], ['id']) - ->willReturnOnConsecutiveCalls('user', null); + ->willReturnMap([ + ['uid', 'user'], + ['id', null] + ]); $this->consoleInput->expects($this->once()) ->method('getOption') @@ -116,8 +125,10 @@ class DeleteTest extends TestCase { public function testLastUsedBeforeAcceptsYmd(): void { $this->consoleInput->expects($this->exactly(2)) ->method('getArgument') - ->withConsecutive(['uid'], ['id']) - ->willReturnOnConsecutiveCalls('user', null); + ->willReturnMap([ + ['uid', 'user'], + ['id', null] + ]); $this->consoleInput->expects($this->once()) ->method('getOption') @@ -135,8 +146,10 @@ class DeleteTest extends TestCase { public function testIdAndLastUsedBeforeAreMutuallyExclusive(): void { $this->consoleInput->expects($this->exactly(2)) ->method('getArgument') - ->withConsecutive(['uid'], ['id']) - ->willReturnOnConsecutiveCalls('user', 42); + ->willReturnMap([ + ['uid', 'user'], + ['id', '42'] + ]); $this->consoleInput->expects($this->once()) ->method('getOption') diff --git a/tests/Core/Command/User/DeleteTest.php b/tests/Core/Command/User/DeleteTest.php index 42244412d9f..4e06b0f91fc 100644 --- a/tests/Core/Command/User/DeleteTest.php +++ b/tests/Core/Command/User/DeleteTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -34,12 +35,12 @@ class DeleteTest extends TestCase { $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); - /** @var \OCP\IUserManager $userManager */ + /** @var IUserManager $userManager */ $this->command = new Delete($userManager); } - public function validUserLastSeen() { + public static function validUserLastSeen(): array { return [ [true, 'The specified user was deleted'], [false, 'The specified user could not be deleted'], @@ -47,11 +48,11 @@ class DeleteTest extends TestCase { } /** - * @dataProvider validUserLastSeen * * @param bool $deleteSuccess * @param string $expectedString */ + #[\PHPUnit\Framework\Attributes\DataProvider('validUserLastSeen')] public function testValidUser($deleteSuccess, $expectedString): void { $user = $this->getMockBuilder(IUser::class)->getMock(); $user->expects($this->once()) diff --git a/tests/Core/Command/User/DisableTest.php b/tests/Core/Command/User/DisableTest.php index 1e464ca93a5..c1bc10dc6bf 100644 --- a/tests/Core/Command/User/DisableTest.php +++ b/tests/Core/Command/User/DisableTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/tests/Core/Command/User/EnableTest.php b/tests/Core/Command/User/EnableTest.php index b590530d93d..b2820de14ef 100644 --- a/tests/Core/Command/User/EnableTest.php +++ b/tests/Core/Command/User/EnableTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/tests/Core/Command/User/LastSeenTest.php b/tests/Core/Command/User/LastSeenTest.php index 014c981112e..64c710eacc5 100644 --- a/tests/Core/Command/User/LastSeenTest.php +++ b/tests/Core/Command/User/LastSeenTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -34,11 +35,11 @@ class LastSeenTest extends TestCase { $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); - /** @var \OCP\IUserManager $userManager */ + /** @var IUserManager $userManager */ $this->command = new LastSeen($userManager); } - public function validUserLastSeen() { + public static function validUserLastSeen(): array { return [ [0, 'never logged in'], [time(), 'last login'], @@ -46,11 +47,11 @@ class LastSeenTest extends TestCase { } /** - * @dataProvider validUserLastSeen * * @param int $lastSeen * @param string $expectedString */ + #[\PHPUnit\Framework\Attributes\DataProvider('validUserLastSeen')] public function testValidUser($lastSeen, $expectedString): void { $user = $this->getMockBuilder(IUser::class)->getMock(); $user->expects($this->once()) diff --git a/tests/Core/Command/User/ProfileTest.php b/tests/Core/Command/User/ProfileTest.php new file mode 100644 index 00000000000..ff5568bacfc --- /dev/null +++ b/tests/Core/Command/User/ProfileTest.php @@ -0,0 +1,465 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace Core\Command\User; + +use OC\Core\Command\User\Profile; +use OCP\Accounts\IAccount; +use OCP\Accounts\IAccountManager; +use OCP\Accounts\IAccountProperty; +use OCP\IDBConnection; +use OCP\IUser; +use OCP\IUserManager; +use PHPUnit\Framework\MockObject\MockObject; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Test\TestCase; + +class ProfileTest extends TestCase { + + protected IAccountManager&MockObject $accountManager; + protected IUserManager&MockObject $userManager; + protected IDBConnection&MockObject $connection; + protected InputInterface&MockObject $consoleInput; + protected OutputInterface&MockObject $consoleOutput; + + protected function setUp(): void { + parent::setUp(); + + $this->accountManager = $this->createMock(IAccountManager::class); + $this->userManager = $this->createMock(IUserManager::class); + $this->connection = $this->createMock(IDBConnection::class); + $this->consoleInput = $this->createMock(InputInterface::class); + $this->consoleOutput = $this->createMock(OutputInterface::class); + } + + public function getCommand(array $methods = []): Profile|MockObject { + if (empty($methods)) { + return new Profile($this->userManager, $this->accountManager); + } else { + return $this->getMockBuilder(Profile::class) + ->setConstructorArgs([ + $this->userManager, + $this->accountManager, + ]) + ->onlyMethods($methods) + ->getMock(); + } + } + + public static function dataCheckInput(): array { + return [ + 'Call with existing user should pass check' => [ + [['uid', 'username']], + [], + [], + true, + null, + ], + 'Call with non-existing user should fail check' => [ + [['uid', 'username']], + [], + [], + false, + 'The user "username" does not exist.', + ], + + 'Call with uid, key and --default value should pass check' => [ + [['uid', 'username'], ['key', 'configkey']], + [], + [['--default-value', false, true]], + true, + null, + ], + 'Call with uid and empty key with default-value option should fail check' => [ + [['uid', 'username'], ['key', '']], + [], + [['--default-value', false, true]], + true, + 'The "default-value" option can only be used when specifying a key.', + ], + + 'Call with uid, key, value should pass check' => [ + [['uid', 'username'], ['key', 'configkey'], ['value', '']], + [], + [], + true, + null, + ], + 'Call with uid, empty key and empty value should fail check' => [ + [['uid', 'username'], ['key', ''], ['value', '']], + [], + [], + true, + 'The value argument can only be used when specifying a key.', + ], + 'Call with uid, key, empty value and default-value option should fail check' => [ + [['uid', 'username'], ['key', 'configkey'], ['value', '']], + [], + [['--default-value', false, true]], + true, + 'The value argument can not be used together with "default-value".', + ], + 'Call with uid, key, empty value and update-only option should pass check' => [ + [['uid', 'username'], ['key', 'configkey'], ['value', '']], + [['update-only', true]], + [], + true, + null, + ], + 'Call with uid, key, null value and update-only option should fail check' => [ + [['uid', 'username'], ['key', 'configkey'], ['value', null]], + [['update-only', true]], + [], + true, + 'The "update-only" option can only be used together with "value".', + ], + + 'Call with uid, key and delete option should pass check' => [ + [['uid', 'username'], ['key', 'configkey']], + [['delete', true]], + [], + true, + null, + ], + 'Call with uid, empty key and delete option should fail check' => [ + [['uid', 'username'], ['key', '']], + [['delete', true]], + [], + true, + 'The "delete" option can only be used when specifying a key.', + ], + 'Call with uid, key, delete option and default-value should fail check' => [ + [['uid', 'username'], ['key', 'configkey']], + [['delete', true]], + [['--default-value', false, true]], + true, + 'The "delete" option can not be used together with "default-value".', + ], + 'Call with uid, key, empty value and delete option should fail check' => [ + [['uid', 'username'], ['key', 'configkey'], ['value', '']], + [['delete', true]], + [], + true, + 'The "delete" option can not be used together with "value".', + ], + 'Call with uid, key, delete and error-if-not-exists should pass check' => [ + [['uid', 'username'], ['key', 'configkey']], + [['delete', true], ['error-if-not-exists', true]], + [], + true, + null, + ], + 'Call with uid, key and error-if-not-exists should fail check' => [ + [['uid', 'username'], ['key', 'configkey']], + [['delete', false], ['error-if-not-exists', true]], + [], + true, + 'The "error-if-not-exists" option can only be used together with "delete".', + ], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('dataCheckInput')] + public function testCheckInput(array $arguments, array $options, array $parameterOptions, bool $existingUser, ?string $expectedException): void { + $this->consoleInput->expects($this->any()) + ->method('getArgument') + ->willReturnMap($arguments); + $this->consoleInput->expects($this->any()) + ->method('getOption') + ->willReturnMap($options); + $this->consoleInput->expects($this->any()) + ->method('hasParameterOption') + ->willReturnCallback(function (string|array $values, bool $onlyParams = false) use ($parameterOptions): bool { + $arguments = func_get_args(); + foreach ($parameterOptions as $parameterOption) { + // check the arguments of the function, if they are the same, return the mocked value + if (array_diff($arguments, $parameterOption) === []) { + return end($parameterOption); + } + } + + return false; + }); + + $returnedUser = null; + if ($existingUser) { + $mockUser = $this->createMock(IUser::class); + $mockUser->expects($this->once())->method('getUID')->willReturn('user'); + $returnedUser = $mockUser; + } + $this->userManager->expects($this->once()) + ->method('get') + ->willReturn($returnedUser); + + $command = $this->getCommand(); + try { + $this->invokePrivate($command, 'checkInput', [$this->consoleInput]); + $this->assertNull($expectedException); + } catch (\InvalidArgumentException $e) { + $this->assertEquals($expectedException, $e->getMessage()); + } + } + + public function testCheckInputExceptionCatch(): void { + $command = $this->getCommand(['checkInput']); + $command->expects($this->once()) + ->method('checkInput') + ->willThrowException(new \InvalidArgumentException('test')); + + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with('<error>test</error>'); + + $this->assertEquals(1, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput])); + } + + public static function dataExecuteDeleteProfileProperty(): array { + return [ + 'Deleting existing property should succeed' => ['address', 'Berlin', false, null, Command::SUCCESS], + 'Deleting existing property with error-if-not-exists should succeed' => ['address', 'Berlin', true, null, Command::SUCCESS], + 'Deleting non-existing property should succeed' => ['address', '', false, null, Command::SUCCESS], + 'Deleting non-existing property with error-if-not-exists should fail' => ['address', '', true, '<error>The property does not exist for user "username".</error>', Command::FAILURE], + ]; + } + + /** + * Tests the deletion mechanism on profile settings. + */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataExecuteDeleteProfileProperty')] + public function testExecuteDeleteProfileProperty(string $configKey, string $value, bool $errorIfNotExists, ?string $expectedLine, int $expectedReturn): void { + $uid = 'username'; + $appName = 'profile'; + $command = $this->getCommand([ + 'writeArrayInOutputFormat', + 'checkInput', + ]); + + $this->consoleInput->expects($this->any()) + ->method('getArgument') + ->willReturnMap([ + ['uid', $uid], + ['app', $appName], + ['key', $configKey], + ]); + + $mocks = $this->setupProfilePropertiesMock([$configKey => $value]); + + $command->expects($this->once()) + ->method('checkInput') + ->willReturn($mocks['userMock']); + + $this->consoleInput->expects($this->atLeastOnce()) + ->method('hasParameterOption') + ->willReturnMap([ + ['--delete', false, true], + ['--error-if-not-exists', false, $errorIfNotExists], + ]); + + if ($expectedLine === null) { + $this->consoleOutput->expects($this->never()) + ->method('writeln'); + $mocks['profilePropertiesMocks'][0]->expects($this->once()) + ->method('setValue') + ->with(''); + $this->accountManager->expects($this->once()) + ->method('updateAccount') + ->with($mocks['accountMock']); + } else { + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with($expectedLine); + $this->accountManager->expects($this->never()) + ->method('updateAccount'); + } + + $this->assertEquals($expectedReturn, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput])); + } + + public function testExecuteSetProfileProperty(): void { + $command = $this->getCommand([ + 'writeArrayInOutputFormat', + 'checkInput', + ]); + + $uid = 'username'; + $propertyKey = 'address'; + $propertyValue = 'Barcelona'; + + $this->consoleInput->expects($this->atLeast(3)) + ->method('getArgument') + ->willReturnMap([ + ['uid', $uid], + ['key', $propertyKey], + ['value', $propertyValue], + ]); + + $mocks = $this->setupProfilePropertiesMock([$propertyKey => $propertyValue]); + + $command->expects($this->once()) + ->method('checkInput') + ->willReturn($mocks['userMock']); + + $mocks['profilePropertiesMocks'][0]->expects($this->once()) + ->method('setValue') + ->with($propertyValue); + $this->accountManager->expects($this->once()) + ->method('updateAccount') + ->with($mocks['accountMock']); + + $this->assertEquals(0, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput])); + } + + public static function dataExecuteGet(): array { + return [ + 'Get property with set value should pass' => ['configkey', 'value', null, 'value', Command::SUCCESS], + 'Get property with empty value and default-value option should pass' => ['configkey', '', 'default-value', 'default-value', Command::SUCCESS], + 'Get property with empty value should fail' => ['configkey', '', null, '<error>The property does not exist for user "username".</error>', Command::FAILURE], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('dataExecuteGet')] + public function testExecuteGet(string $key, string $value, ?string $defaultValue, string $expectedLine, int $expectedReturn): void { + $command = $this->getCommand([ + 'writeArrayInOutputFormat', + 'checkInput', + ]); + + $uid = 'username'; + + $this->consoleInput->expects($this->any()) + ->method('getArgument') + ->willReturnMap([ + ['uid', $uid], + ['key', $key], + ]); + + $mocks = $this->setupProfilePropertiesMock([$key => $value]); + + $command->expects($this->once()) + ->method('checkInput') + ->willReturn($mocks['userMock']); + + if ($value === '') { + if ($defaultValue === null) { + $this->consoleInput->expects($this->atLeastOnce()) + ->method('hasParameterOption') + ->willReturn(false); + } else { + $this->consoleInput->expects($this->atLeastOnce()) + ->method('hasParameterOption') + ->willReturnCallback(fn (string|array $values): bool => $values === '--default-value'); + $this->consoleInput->expects($this->once()) + ->method('getOption') + ->with('default-value') + ->willReturn($defaultValue); + } + } + + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with($expectedLine); + + $this->assertEquals($expectedReturn, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput])); + } + + public function testExecuteList(): void { + $uid = 'username'; + $profileData = [ + 'pronouns' => 'they/them', + 'address' => 'Berlin', + ]; + + $command = $this->getCommand([ + 'writeArrayInOutputFormat', + 'checkInput', + ]); + + $this->consoleInput->expects($this->any()) + ->method('getArgument') + ->willReturnMap([ + ['uid', $uid], + ['key', ''], + ]); + + $mocks = $this->setupProfilePropertiesMock(['address' => $profileData['address'], 'pronouns' => $profileData['pronouns']]); + + $command->expects($this->once()) + ->method('checkInput') + ->willReturn($mocks['userMock']); + + $command->expects($this->once()) + ->method('writeArrayInOutputFormat') + ->with($this->consoleInput, $this->consoleOutput, $profileData); + + + $this->assertEquals(0, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput])); + } + + /** + * Helper to avoid boilerplate in tests in this file when mocking objects + * of IAccountProperty type. + * + * @param array<string, string> $properties the properties to be set up as key => value + * @return array{ + * userMock: IUser&MockObject, + * accountMock: IAccount&MockObject, + * profilePropertiesMocks: IAccountProperty&MockObject[] + * } + */ + private function setupProfilePropertiesMock(array $properties): array { + $userMock = $this->createMock(IUser::class); + $accountMock = $this->createMock(IAccount::class); + $this->accountManager->expects($this->atLeastOnce()) + ->method('getAccount') + ->with($userMock) + ->willReturn($accountMock); + + /** @var IAccountProperty&MockObject[] $propertiesMocks */ + $propertiesMocks = []; + foreach ($properties as $key => $value) { + $propertiesMocks[] = $this->getAccountPropertyMock($key, $value); + } + + if (count($properties) === 1) { + $accountMock->expects($this->atLeastOnce()) + ->method('getProperty') + ->with(array_keys($properties)[0]) + ->willReturn($propertiesMocks[array_key_first($propertiesMocks)]); + } else { + $accountMock->expects($this->atLeastOnce()) + ->method('getAllProperties') + ->willReturnCallback(function () use ($propertiesMocks) { + foreach ($propertiesMocks as $property) { + yield $property; + } + }); + } + + return [ + 'userMock' => $userMock, + 'accountMock' => $accountMock, + 'profilePropertiesMocks' => $propertiesMocks, + ]; + } + + private function getAccountPropertyMock(string $name, string $value): IAccountProperty&MockObject { + $propertyMock = $this->getMockBuilder(IAccountProperty::class) + ->disableOriginalConstructor() + ->getMock(); + $propertyMock->expects($this->any()) + ->method('getName') + ->willReturn($name); + $propertyMock->expects($this->any()) + ->method('getValue') + ->willReturn($value); + + return $propertyMock; + } +} diff --git a/tests/Core/Command/User/SettingTest.php b/tests/Core/Command/User/SettingTest.php index 62b75191d36..706e5b24742 100644 --- a/tests/Core/Command/User/SettingTest.php +++ b/tests/Core/Command/User/SettingTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -7,44 +8,31 @@ namespace Tests\Core\Command\User; +use InvalidArgumentException; use OC\Core\Command\User\Setting; use OCP\IConfig; use OCP\IDBConnection; use OCP\IUserManager; +use PHPUnit\Framework\MockObject\MockObject; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Test\TestCase; class SettingTest extends TestCase { - /** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $userManager; - /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */ - protected $config; - /** @var \OCP\IDBConnection|\PHPUnit\Framework\MockObject\MockObject */ - protected $connection; - /** @var \Symfony\Component\Console\Input\InputInterface|\PHPUnit\Framework\MockObject\MockObject */ - protected $consoleInput; - /** @var \Symfony\Component\Console\Output\OutputInterface|\PHPUnit\Framework\MockObject\MockObject */ - protected $consoleOutput; + protected IUserManager&MockObject $userManager; + protected IConfig&MockObject $config; + protected IDBConnection&MockObject $connection; + protected InputInterface&MockObject $consoleInput; + protected MockObject&OutputInterface $consoleOutput; protected function setUp(): void { parent::setUp(); - $this->userManager = $this->getMockBuilder(IUserManager::class) - ->disableOriginalConstructor() - ->getMock(); - $this->config = $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock(); - $this->connection = $this->getMockBuilder(IDBConnection::class) - ->disableOriginalConstructor() - ->getMock(); - $this->consoleInput = $this->getMockBuilder(InputInterface::class) - ->disableOriginalConstructor() - ->getMock(); - $this->consoleOutput = $this->getMockBuilder(OutputInterface::class) - ->disableOriginalConstructor() - ->getMock(); + $this->userManager = $this->createMock(IUserManager::class); + $this->config = $this->createMock(IConfig::class); + $this->connection = $this->createMock(IDBConnection::class); + $this->consoleInput = $this->createMock(InputInterface::class); + $this->consoleOutput = $this->createMock(OutputInterface::class); } public function getCommand(array $methods = []) { @@ -62,7 +50,7 @@ class SettingTest extends TestCase { } } - public function dataCheckInput() { + public static function dataCheckInput(): array { return [ [ [['uid', 'username']], @@ -176,7 +164,6 @@ class SettingTest extends TestCase { } /** - * @dataProvider dataCheckInput * * @param array $arguments * @param array $options @@ -184,6 +171,7 @@ class SettingTest extends TestCase { * @param mixed $user * @param string $expectedException */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataCheckInput')] public function testCheckInput($arguments, $options, $parameterOptions, $user, $expectedException): void { $this->consoleInput->expects($this->any()) ->method('getArgument') @@ -217,7 +205,7 @@ class SettingTest extends TestCase { try { $this->invokePrivate($command, 'checkInput', [$this->consoleInput]); $this->assertFalse($expectedException); - } catch (\InvalidArgumentException $e) { + } catch (InvalidArgumentException $e) { $this->assertEquals($expectedException, $e->getMessage()); } } @@ -226,7 +214,7 @@ class SettingTest extends TestCase { $command = $this->getCommand(['checkInput']); $command->expects($this->once()) ->method('checkInput') - ->willThrowException(new \InvalidArgumentException('test')); + ->willThrowException(new InvalidArgumentException('test')); $this->consoleOutput->expects($this->once()) ->method('writeln') @@ -235,7 +223,7 @@ class SettingTest extends TestCase { $this->assertEquals(1, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput])); } - public function dataExecuteDelete() { + public static function dataExecuteDelete(): array { return [ ['config', false, null, 0], ['config', true, null, 0], @@ -245,13 +233,13 @@ class SettingTest extends TestCase { } /** - * @dataProvider dataExecuteDelete * * @param string|null $value * @param bool $errorIfNotExists * @param string $expectedLine * @param int $expectedReturn */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataExecuteDelete')] public function testExecuteDelete($value, $errorIfNotExists, $expectedLine, $expectedReturn): void { $command = $this->getCommand([ 'writeArrayInOutputFormat', @@ -299,7 +287,7 @@ class SettingTest extends TestCase { $this->assertEquals($expectedReturn, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput])); } - public function dataExecuteSet() { + public static function dataExecuteSet(): array { return [ ['config', false, null, 0], ['config', true, null, 0], @@ -309,13 +297,13 @@ class SettingTest extends TestCase { } /** - * @dataProvider dataExecuteSet * * @param string|null $value * @param bool $updateOnly * @param string $expectedLine * @param int $expectedReturn */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataExecuteSet')] public function testExecuteSet($value, $updateOnly, $expectedLine, $expectedReturn): void { $command = $this->getCommand([ 'writeArrayInOutputFormat', @@ -367,7 +355,7 @@ class SettingTest extends TestCase { $this->assertEquals($expectedReturn, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput])); } - public function dataExecuteGet() { + public static function dataExecuteGet(): array { return [ ['config', null, 'config', 0], [null, 'config', 'config', 0], @@ -376,13 +364,13 @@ class SettingTest extends TestCase { } /** - * @dataProvider dataExecuteGet * * @param string|null $value * @param string|null $defaultValue * @param string $expectedLine * @param int $expectedReturn */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataExecuteGet')] public function testExecuteGet($value, $defaultValue, $expectedLine, $expectedReturn): void { $command = $this->getCommand([ 'writeArrayInOutputFormat', diff --git a/tests/Core/Controller/AppPasswordControllerTest.php b/tests/Core/Controller/AppPasswordControllerTest.php index b33033edac3..eb1566eca8b 100644 --- a/tests/Core/Controller/AppPasswordControllerTest.php +++ b/tests/Core/Controller/AppPasswordControllerTest.php @@ -123,7 +123,7 @@ class AppPasswordControllerTest extends TestCase { $credentials->method('getLoginName') ->willReturn('myLoginName'); $this->request->method('getHeader') - ->with('USER_AGENT') + ->with('user-agent') ->willReturn('myUA'); $this->random->method('generate') ->with( @@ -164,7 +164,7 @@ class AppPasswordControllerTest extends TestCase { $credentials->method('getLoginName') ->willReturn('myLoginName'); $this->request->method('getHeader') - ->with('USER_AGENT') + ->with('user-agent') ->willReturn('myUA'); $this->random->method('generate') ->with( diff --git a/tests/Core/Controller/AutoCompleteControllerTest.php b/tests/Core/Controller/AutoCompleteControllerTest.php index 23bd03be7af..c5574f78fc1 100644 --- a/tests/Core/Controller/AutoCompleteControllerTest.php +++ b/tests/Core/Controller/AutoCompleteControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -42,7 +43,7 @@ class AutoCompleteControllerTest extends TestCase { ); } - public function searchDataProvider() { + public static function searchDataProvider(): array { return [ [ #0 – regular search // searchResults @@ -153,9 +154,7 @@ class AutoCompleteControllerTest extends TestCase { ]; } - /** - * @dataProvider searchDataProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('searchDataProvider')] public function testGet(array $searchResults, array $expected, string $searchTerm, ?string $itemType, ?string $itemId, ?string $sorter): void { $this->collaboratorSearch->expects($this->once()) ->method('search') diff --git a/tests/Core/Controller/AvatarControllerTest.php b/tests/Core/Controller/AvatarControllerTest.php index 0dca611b020..a78e2c1bb5c 100644 --- a/tests/Core/Controller/AvatarControllerTest.php +++ b/tests/Core/Controller/AvatarControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -130,7 +131,7 @@ class AvatarControllerTest extends \Test\TestCase { */ public function testGetAvatarNoAvatar(): void { $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); - $this->avatarMock->method('getFile')->will($this->throwException(new NotFoundException())); + $this->avatarMock->method('getFile')->willThrowException(new NotFoundException()); $response = $this->avatarController->getAvatar('userId', 32); //Comment out until JS is fixed @@ -183,7 +184,7 @@ class AvatarControllerTest extends \Test\TestCase { $this->avatarManager ->method('getAvatar') ->with('userDoesNotExist') - ->will($this->throwException(new \Exception('user does not exist'))); + ->willThrowException(new \Exception('user does not exist')); $response = $this->avatarController->getAvatar('userDoesNotExist', 32); @@ -287,7 +288,7 @@ class AvatarControllerTest extends \Test\TestCase { * Test what happens if the removing of the avatar fails */ public function testDeleteAvatarException(): void { - $this->avatarMock->method('remove')->will($this->throwException(new \Exception('foo'))); + $this->avatarMock->method('remove')->willThrowException(new \Exception('foo')); $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); $this->logger->expects($this->once()) @@ -469,7 +470,7 @@ class AvatarControllerTest extends \Test\TestCase { public function testPostAvatarException(): void { $this->cache->expects($this->once()) ->method('set') - ->will($this->throwException(new \Exception('foo'))); + ->willThrowException(new \Exception('foo')); $file = $this->getMockBuilder('OCP\Files\File') ->disableOriginalConstructor()->getMock(); $file->expects($this->once()) @@ -514,7 +515,7 @@ class AvatarControllerTest extends \Test\TestCase { public function testPostCroppedAvatarNoSquareCrop(): void { $this->cache->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT . '/tests/data/testimage.jpg')); - $this->avatarMock->method('set')->will($this->throwException(new \OC\NotSquareException)); + $this->avatarMock->method('set')->willThrowException(new \OC\NotSquareException); $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); $response = $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 11]); @@ -539,7 +540,7 @@ class AvatarControllerTest extends \Test\TestCase { public function testPostCroppedAvatarException(): void { $this->cache->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT . '/tests/data/testimage.jpg')); - $this->avatarMock->method('set')->will($this->throwException(new \Exception('foo'))); + $this->avatarMock->method('set')->willThrowException(new \Exception('foo')); $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); $this->logger->expects($this->once()) diff --git a/tests/Core/Controller/ChangePasswordControllerTest.php b/tests/Core/Controller/ChangePasswordControllerTest.php index a806b091477..aae36fb52b8 100644 --- a/tests/Core/Controller/ChangePasswordControllerTest.php +++ b/tests/Core/Controller/ChangePasswordControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -96,7 +97,7 @@ class ChangePasswordControllerTest extends \Test\TestCase { $user->expects($this->once()) ->method('setPassword') ->with('new') - ->will($this->throwException(new HintException('Common password'))); + ->willThrowException(new HintException('Common password')); $expects = new JSONResponse([ 'status' => 'error', diff --git a/tests/Core/Controller/ClientFlowLoginControllerTest.php b/tests/Core/Controller/ClientFlowLoginControllerTest.php index 1f4575208b8..b182bb1bb39 100644 --- a/tests/Core/Controller/ClientFlowLoginControllerTest.php +++ b/tests/Core/Controller/ClientFlowLoginControllerTest.php @@ -18,10 +18,14 @@ use OCA\OAuth2\Db\AccessTokenMapper; use OCA\OAuth2\Db\Client; use OCA\OAuth2\Db\ClientMapper; use OCP\AppFramework\Http; +use OCP\AppFramework\Http\ContentSecurityPolicy; +use OCP\AppFramework\Http\RedirectResponse; +use OCP\AppFramework\Http\Response; use OCP\AppFramework\Http\StandaloneTemplateResponse; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Defaults; use OCP\EventDispatcher\IEventDispatcher; +use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; use OCP\ISession; @@ -48,6 +52,7 @@ class ClientFlowLoginControllerTest extends TestCase { private ICrypto&MockObject $crypto; private IEventDispatcher&MockObject $eventDispatcher; private ITimeFactory&MockObject $timeFactory; + private IConfig&MockObject $config; private ClientFlowLoginController $clientFlowLoginController; @@ -73,6 +78,7 @@ class ClientFlowLoginControllerTest extends TestCase { $this->crypto = $this->createMock(ICrypto::class); $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->config = $this->createMock(IConfig::class); $this->clientFlowLoginController = new ClientFlowLoginController( 'core', @@ -89,6 +95,7 @@ class ClientFlowLoginControllerTest extends TestCase { $this->crypto, $this->eventDispatcher, $this->timeFactory, + $this->config, ); } @@ -97,8 +104,8 @@ class ClientFlowLoginControllerTest extends TestCase { 'core', 'error', [ - 'errors' => - [ + 'errors' + => [ [ 'error' => 'Access Forbidden', 'hint' => 'Invalid request', @@ -114,12 +121,8 @@ class ClientFlowLoginControllerTest extends TestCase { public function testShowAuthPickerPageWithOcsHeader(): void { $this->request ->method('getHeader') - ->withConsecutive( - ['USER_AGENT'], - ['OCS-APIREQUEST'] - ) ->willReturnMap([ - ['USER_AGENT', 'Mac OS X Sync Client'], + ['user-agent', 'Mac OS X Sync Client'], ['OCS-APIREQUEST', 'true'], ]); $this->random @@ -163,11 +166,12 @@ class ClientFlowLoginControllerTest extends TestCase { 'serverHost' => 'https://example.com', 'oauthState' => 'OauthStateToken', 'user' => '', - 'direct' => 0 + 'direct' => 0, + 'providedRedirectUri' => '', ], 'guest' ); - $csp = new Http\ContentSecurityPolicy(); + $csp = new ContentSecurityPolicy(); $csp->addAllowedFormActionDomain('nc://*'); $expected->setContentSecurityPolicy($csp); $this->assertEquals($expected, $this->clientFlowLoginController->showAuthPickerPage()); @@ -176,12 +180,8 @@ class ClientFlowLoginControllerTest extends TestCase { public function testShowAuthPickerPageWithOauth(): void { $this->request ->method('getHeader') - ->withConsecutive( - ['USER_AGENT'], - ['OCS-APIREQUEST'] - ) ->willReturnMap([ - ['USER_AGENT', 'Mac OS X Sync Client'], + ['user-agent', 'Mac OS X Sync Client'], ['OCS-APIREQUEST', 'false'], ]); $client = new Client(); @@ -233,11 +233,12 @@ class ClientFlowLoginControllerTest extends TestCase { 'serverHost' => 'https://example.com', 'oauthState' => 'OauthStateToken', 'user' => '', - 'direct' => 0 + 'direct' => 0, + 'providedRedirectUri' => '', ], 'guest' ); - $csp = new Http\ContentSecurityPolicy(); + $csp = new ContentSecurityPolicy(); $csp->addAllowedFormActionDomain('https://example.com/redirect.php'); $expected->setContentSecurityPolicy($csp); $this->assertEquals($expected, $this->clientFlowLoginController->showAuthPickerPage('MyClientIdentifier')); @@ -281,7 +282,7 @@ class ClientFlowLoginControllerTest extends TestCase { ->method('getId') ->willThrowException(new SessionNotAvailableException()); - $expected = new Http\Response(); + $expected = new Response(); $expected->setStatus(Http::STATUS_FORBIDDEN); $this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken')); } @@ -306,7 +307,7 @@ class ClientFlowLoginControllerTest extends TestCase { ->with('SessionId') ->willThrowException(new InvalidTokenException()); - $expected = new Http\Response(); + $expected = new Response(); $expected->setStatus(Http::STATUS_FORBIDDEN); $this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken')); } @@ -382,7 +383,7 @@ class ClientFlowLoginControllerTest extends TestCase { $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped'); - $expected = new Http\RedirectResponse('nc://login/server:http://example.com&user:MyLoginName&password:MyGeneratedToken'); + $expected = new RedirectResponse('nc://login/server:http://example.com&user:MyLoginName&password:MyGeneratedToken'); $this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken')); } @@ -398,20 +399,20 @@ class ClientFlowLoginControllerTest extends TestCase { public function testGeneratePasswordWithPasswordForOauthClient($redirectUri, $redirectUrl): void { $this->session ->method('get') - ->withConsecutive( - ['client.flow.state.token'], - ['oauth.state'] - ) ->willReturnMap([ ['client.flow.state.token', 'MyStateToken'], ['oauth.state', 'MyOauthState'], ]); + $calls = [ + 'client.flow.state.token', + 'oauth.state', + ]; $this->session ->method('remove') - ->withConsecutive( - ['client.flow.state.token'], - ['oauth.state'] - ); + ->willReturnCallback(function ($key) use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, $key); + }); $this->session ->expects($this->once()) ->method('getId') @@ -433,10 +434,6 @@ class ClientFlowLoginControllerTest extends TestCase { ->willReturn('MyPassword'); $this->random ->method('generate') - ->withConsecutive( - [72], - [128] - ) ->willReturnMap([ [72, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS, 'MyGeneratedToken'], [128, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS, 'MyAccessCode'], @@ -476,7 +473,7 @@ class ClientFlowLoginControllerTest extends TestCase { $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped'); - $expected = new Http\RedirectResponse($redirectUrl); + $expected = new RedirectResponse($redirectUrl); $this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken', 'MyClientIdentifier')); } @@ -551,17 +548,17 @@ class ClientFlowLoginControllerTest extends TestCase { $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped'); - $expected = new Http\RedirectResponse('nc://login/server:http://example.com&user:MyLoginName&password:MyGeneratedToken'); + $expected = new RedirectResponse('nc://login/server:http://example.com&user:MyLoginName&password:MyGeneratedToken'); $this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken')); } - public function dataGeneratePasswordWithHttpsProxy() { + public static function dataGeneratePasswordWithHttpsProxy(): array { return [ [ [ ['X-Forwarded-Proto', 'http'], ['X-Forwarded-Ssl', 'off'], - ['USER_AGENT', ''], + ['user-agent', ''], ], 'http', 'http', @@ -570,7 +567,7 @@ class ClientFlowLoginControllerTest extends TestCase { [ ['X-Forwarded-Proto', 'http'], ['X-Forwarded-Ssl', 'off'], - ['USER_AGENT', ''], + ['user-agent', ''], ], 'https', 'https', @@ -579,7 +576,7 @@ class ClientFlowLoginControllerTest extends TestCase { [ ['X-Forwarded-Proto', 'https'], ['X-Forwarded-Ssl', 'off'], - ['USER_AGENT', ''], + ['user-agent', ''], ], 'http', 'https', @@ -588,7 +585,7 @@ class ClientFlowLoginControllerTest extends TestCase { [ ['X-Forwarded-Proto', 'https'], ['X-Forwarded-Ssl', 'on'], - ['USER_AGENT', ''], + ['user-agent', ''], ], 'http', 'https', @@ -597,7 +594,7 @@ class ClientFlowLoginControllerTest extends TestCase { [ ['X-Forwarded-Proto', 'http'], ['X-Forwarded-Ssl', 'on'], - ['USER_AGENT', ''], + ['user-agent', ''], ], 'http', 'https', @@ -606,11 +603,11 @@ class ClientFlowLoginControllerTest extends TestCase { } /** - * @dataProvider dataGeneratePasswordWithHttpsProxy * @param array $headers * @param string $protocol * @param string $expected */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataGeneratePasswordWithHttpsProxy')] public function testGeneratePasswordWithHttpsProxy(array $headers, $protocol, $expected): void { $this->session ->expects($this->once()) @@ -682,7 +679,7 @@ class ClientFlowLoginControllerTest extends TestCase { $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped'); - $expected = new Http\RedirectResponse('nc://login/server:' . $expected . '://example.com&user:MyLoginName&password:MyGeneratedToken'); + $expected = new RedirectResponse('nc://login/server:' . $expected . '://example.com&user:MyLoginName&password:MyGeneratedToken'); $this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken')); } } diff --git a/tests/Core/Controller/ClientFlowLoginV2ControllerTest.php b/tests/Core/Controller/ClientFlowLoginV2ControllerTest.php index 98c7821791d..d130eb75c1a 100644 --- a/tests/Core/Controller/ClientFlowLoginV2ControllerTest.php +++ b/tests/Core/Controller/ClientFlowLoginV2ControllerTest.php @@ -11,9 +11,12 @@ namespace Test\Core\Controller; use OC\Core\Controller\ClientFlowLoginV2Controller; use OC\Core\Data\LoginFlowV2Credentials; use OC\Core\Db\LoginFlowV2; +use OC\Core\Exception\LoginFlowV2ClientForbiddenException; use OC\Core\Exception\LoginFlowV2NotFoundException; use OC\Core\Service\LoginFlowV2Service; use OCP\AppFramework\Http; +use OCP\AppFramework\Http\RedirectResponse; +use OCP\AppFramework\Http\StandaloneTemplateResponse; use OCP\Defaults; use OCP\IL10N; use OCP\IRequest; @@ -56,6 +59,12 @@ class ClientFlowLoginV2ControllerTest extends TestCase { $this->random = $this->createMock(ISecureRandom::class); $this->defaults = $this->createMock(Defaults::class); $this->l = $this->createMock(IL10N::class); + $this->l + ->expects($this->any()) + ->method('t') + ->willReturnCallback(function ($text, $parameters = []) { + return vsprintf($text, $parameters); + }); $this->controller = new ClientFlowLoginV2Controller( 'core', $this->request, @@ -104,7 +113,7 @@ class ClientFlowLoginV2ControllerTest extends TestCase { $result = $this->controller->landing('token'); $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus()); - $this->assertInstanceOf(Http\StandaloneTemplateResponse::class, $result); + $this->assertInstanceOf(StandaloneTemplateResponse::class, $result); } public function testLandingValid(): void { @@ -122,7 +131,7 @@ class ClientFlowLoginV2ControllerTest extends TestCase { $result = $this->controller->landing('token'); - $this->assertInstanceOf(Http\RedirectResponse::class, $result); + $this->assertInstanceOf(RedirectResponse::class, $result); $this->assertSame(Http::STATUS_SEE_OTHER, $result->getStatus()); $this->assertSame('https://server/path', $result->getRedirectURL()); } @@ -150,6 +159,22 @@ class ClientFlowLoginV2ControllerTest extends TestCase { $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus()); } + public function testShowAuthPickerForbiddenUserClient() { + $this->session->method('get') + ->with('client.flow.v2.login.token') + ->willReturn('loginToken'); + + $this->loginFlowV2Service->method('getByLoginToken') + ->with('loginToken') + ->willThrowException(new LoginFlowV2ClientForbiddenException()); + + $result = $this->controller->showAuthPickerPage(); + + $this->assertInstanceOf(StandaloneTemplateResponse::class, $result); + $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus()); + $this->assertSame('Please use original client', $result->getParams()['message']); + } + public function testShowAuthPickerValidLoginToken(): void { $this->session->method('get') ->with('client.flow.v2.login.token') @@ -206,6 +231,29 @@ class ClientFlowLoginV2ControllerTest extends TestCase { $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus()); } + public function testGrantPageForbiddenUserClient() { + $this->session->method('get') + ->willReturnCallback(function ($name) { + if ($name === 'client.flow.v2.state.token') { + return 'stateToken'; + } + if ($name === 'client.flow.v2.login.token') { + return 'loginToken'; + } + return null; + }); + + $this->loginFlowV2Service->method('getByLoginToken') + ->with('loginToken') + ->willThrowException(new LoginFlowV2ClientForbiddenException()); + + $result = $this->controller->grantPage('stateToken'); + + $this->assertInstanceOf(StandaloneTemplateResponse::class, $result); + $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus()); + $this->assertSame('Please use original client', $result->getParams()['message']); + } + public function testGrantPageValid(): void { $this->session->method('get') ->willReturnCallback(function ($name) { @@ -266,6 +314,29 @@ class ClientFlowLoginV2ControllerTest extends TestCase { $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus()); } + public function testGenerateAppPasswordForbiddenUserClient() { + $this->session->method('get') + ->willReturnCallback(function ($name) { + if ($name === 'client.flow.v2.state.token') { + return 'stateToken'; + } + if ($name === 'client.flow.v2.login.token') { + return 'loginToken'; + } + return null; + }); + + $this->loginFlowV2Service->method('getByLoginToken') + ->with('loginToken') + ->willThrowException(new LoginFlowV2ClientForbiddenException()); + + $result = $this->controller->generateAppPassword('stateToken'); + + $this->assertInstanceOf(StandaloneTemplateResponse::class, $result); + $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus()); + $this->assertSame('Please use original client', $result->getParams()['message']); + } + public function testGenerateAppPassworValid(): void { $this->session->method('get') ->willReturnCallback(function ($name) { @@ -286,7 +357,7 @@ class ClientFlowLoginV2ControllerTest extends TestCase { $clearedState = false; $clearedLogin = false; $this->session->method('remove') - ->willReturnCallback(function ($name) use (&$clearedLogin, &$clearedState) { + ->willReturnCallback(function ($name) use (&$clearedLogin, &$clearedState): void { if ($name === 'client.flow.v2.state.token') { $clearedState = true; } diff --git a/tests/Core/Controller/CssControllerTest.php b/tests/Core/Controller/CssControllerTest.php index cae6f7989c4..b4764d6ea3a 100644 --- a/tests/Core/Controller/CssControllerTest.php +++ b/tests/Core/Controller/CssControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/tests/Core/Controller/GuestAvatarControllerTest.php b/tests/Core/Controller/GuestAvatarControllerTest.php index 1ad9e49f858..66a83098130 100644 --- a/tests/Core/Controller/GuestAvatarControllerTest.php +++ b/tests/Core/Controller/GuestAvatarControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -7,6 +8,7 @@ namespace Core\Controller; use OC\Core\Controller\GuestAvatarController; use OCP\AppFramework\Http\FileDisplayResponse; +use OCP\Files\File; use OCP\Files\SimpleFS\ISimpleFile; use OCP\IAvatar; use OCP\IAvatarManager; @@ -38,7 +40,7 @@ class GuestAvatarControllerTest extends \Test\TestCase { private $avatar; /** - * @var \OCP\Files\File|\PHPUnit\Framework\MockObject\MockObject + * @var File|\PHPUnit\Framework\MockObject\MockObject */ private $file; diff --git a/tests/Core/Controller/JsControllerTest.php b/tests/Core/Controller/JsControllerTest.php index 1500ed6eacf..30bc02e8625 100644 --- a/tests/Core/Controller/JsControllerTest.php +++ b/tests/Core/Controller/JsControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/tests/Core/Controller/LoginControllerTest.php b/tests/Core/Controller/LoginControllerTest.php index a6438a35af0..18baaf5b08c 100644 --- a/tests/Core/Controller/LoginControllerTest.php +++ b/tests/Core/Controller/LoginControllerTest.php @@ -255,9 +255,9 @@ class LoginControllerTest extends TestCase { ], ] ); - $this->initialState->expects($this->exactly(13)) - ->method('provideInitialState') - ->withConsecutive([ + + $calls = [ + [ 'loginMessages', [ 'MessageArray1', @@ -265,17 +265,26 @@ class LoginControllerTest extends TestCase { 'This community release of Nextcloud is unsupported and push notifications are limited.', ], ], + [ + 'loginErrors', [ - 'loginErrors', - [ - 'ErrorArray1', - 'ErrorArray2', - ], + 'ErrorArray1', + 'ErrorArray2', ], - [ - 'loginUsername', - '', - ]); + ], + [ + 'loginUsername', + '', + ] + ]; + $this->initialState->expects($this->exactly(13)) + ->method('provideInitialState') + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + if (!empty($expected)) { + $this->assertEquals($expected, func_get_args()); + } + }); $expectedResponse = new TemplateResponse( 'core', @@ -294,15 +303,25 @@ class LoginControllerTest extends TestCase { ->expects($this->once()) ->method('isLoggedIn') ->willReturn(false); - $this->initialState->expects($this->exactly(14)) - ->method('provideInitialState') - ->withConsecutive([], [], [], [ + $calls = [ + [], [], [], + [ 'loginAutocomplete', false - ], [ + ], + [ 'loginRedirectUrl', 'login/flow' - ]); + ], + ]; + $this->initialState->expects($this->exactly(14)) + ->method('provideInitialState') + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + if (!empty($expected)) { + $this->assertEquals($expected, func_get_args()); + } + }); $expectedResponse = new TemplateResponse( 'core', @@ -319,7 +338,7 @@ class LoginControllerTest extends TestCase { /** * @return array */ - public function passwordResetDataProvider(): array { + public static function passwordResetDataProvider(): array { return [ [ true, @@ -332,9 +351,7 @@ class LoginControllerTest extends TestCase { ]; } - /** - * @dataProvider passwordResetDataProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('passwordResetDataProvider')] public function testShowLoginFormWithPasswordResetOption($canChangePassword, $expectedResult): void { $this->userSession @@ -363,15 +380,26 @@ class LoginControllerTest extends TestCase { ->method('get') ->with('LdapUser') ->willReturn($user); - $this->initialState->expects($this->exactly(13)) - ->method('provideInitialState') - ->withConsecutive([], [], [ + $calls = [ + [], [], + [ 'loginUsername', 'LdapUser' - ], [], [], [], [ + ], + [], [], [], + [ 'loginCanResetPassword', $expectedResult - ]); + ], + ]; + $this->initialState->expects($this->exactly(13)) + ->method('provideInitialState') + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + if (!empty($expected)) { + $this->assertEquals($expected, func_get_args()); + } + }); $expectedResponse = new TemplateResponse( 'core', @@ -411,18 +439,30 @@ class LoginControllerTest extends TestCase { ->method('get') ->with('0') ->willReturn($user); - $this->initialState->expects($this->exactly(13)) - ->method('provideInitialState') - ->withConsecutive([], [], [], [ + $calls = [ + [], [], [], + [ 'loginAutocomplete', true - ], [], [ + ], + [], + [ 'loginResetPasswordLink', false - ], [ + ], + [ 'loginCanResetPassword', false - ]); + ], + ]; + $this->initialState->expects($this->exactly(13)) + ->method('provideInitialState') + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + if (!empty($expected)) { + $this->assertEquals($expected, func_get_args()); + } + }); $expectedResponse = new TemplateResponse( 'core', diff --git a/tests/Core/Controller/LostControllerTest.php b/tests/Core/Controller/LostControllerTest.php index 2a99c9f9d16..bbb5f2c2e54 100644 --- a/tests/Core/Controller/LostControllerTest.php +++ b/tests/Core/Controller/LostControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-only @@ -172,13 +173,18 @@ class LostControllerTest extends TestCase { ->method('linkToRouteAbsolute') ->with('core.lost.setPassword', ['userId' => 'ValidTokenUser', 'token' => 'MySecretToken']) ->willReturn('https://example.tld/index.php/lostpassword/set/sometoken/someuser'); + + $calls = [ + ['resetPasswordUser', 'ValidTokenUser'], + ['resetPasswordTarget', 'https://example.tld/index.php/lostpassword/set/sometoken/someuser'], + ]; $this->initialState ->expects($this->exactly(2)) ->method('provideInitialState') - ->withConsecutive( - ['resetPasswordUser', 'ValidTokenUser'], - ['resetPasswordTarget', 'https://example.tld/index.php/lostpassword/set/sometoken/someuser'] - ); + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $response = $this->lostController->resetform('MySecretToken', 'ValidTokenUser'); $expectedResponse = new TemplateResponse('core', @@ -398,7 +404,7 @@ class LostControllerTest extends TestCase { ->expects($this->once()) ->method('send') ->with($message) - ->will($this->throwException(new \Exception())); + ->willThrowException(new \Exception()); $this->logger->expects($this->exactly(1)) ->method('error'); @@ -448,12 +454,19 @@ class LostControllerTest extends TestCase { $this->userManager->method('get') ->with('ValidTokenUser') ->willReturn($this->existingUser); - $beforePasswordResetEvent = new BeforePasswordResetEvent($this->existingUser, 'NewPassword'); - $passwordResetEvent = new PasswordResetEvent($this->existingUser, 'NewPassword'); + + $calls = [ + [new BeforePasswordResetEvent($this->existingUser, 'NewPassword')], + [new PasswordResetEvent($this->existingUser, 'NewPassword')], + ]; $this->eventDispatcher ->expects($this->exactly(2)) ->method('dispatchTyped') - ->withConsecutive([$beforePasswordResetEvent], [$passwordResetEvent]); + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); + $this->config->expects($this->once()) ->method('deleteUserValue') ->with('ValidTokenUser', 'core', 'lostpassword'); @@ -666,18 +679,18 @@ class LostControllerTest extends TestCase { /** * @return array */ - public function dataTwoUserswithSameEmailOneDisabled(): array { + public static function dataTwoUsersWithSameEmailOneDisabled(): array { return [ - ['user1' => true, 'user2' => false], - ['user1' => false, 'user2' => true] + ['userEnabled1' => true, 'userEnabled2' => false], + ['userEnabled1' => false, 'userEnabled2' => true] ]; } /** - * @dataProvider dataTwoUserswithSameEmailOneDisabled * @param bool $userEnabled1 * @param bool $userEnabled2 */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTwoUsersWithSameEmailOneDisabled')] public function testTwoUsersWithSameEmailOneDisabled(bool $userEnabled1, bool $userEnabled2): void { $user1 = $this->createMock(IUser::class); $user1->method('getEMailAddress') diff --git a/tests/Core/Controller/NavigationControllerTest.php b/tests/Core/Controller/NavigationControllerTest.php index 4995bd2fed0..d00976f18ec 100644 --- a/tests/Core/Controller/NavigationControllerTest.php +++ b/tests/Core/Controller/NavigationControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -7,7 +8,6 @@ namespace Tests\Core\Controller; use OC\Core\Controller\NavigationController; -use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\INavigationManager; use OCP\IRequest; @@ -42,13 +42,14 @@ class NavigationControllerTest extends TestCase { ); } - public function dataGetNavigation() { + public static function dataGetNavigation(): array { return [ - [false], [true] + [false], + [true], ]; } - /** @dataProvider dataGetNavigation */ - public function testGetAppNavigation($absolute): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetNavigation')] + public function testGetAppNavigation(bool $absolute): void { $this->navigationManager->expects($this->once()) ->method('getAll') ->with('link') @@ -59,11 +60,10 @@ class NavigationControllerTest extends TestCase { ->willReturn('http://localhost/'); $this->urlGenerator->expects($this->exactly(2)) ->method('getAbsoluteURL') - ->withConsecutive(['/index.php/apps/files'], ['icon']) - ->willReturnOnConsecutiveCalls( - 'http://localhost/index.php/apps/files', - 'http://localhost/icon' - ); + ->willReturnMap([ + ['/index.php/apps/files', 'http://localhost/index.php/apps/files'], + ['icon', 'http://localhost/icon'], + ]); $actual = $this->controller->getAppsNavigation($absolute); $this->assertInstanceOf(DataResponse::class, $actual); $this->assertEquals('http://localhost/index.php/apps/files', $actual->getData()[0]['href']); @@ -76,8 +76,8 @@ class NavigationControllerTest extends TestCase { } } - /** @dataProvider dataGetNavigation */ - public function testGetSettingsNavigation($absolute): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetNavigation')] + public function testGetSettingsNavigation(bool $absolute): void { $this->navigationManager->expects($this->once()) ->method('getAll') ->with('settings') @@ -88,14 +88,10 @@ class NavigationControllerTest extends TestCase { ->willReturn('http://localhost/'); $this->urlGenerator->expects($this->exactly(2)) ->method('getAbsoluteURL') - ->withConsecutive( - ['/index.php/settings/user'], - ['/core/img/settings.svg'] - ) - ->willReturnOnConsecutiveCalls( - 'http://localhost/index.php/settings/user', - 'http://localhost/core/img/settings.svg' - ); + ->willReturnMap([ + ['/index.php/settings/user', 'http://localhost/index.php/settings/user'], + ['/core/img/settings.svg', 'http://localhost/core/img/settings.svg'] + ]); $actual = $this->controller->getSettingsNavigation($absolute); $this->assertInstanceOf(DataResponse::class, $actual); $this->assertEquals('http://localhost/index.php/settings/user', $actual->getData()[0]['href']); @@ -108,33 +104,35 @@ class NavigationControllerTest extends TestCase { } } - public function testGetAppNavigationEtagMatch(): void { - $navigation = [ ['id' => 'files', 'href' => '/index.php/apps/files', 'icon' => 'icon' ] ]; - $this->request->expects($this->once()) - ->method('getHeader') - ->with('If-None-Match') - ->willReturn(md5(json_encode($navigation))); - $this->navigationManager->expects($this->once()) + public function testEtagIgnoresLogout(): void { + $navigation1 = [ + ['id' => 'files', 'href' => '/index.php/apps/files', 'icon' => 'icon' ], + ['id' => 'logout', 'href' => '/index.php/logout?requesttoken=abcd', 'icon' => 'icon' ], + ]; + $navigation2 = [ + ['id' => 'files', 'href' => '/index.php/apps/files', 'icon' => 'icon' ], + ['id' => 'logout', 'href' => '/index.php/logout?requesttoken=1234', 'icon' => 'icon' ], + ]; + $navigation3 = [ + ['id' => 'files', 'href' => '/index.php/apps/files/test', 'icon' => 'icon' ], + ['id' => 'logout', 'href' => '/index.php/logout?requesttoken=1234', 'icon' => 'icon' ], + ]; + $this->navigationManager->expects($this->exactly(3)) ->method('getAll') ->with('link') - ->willReturn($navigation); - $actual = $this->controller->getAppsNavigation(); - $this->assertInstanceOf(DataResponse::class, $actual); - $this->assertEquals(Http::STATUS_NOT_MODIFIED, $actual->getStatus()); - } + ->willReturnOnConsecutiveCalls( + $navigation1, + $navigation2, + $navigation3, + ); - public function testGetSettingsNavigationEtagMatch(): void { - $navigation = [ ['id' => 'logout', 'href' => '/index.php/apps/files', 'icon' => 'icon' ] ]; - $this->request->expects($this->once()) - ->method('getHeader') - ->with('If-None-Match') - ->willReturn(md5(json_encode([ ['id' => 'logout', 'href' => 'logout', 'icon' => 'icon' ] ]))); - $this->navigationManager->expects($this->once()) - ->method('getAll') - ->with('settings') - ->willReturn($navigation); - $actual = $this->controller->getSettingsNavigation(); - $this->assertInstanceOf(DataResponse::class, $actual); - $this->assertEquals(Http::STATUS_NOT_MODIFIED, $actual->getStatus()); + // Changes in the logout url should not change the ETag + $request1 = $this->controller->getAppsNavigation(); + $request2 = $this->controller->getAppsNavigation(); + $this->assertEquals($request1->getETag(), $request2->getETag()); + + // Changes in non-logout urls should result in a different ETag + $request3 = $this->controller->getAppsNavigation(); + $this->assertNotEquals($request2->getETag(), $request3->getETag()); } } diff --git a/tests/Core/Controller/OCSControllerTest.php b/tests/Core/Controller/OCSControllerTest.php index 142a15e2308..bd7e26d5e8f 100644 --- a/tests/Core/Controller/OCSControllerTest.php +++ b/tests/Core/Controller/OCSControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -15,6 +16,7 @@ use OCP\IRequest; use OCP\IUser; use OCP\IUserManager; use OCP\IUserSession; +use OCP\Server; use OCP\ServerVersion; use Test\TestCase; @@ -42,7 +44,7 @@ class OCSControllerTest extends TestCase { $this->userSession = $this->createMock(IUserSession::class); $this->userManager = $this->createMock(IUserManager::class); $this->keyManager = $this->createMock(Manager::class); - $serverVersion = \OCP\Server::get(ServerVersion::class); + $serverVersion = Server::get(ServerVersion::class); $this->controller = new OCSController( 'core', @@ -78,7 +80,7 @@ class OCSControllerTest extends TestCase { ->method('isLoggedIn') ->willReturn(true); - $serverVersion = \OCP\Server::get(ServerVersion::class); + $serverVersion = Server::get(ServerVersion::class); $result = []; $result['version'] = [ @@ -111,7 +113,7 @@ class OCSControllerTest extends TestCase { $this->userSession->expects($this->once()) ->method('isLoggedIn') ->willReturn(false); - $serverVersion = \OCP\Server::get(ServerVersion::class); + $serverVersion = Server::get(ServerVersion::class); $result = []; $result['version'] = [ diff --git a/tests/Core/Controller/PreviewControllerTest.php b/tests/Core/Controller/PreviewControllerTest.php index e7ecba27064..5a6cd1fba0a 100644 --- a/tests/Core/Controller/PreviewControllerTest.php +++ b/tests/Core/Controller/PreviewControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -19,7 +20,6 @@ use OCP\Files\Storage\IStorage; use OCP\IPreview; use OCP\IRequest; use OCP\Preview\IMimeIconProvider; -use OCP\Share\IAttributes; use OCP\Share\IShare; use PHPUnit\Framework\MockObject\MockObject; @@ -196,15 +196,9 @@ class PreviewControllerTest extends \Test\TestCase { ->with($this->equalTo($file)) ->willReturn(true); - $shareAttributes = $this->createMock(IAttributes::class); - $shareAttributes->expects(self::atLeastOnce()) - ->method('getAttribute') - ->with('permissions', 'download') - ->willReturn(false); - $share = $this->createMock(IShare::class); - $share->method('getAttributes') - ->willReturn($shareAttributes); + $share->method('canSeeContent') + ->willReturn(false); $storage = $this->createMock(ISharedStorage::class); $storage->method('instanceOfStorage') @@ -242,14 +236,9 @@ class PreviewControllerTest extends \Test\TestCase { ->with($this->equalTo($file)) ->willReturn(true); - $shareAttributes = $this->createMock(IAttributes::class); - $shareAttributes->method('getAttribute') - ->with('permissions', 'download') - ->willReturn(false); - $share = $this->createMock(IShare::class); - $share->method('getAttributes') - ->willReturn($shareAttributes); + $share->method('canSeeContent') + ->willReturn(false); $storage = $this->createMock(ISharedStorage::class); $storage->method('instanceOfStorage') @@ -265,7 +254,7 @@ class PreviewControllerTest extends \Test\TestCase { $this->request ->method('getHeader') - ->with('X-NC-Preview') + ->with('x-nc-preview') ->willReturn('true'); $preview = $this->createMock(ISimpleFile::class); @@ -341,8 +330,8 @@ class PreviewControllerTest extends \Test\TestCase { // No attributes set -> download permitted $share = $this->createMock(IShare::class); - $share->method('getAttributes') - ->willReturn(null); + $share->method('canSeeContent') + ->willReturn(true); $storage = $this->createMock(ISharedStorage::class); $storage->method('instanceOfStorage') diff --git a/tests/Core/Controller/TwoFactorChallengeControllerTest.php b/tests/Core/Controller/TwoFactorChallengeControllerTest.php index 11e18bd622a..d9ea1ca263f 100644 --- a/tests/Core/Controller/TwoFactorChallengeControllerTest.php +++ b/tests/Core/Controller/TwoFactorChallengeControllerTest.php @@ -22,7 +22,7 @@ use OCP\ISession; use OCP\IURLGenerator; use OCP\IUser; use OCP\IUserSession; -use OCP\Template; +use OCP\Template\ITemplate; use Psr\Log\LoggerInterface; use Test\TestCase; @@ -68,7 +68,7 @@ class TwoFactorChallengeControllerTest extends TestCase { $this->urlGenerator, $this->logger, ]) - ->setMethods(['getLogoutUrl']) + ->onlyMethods(['getLogoutUrl']) ->getMock(); $this->controller->expects($this->any()) ->method('getLogoutUrl') @@ -115,7 +115,7 @@ class TwoFactorChallengeControllerTest extends TestCase { $provider->method('getId')->willReturn('myprovider'); $backupProvider = $this->createMock(IProvider::class); $backupProvider->method('getId')->willReturn('backup_codes'); - $tmpl = $this->createMock(Template::class); + $tmpl = $this->createMock(ITemplate::class); $providerSet = new ProviderSet([$provider, $backupProvider], true); $this->userSession->expects($this->once()) @@ -301,13 +301,17 @@ class TwoFactorChallengeControllerTest extends TestCase { $this->twoFactorManager->expects($this->once()) ->method('verifyChallenge') ->with('myprovider', $user, 'token') - ->will($this->throwException($exception)); + ->willThrowException($exception); + $calls = [ + ['two_factor_auth_error_message', '2FA failed'], + ['two_factor_auth_error', true], + ]; $this->session->expects($this->exactly(2)) ->method('set') - ->withConsecutive( - ['two_factor_auth_error_message', '2FA failed'], - ['two_factor_auth_error', true] - ); + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->urlGenerator->expects($this->once()) ->method('linkToRoute') ->with('core.TwoFactorChallenge.showChallenge', [ @@ -399,7 +403,7 @@ class TwoFactorChallengeControllerTest extends TestCase { ->method('getLoginSetup') ->with($user) ->willReturn($loginSetup); - $tmpl = $this->createMock(Template::class); + $tmpl = $this->createMock(ITemplate::class); $loginSetup->expects($this->once()) ->method('getBody') ->willReturn($tmpl); diff --git a/tests/Core/Controller/UserControllerTest.php b/tests/Core/Controller/UserControllerTest.php index 979c723dd85..2473f280580 100644 --- a/tests/Core/Controller/UserControllerTest.php +++ b/tests/Core/Controller/UserControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/tests/Core/Controller/WipeControllerTest.php b/tests/Core/Controller/WipeControllerTest.php index 2cd315db5bf..5330eb599e6 100644 --- a/tests/Core/Controller/WipeControllerTest.php +++ b/tests/Core/Controller/WipeControllerTest.php @@ -33,7 +33,7 @@ class WipeControllerTest extends TestCase { $this->remoteWipe); } - public function dataTest() { + public static function dataTest(): array { return [ // valid token, could perform operation, valid result [ true, true, true], @@ -47,9 +47,8 @@ class WipeControllerTest extends TestCase { * @param bool $valid * @param bool $couldPerform * @param bool $result - * - * @dataProvider dataTest */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTest')] public function testCheckWipe(bool $valid, bool $couldPerform, bool $result): void { if (!$valid) { $this->remoteWipe->method('start') @@ -76,9 +75,8 @@ class WipeControllerTest extends TestCase { * @param bool $valid * @param bool $couldPerform * @param bool $result - * - * @dataProvider dataTest */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTest')] public function testWipeDone(bool $valid, bool $couldPerform, bool $result): void { if (!$valid) { $this->remoteWipe->method('finish') diff --git a/tests/Core/Data/LoginFlowV2CredentialsTest.php b/tests/Core/Data/LoginFlowV2CredentialsTest.php index 41575f33a9c..ffa06f1a451 100644 --- a/tests/Core/Data/LoginFlowV2CredentialsTest.php +++ b/tests/Core/Data/LoginFlowV2CredentialsTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-only @@ -11,7 +12,7 @@ use OC\Core\Data\LoginFlowV2Credentials; use Test\TestCase; class LoginFlowV2CredentialsTest extends TestCase { - /** @var \OC\Core\Data\LoginFlowV2Credentials */ + /** @var LoginFlowV2Credentials */ private $fixture; public function setUp(): void { diff --git a/tests/Core/Middleware/TwoFactorMiddlewareTest.php b/tests/Core/Middleware/TwoFactorMiddlewareTest.php index 6b8bf1c76e0..10afdd7c5e1 100644 --- a/tests/Core/Middleware/TwoFactorMiddlewareTest.php +++ b/tests/Core/Middleware/TwoFactorMiddlewareTest.php @@ -234,23 +234,27 @@ class TwoFactorMiddlewareTest extends TestCase { $this->middleware->beforeController($twoFactorChallengeController, 'index'); } - public function dataRequires2FASetupDone() { - $provider = $this->createMock(IProvider::class); - $provider->method('getId') - ->willReturn('2FAftw'); - + public static function dataRequires2FASetupDone(): array { return [ - [[], false, false], - [[], true, true], - [[$provider], false, true], - [[$provider], true, true], + [false, false, false], + [false, true, true], + [true, false, true], + [true, true, true], ]; } - /** - * @dataProvider dataRequires2FASetupDone - */ - public function testRequires2FASetupDone(array $providers, bool $missingProviders, bool $expectEception): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataRequires2FASetupDone')] + public function testRequires2FASetupDone(bool $hasProvider, bool $missingProviders, bool $expectEception): void { + if ($hasProvider) { + $provider = $this->createMock(IProvider::class); + $provider->method('getId') + ->willReturn('2FAftw'); + $providers = [$provider]; + } else { + $providers = []; + } + + $user = $this->createMock(IUser::class); $this->reflector diff --git a/tests/Core/Service/LoginFlowV2ServiceUnitTest.php b/tests/Core/Service/LoginFlowV2ServiceUnitTest.php index 1c31efee8f7..5d05a1c6e0a 100644 --- a/tests/Core/Service/LoginFlowV2ServiceUnitTest.php +++ b/tests/Core/Service/LoginFlowV2ServiceUnitTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-only @@ -14,6 +15,7 @@ use OC\Core\Data\LoginFlowV2Credentials; use OC\Core\Data\LoginFlowV2Tokens; use OC\Core\Db\LoginFlowV2; use OC\Core\Db\LoginFlowV2Mapper; +use OC\Core\Exception\LoginFlowV2ClientForbiddenException; use OC\Core\Exception\LoginFlowV2NotFoundException; use OC\Core\Service\LoginFlowV2Service; use OCP\AppFramework\Db\DoesNotExistException; @@ -29,25 +31,25 @@ use Test\TestCase; * Unit tests for \OC\Core\Service\LoginFlowV2Service */ class LoginFlowV2ServiceUnitTest extends TestCase { - /** @var \OCP\IConfig */ + /** @var IConfig */ private $config; - /** @var \OCP\Security\ICrypto */ + /** @var ICrypto */ private $crypto; /** @var LoggerInterface|MockObject */ private $logger; - /** @var \OC\Core\Db\LoginFlowV2Mapper */ + /** @var LoginFlowV2Mapper */ private $mapper; - /** @var \OCP\Security\ISecureRandom */ + /** @var ISecureRandom */ private $secureRandom; - /** @var \OC\Core\Service\LoginFlowV2Service */ + /** @var LoginFlowV2Service */ private $subjectUnderTest; - /** @var \OCP\AppFramework\Utility\ITimeFactory */ + /** @var ITimeFactory */ private $timeFactory; /** @var \OC\Authentication\Token\IProvider */ @@ -65,26 +67,13 @@ class LoginFlowV2ServiceUnitTest extends TestCase { * Code was moved to separate function to keep setUp function small and clear. */ private function setupSubjectUnderTest(): void { - $this->config = $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor()->getMock(); - - $this->crypto = $this->getMockBuilder(ICrypto::class) - ->disableOriginalConstructor()->getMock(); - - $this->mapper = $this->getMockBuilder(LoginFlowV2Mapper::class) - ->disableOriginalConstructor()->getMock(); - - $this->logger = $this->getMockBuilder(LoggerInterface::class) - ->disableOriginalConstructor()->getMock(); - - $this->tokenProvider = $this->getMockBuilder(IProvider::class) - ->disableOriginalConstructor()->getMock(); - - $this->secureRandom = $this->getMockBuilder(ISecureRandom::class) - ->disableOriginalConstructor()->getMock(); - - $this->timeFactory = $this->getMockBuilder(ITimeFactory::class) - ->disableOriginalConstructor()->getMock(); + $this->config = $this->createMock(IConfig::class); + $this->crypto = $this->createMock(ICrypto::class); + $this->mapper = $this->createMock(LoginFlowV2Mapper::class); + $this->logger = $this->createMock(LoggerInterface::class); + $this->tokenProvider = $this->createMock(IProvider::class); + $this->secureRandom = $this->createMock(ISecureRandom::class); + $this->timeFactory = $this->createMock(ITimeFactory::class); $this->subjectUnderTest = new LoginFlowV2Service( $this->mapper, @@ -126,11 +115,14 @@ class LoginFlowV2ServiceUnitTest extends TestCase { /* * Tests for poll */ - - public function testPollApptokenCouldNotBeDecrypted(): void { + public function testPollPrivateKeyCouldNotBeDecrypted(): void { $this->expectException(LoginFlowV2NotFoundException::class); $this->expectExceptionMessage('Apptoken could not be decrypted'); + $this->crypto->expects($this->once()) + ->method('decrypt') + ->willThrowException(new \Exception('HMAC mismatch')); + /* * Cannot be mocked, because functions like getLoginName are magic functions. * To be able to set internal properties, we have to use the real class here. @@ -148,6 +140,32 @@ class LoginFlowV2ServiceUnitTest extends TestCase { $this->subjectUnderTest->poll(''); } + public function testPollApptokenCouldNotBeDecrypted(): void { + $this->expectException(LoginFlowV2NotFoundException::class); + $this->expectExceptionMessage('Apptoken could not be decrypted'); + + /* + * Cannot be mocked, because functions like getLoginName are magic functions. + * To be able to set internal properties, we have to use the real class here. + */ + [$encrypted, $privateKey,] = $this->getOpenSSLEncryptedPublicAndPrivateKey('test'); + $loginFlowV2 = new LoginFlowV2(); + $loginFlowV2->setLoginName('test'); + $loginFlowV2->setServer('test'); + $loginFlowV2->setAppPassword('broken#' . $encrypted); + $loginFlowV2->setPrivateKey('encrypted(test)'); + + $this->crypto->expects($this->once()) + ->method('decrypt') + ->willReturn($privateKey); + + $this->mapper->expects($this->once()) + ->method('getByPollToken') + ->willReturn($loginFlowV2); + + $this->subjectUnderTest->poll('test'); + } + public function testPollInvalidToken(): void { $this->expectException(LoginFlowV2NotFoundException::class); $this->expectExceptionMessage('Invalid token'); @@ -237,6 +255,57 @@ class LoginFlowV2ServiceUnitTest extends TestCase { $this->subjectUnderTest->getByLoginToken('test_token'); } + public function testGetByLoginTokenClientForbidden() { + $this->expectException(LoginFlowV2ClientForbiddenException::class); + $this->expectExceptionMessage('Client not allowed'); + + $allowedClients = [ + '/Custom Allowed Client/i' + ]; + + $this->config->expects($this->exactly(1)) + ->method('getSystemValue') + ->willReturn($this->returnCallback(function ($key) use ($allowedClients) { + // Note: \OCP\IConfig::getSystemValue returns either an array or string. + return $key == 'core.login_flow_v2.allowed_user_agents' ? $allowedClients : ''; + })); + + $loginFlowV2 = new LoginFlowV2(); + $loginFlowV2->setClientName('Rogue Curl Client/1.0'); + + $this->mapper->expects($this->once()) + ->method('getByLoginToken') + ->willReturn($loginFlowV2); + + $this->subjectUnderTest->getByLoginToken('test_token'); + } + + public function testGetByLoginTokenClientAllowed() { + $allowedClients = [ + '/Foo Allowed Client/i', + '/Custom Allowed Client/i' + ]; + + $loginFlowV2 = new LoginFlowV2(); + $loginFlowV2->setClientName('Custom Allowed Client Curl Client/1.0'); + + $this->config->expects($this->exactly(1)) + ->method('getSystemValue') + ->willReturn($this->returnCallback(function ($key) use ($allowedClients) { + // Note: \OCP\IConfig::getSystemValue returns either an array or string. + return $key == 'core.login_flow_v2.allowed_user_agents' ? $allowedClients : ''; + })); + + $this->mapper->expects($this->once()) + ->method('getByLoginToken') + ->willReturn($loginFlowV2); + + $result = $this->subjectUnderTest->getByLoginToken('test_token'); + + $this->assertTrue($result instanceof LoginFlowV2); + $this->assertEquals('Custom Allowed Client Curl Client/1.0', $result->getClientName()); + } + /* * Tests for startLoginFlow */ |