xref: /webtrees/tests/app/ValidatorTest.php (revision f507cef925e69f3dc0d47102f965899785571595)
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
22f6fdd746SJonathan Jaubartuse Fisharebest\Webtrees\Http\Exceptions\HttpBadRequestException;
23f6fdd746SJonathan Jaubartuse LogicException;
24*f507cef9SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
25f6fdd746SJonathan Jaubart
26f6fdd746SJonathan Jaubart/**
27f6fdd746SJonathan Jaubart * Test harness for the class Validator
28f6fdd746SJonathan Jaubart */
29f6fdd746SJonathan Jaubartclass ValidatorTest extends TestCase
30f6fdd746SJonathan Jaubart{
31f6fdd746SJonathan Jaubart    /**
32b55cbc6bSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::array
33f6fdd746SJonathan Jaubart     */
34f6fdd746SJonathan Jaubart    public function testRequiredArrayParameter(): void
35f6fdd746SJonathan Jaubart    {
36*f507cef9SGreg Roach        $request    = $this->createStub(ServerRequestInterface::class);
37f6fdd746SJonathan Jaubart        $parameters = ['param' => ['test'], 'invalid' => 'not_array'];
38*f507cef9SGreg Roach        $validator  = new Validator($parameters, $request);
39f6fdd746SJonathan Jaubart
40b55cbc6bSGreg Roach        self::assertSame(['test'], $validator->array('param'));
41f6fdd746SJonathan Jaubart
42f6fdd746SJonathan Jaubart        $this->expectException(HttpBadRequestException::class);
43b55cbc6bSGreg Roach        $validator->array('invalid');
44f6fdd746SJonathan Jaubart    }
45f6fdd746SJonathan Jaubart
46f6fdd746SJonathan Jaubart    /**
47b55cbc6bSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::integer
48f6fdd746SJonathan Jaubart     */
49f6fdd746SJonathan Jaubart    public function testRequiredIntegerParameter(): void
50f6fdd746SJonathan Jaubart    {
51*f507cef9SGreg Roach        $request    = $this->createStub(ServerRequestInterface::class);
52f6fdd746SJonathan Jaubart        $parameters = ['param' => '42', 'invalid' => 'not_int'];
53*f507cef9SGreg Roach        $validator  = new Validator($parameters, $request);
54f6fdd746SJonathan Jaubart
55b55cbc6bSGreg Roach        self::assertSame(42, $validator->integer('param'));
56f6fdd746SJonathan Jaubart
57f6fdd746SJonathan Jaubart        $this->expectException(HttpBadRequestException::class);
58b55cbc6bSGreg Roach        $validator->integer('invalid');
59f6fdd746SJonathan Jaubart    }
60f6fdd746SJonathan Jaubart
61f6fdd746SJonathan Jaubart    /**
62b55cbc6bSGreg Roach     * @covers \Fisharebest\Webtrees\Validator::string
63f6fdd746SJonathan Jaubart     */
64f6fdd746SJonathan Jaubart    public function testRequiredStringParameter(): void
65f6fdd746SJonathan Jaubart    {
66*f507cef9SGreg Roach        $request    = $this->createStub(ServerRequestInterface::class);
67f6fdd746SJonathan Jaubart        $parameters = ['param' => 'test', 'invalid' => ['not_string']];
68*f507cef9SGreg Roach        $validator  = new Validator($parameters, $request);
69f6fdd746SJonathan Jaubart
70b55cbc6bSGreg Roach        self::assertSame('test', $validator->string('param'));
71f6fdd746SJonathan Jaubart
72f6fdd746SJonathan Jaubart        $this->expectException(HttpBadRequestException::class);
73b55cbc6bSGreg Roach        $validator->string('invalid');
74f6fdd746SJonathan Jaubart    }
75f6fdd746SJonathan Jaubart
76f6fdd746SJonathan Jaubart    /**
77f6fdd746SJonathan Jaubart     * @covers \Fisharebest\Webtrees\Validator::isBetween
78f6fdd746SJonathan Jaubart     */
79f6fdd746SJonathan Jaubart    public function testIsBetweenParameter(): void
80f6fdd746SJonathan Jaubart    {
81*f507cef9SGreg Roach        $request    = $this->createStub(ServerRequestInterface::class);
82*f507cef9SGreg Roach        $parameters = ['param' => '42', 'invalid' => '10', 'wrongtype' => 'not_integer'];
83*f507cef9SGreg Roach        $validator  = (new Validator($parameters, $request))->isBetween(40, 45);
84f6fdd746SJonathan Jaubart
851e60ebf4SGreg Roach        self::assertSame(42, $validator->integer('param'));
861e60ebf4SGreg Roach        self::assertSame(42, $validator->integer('invalid', 42));
871e60ebf4SGreg Roach        self::assertSame(42, $validator->integer('wrongtype', 42));
88f6fdd746SJonathan Jaubart    }
89f6fdd746SJonathan Jaubart
90f6fdd746SJonathan Jaubart    /**
91f6fdd746SJonathan Jaubart     * @covers \Fisharebest\Webtrees\Validator::isXref
92f6fdd746SJonathan Jaubart     */
93f6fdd746SJonathan Jaubart    public function testIsXrefParameter(): void
94f6fdd746SJonathan Jaubart    {
95*f507cef9SGreg Roach        $request    = $this->createStub(ServerRequestInterface::class);
96*f507cef9SGreg Roach        $parameters = ['param' => 'X1', 'invalid' => '@X1@'];
97*f507cef9SGreg Roach        $validator  = (new Validator($parameters, $request))->isXref();
98f6fdd746SJonathan Jaubart
991e60ebf4SGreg Roach        self::assertSame('X1', $validator->string('param'));
1001e60ebf4SGreg Roach
1011e60ebf4SGreg Roach        $this->expectException(HttpBadRequestException::class);
1021e60ebf4SGreg Roach        $validator->string('invalid');
103f6fdd746SJonathan Jaubart    }
104f6fdd746SJonathan Jaubart
105f6fdd746SJonathan Jaubart    /**
106f6fdd746SJonathan Jaubart     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
107f6fdd746SJonathan Jaubart     */
108f6fdd746SJonathan Jaubart    public function testIsLocalUrlParameter(): void
109f6fdd746SJonathan Jaubart    {
110*f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
111*f507cef9SGreg Roach        $request->method('getAttribute')->with('base_url')->willReturn('http://example.local/wt');
112*f507cef9SGreg Roach
113*f507cef9SGreg Roach        $parameters = ['param' => 'http://example.local/wt/page', 'noscheme' => '//example.local/wt/page'];
114*f507cef9SGreg Roach        $validator  = (new Validator($parameters, $request))->isLocalUrl();
1151e60ebf4SGreg Roach
1161e60ebf4SGreg Roach        self::assertSame('http://example.local/wt/page', $validator->string('param'));
1171e60ebf4SGreg Roach        self::assertSame('//example.local/wt/page', $validator->string('noscheme'));
1181e60ebf4SGreg Roach    }
1191e60ebf4SGreg Roach
1201e60ebf4SGreg Roach    /**
1211e60ebf4SGreg Roach     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
1221e60ebf4SGreg Roach     */
1231e60ebf4SGreg Roach    public function testIsLocalUrlParameterWrongScheme(): void
1241e60ebf4SGreg Roach    {
125*f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
126*f507cef9SGreg Roach        $request->method('getAttribute')->with('base_url')->willReturn('http://example.local/wt');
127*f507cef9SGreg Roach
128*f507cef9SGreg Roach        $parameters = ['https' => 'https://example.local/wt/page'];
129*f507cef9SGreg Roach        $validator  = (new Validator($parameters, $request))->isLocalUrl();
1301e60ebf4SGreg Roach
1311e60ebf4SGreg Roach        $this->expectException(HttpBadRequestException::class);
1321e60ebf4SGreg Roach        $validator->string('https');
1331e60ebf4SGreg Roach    }
1341e60ebf4SGreg Roach
1351e60ebf4SGreg Roach    /**
1361e60ebf4SGreg Roach     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
1371e60ebf4SGreg Roach     */
1381e60ebf4SGreg Roach    public function testIsLocalUrlParameterWrongDomain(): void
1391e60ebf4SGreg Roach    {
140*f507cef9SGreg Roach        $request = $this->createStub(ServerRequestInterface::class);
141*f507cef9SGreg Roach        $request->method('getAttribute')->with('base_url')->willReturn('http://example.local/wt');
142*f507cef9SGreg Roach
143*f507cef9SGreg Roach        $parameters = ['invalid' => 'http://example.com/wt/page'];
144*f507cef9SGreg Roach        $validator  = (new Validator($parameters, $request))->isLocalUrl();
1451e60ebf4SGreg Roach
1461e60ebf4SGreg Roach        $this->expectException(HttpBadRequestException::class);
1471e60ebf4SGreg Roach        $validator->string('invalid');
1481e60ebf4SGreg Roach    }
1491e60ebf4SGreg Roach
1501e60ebf4SGreg Roach    /**
1511e60ebf4SGreg Roach     * @covers \Fisharebest\Webtrees\Validator::isLocalUrl
1521e60ebf4SGreg Roach     */
1531e60ebf4SGreg Roach    public function testIsLocalUrlParameterWrongType(): void
1541e60ebf4SGreg Roach    {
155*f507cef9SGreg Roach        $request    = $this->createStub(ServerRequestInterface::class);
156*f507cef9SGreg Roach        $parameters = ['wrongtype' => ['42']];
157*f507cef9SGreg Roach        $validator  = (new Validator($parameters, $request))->isLocalUrl();
158f6fdd746SJonathan Jaubart
1591e60ebf4SGreg Roach        $this->expectException(HttpBadRequestException::class);
1601e60ebf4SGreg Roach        $validator->string('wrongtype');
161f6fdd746SJonathan Jaubart    }
162f6fdd746SJonathan Jaubart
163f6fdd746SJonathan Jaubart    /**
164f6fdd746SJonathan Jaubart     * @covers \Fisharebest\Webtrees\Validator::__construct
165f6fdd746SJonathan Jaubart     * @covers \Fisharebest\Webtrees\Validator::parsedBody
166f6fdd746SJonathan Jaubart     */
167f6fdd746SJonathan Jaubart    public function testParsedBody(): void
168f6fdd746SJonathan Jaubart    {
169f6fdd746SJonathan Jaubart        $request = self::createRequest()->withParsedBody(['param' => 'test']);
1701e60ebf4SGreg Roach        self::assertSame('test', Validator::parsedBody($request)->string('param'));
1711e60ebf4SGreg Roach
1721e60ebf4SGreg Roach        $this->expectException(HttpBadRequestException::class);
1731e60ebf4SGreg Roach        $request = self::createRequest()->withQueryParams(['param' => 'test']);
1741e60ebf4SGreg Roach        Validator::parsedBody($request)->string('param');
175f6fdd746SJonathan Jaubart    }
176f6fdd746SJonathan Jaubart
177f6fdd746SJonathan Jaubart    /**
178f6fdd746SJonathan Jaubart     * @covers \Fisharebest\Webtrees\Validator::__construct
179f6fdd746SJonathan Jaubart     * @covers \Fisharebest\Webtrees\Validator::queryParams
180f6fdd746SJonathan Jaubart     */
181f6fdd746SJonathan Jaubart    public function testQueryParams(): void
182f6fdd746SJonathan Jaubart    {
183f6fdd746SJonathan Jaubart        $request = self::createRequest()->withQueryParams(['param' => 'test']);
1841e60ebf4SGreg Roach        self::assertSame('test', Validator::queryParams($request)->string('param'));
1851e60ebf4SGreg Roach
1861e60ebf4SGreg Roach        $this->expectException(HttpBadRequestException::class);
1871e60ebf4SGreg Roach        $request = self::createRequest()->withParsedBody(['param' => 'test']);
1881e60ebf4SGreg Roach        Validator::queryParams($request)->string('param');
189f6fdd746SJonathan Jaubart    }
190f6fdd746SJonathan Jaubart}
191