diff options
Diffstat (limited to 'tests/lib/Settings')
-rw-r--r-- | tests/lib/Settings/DeclarativeManagerTest.php | 118 | ||||
-rw-r--r-- | tests/lib/Settings/ManagerTest.php | 45 | ||||
-rw-r--r-- | tests/lib/Settings/SectionTest.php | 7 |
3 files changed, 140 insertions, 30 deletions
diff --git a/tests/lib/Settings/DeclarativeManagerTest.php b/tests/lib/Settings/DeclarativeManagerTest.php index f0d97c7ea25..59411e1f825 100644 --- a/tests/lib/Settings/DeclarativeManagerTest.php +++ b/tests/lib/Settings/DeclarativeManagerTest.php @@ -10,15 +10,20 @@ declare(strict_types=1); namespace Test\Settings; use OC\AppFramework\Bootstrap\Coordinator; +use OC\AppFramework\Bootstrap\RegistrationContext; +use OC\AppFramework\Bootstrap\ServiceRegistration; use OC\Settings\DeclarativeManager; use OCP\EventDispatcher\IEventDispatcher; use OCP\IAppConfig; use OCP\IConfig; use OCP\IGroupManager; use OCP\IUser; +use OCP\Security\ICrypto; use OCP\Settings\DeclarativeSettingsTypes; use OCP\Settings\Events\DeclarativeSettingsSetValueEvent; use OCP\Settings\IDeclarativeManager; +use OCP\Settings\IDeclarativeSettingsForm; +use OCP\Settings\IDeclarativeSettingsFormWithHandlers; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\TestCase; @@ -46,12 +51,17 @@ class DeclarativeManagerTest extends TestCase { /** @var LoggerInterface|MockObject */ private $logger; + /** @var ICrypto|MockObject */ + private $crypto; + /** @var IUser|MockObject */ private $user; /** @var IUser|MockObject */ private $adminUser; + private IDeclarativeSettingsForm&MockObject $closureForm; + public const validSchemaAllFields = [ 'id' => 'test_form_1', 'priority' => 10, @@ -209,6 +219,36 @@ class DeclarativeManagerTest extends TestCase { ], ], ], + [ + 'id' => 'test_sensitive_field', + 'title' => 'Sensitive text field', + 'description' => 'Set some secure value setting that is stored encrypted', + 'type' => DeclarativeSettingsTypes::TEXT, + 'label' => 'Sensitive field', + 'placeholder' => 'Set secure value', + 'default' => '', + 'sensitive' => true, // only for TEXT, PASSWORD types + ], + [ + 'id' => 'test_sensitive_field_2', + 'title' => 'Sensitive password field', + 'description' => 'Set some password setting that is stored encrypted', + 'type' => DeclarativeSettingsTypes::PASSWORD, + 'label' => 'Sensitive field', + 'placeholder' => 'Set secure value', + 'default' => '', + 'sensitive' => true, // only for TEXT, PASSWORD types + ], + [ + 'id' => 'test_non_sensitive_field', + 'title' => 'Password field', + 'description' => 'Set some password setting', + 'type' => DeclarativeSettingsTypes::PASSWORD, + 'label' => 'Password field', + 'placeholder' => 'Set secure value', + 'default' => '', + 'sensitive' => false, + ], ], ]; @@ -223,6 +263,7 @@ class DeclarativeManagerTest extends TestCase { $this->config = $this->createMock(IConfig::class); $this->appConfig = $this->createMock(IAppConfig::class); $this->logger = $this->createMock(LoggerInterface::class); + $this->crypto = $this->createMock(ICrypto::class); $this->declarativeManager = new DeclarativeManager( $this->eventDispatcher, @@ -230,7 +271,8 @@ class DeclarativeManagerTest extends TestCase { $this->coordinator, $this->config, $this->appConfig, - $this->logger + $this->logger, + $this->crypto, ); $this->user = $this->createMock(IUser::class); @@ -303,9 +345,7 @@ class DeclarativeManagerTest extends TestCase { $this->assertFalse(isset($formIds[$app]) && in_array($schemaDuplicateFields['id'], $formIds[$app])); } - /** - * @dataProvider dataValidateSchema - */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataValidateSchema')] public function testValidateSchema(bool $expected, bool $expectException, string $app, array $schema): void { if ($expectException) { $this->expectException(\Exception::class); @@ -518,4 +558,74 @@ class DeclarativeManagerTest extends TestCase { $this->expectException(\Exception::class); $this->declarativeManager->getFormsWithValues($this->user, $schema['section_type'], $schema['section_id']); } + + /** + * Ensure that the `setValue` method is called if the form implements the handler interface. + */ + public function testSetValueWithHandler(): void { + $schema = self::validSchemaAllFields; + $schema['storage_type'] = DeclarativeSettingsTypes::STORAGE_TYPE_EXTERNAL; + + $form = $this->createMock(IDeclarativeSettingsFormWithHandlers::class); + $form->expects(self::atLeastOnce()) + ->method('getSchema') + ->willReturn($schema); + // The setter should be called once! + $form->expects(self::once()) + ->method('setValue') + ->with('test_field_2', 'some password', $this->adminUser); + + \OC::$server->registerService('OCA\\Testing\\Settings\\DeclarativeForm', fn () => $form, false); + + $context = $this->createMock(RegistrationContext::class); + $context->expects(self::atLeastOnce()) + ->method('getDeclarativeSettings') + ->willReturn([new ServiceRegistration('testing', 'OCA\\Testing\\Settings\\DeclarativeForm')]); + + $this->coordinator->expects(self::atLeastOnce()) + ->method('getRegistrationContext') + ->willReturn($context); + + $this->declarativeManager->loadSchemas(); + + $this->eventDispatcher->expects(self::never()) + ->method('dispatchTyped'); + + $this->declarativeManager->setValue($this->adminUser, 'testing', 'test_form_1', 'test_field_2', 'some password'); + } + + public function testGetValueWithHandler(): void { + $schema = self::validSchemaAllFields; + $schema['storage_type'] = DeclarativeSettingsTypes::STORAGE_TYPE_EXTERNAL; + + $form = $this->createMock(IDeclarativeSettingsFormWithHandlers::class); + $form->expects(self::atLeastOnce()) + ->method('getSchema') + ->willReturn($schema); + // The setter should be called once! + $form->expects(self::once()) + ->method('getValue') + ->with('test_field_2', $this->adminUser) + ->willReturn('very secret password'); + + \OC::$server->registerService('OCA\\Testing\\Settings\\DeclarativeForm', fn () => $form, false); + + $context = $this->createMock(RegistrationContext::class); + $context->expects(self::atLeastOnce()) + ->method('getDeclarativeSettings') + ->willReturn([new ServiceRegistration('testing', 'OCA\\Testing\\Settings\\DeclarativeForm')]); + + $this->coordinator->expects(self::atLeastOnce()) + ->method('getRegistrationContext') + ->willReturn($context); + + $this->declarativeManager->loadSchemas(); + + $this->eventDispatcher->expects(self::never()) + ->method('dispatchTyped'); + + $password = $this->invokePrivate($this->declarativeManager, 'getValue', [$this->adminUser, 'testing', 'test_form_1', 'test_field_2']); + self::assertEquals('very secret password', $password); + } + } diff --git a/tests/lib/Settings/ManagerTest.php b/tests/lib/Settings/ManagerTest.php index 6ed8ed74359..91277cc7f1e 100644 --- a/tests/lib/Settings/ManagerTest.php +++ b/tests/lib/Settings/ManagerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -8,6 +9,7 @@ namespace OC\Settings\Tests\AppInfo; use OC\Settings\AuthorizedGroupMapper; use OC\Settings\Manager; +use OCA\WorkflowEngine\Settings\Section; use OCP\Group\ISubAdmin; use OCP\IDBConnection; use OCP\IGroupManager; @@ -15,6 +17,7 @@ use OCP\IL10N; use OCP\IServerContainer; use OCP\IURLGenerator; use OCP\L10N\IFactory; +use OCP\Server; use OCP\Settings\ISettings; use OCP\Settings\ISubAdminSettings; use PHPUnit\Framework\MockObject\MockObject; @@ -64,12 +67,12 @@ class ManagerTest extends TestCase { ); } - public function testGetAdminSections() { - $this->manager->registerSection('admin', \OCA\WorkflowEngine\Settings\Section::class); + public function testGetAdminSections(): void { + $this->manager->registerSection('admin', Section::class); - $section = \OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class); + $section = Server::get(Section::class); $this->container->method('get') - ->with(\OCA\WorkflowEngine\Settings\Section::class) + ->with(Section::class) ->willReturn($section); $this->assertEquals([ @@ -77,12 +80,12 @@ class ManagerTest extends TestCase { ], $this->manager->getAdminSections()); } - public function testGetPersonalSections() { - $this->manager->registerSection('personal', \OCA\WorkflowEngine\Settings\Section::class); + public function testGetPersonalSections(): void { + $this->manager->registerSection('personal', Section::class); - $section = \OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class); + $section = Server::get(Section::class); $this->container->method('get') - ->with(\OCA\WorkflowEngine\Settings\Section::class) + ->with(Section::class) ->willReturn($section); $this->assertEquals([ @@ -90,11 +93,11 @@ class ManagerTest extends TestCase { ], $this->manager->getPersonalSections()); } - public function testGetAdminSectionsEmptySection() { + public function testGetAdminSectionsEmptySection(): void { $this->assertEquals([], $this->manager->getAdminSections()); } - public function testGetPersonalSectionsEmptySection() { + public function testGetPersonalSectionsEmptySection(): void { $this->l10nFactory ->expects($this->once()) ->method('get') @@ -108,7 +111,7 @@ class ManagerTest extends TestCase { $this->assertEquals([], $this->manager->getPersonalSections()); } - public function testGetAdminSettings() { + public function testGetAdminSettings(): void { $section = $this->createMock(ISettings::class); $section->method('getPriority') ->willReturn(13); @@ -126,7 +129,7 @@ class ManagerTest extends TestCase { ], $settings); } - public function testGetAdminSettingsAsSubAdmin() { + public function testGetAdminSettingsAsSubAdmin(): void { $section = $this->createMock(ISettings::class); $section->method('getPriority') ->willReturn(13); @@ -142,7 +145,7 @@ class ManagerTest extends TestCase { $this->assertEquals([], $settings); } - public function testGetSubAdminSettingsAsSubAdmin() { + public function testGetSubAdminSettingsAsSubAdmin(): void { $section = $this->createMock(ISubAdminSettings::class); $section->method('getPriority') ->willReturn(13); @@ -161,7 +164,7 @@ class ManagerTest extends TestCase { ], $settings); } - public function testGetPersonalSettings() { + public function testGetPersonalSettings(): void { $section = $this->createMock(ISettings::class); $section->method('getPriority') ->willReturn(16); @@ -178,10 +181,6 @@ class ManagerTest extends TestCase { $this->container->expects($this->exactly(2)) ->method('get') - ->withConsecutive( - ['section1'], - ['section2'] - ) ->willReturnMap([ ['section1', $section], ['section2', $section2], @@ -195,7 +194,7 @@ class ManagerTest extends TestCase { ], $settings); } - public function testSameSectionAsPersonalAndAdmin() { + public function testSameSectionAsPersonalAndAdmin(): void { $this->l10nFactory ->expects($this->once()) ->method('get') @@ -206,13 +205,13 @@ class ManagerTest extends TestCase { ->method('t') ->willReturnArgument(0); - $this->manager->registerSection('personal', \OCA\WorkflowEngine\Settings\Section::class); - $this->manager->registerSection('admin', \OCA\WorkflowEngine\Settings\Section::class); + $this->manager->registerSection('personal', Section::class); + $this->manager->registerSection('admin', Section::class); - $section = \OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class); + $section = Server::get(Section::class); $this->container->method('get') - ->with(\OCA\WorkflowEngine\Settings\Section::class) + ->with(Section::class) ->willReturn($section); $this->assertEquals([ diff --git a/tests/lib/Settings/SectionTest.php b/tests/lib/Settings/SectionTest.php index 964256f303c..ecd654ba748 100644 --- a/tests/lib/Settings/SectionTest.php +++ b/tests/lib/Settings/SectionTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -10,13 +11,13 @@ use OC\Settings\Section; use Test\TestCase; class SectionTest extends TestCase { - public function testGetID() { + public function testGetID(): void { $this->assertSame('ldap', (new Section('ldap', 'name', 1))->getID()); } - public function testGetName() { + public function testGetName(): void { $this->assertSame('name', (new Section('ldap', 'name', 1))->getName()); } - public function testGetPriority() { + public function testGetPriority(): void { $this->assertSame(1, (new Section('ldap', 'name', 1))->getPriority()); } } |