diff options
Diffstat (limited to 'apps/provisioning_api/tests/Controller/AppConfigControllerTest.php')
-rw-r--r-- | apps/provisioning_api/tests/Controller/AppConfigControllerTest.php | 166 |
1 files changed, 83 insertions, 83 deletions
diff --git a/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php b/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php index f932aceaf8e..1b09838cbc3 100644 --- a/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php +++ b/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php @@ -1,4 +1,6 @@ <?php + +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -7,16 +9,22 @@ namespace OCA\Provisioning_API\Tests\Controller; use OC\AppConfig; use OCA\Provisioning_API\Controller\AppConfigController; +use OCP\App\IAppManager; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; +use OCP\Exceptions\AppConfigUnknownKeyException; use OCP\IAppConfig; use OCP\IGroupManager; use OCP\IL10N; use OCP\IRequest; use OCP\IUser; use OCP\IUserSession; +use OCP\Server; use OCP\Settings\IManager; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; +use function json_decode; +use function json_encode; /** * Class AppConfigControllerTest @@ -24,17 +32,12 @@ use Test\TestCase; * @package OCA\Provisioning_API\Tests */ class AppConfigControllerTest extends TestCase { - - /** @var IAppConfig|\PHPUnit\Framework\MockObject\MockObject */ - private $appConfig; - /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */ - private $userSession; - /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */ - private $l10n; - /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ - private $settingManager; - /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */ - private $groupManager; + private IAppConfig&MockObject $appConfig; + private IUserSession&MockObject $userSession; + private IL10N&MockObject $l10n; + private IManager&MockObject $settingManager; + private IGroupManager&MockObject $groupManager; + private IAppManager $appManager; protected function setUp(): void { parent::setUp(); @@ -42,13 +45,14 @@ class AppConfigControllerTest extends TestCase { $this->appConfig = $this->createMock(AppConfig::class); $this->userSession = $this->createMock(IUserSession::class); $this->l10n = $this->createMock(IL10N::class); - $this->groupManager = $this->createMock(IGroupManager::class); $this->settingManager = $this->createMock(IManager::class); + $this->groupManager = $this->createMock(IGroupManager::class); + $this->appManager = Server::get(IAppManager::class); } /** * @param string[] $methods - * @return AppConfigController|\PHPUnit\Framework\MockObject\MockObject + * @return AppConfigController|MockObject */ protected function getInstance(array $methods = []) { $request = $this->createMock(IRequest::class); @@ -61,7 +65,8 @@ class AppConfigControllerTest extends TestCase { $this->userSession, $this->l10n, $this->groupManager, - $this->settingManager + $this->settingManager, + $this->appManager, ); } else { return $this->getMockBuilder(AppConfigController::class) @@ -72,14 +77,15 @@ class AppConfigControllerTest extends TestCase { $this->userSession, $this->l10n, $this->groupManager, - $this->settingManager + $this->settingManager, + $this->appManager, ]) - ->setMethods($methods) + ->onlyMethods($methods) ->getMock(); } } - public function testGetApps() { + public function testGetApps(): void { $this->appConfig->expects($this->once()) ->method('getApps') ->willReturn(['apps']); @@ -90,21 +96,15 @@ class AppConfigControllerTest extends TestCase { $this->assertEquals(['data' => ['apps']], $result->getData()); } - public function dataGetKeys() { + public static function dataGetKeys(): array { return [ ['app1 ', null, new \InvalidArgumentException('error'), Http::STATUS_FORBIDDEN], ['app2', ['keys'], null, Http::STATUS_OK], ]; } - /** - * @dataProvider dataGetKeys - * @param string $app - * @param array|null $keys - * @param \Exception|null $throws - * @param int $status - */ - public function testGetKeys($app, $keys, $throws, $status) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetKeys')] + public function testGetKeys(string $app, ?array $keys, ?\Throwable $throws, int $status): void { $api = $this->getInstance(['verifyAppId']); if ($throws instanceof \Exception) { $api->expects($this->once()) @@ -135,23 +135,15 @@ class AppConfigControllerTest extends TestCase { } } - public function dataGetValue() { + public static function dataGetValue(): array { return [ ['app1', 'key', 'default', null, new \InvalidArgumentException('error'), Http::STATUS_FORBIDDEN], ['app2', 'key', 'default', 'return', null, Http::STATUS_OK], ]; } - /** - * @dataProvider dataGetValue - * @param string $app - * @param string|null $key - * @param string|null $default - * @param string|null $return - * @param \Exception|null $throws - * @param int $status - */ - public function testGetValue($app, $key, $default, $return, $throws, $status) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetValue')] + public function testGetValue(string $app, string $key, string $default, ?string $return, ?\Throwable $throws, int $status): void { $api = $this->getInstance(['verifyAppId']); if ($throws instanceof \Exception) { $api->expects($this->once()) @@ -179,24 +171,23 @@ class AppConfigControllerTest extends TestCase { } } - public function dataSetValue() { + public static function dataSetValue(): array { return [ ['app1', 'key', 'default', new \InvalidArgumentException('error1'), null, Http::STATUS_FORBIDDEN], ['app2', 'key', 'default', null, new \InvalidArgumentException('error2'), Http::STATUS_FORBIDDEN], ['app2', 'key', 'default', null, null, Http::STATUS_OK], + ['app2', 'key', '1', null, null, Http::STATUS_OK, IAppConfig::VALUE_BOOL], + ['app2', 'key', '42', null, null, Http::STATUS_OK, IAppConfig::VALUE_INT], + ['app2', 'key', '4.2', null, null, Http::STATUS_OK, IAppConfig::VALUE_FLOAT], + ['app2', 'key', '42', null, null, Http::STATUS_OK, IAppConfig::VALUE_STRING], + ['app2', 'key', 'secret', null, null, Http::STATUS_OK, IAppConfig::VALUE_STRING | IAppConfig::VALUE_SENSITIVE], + ['app2', 'key', json_encode([4, 2]), null, null, Http::STATUS_OK, IAppConfig::VALUE_ARRAY], + ['app2', 'key', json_encode([4, 2]), null, null, Http::STATUS_OK, new AppConfigUnknownKeyException()], ]; } - /** - * @dataProvider dataSetValue - * @param string $app - * @param string|null $key - * @param string|null $value - * @param \Exception|null $appThrows - * @param \Exception|null $keyThrows - * @param int $status - */ - public function testSetValue($app, $key, $value, $appThrows, $keyThrows, $status) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataSetValue')] + public function testSetValue(string $app, string $key, string $value, ?\Throwable $appThrows, ?\Throwable $keyThrows, int $status, int|\Throwable $type = IAppConfig::VALUE_MIXED): void { $adminUser = $this->createMock(IUser::class); $adminUser->expects($this->once()) ->method('getUid') @@ -239,9 +230,38 @@ class AppConfigControllerTest extends TestCase { ->method('verifyConfigKey') ->with($app, $key); + if ($type instanceof \Throwable) { + $this->appConfig->expects($this->once()) + ->method('getDetails') + ->with($app, $key) + ->willThrowException($type); + } else { + $this->appConfig->expects($this->once()) + ->method('getDetails') + ->with($app, $key) + ->willReturn([ + 'app' => $app, + 'key' => $key, + 'value' => '', // 🤷 + 'type' => $type, + 'lazy' => false, + 'typeString' => (string)$type, // this is not accurate, but acceptable + 'sensitive' => ($type & IAppConfig::VALUE_SENSITIVE) !== 0, + ]); + } + + $configValueSetter = match ($type) { + IAppConfig::VALUE_BOOL => 'setValueBool', + IAppConfig::VALUE_FLOAT => 'setValueFloat', + IAppConfig::VALUE_INT => 'setValueInt', + IAppConfig::VALUE_STRING => 'setValueString', + IAppConfig::VALUE_ARRAY => 'setValueArray', + default => 'setValueMixed', + }; + $this->appConfig->expects($this->once()) - ->method('setValueMixed') - ->with($app, $key, $value); + ->method($configValueSetter) + ->with($app, $key, $configValueSetter === 'setValueArray' ? json_decode($value, true) : $value); } $result = $api->setValue($app, $key, $value); @@ -256,7 +276,7 @@ class AppConfigControllerTest extends TestCase { } } - public function dataDeleteValue() { + public static function dataDeleteValue(): array { return [ ['app1', 'key', new \InvalidArgumentException('error1'), null, Http::STATUS_FORBIDDEN], ['app2', 'key', null, new \InvalidArgumentException('error2'), Http::STATUS_FORBIDDEN], @@ -264,15 +284,8 @@ class AppConfigControllerTest extends TestCase { ]; } - /** - * @dataProvider dataDeleteValue - * @param string $app - * @param string|null $key - * @param \Exception|null $appThrows - * @param \Exception|null $keyThrows - * @param int $status - */ - public function testDeleteValue($app, $key, $appThrows, $keyThrows, $status) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataDeleteValue')] + public function testDeleteValue(string $app, string $key, ?\Throwable $appThrows, ?\Throwable $keyThrows, int $status): void { $api = $this->getInstance(['verifyAppId', 'verifyConfigKey']); if ($appThrows instanceof \Exception) { $api->expects($this->once()) @@ -320,13 +333,13 @@ class AppConfigControllerTest extends TestCase { } } - public function testVerifyAppId() { + public function testVerifyAppId(): void { $api = $this->getInstance(); $this->invokePrivate($api, 'verifyAppId', ['activity']); $this->addToAssertionCount(1); } - public function dataVerifyAppIdThrows() { + public static function dataVerifyAppIdThrows(): array { return [ ['activity..'], ['activity/'], @@ -335,18 +348,15 @@ class AppConfigControllerTest extends TestCase { ]; } - /** - * @dataProvider dataVerifyAppIdThrows - * @param string $app - */ - public function testVerifyAppIdThrows($app) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataVerifyAppIdThrows')] + public function testVerifyAppIdThrows(string $app): void { $this->expectException(\InvalidArgumentException::class); $api = $this->getInstance(); $this->invokePrivate($api, 'verifyAppId', [$app]); } - public function dataVerifyConfigKey() { + public static function dataVerifyConfigKey(): array { return [ ['activity', 'abc', ''], ['dav', 'public_route', ''], @@ -355,19 +365,14 @@ class AppConfigControllerTest extends TestCase { ]; } - /** - * @dataProvider dataVerifyConfigKey - * @param string $app - * @param string $key - * @param string $value - */ - public function testVerifyConfigKey($app, $key, $value) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataVerifyConfigKey')] + public function testVerifyConfigKey(string $app, string $key, string $value): void { $api = $this->getInstance(); $this->invokePrivate($api, 'verifyConfigKey', [$app, $key, $value]); $this->addToAssertionCount(1); } - public function dataVerifyConfigKeyThrows() { + public static function dataVerifyConfigKeyThrows(): array { return [ ['activity', 'installed_version', ''], ['calendar', 'enabled', ''], @@ -381,13 +386,8 @@ class AppConfigControllerTest extends TestCase { ]; } - /** - * @dataProvider dataVerifyConfigKeyThrows - * @param string $app - * @param string $key - * @param string $value - */ - public function testVerifyConfigKeyThrows($app, $key, $value) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataVerifyConfigKeyThrows')] + public function testVerifyConfigKeyThrows(string $app, string $key, string $value): void { $this->expectException(\InvalidArgumentException::class); $api = $this->getInstance(); |