ValidatorTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\RichObjectStrings;
  8. use OC\RichObjectStrings\Validator;
  9. use OCP\RichObjectStrings\Definitions;
  10. use OCP\RichObjectStrings\InvalidObjectExeption;
  11. use Test\TestCase;
  12. class ValidatorTest extends TestCase {
  13. public function testValidate(): void {
  14. $v = new Validator(new Definitions());
  15. $v->validate('test', []);
  16. $v->validate('test {string1} test {foo} test {bar}.', [
  17. 'string1' => [
  18. 'type' => 'user',
  19. 'id' => 'johndoe',
  20. 'name' => 'John Doe',
  21. ],
  22. 'foo' => [
  23. 'type' => 'user-group',
  24. 'id' => 'sample',
  25. 'name' => 'Sample Group',
  26. ],
  27. 'bar' => [
  28. 'type' => 'file',
  29. 'id' => '42',
  30. 'name' => 'test.txt',
  31. 'path' => 'path/to/test.txt',
  32. ],
  33. ]);
  34. $this->addToAssertionCount(2);
  35. $this->expectException(InvalidObjectExeption::class);
  36. $this->expectExceptionMessage('Object is invalid, value 123 is not a string');
  37. $v->validate('test {string1} test.', [
  38. 'string1' => [
  39. 'type' => 'user',
  40. 'id' => 'johndoe',
  41. 'name' => 'John Doe',
  42. 'key' => 123,
  43. ],
  44. ]);
  45. $this->expectExceptionMessage('Object is invalid, key 456 is not a string');
  46. $v->validate('test {string1} test.', [
  47. 'string1' => [
  48. 'type' => 'user',
  49. 'id' => 'johndoe',
  50. 'name' => 'John Doe',
  51. 456 => 'value',
  52. ],
  53. ]);
  54. }
  55. public static function dataValidateParameterKeys(): array {
  56. return [
  57. 'not a string' => ['key' => 0, 'throws' => 'Parameter key is invalid'],
  58. '@ is not allowed' => ['key' => 'user@0', 'throws' => 'Parameter key is invalid'],
  59. '? is not allowed' => ['key' => 'user?0', 'throws' => 'Parameter key is invalid'],
  60. 'slash is not allowed' => ['key' => 'user/0', 'throws' => 'Parameter key is invalid'],
  61. 'backslash is not allowed' => ['key' => 'user\\0', 'throws' => 'Parameter key is invalid'],
  62. 'hash is not allowed' => ['key' => 'user#0', 'throws' => 'Parameter key is invalid'],
  63. 'space is not allowed' => ['key' => 'user 0', 'throws' => 'Parameter key is invalid'],
  64. 'has to start with letter, but is number' => ['key' => '0abc', 'throws' => 'Parameter key is invalid'],
  65. 'has to start with letter, but is dot' => ['key' => '.abc', 'throws' => 'Parameter key is invalid'],
  66. 'has to start with letter, but is slash' => ['key' => '-abc', 'throws' => 'Parameter key is invalid'],
  67. 'has to start with letter, but is underscore' => ['key' => '_abc', 'throws' => 'Parameter key is invalid'],
  68. ['key' => 'user-0', 'throws' => null],
  69. ['key' => 'user_0', 'throws' => null],
  70. ['key' => 'user.0', 'throws' => null],
  71. ['key' => 'a._-0', 'throws' => null],
  72. ];
  73. }
  74. /**
  75. * @dataProvider dataValidateParameterKeys
  76. */
  77. public function testValidateParameterKeys(mixed $key, ?string $throws): void {
  78. if ($throws !== null) {
  79. $this->expectExceptionMessage($throws);
  80. }
  81. $v = new Validator(new Definitions());
  82. $v->validate('{' . $key . '}', [
  83. $key => [
  84. 'type' => 'highlight',
  85. 'id' => 'identifier',
  86. 'name' => 'Display name',
  87. ],
  88. ]);
  89. if ($throws === null) {
  90. $this->addToAssertionCount(1);
  91. }
  92. }
  93. }