xref: /webtrees/tests/app/ValidatorTest.php (revision cd1ec0d0efaac433e873afc688050c81d1b4ad02)
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 = [
118            'param'     => '42',
119            'invalid'   => '10',
120            'wrongtype' => 'not_integer',
121        ];
122        $validator = (new Validator($parameters))->isBetween(40, 45);
123
124        self::assertSame(42, $validator->integer('param'));
125        self::assertNull($validator->integer('invalid'));
126        self::assertNull($validator->integer('wrongtype'));
127    }
128
129    /**
130     * @covers \Fisharebest\Webtrees\Validator::isXref
131     */
132    public function testIsXrefParameter(): void
133    {
134        $parameters = [
135            'param' => 'X1',
136            'invalid' => '@X1@',
137        ];
138        $validator = (new Validator($parameters))->isXref();
139
140        self::assertSame('X1', $validator->string('param'));
141        self::assertNull($validator->string('invalid'));
142    }
143
144    /**
145     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
146     */
147    public function testIsLocalUrlParameter(): void
148    {
149        $parameters = [
150            'param'     => 'http://example.local/wt/page',
151            'noscheme'  => '//example.local/wt/page',
152            'https'     => 'https://example.local/wt/page',
153            'invalid'   => 'http://example.com/wt/page',
154            'wrongtype' => ['42']
155        ];
156        $validator = (new Validator($parameters))->isLocalUrl('http://example.local/wt');
157
158        self::assertSame('http://example.local/wt/page', $validator->string('param'));
159        self::assertSame('//example.local/wt/page', $validator->string('noscheme'));
160        self::assertNull($validator->string('https'));
161        self::assertNull($validator->string('invalid'));
162        self::assertNull($validator->string('wrongtype'));
163    }
164
165    /**
166     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
167     */
168    public function testIsLocalUrlWithInvalidBaseUrl(): void
169    {
170        $this->expectException(LogicException::class);
171        (new Validator(['param' => 'test']))->isLocalUrl('http://:invalid.url/')->string('param');
172    }
173
174    /**
175     * @covers \Fisharebest\Webtrees\Validator::__construct
176     * @covers \Fisharebest\Webtrees\Validator::parsedBody
177     */
178    public function testParsedBody(): void
179    {
180        $request = self::createRequest()->withQueryParams(['param' => 'test']);
181        self::assertNull(Validator::parsedBody($request)->string('param'));
182
183        $request = self::createRequest()->withParsedBody(['param' => 'test']);
184        self::assertSame('test', Validator::parsedBody($request)->string('param'));
185    }
186
187    /**
188     * @covers \Fisharebest\Webtrees\Validator::__construct
189     * @covers \Fisharebest\Webtrees\Validator::queryParams
190     */
191    public function testQueryParams(): void
192    {
193        $request = self::createRequest()->withParsedBody(['param' => 'test']);
194        self::assertNull(Validator::queryParams($request)->string('param'));
195
196        $request = self::createRequest()->withQueryParams(['param' => 'test']);
197        self::assertSame('test', Validator::queryParams($request)->string('param'));
198    }
199}
200