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