DefinitionParameterTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. $this->assertEquals([
  37. 'value' => 'bar',
  38. 'flags' => 0,
  39. 'type' => Param::VALUE_BOOLEAN,
  40. 'tooltip' => '',
  41. ], $param->jsonSerialize());
  42. $param->setType(Param::VALUE_PASSWORD);
  43. $param->setFlag(Param::FLAG_OPTIONAL);
  44. $this->assertEquals([
  45. 'value' => 'bar',
  46. 'flags' => Param::FLAG_OPTIONAL,
  47. 'type' => Param::VALUE_PASSWORD,
  48. 'tooltip' => '',
  49. ], $param->jsonSerialize());
  50. $param->setType(Param::VALUE_HIDDEN);
  51. $param->setFlags(Param::FLAG_NONE);
  52. $this->assertEquals([
  53. 'value' => 'bar',
  54. 'flags' => Param::FLAG_NONE,
  55. 'type' => Param::VALUE_HIDDEN,
  56. 'tooltip' => '',
  57. ], $param->jsonSerialize());
  58. }
  59. public function validateValueProvider() {
  60. return [
  61. [Param::VALUE_TEXT, Param::FLAG_NONE, 'abc', true],
  62. [Param::VALUE_TEXT, Param::FLAG_NONE, '', false],
  63. [Param::VALUE_TEXT, Param::FLAG_OPTIONAL, '', true],
  64. [Param::VALUE_BOOLEAN, Param::FLAG_NONE, false, true],
  65. [Param::VALUE_BOOLEAN, Param::FLAG_NONE, 123, false],
  66. // conversion from string to boolean
  67. [Param::VALUE_BOOLEAN, Param::FLAG_NONE, 'false', true, false],
  68. [Param::VALUE_BOOLEAN, Param::FLAG_NONE, 'true', true, true],
  69. [Param::VALUE_PASSWORD, Param::FLAG_NONE, 'foobar', true],
  70. [Param::VALUE_PASSWORD, Param::FLAG_NONE, '', false],
  71. [Param::VALUE_HIDDEN, Param::FLAG_NONE, '', false]
  72. ];
  73. }
  74. /**
  75. * @dataProvider validateValueProvider
  76. */
  77. public function testValidateValue($type, $flags, $value, $success, $expectedValue = null) {
  78. $param = new Param('foo', 'bar');
  79. $param->setType($type);
  80. $param->setFlags($flags);
  81. $this->assertEquals($success, $param->validateValue($value));
  82. if (isset($expectedValue)) {
  83. $this->assertEquals($expectedValue, $value);
  84. }
  85. }
  86. }