DefinitionParameterTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. * @author Robin McCorkell <robin@mccorkell.me.uk>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files_External\Tests;
  25. use OCA\Files_External\Lib\DefinitionParameter as Param;
  26. class DefinitionParameterTest extends \Test\TestCase {
  27. public function testJsonSerialization() {
  28. $param = new Param('foo', 'bar');
  29. $this->assertEquals([
  30. 'value' => 'bar',
  31. 'flags' => 0,
  32. 'type' => 0,
  33. 'tooltip' => '',
  34. ], $param->jsonSerialize());
  35. $param->setType(Param::VALUE_BOOLEAN);
  36. $param->setDefaultValue(true);
  37. $this->assertEquals([
  38. 'value' => 'bar',
  39. 'flags' => 0,
  40. 'type' => Param::VALUE_BOOLEAN,
  41. 'tooltip' => '',
  42. 'defaultValue' => true,
  43. ], $param->jsonSerialize());
  44. $param->setType(Param::VALUE_PASSWORD);
  45. $param->setFlag(Param::FLAG_OPTIONAL);
  46. $param->setDefaultValue(null);
  47. $this->assertEquals([
  48. 'value' => 'bar',
  49. 'flags' => Param::FLAG_OPTIONAL,
  50. 'type' => Param::VALUE_PASSWORD,
  51. 'tooltip' => '',
  52. ], $param->jsonSerialize());
  53. $param->setType(Param::VALUE_HIDDEN);
  54. $param->setFlags(Param::FLAG_NONE);
  55. $this->assertEquals([
  56. 'value' => 'bar',
  57. 'flags' => Param::FLAG_NONE,
  58. 'type' => Param::VALUE_HIDDEN,
  59. 'tooltip' => '',
  60. ], $param->jsonSerialize());
  61. }
  62. public function validateValueProvider() {
  63. return [
  64. [Param::VALUE_TEXT, Param::FLAG_NONE, 'abc', true],
  65. [Param::VALUE_TEXT, Param::FLAG_NONE, '', false],
  66. [Param::VALUE_TEXT, Param::FLAG_OPTIONAL, '', true],
  67. [Param::VALUE_BOOLEAN, Param::FLAG_NONE, false, true],
  68. [Param::VALUE_BOOLEAN, Param::FLAG_NONE, 123, false],
  69. // conversion from string to boolean
  70. [Param::VALUE_BOOLEAN, Param::FLAG_NONE, 'false', true, false],
  71. [Param::VALUE_BOOLEAN, Param::FLAG_NONE, 'true', true, true],
  72. [Param::VALUE_PASSWORD, Param::FLAG_NONE, 'foobar', true],
  73. [Param::VALUE_PASSWORD, Param::FLAG_NONE, '', false],
  74. [Param::VALUE_HIDDEN, Param::FLAG_NONE, '', false]
  75. ];
  76. }
  77. /**
  78. * @dataProvider validateValueProvider
  79. */
  80. public function testValidateValue($type, $flags, $value, $success, $expectedValue = null) {
  81. $param = new Param('foo', 'bar');
  82. $param->setType($type);
  83. $param->setFlags($flags);
  84. $this->assertEquals($success, $param->validateValue($value));
  85. if (isset($expectedValue)) {
  86. $this->assertEquals($expectedValue, $value);
  87. }
  88. }
  89. }