xref: /webtrees/tests/app/ValidatorTest.php (revision d11be7027e34e3121be11cc025421873364403f9)
1f6fdd746SJonathan Jaubart<?php
2f6fdd746SJonathan Jaubart
3f6fdd746SJonathan Jaubart/**
4f6fdd746SJonathan Jaubart * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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
22a5f003cfSGreg Roachuse Aura\Router\Route;
23a5f003cfSGreg 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{
32a5f003cfSGreg Roach    /**
33a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::attributes
34a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
35a5f003cfSGreg Roach     */
36a5f003cfSGreg Roach    public function testAttributes(): void
37a5f003cfSGreg Roach    {
38a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
39a5f003cfSGreg Roach        $request
40a5f003cfSGreg Roach            ->method('getAttributes')
41a5f003cfSGreg Roach            ->willReturn(['param' => 'test']);
42a5f003cfSGreg Roach
43a5f003cfSGreg Roach        self::assertSame('test', Validator::attributes($request)->string('param'));
44a5f003cfSGreg Roach    }
45a5f003cfSGreg Roach
46a5f003cfSGreg Roach    /**
47a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::parsedBody
48a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
49a5f003cfSGreg Roach     */
50a5f003cfSGreg Roach    public function testParsedBody(): void
51a5f003cfSGreg Roach    {
52a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
53a5f003cfSGreg Roach        $request
54a5f003cfSGreg Roach            ->method('getParsedBody')
55a5f003cfSGreg Roach            ->willReturn(['param' => 'test']);
56a5f003cfSGreg Roach
57a5f003cfSGreg Roach        self::assertSame('test', Validator::parsedBody($request)->string('param'));
58a5f003cfSGreg Roach    }
59a5f003cfSGreg Roach
60a5f003cfSGreg Roach    /**
61a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::queryParams
62a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
63a5f003cfSGreg Roach     */
64a5f003cfSGreg Roach    public function testQueryParams(): void
65a5f003cfSGreg Roach    {
66a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
67a5f003cfSGreg Roach        $request
68a5f003cfSGreg Roach            ->method('getQueryParams')
69a5f003cfSGreg Roach            ->willReturn(['param' => 'test']);
70a5f003cfSGreg Roach
71a5f003cfSGreg Roach        self::assertSame('test', Validator::queryParams($request)->string('param'));
72a5f003cfSGreg Roach    }
73a5f003cfSGreg Roach
74a5f003cfSGreg Roach    /**
75a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::serverParams
76a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
77a5f003cfSGreg Roach     */
78a5f003cfSGreg Roach    public function testServerParams(): void
79a5f003cfSGreg Roach    {
80a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
81a5f003cfSGreg Roach        $request
82a5f003cfSGreg Roach            ->method('getServerParams')
83a5f003cfSGreg Roach            ->willReturn(['param' => 'test']);
84a5f003cfSGreg Roach
85a5f003cfSGreg Roach        self::assertSame('test', Validator::serverParams($request)->string('param'));
86a5f003cfSGreg Roach    }
87a5f003cfSGreg Roach
88a5f003cfSGreg Roach    /**
89a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::queryParams
90a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
91a5f003cfSGreg Roach     */
92a5f003cfSGreg Roach    public function testNonUTF8QueryParameterName(): void
93a5f003cfSGreg Roach    {
94a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
95a5f003cfSGreg Roach        $request
96a5f003cfSGreg Roach            ->method('getQueryParams')
97a5f003cfSGreg Roach            ->willReturn(["\xFF" => 'test']);
98a5f003cfSGreg Roach
99a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
100a5f003cfSGreg Roach
101a5f003cfSGreg Roach        Validator::queryParams($request);
102a5f003cfSGreg Roach    }
103a5f003cfSGreg Roach
104a5f003cfSGreg Roach    /**
105a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::queryParams
106a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
107a5f003cfSGreg Roach     */
108a5f003cfSGreg Roach    public function testNonUTF8QueryParameterValue(): void
109a5f003cfSGreg Roach    {
110a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
111a5f003cfSGreg Roach        $request
112a5f003cfSGreg Roach            ->method('getQueryParams')
113a5f003cfSGreg Roach            ->willReturn(['test' => "\xFF"]);
114a5f003cfSGreg Roach
115a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
116a5f003cfSGreg Roach
117a5f003cfSGreg Roach        Validator::queryParams($request);
118a5f003cfSGreg Roach    }
119a5f003cfSGreg Roach
120f6fdd746SJonathan Jaubart    /**
121b55cbc6bSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::array
122a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
123f6fdd746SJonathan Jaubart     */
124f6fdd746SJonathan Jaubart    public function testRequiredArrayParameter(): void
125f6fdd746SJonathan Jaubart    {
126f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
127a5f003cfSGreg Roach        $request
128a5f003cfSGreg Roach            ->method('getQueryParams')
129a5f003cfSGreg Roach            ->willReturn(['param' => ['test'], 'invalid' => 'not_array']);
130f6fdd746SJonathan Jaubart
131a5f003cfSGreg Roach
132a5f003cfSGreg Roach        self::assertSame(['test'], Validator::queryParams($request)->array('param'));
133f6fdd746SJonathan Jaubart
134f6fdd746SJonathan Jaubart        $this->expectException(HttpBadRequestException::class);
135a5f003cfSGreg Roach
136a5f003cfSGreg Roach        Validator::queryParams($request)->array('invalid');
137a5f003cfSGreg Roach    }
138a5f003cfSGreg Roach
139a5f003cfSGreg Roach    /**
140a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::boolean
141a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
142a5f003cfSGreg Roach     */
143a5f003cfSGreg Roach    public function testRequiredBooleanParameter(): void
144a5f003cfSGreg Roach    {
145a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
146a5f003cfSGreg Roach        $request
147a5f003cfSGreg Roach            ->method('getQueryParams')
148a5f003cfSGreg Roach            ->willReturn([
149a5f003cfSGreg Roach                'a'    => '1',
150a5f003cfSGreg Roach                'b'    => 'on',
151a5f003cfSGreg Roach                'c'    => true,
152a5f003cfSGreg Roach                'd' => '0',
153a5f003cfSGreg Roach                'e' => '',
154a5f003cfSGreg Roach                'f' => false,
155a5f003cfSGreg Roach            ]);
156a5f003cfSGreg Roach
157a5f003cfSGreg Roach        self::assertSame(true, Validator::queryParams($request)->boolean('a'));
158a5f003cfSGreg Roach        self::assertSame(true, Validator::queryParams($request)->boolean('b'));
159a5f003cfSGreg Roach        self::assertSame(true, Validator::queryParams($request)->boolean('c'));
160a5f003cfSGreg Roach        self::assertSame(false, Validator::queryParams($request)->boolean('d'));
161a5f003cfSGreg Roach        self::assertSame(false, Validator::queryParams($request)->boolean('e'));
162a5f003cfSGreg Roach        self::assertSame(false, Validator::queryParams($request)->boolean('f'));
163a5f003cfSGreg Roach        self::assertSame(false, Validator::queryParams($request)->boolean('g', false));
164a5f003cfSGreg Roach
165a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
166a5f003cfSGreg Roach
167a5f003cfSGreg Roach        Validator::queryParams($request)->boolean('h');
168f6fdd746SJonathan Jaubart    }
169f6fdd746SJonathan Jaubart
170f6fdd746SJonathan Jaubart    /**
171b55cbc6bSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::integer
172a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
173f6fdd746SJonathan Jaubart     */
174f6fdd746SJonathan Jaubart    public function testRequiredIntegerParameter(): void
175f6fdd746SJonathan Jaubart    {
176f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
177a5f003cfSGreg Roach        $request
178a5f003cfSGreg Roach            ->method('getQueryParams')
179a5f003cfSGreg Roach            ->willReturn([
18065625b93SGreg Roach                'int_type_positive'    => 42,
18165625b93SGreg Roach                'int_type_negative'    => -42,
18265625b93SGreg Roach                'string_type_positive' => '42',
18365625b93SGreg Roach                'string_type_negative' => '-42',
18465625b93SGreg Roach                'invalid'              => 'not_int',
185a5f003cfSGreg Roach            ]);
186f6fdd746SJonathan Jaubart
187a5f003cfSGreg Roach        self::assertSame(42, Validator::queryParams($request)->integer('int_type_positive'));
188a5f003cfSGreg Roach        self::assertSame(-42, Validator::queryParams($request)->integer('int_type_negative'));
189a5f003cfSGreg Roach        self::assertSame(42, Validator::queryParams($request)->integer('string_type_positive'));
190a5f003cfSGreg Roach        self::assertSame(-42, Validator::queryParams($request)->integer('string_type_negative'));
191f6fdd746SJonathan Jaubart
192f6fdd746SJonathan Jaubart        $this->expectException(HttpBadRequestException::class);
193a5f003cfSGreg Roach
194a5f003cfSGreg Roach        Validator::queryParams($request)->integer('invalid');
195a5f003cfSGreg Roach    }
196a5f003cfSGreg Roach
197a5f003cfSGreg Roach    /**
198a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::route
199a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
200a5f003cfSGreg Roach     */
201a5f003cfSGreg Roach    public function testRequiredRouteParameter(): void
202a5f003cfSGreg Roach    {
203a5f003cfSGreg Roach        $route = $this->createStub(Route::class);
204a5f003cfSGreg Roach
205a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
206a5f003cfSGreg Roach        $request
207a5f003cfSGreg Roach            ->method('getQueryParams')
208a5f003cfSGreg Roach            ->willReturn([
209a5f003cfSGreg Roach                'valid-route' => $route,
210a5f003cfSGreg Roach                'not-route'   => '',
211a5f003cfSGreg Roach            ]);
212a5f003cfSGreg Roach
213a5f003cfSGreg Roach        self::assertSame($route, Validator::queryParams($request)->route('valid-route'));
214a5f003cfSGreg Roach
215a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
216a5f003cfSGreg Roach
217a5f003cfSGreg Roach        Validator::queryParams($request)->route('not-route');
218f6fdd746SJonathan Jaubart    }
219f6fdd746SJonathan Jaubart
220f6fdd746SJonathan Jaubart    /**
221b55cbc6bSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::string
222a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
223f6fdd746SJonathan Jaubart     */
224f6fdd746SJonathan Jaubart    public function testRequiredStringParameter(): void
225f6fdd746SJonathan Jaubart    {
226f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
227a5f003cfSGreg Roach        $request
228a5f003cfSGreg Roach            ->method('getQueryParams')
229a5f003cfSGreg Roach            ->willReturn(['param' => 'test', 'invalid' => ['not_string']]);
230f6fdd746SJonathan Jaubart
231a5f003cfSGreg Roach        self::assertSame('test', Validator::queryParams($request)->string('param'));
232f6fdd746SJonathan Jaubart
233f6fdd746SJonathan Jaubart        $this->expectException(HttpBadRequestException::class);
234a5f003cfSGreg Roach
235a5f003cfSGreg Roach        Validator::queryParams($request)->string('invalid');
236a5f003cfSGreg Roach    }
237a5f003cfSGreg Roach
238a5f003cfSGreg Roach    /**
239a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::tree
240a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
241a5f003cfSGreg Roach     */
242a5f003cfSGreg Roach    public function testRequiredTreeParameter(): void
243a5f003cfSGreg Roach    {
244a5f003cfSGreg Roach        $tree = $this->createStub(Tree::class);
245a5f003cfSGreg Roach
246a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
247a5f003cfSGreg Roach        $request
248a5f003cfSGreg Roach            ->method('getQueryParams')
249a5f003cfSGreg Roach            ->willReturn([
250a5f003cfSGreg Roach                'valid-tree' => $tree,
251a5f003cfSGreg Roach                'not-tree'   => '',
252a5f003cfSGreg Roach            ]);
253a5f003cfSGreg Roach
254a5f003cfSGreg Roach        self::assertSame($tree, Validator::queryParams($request)->tree('valid-tree'));
255a5f003cfSGreg Roach
256a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
257a5f003cfSGreg Roach
258a5f003cfSGreg Roach        Validator::queryParams($request)->tree('no-tree');
259a5f003cfSGreg Roach    }
260a5f003cfSGreg Roach
261a5f003cfSGreg Roach    /**
262a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::treeOptional
263a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
264a5f003cfSGreg Roach     */
265a5f003cfSGreg Roach    public function testOptionalTreeParameter(): void
266a5f003cfSGreg Roach    {
267a5f003cfSGreg Roach        $tree = $this->createStub(Tree::class);
268a5f003cfSGreg Roach
269a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
270a5f003cfSGreg Roach        $request
271a5f003cfSGreg Roach            ->method('getQueryParams')
272a5f003cfSGreg Roach            ->willReturn([
273a5f003cfSGreg Roach                'valid-tree' => $tree,
274a5f003cfSGreg Roach                'not-tree'   => '',
275a5f003cfSGreg Roach            ]);
276a5f003cfSGreg Roach
277a5f003cfSGreg Roach        self::assertSame($tree, Validator::queryParams($request)->treeOptional('valid-tree'));
278a5f003cfSGreg Roach        self::assertSame(null, Validator::queryParams($request)->treeOptional('missing-tree'));
279a5f003cfSGreg Roach
280a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
281a5f003cfSGreg Roach
282a5f003cfSGreg Roach        Validator::queryParams($request)->treeOptional('not-tree');
283a5f003cfSGreg Roach    }
284a5f003cfSGreg Roach
285a5f003cfSGreg Roach    /**
286a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::user
287a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
288a5f003cfSGreg Roach     */
289a5f003cfSGreg Roach    public function testRequiredUserParameter(): void
290a5f003cfSGreg Roach    {
291a5f003cfSGreg Roach        $user = $this->createStub(UserInterface::class);
292a5f003cfSGreg Roach
293a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
294a5f003cfSGreg Roach        $request
295a5f003cfSGreg Roach            ->method('getQueryParams')
296a5f003cfSGreg Roach            ->willReturn([
297a5f003cfSGreg Roach                'valid-user' => $user,
298a5f003cfSGreg Roach                'not-user'   => '',
299a5f003cfSGreg Roach            ]);
300a5f003cfSGreg Roach
301a5f003cfSGreg Roach        self::assertSame($user, Validator::queryParams($request)->user('valid-user'));
302a5f003cfSGreg Roach
303a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
304a5f003cfSGreg Roach
305a5f003cfSGreg Roach        Validator::queryParams($request)->user('not-user');
306f6fdd746SJonathan Jaubart    }
307f6fdd746SJonathan Jaubart
308f6fdd746SJonathan Jaubart    /**
309f6fdd746SJonathan Jaubart     * @covers \Fisharebest\Webtrees\Validator::isBetween
310a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
311f6fdd746SJonathan Jaubart     */
312f6fdd746SJonathan Jaubart    public function testIsBetweenParameter(): void
313f6fdd746SJonathan Jaubart    {
314f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
315a5f003cfSGreg Roach        $request
316a5f003cfSGreg Roach            ->method('getQueryParams')
317a5f003cfSGreg Roach            ->willReturn(['param' => '42', 'invalid' => '10', 'wrongtype' => 'not_integer']);
318f6fdd746SJonathan Jaubart
319a5f003cfSGreg Roach        self::assertSame(42, Validator::queryParams($request)->isBetween(40, 45)->integer('param'));
320a5f003cfSGreg Roach        self::assertSame(42, Validator::queryParams($request)->isBetween(40, 45)->integer('invalid', 42));
321a5f003cfSGreg Roach        self::assertSame(42, Validator::queryParams($request)->isBetween(40, 45)->integer('wrongtype', 42));
322a5f003cfSGreg Roach    }
323a5f003cfSGreg Roach
324a5f003cfSGreg Roach    /**
325a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::isInArray
326a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
327a5f003cfSGreg Roach     */
328a5f003cfSGreg Roach    public function testIsInArray(): void
329a5f003cfSGreg Roach    {
330a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
331a5f003cfSGreg Roach        $request
332a5f003cfSGreg Roach            ->method('getQueryParams')
333a5f003cfSGreg Roach            ->willReturn(['param' => 'foo']);
334a5f003cfSGreg Roach
335a5f003cfSGreg Roach        self::assertSame('foo', Validator::queryParams($request)->isInArray(['foo', 'bar'])->string('param'));
336a5f003cfSGreg Roach
337a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
338a5f003cfSGreg Roach
339a5f003cfSGreg Roach        Validator::queryParams($request)->isInArray(['baz'])->string('param');
340a5f003cfSGreg Roach    }
341a5f003cfSGreg Roach
342a5f003cfSGreg Roach    /**
343a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::isInArrayKeys
344a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
345a5f003cfSGreg Roach     */
346a5f003cfSGreg Roach    public function testIsInArrayKeys(): void
347a5f003cfSGreg Roach    {
348a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
349a5f003cfSGreg Roach        $request
350a5f003cfSGreg Roach            ->method('getQueryParams')
351a5f003cfSGreg Roach            ->willReturn(['param' => 'foo']);
352a5f003cfSGreg Roach
353a5f003cfSGreg Roach        self::assertSame('foo', Validator::queryParams($request)->isInArrayKeys(['foo' => 1, 'bar' => 2])->string('param'));
354a5f003cfSGreg Roach
355a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
356a5f003cfSGreg Roach
357a5f003cfSGreg Roach        Validator::queryParams($request)->isInArrayKeys(['baz' => 3])->string('param');
358a5f003cfSGreg Roach    }
359a5f003cfSGreg Roach
360a5f003cfSGreg Roach    /**
361a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::isNotEmpty
362a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
363a5f003cfSGreg Roach     */
364a5f003cfSGreg Roach    public function testIsNotEmpty(): void
365a5f003cfSGreg Roach    {
366a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
367a5f003cfSGreg Roach        $request
368a5f003cfSGreg Roach            ->method('getQueryParams')
369a5f003cfSGreg Roach            ->willReturn(['empty' => '', 'not-empty' => 'foo']);
370a5f003cfSGreg Roach
371a5f003cfSGreg Roach        self::assertSame('foo', Validator::queryParams($request)->isNotEmpty()->string('not-empty'));
372a5f003cfSGreg Roach
373a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
374a5f003cfSGreg Roach
375a5f003cfSGreg Roach        Validator::queryParams($request)->isNotEmpty()->string('empty');
376a5f003cfSGreg Roach    }
377a5f003cfSGreg Roach
378a5f003cfSGreg Roach    /**
379a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::isTag
380a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
381a5f003cfSGreg Roach     */
382a5f003cfSGreg Roach    public function testIsTagParameter(): void
383a5f003cfSGreg Roach    {
384a5f003cfSGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
385a5f003cfSGreg Roach        $request
386a5f003cfSGreg Roach            ->method('getQueryParams')
387a5f003cfSGreg Roach            ->willReturn(['valid' => 'BIRT', 'invalid' => '@X1@']);
388a5f003cfSGreg Roach
389a5f003cfSGreg Roach        self::assertSame('BIRT', Validator::queryParams($request)->isTag()->string('valid'));
390a5f003cfSGreg Roach
391a5f003cfSGreg Roach        $this->expectException(HttpBadRequestException::class);
392a5f003cfSGreg Roach
393a5f003cfSGreg Roach        Validator::queryParams($request)->isTag()->string('invalid');
394f6fdd746SJonathan Jaubart    }
395f6fdd746SJonathan Jaubart
396f6fdd746SJonathan Jaubart    /**
397f6fdd746SJonathan Jaubart     * @covers \Fisharebest\Webtrees\Validator::isXref
398a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
399f6fdd746SJonathan Jaubart     */
400f6fdd746SJonathan Jaubart    public function testIsXrefParameter(): void
401f6fdd746SJonathan Jaubart    {
402f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
403a5f003cfSGreg Roach        $request
404a5f003cfSGreg Roach            ->method('getQueryParams')
405a5f003cfSGreg Roach            ->willReturn(['valid' => 'X1', 'invalid' => '@X1@', 'valid-array' => ['X1'], 'invalid-array' => ['@X1@']]);
406f6fdd746SJonathan Jaubart
407a5f003cfSGreg Roach        self::assertSame('X1', Validator::queryParams($request)->isXref()->string('valid'));
408a5f003cfSGreg Roach        self::assertSame(['X1'], Validator::queryParams($request)->isXref()->array('valid-array'));
409a5f003cfSGreg Roach        self::assertSame([], Validator::queryParams($request)->isXref()->array('invalid-array'));
4101e60ebf4SGreg Roach
4111e60ebf4SGreg Roach        $this->expectException(HttpBadRequestException::class);
412a5f003cfSGreg Roach
413a5f003cfSGreg Roach        Validator::queryParams($request)->isXref()->string('invalid');
414f6fdd746SJonathan Jaubart    }
415f6fdd746SJonathan Jaubart
416f6fdd746SJonathan Jaubart    /**
417f6fdd746SJonathan Jaubart     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
418a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
419f6fdd746SJonathan Jaubart     */
420f6fdd746SJonathan Jaubart    public function testIsLocalUrlParameter(): void
421f6fdd746SJonathan Jaubart    {
422f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
423a5f003cfSGreg Roach        $request
424a5f003cfSGreg Roach            ->method('getAttribute')
425a5f003cfSGreg Roach            ->with('base_url')->willReturn('http://example.local/wt');
426a5f003cfSGreg Roach        $request
427a5f003cfSGreg Roach            ->method('getQueryParams')
428a5f003cfSGreg Roach            ->willReturn(['param' => 'http://example.local/wt/page', 'noscheme' => '//example.local/wt/page']);
429f507cef9SGreg Roach
4301e60ebf4SGreg Roach
431a5f003cfSGreg Roach        self::assertSame('http://example.local/wt/page', Validator::queryParams($request)->isLocalUrl()->string('param'));
432a5f003cfSGreg Roach        self::assertSame('//example.local/wt/page', Validator::queryParams($request)->isLocalUrl()->string('noscheme'));
4331e60ebf4SGreg Roach    }
4341e60ebf4SGreg Roach
4351e60ebf4SGreg Roach    /**
4361e60ebf4SGreg Roach     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
437a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
4381e60ebf4SGreg Roach     */
4391e60ebf4SGreg Roach    public function testIsLocalUrlParameterWrongScheme(): void
4401e60ebf4SGreg Roach    {
441f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
442a5f003cfSGreg Roach        $request
443a5f003cfSGreg Roach            ->method('getAttribute')
444a5f003cfSGreg Roach            ->with('base_url')
445a5f003cfSGreg Roach            ->willReturn('http://example.local/wt');
446a5f003cfSGreg Roach        $request
447a5f003cfSGreg Roach            ->method('getQueryParams')
448a5f003cfSGreg Roach            ->willReturn(['https' => 'https://example.local/wt/page']);
4491e60ebf4SGreg Roach
4501e60ebf4SGreg Roach        $this->expectException(HttpBadRequestException::class);
451a5f003cfSGreg Roach
452a5f003cfSGreg Roach        Validator::queryParams($request)->isLocalUrl()->string('https');
4531e60ebf4SGreg Roach    }
4541e60ebf4SGreg Roach
4551e60ebf4SGreg Roach    /**
4561e60ebf4SGreg Roach     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
457a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
4581e60ebf4SGreg Roach     */
4591e60ebf4SGreg Roach    public function testIsLocalUrlParameterWrongDomain(): void
4601e60ebf4SGreg Roach    {
461f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
462a5f003cfSGreg Roach        $request
463a5f003cfSGreg Roach            ->method('getAttribute')
464a5f003cfSGreg Roach            ->with('base_url')
465a5f003cfSGreg Roach            ->willReturn('http://example.local/wt');
466a5f003cfSGreg Roach        $request
467a5f003cfSGreg Roach            ->method('getQueryParams')
468a5f003cfSGreg Roach            ->willReturn(['invalid' => 'http://example.com/wt/page']);
4691e60ebf4SGreg Roach
4701e60ebf4SGreg Roach        $this->expectException(HttpBadRequestException::class);
471a5f003cfSGreg Roach
472a5f003cfSGreg Roach        Validator::queryParams($request)->isLocalUrl()->string('invalid');
4731e60ebf4SGreg Roach    }
4741e60ebf4SGreg Roach
4751e60ebf4SGreg Roach    /**
4761e60ebf4SGreg Roach     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
477a5f003cfSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::__construct
4781e60ebf4SGreg Roach     */
4791e60ebf4SGreg Roach    public function testIsLocalUrlParameterWrongType(): void
4801e60ebf4SGreg Roach    {
481f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
482a5f003cfSGreg Roach        $request
483a5f003cfSGreg Roach            ->method('getQueryParams')
484a5f003cfSGreg Roach            ->willReturn(['wrongtype' => ['42']]);
485f6fdd746SJonathan Jaubart
4861e60ebf4SGreg Roach        $this->expectException(HttpBadRequestException::class);
487f6fdd746SJonathan Jaubart
488a5f003cfSGreg Roach        Validator::queryParams($request)->isLocalUrl()->isLocalUrl()->string('wrongtype');
489f6fdd746SJonathan Jaubart    }
490f6fdd746SJonathan Jaubart}
491