definitionparameterttest.php 2.4 KB

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