| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Json\FormatJson; |
| 4 |
use MediaWiki\Request\FauxRequest; |
| 5 |
use MediaWiki\Request\WebRequest; |
| 6 |
use MediaWiki\Status\Status; |
| 7 |
|
| 8 |
class MWRestrictionsTest extends MediaWikiUnitTestCase { |
| 9 |
|
| 10 |
|
| 11 |
protected static $restrictionsForChecks; |
| 12 |
|
| 13 |
public static function setUpBeforeClass(): void { |
| 14 |
parent::setUpBeforeClass(); |
| 15 |
self::$restrictionsForChecks = MWRestrictions::newFromArray( [ |
| 16 |
'IPAddresses' => [ |
| 17 |
'10.0.0.0/8', |
| 18 |
'172.16.0.0/12', |
| 19 |
'2001:db8::/33', |
| 20 |
] |
| 21 |
] ); |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public function testNewDefault() { |
| 29 |
$ret = MWRestrictions::newDefault(); |
| 30 |
$this->assertInstanceOf( MWRestrictions::class, $ret ); |
| 31 |
$this->assertSame( |
| 32 |
'{"IPAddresses":["0.0.0.0/0","::/0"]}', |
| 33 |
$ret->toJson() |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function testArray( $data, $expect ) { |
| 48 |
if ( $expect instanceof StatusValue ) { |
| 49 |
$ret = MWRestrictions::newFromArray( $data ); |
| 50 |
$this->assertInstanceOf( MWRestrictions::class, $ret ); |
| 51 |
$this->assertSame( $data, $ret->toArray() ); |
| 52 |
$this->assertStatusMessagesExactly( $expect, $ret->validity ); |
| 53 |
} else { |
| 54 |
try { |
| 55 |
MWRestrictions::newFromArray( $data ); |
| 56 |
$this->fail( 'Expected exception not thrown' ); |
| 57 |
} catch ( InvalidArgumentException $ex ) { |
| 58 |
$this->assertEquals( $expect, $ex ); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public static function provideArray() { |
| 64 |
return [ |
| 65 |
[ [ 'IPAddresses' => [] ], StatusValue::newGood() ], |
| 66 |
[ [ 'IPAddresses' => [ '127.0.0.1/32' ] ], StatusValue::newGood() ], |
| 67 |
[ |
| 68 |
[ 'IPAddresses' => [ '256.0.0.1/32' ] ], |
| 69 |
StatusValue::newFatal( 'restrictionsfield-badip', '256.0.0.1/32' ) |
| 70 |
], |
| 71 |
[ |
| 72 |
[ 'IPAddresses' => [ '127.0.0.1/32' ], 'Pages' => [ "Main Page", "Main Page/sandbox" ] ], |
| 73 |
StatusValue::newGood() |
| 74 |
], |
| 75 |
[ |
| 76 |
[ 'IPAddresses' => '127.0.0.1/32' ], |
| 77 |
new InvalidArgumentException( 'IPAddresses is not an array' ) |
| 78 |
], |
| 79 |
[ |
| 80 |
[], |
| 81 |
new InvalidArgumentException( 'Array is missing required keys: IPAddresses' ) |
| 82 |
] |
| 83 |
]; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function testJson( $json, $restrictions, $expect ) { |
| 98 |
if ( $expect instanceof StatusValue ) { |
| 99 |
$ret = MWRestrictions::newFromJson( $json ); |
| 100 |
$this->assertInstanceOf( MWRestrictions::class, $ret ); |
| 101 |
$this->assertSame( $restrictions, $ret->toArray() ); |
| 102 |
|
| 103 |
$this->assertSame( $json, $ret->toJson( false ) ); |
| 104 |
$this->assertSame( $json, (string)$ret ); |
| 105 |
|
| 106 |
$this->assertSame( |
| 107 |
FormatJson::encode( $restrictions, true, FormatJson::ALL_OK ), |
| 108 |
$ret->toJson( true ) |
| 109 |
); |
| 110 |
$this->assertStatusMessagesExactly( $expect, $ret->validity ); |
| 111 |
} else { |
| 112 |
try { |
| 113 |
MWRestrictions::newFromJson( $json ); |
| 114 |
$this->fail( 'Expected exception not thrown' ); |
| 115 |
} catch ( InvalidArgumentException $ex ) { |
| 116 |
$this->assertEquals( $expect, $ex ); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
public static function provideJson() { |
| 122 |
return [ |
| 123 |
[ |
| 124 |
'{"IPAddresses":[]}', |
| 125 |
[ 'IPAddresses' => [] ], |
| 126 |
StatusValue::newGood() |
| 127 |
], |
| 128 |
[ |
| 129 |
'{"IPAddresses":["127.0.0.1/32"]}', |
| 130 |
[ 'IPAddresses' => [ '127.0.0.1/32' ] ], |
| 131 |
StatusValue::newGood() |
| 132 |
], |
| 133 |
[ |
| 134 |
'{"IPAddresses":["127.0.0.1/32"],"Pages":["Main Page"]}', |
| 135 |
[ 'IPAddresses' => [ '127.0.0.1/32' ], 'Pages' => [ "Main Page" ] ], |
| 136 |
StatusValue::newGood() |
| 137 |
], |
| 138 |
[ |
| 139 |
'{"IPAddresses":["256.0.0.1/32"]}', |
| 140 |
[ 'IPAddresses' => [ '256.0.0.1/32' ] ], |
| 141 |
StatusValue::newFatal( 'restrictionsfield-badip', '256.0.0.1/32' ) |
| 142 |
], |
| 143 |
[ |
| 144 |
'{"IPAddresses":"127.0.0.1/32"}', |
| 145 |
null, |
| 146 |
new InvalidArgumentException( 'IPAddresses is not an array' ) |
| 147 |
], |
| 148 |
[ |
| 149 |
'{}', |
| 150 |
null, |
| 151 |
new InvalidArgumentException( 'Array is missing required keys: IPAddresses' ) |
| 152 |
], |
| 153 |
[ |
| 154 |
'{"IPAddresses":[]', |
| 155 |
null, |
| 156 |
new InvalidArgumentException( 'Invalid restrictions JSON' ) |
| 157 |
], |
| 158 |
[ |
| 159 |
'"IPAddresses"', |
| 160 |
null, |
| 161 |
new InvalidArgumentException( 'Invalid restrictions JSON' ) |
| 162 |
], |
| 163 |
]; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public function testCheckIP( $ip, $pass ) { |
| 173 |
$this->assertSame( $pass, self::$restrictionsForChecks->checkIP( $ip ) ); |
| 174 |
} |
| 175 |
|
| 176 |
public static function provideCheckIP() { |
| 177 |
return [ |
| 178 |
[ '10.0.0.1', true ], |
| 179 |
[ '172.16.0.0', true ], |
| 180 |
[ '192.0.2.1', false ], |
| 181 |
[ '2001:db8:1::', true ], |
| 182 |
[ '2001:0db8:0000:0000:0000:0000:0000:0000', true ], |
| 183 |
[ '2001:0DB8:8000::', false ], |
| 184 |
]; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public function testCheck( $request, $expect ) { |
| 194 |
$this->assertEquals( $expect, self::$restrictionsForChecks->check( $request ) ); |
| 195 |
} |
| 196 |
|
| 197 |
public function provideCheck() { |
| 198 |
$ret = []; |
| 199 |
|
| 200 |
$mockBuilder = $this->getMockBuilder( FauxRequest::class ) |
| 201 |
->onlyMethods( [ 'getIP' ] ); |
| 202 |
|
| 203 |
foreach ( self::provideCheckIP() as $checkIP ) { |
| 204 |
$ok = []; |
| 205 |
$request = $mockBuilder->getMock(); |
| 206 |
|
| 207 |
$request->method( 'getIP' ) |
| 208 |
->willReturn( $checkIP[0] ); |
| 209 |
$ok['ip'] = $checkIP[1]; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
$status = Status::newGood(); |
| 226 |
$status->setResult( $ok === array_filter( $ok ), $ok ); |
| 227 |
$ret[] = [ $request, $status ]; |
| 228 |
} |
| 229 |
|
| 230 |
return $ret; |
| 231 |
} |
| 232 |
} |