1
0

DefinitionParameterTest.php 2.8 KB

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