1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees; 21 22use Fisharebest\Webtrees\Http\Exceptions\HttpBadRequestException; 23use LogicException; 24 25/** 26 * Test harness for the class Validator 27 */ 28class ValidatorTest extends TestCase 29{ 30 /** 31 * @covers \Fisharebest\Webtrees\Validator::array 32 */ 33 public function testRequiredArrayParameter(): void 34 { 35 $parameters = ['param' => ['test'], 'invalid' => 'not_array']; 36 $validator = new Validator($parameters); 37 38 self::assertSame(['test'], $validator->array('param')); 39 40 $this->expectException(HttpBadRequestException::class); 41 $validator->array('invalid'); 42 } 43 44 /** 45 * @covers \Fisharebest\Webtrees\Validator::integer 46 */ 47 public function testRequiredIntegerParameter(): void 48 { 49 $parameters = ['param' => '42', 'invalid' => 'not_int']; 50 $validator = new Validator($parameters); 51 52 self::assertSame(42, $validator->integer('param')); 53 54 $this->expectException(HttpBadRequestException::class); 55 $validator->integer('invalid'); 56 } 57 58 /** 59 * @covers \Fisharebest\Webtrees\Validator::string 60 */ 61 public function testRequiredStringParameter(): void 62 { 63 $parameters = ['param' => 'test', 'invalid' => ['not_string']]; 64 $validator = new Validator($parameters); 65 66 self::assertSame('test', $validator->string('param')); 67 68 $this->expectException(HttpBadRequestException::class); 69 $validator->string('invalid'); 70 } 71 72 /** 73 * @covers \Fisharebest\Webtrees\Validator::isBetween 74 */ 75 public function testIsBetweenParameter(): void 76 { 77 $parameters = [ 78 'param' => '42', 79 'invalid' => '10', 80 'wrongtype' => 'not_integer', 81 ]; 82 $validator = (new Validator($parameters))->isBetween(40, 45); 83 84 self::assertSame(42, $validator->integer('param')); 85 self::assertSame(42, $validator->integer('invalid', 42)); 86 self::assertSame(42, $validator->integer('wrongtype', 42)); 87 } 88 89 /** 90 * @covers \Fisharebest\Webtrees\Validator::isXref 91 */ 92 public function testIsXrefParameter(): void 93 { 94 $parameters = [ 95 'param' => 'X1', 96 'invalid' => '@X1@', 97 ]; 98 $validator = (new Validator($parameters))->isXref(); 99 100 self::assertSame('X1', $validator->string('param')); 101 102 $this->expectException(HttpBadRequestException::class); 103 $validator->string('invalid'); 104 } 105 106 /** 107 * @covers \Fisharebest\Webtrees\Validator::isLocalUrl 108 */ 109 public function testIsLocalUrlParameter(): void 110 { 111 $parameters = [ 112 'param' => 'http://example.local/wt/page', 113 'noscheme' => '//example.local/wt/page', 114 ]; 115 $validator = (new Validator($parameters))->isLocalUrl('http://example.local/wt'); 116 117 self::assertSame('http://example.local/wt/page', $validator->string('param')); 118 self::assertSame('//example.local/wt/page', $validator->string('noscheme')); 119 } 120 121 /** 122 * @covers \Fisharebest\Webtrees\Validator::isLocalUrl 123 */ 124 public function testIsLocalUrlParameterWrongScheme(): void 125 { 126 $parameters = [ 127 'https' => 'https://example.local/wt/page', 128 ]; 129 $validator = (new Validator($parameters))->isLocalUrl('http://example.local/wt'); 130 131 $this->expectException(HttpBadRequestException::class); 132 $validator->string('https'); 133 } 134 135 /** 136 * @covers \Fisharebest\Webtrees\Validator::isLocalUrl 137 */ 138 public function testIsLocalUrlParameterWrongDomain(): void 139 { 140 $parameters = [ 141 'invalid' => 'http://example.com/wt/page', 142 ]; 143 $validator = (new Validator($parameters))->isLocalUrl('http://example.local/wt'); 144 145 $this->expectException(HttpBadRequestException::class); 146 $validator->string('invalid'); 147 } 148 149 /** 150 * @covers \Fisharebest\Webtrees\Validator::isLocalUrl 151 */ 152 public function testIsLocalUrlParameterWrongType(): void 153 { 154 $parameters = [ 155 'wrongtype' => ['42'] 156 ]; 157 $validator = (new Validator($parameters))->isLocalUrl('http://example.local/wt'); 158 159 $this->expectException(HttpBadRequestException::class); 160 $validator->string('wrongtype'); 161 } 162 163 /** 164 * @covers \Fisharebest\Webtrees\Validator::isLocalUrl 165 */ 166 public function testIsLocalUrlWithInvalidBaseUrl(): void 167 { 168 $this->expectException(LogicException::class); 169 (new Validator(['param' => 'test']))->isLocalUrl('http://:invalid.url/')->string('param'); 170 } 171 172 /** 173 * @covers \Fisharebest\Webtrees\Validator::__construct 174 * @covers \Fisharebest\Webtrees\Validator::parsedBody 175 */ 176 public function testParsedBody(): void 177 { 178 $request = self::createRequest()->withParsedBody(['param' => 'test']); 179 self::assertSame('test', Validator::parsedBody($request)->string('param')); 180 181 $this->expectException(HttpBadRequestException::class); 182 $request = self::createRequest()->withQueryParams(['param' => 'test']); 183 Validator::parsedBody($request)->string('param'); 184 } 185 186 /** 187 * @covers \Fisharebest\Webtrees\Validator::__construct 188 * @covers \Fisharebest\Webtrees\Validator::queryParams 189 */ 190 public function testQueryParams(): void 191 { 192 $request = self::createRequest()->withQueryParams(['param' => 'test']); 193 self::assertSame('test', Validator::queryParams($request)->string('param')); 194 195 $this->expectException(HttpBadRequestException::class); 196 $request = self::createRequest()->withParsedBody(['param' => 'test']); 197 Validator::queryParams($request)->string('param'); 198 } 199} 200