xref: /webtrees/tests/app/ValidatorTest.php (revision f6fdd7466ca895d4b685167619a017925064cccb)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use Fisharebest\Webtrees\Http\Exceptions\HttpBadRequestException;
23use LogicException;
24
25/**
26 * Test harness for the class Validator
27 */
28class ValidatorTest extends TestCase
29{
30    /**
31     * @covers \Fisharebest\Webtrees\Validator::array
32     */
33    public function testArrayParameter(): void
34    {
35        $parameters = ['param' => ['test'], 'invalid' => 'not_array'];
36        $validator = new Validator($parameters);
37
38        self::assertSame(['test'], $validator->array('param'));
39        self::assertNull($validator->array('invalid'));
40        self::assertNull($validator->array('param2'));
41    }
42
43    /**
44     * @covers \Fisharebest\Webtrees\Validator::integer
45     */
46    public function testIntegerParameter(): void
47    {
48        $parameters = ['param' => '42', 'invalid' => 'not_int', 'integer' => 42];
49        $validator = new Validator($parameters);
50
51        self::assertSame(42, $validator->integer('param'));
52        self::assertNull($validator->integer('invalid'));
53        self::assertNull($validator->integer('integer'));
54        self::assertNull($validator->integer('param2'));
55    }
56
57    /**
58     * @covers \Fisharebest\Webtrees\Validator::string
59     */
60    public function testStringParameter(): void
61    {
62        $parameters = ['param' => 'test', 'invalid' => ['not_string']];
63        $validator = new Validator($parameters);
64
65        self::assertSame('test', $validator->string('param'));
66        self::assertNull($validator->string('invalid'));
67        self::assertNull($validator->string('param2'));
68    }
69
70    /**
71     * @covers \Fisharebest\Webtrees\Validator::requiredArray
72     */
73    public function testRequiredArrayParameter(): void
74    {
75        $parameters = ['param' => ['test'], 'invalid' => 'not_array'];
76        $validator = new Validator($parameters);
77
78        self::assertSame(['test'], $validator->requiredArray('param'));
79
80        $this->expectException(HttpBadRequestException::class);
81        $validator->requiredArray('invalid');
82    }
83
84    /**
85     * @covers \Fisharebest\Webtrees\Validator::requiredInteger
86     */
87    public function testRequiredIntegerParameter(): void
88    {
89        $parameters = ['param' => '42', 'invalid' => 'not_int'];
90        $validator = new Validator($parameters);
91
92        self::assertSame(42, $validator->requiredInteger('param'));
93
94        $this->expectException(HttpBadRequestException::class);
95        $validator->requiredInteger('invalid');
96    }
97
98    /**
99     * @covers \Fisharebest\Webtrees\Validator::requiredString
100     */
101    public function testRequiredStringParameter(): void
102    {
103        $parameters = ['param' => 'test', 'invalid' => ['not_string']];
104        $validator = new Validator($parameters);
105
106        self::assertSame('test', $validator->requiredString('param'));
107
108        $this->expectException(HttpBadRequestException::class);
109        $validator->requiredString('invalid');
110    }
111
112    /**
113     * @covers \Fisharebest\Webtrees\Validator::isBetween
114     */
115    public function testIsBetweenParameter(): void
116    {
117        $parameters = ['param' => '42', 'invalid' => '10', 'wrongtype' => 'not_integer'];
118        $validator = (new Validator($parameters))->isBetween(40, 45);
119
120        self::assertSame(42, $validator->integer('param'));
121        self::assertNull($validator->integer('invalid'));
122        self::assertNull($validator->integer('wrongtype'));
123
124        $this->expectException(LogicException::class);
125        $validator->string('wrongtype');
126    }
127
128    /**
129     * @covers \Fisharebest\Webtrees\Validator::isXref
130     */
131    public function testIsXrefParameter(): void
132    {
133        $parameters = [
134            'param' => 'X1',
135            'param2' => ['X2', 'X3'],
136            'invalid' => '@X1@',
137            'invalid2' => ['X2', '#X4!'],
138            'wrongtype' => '42'
139        ];
140        $validator = (new Validator($parameters))->isXref();
141
142        self::assertSame('X1', $validator->string('param'));
143        self::assertSame(['X2', 'X3'], $validator->array('param2'));
144        self::assertNull($validator->string('param2'));
145        self::assertNull($validator->string('invalid'));
146        self::assertNull($validator->array('invalid2'));
147
148        $this->expectException(LogicException::class);
149        $validator->integer('wrongtype');
150    }
151
152    /**
153     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
154     */
155    public function testIsLocalUrlParameter(): void
156    {
157        $parameters = [
158            'param' => 'http://example.local/wt/page',
159            'noscheme' => '//example.local/wt/page',
160            'https' => 'https://example.local/wt/page',
161            'invalid' => 'http://example.com/wt/page',
162            'wrongtype' => '42'
163        ];
164        $validator = (new Validator($parameters))->isLocalUrl('http://example.local/wt');
165
166        self::assertSame('http://example.local/wt/page', $validator->string('param'));
167        self::assertSame('//example.local/wt/page', $validator->string('noscheme'));
168        self::assertNull($validator->string('https'));
169        self::assertNull($validator->string('invalid'));
170        self::assertNull($validator->integer('param'));
171
172        $this->expectException(LogicException::class);
173        $validator->integer('wrongtype');
174    }
175
176    /**
177     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
178     */
179    public function testIsLocalUrlWithInvalidBaseUrl(): void
180    {
181        $this->expectException(LogicException::class);
182        (new Validator(['param' => 'test']))->isLocalUrl('http://:invalid.url/')->string('param');
183    }
184
185    /**
186     * @covers \Fisharebest\Webtrees\Validator::__construct
187     * @covers \Fisharebest\Webtrees\Validator::parsedBody
188     */
189    public function testParsedBody(): void
190    {
191        $request = self::createRequest()->withQueryParams(['param' => 'test']);
192        self::assertNull(Validator::parsedBody($request)->string('param'));
193
194        $request = self::createRequest()->withParsedBody(['param' => 'test']);
195        self::assertSame('test', Validator::parsedBody($request)->string('param'));
196    }
197
198    /**
199     * @covers \Fisharebest\Webtrees\Validator::__construct
200     * @covers \Fisharebest\Webtrees\Validator::queryParams
201     */
202    public function testQueryParams(): void
203    {
204        $request = self::createRequest()->withParsedBody(['param' => 'test']);
205        self::assertNull(Validator::queryParams($request)->string('param'));
206
207        $request = self::createRequest()->withQueryParams(['param' => 'test']);
208        self::assertSame('test', Validator::queryParams($request)->string('param'));
209    }
210}
211