diff options
Diffstat (limited to 'tests/lib/RichObjectStrings')
-rw-r--r-- | tests/lib/RichObjectStrings/DefinitionsTest.php | 14 | ||||
-rw-r--r-- | tests/lib/RichObjectStrings/ValidatorTest.php | 68 |
2 files changed, 75 insertions, 7 deletions
diff --git a/tests/lib/RichObjectStrings/DefinitionsTest.php b/tests/lib/RichObjectStrings/DefinitionsTest.php index efe372a25e8..60ea820e844 100644 --- a/tests/lib/RichObjectStrings/DefinitionsTest.php +++ b/tests/lib/RichObjectStrings/DefinitionsTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -7,10 +8,11 @@ namespace Test\RichObjectStrings; use OCP\RichObjectStrings\Definitions; +use OCP\RichObjectStrings\InvalidObjectExeption; use Test\TestCase; class DefinitionsTest extends TestCase { - public function dataGetDefinition() { + public static function dataGetDefinition() { $definitions = new Definitions(); $testsuite = []; foreach ($definitions->definitions as $type => $definition) { @@ -19,9 +21,9 @@ class DefinitionsTest extends TestCase { return $testsuite; } - - public function testGetDefinitionNotExisting() { - $this->expectException(\OCP\RichObjectStrings\InvalidObjectExeption::class); + + public function testGetDefinitionNotExisting(): void { + $this->expectException(InvalidObjectExeption::class); $this->expectExceptionMessage('Object type is undefined'); $definitions = new Definitions(); @@ -29,11 +31,11 @@ class DefinitionsTest extends TestCase { } /** - * @dataProvider dataGetDefinition * @param string $type * @param array $expected */ - public function testGetDefinition($type, array $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetDefinition')] + public function testGetDefinition($type, array $expected): void { $definitions = new Definitions(); $definition = $definitions->getDefinition($type); diff --git a/tests/lib/RichObjectStrings/ValidatorTest.php b/tests/lib/RichObjectStrings/ValidatorTest.php index 3604dcba4f7..26688d3401a 100644 --- a/tests/lib/RichObjectStrings/ValidatorTest.php +++ b/tests/lib/RichObjectStrings/ValidatorTest.php @@ -1,4 +1,6 @@ <?php + +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -8,10 +10,11 @@ namespace Test\RichObjectStrings; use OC\RichObjectStrings\Validator; use OCP\RichObjectStrings\Definitions; +use OCP\RichObjectStrings\InvalidObjectExeption; use Test\TestCase; class ValidatorTest extends TestCase { - public function test() { + public function testValidate(): void { $v = new Validator(new Definitions()); $v->validate('test', []); $v->validate('test {string1} test {foo} test {bar}.', [ @@ -33,5 +36,68 @@ class ValidatorTest extends TestCase { ], ]); $this->addToAssertionCount(2); + + $this->expectException(InvalidObjectExeption::class); + + $this->expectExceptionMessage('Object for placeholder string1 is invalid, value 123 for key key is not a string'); + $v->validate('test {string1} test.', [ + 'string1' => [ + 'type' => 'user', + 'id' => 'johndoe', + 'name' => 'John Doe', + 'key' => 123, + ], + ]); + + $this->expectExceptionMessage('Object for placeholder string1 is invalid, key 456 is not a string'); + $v->validate('test {string1} test.', [ + 'string1' => [ + 'type' => 'user', + 'id' => 'johndoe', + 'name' => 'John Doe', + 456 => 'value', + ], + ]); + } + + public static function dataValidateParameterKeys(): array { + return [ + 'not a string' => ['key' => 0, 'throws' => 'Parameter key is invalid'], + '@ is not allowed' => ['key' => 'user@0', 'throws' => 'Parameter key is invalid'], + '? is not allowed' => ['key' => 'user?0', 'throws' => 'Parameter key is invalid'], + 'slash is not allowed' => ['key' => 'user/0', 'throws' => 'Parameter key is invalid'], + 'backslash is not allowed' => ['key' => 'user\\0', 'throws' => 'Parameter key is invalid'], + 'hash is not allowed' => ['key' => 'user#0', 'throws' => 'Parameter key is invalid'], + 'space is not allowed' => ['key' => 'user 0', 'throws' => 'Parameter key is invalid'], + 'has to start with letter, but is number' => ['key' => '0abc', 'throws' => 'Parameter key is invalid'], + 'has to start with letter, but is dot' => ['key' => '.abc', 'throws' => 'Parameter key is invalid'], + 'has to start with letter, but is slash' => ['key' => '-abc', 'throws' => 'Parameter key is invalid'], + 'has to start with letter, but is underscore' => ['key' => '_abc', 'throws' => 'Parameter key is invalid'], + ['key' => 'user-0', 'throws' => null], + ['key' => 'user_0', 'throws' => null], + ['key' => 'user.0', 'throws' => null], + ['key' => 'a._-0', 'throws' => null], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('dataValidateParameterKeys')] + public function testValidateParameterKeys(mixed $key, ?string $throws): void { + + if ($throws !== null) { + $this->expectExceptionMessage($throws); + } + + $v = new Validator(new Definitions()); + $v->validate('{' . $key . '}', [ + $key => [ + 'type' => 'highlight', + 'id' => 'identifier', + 'name' => 'Display name', + ], + ]); + + if ($throws === null) { + $this->addToAssertionCount(1); + } } } |