1f6fdd746SJonathan Jaubart<?php 2f6fdd746SJonathan Jaubart 3f6fdd746SJonathan Jaubart/** 4f6fdd746SJonathan Jaubart * webtrees: online genealogy 5*5bfc6897SGreg 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; 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 841e60ebf4SGreg Roach self::assertSame(42, $validator->integer('param')); 851e60ebf4SGreg Roach self::assertSame(42, $validator->integer('invalid', 42)); 861e60ebf4SGreg 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 1001e60ebf4SGreg Roach self::assertSame('X1', $validator->string('param')); 1011e60ebf4SGreg Roach 1021e60ebf4SGreg Roach $this->expectException(HttpBadRequestException::class); 1031e60ebf4SGreg 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', 1141e60ebf4SGreg Roach ]; 1151e60ebf4SGreg Roach $validator = (new Validator($parameters))->isLocalUrl('http://example.local/wt'); 1161e60ebf4SGreg Roach 1171e60ebf4SGreg Roach self::assertSame('http://example.local/wt/page', $validator->string('param')); 1181e60ebf4SGreg Roach self::assertSame('//example.local/wt/page', $validator->string('noscheme')); 1191e60ebf4SGreg Roach } 1201e60ebf4SGreg Roach 1211e60ebf4SGreg Roach /** 1221e60ebf4SGreg Roach * @covers \Fisharebest\Webtrees\Validator::isLocalUrl 1231e60ebf4SGreg Roach */ 1241e60ebf4SGreg Roach public function testIsLocalUrlParameterWrongScheme(): void 1251e60ebf4SGreg Roach { 1261e60ebf4SGreg Roach $parameters = [ 127f6fdd746SJonathan Jaubart 'https' => 'https://example.local/wt/page', 1281e60ebf4SGreg Roach ]; 1291e60ebf4SGreg Roach $validator = (new Validator($parameters))->isLocalUrl('http://example.local/wt'); 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 { 1401e60ebf4SGreg Roach $parameters = [ 141f6fdd746SJonathan Jaubart 'invalid' => 'http://example.com/wt/page', 1421e60ebf4SGreg Roach ]; 1431e60ebf4SGreg Roach $validator = (new Validator($parameters))->isLocalUrl('http://example.local/wt'); 1441e60ebf4SGreg Roach 1451e60ebf4SGreg Roach $this->expectException(HttpBadRequestException::class); 1461e60ebf4SGreg Roach $validator->string('invalid'); 1471e60ebf4SGreg Roach } 1481e60ebf4SGreg Roach 1491e60ebf4SGreg Roach /** 1501e60ebf4SGreg Roach * @covers \Fisharebest\Webtrees\Validator::isLocalUrl 1511e60ebf4SGreg Roach */ 1521e60ebf4SGreg Roach public function testIsLocalUrlParameterWrongType(): void 1531e60ebf4SGreg Roach { 1541e60ebf4SGreg Roach $parameters = [ 1552b1a9a98SGreg Roach 'wrongtype' => ['42'] 156f6fdd746SJonathan Jaubart ]; 157f6fdd746SJonathan Jaubart $validator = (new Validator($parameters))->isLocalUrl('http://example.local/wt'); 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::isLocalUrl 165f6fdd746SJonathan Jaubart */ 166f6fdd746SJonathan Jaubart public function testIsLocalUrlWithInvalidBaseUrl(): void 167f6fdd746SJonathan Jaubart { 168f6fdd746SJonathan Jaubart $this->expectException(LogicException::class); 1691e60ebf4SGreg 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']); 1791e60ebf4SGreg Roach self::assertSame('test', Validator::parsedBody($request)->string('param')); 1801e60ebf4SGreg Roach 1811e60ebf4SGreg Roach $this->expectException(HttpBadRequestException::class); 1821e60ebf4SGreg Roach $request = self::createRequest()->withQueryParams(['param' => 'test']); 1831e60ebf4SGreg 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']); 1931e60ebf4SGreg Roach self::assertSame('test', Validator::queryParams($request)->string('param')); 1941e60ebf4SGreg Roach 1951e60ebf4SGreg Roach $this->expectException(HttpBadRequestException::class); 1961e60ebf4SGreg Roach $request = self::createRequest()->withParsedBody(['param' => 'test']); 1971e60ebf4SGreg Roach Validator::queryParams($request)->string('param'); 198f6fdd746SJonathan Jaubart } 199f6fdd746SJonathan Jaubart} 200