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