aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorFerdinand Thiessen <opensource@fthiessen.de>2024-10-15 20:22:33 +0200
committerFerdinand Thiessen <opensource@fthiessen.de>2024-10-15 20:33:45 +0200
commit9a7e1bb2276f4a4635cd330fbc2a385ebaeed5ee (patch)
treee33bb863c02fc890f596a64354fc440278e67b30 /tests
parentcd3dc1719b8e84526f2c99634f0dc8bf16380579 (diff)
downloadnextcloud-server-9a7e1bb2276f4a4635cd330fbc2a385ebaeed5ee.tar.gz
nextcloud-server-9a7e1bb2276f4a4635cd330fbc2a385ebaeed5ee.zip
feat(DeclarativeSettings): Allow to implement value getter and setter directly in Form
Instead of implementing the form class, a setter event listener and a getter event listener, this allows to simply write a basic class that provides `getSchema`, `setValue` and `getValue` functions. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/Settings/DeclarativeManagerTest.php76
1 files changed, 76 insertions, 0 deletions
diff --git a/tests/lib/Settings/DeclarativeManagerTest.php b/tests/lib/Settings/DeclarativeManagerTest.php
index f0d97c7ea25..522264acd8c 100644
--- a/tests/lib/Settings/DeclarativeManagerTest.php
+++ b/tests/lib/Settings/DeclarativeManagerTest.php
@@ -10,6 +10,8 @@ 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;
@@ -19,6 +21,8 @@ use OCP\IUser;
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;
@@ -52,6 +56,8 @@ class DeclarativeManagerTest extends TestCase {
/** @var IUser|MockObject */
private $adminUser;
+ private IDeclarativeSettingsForm&MockObject $closureForm;
+
public const validSchemaAllFields = [
'id' => 'test_form_1',
'priority' => 10,
@@ -518,4 +524,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);
+ }
+
}