DefinitionParameterTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Tests;
  8. use OCA\Files_External\Lib\DefinitionParameter as Param;
  9. class DefinitionParameterTest extends \Test\TestCase {
  10. public function testJsonSerialization(): void {
  11. $param = new Param('foo', 'bar');
  12. $this->assertEquals([
  13. 'value' => 'bar',
  14. 'flags' => 0,
  15. 'type' => 0,
  16. 'tooltip' => '',
  17. ], $param->jsonSerialize());
  18. $param->setType(Param::VALUE_BOOLEAN);
  19. $param->setDefaultValue(true);
  20. $this->assertEquals([
  21. 'value' => 'bar',
  22. 'flags' => 0,
  23. 'type' => Param::VALUE_BOOLEAN,
  24. 'tooltip' => '',
  25. 'defaultValue' => true,
  26. ], $param->jsonSerialize());
  27. $param->setType(Param::VALUE_PASSWORD);
  28. $param->setFlag(Param::FLAG_OPTIONAL);
  29. $param->setDefaultValue(null);
  30. $this->assertEquals([
  31. 'value' => 'bar',
  32. 'flags' => Param::FLAG_OPTIONAL,
  33. 'type' => Param::VALUE_PASSWORD,
  34. 'tooltip' => '',
  35. ], $param->jsonSerialize());
  36. $param->setType(Param::VALUE_HIDDEN);
  37. $param->setFlags(Param::FLAG_NONE);
  38. $this->assertEquals([
  39. 'value' => 'bar',
  40. 'flags' => Param::FLAG_NONE,
  41. 'type' => Param::VALUE_HIDDEN,
  42. 'tooltip' => '',
  43. ], $param->jsonSerialize());
  44. }
  45. public function validateValueProvider() {
  46. return [
  47. [Param::VALUE_TEXT, Param::FLAG_NONE, 'abc', true],
  48. [Param::VALUE_TEXT, Param::FLAG_NONE, '', false],
  49. [Param::VALUE_TEXT, Param::FLAG_OPTIONAL, '', true],
  50. [Param::VALUE_BOOLEAN, Param::FLAG_NONE, false, true],
  51. [Param::VALUE_BOOLEAN, Param::FLAG_NONE, 123, false],
  52. // conversion from string to boolean
  53. [Param::VALUE_BOOLEAN, Param::FLAG_NONE, 'false', true, false],
  54. [Param::VALUE_BOOLEAN, Param::FLAG_NONE, 'true', true, true],
  55. [Param::VALUE_PASSWORD, Param::FLAG_NONE, 'foobar', true],
  56. [Param::VALUE_PASSWORD, Param::FLAG_NONE, '', false],
  57. [Param::VALUE_HIDDEN, Param::FLAG_NONE, '', false]
  58. ];
  59. }
  60. /**
  61. * @dataProvider validateValueProvider
  62. */
  63. public function testValidateValue($type, $flags, $value, $success, $expectedValue = null): void {
  64. $param = new Param('foo', 'bar');
  65. $param->setType($type);
  66. $param->setFlags($flags);
  67. $this->assertEquals($success, $param->validateValue($value));
  68. if (isset($expectedValue)) {
  69. $this->assertEquals($expectedValue, $value);
  70. }
  71. }
  72. }