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