xref: /webtrees/tests/app/ValidatorTest.php (revision b63ebfff10c4f748c10374a14a84550d3b653912)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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;
24use Psr\Http\Message\ServerRequestInterface;
25
26/**
27 * Test harness for the class Validator
28 */
29class ValidatorTest extends TestCase
30{
31    /**
32     * @covers \Fisharebest\Webtrees\Validator::array
33     */
34    public function testRequiredArrayParameter(): void
35    {
36        $request    = $this->createStub(ServerRequestInterface::class);
37        $parameters = ['param' => ['test'], 'invalid' => 'not_array'];
38        $validator  = new Validator($parameters, $request);
39
40        self::assertSame(['test'], $validator->array('param'));
41
42        $this->expectException(HttpBadRequestException::class);
43        $validator->array('invalid');
44    }
45
46    /**
47     * @covers \Fisharebest\Webtrees\Validator::integer
48     */
49    public function testRequiredIntegerParameter(): void
50    {
51        $request    = $this->createStub(ServerRequestInterface::class);
52        $parameters = ['param' => '42', 'invalid' => 'not_int'];
53        $validator  = new Validator($parameters, $request);
54
55        self::assertSame(42, $validator->integer('param'));
56
57        $this->expectException(HttpBadRequestException::class);
58        $validator->integer('invalid');
59    }
60
61    /**
62     * @covers \Fisharebest\Webtrees\Validator::string
63     */
64    public function testRequiredStringParameter(): void
65    {
66        $request    = $this->createStub(ServerRequestInterface::class);
67        $parameters = ['param' => 'test', 'invalid' => ['not_string']];
68        $validator  = new Validator($parameters, $request);
69
70        self::assertSame('test', $validator->string('param'));
71
72        $this->expectException(HttpBadRequestException::class);
73        $validator->string('invalid');
74    }
75
76    /**
77     * @covers \Fisharebest\Webtrees\Validator::isBetween
78     */
79    public function testIsBetweenParameter(): void
80    {
81        $request    = $this->createStub(ServerRequestInterface::class);
82        $parameters = ['param' => '42', 'invalid' => '10', 'wrongtype' => 'not_integer'];
83        $validator  = (new Validator($parameters, $request))->isBetween(40, 45);
84
85        self::assertSame(42, $validator->integer('param'));
86        self::assertSame(42, $validator->integer('invalid', 42));
87        self::assertSame(42, $validator->integer('wrongtype', 42));
88    }
89
90    /**
91     * @covers \Fisharebest\Webtrees\Validator::isXref
92     */
93    public function testIsXrefParameter(): void
94    {
95        $request    = $this->createStub(ServerRequestInterface::class);
96        $parameters = ['param' => 'X1', 'invalid' => '@X1@'];
97        $validator  = (new Validator($parameters, $request))->isXref();
98
99        self::assertSame('X1', $validator->string('param'));
100
101        $this->expectException(HttpBadRequestException::class);
102        $validator->string('invalid');
103    }
104
105    /**
106     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
107     */
108    public function testIsLocalUrlParameter(): void
109    {
110        $request = $this->createStub(ServerRequestInterface::class);
111        $request->method('getAttribute')->with('base_url')->willReturn('http://example.local/wt');
112
113        $parameters = ['param' => 'http://example.local/wt/page', 'noscheme' => '//example.local/wt/page'];
114        $validator  = (new Validator($parameters, $request))->isLocalUrl();
115
116        self::assertSame('http://example.local/wt/page', $validator->string('param'));
117        self::assertSame('//example.local/wt/page', $validator->string('noscheme'));
118    }
119
120    /**
121     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
122     */
123    public function testIsLocalUrlParameterWrongScheme(): void
124    {
125        $request = $this->createStub(ServerRequestInterface::class);
126        $request->method('getAttribute')->with('base_url')->willReturn('http://example.local/wt');
127
128        $parameters = ['https' => 'https://example.local/wt/page'];
129        $validator  = (new Validator($parameters, $request))->isLocalUrl();
130
131        $this->expectException(HttpBadRequestException::class);
132        $validator->string('https');
133    }
134
135    /**
136     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
137     */
138    public function testIsLocalUrlParameterWrongDomain(): void
139    {
140        $request = $this->createStub(ServerRequestInterface::class);
141        $request->method('getAttribute')->with('base_url')->willReturn('http://example.local/wt');
142
143        $parameters = ['invalid' => 'http://example.com/wt/page'];
144        $validator  = (new Validator($parameters, $request))->isLocalUrl();
145
146        $this->expectException(HttpBadRequestException::class);
147        $validator->string('invalid');
148    }
149
150    /**
151     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
152     */
153    public function testIsLocalUrlParameterWrongType(): void
154    {
155        $request    = $this->createStub(ServerRequestInterface::class);
156        $parameters = ['wrongtype' => ['42']];
157        $validator  = (new Validator($parameters, $request))->isLocalUrl();
158
159        $this->expectException(HttpBadRequestException::class);
160        $validator->string('wrongtype');
161    }
162
163    /**
164     * @covers \Fisharebest\Webtrees\Validator::__construct
165     * @covers \Fisharebest\Webtrees\Validator::parsedBody
166     */
167    public function testParsedBody(): void
168    {
169        $request = self::createRequest()->withParsedBody(['param' => 'test']);
170        self::assertSame('test', Validator::parsedBody($request)->string('param'));
171
172        $this->expectException(HttpBadRequestException::class);
173        $request = self::createRequest()->withQueryParams(['param' => 'test']);
174        Validator::parsedBody($request)->string('param');
175    }
176
177    /**
178     * @covers \Fisharebest\Webtrees\Validator::__construct
179     * @covers \Fisharebest\Webtrees\Validator::queryParams
180     */
181    public function testQueryParams(): void
182    {
183        $request = self::createRequest()->withQueryParams(['param' => 'test']);
184        self::assertSame('test', Validator::queryParams($request)->string('param'));
185
186        $this->expectException(HttpBadRequestException::class);
187        $request = self::createRequest()->withParsedBody(['param' => 'test']);
188        Validator::queryParams($request)->string('param');
189    }
190}
191