aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/RichObjectStrings/ValidatorTest.php
blob: c5f88394a3392707c8ac85ec28f2762d6fa1b24c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php

declare(strict_types=1);
/**
 * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace Test\RichObjectStrings;

use OC\RichObjectStrings\Validator;
use OCP\RichObjectStrings\Definitions;
use OCP\RichObjectStrings\InvalidObjectExeption;
use Test\TestCase;

class ValidatorTest extends TestCase {
	public function testValidate(): void {
		$v = new Validator(new Definitions());
		$v->validate('test', []);
		$v->validate('test {string1} test {foo} test {bar}.', [
			'string1' => [
				'type' => 'user',
				'id' => 'johndoe',
				'name' => 'John Doe',
			],
			'foo' => [
				'type' => 'user-group',
				'id' => 'sample',
				'name' => 'Sample Group',
			],
			'bar' => [
				'type' => 'file',
				'id' => '42',
				'name' => 'test.txt',
				'path' => 'path/to/test.txt',
			],
		]);
		$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],
		];
	}

	/**
	 * @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);
		}
	}
}