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; 23f507cef9SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 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 { 35f507cef9SGreg Roach $request = $this->createStub(ServerRequestInterface::class); 36f6fdd746SJonathan Jaubart $parameters = ['param' => ['test'], 'invalid' => 'not_array']; 37f507cef9SGreg Roach $validator = new Validator($parameters, $request); 38f6fdd746SJonathan Jaubart 39b55cbc6bSGreg Roach self::assertSame(['test'], $validator->array('param')); 40f6fdd746SJonathan Jaubart 41f6fdd746SJonathan Jaubart $this->expectException(HttpBadRequestException::class); 42b55cbc6bSGreg Roach $validator->array('invalid'); 43f6fdd746SJonathan Jaubart } 44f6fdd746SJonathan Jaubart 45f6fdd746SJonathan Jaubart /** 46b55cbc6bSGreg Roach * @covers \Fisharebest\Webtrees\Validator::integer 47f6fdd746SJonathan Jaubart */ 48f6fdd746SJonathan Jaubart public function testRequiredIntegerParameter(): void 49f6fdd746SJonathan Jaubart { 50f507cef9SGreg Roach $request = $this->createStub(ServerRequestInterface::class); 51*65625b93SGreg Roach $parameters = [ 52*65625b93SGreg Roach 'int_type_positive' => 42, 53*65625b93SGreg Roach 'int_type_negative' => -42, 54*65625b93SGreg Roach 'string_type_positive' => '42', 55*65625b93SGreg Roach 'string_type_negative' => '-42', 56*65625b93SGreg Roach 'invalid' => 'not_int', 57*65625b93SGreg Roach ]; 58f507cef9SGreg Roach $validator = new Validator($parameters, $request); 59f6fdd746SJonathan Jaubart 60*65625b93SGreg Roach self::assertSame(42, $validator->integer('int_type_positive')); 61*65625b93SGreg Roach self::assertSame(-42, $validator->integer('int_type_negative')); 62*65625b93SGreg Roach self::assertSame(42, $validator->integer('string_type_positive')); 63*65625b93SGreg Roach self::assertSame(-42, $validator->integer('string_type_negative')); 64f6fdd746SJonathan Jaubart 65f6fdd746SJonathan Jaubart $this->expectException(HttpBadRequestException::class); 66b55cbc6bSGreg Roach $validator->integer('invalid'); 67f6fdd746SJonathan Jaubart } 68f6fdd746SJonathan Jaubart 69f6fdd746SJonathan Jaubart /** 70b55cbc6bSGreg Roach * @covers \Fisharebest\Webtrees\Validator::string 71f6fdd746SJonathan Jaubart */ 72f6fdd746SJonathan Jaubart public function testRequiredStringParameter(): void 73f6fdd746SJonathan Jaubart { 74f507cef9SGreg Roach $request = $this->createStub(ServerRequestInterface::class); 75f6fdd746SJonathan Jaubart $parameters = ['param' => 'test', 'invalid' => ['not_string']]; 76f507cef9SGreg Roach $validator = new Validator($parameters, $request); 77f6fdd746SJonathan Jaubart 78b55cbc6bSGreg Roach self::assertSame('test', $validator->string('param')); 79f6fdd746SJonathan Jaubart 80f6fdd746SJonathan Jaubart $this->expectException(HttpBadRequestException::class); 81b55cbc6bSGreg Roach $validator->string('invalid'); 82f6fdd746SJonathan Jaubart } 83f6fdd746SJonathan Jaubart 84f6fdd746SJonathan Jaubart /** 85f6fdd746SJonathan Jaubart * @covers \Fisharebest\Webtrees\Validator::isBetween 86f6fdd746SJonathan Jaubart */ 87f6fdd746SJonathan Jaubart public function testIsBetweenParameter(): void 88f6fdd746SJonathan Jaubart { 89f507cef9SGreg Roach $request = $this->createStub(ServerRequestInterface::class); 90f507cef9SGreg Roach $parameters = ['param' => '42', 'invalid' => '10', 'wrongtype' => 'not_integer']; 91f507cef9SGreg Roach $validator = (new Validator($parameters, $request))->isBetween(40, 45); 92f6fdd746SJonathan Jaubart 931e60ebf4SGreg Roach self::assertSame(42, $validator->integer('param')); 941e60ebf4SGreg Roach self::assertSame(42, $validator->integer('invalid', 42)); 951e60ebf4SGreg Roach self::assertSame(42, $validator->integer('wrongtype', 42)); 96f6fdd746SJonathan Jaubart } 97f6fdd746SJonathan Jaubart 98f6fdd746SJonathan Jaubart /** 99f6fdd746SJonathan Jaubart * @covers \Fisharebest\Webtrees\Validator::isXref 100f6fdd746SJonathan Jaubart */ 101f6fdd746SJonathan Jaubart public function testIsXrefParameter(): void 102f6fdd746SJonathan Jaubart { 103f507cef9SGreg Roach $request = $this->createStub(ServerRequestInterface::class); 104f507cef9SGreg Roach $parameters = ['param' => 'X1', 'invalid' => '@X1@']; 105f507cef9SGreg Roach $validator = (new Validator($parameters, $request))->isXref(); 106f6fdd746SJonathan Jaubart 1071e60ebf4SGreg Roach self::assertSame('X1', $validator->string('param')); 1081e60ebf4SGreg Roach 1091e60ebf4SGreg Roach $this->expectException(HttpBadRequestException::class); 1101e60ebf4SGreg Roach $validator->string('invalid'); 111f6fdd746SJonathan Jaubart } 112f6fdd746SJonathan Jaubart 113f6fdd746SJonathan Jaubart /** 114f6fdd746SJonathan Jaubart * @covers \Fisharebest\Webtrees\Validator::isLocalUrl 115f6fdd746SJonathan Jaubart */ 116f6fdd746SJonathan Jaubart public function testIsLocalUrlParameter(): void 117f6fdd746SJonathan Jaubart { 118f507cef9SGreg Roach $request = $this->createStub(ServerRequestInterface::class); 119f507cef9SGreg Roach $request->method('getAttribute')->with('base_url')->willReturn('http://example.local/wt'); 120f507cef9SGreg Roach 121f507cef9SGreg Roach $parameters = ['param' => 'http://example.local/wt/page', 'noscheme' => '//example.local/wt/page']; 122f507cef9SGreg Roach $validator = (new Validator($parameters, $request))->isLocalUrl(); 1231e60ebf4SGreg Roach 1241e60ebf4SGreg Roach self::assertSame('http://example.local/wt/page', $validator->string('param')); 1251e60ebf4SGreg Roach self::assertSame('//example.local/wt/page', $validator->string('noscheme')); 1261e60ebf4SGreg Roach } 1271e60ebf4SGreg Roach 1281e60ebf4SGreg Roach /** 1291e60ebf4SGreg Roach * @covers \Fisharebest\Webtrees\Validator::isLocalUrl 1301e60ebf4SGreg Roach */ 1311e60ebf4SGreg Roach public function testIsLocalUrlParameterWrongScheme(): void 1321e60ebf4SGreg Roach { 133f507cef9SGreg Roach $request = $this->createStub(ServerRequestInterface::class); 134f507cef9SGreg Roach $request->method('getAttribute')->with('base_url')->willReturn('http://example.local/wt'); 135f507cef9SGreg Roach 136f507cef9SGreg Roach $parameters = ['https' => 'https://example.local/wt/page']; 137f507cef9SGreg Roach $validator = (new Validator($parameters, $request))->isLocalUrl(); 1381e60ebf4SGreg Roach 1391e60ebf4SGreg Roach $this->expectException(HttpBadRequestException::class); 1401e60ebf4SGreg Roach $validator->string('https'); 1411e60ebf4SGreg Roach } 1421e60ebf4SGreg Roach 1431e60ebf4SGreg Roach /** 1441e60ebf4SGreg Roach * @covers \Fisharebest\Webtrees\Validator::isLocalUrl 1451e60ebf4SGreg Roach */ 1461e60ebf4SGreg Roach public function testIsLocalUrlParameterWrongDomain(): void 1471e60ebf4SGreg Roach { 148f507cef9SGreg Roach $request = $this->createStub(ServerRequestInterface::class); 149f507cef9SGreg Roach $request->method('getAttribute')->with('base_url')->willReturn('http://example.local/wt'); 150f507cef9SGreg Roach 151f507cef9SGreg Roach $parameters = ['invalid' => 'http://example.com/wt/page']; 152f507cef9SGreg Roach $validator = (new Validator($parameters, $request))->isLocalUrl(); 1531e60ebf4SGreg Roach 1541e60ebf4SGreg Roach $this->expectException(HttpBadRequestException::class); 1551e60ebf4SGreg Roach $validator->string('invalid'); 1561e60ebf4SGreg Roach } 1571e60ebf4SGreg Roach 1581e60ebf4SGreg Roach /** 1591e60ebf4SGreg Roach * @covers \Fisharebest\Webtrees\Validator::isLocalUrl 1601e60ebf4SGreg Roach */ 1611e60ebf4SGreg Roach public function testIsLocalUrlParameterWrongType(): void 1621e60ebf4SGreg Roach { 163f507cef9SGreg Roach $request = $this->createStub(ServerRequestInterface::class); 164f507cef9SGreg Roach $parameters = ['wrongtype' => ['42']]; 165f507cef9SGreg Roach $validator = (new Validator($parameters, $request))->isLocalUrl(); 166f6fdd746SJonathan Jaubart 1671e60ebf4SGreg Roach $this->expectException(HttpBadRequestException::class); 1681e60ebf4SGreg Roach $validator->string('wrongtype'); 169f6fdd746SJonathan Jaubart } 170f6fdd746SJonathan Jaubart 171f6fdd746SJonathan Jaubart /** 172f6fdd746SJonathan Jaubart * @covers \Fisharebest\Webtrees\Validator::__construct 173f6fdd746SJonathan Jaubart * @covers \Fisharebest\Webtrees\Validator::parsedBody 174f6fdd746SJonathan Jaubart */ 175f6fdd746SJonathan Jaubart public function testParsedBody(): void 176f6fdd746SJonathan Jaubart { 177f6fdd746SJonathan Jaubart $request = self::createRequest()->withParsedBody(['param' => 'test']); 1781e60ebf4SGreg Roach self::assertSame('test', Validator::parsedBody($request)->string('param')); 1791e60ebf4SGreg Roach 1801e60ebf4SGreg Roach $this->expectException(HttpBadRequestException::class); 1811e60ebf4SGreg Roach $request = self::createRequest()->withQueryParams(['param' => 'test']); 1821e60ebf4SGreg Roach Validator::parsedBody($request)->string('param'); 183f6fdd746SJonathan Jaubart } 184f6fdd746SJonathan Jaubart 185f6fdd746SJonathan Jaubart /** 186f6fdd746SJonathan Jaubart * @covers \Fisharebest\Webtrees\Validator::__construct 187f6fdd746SJonathan Jaubart * @covers \Fisharebest\Webtrees\Validator::queryParams 188f6fdd746SJonathan Jaubart */ 189f6fdd746SJonathan Jaubart public function testQueryParams(): void 190f6fdd746SJonathan Jaubart { 191f6fdd746SJonathan Jaubart $request = self::createRequest()->withQueryParams(['param' => 'test']); 1921e60ebf4SGreg Roach self::assertSame('test', Validator::queryParams($request)->string('param')); 1931e60ebf4SGreg Roach 1941e60ebf4SGreg Roach $this->expectException(HttpBadRequestException::class); 1951e60ebf4SGreg Roach $request = self::createRequest()->withParsedBody(['param' => 'test']); 1961e60ebf4SGreg Roach Validator::queryParams($request)->string('param'); 197f6fdd746SJonathan Jaubart } 198f6fdd746SJonathan Jaubart} 199