xref: /webtrees/tests/app/ValidatorTest.php (revision a5f003cf4c42de438f568dff311c4fbb7495e541)
1f6fdd746SJonathan Jaubart<?php
2f6fdd746SJonathan Jaubart
3f6fdd746SJonathan Jaubart/**
4f6fdd746SJonathan Jaubart * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
6f6fdd746SJonathan Jaubart * This program is free software: you can redistribute it and/or modify
7f6fdd746SJonathan Jaubart * it under the terms of the GNU General Public License as published by
8f6fdd746SJonathan Jaubart * the Free Software Foundation, either version 3 of the License, or
9f6fdd746SJonathan Jaubart * (at your option) any later version.
10f6fdd746SJonathan Jaubart * This program is distributed in the hope that it will be useful,
11f6fdd746SJonathan Jaubart * but WITHOUT ANY WARRANTY; without even the implied warranty of
12f6fdd746SJonathan Jaubart * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13f6fdd746SJonathan Jaubart * GNU General Public License for more details.
14f6fdd746SJonathan Jaubart * You should have received a copy of the GNU General Public License
15f6fdd746SJonathan Jaubart * along with this program. If not, see <https://www.gnu.org/licenses/>.
16f6fdd746SJonathan Jaubart */
17f6fdd746SJonathan Jaubart
18f6fdd746SJonathan Jaubartdeclare(strict_types=1);
19f6fdd746SJonathan Jaubart
20f6fdd746SJonathan Jaubartnamespace Fisharebest\Webtrees;
21f6fdd746SJonathan Jaubart
22*a5f003cfSGreg Roachuse Aura\Router\Route;
23*a5f003cfSGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
24f6fdd746SJonathan Jaubartuse Fisharebest\Webtrees\Http\Exceptions\HttpBadRequestException;
25f507cef9SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
26f6fdd746SJonathan Jaubart
27f6fdd746SJonathan Jaubart/**
28f6fdd746SJonathan Jaubart * Test harness for the class Validator
29f6fdd746SJonathan Jaubart */
30f6fdd746SJonathan Jaubartclass ValidatorTest extends TestCase
31f6fdd746SJonathan Jaubart{
32*a5f003cfSGreg Roach
33*a5f003cfSGreg Roach    /**
34*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::attributes
35*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
36*a5f003cfSGreg Roach     */
37*a5f003cfSGreg Roach    public function testAttributes(): void
38*a5f003cfSGreg Roach    {
39*a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
40*a5f003cfSGreg Roach        $request
41*a5f003cfSGreg Roach            ->method('getAttributes')
42*a5f003cfSGreg Roach            ->willReturn(['param' => 'test']);
43*a5f003cfSGreg Roach
44*a5f003cfSGreg Roach        self::assertSame('test', Validator::attributes($request)->string('param'));
45*a5f003cfSGreg Roach    }
46*a5f003cfSGreg Roach
47*a5f003cfSGreg Roach    /**
48*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::parsedBody
49*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
50*a5f003cfSGreg Roach     */
51*a5f003cfSGreg Roach    public function testParsedBody(): void
52*a5f003cfSGreg Roach    {
53*a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
54*a5f003cfSGreg Roach        $request
55*a5f003cfSGreg Roach            ->method('getParsedBody')
56*a5f003cfSGreg Roach            ->willReturn(['param' => 'test']);
57*a5f003cfSGreg Roach
58*a5f003cfSGreg Roach        self::assertSame('test', Validator::parsedBody($request)->string('param'));
59*a5f003cfSGreg Roach    }
60*a5f003cfSGreg Roach
61*a5f003cfSGreg Roach    /**
62*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::queryParams
63*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
64*a5f003cfSGreg Roach     */
65*a5f003cfSGreg Roach    public function testQueryParams(): void
66*a5f003cfSGreg Roach    {
67*a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
68*a5f003cfSGreg Roach        $request
69*a5f003cfSGreg Roach            ->method('getQueryParams')
70*a5f003cfSGreg Roach            ->willReturn(['param' => 'test']);
71*a5f003cfSGreg Roach
72*a5f003cfSGreg Roach        self::assertSame('test', Validator::queryParams($request)->string('param'));
73*a5f003cfSGreg Roach    }
74*a5f003cfSGreg Roach
75*a5f003cfSGreg Roach    /**
76*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::serverParams
77*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
78*a5f003cfSGreg Roach     */
79*a5f003cfSGreg Roach    public function testServerParams(): void
80*a5f003cfSGreg Roach    {
81*a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
82*a5f003cfSGreg Roach        $request
83*a5f003cfSGreg Roach            ->method('getServerParams')
84*a5f003cfSGreg Roach            ->willReturn(['param' => 'test']);
85*a5f003cfSGreg Roach
86*a5f003cfSGreg Roach        self::assertSame('test', Validator::serverParams($request)->string('param'));
87*a5f003cfSGreg Roach    }
88*a5f003cfSGreg Roach
89*a5f003cfSGreg Roach    /**
90*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::queryParams
91*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
92*a5f003cfSGreg Roach     */
93*a5f003cfSGreg Roach    public function testNonUTF8QueryParameterName(): void
94*a5f003cfSGreg Roach    {
95*a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
96*a5f003cfSGreg Roach        $request
97*a5f003cfSGreg Roach            ->method('getQueryParams')
98*a5f003cfSGreg Roach            ->willReturn(["\xFF" => 'test']);
99*a5f003cfSGreg Roach
100*a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
101*a5f003cfSGreg Roach
102*a5f003cfSGreg Roach        Validator::queryParams($request);
103*a5f003cfSGreg Roach    }
104*a5f003cfSGreg Roach
105*a5f003cfSGreg Roach    /**
106*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::queryParams
107*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
108*a5f003cfSGreg Roach     */
109*a5f003cfSGreg Roach    public function testNonUTF8QueryParameterValue(): void
110*a5f003cfSGreg Roach    {
111*a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
112*a5f003cfSGreg Roach        $request
113*a5f003cfSGreg Roach            ->method('getQueryParams')
114*a5f003cfSGreg Roach            ->willReturn(['test' => "\xFF"]);
115*a5f003cfSGreg Roach
116*a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
117*a5f003cfSGreg Roach
118*a5f003cfSGreg Roach        Validator::queryParams($request);
119*a5f003cfSGreg Roach    }
120*a5f003cfSGreg Roach
121f6fdd746SJonathan Jaubart    /**
122b55cbc6bSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::array
123*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
124f6fdd746SJonathan Jaubart     */
125f6fdd746SJonathan Jaubart    public function testRequiredArrayParameter(): void
126f6fdd746SJonathan Jaubart    {
127f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
128*a5f003cfSGreg Roach        $request
129*a5f003cfSGreg Roach            ->method('getQueryParams')
130*a5f003cfSGreg Roach            ->willReturn(['param' => ['test'], 'invalid' => 'not_array']);
131f6fdd746SJonathan Jaubart
132*a5f003cfSGreg Roach
133*a5f003cfSGreg Roach        self::assertSame(['test'], Validator::queryParams($request)->array('param'));
134f6fdd746SJonathan Jaubart
135f6fdd746SJonathan Jaubart        $this->expectException(HttpBadRequestException::class);
136*a5f003cfSGreg Roach
137*a5f003cfSGreg Roach        Validator::queryParams($request)->array('invalid');
138*a5f003cfSGreg Roach    }
139*a5f003cfSGreg Roach
140*a5f003cfSGreg Roach    /**
141*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::boolean
142*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
143*a5f003cfSGreg Roach     */
144*a5f003cfSGreg Roach    public function testRequiredBooleanParameter(): void
145*a5f003cfSGreg Roach    {
146*a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
147*a5f003cfSGreg Roach        $request
148*a5f003cfSGreg Roach            ->method('getQueryParams')
149*a5f003cfSGreg Roach            ->willReturn([
150*a5f003cfSGreg Roach                'a'    => '1',
151*a5f003cfSGreg Roach                'b'    => 'on',
152*a5f003cfSGreg Roach                'c'    => true,
153*a5f003cfSGreg Roach                'd' => '0',
154*a5f003cfSGreg Roach                'e' => '',
155*a5f003cfSGreg Roach                'f' => false,
156*a5f003cfSGreg Roach            ]);
157*a5f003cfSGreg Roach
158*a5f003cfSGreg Roach        self::assertSame(true, Validator::queryParams($request)->boolean('a'));
159*a5f003cfSGreg Roach        self::assertSame(true, Validator::queryParams($request)->boolean('b'));
160*a5f003cfSGreg Roach        self::assertSame(true, Validator::queryParams($request)->boolean('c'));
161*a5f003cfSGreg Roach        self::assertSame(false, Validator::queryParams($request)->boolean('d'));
162*a5f003cfSGreg Roach        self::assertSame(false, Validator::queryParams($request)->boolean('e'));
163*a5f003cfSGreg Roach        self::assertSame(false, Validator::queryParams($request)->boolean('f'));
164*a5f003cfSGreg Roach        self::assertSame(false, Validator::queryParams($request)->boolean('g', false));
165*a5f003cfSGreg Roach
166*a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
167*a5f003cfSGreg Roach
168*a5f003cfSGreg Roach        Validator::queryParams($request)->boolean('h');
169f6fdd746SJonathan Jaubart    }
170f6fdd746SJonathan Jaubart
171f6fdd746SJonathan Jaubart    /**
172b55cbc6bSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::integer
173*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
174f6fdd746SJonathan Jaubart     */
175f6fdd746SJonathan Jaubart    public function testRequiredIntegerParameter(): void
176f6fdd746SJonathan Jaubart    {
177f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
178*a5f003cfSGreg Roach        $request
179*a5f003cfSGreg Roach            ->method('getQueryParams')
180*a5f003cfSGreg Roach            ->willReturn([
18165625b93SGreg Roach                'int_type_positive'    => 42,
18265625b93SGreg Roach                'int_type_negative'    => -42,
18365625b93SGreg Roach                'string_type_positive' => '42',
18465625b93SGreg Roach                'string_type_negative' => '-42',
18565625b93SGreg Roach                'invalid'              => 'not_int',
186*a5f003cfSGreg Roach            ]);
187f6fdd746SJonathan Jaubart
188*a5f003cfSGreg Roach        self::assertSame(42, Validator::queryParams($request)->integer('int_type_positive'));
189*a5f003cfSGreg Roach        self::assertSame(-42, Validator::queryParams($request)->integer('int_type_negative'));
190*a5f003cfSGreg Roach        self::assertSame(42, Validator::queryParams($request)->integer('string_type_positive'));
191*a5f003cfSGreg Roach        self::assertSame(-42, Validator::queryParams($request)->integer('string_type_negative'));
192f6fdd746SJonathan Jaubart
193f6fdd746SJonathan Jaubart        $this->expectException(HttpBadRequestException::class);
194*a5f003cfSGreg Roach
195*a5f003cfSGreg Roach        Validator::queryParams($request)->integer('invalid');
196*a5f003cfSGreg Roach    }
197*a5f003cfSGreg Roach
198*a5f003cfSGreg Roach    /**
199*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::route
200*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
201*a5f003cfSGreg Roach     */
202*a5f003cfSGreg Roach    public function testRequiredRouteParameter(): void
203*a5f003cfSGreg Roach    {
204*a5f003cfSGreg Roach        $route = $this->createStub(Route::class);
205*a5f003cfSGreg Roach
206*a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
207*a5f003cfSGreg Roach        $request
208*a5f003cfSGreg Roach            ->method('getQueryParams')
209*a5f003cfSGreg Roach            ->willReturn([
210*a5f003cfSGreg Roach                'valid-route' => $route,
211*a5f003cfSGreg Roach                'not-route'   => '',
212*a5f003cfSGreg Roach            ]);
213*a5f003cfSGreg Roach
214*a5f003cfSGreg Roach        self::assertSame($route, Validator::queryParams($request)->route('valid-route'));
215*a5f003cfSGreg Roach
216*a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
217*a5f003cfSGreg Roach
218*a5f003cfSGreg Roach        Validator::queryParams($request)->route('not-route');
219f6fdd746SJonathan Jaubart    }
220f6fdd746SJonathan Jaubart
221f6fdd746SJonathan Jaubart    /**
222b55cbc6bSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::string
223*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
224f6fdd746SJonathan Jaubart     */
225f6fdd746SJonathan Jaubart    public function testRequiredStringParameter(): void
226f6fdd746SJonathan Jaubart    {
227f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
228*a5f003cfSGreg Roach        $request
229*a5f003cfSGreg Roach            ->method('getQueryParams')
230*a5f003cfSGreg Roach            ->willReturn(['param' => 'test', 'invalid' => ['not_string']]);
231f6fdd746SJonathan Jaubart
232*a5f003cfSGreg Roach        self::assertSame('test', Validator::queryParams($request)->string('param'));
233f6fdd746SJonathan Jaubart
234f6fdd746SJonathan Jaubart        $this->expectException(HttpBadRequestException::class);
235*a5f003cfSGreg Roach
236*a5f003cfSGreg Roach        Validator::queryParams($request)->string('invalid');
237*a5f003cfSGreg Roach    }
238*a5f003cfSGreg Roach
239*a5f003cfSGreg Roach    /**
240*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::tree
241*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
242*a5f003cfSGreg Roach     */
243*a5f003cfSGreg Roach    public function testRequiredTreeParameter(): void
244*a5f003cfSGreg Roach    {
245*a5f003cfSGreg Roach        $tree = $this->createStub(Tree::class);
246*a5f003cfSGreg Roach
247*a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
248*a5f003cfSGreg Roach        $request
249*a5f003cfSGreg Roach            ->method('getQueryParams')
250*a5f003cfSGreg Roach            ->willReturn([
251*a5f003cfSGreg Roach                'valid-tree' => $tree,
252*a5f003cfSGreg Roach                'not-tree'   => '',
253*a5f003cfSGreg Roach            ]);
254*a5f003cfSGreg Roach
255*a5f003cfSGreg Roach        self::assertSame($tree, Validator::queryParams($request)->tree('valid-tree'));
256*a5f003cfSGreg Roach
257*a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
258*a5f003cfSGreg Roach
259*a5f003cfSGreg Roach        Validator::queryParams($request)->tree('no-tree');
260*a5f003cfSGreg Roach    }
261*a5f003cfSGreg Roach
262*a5f003cfSGreg Roach    /**
263*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::treeOptional
264*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
265*a5f003cfSGreg Roach     */
266*a5f003cfSGreg Roach    public function testOptionalTreeParameter(): void
267*a5f003cfSGreg Roach    {
268*a5f003cfSGreg Roach        $tree = $this->createStub(Tree::class);
269*a5f003cfSGreg Roach
270*a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
271*a5f003cfSGreg Roach        $request
272*a5f003cfSGreg Roach            ->method('getQueryParams')
273*a5f003cfSGreg Roach            ->willReturn([
274*a5f003cfSGreg Roach                'valid-tree' => $tree,
275*a5f003cfSGreg Roach                'not-tree'   => '',
276*a5f003cfSGreg Roach            ]);
277*a5f003cfSGreg Roach
278*a5f003cfSGreg Roach        self::assertSame($tree, Validator::queryParams($request)->treeOptional('valid-tree'));
279*a5f003cfSGreg Roach        self::assertSame(null, Validator::queryParams($request)->treeOptional('missing-tree'));
280*a5f003cfSGreg Roach
281*a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
282*a5f003cfSGreg Roach
283*a5f003cfSGreg Roach        Validator::queryParams($request)->treeOptional('not-tree');
284*a5f003cfSGreg Roach    }
285*a5f003cfSGreg Roach
286*a5f003cfSGreg Roach    /**
287*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::user
288*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
289*a5f003cfSGreg Roach     */
290*a5f003cfSGreg Roach    public function testRequiredUserParameter(): void
291*a5f003cfSGreg Roach    {
292*a5f003cfSGreg Roach        $user = $this->createStub(UserInterface::class);
293*a5f003cfSGreg Roach
294*a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
295*a5f003cfSGreg Roach        $request
296*a5f003cfSGreg Roach            ->method('getQueryParams')
297*a5f003cfSGreg Roach            ->willReturn([
298*a5f003cfSGreg Roach                'valid-user' => $user,
299*a5f003cfSGreg Roach                'not-user'   => '',
300*a5f003cfSGreg Roach            ]);
301*a5f003cfSGreg Roach
302*a5f003cfSGreg Roach        self::assertSame($user, Validator::queryParams($request)->user('valid-user'));
303*a5f003cfSGreg Roach
304*a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
305*a5f003cfSGreg Roach
306*a5f003cfSGreg Roach        Validator::queryParams($request)->user('not-user');
307f6fdd746SJonathan Jaubart    }
308f6fdd746SJonathan Jaubart
309f6fdd746SJonathan Jaubart    /**
310f6fdd746SJonathan Jaubart     * @covers \Fisharebest\Webtrees\Validator::isBetween
311*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
312f6fdd746SJonathan Jaubart     */
313f6fdd746SJonathan Jaubart    public function testIsBetweenParameter(): void
314f6fdd746SJonathan Jaubart    {
315f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
316*a5f003cfSGreg Roach        $request
317*a5f003cfSGreg Roach            ->method('getQueryParams')
318*a5f003cfSGreg Roach            ->willReturn(['param' => '42', 'invalid' => '10', 'wrongtype' => 'not_integer']);
319f6fdd746SJonathan Jaubart
320*a5f003cfSGreg Roach        self::assertSame(42, Validator::queryParams($request)->isBetween(40, 45)->integer('param'));
321*a5f003cfSGreg Roach        self::assertSame(42, Validator::queryParams($request)->isBetween(40, 45)->integer('invalid', 42));
322*a5f003cfSGreg Roach        self::assertSame(42, Validator::queryParams($request)->isBetween(40, 45)->integer('wrongtype', 42));
323*a5f003cfSGreg Roach    }
324*a5f003cfSGreg Roach
325*a5f003cfSGreg Roach    /**
326*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::isInArray
327*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
328*a5f003cfSGreg Roach     */
329*a5f003cfSGreg Roach    public function testIsInArray(): void
330*a5f003cfSGreg Roach    {
331*a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
332*a5f003cfSGreg Roach        $request
333*a5f003cfSGreg Roach            ->method('getQueryParams')
334*a5f003cfSGreg Roach            ->willReturn(['param' => 'foo']);
335*a5f003cfSGreg Roach
336*a5f003cfSGreg Roach        self::assertSame('foo', Validator::queryParams($request)->isInArray(['foo', 'bar'])->string('param'));
337*a5f003cfSGreg Roach
338*a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
339*a5f003cfSGreg Roach
340*a5f003cfSGreg Roach        Validator::queryParams($request)->isInArray(['baz'])->string('param');
341*a5f003cfSGreg Roach    }
342*a5f003cfSGreg Roach
343*a5f003cfSGreg Roach    /**
344*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::isInArrayKeys
345*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
346*a5f003cfSGreg Roach     */
347*a5f003cfSGreg Roach    public function testIsInArrayKeys(): void
348*a5f003cfSGreg Roach    {
349*a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
350*a5f003cfSGreg Roach        $request
351*a5f003cfSGreg Roach            ->method('getQueryParams')
352*a5f003cfSGreg Roach            ->willReturn(['param' => 'foo']);
353*a5f003cfSGreg Roach
354*a5f003cfSGreg Roach        self::assertSame('foo', Validator::queryParams($request)->isInArrayKeys(['foo' => 1, 'bar' => 2])->string('param'));
355*a5f003cfSGreg Roach
356*a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
357*a5f003cfSGreg Roach
358*a5f003cfSGreg Roach        Validator::queryParams($request)->isInArrayKeys(['baz' => 3])->string('param');
359*a5f003cfSGreg Roach    }
360*a5f003cfSGreg Roach
361*a5f003cfSGreg Roach    /**
362*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::isNotEmpty
363*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
364*a5f003cfSGreg Roach     */
365*a5f003cfSGreg Roach    public function testIsNotEmpty(): void
366*a5f003cfSGreg Roach    {
367*a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
368*a5f003cfSGreg Roach        $request
369*a5f003cfSGreg Roach            ->method('getQueryParams')
370*a5f003cfSGreg Roach            ->willReturn(['empty' => '', 'not-empty' => 'foo']);
371*a5f003cfSGreg Roach
372*a5f003cfSGreg Roach        self::assertSame('foo', Validator::queryParams($request)->isNotEmpty()->string('not-empty'));
373*a5f003cfSGreg Roach
374*a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
375*a5f003cfSGreg Roach
376*a5f003cfSGreg Roach        Validator::queryParams($request)->isNotEmpty()->string('empty');
377*a5f003cfSGreg Roach    }
378*a5f003cfSGreg Roach
379*a5f003cfSGreg Roach    /**
380*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::isTag
381*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
382*a5f003cfSGreg Roach     */
383*a5f003cfSGreg Roach    public function testIsTagParameter(): void
384*a5f003cfSGreg Roach    {
385*a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
386*a5f003cfSGreg Roach        $request
387*a5f003cfSGreg Roach            ->method('getQueryParams')
388*a5f003cfSGreg Roach            ->willReturn(['valid' => 'BIRT', 'invalid' => '@X1@']);
389*a5f003cfSGreg Roach
390*a5f003cfSGreg Roach        self::assertSame('BIRT', Validator::queryParams($request)->isTag()->string('valid'));
391*a5f003cfSGreg Roach
392*a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
393*a5f003cfSGreg Roach
394*a5f003cfSGreg Roach        Validator::queryParams($request)->isTag()->string('invalid');
395f6fdd746SJonathan Jaubart    }
396f6fdd746SJonathan Jaubart
397f6fdd746SJonathan Jaubart    /**
398f6fdd746SJonathan Jaubart     * @covers \Fisharebest\Webtrees\Validator::isXref
399*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
400f6fdd746SJonathan Jaubart     */
401f6fdd746SJonathan Jaubart    public function testIsXrefParameter(): void
402f6fdd746SJonathan Jaubart    {
403f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
404*a5f003cfSGreg Roach        $request
405*a5f003cfSGreg Roach            ->method('getQueryParams')
406*a5f003cfSGreg Roach            ->willReturn(['valid' => 'X1', 'invalid' => '@X1@', 'valid-array' => ['X1'], 'invalid-array' => ['@X1@']]);
407f6fdd746SJonathan Jaubart
408*a5f003cfSGreg Roach        self::assertSame('X1', Validator::queryParams($request)->isXref()->string('valid'));
409*a5f003cfSGreg Roach        self::assertSame(['X1'], Validator::queryParams($request)->isXref()->array('valid-array'));
410*a5f003cfSGreg Roach        self::assertSame([], Validator::queryParams($request)->isXref()->array('invalid-array'));
4111e60ebf4SGreg Roach
4121e60ebf4SGreg Roach        $this->expectException(HttpBadRequestException::class);
413*a5f003cfSGreg Roach
414*a5f003cfSGreg Roach        Validator::queryParams($request)->isXref()->string('invalid');
415f6fdd746SJonathan Jaubart    }
416f6fdd746SJonathan Jaubart
417f6fdd746SJonathan Jaubart    /**
418f6fdd746SJonathan Jaubart     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
419*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
420f6fdd746SJonathan Jaubart     */
421f6fdd746SJonathan Jaubart    public function testIsLocalUrlParameter(): void
422f6fdd746SJonathan Jaubart    {
423f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
424*a5f003cfSGreg Roach        $request
425*a5f003cfSGreg Roach            ->method('getAttribute')
426*a5f003cfSGreg Roach            ->with('base_url')->willReturn('http://example.local/wt');
427*a5f003cfSGreg Roach        $request
428*a5f003cfSGreg Roach            ->method('getQueryParams')
429*a5f003cfSGreg Roach            ->willReturn(['param' => 'http://example.local/wt/page', 'noscheme' => '//example.local/wt/page']);
430f507cef9SGreg Roach
4311e60ebf4SGreg Roach
432*a5f003cfSGreg Roach        self::assertSame('http://example.local/wt/page', Validator::queryParams($request)->isLocalUrl()->string('param'));
433*a5f003cfSGreg Roach        self::assertSame('//example.local/wt/page', Validator::queryParams($request)->isLocalUrl()->string('noscheme'));
4341e60ebf4SGreg Roach    }
4351e60ebf4SGreg Roach
4361e60ebf4SGreg Roach    /**
4371e60ebf4SGreg Roach     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
438*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
4391e60ebf4SGreg Roach     */
4401e60ebf4SGreg Roach    public function testIsLocalUrlParameterWrongScheme(): void
4411e60ebf4SGreg Roach    {
442f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
443*a5f003cfSGreg Roach        $request
444*a5f003cfSGreg Roach            ->method('getAttribute')
445*a5f003cfSGreg Roach            ->with('base_url')
446*a5f003cfSGreg Roach            ->willReturn('http://example.local/wt');
447*a5f003cfSGreg Roach        $request
448*a5f003cfSGreg Roach            ->method('getQueryParams')
449*a5f003cfSGreg Roach            ->willReturn(['https' => 'https://example.local/wt/page']);
4501e60ebf4SGreg Roach
4511e60ebf4SGreg Roach        $this->expectException(HttpBadRequestException::class);
452*a5f003cfSGreg Roach
453*a5f003cfSGreg Roach        Validator::queryParams($request)->isLocalUrl()->string('https');
4541e60ebf4SGreg Roach    }
4551e60ebf4SGreg Roach
4561e60ebf4SGreg Roach    /**
4571e60ebf4SGreg Roach     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
458*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
4591e60ebf4SGreg Roach     */
4601e60ebf4SGreg Roach    public function testIsLocalUrlParameterWrongDomain(): void
4611e60ebf4SGreg Roach    {
462f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
463*a5f003cfSGreg Roach        $request
464*a5f003cfSGreg Roach            ->method('getAttribute')
465*a5f003cfSGreg Roach            ->with('base_url')
466*a5f003cfSGreg Roach            ->willReturn('http://example.local/wt');
467*a5f003cfSGreg Roach        $request
468*a5f003cfSGreg Roach            ->method('getQueryParams')
469*a5f003cfSGreg Roach            ->willReturn(['invalid' => 'http://example.com/wt/page']);
4701e60ebf4SGreg Roach
4711e60ebf4SGreg Roach        $this->expectException(HttpBadRequestException::class);
472*a5f003cfSGreg Roach
473*a5f003cfSGreg Roach        Validator::queryParams($request)->isLocalUrl()->string('invalid');
4741e60ebf4SGreg Roach    }
4751e60ebf4SGreg Roach
4761e60ebf4SGreg Roach    /**
4771e60ebf4SGreg Roach     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
478*a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
4791e60ebf4SGreg Roach     */
4801e60ebf4SGreg Roach    public function testIsLocalUrlParameterWrongType(): void
4811e60ebf4SGreg Roach    {
482f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
483*a5f003cfSGreg Roach        $request
484*a5f003cfSGreg Roach            ->method('getQueryParams')
485*a5f003cfSGreg Roach            ->willReturn(['wrongtype' => ['42']]);
486f6fdd746SJonathan Jaubart
4871e60ebf4SGreg Roach        $this->expectException(HttpBadRequestException::class);
488f6fdd746SJonathan Jaubart
489*a5f003cfSGreg Roach        Validator::queryParams($request)->isLocalUrl()->isLocalUrl()->string('wrongtype');
490f6fdd746SJonathan Jaubart    }
491f6fdd746SJonathan Jaubart}
492