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